1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
7
jc "github.com/juju/testing/checkers"
8
"github.com/juju/utils"
10
"gopkg.in/juju/names.v2"
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"
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 {
26
controllerMachine *state.Machine
27
wordpressMachine *state.Machine
28
wordpressService *state.Application
29
wordpressCharm *state.Charm
30
wordpressUnit *state.Unit
35
var _ = gc.Suite(&uniterSuite{})
37
func (s *uniterSuite) SetUpTest(c *gc.C) {
41
func (s *uniterSuite) setUpTest(c *gc.C, addController bool) {
42
s.JujuConnSuite.SetUpTest(c)
45
s.controllerMachine = testing.AddControllerMachine(c, s.State)
48
// Bind "db" relation of wordpress to space "internal",
49
// and the "admin-api" extra-binding to space "public".
50
bindings := map[string]string{
52
"admin-api": "public",
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)
59
// Create a machine, a service and add a unit so we can log in as
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)
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)
74
func (s *uniterSuite) addMachineBoundServiceCharmAndUnit(c *gc.C, serviceName string, bindings map[string]string) (*state.Machine, *state.Application, *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)
79
service, err := s.State.AddApplication(state.AddApplicationArgs{
82
EndpointBindings: bindings,
84
c.Assert(err, jc.ErrorIsNil)
86
unit, err := service.AddUnit()
87
c.Assert(err, jc.ErrorIsNil)
89
err = unit.AssignToMachine(machine)
90
c.Assert(err, jc.ErrorIsNil)
92
return machine, service, charm, unit
95
func (s *uniterSuite) addMachineServiceCharmAndUnit(c *gc.C, serviceName string) (*state.Machine, *state.Application, *state.Charm, *state.Unit) {
96
return s.addMachineBoundServiceCharmAndUnit(c, serviceName, nil)
99
func (s *uniterSuite) addRelation(c *gc.C, first, second string) *state.Relation {
100
eps, err := s.State.InferEndpoints(first, second)
101
c.Assert(err, jc.ErrorIsNil)
102
rel, err := s.State.AddRelation(eps...)
103
c.Assert(err, jc.ErrorIsNil)
107
func (s *uniterSuite) addRelatedService(c *gc.C, firstSvc, relatedSvc string, unit *state.Unit) (*state.Relation, *state.Application, *state.Unit) {
108
relatedService := s.AddTestingService(c, relatedSvc, s.AddTestingCharm(c, relatedSvc))
109
rel := s.addRelation(c, firstSvc, relatedSvc)
110
relUnit, err := rel.Unit(unit)
111
c.Assert(err, jc.ErrorIsNil)
112
err = relUnit.EnterScope(nil)
113
c.Assert(err, jc.ErrorIsNil)
114
relatedUnit, err := s.State.Unit(relatedSvc + "/0")
115
c.Assert(err, jc.ErrorIsNil)
116
return rel, relatedService, relatedUnit
119
func (s *uniterSuite) assertInScope(c *gc.C, relUnit *state.RelationUnit, inScope bool) {
120
ok, err := relUnit.InScope()
121
c.Assert(err, jc.ErrorIsNil)
122
c.Assert(ok, gc.Equals, inScope)
125
func (s *uniterSuite) patchNewState(
127
patchFunc func(_ base.APICaller, _ names.UnitTag) *uniter.State,
129
s.PatchValue(&uniter.NewState, patchFunc)
131
s.uniter, err = s.st.Uniter()
132
c.Assert(err, jc.ErrorIsNil)
133
c.Assert(s.uniter, gc.NotNil)