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

     1  // Copyright 2020 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package network
     5  
     6  import (
     7  	"os/exec"
     8  
     9  	"github.com/juju/testing"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  )
    13  
    14  type ovsSuite struct {
    15  	testing.IsolationSuite
    16  }
    17  
    18  var _ = gc.Suite(&ovsSuite{})
    19  
    20  func (s *ovsSuite) SetUpSuite(c *gc.C) {
    21  	s.IsolationSuite.SetUpSuite(c)
    22  }
    23  
    24  func (s *ovsSuite) SetUpTest(c *gc.C) {
    25  	s.IsolationSuite.SetUpTest(c)
    26  }
    27  
    28  func (s *ovsSuite) TestExistingOvsManagedBridgeInterfaces(c *gc.C) {
    29  	// Patch output for "ovs-vsctl list-br" and make sure exec.LookPath can
    30  	// detect it in the path
    31  	testing.PatchExecutableAsEchoArgs(c, s, "ovs-vsctl", 0)
    32  	s.PatchValue(&getCommandOutput, func(cmd *exec.Cmd) ([]byte, error) {
    33  		c.Assert(cmd.Args, gc.DeepEquals, []string{"ovs-vsctl", "list-br"}, gc.Commentf("expected ovs-vsctl to be invoked with 'list-br' as an argument"))
    34  		return []byte("ovsbr1" + "\n"), nil
    35  	})
    36  
    37  	ifaces := InterfaceInfos{
    38  		{InterfaceName: "eth0"},
    39  		{InterfaceName: "eth1"},
    40  		{InterfaceName: "lxdbr0"},
    41  		{InterfaceName: "ovsbr1"},
    42  	}
    43  
    44  	ovsIfaces, err := OvsManagedBridgeInterfaces(ifaces)
    45  	c.Assert(err, jc.ErrorIsNil)
    46  	c.Assert(ovsIfaces, gc.HasLen, 1, gc.Commentf("expected ovs-managed bridge list to contain a single entry"))
    47  	c.Assert(ovsIfaces[0].InterfaceName, gc.Equals, "ovsbr1", gc.Commentf("expected ovs-managed bridge list to contain iface 'ovsbr1'"))
    48  }
    49  
    50  func (s *ovsSuite) TestNonExistingOvsManagedBridgeInterfaces(c *gc.C) {
    51  	// Patch output for "ovs-vsctl list-br" and make sure exec.LookPath can
    52  	// detect it in the path
    53  	testing.PatchExecutableAsEchoArgs(c, s, "ovs-vsctl", 0)
    54  	s.PatchValue(&getCommandOutput, func(cmd *exec.Cmd) ([]byte, error) {
    55  		c.Assert(cmd.Args, gc.DeepEquals, []string{"ovs-vsctl", "list-br"}, gc.Commentf("expected ovs-vsctl to be invoked with 'list-br' as an argument"))
    56  		return []byte("\n"), nil
    57  	})
    58  
    59  	ifaces := InterfaceInfos{
    60  		{InterfaceName: "eth0"},
    61  		{InterfaceName: "eth1"},
    62  		{InterfaceName: "lxdbr0"},
    63  	}
    64  
    65  	ovsIfaces, err := OvsManagedBridgeInterfaces(ifaces)
    66  	c.Assert(err, jc.ErrorIsNil)
    67  	c.Assert(ovsIfaces, gc.HasLen, 0, gc.Commentf("expected ovs-managed bridge list to be empty"))
    68  }
    69  
    70  func (s *ovsSuite) TestMissingOvsTools(c *gc.C) {
    71  	ifaces := InterfaceInfos{{InterfaceName: "eth0"}}
    72  	ovsIfaces, err := OvsManagedBridgeInterfaces(ifaces)
    73  	c.Assert(err, jc.ErrorIsNil)
    74  	c.Assert(ovsIfaces, gc.HasLen, 0, gc.Commentf("expected ovs-managed bridge list to be empty"))
    75  }