...

Source file src/github.com/prometheus/procfs/net_dev_test.go

Documentation: github.com/prometheus/procfs

     1  // Copyright 2018 The Prometheus Authors
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package procfs
    15  
    16  import (
    17  	"fmt"
    18  	"testing"
    19  )
    20  
    21  func TestNetDevParseLine(t *testing.T) {
    22  	tc := []string{"eth0", "eth0:1"}
    23  	for i := range tc {
    24  		rawLine := fmt.Sprintf(`  %v: 1 2 3    4    5     6          7         8 9  10    11    12    13     14       15          16`, tc[i])
    25  		have, err := NetDev{}.parseLine(rawLine)
    26  		if err != nil {
    27  			t.Fatal(err)
    28  		}
    29  		want := NetDevLine{tc[i], 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
    30  		if want != *have {
    31  			t.Errorf("want %v, have %v", want, have)
    32  		}
    33  	}
    34  
    35  }
    36  
    37  func TestNetDev(t *testing.T) {
    38  	fs, err := NewFS(procTestFixtures)
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  
    43  	netDev, err := fs.NetDev()
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  
    48  	lines := map[string]NetDevLine{
    49  		"vethf345468": {Name: "vethf345468", RxBytes: 648, RxPackets: 8, TxBytes: 438, TxPackets: 5},
    50  		"lo":          {Name: "lo", RxBytes: 1664039048, RxPackets: 1566805, TxBytes: 1664039048, TxPackets: 1566805},
    51  		"docker0":     {Name: "docker0", RxBytes: 2568, RxPackets: 38, TxBytes: 438, TxPackets: 5},
    52  		"eth0":        {Name: "eth0", RxBytes: 874354587, RxPackets: 1036395, TxBytes: 563352563, TxPackets: 732147},
    53  	}
    54  
    55  	if want, have := len(lines), len(netDev); want != have {
    56  		t.Errorf("want %d parsed net/dev lines, have %d", want, have)
    57  	}
    58  	for _, line := range netDev {
    59  		if want, have := lines[line.Name], line; want != have {
    60  			t.Errorf("%s: want %v, have %v", line.Name, want, have)
    61  		}
    62  	}
    63  }
    64  
    65  func TestProcNetDev(t *testing.T) {
    66  	p, err := getProcFixtures(t).Proc(26231)
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  
    71  	netDev, err := p.NetDev()
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    75  
    76  	lines := map[string]NetDevLine{
    77  		"lo":   {Name: "lo"},
    78  		"eth0": {Name: "eth0", RxBytes: 438, RxPackets: 5, TxBytes: 648, TxPackets: 8},
    79  	}
    80  
    81  	if want, have := len(lines), len(netDev); want != have {
    82  		t.Errorf("want %d parsed net/dev lines, have %d", want, have)
    83  	}
    84  	for _, line := range netDev {
    85  		if want, have := lines[line.Name], line; want != have {
    86  			t.Errorf("%s: want %v, have %v", line.Name, want, have)
    87  		}
    88  	}
    89  }
    90  

View as plain text