~themue/juju-core/053-env-more-script-friendly

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package deployer_test

import (
	"sort"
	"strings"
	stdtesting "testing"
	"time"

	. "launchpad.net/gocheck"

	"launchpad.net/juju-core/errors"
	jujutesting "launchpad.net/juju-core/juju/testing"
	"launchpad.net/juju-core/names"
	"launchpad.net/juju-core/state"
	"launchpad.net/juju-core/state/api"
	apideployer "launchpad.net/juju-core/state/api/deployer"
	"launchpad.net/juju-core/state/api/params"
	coretesting "launchpad.net/juju-core/testing"
	"launchpad.net/juju-core/worker/deployer"
)

func TestPackage(t *stdtesting.T) {
	coretesting.MgoTestPackage(t)
}

type DeployerSuite struct {
	jujutesting.JujuConnSuite
	SimpleToolsFixture

	machine       *state.Machine
	stateAPI      *api.State
	deployerState *apideployer.State
}

var _ = Suite(&DeployerSuite{})

func (s *DeployerSuite) SetUpTest(c *C) {
	s.JujuConnSuite.SetUpTest(c)
	s.SimpleToolsFixture.SetUp(c, s.DataDir())

	// Create a machine to work with.
	var err error
	s.machine, err = s.State.AddMachine("series", state.JobHostUnits)
	c.Assert(err, IsNil)
	err = s.machine.SetProvisioned("foo", "fake_nonce", nil)
	c.Assert(err, IsNil)
	err = s.machine.SetPassword("test-password")
	c.Assert(err, IsNil)

	// Log in as the machine agent of the created machine.
	s.stateAPI = s.OpenAPIAsMachine(c, s.machine.Tag(), "test-password", "fake_nonce")
	c.Assert(s.stateAPI, NotNil)

	// Create the deployer facade.
	s.deployerState = s.stateAPI.Deployer()
	c.Assert(s.deployerState, NotNil)
}

func (s *DeployerSuite) TearDownTest(c *C) {
	if s.stateAPI != nil {
		err := s.stateAPI.Close()
		c.Check(err, IsNil)
	}
	s.SimpleToolsFixture.TearDown(c)
	s.JujuConnSuite.TearDownTest(c)
}

var _ = (*deployer.Deployer)(nil)

func (s *DeployerSuite) TestDeployRecallRemovePrincipals(c *C) {
	// Create a machine, and a couple of units.
	svc, err := s.State.AddService("wordpress", s.AddTestingCharm(c, "wordpress"))
	c.Assert(err, IsNil)
	u0, err := svc.AddUnit()
	c.Assert(err, IsNil)
	u1, err := svc.AddUnit()
	c.Assert(err, IsNil)

	// Create a deployer acting on behalf of the machine.
	ctx := s.getContext(c)
	dep := deployer.NewDeployer(s.deployerState, ctx, s.machine.Tag())
	defer stop(c, dep)

	// Assign one unit, and wait for it to be deployed.
	err = u0.AssignToMachine(s.machine)
	c.Assert(err, IsNil)
	s.waitFor(c, isDeployed(ctx, u0.Name()))

	// Assign another unit, and wait for that to be deployed.
	err = u1.AssignToMachine(s.machine)
	c.Assert(err, IsNil)
	s.waitFor(c, isDeployed(ctx, u0.Name(), u1.Name()))

	// Cause a unit to become Dying, and check no change.
	err = u1.SetStatus(params.StatusInstalled, "")
	c.Assert(err, IsNil)
	err = u1.Destroy()
	c.Assert(err, IsNil)
	s.waitFor(c, isDeployed(ctx, u0.Name(), u1.Name()))

	// Cause a unit to become Dead, and check that it is both recalled and
	// removed from state.
	err = u0.EnsureDead()
	c.Assert(err, IsNil)
	s.waitFor(c, isRemoved(s.State, u0.Name()))
	s.waitFor(c, isDeployed(ctx, u1.Name()))

	// Remove the Dying unit from the machine, and check that it is recalled...
	err = u1.UnassignFromMachine()
	c.Assert(err, IsNil)
	s.waitFor(c, isDeployed(ctx))

	// ...and that the deployer, no longer bearing any responsibility for the
	// Dying unit, does nothing further to it.
	err = u1.Refresh()
	c.Assert(err, IsNil)
	c.Assert(u1.Life(), Equals, state.Dying)
}

func (s *DeployerSuite) TestRemoveNonAlivePrincipals(c *C) {
	// Create a service, and a couple of units.
	svc, err := s.State.AddService("wordpress", s.AddTestingCharm(c, "wordpress"))
	c.Assert(err, IsNil)
	u0, err := svc.AddUnit()
	c.Assert(err, IsNil)
	u1, err := svc.AddUnit()
	c.Assert(err, IsNil)

	// Assign the units to the machine, and set them to Dying/Dead.
	err = u0.AssignToMachine(s.machine)
	c.Assert(err, IsNil)
	err = u0.EnsureDead()
	c.Assert(err, IsNil)
	err = u1.AssignToMachine(s.machine)
	c.Assert(err, IsNil)
	// note: this is not a sane state; for the unit to have a status it must
	// have been deployed. But it's instructive to check that the right thing
	// would happen if it were possible to have a dying unit in this situation.
	err = u1.SetStatus(params.StatusInstalled, "")
	c.Assert(err, IsNil)
	err = u1.Destroy()
	c.Assert(err, IsNil)

	// When the deployer is started, in each case (1) no unit agent is deployed
	// and (2) the non-Alive unit is been removed from state.
	ctx := s.getContext(c)
	dep := deployer.NewDeployer(s.deployerState, ctx, s.machine.Tag())
	defer stop(c, dep)
	s.waitFor(c, isRemoved(s.State, u0.Name()))
	s.waitFor(c, isRemoved(s.State, u1.Name()))
	s.waitFor(c, isDeployed(ctx))
}

