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// +build !windows
15
16package sysfs
17
18import (
19	"reflect"
20	"testing"
21)
22
23func TestNewNetClassDevices(t *testing.T) {
24	fs, err := NewFS(sysTestFixtures)
25	if err != nil {
26		t.Fatal(err)
27	}
28
29	devices, err := fs.NetClassDevices()
30	if err != nil {
31		t.Fatal(err)
32	}
33
34	if len(devices) != 1 {
35		t.Errorf("Unexpected number of devices, want %d, have %d", 1, len(devices))
36	}
37	if devices[0] != "eth0" {
38		t.Errorf("Found unexpected device, want %s, have %s", "eth0", devices[0])
39	}
40}
41
42func TestNetClass(t *testing.T) {
43	fs, err := NewFS(sysTestFixtures)
44	if err != nil {
45		t.Fatal(err)
46	}
47
48	nc, err := fs.NetClass()
49	if err != nil {
50		t.Fatal(err)
51	}
52
53	var (
54		addrAssignType   int64 = 3
55		addrLen          int64 = 6
56		carrier          int64 = 1
57		carrierChanges   int64 = 2
58		carrierDownCount int64 = 1
59		carrierUpCount   int64 = 1
60		devID            int64 = 32
61		dormant          int64 = 1
62		flags            int64 = 4867
63		ifIndex          int64 = 2
64		ifLink           int64 = 2
65		linkMode         int64 = 1
66		mtu              int64 = 1500
67		nameAssignType   int64 = 2
68		netDevGroup      int64 = 0
69		speed            int64 = 1000
70		txQueueLen       int64 = 1000
71		netType          int64 = 1
72	)
73
74	netClass := NetClass{
75		"eth0": {
76			Address:          "01:01:01:01:01:01",
77			AddrAssignType:   &addrAssignType,
78			AddrLen:          &addrLen,
79			Broadcast:        "ff:ff:ff:ff:ff:ff",
80			Carrier:          &carrier,
81			CarrierChanges:   &carrierChanges,
82			CarrierDownCount: &carrierDownCount,
83			CarrierUpCount:   &carrierUpCount,
84			DevID:            &devID,
85			Dormant:          &dormant,
86			Duplex:           "full",
87			Flags:            &flags,
88			IfAlias:          "",
89			IfIndex:          &ifIndex,
90			IfLink:           &ifLink,
91			LinkMode:         &linkMode,
92			MTU:              &mtu,
93			Name:             "eth0",
94			NameAssignType:   &nameAssignType,
95			NetDevGroup:      &netDevGroup,
96			OperState:        "up",
97			PhysPortID:       "",
98			PhysPortName:     "",
99			PhysSwitchID:     "",
100			Speed:            &speed,
101			TxQueueLen:       &txQueueLen,
102			Type:             &netType,
103		},
104	}
105
106	if !reflect.DeepEqual(netClass, nc) {
107		t.Errorf("Result not correct: want %v, have %v", netClass, nc)
108	}
109}
110