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

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package network_test
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/clock"
    10  	"github.com/juju/testing"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/network"
    14  )
    15  
    16  // A note regarding the use of clock.WallClock in these unit tests.
    17  //
    18  // All the tests pass 0 for a timeout, which means indefinite, and
    19  // therefore no timer/clock is used. There is one test that checks for
    20  // timeout and passes 0.5s as its timeout value. Because of this it's
    21  // not clear why the 'testing clock' would be a better choice.
    22  
    23  type BridgeSuite struct {
    24  	testing.IsolationSuite
    25  }
    26  
    27  var _ = gc.Suite(&BridgeSuite{})
    28  
    29  func (s *BridgeSuite) SetUpSuite(c *gc.C) {
    30  	s.IsolationSuite.SetUpSuite(c)
    31  }
    32  
    33  func assertENIBridgerError(c *gc.C, devices []network.DeviceToBridge, timeout time.Duration, clock clock.Clock, filename string, dryRun bool, reconfigureDelay int, expected string) {
    34  	bridger := network.NewEtcNetworkInterfacesBridger(clock, timeout, filename, dryRun)
    35  	err := bridger.Bridge(devices, reconfigureDelay)
    36  	c.Assert(err, gc.NotNil)
    37  	c.Assert(err, gc.ErrorMatches, expected)
    38  }
    39  
    40  func (*BridgeSuite) TestENIBridgerWithMissingFilenameArgument(c *gc.C) {
    41  	devices := []network.DeviceToBridge{
    42  		{
    43  			DeviceName: "ens123",
    44  			BridgeName: "br-ens123",
    45  		},
    46  	}
    47  	expected := `bridge activation error: filename and input is nil`
    48  	assertENIBridgerError(c, devices, 0, clock.WallClock, "", true, 0, expected)
    49  }
    50  
    51  func (*BridgeSuite) TestENIBridgerWithEmptyDeviceNamesArgument(c *gc.C) {
    52  	devices := []network.DeviceToBridge{}
    53  	expected := `bridge activation error: no devices specified`
    54  	assertENIBridgerError(c, devices, 0, clock.WallClock, "testdata/non-existent-filename", true, 0, expected)
    55  }
    56  
    57  func (*BridgeSuite) TestENIBridgerWithNonExistentFile(c *gc.C) {
    58  	devices := []network.DeviceToBridge{
    59  		{
    60  			DeviceName: "ens123",
    61  			BridgeName: "br-ens123",
    62  		},
    63  	}
    64  	expected := `bridge activation error: open testdata/non-existent-file: no such file or directory`
    65  	assertENIBridgerError(c, devices, 0, clock.WallClock, "testdata/non-existent-file", true, 0, expected)
    66  }
    67  
    68  func (*BridgeSuite) TestENIBridgerWithTimeout(c *gc.C) {
    69  	devices := []network.DeviceToBridge{
    70  		{
    71  			DeviceName: "ens123",
    72  			BridgeName: "br-ens123",
    73  		},
    74  	}
    75  	expected := "bridge activation error: bridge activation error: command cancelled"
    76  	// 25694 is a magic value that causes the bridging script to sleep
    77  	assertENIBridgerError(c, devices, 500*time.Millisecond, clock.WallClock, "testdata/interfaces", true, 25694, expected)
    78  }
    79  
    80  func (*BridgeSuite) TestENIBridgerWithDryRun(c *gc.C) {
    81  	devices := []network.DeviceToBridge{
    82  		{
    83  			DeviceName: "ens123",
    84  			BridgeName: "br-ens123",
    85  		},
    86  	}
    87  	bridger := network.NewEtcNetworkInterfacesBridger(clock.WallClock, 0, "testdata/interfaces", true)
    88  	err := bridger.Bridge(devices, 0)
    89  	c.Assert(err, gc.IsNil)
    90  }