func (s *DeployerSuite) prepareSubordinates(c *C) (*state.Unit, []*state.RelationUnit) {
	svc, err := s.State.AddService("wordpress", s.AddTestingCharm(c, "wordpress"))
	c.Assert(err, IsNil)
	u, err := svc.AddUnit()
	c.Assert(err, IsNil)
	err = u.AssignToMachine(s.machine)
	c.Assert(err, IsNil)
	rus := []*state.RelationUnit{}
	logging := s.AddTestingCharm(c, "logging")
	for _, name := range []string{"subsvc0", "subsvc1"} {
		_, err := s.State.AddService(name, logging)
		c.Assert(err, IsNil)
		eps, err := s.State.InferEndpoints([]string{"wordpress", name})
		c.Assert(err, IsNil)
		rel, err := s.State.AddRelation(eps...)
		c.Assert(err, IsNil)
		ru, err := rel.Unit(u)
		c.Assert(err, IsNil)
		rus = append(rus, ru)
	}
	return u, rus
}

func (s *DeployerSuite) TestDeployRecallRemoveSubordinates(c *C) {
	// Create a deployer acting on behalf of the principal.
	u, rus := s.prepareSubordinates(c)
	ctx := s.getContext(c)
	machineId, err := u.AssignedMachineId()
	c.Assert(err, IsNil)
	dep := deployer.NewDeployer(s.deployerState, ctx, names.MachineTag(machineId))
	defer stop(c, dep)

	// Add a subordinate, and wait for it to be deployed.
	err = rus[0].EnterScope(nil)
	c.Assert(err, IsNil)
	sub0, err := s.State.Unit("subsvc0/0")
	c.Assert(err, IsNil)
	// Make sure the principal is deployed first, then the subordinate
	s.waitFor(c, isDeployed(ctx, u.Name(), sub0.Name()))

	// And another.
	err = rus[1].EnterScope(nil)
	c.Assert(err, IsNil)
	sub1, err := s.State.Unit("subsvc1/0")
	c.Assert(err, IsNil)
	s.waitFor(c, isDeployed(ctx, u.Name(), sub0.Name(), sub1.Name()))

	// Set one to Dying; check nothing happens.
	err = sub1.Destroy()
	c.Assert(err, IsNil)
	s.State.StartSync()
	c.Assert(isRemoved(s.State, sub1.Name())(c), Equals, false)
	s.waitFor(c, isDeployed(ctx, u.Name(), sub0.Name(), sub1.Name()))

	// Set the other to Dead; check it's recalled and removed.
	err = sub0.EnsureDead()
	c.Assert(err, IsNil)
	s.waitFor(c, isDeployed(ctx, u.Name(), sub1.Name()))
	s.waitFor(c, isRemoved(s.State, sub0.Name()))
}

func (s *DeployerSuite) TestNonAliveSubordinates(c *C) {
	// Add two subordinate units and set them to Dead/Dying respectively.
	u, rus := s.prepareSubordinates(c)
	err := rus[0].EnterScope(nil)
	c.Assert(err, IsNil)
	sub0, err := s.State.Unit("subsvc0/0")
	c.Assert(err, IsNil)
	err = sub0.EnsureDead()
	c.Assert(err, IsNil)
	err = rus[1].EnterScope(nil)
	c.Assert(err, IsNil)
	sub1, err := s.State.Unit("subsvc1/0")
	c.Assert(err, IsNil)
	err = sub1.Destroy()
	c.Assert(err, IsNil)

	// When we start a new deployer, neither unit will be deployed and
	// both will be removed.
	ctx := s.getContext(c)
	machineId, err := u.AssignedMachineId()
	c.Assert(err, IsNil)
	dep := deployer.NewDeployer(s.deployerState, ctx, names.MachineTag(machineId))
	defer stop(c, dep)
	s.waitFor(c, isRemoved(s.State, sub0.Name()))
	s.waitFor(c, isRemoved(s.State, sub1.Name()))
}

func (s *DeployerSuite) waitFor(c *C, t func(c *C) bool) {
	s.BackingState.StartSync()
	if t(c) {
		return
	}
	timeout := time.After(coretesting.LongWait)
	for {
		select {
		case <-timeout:
			c.Fatalf("timeout")
		case <-time.After(coretesting.ShortWait):
			if t(c) {
				return
			}
		}
	}
	panic("unreachable")
}

func isDeployed(ctx deployer.Context, expected ...string) func(*C) bool {
	return func(c *C) bool {
		sort.Strings(expected)
		current, err := ctx.DeployedUnits()
		c.Assert(err, IsNil)
		sort.Strings(current)
		return strings.Join(expected, ":") == strings.Join(current, ":")
	}
}

func isRemoved(st *state.State, name string) func(*C) bool {
	return func(c *C) bool {
		_, err := st.Unit(name)
		if errors.IsNotFoundError(err) {
			return true
		}
		c.Assert(err, IsNil)
		return false
	}
}

type stopper interface {
	Stop() error
}

func stop(c *C, stopper stopper) {
	c.Assert(stopper.Stop(), IsNil)
}