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 14package procfs 15 16import ( 17 "testing" 18) 19 20func TestNetDevParseLine(t *testing.T) { 21 const rawLine = ` eth0: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16` 22 23 have, err := NetDev{}.parseLine(rawLine) 24 if err != nil { 25 t.Fatal(err) 26 } 27 28 want := NetDevLine{"eth0", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16} 29 if want != *have { 30 t.Errorf("want %v, have %v", want, have) 31 } 32} 33 34func TestNetDev(t *testing.T) { 35 fs, err := NewFS(procTestFixtures) 36 if err != nil { 37 t.Fatal(err) 38 } 39 40 netDev, err := fs.NetDev() 41 if err != nil { 42 t.Fatal(err) 43 } 44 45 lines := map[string]NetDevLine{ 46 "vethf345468": {Name: "vethf345468", RxBytes: 648, RxPackets: 8, TxBytes: 438, TxPackets: 5}, 47 "lo": {Name: "lo", RxBytes: 1664039048, RxPackets: 1566805, TxBytes: 1664039048, TxPackets: 1566805}, 48 "docker0": {Name: "docker0", RxBytes: 2568, RxPackets: 38, TxBytes: 438, TxPackets: 5}, 49 "eth0": {Name: "eth0", RxBytes: 874354587, RxPackets: 1036395, TxBytes: 563352563, TxPackets: 732147}, 50 } 51 52 if want, have := len(lines), len(netDev); want != have { 53 t.Errorf("want %d parsed net/dev lines, have %d", want, have) 54 } 55 for _, line := range netDev { 56 if want, have := lines[line.Name], line; want != have { 57 t.Errorf("%s: want %v, have %v", line.Name, want, have) 58 } 59 } 60} 61 62func TestProcNetDev(t *testing.T) { 63 p, err := getProcFixtures(t).Proc(26231) 64 if err != nil { 65 t.Fatal(err) 66 } 67 68 netDev, err := p.NetDev() 69 if err != nil { 70 t.Fatal(err) 71 } 72 73 lines := map[string]NetDevLine{ 74 "lo": {Name: "lo"}, 75 "eth0": {Name: "eth0", RxBytes: 438, RxPackets: 5, TxBytes: 648, TxPackets: 8}, 76 } 77 78 if want, have := len(lines), len(netDev); want != have { 79 t.Errorf("want %d parsed net/dev lines, have %d", want, have) 80 } 81 for _, line := range netDev { 82 if want, have := lines[line.Name], line; want != have { 83 t.Errorf("%s: want %v, have %v", line.Name, want, have) 84 } 85 } 86} 87