...

Source file src/github.com/prometheus/procfs/sysfs/net_class_test.go

Documentation: github.com/prometheus/procfs/sysfs

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

View as plain text