github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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 controllerMachine *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, addController bool) { 42 s.JujuConnSuite.SetUpTest(c) 43 44 if addController { 45 s.controllerMachine = testing.AddControllerMachine(c, s.State) 46 } 47 48 // Bind "db" relation of wordpress to space "internal", 49 // and the "admin-api" extra-binding to space "public". 50 bindings := map[string]string{ 51 "db": "internal", 52 "admin-api": "public", 53 } 54 _, err := s.State.AddSpace("internal", "", nil, false) 55 c.Assert(err, jc.ErrorIsNil) 56 _, err = s.State.AddSpace("public", "", nil, true) 57 c.Assert(err, jc.ErrorIsNil) 58 59 // Create a machine, a service and add a unit so we can log in as 60 // its agent. 61 s.wordpressMachine, s.wordpressService, s.wordpressCharm, s.wordpressUnit = s.addMachineBoundServiceCharmAndUnit(c, "wordpress", bindings) 62 password, err := utils.RandomPassword() 63 c.Assert(err, jc.ErrorIsNil) 64 err = s.wordpressUnit.SetPassword(password) 65 c.Assert(err, jc.ErrorIsNil) 66 s.st = s.OpenAPIAs(c, s.wordpressUnit.Tag(), password) 67 68 // Create the uniter API facade. 69 s.uniter, err = s.st.Uniter() 70 c.Assert(err, jc.ErrorIsNil) 71 c.Assert(s.uniter, gc.NotNil) 72 } 73 74 func (s *uniterSuite) addMachineBoundServiceCharmAndUnit(c *gc.C, serviceName string, bindings map[string]string) (*state.Machine, *state.Service, *state.Charm, *state.Unit) { 75 machine, err := s.State.AddMachine("quantal", state.JobHostUnits) 76 c.Assert(err, jc.ErrorIsNil) 77 charm := s.AddTestingCharm(c, serviceName) 78 79 owner := s.AdminUserTag(c).String() 80 service, err := s.State.AddService(state.AddServiceArgs{ 81 Name: serviceName, 82 Owner: owner, 83 Charm: charm, 84 EndpointBindings: bindings, 85 }) 86 c.Assert(err, jc.ErrorIsNil) 87 88 unit, err := service.AddUnit() 89 c.Assert(err, jc.ErrorIsNil) 90 91 err = unit.AssignToMachine(machine) 92 c.Assert(err, jc.ErrorIsNil) 93 94 return machine, service, charm, unit 95 } 96 97 func (s *uniterSuite) addMachineServiceCharmAndUnit(c *gc.C, serviceName string) (*state.Machine, *state.Service, *state.Charm, *state.Unit) { 98 return s.addMachineBoundServiceCharmAndUnit(c, serviceName, nil) 99 } 100 101 func (s *uniterSuite) addRelation(c *gc.C, first, second string) *state.Relation { 102 eps, err := s.State.InferEndpoints(first, second) 103 c.Assert(err, jc.ErrorIsNil) 104 rel, err := s.State.AddRelation(eps...) 105 c.Assert(err, jc.ErrorIsNil) 106 return rel 107 } 108 109 func (s *uniterSuite) addRelatedService(c *gc.C, firstSvc, relatedSvc string, unit *state.Unit) (*state.Relation, *state.Service, *state.Unit) { 110 relatedService := s.AddTestingService(c, relatedSvc, s.AddTestingCharm(c, relatedSvc)) 111 rel := s.addRelation(c, firstSvc, relatedSvc) 112 relUnit, err := rel.Unit(unit) 113 c.Assert(err, jc.ErrorIsNil) 114 err = relUnit.EnterScope(nil) 115 c.Assert(err, jc.ErrorIsNil) 116 relatedUnit, err := s.State.Unit(relatedSvc + "/0") 117 c.Assert(err, jc.ErrorIsNil) 118 return rel, relatedService, relatedUnit 119 } 120 121 func (s *uniterSuite) assertInScope(c *gc.C, relUnit *state.RelationUnit, inScope bool) { 122 ok, err := relUnit.InScope() 123 c.Assert(err, jc.ErrorIsNil) 124 c.Assert(ok, gc.Equals, inScope) 125 } 126 127 func (s *uniterSuite) patchNewState( 128 c *gc.C, 129 patchFunc func(_ base.APICaller, _ names.UnitTag) *uniter.State, 130 ) { 131 s.PatchValue(&uniter.NewState, patchFunc) 132 var err error 133 s.uniter, err = s.st.Uniter() 134 c.Assert(err, jc.ErrorIsNil) 135 c.Assert(s.uniter, gc.NotNil) 136 }