github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/state/api/firewaller/unit_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  	gc "launchpad.net/gocheck"
     8  
     9  	"launchpad.net/juju-core/instance"
    10  	"launchpad.net/juju-core/state/api/firewaller"
    11  	"launchpad.net/juju-core/state/api/params"
    12  	statetesting "launchpad.net/juju-core/state/testing"
    13  	jc "launchpad.net/juju-core/testing/checkers"
    14  )
    15  
    16  type unitSuite struct {
    17  	firewallerSuite
    18  
    19  	apiUnit *firewaller.Unit
    20  }
    21  
    22  var _ = gc.Suite(&unitSuite{})
    23  
    24  func (s *unitSuite) SetUpTest(c *gc.C) {
    25  	s.firewallerSuite.SetUpTest(c)
    26  
    27  	var err error
    28  	s.apiUnit, err = s.firewaller.Unit(s.units[0].Tag())
    29  	c.Assert(err, gc.IsNil)
    30  }
    31  
    32  func (s *unitSuite) TearDownTest(c *gc.C) {
    33  	s.firewallerSuite.TearDownTest(c)
    34  }
    35  
    36  func (s *unitSuite) TestUnit(c *gc.C) {
    37  	apiUnitFoo, err := s.firewaller.Unit("unit-foo-42")
    38  	c.Assert(err, gc.ErrorMatches, `unit "foo/42" not found`)
    39  	c.Assert(err, jc.Satisfies, params.IsCodeNotFound)
    40  	c.Assert(apiUnitFoo, gc.IsNil)
    41  
    42  	apiUnit0, err := s.firewaller.Unit(s.units[0].Tag())
    43  	c.Assert(err, gc.IsNil)
    44  	c.Assert(apiUnit0, gc.NotNil)
    45  	c.Assert(apiUnit0.Name(), gc.Equals, s.units[0].Name())
    46  }
    47  
    48  func (s *unitSuite) TestRefresh(c *gc.C) {
    49  	c.Assert(s.apiUnit.Life(), gc.Equals, params.Alive)
    50  
    51  	err := s.units[0].EnsureDead()
    52  	c.Assert(err, gc.IsNil)
    53  	c.Assert(s.apiUnit.Life(), gc.Equals, params.Alive)
    54  
    55  	err = s.apiUnit.Refresh()
    56  	c.Assert(err, gc.IsNil)
    57  	c.Assert(s.apiUnit.Life(), gc.Equals, params.Dead)
    58  }
    59  
    60  func (s *unitSuite) TestWatch(c *gc.C) {
    61  	c.Assert(s.apiUnit.Life(), gc.Equals, params.Alive)
    62  
    63  	w, err := s.apiUnit.Watch()
    64  	c.Assert(err, gc.IsNil)
    65  	defer statetesting.AssertStop(c, w)
    66  	wc := statetesting.NewNotifyWatcherC(c, s.BackingState, w)
    67  
    68  	// Initial event.
    69  	wc.AssertOneChange()
    70  
    71  	// Change something other than the life cycle and make sure it's
    72  	// not detected.
    73  	err = s.units[0].SetStatus(params.StatusStarted, "not really", nil)
    74  	c.Assert(err, gc.IsNil)
    75  	wc.AssertNoChange()
    76  
    77  	// Make the unit dead and check it's detected.
    78  	err = s.units[0].EnsureDead()
    79  	c.Assert(err, gc.IsNil)
    80  	wc.AssertOneChange()
    81  
    82  	statetesting.AssertStop(c, w)
    83  	wc.AssertClosed()
    84  }
    85  
    86  func (s *unitSuite) TestAssignedMachine(c *gc.C) {
    87  	machineTag, err := s.apiUnit.AssignedMachine()
    88  	c.Assert(err, gc.IsNil)
    89  	c.Assert(machineTag, gc.Equals, s.machines[0].Tag())
    90  
    91  	// Unassign now and check CodeNotAssigned is reported.
    92  	err = s.units[0].UnassignFromMachine()
    93  	c.Assert(err, gc.IsNil)
    94  	_, err = s.apiUnit.AssignedMachine()
    95  	c.Assert(err, gc.ErrorMatches, `unit "wordpress/0" is not assigned to a machine`)
    96  	c.Assert(err, jc.Satisfies, params.IsCodeNotAssigned)
    97  }
    98  
    99  func (s *unitSuite) TestOpenedPorts(c *gc.C) {
   100  	ports, err := s.apiUnit.OpenedPorts()
   101  	c.Assert(err, gc.IsNil)
   102  	c.Assert(ports, jc.DeepEquals, []instance.Port{})
   103  
   104  	// Open some ports and check again.
   105  	err = s.units[0].OpenPort("foo", 1234)
   106  	c.Assert(err, gc.IsNil)
   107  	err = s.units[0].OpenPort("bar", 4321)
   108  	c.Assert(err, gc.IsNil)
   109  	ports, err = s.apiUnit.OpenedPorts()
   110  	c.Assert(err, gc.IsNil)
   111  	c.Assert(ports, jc.DeepEquals, []instance.Port{{"bar", 4321}, {"foo", 1234}})
   112  }
   113  
   114  func (s *unitSuite) TestService(c *gc.C) {
   115  	service, err := s.apiUnit.Service()
   116  	c.Assert(err, gc.IsNil)
   117  	c.Assert(service.Name(), gc.Equals, s.service.Name())
   118  }
   119  
   120  func (s *unitSuite) TestName(c *gc.C) {
   121  	c.Assert(s.apiUnit.Name(), gc.Equals, s.units[0].Name())
   122  }