github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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 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 ) 14 15 type unitSuite struct { 16 firewallerSuite 17 18 apiUnit *firewaller.Unit 19 } 20 21 var _ = gc.Suite(&unitSuite{}) 22 23 func (s *unitSuite) SetUpTest(c *gc.C) { 24 s.firewallerSuite.SetUpTest(c) 25 26 var err error 27 s.apiUnit, err = s.firewaller.Unit(s.units[0].Tag().(names.UnitTag)) 28 c.Assert(err, jc.ErrorIsNil) 29 } 30 31 func (s *unitSuite) TearDownTest(c *gc.C) { 32 s.firewallerSuite.TearDownTest(c) 33 } 34 35 func (s *unitSuite) TestUnit(c *gc.C) { 36 apiUnitFoo, err := s.firewaller.Unit(names.NewUnitTag("foo/42")) 37 c.Assert(err, gc.ErrorMatches, `unit "foo/42" not found`) 38 c.Assert(err, jc.Satisfies, params.IsCodeNotFound) 39 c.Assert(apiUnitFoo, gc.IsNil) 40 41 apiUnit0, err := s.firewaller.Unit(s.units[0].Tag().(names.UnitTag)) 42 c.Assert(err, jc.ErrorIsNil) 43 c.Assert(apiUnit0, gc.NotNil) 44 c.Assert(apiUnit0.Name(), gc.Equals, s.units[0].Name()) 45 c.Assert(apiUnit0.Tag(), gc.Equals, names.NewUnitTag(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, jc.ErrorIsNil) 53 c.Assert(s.apiUnit.Life(), gc.Equals, params.Alive) 54 55 err = s.apiUnit.Refresh() 56 c.Assert(err, jc.ErrorIsNil) 57 c.Assert(s.apiUnit.Life(), gc.Equals, params.Dead) 58 } 59 60 func (s *unitSuite) TestAssignedMachine(c *gc.C) { 61 machineTag, err := s.apiUnit.AssignedMachine() 62 c.Assert(err, jc.ErrorIsNil) 63 c.Assert(machineTag, gc.Equals, names.NewMachineTag(s.machines[0].Id())) 64 65 // Unassign now and check CodeNotAssigned is reported. 66 err = s.units[0].UnassignFromMachine() 67 c.Assert(err, jc.ErrorIsNil) 68 _, err = s.apiUnit.AssignedMachine() 69 c.Assert(err, gc.ErrorMatches, `unit "wordpress/0" is not assigned to a machine`) 70 c.Assert(err, jc.Satisfies, params.IsCodeNotAssigned) 71 } 72 73 func (s *unitSuite) TestService(c *gc.C) { 74 service, err := s.apiUnit.Application() 75 c.Assert(err, jc.ErrorIsNil) 76 c.Assert(service.Name(), gc.Equals, s.application.Name()) 77 } 78 79 func (s *unitSuite) TestName(c *gc.C) { 80 c.Assert(s.apiUnit.Name(), gc.Equals, s.units[0].Name()) 81 }