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