github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/api/agent/unit_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package agent_test
     5  
     6  import (
     7  	"fmt"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	"github.com/juju/utils"
    11  	gc "gopkg.in/check.v1"
    12  	"gopkg.in/juju/names.v2"
    13  
    14  	"github.com/juju/juju/api"
    15  	apiagent "github.com/juju/juju/api/agent"
    16  	"github.com/juju/juju/apiserver/params"
    17  	"github.com/juju/juju/juju/testing"
    18  	"github.com/juju/juju/state"
    19  )
    20  
    21  var _ = gc.Suite(&unitSuite{})
    22  
    23  type unitSuite struct {
    24  	testing.JujuConnSuite
    25  	unit *state.Unit
    26  	st   api.Connection
    27  }
    28  
    29  func (s *unitSuite) SetUpTest(c *gc.C) {
    30  	var err error
    31  	s.JujuConnSuite.SetUpTest(c)
    32  	app := s.AddTestingApplication(c, "wordpress", s.AddTestingCharm(c, "wordpress"))
    33  	s.unit, err = app.AddUnit(state.AddUnitParams{})
    34  	c.Assert(err, jc.ErrorIsNil)
    35  	password, err := utils.RandomPassword()
    36  	c.Assert(err, jc.ErrorIsNil)
    37  	err = s.unit.SetPassword(password)
    38  	c.Assert(err, jc.ErrorIsNil)
    39  
    40  	s.st = s.OpenAPIAs(c, s.unit.Tag(), password)
    41  }
    42  
    43  func (s *unitSuite) TestUnitEntity(c *gc.C) {
    44  	tag := names.NewUnitTag("wordpress/1")
    45  	apiSt, err := apiagent.NewState(s.st)
    46  	c.Assert(err, jc.ErrorIsNil)
    47  	m, err := apiSt.Entity(tag)
    48  	c.Assert(err, gc.ErrorMatches, "permission denied")
    49  	c.Assert(err, jc.Satisfies, params.IsCodeUnauthorized)
    50  	c.Assert(m, gc.IsNil)
    51  
    52  	apiSt, err = apiagent.NewState(s.st)
    53  	m, err = apiSt.Entity(s.unit.Tag())
    54  	c.Assert(err, jc.ErrorIsNil)
    55  	c.Assert(m.Tag(), gc.Equals, s.unit.Tag().String())
    56  	c.Assert(m.Life(), gc.Equals, params.Alive)
    57  	c.Assert(m.Jobs(), gc.HasLen, 0)
    58  
    59  	err = s.unit.EnsureDead()
    60  	c.Assert(err, jc.ErrorIsNil)
    61  	err = s.unit.Remove()
    62  	c.Assert(err, jc.ErrorIsNil)
    63  
    64  	apiSt, err = apiagent.NewState(s.st)
    65  	m, err = apiSt.Entity(s.unit.Tag())
    66  	c.Assert(err, gc.ErrorMatches, fmt.Sprintf("unit %q not found", s.unit.Name()))
    67  	c.Assert(err, jc.Satisfies, params.IsCodeNotFound)
    68  	c.Assert(m, gc.IsNil)
    69  }