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

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package state
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/testing"
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/core/network"
    13  	coretesting "github.com/juju/juju/testing"
    14  )
    15  
    16  // linkLayerDevicesInternalSuite contains white-box tests for link-layer network
    17  // devices' internals, which do not actually access mongo. The rest of the logic
    18  // is tested in linkLayerDevicesStateSuite.
    19  type linkLayerDevicesInternalSuite struct {
    20  	testing.IsolationSuite
    21  }
    22  
    23  var _ = gc.Suite(&linkLayerDevicesInternalSuite{})
    24  
    25  func (s *linkLayerDevicesInternalSuite) TestNewLinkLayerDeviceCreatesLinkLayerDevice(c *gc.C) {
    26  	result := newLinkLayerDevice(nil, linkLayerDeviceDoc{})
    27  	c.Assert(result, gc.NotNil)
    28  	c.Assert(result.st, gc.IsNil)
    29  	c.Assert(result.doc, jc.DeepEquals, linkLayerDeviceDoc{})
    30  }
    31  
    32  func (s *linkLayerDevicesInternalSuite) TestDocIDIncludesModelUUID(c *gc.C) {
    33  	const localDocID = "foo"
    34  	globalDocID := coretesting.ModelTag.Id() + ":" + localDocID
    35  
    36  	result := s.newLinkLayerDeviceWithDummyState(linkLayerDeviceDoc{DocID: localDocID})
    37  	c.Assert(result.DocID(), gc.Equals, globalDocID)
    38  
    39  	result = s.newLinkLayerDeviceWithDummyState(linkLayerDeviceDoc{DocID: globalDocID})
    40  	c.Assert(result.DocID(), gc.Equals, globalDocID)
    41  }
    42  
    43  func (s *linkLayerDevicesInternalSuite) newLinkLayerDeviceWithDummyState(doc linkLayerDeviceDoc) *LinkLayerDevice {
    44  	// We only need the model UUID set for localID() and docID() to work.
    45  	// The rest is tested in linkLayerDevicesStateSuite.
    46  	dummyState := &State{modelTag: coretesting.ModelTag}
    47  	return newLinkLayerDevice(dummyState, doc)
    48  }
    49  
    50  func (s *linkLayerDevicesInternalSuite) TestProviderIDIsEmptyWhenNotSet(c *gc.C) {
    51  	result := s.newLinkLayerDeviceWithDummyState(linkLayerDeviceDoc{})
    52  	c.Assert(result.ProviderID(), gc.Equals, network.Id(""))
    53  }
    54  
    55  func (s *linkLayerDevicesInternalSuite) TestProviderIDDoesNotIncludeModelUUIDWhenSet(c *gc.C) {
    56  	const localProviderID = "foo"
    57  	result := s.newLinkLayerDeviceWithDummyState(linkLayerDeviceDoc{ProviderID: localProviderID})
    58  	c.Assert(result.ProviderID(), gc.Equals, network.Id(localProviderID))
    59  }
    60  
    61  func (s *linkLayerDevicesInternalSuite) TestParentDeviceReturnsNoErrorWhenParentNameNotSet(c *gc.C) {
    62  	result := s.newLinkLayerDeviceWithDummyState(linkLayerDeviceDoc{})
    63  	parent, err := result.ParentDevice()
    64  	c.Check(parent, gc.IsNil)
    65  	c.Check(err, jc.ErrorIsNil)
    66  }
    67  
    68  func (s *linkLayerDevicesInternalSuite) TestLinkLayerDeviceGlobalKeyHelper(c *gc.C) {
    69  	result := linkLayerDeviceGlobalKey("42", "eno1")
    70  	c.Assert(result, gc.Equals, "m#42#d#eno1")
    71  
    72  	result = linkLayerDeviceGlobalKey("", "")
    73  	c.Assert(result, gc.Equals, "")
    74  }
    75  
    76  func (s *linkLayerDevicesInternalSuite) TestParseLinkLayerParentNameAsGlobalKey(c *gc.C) {
    77  	for i, test := range []struct {
    78  		about              string
    79  		input              string
    80  		expectedError      string
    81  		expectedMachineID  string
    82  		expectedParentName string
    83  	}{{
    84  		about: "empty input - empty outputs and no error",
    85  		input: "",
    86  	}, {
    87  		about: "name only as input - empty outputs and no error",
    88  		input: "some-parent",
    89  	}, {
    90  		about:              "global key as input - parsed outputs and no error",
    91  		input:              "m#42#d#br-eth1",
    92  		expectedMachineID:  "42",
    93  		expectedParentName: "br-eth1",
    94  	}, {
    95  		about:         "invalid name as input - empty outputs and NotValidError",
    96  		input:         "some name with not enough # in it",
    97  		expectedError: `ParentName "some name with not enough # in it" format not valid`,
    98  	}, {
    99  		about:         "almost a global key as input - empty outputs and NotValidError",
   100  		input:         "x#foo#y#bar",
   101  		expectedError: `ParentName "x#foo#y#bar" format not valid`,
   102  	}} {
   103  		c.Logf("test #%d: %q", i, test.about)
   104  		gotMachineID, gotParentName, gotError := parseLinkLayerDeviceParentNameAsGlobalKey(test.input)
   105  		if test.expectedError != "" {
   106  			c.Check(gotError, gc.ErrorMatches, test.expectedError)
   107  			c.Check(gotError, jc.Satisfies, errors.IsNotValid)
   108  		} else {
   109  			c.Check(gotError, jc.ErrorIsNil)
   110  		}
   111  		c.Check(gotMachineID, gc.Equals, test.expectedMachineID)
   112  		c.Check(gotParentName, gc.Equals, test.expectedParentName)
   113  	}
   114  }
   115  
   116  func (s *linkLayerDevicesInternalSuite) TestStringIncludesTypeNameAndMachineID(c *gc.C) {
   117  	doc := linkLayerDeviceDoc{
   118  		MachineID: "42",
   119  		Name:      "foo",
   120  		Type:      network.BondDevice,
   121  	}
   122  	result := s.newLinkLayerDeviceWithDummyState(doc)
   123  	expectedString := `bond device "foo" on machine "42"`
   124  
   125  	c.Assert(result.String(), gc.Equals, expectedString)
   126  }
   127  
   128  func (s *linkLayerDevicesInternalSuite) TestRemainingSimpleGetterMethods(c *gc.C) {
   129  	doc := linkLayerDeviceDoc{
   130  		Name:        "bond0",
   131  		MachineID:   "99",
   132  		MTU:         uint(9000),
   133  		Type:        network.BondDevice,
   134  		MACAddress:  "aa:bb:cc:dd:ee:f0",
   135  		IsAutoStart: true,
   136  		IsUp:        true,
   137  		ParentName:  "br-bond0",
   138  	}
   139  	result := s.newLinkLayerDeviceWithDummyState(doc)
   140  
   141  	c.Check(result.Name(), gc.Equals, "bond0")
   142  	c.Check(result.MachineID(), gc.Equals, "99")
   143  	c.Check(result.MTU(), gc.Equals, uint(9000))
   144  	c.Check(result.Type(), gc.Equals, network.BondDevice)
   145  	c.Check(result.MACAddress(), gc.Equals, "aa:bb:cc:dd:ee:f0")
   146  	c.Check(result.IsAutoStart(), jc.IsTrue)
   147  	c.Check(result.IsUp(), jc.IsTrue)
   148  	c.Check(result.ParentName(), gc.Equals, "br-bond0")
   149  }