~rogpeppe/juju-core/azure

1206.2.1 by Martin Packman
Add copyright statement at the top of all go files bar thirdparty
1
// Copyright 2012, 2013 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
3
37.3.20 by William Reade
ultra-basic test for juju.NewConn
4
package juju_test
5
6
import (
7
	"io/ioutil"
1408.1.1 by John Arbash Meinel
Start changing the imports of the middle level files.
8
	"os"
9
	"path/filepath"
10
	stdtesting "testing"
11
37.3.20 by William Reade
ultra-basic test for juju.NewConn
12
	. "launchpad.net/gocheck"
1408.1.1 by John Arbash Meinel
Start changing the imports of the middle level files.
13
701.1.1 by William Reade
consolidate juju.Conn files
14
	"launchpad.net/juju-core/charm"
1010.3.9 by Benji York
review changes in-process; tests pass
15
	"launchpad.net/juju-core/constraints"
381.1.11 by Roger Peppe
state: use JujuConnSuite
16
	"launchpad.net/juju-core/environs"
1120.2.9 by Francesco Banconi
Chenges as per review.
17
	"launchpad.net/juju-core/environs/config"
331.6.4 by Dave Cheney
added tests
18
	"launchpad.net/juju-core/environs/dummy"
1262.7.1 by William Reade
rearrange deploy functionality to appropriate layers
19
	"launchpad.net/juju-core/errors"
381.1.11 by Roger Peppe
state: use JujuConnSuite
20
	"launchpad.net/juju-core/juju"
955.3.16 by Brad Crittenden
checkpoint before experimental change to add state to all tests
21
	"launchpad.net/juju-core/juju/testing"
381.1.10 by Roger Peppe
juju: avoid use of StateSuite or JujuConnSuite
22
	"launchpad.net/juju-core/state"
634.2.6 by Roger Peppe
juju: make password test work
23
	coretesting "launchpad.net/juju-core/testing"
1322.1.2 by Roger Peppe
many: use checkers.Satisfies
24
	"launchpad.net/juju-core/testing/checkers"
1151.1.1 by Tim Penhey
Trivial rename of trivial -> utils.
25
	"launchpad.net/juju-core/utils"
1262.7.1 by William Reade
rearrange deploy functionality to appropriate layers
26
	"launchpad.net/juju-core/utils/set"
37.3.20 by William Reade
ultra-basic test for juju.NewConn
27
)
28
187.2.1 by William Reade
add State to juju.Conn
29
func Test(t *stdtesting.T) {
544.2.3 by Roger Peppe
zk -> mgo; fix worker/machiner test to defer watcher stop
30
	coretesting.MgoTestPackage(t)
187.2.1 by William Reade
add State to juju.Conn
31
}
37.3.20 by William Reade
ultra-basic test for juju.NewConn
32
701.1.1 by William Reade
consolidate juju.Conn files
33
type NewConnSuite struct {
331.6.4 by Dave Cheney
added tests
34
	coretesting.LoggingSuite
314.2.4 by Roger Peppe
wip
35
}
37.3.20 by William Reade
ultra-basic test for juju.NewConn
36
701.1.1 by William Reade
consolidate juju.Conn files
37
var _ = Suite(&NewConnSuite{})
319.2.2 by Roger Peppe
go fmt
38
701.1.1 by William Reade
consolidate juju.Conn files
39
func (cs *NewConnSuite) TearDownTest(c *C) {
331.6.4 by Dave Cheney
added tests
40
	dummy.Reset()
41
	cs.LoggingSuite.TearDownTest(c)
42
}
43
701.1.1 by William Reade
consolidate juju.Conn files
44
func (*NewConnSuite) TestNewConnWithoutAdminSecret(c *C) {
664.3.13 by Roger Peppe
juju: add test for NewConn with no admin-secret
45
	attrs := map[string]interface{}{
713.7.29 by Roger Peppe
cmd/juju, juju/testing: use private-key.pem
46
		"name":            "erewhemos",
47
		"type":            "dummy",
48
		"state-server":    true,
49
		"authorized-keys": "i-am-a-key",
50
		"secret":          "pork",
51
		"admin-secret":    "really",
715.3.17 by Roger Peppe
CACertPEM -> CACert
52
		"ca-cert":         coretesting.CACert,
715.3.18 by Roger Peppe
more certPEM->cert, keyPEM->key
53
		"ca-private-key":  coretesting.CAKey,
664.3.13 by Roger Peppe
juju: add test for NewConn with no admin-secret
54
	}
55
	env, err := environs.NewFromAttrs(attrs)
56
	c.Assert(err, IsNil)
1010.3.12 by Benji York
merge from tunk, fixing conflicts
57
	err = environs.Bootstrap(env, constraints.Value{})
664.3.13 by Roger Peppe
juju: add test for NewConn with no admin-secret
58
	c.Assert(err, IsNil)
59
60
	delete(attrs, "admin-secret")
61
	env1, err := environs.NewFromAttrs(attrs)
62
	c.Assert(err, IsNil)
63
	conn, err := juju.NewConn(env1)
64
	c.Check(conn, IsNil)
65
	c.Assert(err, ErrorMatches, "cannot connect without admin-secret")
66
}
67
955.2.2 by Tim Penhey
Start the teasing out of the existing tests.
68
func (*NewConnSuite) TestNewConnFromNameGetUnbootstrapped(c *C) {
955.2.6 by Tim Penhey
Make an even simpler way to make a home dir with and environment.
69
	defer coretesting.MakeSampleHome(c).Restore()
955.2.2 by Tim Penhey
Start the teasing out of the existing tests.
70
	_, err := juju.NewConnFromName("")
71
	c.Assert(err, ErrorMatches, "dummy environment not bootstrapped")
72
}
73
955.2.8 by Tim Penhey
Failing test for JUJU_ENV.
74
func bootstrapEnv(c *C, envName string) {
955.2.4 by Tim Penhey
Multiple close test extracted out.
75
	environ, err := environs.NewFromName(envName)
76
	c.Assert(err, IsNil)
1010.3.12 by Benji York
merge from tunk, fixing conflicts
77
	err = environs.Bootstrap(environ, constraints.Value{})
955.2.3 by Tim Penhey
Use common setup.
78
	c.Assert(err, IsNil)
955.2.4 by Tim Penhey
Multiple close test extracted out.
79
}
80
955.2.8 by Tim Penhey
Failing test for JUJU_ENV.
81
func (*NewConnSuite) TestConnMultipleCloseOk(c *C) {
955.2.6 by Tim Penhey
Make an even simpler way to make a home dir with and environment.
82
	defer coretesting.MakeSampleHome(c).Restore()
955.2.8 by Tim Penhey
Failing test for JUJU_ENV.
83
	bootstrapEnv(c, "")
955.2.5 by Tim Penhey
Move common envorinment config into the testing package.
84
	// Error return from here is tested in TestNewConnFromNameNotSetGetsDefault.
955.2.4 by Tim Penhey
Multiple close test extracted out.
85
	conn, _ := juju.NewConnFromName("")
86
	conn.Close()
87
	conn.Close()
88
	conn.Close()
89
}
90
955.2.8 by Tim Penhey
Failing test for JUJU_ENV.
91
func (*NewConnSuite) TestNewConnFromNameNotSetGetsDefault(c *C) {
955.2.6 by Tim Penhey
Make an even simpler way to make a home dir with and environment.
92
	defer coretesting.MakeSampleHome(c).Restore()
955.2.8 by Tim Penhey
Failing test for JUJU_ENV.
93
	bootstrapEnv(c, "")
455.1.7 by Roger Peppe
juju: rename NewConnWithEnviron->NewConn, NewConn->NewConnWithName
94
	conn, err := juju.NewConnFromName("")
187.2.1 by William Reade
add State to juju.Conn
95
	c.Assert(err, IsNil)
196.1.1 by William Reade
add juju.Conn.Close
96
	defer conn.Close()
955.2.7 by Tim Penhey
Test getting a non-default connection.
97
	c.Assert(conn.Environ.Name(), Equals, coretesting.SampleEnvName)
98
}
99
955.2.8 by Tim Penhey
Failing test for JUJU_ENV.
100
func (*NewConnSuite) TestNewConnFromNameNotDefault(c *C) {
955.2.7 by Tim Penhey
Test getting a non-default connection.
101
	defer coretesting.MakeMultipleEnvHome(c).Restore()
102
	// The default environment is "erewhemos", so make sure we get what we ask for.
955.2.8 by Tim Penhey
Failing test for JUJU_ENV.
103
	const envName = "erewhemos-2"
104
	bootstrapEnv(c, envName)
105
	conn, err := juju.NewConnFromName(envName)
106
	c.Assert(err, IsNil)
107
	defer conn.Close()
108
	c.Assert(conn.Environ.Name(), Equals, envName)
109
}
110
701.1.1 by William Reade
consolidate juju.Conn files
111
func (cs *NewConnSuite) TestConnStateSecretsSideEffect(c *C) {
455.1.1 by Roger Peppe
juju: move State into Conn; remove Bootstrap and Destroy methods
112
	attrs := map[string]interface{}{
381.1.10 by Roger Peppe
juju: avoid use of StateSuite or JujuConnSuite
113
		"name":            "erewhemos",
114
		"type":            "dummy",
544.2.10 by Roger Peppe
gofmt
115
		"state-server":    true,
381.1.10 by Roger Peppe
juju: avoid use of StateSuite or JujuConnSuite
116
		"authorized-keys": "i-am-a-key",
453.1.7 by Dave Cheney
merge with trunk
117
		"secret":          "pork",
664.3.7 by Roger Peppe
gofmt
118
		"admin-secret":    "side-effect secret",
715.3.17 by Roger Peppe
CACertPEM -> CACert
119
		"ca-cert":         coretesting.CACert,
715.3.18 by Roger Peppe
more certPEM->cert, keyPEM->key
120
		"ca-private-key":  coretesting.CAKey,
455.1.1 by Roger Peppe
juju: move State into Conn; remove Bootstrap and Destroy methods
121
	}
122
	env, err := environs.NewFromAttrs(attrs)
381.1.10 by Roger Peppe
juju: avoid use of StateSuite or JujuConnSuite
123
	c.Assert(err, IsNil)
1010.3.12 by Benji York
merge from tunk, fixing conflicts
124
	err = environs.Bootstrap(env, constraints.Value{})
381.1.10 by Roger Peppe
juju: avoid use of StateSuite or JujuConnSuite
125
	c.Assert(err, IsNil)
849.2.3 by Roger Peppe
environs: make StateInfo and StartInstance return and take api.Info
126
	info, _, err := env.StateInfo()
381.1.10 by Roger Peppe
juju: avoid use of StateSuite or JujuConnSuite
127
	c.Assert(err, IsNil)
1151.1.1 by Tim Penhey
Trivial rename of trivial -> utils.
128
	info.Password = utils.PasswordHash("side-effect secret")
1041.2.7 by Dave Cheney
wip
129
	st, err := state.Open(info, state.DefaultDialOpts())
381.1.10 by Roger Peppe
juju: avoid use of StateSuite or JujuConnSuite
130
	c.Assert(err, IsNil)
131
455.1.6 by Roger Peppe
juju: remove NewConnFromAttrs
132
	// Verify we have no secret in the environ config
381.1.10 by Roger Peppe
juju: avoid use of StateSuite or JujuConnSuite
133
	cfg, err := st.EnvironConfig()
134
	c.Assert(err, IsNil)
437.1.3 by Frank Mueller
state: fixes after review
135
	c.Assert(cfg.UnknownAttrs()["secret"], IsNil)
136
664.3.6 by Roger Peppe
juju: fix tests for admin-secret
137
	// Make a new Conn, which will push the secrets.
455.1.7 by Roger Peppe
juju: rename NewConnWithEnviron->NewConn, NewConn->NewConnWithName
138
	conn, err := juju.NewConn(env)
331.6.4 by Dave Cheney
added tests
139
	c.Assert(err, IsNil)
140
	defer conn.Close()
664.3.6 by Roger Peppe
juju: fix tests for admin-secret
141
455.1.1 by Roger Peppe
juju: move State into Conn; remove Bootstrap and Destroy methods
142
	cfg, err = conn.State.EnvironConfig()
143
	c.Assert(err, IsNil)
453.1.7 by Dave Cheney
merge with trunk
144
	c.Assert(cfg.UnknownAttrs()["secret"], Equals, "pork")
664.3.6 by Roger Peppe
juju: fix tests for admin-secret
145
146
	// Reset the admin password so the state db can be reused.
829.1.1 by Roger Peppe
src: rename SetPassword to SetMongoPassword
147
	err = conn.State.SetAdminMongoPassword("")
664.3.6 by Roger Peppe
juju: fix tests for admin-secret
148
	c.Assert(err, IsNil)
331.6.4 by Dave Cheney
added tests
149
}
150
701.1.1 by William Reade
consolidate juju.Conn files
151
func (cs *NewConnSuite) TestConnStateDoesNotUpdateExistingSecrets(c *C) {
664.3.6 by Roger Peppe
juju: fix tests for admin-secret
152
	attrs := map[string]interface{}{
453.1.2 by Dave Cheney
final tweaks
153
		"name":            "erewhemos",
154
		"type":            "dummy",
544.2.10 by Roger Peppe
gofmt
155
		"state-server":    true,
453.1.2 by Dave Cheney
final tweaks
156
		"authorized-keys": "i-am-a-key",
664.3.6 by Roger Peppe
juju: fix tests for admin-secret
157
		"secret":          "pork",
664.3.7 by Roger Peppe
gofmt
158
		"admin-secret":    "some secret",
715.3.17 by Roger Peppe
CACertPEM -> CACert
159
		"ca-cert":         coretesting.CACert,
715.3.18 by Roger Peppe
more certPEM->cert, keyPEM->key
160
		"ca-private-key":  coretesting.CAKey,
664.3.6 by Roger Peppe
juju: fix tests for admin-secret
161
	}
162
	env, err := environs.NewFromAttrs(attrs)
163
	c.Assert(err, IsNil)
1010.3.12 by Benji York
merge from tunk, fixing conflicts
164
	err = environs.Bootstrap(env, constraints.Value{})
664.3.6 by Roger Peppe
juju: fix tests for admin-secret
165
	c.Assert(err, IsNil)
166
167
	// Make a new Conn, which will push the secrets.
453.1.7 by Dave Cheney
merge with trunk
168
	conn, err := juju.NewConn(env)
169
	c.Assert(err, IsNil)
453.1.2 by Dave Cheney
final tweaks
170
	defer conn.Close()
664.3.6 by Roger Peppe
juju: fix tests for admin-secret
171
172
	// Make another env with a different secret.
173
	attrs["secret"] = "squirrel"
174
	env1, err := environs.NewFromAttrs(attrs)
175
	c.Assert(err, IsNil)
664.3.7 by Roger Peppe
gofmt
176
664.3.6 by Roger Peppe
juju: fix tests for admin-secret
177
	// Connect with the new env and check that the secret has not changed
178
	conn, err = juju.NewConn(env1)
179
	c.Assert(err, IsNil)
180
	defer conn.Close()
453.1.7 by Dave Cheney
merge with trunk
181
	cfg, err := conn.State.EnvironConfig()
453.1.2 by Dave Cheney
final tweaks
182
	c.Assert(err, IsNil)
183
	c.Assert(cfg.UnknownAttrs()["secret"], Equals, "pork")
664.3.6 by Roger Peppe
juju: fix tests for admin-secret
184
185
	// Reset the admin password so the state db can be reused.
829.1.1 by Roger Peppe
src: rename SetPassword to SetMongoPassword
186
	err = conn.State.SetAdminMongoPassword("")
664.3.6 by Roger Peppe
juju: fix tests for admin-secret
187
	c.Assert(err, IsNil)
453.1.2 by Dave Cheney
final tweaks
188
}
634.2.4 by Roger Peppe
juju: sets for conn with password
189
701.1.1 by William Reade
consolidate juju.Conn files
190
func (cs *NewConnSuite) TestConnWithPassword(c *C) {
634.2.4 by Roger Peppe
juju: sets for conn with password
191
	env, err := environs.NewFromAttrs(map[string]interface{}{
192
		"name":            "erewhemos",
193
		"type":            "dummy",
194
		"state-server":    true,
195
		"authorized-keys": "i-am-a-key",
196
		"secret":          "squirrel",
634.2.6 by Roger Peppe
juju: make password test work
197
		"admin-secret":    "nutkin",
715.3.17 by Roger Peppe
CACertPEM -> CACert
198
		"ca-cert":         coretesting.CACert,
715.3.18 by Roger Peppe
more certPEM->cert, keyPEM->key
199
		"ca-private-key":  coretesting.CAKey,
634.2.4 by Roger Peppe
juju: sets for conn with password
200
	})
201
	c.Assert(err, IsNil)
1010.3.12 by Benji York
merge from tunk, fixing conflicts
202
	err = environs.Bootstrap(env, constraints.Value{})
634.2.4 by Roger Peppe
juju: sets for conn with password
203
	c.Assert(err, IsNil)
634.2.6 by Roger Peppe
juju: make password test work
204
205
	// Check that Bootstrap has correctly used a hash
206
	// of the admin password.
849.2.3 by Roger Peppe
environs: make StateInfo and StartInstance return and take api.Info
207
	info, _, err := env.StateInfo()
634.2.4 by Roger Peppe
juju: sets for conn with password
208
	c.Assert(err, IsNil)
1151.1.1 by Tim Penhey
Trivial rename of trivial -> utils.
209
	info.Password = utils.PasswordHash("nutkin")
1041.2.7 by Dave Cheney
wip
210
	st, err := state.Open(info, state.DefaultDialOpts())
634.2.4 by Roger Peppe
juju: sets for conn with password
211
	c.Assert(err, IsNil)
634.2.6 by Roger Peppe
juju: make password test work
212
	st.Close()
634.2.4 by Roger Peppe
juju: sets for conn with password
213
214
	// Check that we can connect with the original environment.
215
	conn, err := juju.NewConn(env)
216
	c.Assert(err, IsNil)
217
	conn.Close()
218
634.2.6 by Roger Peppe
juju: make password test work
219
	// Check that the password has now been changed to the original
220
	// admin password.
634.2.4 by Roger Peppe
juju: sets for conn with password
221
	info.Password = "nutkin"
1041.2.7 by Dave Cheney
wip
222
	st1, err := state.Open(info, state.DefaultDialOpts())
634.2.4 by Roger Peppe
juju: sets for conn with password
223
	c.Assert(err, IsNil)
224
	st1.Close()
225
634.2.6 by Roger Peppe
juju: make password test work
226
	// Check that we can still connect with the original
227
	// environment.
634.2.4 by Roger Peppe
juju: sets for conn with password
228
	conn, err = juju.NewConn(env)
229
	c.Assert(err, IsNil)
634.2.6 by Roger Peppe
juju: make password test work
230
	defer conn.Close()
231
664.3.6 by Roger Peppe
juju: fix tests for admin-secret
232
	// Reset the admin password so the state db can be reused.
829.1.1 by Roger Peppe
src: rename SetPassword to SetMongoPassword
233
	err = conn.State.SetAdminMongoPassword("")
634.2.6 by Roger Peppe
juju: make password test work
234
	c.Assert(err, IsNil)
235
}
701.1.1 by William Reade
consolidate juju.Conn files
236
237
type ConnSuite struct {
238
	coretesting.LoggingSuite
239
	coretesting.MgoSuite
240
	conn *juju.Conn
241
	repo *charm.LocalRepository
242
}
243
244
var _ = Suite(&ConnSuite{})
245
246
func (s *ConnSuite) SetUpTest(c *C) {
247
	s.LoggingSuite.SetUpTest(c)
248
	s.MgoSuite.SetUpTest(c)
249
	attrs := map[string]interface{}{
250
		"name":            "erewhemos",
251
		"type":            "dummy",
252
		"state-server":    true,
253
		"authorized-keys": "i-am-a-key",
254
		"admin-secret":    "deploy-test-secret",
715.3.17 by Roger Peppe
CACertPEM -> CACert
255
		"ca-cert":         coretesting.CACert,
715.3.18 by Roger Peppe
more certPEM->cert, keyPEM->key
256
		"ca-private-key":  coretesting.CAKey,
701.1.1 by William Reade
consolidate juju.Conn files
257
	}
258
	environ, err := environs.NewFromAttrs(attrs)
259
	c.Assert(err, IsNil)
1010.3.12 by Benji York
merge from tunk, fixing conflicts
260
	err = environs.Bootstrap(environ, constraints.Value{})
701.1.1 by William Reade
consolidate juju.Conn files
261
	c.Assert(err, IsNil)
262
	s.conn, err = juju.NewConn(environ)
263
	c.Assert(err, IsNil)
264
	s.repo = &charm.LocalRepository{Path: c.MkDir()}
265
}
266
267
func (s *ConnSuite) TearDownTest(c *C) {
268
	if s.conn == nil {
269
		return
270
	}
829.1.1 by Roger Peppe
src: rename SetPassword to SetMongoPassword
271
	err := s.conn.State.SetAdminMongoPassword("")
701.1.1 by William Reade
consolidate juju.Conn files
272
	c.Assert(err, IsNil)
273
	err = s.conn.Environ.Destroy(nil)
274
	c.Check(err, IsNil)
275
	s.conn.Close()
276
	s.conn = nil
955.3.16 by Brad Crittenden
checkpoint before experimental change to add state to all tests
277
	dummy.Reset()
701.1.1 by William Reade
consolidate juju.Conn files
278
	s.MgoSuite.TearDownTest(c)
279
	s.LoggingSuite.TearDownTest(c)
280
}
281
282
func (s *ConnSuite) SetUpSuite(c *C) {
283
	s.LoggingSuite.SetUpSuite(c)
284
	s.MgoSuite.SetUpSuite(c)
285
}
286
287
func (s *ConnSuite) TearDownSuite(c *C) {
288
	s.LoggingSuite.TearDownSuite(c)
289
	s.MgoSuite.TearDownSuite(c)
290
}
291
933.5.1 by Roger Peppe
juju: add NewConnFromState
292
func (s *ConnSuite) TestNewConnFromState(c *C) {
293
	conn, err := juju.NewConnFromState(s.conn.State)
294
	c.Assert(err, IsNil)
295
	c.Assert(conn.Environ.Name(), Equals, "erewhemos")
296
}
933.5.2 by Roger Peppe
gofmt
297
701.1.1 by William Reade
consolidate juju.Conn files
298
func (s *ConnSuite) TestPutCharmBasic(c *C) {
299
	curl := coretesting.Charms.ClonedURL(s.repo.Path, "series", "riak")
300
	curl.Revision = -1 // make sure we trigger the repo.Latest logic.
301
	sch, err := s.conn.PutCharm(curl, s.repo, false)
302
	c.Assert(err, IsNil)
303
	c.Assert(sch.Meta().Summary, Equals, "K/V storage engine")
304
305
	sch, err = s.conn.State.Charm(sch.URL())
306
	c.Assert(err, IsNil)
307
	c.Assert(sch.Meta().Summary, Equals, "K/V storage engine")
308
}
309
310
func (s *ConnSuite) TestPutBundledCharm(c *C) {
311
	// Bundle the riak charm into a charm repo directory.
312
	dir := filepath.Join(s.repo.Path, "series")
313
	err := os.Mkdir(dir, 0777)
314
	c.Assert(err, IsNil)
315
	w, err := os.Create(filepath.Join(dir, "riak.charm"))
316
	c.Assert(err, IsNil)
317
	defer w.Close()
875.1.2 by John Arbash Meinel
Change all of the Charms apis to not take a 'series' parameter.
318
	charmDir := coretesting.Charms.Dir("riak")
701.1.1 by William Reade
consolidate juju.Conn files
319
	err = charmDir.BundleTo(w)
320
	c.Assert(err, IsNil)
321
322
	// Invent a URL that points to the bundled charm, and
323
	// test putting that.
324
	curl := &charm.URL{
325
		Schema:   "local",
326
		Series:   "series",
327
		Name:     "riak",
328
		Revision: -1,
329
	}
1026.1.6 by Dimiter Naydenov
Changes after peering
330
	_, err = s.conn.PutCharm(curl, s.repo, true)
1026.1.7 by Dimiter Naydenov
Changes after peering
331
	c.Assert(err, ErrorMatches, `cannot increment revision of charm "local:series/riak-7": not a directory`)
1026.1.6 by Dimiter Naydenov
Changes after peering
332
701.1.1 by William Reade
consolidate juju.Conn files
333
	sch, err := s.conn.PutCharm(curl, s.repo, false)
334
	c.Assert(err, IsNil)
335
	c.Assert(sch.Meta().Summary, Equals, "K/V storage engine")
336
337
	// Check that we can get the charm from the state.
338
	sch, err = s.conn.State.Charm(sch.URL())
339
	c.Assert(err, IsNil)
340
	c.Assert(sch.Meta().Summary, Equals, "K/V storage engine")
341
}
342
343
func (s *ConnSuite) TestPutCharmUpload(c *C) {
344
	repo := &charm.LocalRepository{c.MkDir()}
345
	curl := coretesting.Charms.ClonedURL(repo.Path, "series", "riak")
346
347
	// Put charm for the first time.
348
	sch, err := s.conn.PutCharm(curl, repo, false)
349
	c.Assert(err, IsNil)
350
	c.Assert(sch.Meta().Summary, Equals, "K/V storage engine")
351
352
	sch, err = s.conn.State.Charm(sch.URL())
353
	c.Assert(err, IsNil)
354
	sha256 := sch.BundleSha256()
355
	rev := sch.Revision()
356
357
	// Change the charm on disk.
358
	ch, err := repo.Get(curl)
359
	c.Assert(err, IsNil)
360
	chd := ch.(*charm.Dir)
361
	err = ioutil.WriteFile(filepath.Join(chd.Path, "extra"), []byte("arble"), 0666)
362
	c.Assert(err, IsNil)
363
364
	// Put charm again and check that it has not changed in the state.
365
	sch, err = s.conn.PutCharm(curl, repo, false)
366
	c.Assert(err, IsNil)
367
368
	sch, err = s.conn.State.Charm(sch.URL())
369
	c.Assert(err, IsNil)
370
	c.Assert(sch.BundleSha256(), Equals, sha256)
371
	c.Assert(sch.Revision(), Equals, rev)
372
373
	// Put charm again, with bumpRevision this time, and check that
374
	// it has changed.
375
	sch, err = s.conn.PutCharm(curl, repo, true)
376
	c.Assert(err, IsNil)
377
378
	sch, err = s.conn.State.Charm(sch.URL())
379
	c.Assert(err, IsNil)
380
	c.Assert(sch.BundleSha256(), Not(Equals), sha256)
381
	c.Assert(sch.Revision(), Equals, rev+1)
382
}
383
384
func (s *ConnSuite) TestAddUnits(c *C) {
385
	curl := coretesting.Charms.ClonedURL(s.repo.Path, "series", "riak")
386
	sch, err := s.conn.PutCharm(curl, s.repo, false)
387
	c.Assert(err, IsNil)
858.4.2 by William Reade
fix remaining tests
388
	svc, err := s.conn.State.AddService("testriak", sch)
701.1.1 by William Reade
consolidate juju.Conn files
389
	c.Assert(err, IsNil)
1117.6.1 by Kapil Thangavelu
deploy-to funcionality
390
	units, err := s.conn.AddUnits(svc, 2, "")
701.1.1 by William Reade
consolidate juju.Conn files
391
	c.Assert(err, IsNil)
392
	c.Assert(units, HasLen, 2)
393
394
	id0, err := units[0].AssignedMachineId()
395
	c.Assert(err, IsNil)
396
	id1, err := units[1].AssignedMachineId()
397
	c.Assert(err, IsNil)
398
	c.Assert(id0, Not(Equals), id1)
1117.6.1 by Kapil Thangavelu
deploy-to funcionality
399
400
	units, err = s.conn.AddUnits(svc, 2, "0")
1117.6.8 by Kapil Thangavelu
address review comments
401
	c.Assert(err, ErrorMatches, `cannot add multiple units of service "testriak" to a single machine`)
1117.6.1 by Kapil Thangavelu
deploy-to funcionality
402
403
	units, err = s.conn.AddUnits(svc, 1, "0")
404
	c.Assert(err, IsNil)
405
	id2, err := units[0].AssignedMachineId()
406
	c.Assert(id2, Equals, id0)
407
701.1.1 by William Reade
consolidate juju.Conn files
408
}
409
1262.7.1 by William Reade
rearrange deploy functionality to appropriate layers
410
// DeployLocalSuite uses a fresh copy of the same local dummy charm for each
1262.7.4 by William Reade
address review
411
// test, because DeployService demands that a charm already exists in state,
1262.7.1 by William Reade
rearrange deploy functionality to appropriate layers
412
// and that's is the simplest way to get one in there.
955.3.16 by Brad Crittenden
checkpoint before experimental change to add state to all tests
413
type DeployLocalSuite struct {
414
	testing.JujuConnSuite
1262.7.1 by William Reade
rearrange deploy functionality to appropriate layers
415
	repo        charm.Repository
416
	charm       *state.Charm
417
	oldCacheDir string
955.3.16 by Brad Crittenden
checkpoint before experimental change to add state to all tests
418
}
419
420
var _ = Suite(&DeployLocalSuite{})
421
1262.7.1 by William Reade
rearrange deploy functionality to appropriate layers
422
func (s *DeployLocalSuite) SetUpSuite(c *C) {
423
	s.JujuConnSuite.SetUpSuite(c)
424
	s.repo = &charm.LocalRepository{Path: coretesting.Charms.Path}
425
	s.oldCacheDir, charm.CacheDir = charm.CacheDir, c.MkDir()
426
}
427
428
func (s *DeployLocalSuite) TearDownSuite(c *C) {
429
	charm.CacheDir = s.oldCacheDir
430
	s.JujuConnSuite.TearDownSuite(c)
431
}
432
955.3.16 by Brad Crittenden
checkpoint before experimental change to add state to all tests
433
func (s *DeployLocalSuite) SetUpTest(c *C) {
434
	s.JujuConnSuite.SetUpTest(c)
1262.7.1 by William Reade
rearrange deploy functionality to appropriate layers
435
	curl := charm.MustParseURL("local:series/dummy")
436
	charm, err := s.Conn.PutCharm(curl, s.repo, false)
437
	c.Assert(err, IsNil)
438
	s.charm = charm
439
}
440
441
func (s *DeployLocalSuite) TestDeployMinimal(c *C) {
442
	service, err := s.Conn.DeployService(juju.DeployServiceParams{
443
		ServiceName: "bob",
444
		Charm:       s.charm,
445
	})
446
	c.Assert(err, IsNil)
447
	s.assertCharm(c, service, s.charm.URL())
448
	s.assertSettings(c, service, charm.Settings{})
449
	s.assertConstraints(c, service, constraints.Value{})
450
	s.assertMachines(c, service, constraints.Value{})
451
}
452
453
func (s *DeployLocalSuite) TestDeploySettings(c *C) {
454
	service, err := s.Conn.DeployService(juju.DeployServiceParams{
455
		ServiceName: "bob",
456
		Charm:       s.charm,
457
		ConfigSettings: charm.Settings{
458
			"title":       "banana cupcakes",
459
			"skill-level": 9901,
460
		},
461
	})
462
	c.Assert(err, IsNil)
463
	s.assertSettings(c, service, charm.Settings{
464
		"title":       "banana cupcakes",
465
		"skill-level": int64(9901),
466
	})
467
}
468
469
func (s *DeployLocalSuite) TestDeploySettingsError(c *C) {
470
	_, err := s.Conn.DeployService(juju.DeployServiceParams{
471
		ServiceName: "bob",
472
		Charm:       s.charm,
473
		ConfigSettings: charm.Settings{
474
			"skill-level": 99.01,
475
		},
476
	})
477
	c.Assert(err, ErrorMatches, `option "skill-level" expected int, got 99.01`)
478
	_, err = s.State.Service("bob")
1322.1.2 by Roger Peppe
many: use checkers.Satisfies
479
	c.Assert(err, checkers.Satisfies, errors.IsNotFoundError)
1262.7.1 by William Reade
rearrange deploy functionality to appropriate layers
480
}
481
482
func (s *DeployLocalSuite) TestDeployConstraints(c *C) {
483
	err := s.State.SetEnvironConstraints(constraints.MustParse("mem=2G"))
484
	c.Assert(err, IsNil)
485
	serviceCons := constraints.MustParse("cpu-cores=2")
486
	service, err := s.Conn.DeployService(juju.DeployServiceParams{
487
		ServiceName: "bob",
488
		Charm:       s.charm,
489
		Constraints: serviceCons,
490
	})
491
	c.Assert(err, IsNil)
492
	s.assertConstraints(c, service, serviceCons)
493
}
494
495
func (s *DeployLocalSuite) TestDeployNumUnits(c *C) {
496
	err := s.State.SetEnvironConstraints(constraints.MustParse("mem=2G"))
497
	c.Assert(err, IsNil)
498
	serviceCons := constraints.MustParse("cpu-cores=2")
499
	service, err := s.Conn.DeployService(juju.DeployServiceParams{
500
		ServiceName: "bob",
501
		Charm:       s.charm,
502
		Constraints: serviceCons,
503
		NumUnits:    2,
504
	})
505
	c.Assert(err, IsNil)
506
	s.assertConstraints(c, service, serviceCons)
507
	s.assertMachines(c, service, constraints.MustParse("mem=2G cpu-cores=2"), "0", "1")
508
}
509
510
func (s *DeployLocalSuite) TestDeployForceMachineId(c *C) {
511
	machine, err := s.State.AddMachine("series", state.JobHostUnits)
512
	c.Assert(err, IsNil)
513
	c.Assert(machine.Id(), Equals, "0")
514
	err = s.State.SetEnvironConstraints(constraints.MustParse("mem=2G"))
515
	c.Assert(err, IsNil)
516
	serviceCons := constraints.MustParse("cpu-cores=2")
517
	service, err := s.Conn.DeployService(juju.DeployServiceParams{
518
		ServiceName:    "bob",
519
		Charm:          s.charm,
520
		Constraints:    serviceCons,
521
		NumUnits:       1,
522
		ForceMachineId: "0",
523
	})
524
	c.Assert(err, IsNil)
525
	s.assertConstraints(c, service, serviceCons)
526
	s.assertMachines(c, service, constraints.Value{}, "0")
527
}
528
529
func (s *DeployLocalSuite) assertCharm(c *C, service *state.Service, expect *charm.URL) {
530
	curl, force := service.CharmURL()
531
	c.Assert(curl, DeepEquals, expect)
532
	c.Assert(force, Equals, false)
533
}
534
535
func (s *DeployLocalSuite) assertSettings(c *C, service *state.Service, expect charm.Settings) {
536
	settings, err := service.ConfigSettings()
537
	c.Assert(err, IsNil)
538
	c.Assert(settings, DeepEquals, expect)
539
}
540
541
func (s *DeployLocalSuite) assertConstraints(c *C, service *state.Service, expect constraints.Value) {
542
	cons, err := service.Constraints()
543
	c.Assert(err, IsNil)
544
	c.Assert(cons, DeepEquals, expect)
545
}
546
547
func (s *DeployLocalSuite) assertMachines(c *C, service *state.Service, expectCons constraints.Value, expectIds ...string) {
548
	units, err := service.AllUnits()
549
	c.Assert(err, IsNil)
550
	c.Assert(units, HasLen, len(expectIds))
551
	unseenIds := set.NewStrings(expectIds...)
1061.8.1 by William Reade
add constraints handling to deploy throughout
552
	for _, unit := range units {
1262.7.1 by William Reade
rearrange deploy functionality to appropriate layers
553
		id, err := unit.AssignedMachineId()
554
		c.Assert(err, IsNil)
555
		unseenIds.Remove(id)
556
		machine, err := s.State.Machine(id)
557
		c.Assert(err, IsNil)
558
		cons, err := machine.Constraints()
559
		c.Assert(err, IsNil)
560
		c.Assert(cons, DeepEquals, expectCons)
1061.8.1 by William Reade
add constraints handling to deploy throughout
561
	}
1262.7.1 by William Reade
rearrange deploy functionality to appropriate layers
562
	c.Assert(unseenIds, DeepEquals, set.NewStrings())
955.3.16 by Brad Crittenden
checkpoint before experimental change to add state to all tests
563
}
1120.2.9 by Francesco Banconi
Chenges as per review.
564
565
type InitJujuHomeSuite struct {
566
	originalHome     string
567
	originalJujuHome string
568
}
569
570
var _ = Suite(&InitJujuHomeSuite{})
571
572
func (s *InitJujuHomeSuite) SetUpTest(c *C) {
573
	s.originalHome = os.Getenv("HOME")
574
	s.originalJujuHome = os.Getenv("JUJU_HOME")
575
}
576
577
func (s *InitJujuHomeSuite) TearDownTest(c *C) {
578
	os.Setenv("HOME", s.originalHome)
579
	os.Setenv("JUJU_HOME", s.originalJujuHome)
580
}
581
582
func (s *InitJujuHomeSuite) TestJujuHome(c *C) {
583
	os.Setenv("JUJU_HOME", "/my/juju/home")
584
	err := juju.InitJujuHome()
585
	c.Assert(err, IsNil)
586
	c.Assert(config.JujuHome(), Equals, "/my/juju/home")
587
}
588
589
func (s *InitJujuHomeSuite) TestHome(c *C) {
590
	os.Setenv("JUJU_HOME", "")
591
	os.Setenv("HOME", "/my/home/")
592
	err := juju.InitJujuHome()
593
	c.Assert(err, IsNil)
594
	c.Assert(config.JujuHome(), Equals, "/my/home/.juju")
595
}
596
597
func (s *InitJujuHomeSuite) TestError(c *C) {
598
	os.Setenv("JUJU_HOME", "")
599
	os.Setenv("HOME", "")
600
	err := juju.InitJujuHome()
601
	c.Assert(err, ErrorMatches, "cannot determine juju home.*")
602
}
603
604
func (s *InitJujuHomeSuite) TestCacheDir(c *C) {
605
	os.Setenv("JUJU_HOME", "/foo/bar")
606
	c.Assert(charm.CacheDir, Equals, "")
607
	err := juju.InitJujuHome()
608
	c.Assert(err, IsNil)
609
	c.Assert(charm.CacheDir, Equals, "/foo/bar/charmcache")
610
}