github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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  
    13  	"github.com/juju/juju/apiserver/common"
    14  	"github.com/juju/juju/state"
    15  	"github.com/juju/juju/status"
    16  )
    17  
    18  type MachineStatusSuite struct {
    19  	testing.IsolationSuite
    20  	machine *mockMachine
    21  }
    22  
    23  var _ = gc.Suite(&MachineStatusSuite{})
    24  
    25  func (s *MachineStatusSuite) SetUpTest(c *gc.C) {
    26  	s.machine = &mockMachine{
    27  		status: status.Started,
    28  	}
    29  }
    30  
    31  func (s *MachineStatusSuite) checkUntouched(c *gc.C) {
    32  	agent, err := common.MachineStatus(s.machine)
    33  	c.Check(err, jc.ErrorIsNil)
    34  	c.Assert(agent.Status, jc.DeepEquals, s.machine.status)
    35  }
    36  
    37  func (s *MachineStatusSuite) TestNormal(c *gc.C) {
    38  	s.checkUntouched(c)
    39  }
    40  
    41  func (s *MachineStatusSuite) TestErrors(c *gc.C) {
    42  	s.machine.statusErr = errors.New("status error")
    43  
    44  	_, err := common.MachineStatus(s.machine)
    45  	c.Assert(err, gc.ErrorMatches, "status error")
    46  }
    47  
    48  func (s *MachineStatusSuite) TestDown(c *gc.C) {
    49  	s.machine.agentDead = true
    50  	agent, err := common.MachineStatus(s.machine)
    51  	c.Assert(err, jc.ErrorIsNil)
    52  	c.Assert(agent, jc.DeepEquals, status.StatusInfo{
    53  		Status:  status.Down,
    54  		Message: "agent is not communicating with the server",
    55  	})
    56  }
    57  
    58  func (s *MachineStatusSuite) TestDownAndDead(c *gc.C) {
    59  	s.machine.agentDead = true
    60  	s.machine.life = state.Dead
    61  	// Status is untouched if unit is Dead.
    62  	s.checkUntouched(c)
    63  }
    64  
    65  func (s *MachineStatusSuite) TestPresenceError(c *gc.C) {
    66  	s.machine.agentDead = true
    67  	s.machine.presenceErr = errors.New("boom")
    68  	// Presence error gets ignored, so no output is unchanged.
    69  	s.checkUntouched(c)
    70  }
    71  
    72  func (s *MachineStatusSuite) TestNotDownIfPending(c *gc.C) {
    73  	s.machine.agentDead = true
    74  	s.machine.status = status.Pending
    75  	s.checkUntouched(c)
    76  }