github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/api/firewaller/machine_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package firewaller_test 5 6 import ( 7 jc "github.com/juju/testing/checkers" 8 gc "gopkg.in/check.v1" 9 "gopkg.in/juju/names.v2" 10 11 "github.com/juju/juju/api/firewaller" 12 "github.com/juju/juju/apiserver/params" 13 "github.com/juju/juju/instance" 14 "github.com/juju/juju/network" 15 "github.com/juju/juju/state" 16 "github.com/juju/juju/watcher/watchertest" 17 ) 18 19 type machineSuite struct { 20 firewallerSuite 21 22 apiMachine *firewaller.Machine 23 } 24 25 var _ = gc.Suite(&machineSuite{}) 26 27 func (s *machineSuite) SetUpTest(c *gc.C) { 28 s.firewallerSuite.SetUpTest(c) 29 30 var err error 31 s.apiMachine, err = s.firewaller.Machine(s.machines[0].Tag().(names.MachineTag)) 32 c.Assert(err, jc.ErrorIsNil) 33 } 34 35 func (s *machineSuite) TearDownTest(c *gc.C) { 36 s.firewallerSuite.TearDownTest(c) 37 } 38 39 func (s *machineSuite) TestMachine(c *gc.C) { 40 apiMachine42, err := s.firewaller.Machine(names.NewMachineTag("42")) 41 c.Assert(err, gc.ErrorMatches, "machine 42 not found") 42 c.Assert(err, jc.Satisfies, params.IsCodeNotFound) 43 c.Assert(apiMachine42, gc.IsNil) 44 45 apiMachine0, err := s.firewaller.Machine(s.machines[0].Tag().(names.MachineTag)) 46 c.Assert(err, jc.ErrorIsNil) 47 c.Assert(apiMachine0, gc.NotNil) 48 } 49 50 func (s *machineSuite) TestTag(c *gc.C) { 51 c.Assert(s.apiMachine.Tag(), gc.Equals, names.NewMachineTag(s.machines[0].Id())) 52 } 53 54 func (s *machineSuite) TestInstanceId(c *gc.C) { 55 // Add another, not provisioned machine to test 56 // CodeNotProvisioned. 57 newMachine, err := s.State.AddMachine("quantal", state.JobHostUnits) 58 c.Assert(err, jc.ErrorIsNil) 59 apiNewMachine, err := s.firewaller.Machine(newMachine.Tag().(names.MachineTag)) 60 c.Assert(err, jc.ErrorIsNil) 61 _, err = apiNewMachine.InstanceId() 62 c.Assert(err, gc.ErrorMatches, "machine 3 not provisioned") 63 c.Assert(err, jc.Satisfies, params.IsCodeNotProvisioned) 64 65 instanceId, err := s.apiMachine.InstanceId() 66 c.Assert(err, jc.ErrorIsNil) 67 c.Assert(instanceId, gc.Equals, instance.Id("i-manager")) 68 } 69 70 func (s *machineSuite) TestWatchUnits(c *gc.C) { 71 w, err := s.apiMachine.WatchUnits() 72 c.Assert(err, jc.ErrorIsNil) 73 wc := watchertest.NewStringsWatcherC(c, w, s.BackingState.StartSync) 74 defer wc.AssertStops() 75 76 // Initial event. 77 wc.AssertChange("wordpress/0") 78 wc.AssertNoChange() 79 80 // Change something other than the life cycle and make sure it's 81 // not detected. 82 err = s.machines[0].SetPassword("foo") 83 c.Assert(err, gc.ErrorMatches, "password is only 3 bytes long, and is not a valid Agent password") 84 wc.AssertNoChange() 85 86 err = s.machines[0].SetPassword("foo-12345678901234567890") 87 c.Assert(err, jc.ErrorIsNil) 88 wc.AssertNoChange() 89 90 // Unassign unit 0 from the machine and check it's detected. 91 err = s.units[0].UnassignFromMachine() 92 c.Assert(err, jc.ErrorIsNil) 93 wc.AssertChange("wordpress/0") 94 wc.AssertNoChange() 95 } 96 97 func (s *machineSuite) TestActiveSubnets(c *gc.C) { 98 // No ports opened at first, no active subnets. 99 subnets, err := s.apiMachine.ActiveSubnets() 100 c.Assert(err, jc.ErrorIsNil) 101 c.Assert(subnets, gc.HasLen, 0) 102 103 // Open a port and check again. 104 err = s.units[0].OpenPort("tcp", 1234) 105 c.Assert(err, jc.ErrorIsNil) 106 subnets, err = s.apiMachine.ActiveSubnets() 107 c.Assert(err, jc.ErrorIsNil) 108 c.Assert(subnets, jc.DeepEquals, []names.SubnetTag{{}}) 109 110 // Remove all ports, no more active subnets. 111 ports, err := s.machines[0].OpenedPorts("") 112 c.Assert(err, jc.ErrorIsNil) 113 err = ports.Remove() 114 c.Assert(err, jc.ErrorIsNil) 115 subnets, err = s.apiMachine.ActiveSubnets() 116 c.Assert(err, jc.ErrorIsNil) 117 c.Assert(subnets, gc.HasLen, 0) 118 } 119 120 func (s *machineSuite) TestOpenedPorts(c *gc.C) { 121 unitTag := s.units[0].Tag().(names.UnitTag) 122 123 // No ports opened at first. 124 ports, err := s.apiMachine.OpenedPorts(names.SubnetTag{}) 125 c.Assert(err, jc.ErrorIsNil) 126 c.Assert(ports, gc.HasLen, 0) 127 128 // Open a port and check again. 129 err = s.units[0].OpenPort("tcp", 1234) 130 c.Assert(err, jc.ErrorIsNil) 131 ports, err = s.apiMachine.OpenedPorts(names.SubnetTag{}) 132 c.Assert(err, jc.ErrorIsNil) 133 c.Assert(ports, jc.DeepEquals, map[network.PortRange]names.UnitTag{ 134 network.PortRange{FromPort: 1234, ToPort: 1234, Protocol: "tcp"}: unitTag, 135 }) 136 }