github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/api/uniter/uniter_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package uniter_test
     5  
     6  import (
     7  	"github.com/juju/names"
     8  	jc "github.com/juju/testing/checkers"
     9  	"github.com/juju/utils"
    10  	gc "gopkg.in/check.v1"
    11  
    12  	"github.com/juju/juju/api"
    13  	"github.com/juju/juju/api/base"
    14  	"github.com/juju/juju/api/uniter"
    15  	"github.com/juju/juju/juju/testing"
    16  	"github.com/juju/juju/state"
    17  )
    18  
    19  // NOTE: This suite is intended for embedding into other suites,
    20  // so common code can be reused. Do not add test cases to it,
    21  // otherwise they'll be run by each other suite that embeds it.
    22  type uniterSuite struct {
    23  	testing.JujuConnSuite
    24  
    25  	st                 api.Connection
    26  	stateServerMachine *state.Machine
    27  	wordpressMachine   *state.Machine
    28  	wordpressService   *state.Service
    29  	wordpressCharm     *state.Charm
    30  	wordpressUnit      *state.Unit
    31  
    32  	uniter *uniter.State
    33  }
    34  
    35  var _ = gc.Suite(&uniterSuite{})
    36  
    37  func (s *uniterSuite) SetUpTest(c *gc.C) {
    38  	s.setUpTest(c, true)
    39  }
    40  
    41  func (s *uniterSuite) setUpTest(c *gc.C, addStateServer bool) {
    42  	s.JujuConnSuite.SetUpTest(c)
    43  
    44  	if addStateServer {
    45  		s.stateServerMachine = testing.AddStateServerMachine(c, s.State)
    46  	}
    47  
    48  	// Create a machine, a service and add a unit so we can log in as
    49  	// its agent.
    50  	s.wordpressMachine, s.wordpressService, s.wordpressCharm, s.wordpressUnit = s.addMachineServiceCharmAndUnit(c, "wordpress")
    51  	password, err := utils.RandomPassword()
    52  	c.Assert(err, jc.ErrorIsNil)
    53  	err = s.wordpressUnit.SetPassword(password)
    54  	c.Assert(err, jc.ErrorIsNil)
    55  	s.st = s.OpenAPIAs(c, s.wordpressUnit.Tag(), password)
    56  
    57  	// Create the uniter API facade.
    58  	s.uniter, err = s.st.Uniter()
    59  	c.Assert(err, jc.ErrorIsNil)
    60  	c.Assert(s.uniter, gc.NotNil)
    61  }
    62  
    63  func (s *uniterSuite) addMachineServiceCharmAndUnit(c *gc.C, serviceName string) (*state.Machine, *state.Service, *state.Charm, *state.Unit) {
    64  	machine, err := s.State.AddMachine("quantal", state.JobHostUnits)
    65  	c.Assert(err, jc.ErrorIsNil)
    66  	charm := s.AddTestingCharm(c, serviceName)
    67  	service := s.AddTestingService(c, serviceName, charm)
    68  	unit, err := service.AddUnit()
    69  	c.Assert(err, jc.ErrorIsNil)
    70  	err = unit.AssignToMachine(machine)
    71  	c.Assert(err, jc.ErrorIsNil)
    72  	return machine, service, charm, unit
    73  }
    74  
    75  func (s *uniterSuite) addRelation(c *gc.C, first, second string) *state.Relation {
    76  	eps, err := s.State.InferEndpoints(first, second)
    77  	c.Assert(err, jc.ErrorIsNil)
    78  	rel, err := s.State.AddRelation(eps...)
    79  	c.Assert(err, jc.ErrorIsNil)
    80  	return rel
    81  }
    82  
    83  func (s *uniterSuite) addRelatedService(c *gc.C, firstSvc, relatedSvc string, unit *state.Unit) (*state.Relation, *state.Service, *state.Unit) {
    84  	relatedService := s.AddTestingService(c, relatedSvc, s.AddTestingCharm(c, relatedSvc))
    85  	rel := s.addRelation(c, firstSvc, relatedSvc)
    86  	relUnit, err := rel.Unit(unit)
    87  	c.Assert(err, jc.ErrorIsNil)
    88  	err = relUnit.EnterScope(nil)
    89  	c.Assert(err, jc.ErrorIsNil)
    90  	relatedUnit, err := s.State.Unit(relatedSvc + "/0")
    91  	c.Assert(err, jc.ErrorIsNil)
    92  	return rel, relatedService, relatedUnit
    93  }
    94  
    95  func (s *uniterSuite) assertInScope(c *gc.C, relUnit *state.RelationUnit, inScope bool) {
    96  	ok, err := relUnit.InScope()
    97  	c.Assert(err, jc.ErrorIsNil)
    98  	c.Assert(ok, gc.Equals, inScope)
    99  }
   100  
   101  func (s *uniterSuite) patchNewState(
   102  	c *gc.C,
   103  	patchFunc func(_ base.APICaller, _ names.UnitTag) *uniter.State,
   104  ) {
   105  	s.PatchValue(&uniter.NewState, patchFunc)
   106  	var err error
   107  	s.uniter, err = s.st.Uniter()
   108  	c.Assert(err, jc.ErrorIsNil)
   109  	c.Assert(s.uniter, gc.NotNil)
   110  }