|
| 1 | +// Copyright 2020 Canonical Ltd. |
| 2 | +// Licensed under the AGPLv3, see LICENCE file for details. |
| 3 | + |
| 4 | +package broker |
| 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 | + "github.com/juju/juju/core/network" |
| 14 | +) |
| 15 | + |
| 16 | +type brokerSuite struct { |
| 17 | + testing.IsolationSuite |
| 18 | +} |
| 19 | + |
| 20 | +var _ = gc.Suite(&brokerSuite{}) |
| 21 | + |
| 22 | +func (s *brokerSuite) SetUpSuite(c *gc.C) { |
| 23 | + s.IsolationSuite.SetUpSuite(c) |
| 24 | +} |
| 25 | + |
| 26 | +func (s *brokerSuite) SetUpTest(c *gc.C) { |
| 27 | + s.IsolationSuite.SetUpTest(c) |
| 28 | +} |
| 29 | + |
| 30 | +func (s *brokerSuite) TestExistingOVSManagedBridges(c *gc.C) { |
| 31 | + // Patch output for "ovs-vsctl list-br" and make sure exec.LookPath can |
| 32 | + // detect it in the path |
| 33 | + testing.PatchExecutableAsEchoArgs(c, s, "ovs-vsctl", 0) |
| 34 | + s.PatchValue(&getCommandOutput, func(cmd *exec.Cmd) ([]byte, error) { |
| 35 | + 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")) |
| 36 | + return []byte("ovsbr1" + "\n"), nil |
| 37 | + }) |
| 38 | + |
| 39 | + ifaces := network.InterfaceInfos{ |
| 40 | + {InterfaceName: "eth0"}, |
| 41 | + {InterfaceName: "eth1"}, |
| 42 | + {InterfaceName: "lxdbr0"}, |
| 43 | + {InterfaceName: "ovsbr1"}, |
| 44 | + } |
| 45 | + |
| 46 | + ovsIfaces, err := ovsManagedBridges(ifaces) |
| 47 | + c.Assert(err, jc.ErrorIsNil) |
| 48 | + c.Assert(ovsIfaces, gc.HasLen, 1, gc.Commentf("expected ovs-managed bridge list to contain a single entry")) |
| 49 | + c.Assert(ovsIfaces[0].InterfaceName, gc.Equals, "ovsbr1", gc.Commentf("expected ovs-managed bridge list to contain iface 'ovsbr1'")) |
| 50 | +} |
| 51 | + |
| 52 | +func (s *brokerSuite) TestNonExistingOVSManagedBridges(c *gc.C) { |
| 53 | + // Patch output for "ovs-vsctl list-br" and make sure exec.LookPath can |
| 54 | + // detect it in the path |
| 55 | + testing.PatchExecutableAsEchoArgs(c, s, "ovs-vsctl", 0) |
| 56 | + s.PatchValue(&getCommandOutput, func(cmd *exec.Cmd) ([]byte, error) { |
| 57 | + 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")) |
| 58 | + return []byte("\n"), nil |
| 59 | + }) |
| 60 | + |
| 61 | + ifaces := network.InterfaceInfos{ |
| 62 | + {InterfaceName: "eth0"}, |
| 63 | + {InterfaceName: "eth1"}, |
| 64 | + {InterfaceName: "lxdbr0"}, |
| 65 | + } |
| 66 | + |
| 67 | + ovsIfaces, err := ovsManagedBridges(ifaces) |
| 68 | + c.Assert(err, jc.ErrorIsNil) |
| 69 | + c.Assert(ovsIfaces, gc.HasLen, 0, gc.Commentf("expected ovs-managed bridge list to be empty")) |
| 70 | +} |
| 71 | + |
| 72 | +func (s *brokerSuite) TestMissingOVSTools(c *gc.C) { |
| 73 | + ifaces := network.InterfaceInfos{{InterfaceName: "eth0"}} |
| 74 | + ovsIfaces, err := ovsManagedBridges(ifaces) |
| 75 | + c.Assert(err, jc.ErrorIsNil) |
| 76 | + c.Assert(ovsIfaces, gc.HasLen, 0, gc.Commentf("expected ovs-managed bridge list to be empty")) |
| 77 | +} |
0 commit comments