github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/common/machinestatus_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  	"gopkg.in/juju/names.v2"
    13  
    14  	"github.com/juju/juju/apiserver/common"
    15  	"github.com/juju/juju/core/status"
    16  	"github.com/juju/juju/state"
    17  )
    18  
    19  type MachineStatusSuite struct {
    20  	testing.IsolationSuite
    21  	ctx     common.ModelPresenceContext
    22  	machine *mockMachine
    23  }
    24  
    25  var _ = gc.Suite(&MachineStatusSuite{})
    26  
    27  func (s *MachineStatusSuite) SetUpTest(c *gc.C) {
    28  	s.machine = &mockMachine{
    29  		id:     "666",
    30  		status: status.Started,
    31  	}
    32  	s.ctx = common.ModelPresenceContext{
    33  		Presence: agentAlive(names.NewMachineTag(s.machine.id).String()),
    34  	}
    35  }
    36  
    37  func (s *MachineStatusSuite) checkUntouched(c *gc.C) {
    38  	agent, err := s.ctx.MachineStatus(s.machine)
    39  	c.Check(err, jc.ErrorIsNil)
    40  	c.Assert(agent.Status, jc.DeepEquals, s.machine.status)
    41  }
    42  
    43  func (s *MachineStatusSuite) TestNormal(c *gc.C) {
    44  	s.checkUntouched(c)
    45  }
    46  
    47  func (s *MachineStatusSuite) TestErrors(c *gc.C) {
    48  	s.machine.statusErr = errors.New("status error")
    49  
    50  	_, err := s.ctx.MachineStatus(s.machine)
    51  	c.Assert(err, gc.ErrorMatches, "status error")
    52  }
    53  
    54  func (s *MachineStatusSuite) TestDownLegacy(c *gc.C) {
    55  	s.ctx.Presence = nil
    56  	s.machine.agentDead = true
    57  	agent, err := s.ctx.MachineStatus(s.machine)
    58  	c.Assert(err, jc.ErrorIsNil)
    59  	c.Assert(agent, jc.DeepEquals, status.StatusInfo{
    60  		Status:  status.Down,
    61  		Message: "agent is not communicating with the server",
    62  	})
    63  }
    64  
    65  func (s *MachineStatusSuite) TestDown(c *gc.C) {
    66  	s.ctx.Presence = agentDown(names.NewMachineTag(s.machine.Id()).String())
    67  	agent, err := s.ctx.MachineStatus(s.machine)
    68  	c.Assert(err, jc.ErrorIsNil)
    69  	c.Assert(agent, jc.DeepEquals, status.StatusInfo{
    70  		Status:  status.Down,
    71  		Message: "agent is not communicating with the server",
    72  	})
    73  }
    74  
    75  func (s *MachineStatusSuite) TestDownAndDeadLegacy(c *gc.C) {
    76  	s.ctx.Presence = nil
    77  	s.machine.agentDead = true
    78  	s.machine.life = state.Dead
    79  	// Status is untouched if unit is Dead.
    80  	s.checkUntouched(c)
    81  }
    82  
    83  func (s *MachineStatusSuite) TestDownAndDead(c *gc.C) {
    84  	s.ctx.Presence = agentDown(names.NewMachineTag(s.machine.Id()).String())
    85  	s.machine.life = state.Dead
    86  	// Status is untouched if unit is Dead.
    87  	s.checkUntouched(c)
    88  }
    89  
    90  func (s *MachineStatusSuite) TestPresenceErrorLegacy(c *gc.C) {
    91  	s.ctx.Presence = nil
    92  	s.machine.agentDead = true
    93  	s.machine.presenceErr = errors.New("boom")
    94  	// Presence error gets ignored, so no output is unchanged.
    95  	s.checkUntouched(c)
    96  }
    97  
    98  func (s *MachineStatusSuite) TestPresenceError(c *gc.C) {
    99  	s.ctx.Presence = presenceError(names.NewMachineTag(s.machine.Id()).String())
   100  	// Presence error gets ignored, so no output is unchanged.
   101  	s.checkUntouched(c)
   102  }
   103  
   104  func (s *MachineStatusSuite) TestNotDownIfPendingLegacy(c *gc.C) {
   105  	s.ctx.Presence = nil
   106  	s.machine.agentDead = true
   107  	s.machine.status = status.Pending
   108  	s.checkUntouched(c)
   109  }
   110  
   111  func (s *MachineStatusSuite) TestNotDownIfPending(c *gc.C) {
   112  	s.ctx.Presence = agentDown(names.NewMachineTag(s.machine.Id()).String())
   113  	s.machine.status = status.Pending
   114  	s.checkUntouched(c)
   115  }