github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/apiserver/common/unitstatus_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package common_test
     5  
     6  import (
     7  	"errors"
     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/apiserver/common"
    14  	"github.com/juju/juju/state"
    15  	"github.com/juju/juju/status"
    16  )
    17  
    18  type UnitStatusSuite struct {
    19  	testing.IsolationSuite
    20  	unit *fakeStatusUnit
    21  }
    22  
    23  var _ = gc.Suite(&UnitStatusSuite{})
    24  
    25  func (s *UnitStatusSuite) SetUpTest(c *gc.C) {
    26  	s.unit = &fakeStatusUnit{
    27  		agentStatus: status.StatusInfo{
    28  			Status:  status.Started,
    29  			Message: "agent ok",
    30  		},
    31  		status: status.StatusInfo{
    32  			Status:  status.Idle,
    33  			Message: "unit ok",
    34  		},
    35  		presence: true,
    36  	}
    37  }
    38  
    39  func (s *UnitStatusSuite) checkUntouched(c *gc.C) {
    40  	agent, workload := common.UnitStatus(s.unit)
    41  	c.Check(agent.Status, jc.DeepEquals, s.unit.agentStatus)
    42  	c.Check(agent.Err, jc.ErrorIsNil)
    43  	c.Check(workload.Status, jc.DeepEquals, s.unit.status)
    44  	c.Check(workload.Err, jc.ErrorIsNil)
    45  }
    46  
    47  func (s *UnitStatusSuite) TestNormal(c *gc.C) {
    48  	s.checkUntouched(c)
    49  }
    50  
    51  func (s *UnitStatusSuite) TestErrors(c *gc.C) {
    52  	s.unit.agentStatusErr = errors.New("agent status error")
    53  	s.unit.statusErr = errors.New("status error")
    54  
    55  	agent, workload := common.UnitStatus(s.unit)
    56  	c.Check(agent.Err, gc.ErrorMatches, "agent status error")
    57  	c.Check(workload.Err, gc.ErrorMatches, "status error")
    58  }
    59  
    60  func (s *UnitStatusSuite) TestLost(c *gc.C) {
    61  	s.unit.presence = false
    62  	agent, workload := common.UnitStatus(s.unit)
    63  	c.Check(agent.Status, jc.DeepEquals, status.StatusInfo{
    64  		Status:  status.Lost,
    65  		Message: "agent is not communicating with the server",
    66  	})
    67  	c.Check(agent.Err, jc.ErrorIsNil)
    68  	c.Check(workload.Status, jc.DeepEquals, status.StatusInfo{
    69  		Status:  status.Unknown,
    70  		Message: "agent lost, see 'juju show-status-log foo/2'",
    71  	})
    72  	c.Check(workload.Err, jc.ErrorIsNil)
    73  }
    74  
    75  func (s *UnitStatusSuite) TestLostAndDead(c *gc.C) {
    76  	s.unit.presence = false
    77  	s.unit.life = state.Dead
    78  	// Status is untouched if unit is Dead.
    79  	s.checkUntouched(c)
    80  }
    81  
    82  func (s *UnitStatusSuite) TestPresenceError(c *gc.C) {
    83  	s.unit.presence = false
    84  	s.unit.presenceErr = errors.New("boom")
    85  	// Presence error gets ignored, so no output is unchanged.
    86  	s.checkUntouched(c)
    87  }
    88  
    89  func (s *UnitStatusSuite) TestNotLostIfAllocating(c *gc.C) {
    90  	s.unit.presence = false
    91  	s.unit.agentStatus.Status = status.Allocating
    92  	s.checkUntouched(c)
    93  }
    94  
    95  func (s *UnitStatusSuite) TestCantBeLostDuringInstall(c *gc.C) {
    96  	s.unit.presence = false
    97  	s.unit.agentStatus.Status = status.Executing
    98  	s.unit.agentStatus.Message = "running install hook"
    99  	s.checkUntouched(c)
   100  }
   101  
   102  func (s *UnitStatusSuite) TestCantBeLostDuringWorkloadInstall(c *gc.C) {
   103  	s.unit.presence = false
   104  	s.unit.status.Status = status.Maintenance
   105  	s.unit.status.Message = "installing charm software"
   106  	s.checkUntouched(c)
   107  }
   108  
   109  type fakeStatusUnit struct {
   110  	agentStatus    status.StatusInfo
   111  	agentStatusErr error
   112  	status         status.StatusInfo
   113  	statusErr      error
   114  	presence       bool
   115  	presenceErr    error
   116  	life           state.Life
   117  }
   118  
   119  func (u *fakeStatusUnit) Name() string {
   120  	return "foo/2"
   121  }
   122  
   123  func (u *fakeStatusUnit) AgentStatus() (status.StatusInfo, error) {
   124  	return u.agentStatus, u.agentStatusErr
   125  }
   126  
   127  func (u *fakeStatusUnit) Status() (status.StatusInfo, error) {
   128  	return u.status, u.statusErr
   129  }
   130  
   131  func (u *fakeStatusUnit) AgentPresence() (bool, error) {
   132  	return u.presence, u.presenceErr
   133  }
   134  
   135  func (u *fakeStatusUnit) Life() state.Life {
   136  	return u.life
   137  }