github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/core/network/source_test.go (about)

     1  // Copyright 2021 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package network_test
     5  
     6  import (
     7  	"os"
     8  	"path/filepath"
     9  	"strings"
    10  
    11  	"github.com/juju/testing"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  
    15  	"github.com/juju/juju/core/network"
    16  )
    17  
    18  type sourceSuite struct {
    19  	testing.IsolationSuite
    20  }
    21  
    22  var _ = gc.Suite(&sourceSuite{})
    23  
    24  func (*sourceSuite) TestParseInterfaceType(c *gc.C) {
    25  	fakeSysPath := filepath.Join(c.MkDir(), network.SysClassNetPath)
    26  	err := os.MkdirAll(fakeSysPath, 0700)
    27  	c.Check(err, jc.ErrorIsNil)
    28  
    29  	writeFakeUEvent := func(interfaceName string, lines ...string) string {
    30  		fakeInterfacePath := filepath.Join(fakeSysPath, interfaceName)
    31  		err := os.MkdirAll(fakeInterfacePath, 0700)
    32  		c.Check(err, jc.ErrorIsNil)
    33  
    34  		fakeUEventPath := filepath.Join(fakeInterfacePath, "uevent")
    35  		contents := strings.Join(lines, "\n")
    36  		err = os.WriteFile(fakeUEventPath, []byte(contents), 0644)
    37  		c.Check(err, jc.ErrorIsNil)
    38  		return fakeUEventPath
    39  	}
    40  
    41  	result := network.ParseInterfaceType(fakeSysPath, "missing")
    42  	c.Check(result, gc.Equals, network.UnknownDevice)
    43  
    44  	writeFakeUEvent("eth0", "IFINDEX=1", "INTERFACE=eth0")
    45  	result = network.ParseInterfaceType(fakeSysPath, "eth0")
    46  	c.Check(result, gc.Equals, network.UnknownDevice)
    47  
    48  	fakeUEventPath := writeFakeUEvent("eth0.42", "DEVTYPE=vlan")
    49  	result = network.ParseInterfaceType(fakeSysPath, "eth0.42")
    50  	c.Check(result, gc.Equals, network.VLAN8021QDevice)
    51  
    52  	os.Chmod(fakeUEventPath, 0000) // permission denied error is OK
    53  	result = network.ParseInterfaceType(fakeSysPath, "eth0.42")
    54  	c.Check(result, gc.Equals, network.UnknownDevice)
    55  
    56  	writeFakeUEvent("bond0", "DEVTYPE=bond")
    57  	result = network.ParseInterfaceType(fakeSysPath, "bond0")
    58  	c.Check(result, gc.Equals, network.BondDevice)
    59  
    60  	writeFakeUEvent("br-ens4", "DEVTYPE=bridge")
    61  	result = network.ParseInterfaceType(fakeSysPath, "br-ens4")
    62  	c.Check(result, gc.Equals, network.BridgeDevice)
    63  
    64  	// First DEVTYPE found wins.
    65  	writeFakeUEvent("foo", "DEVTYPE=vlan", "DEVTYPE=bridge")
    66  	result = network.ParseInterfaceType(fakeSysPath, "foo")
    67  	c.Check(result, gc.Equals, network.VLAN8021QDevice)
    68  
    69  	writeFakeUEvent("fake", "DEVTYPE=warp-drive")
    70  	result = network.ParseInterfaceType(fakeSysPath, "fake")
    71  	c.Check(result, gc.Equals, network.UnknownDevice)
    72  }
    73  
    74  func (*sourceSuite) TestGetBridgePorts(c *gc.C) {
    75  	fakeSysPath := filepath.Join(c.MkDir(), network.SysClassNetPath)
    76  	err := os.MkdirAll(fakeSysPath, 0700)
    77  	c.Check(err, jc.ErrorIsNil)
    78  
    79  	writeFakePorts := func(bridgeName string, portNames ...string) {
    80  		fakePortsPath := filepath.Join(fakeSysPath, bridgeName, "brif")
    81  		err := os.MkdirAll(fakePortsPath, 0700)
    82  		c.Check(err, jc.ErrorIsNil)
    83  
    84  		for _, portName := range portNames {
    85  			portPath := filepath.Join(fakePortsPath, portName)
    86  			err = os.WriteFile(portPath, []byte(""), 0644)
    87  			c.Check(err, jc.ErrorIsNil)
    88  		}
    89  	}
    90  
    91  	result := network.GetBridgePorts(fakeSysPath, "missing")
    92  	c.Check(result, gc.IsNil)
    93  
    94  	writeFakePorts("br-eth0")
    95  	result = network.GetBridgePorts(fakeSysPath, "br-eth0")
    96  	c.Check(result, gc.IsNil)
    97  
    98  	writeFakePorts("br-eth0", "eth0")
    99  	result = network.GetBridgePorts(fakeSysPath, "br-eth0")
   100  	c.Check(result, jc.DeepEquals, []string{"eth0"})
   101  
   102  	writeFakePorts("br-ovs", "eth0", "eth1", "eth2")
   103  	result = network.GetBridgePorts(fakeSysPath, "br-ovs")
   104  	c.Check(result, jc.DeepEquals, []string{"eth0", "eth1", "eth2"})
   105  }