github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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 jc "github.com/juju/testing/checkers" 8 "github.com/juju/utils" 9 gc "gopkg.in/check.v1" 10 11 "github.com/juju/juju/api" 12 "github.com/juju/juju/api/uniter" 13 "github.com/juju/juju/juju/testing" 14 "github.com/juju/juju/state" 15 ) 16 17 // NOTE: This suite is intended for embedding into other suites, 18 // so common code can be reused. Do not add test cases to it, 19 // otherwise they'll be run by each other suite that embeds it. 20 type uniterSuite struct { 21 testing.JujuConnSuite 22 23 st api.Connection 24 controllerMachine *state.Machine 25 wordpressMachine *state.Machine 26 wordpressApplication *state.Application 27 wordpressCharm *state.Charm 28 wordpressUnit *state.Unit 29 30 uniter *uniter.State 31 } 32 33 var _ = gc.Suite(&uniterSuite{}) 34 35 func (s *uniterSuite) SetUpTest(c *gc.C) { 36 s.setUpTest(c, true) 37 } 38 39 func (s *uniterSuite) setUpTest(c *gc.C, addController bool) { 40 s.JujuConnSuite.SetUpTest(c) 41 42 if addController { 43 s.controllerMachine = testing.AddControllerMachine(c, s.State) 44 } 45 46 // Bind "db" relation of wordpress to space "internal", 47 // and the "admin-api" extra-binding to space "public". 48 bindings := map[string]string{ 49 "db": "internal", 50 "admin-api": "public", 51 } 52 _, err := s.State.AddSpace("internal", "", nil, false) 53 c.Assert(err, jc.ErrorIsNil) 54 _, err = s.State.AddSpace("public", "", nil, true) 55 c.Assert(err, jc.ErrorIsNil) 56 57 // Create a machine, a application and add a unit so we can log in as 58 // its agent. 59 s.wordpressMachine, s.wordpressApplication, s.wordpressCharm, s.wordpressUnit = 60 s.addMachineBoundAppCharmAndUnit(c, "wordpress", bindings) 61 password, err := utils.RandomPassword() 62 c.Assert(err, jc.ErrorIsNil) 63 err = s.wordpressUnit.SetPassword(password) 64 c.Assert(err, jc.ErrorIsNil) 65 s.st = s.OpenAPIAs(c, s.wordpressUnit.Tag(), password) 66 67 // Create the uniter API facade. 68 s.uniter, err = s.st.Uniter() 69 c.Assert(err, jc.ErrorIsNil) 70 c.Assert(s.uniter, gc.NotNil) 71 } 72 73 func (s *uniterSuite) addMachineBoundAppCharmAndUnit( 74 c *gc.C, appName string, bindings map[string]string, 75 ) (*state.Machine, *state.Application, *state.Charm, *state.Unit) { 76 machine, err := s.State.AddMachine("quantal", state.JobHostUnits) 77 c.Assert(err, jc.ErrorIsNil) 78 charm := s.AddTestingCharm(c, appName) 79 80 app, err := s.State.AddApplication(state.AddApplicationArgs{ 81 Name: appName, 82 Charm: charm, 83 EndpointBindings: bindings, 84 }) 85 c.Assert(err, jc.ErrorIsNil) 86 87 unit, err := app.AddUnit(state.AddUnitParams{}) 88 c.Assert(err, jc.ErrorIsNil) 89 90 err = unit.AssignToMachine(machine) 91 c.Assert(err, jc.ErrorIsNil) 92 93 return machine, app, charm, unit 94 } 95 96 func (s *uniterSuite) addMachineAppCharmAndUnit(c *gc.C, appName string, 97 ) (*state.Machine, *state.Application, *state.Charm, *state.Unit) { 98 return s.addMachineBoundAppCharmAndUnit(c, appName, 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) addRelatedApplication(c *gc.C, firstApp, relatedApp string, unit *state.Unit, 110 ) (*state.Relation, *state.Application, *state.Unit) { 111 relatedApplication := s.AddTestingApplication(c, relatedApp, s.AddTestingCharm(c, relatedApp)) 112 rel := s.addRelation(c, firstApp, relatedApp) 113 relUnit, err := rel.Unit(unit) 114 c.Assert(err, jc.ErrorIsNil) 115 err = relUnit.EnterScope(nil) 116 c.Assert(err, jc.ErrorIsNil) 117 relatedUnit, err := s.State.Unit(relatedApp + "/0") 118 c.Assert(err, jc.ErrorIsNil) 119 return rel, relatedApplication, relatedUnit 120 } 121 122 func (s *uniterSuite) addRelationSuspended(c *gc.C, firstApp, relatedApp string, unit *state.Unit) *state.Relation { 123 s.AddTestingApplication(c, relatedApp, s.AddTestingCharm(c, relatedApp)) 124 rel := s.addRelation(c, firstApp, relatedApp) 125 err := rel.SetSuspended(true, "") 126 c.Assert(err, jc.ErrorIsNil) 127 return rel 128 } 129 130 func (s *uniterSuite) assertInScope(c *gc.C, relUnit *state.RelationUnit, inScope bool) { 131 ok, err := relUnit.InScope() 132 c.Assert(err, jc.ErrorIsNil) 133 c.Assert(ok, gc.Equals, inScope) 134 }