github.com/jaypipes/ghw@v0.21.1/pkg/net/net_linux_test.go (about)

     1  //
     2  // Use and distribution licensed under the Apache license version 2.
     3  //
     4  // See the COPYING file in the root project directory for full text.
     5  //
     6  
     7  //go:build linux
     8  // +build linux
     9  
    10  package net
    11  
    12  import (
    13  	"bytes"
    14  	"os"
    15  	"reflect"
    16  	"strings"
    17  	"testing"
    18  )
    19  
    20  func TestParseEthtoolFeature(t *testing.T) {
    21  	if _, ok := os.LookupEnv("GHW_TESTING_SKIP_NET"); ok {
    22  		t.Skip("Skipping network tests.")
    23  	}
    24  
    25  	tests := []struct {
    26  		line     string
    27  		expected *NICCapability
    28  	}{
    29  		{
    30  			line: "scatter-gather: off",
    31  			expected: &NICCapability{
    32  				Name:      "scatter-gather",
    33  				IsEnabled: false,
    34  				CanEnable: true,
    35  			},
    36  		},
    37  		{
    38  			line: "scatter-gather: on",
    39  			expected: &NICCapability{
    40  				Name:      "scatter-gather",
    41  				IsEnabled: true,
    42  				CanEnable: true,
    43  			},
    44  		},
    45  		{
    46  			line: "scatter-gather: off [fixed]",
    47  			expected: &NICCapability{
    48  				Name:      "scatter-gather",
    49  				IsEnabled: false,
    50  				CanEnable: false,
    51  			},
    52  		},
    53  	}
    54  
    55  	for x, test := range tests {
    56  		actual := netParseEthtoolFeature(test.line)
    57  		if !reflect.DeepEqual(test.expected, actual) {
    58  			t.Fatalf("In test %d, expected %v == %v", x, test.expected, actual)
    59  		}
    60  	}
    61  }
    62  
    63  func TestParseNicAttrEthtool(t *testing.T) {
    64  	if _, ok := os.LookupEnv("GHW_TESTING_SKIP_NET"); ok {
    65  		t.Skip("Skipping network tests.")
    66  	}
    67  
    68  	tests := []struct {
    69  		input    string
    70  		expected *NIC
    71  	}{
    72  		{
    73  			input: `Settings for eth0:
    74  	Supported ports: [ TP ]
    75  	Supported link modes:   10baseT/Half 10baseT/Full
    76  	                        100baseT/Half 100baseT/Full
    77  	                        1000baseT/Full
    78  	Supported pause frame use: No
    79  	Supports auto-negotiation: Yes
    80  	Supported FEC modes: Not reported
    81  	Advertised link modes:  10baseT/Half 10baseT/Full
    82  	                        100baseT/Half 100baseT/Full
    83  	                        1000baseT/Full
    84  	Advertised pause frame use: No
    85  	Advertised auto-negotiation: Yes
    86  	Advertised FEC modes: Not reported
    87  	Speed: 1000Mb/s
    88  	Duplex: Full
    89  	Auto-negotiation: on
    90  	Port: Twisted Pair
    91  	PHYAD: 1
    92  	Transceiver: internal
    93  	MDI-X: off (auto)
    94  	Supports Wake-on: pumbg
    95  	Wake-on: d
    96          Current message level: 0x00000007 (7)
    97                                 drv probe link
    98  	Link detected: yes
    99  `,
   100  			expected: &NIC{
   101  				Speed:          "1000Mb/s",
   102  				Duplex:         "Full",
   103  				SupportedPorts: []string{"TP"},
   104  				AdvertisedLinkModes: []string{
   105  					"10baseT/Half",
   106  					"10baseT/Full",
   107  					"100baseT/Half",
   108  					"100baseT/Full",
   109  					"1000baseT/Full",
   110  				},
   111  				SupportedLinkModes: []string{
   112  					"10baseT/Half",
   113  					"10baseT/Full",
   114  					"100baseT/Half",
   115  					"100baseT/Full",
   116  					"1000baseT/Full",
   117  				},
   118  				Capabilities: []*NICCapability{
   119  					{
   120  						Name:      "auto-negotiation",
   121  						IsEnabled: true,
   122  						CanEnable: true,
   123  					},
   124  					{
   125  						Name:      "pause-frame-use",
   126  						IsEnabled: false,
   127  						CanEnable: false,
   128  					},
   129  				},
   130  			},
   131  		},
   132  	}
   133  
   134  	for x, test := range tests {
   135  		m := parseNicAttrEthtool(bytes.NewBufferString(test.input))
   136  		actual := &NIC{}
   137  		actual.Speed = strings.Join(m["Speed"], "")
   138  		actual.Duplex = strings.Join(m["Duplex"], "")
   139  		actual.SupportedLinkModes = m["Supported link modes"]
   140  		actual.SupportedPorts = m["Supported ports"]
   141  		actual.SupportedFECModes = m["Supported FEC modes"]
   142  		actual.AdvertisedLinkModes = m["Advertised link modes"]
   143  		actual.AdvertisedFECModes = m["Advertised FEC modes"]
   144  		actual.Capabilities = append(actual.Capabilities, autoNegCap(m))
   145  		actual.Capabilities = append(actual.Capabilities, pauseFrameUseCap(m))
   146  		if !reflect.DeepEqual(test.expected, actual) {
   147  			t.Fatalf("In test %d\nExpected:\n%+v\nActual:\n%+v\n", x, *test.expected, *actual)
   148  		}
   149  	}
   150  }