~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/api/uniter/uniter_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        "gopkg.in/juju/names.v2"
 
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.Application
 
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.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)
 
78
 
 
79
        service, err := s.State.AddApplication(state.AddApplicationArgs{
 
80
                Name:             serviceName,
 
81
                Charm:            charm,
 
82
                EndpointBindings: bindings,
 
83
        })
 
84
        c.Assert(err, jc.ErrorIsNil)
 
85
 
 
86
        unit, err := service.AddUnit()
 
87
        c.Assert(err, jc.ErrorIsNil)
 
88
 
 
89
        err = unit.AssignToMachine(machine)
 
90
        c.Assert(err, jc.ErrorIsNil)
 
91
 
 
92
        return machine, service, charm, unit
 
93
}
 
94
 
 
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)
 
97
}
 
98
 
 
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)
 
104
        return rel
 
105
}
 
106
 
 
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
 
117
}
 
118
 
 
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)
 
123
}
 
124
 
 
125
func (s *uniterSuite) patchNewState(
 
126
        c *gc.C,
 
127
        patchFunc func(_ base.APICaller, _ names.UnitTag) *uniter.State,
 
128
) {
 
129
        s.PatchValue(&uniter.NewState, patchFunc)
 
130
        var err error
 
131
        s.uniter, err = s.st.Uniter()
 
132
        c.Assert(err, jc.ErrorIsNil)
 
133
        c.Assert(s.uniter, gc.NotNil)
 
134
}