github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/machine/network_linux_test.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  /*
     5  Copyright 2022 The Katalyst Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package machine
    21  
    22  import (
    23  	"testing"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  
    27  	"github.com/kubewharf/katalyst-core/pkg/config/agent/global"
    28  )
    29  
    30  func TestGetExtraNetworkInfo(t *testing.T) {
    31  	t.Parallel()
    32  
    33  	testCases := []struct {
    34  		description  string
    35  		noError      bool
    36  		conf         *global.MachineInfoConfiguration
    37  		expectedResp *ExtraNetworkInfo
    38  	}{
    39  		{
    40  			description: "single NS",
    41  			noError:     true,
    42  			conf: &global.MachineInfoConfiguration{
    43  				NetMultipleNS:   false,
    44  				NetNSDirAbsPath: "",
    45  			},
    46  			expectedResp: nil,
    47  		},
    48  		{
    49  			description: "multi NS",
    50  			noError:     false,
    51  			conf: &global.MachineInfoConfiguration{
    52  				NetMultipleNS:   true,
    53  				NetNSDirAbsPath: "",
    54  			},
    55  			expectedResp: nil,
    56  		},
    57  	}
    58  
    59  	for _, tc := range testCases {
    60  		netInfo, err := GetExtraNetworkInfo(tc.conf)
    61  		if tc.noError {
    62  			assert.Nil(t, err)
    63  			assert.NotNil(t, netInfo)
    64  		} else {
    65  			assert.NotNil(t, err)
    66  			assert.Nil(t, netInfo)
    67  		}
    68  	}
    69  }
    70  
    71  func TestGetInterfaceAttr(t *testing.T) {
    72  	t.Parallel()
    73  
    74  	nic := &InterfaceInfo{
    75  		Iface: "eth0",
    76  	}
    77  
    78  	getInterfaceAttr(nic, "/sys/class/net")
    79  	assert.NotNil(t, nic)
    80  	assert.Equal(t, 0, nic.NumaNode)
    81  	assert.Equal(t, false, nic.Enable)
    82  }
    83  
    84  func TestGetInterfaceAddr(t *testing.T) {
    85  	t.Parallel()
    86  
    87  	addr, err := getInterfaceAddr()
    88  	assert.NotNil(t, addr)
    89  	assert.Nil(t, err)
    90  }
    91  
    92  func TestGetNSNetworkHardwareTopology(t *testing.T) {
    93  	t.Parallel()
    94  
    95  	_, err := getNSNetworkHardwareTopology("", "")
    96  	assert.Nil(t, err)
    97  }