~juju-qa/ubuntu/xenial/juju/2.0-rc2

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cmd/juju/application/upgradecharm_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-09-30 14:39:30 UTC
  • mfrom: (1.8.1)
  • Revision ID: nicholas.skaggs@canonical.com-20160930143930-vwwhrefh6ftckccy
import upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
        "path"
11
11
        "path/filepath"
12
12
 
 
13
        "github.com/juju/cmd"
13
14
        "github.com/juju/errors"
 
15
        "github.com/juju/testing"
14
16
        jc "github.com/juju/testing/checkers"
15
17
        "github.com/juju/utils"
 
18
        "github.com/juju/version"
16
19
        gc "gopkg.in/check.v1"
17
20
        "gopkg.in/juju/charm.v6-unstable"
 
21
        charmresource "gopkg.in/juju/charm.v6-unstable/resource"
18
22
        "gopkg.in/juju/charmrepo.v2-unstable"
19
23
        "gopkg.in/juju/charmrepo.v2-unstable/csclient"
20
24
        csclientparams "gopkg.in/juju/charmrepo.v2-unstable/csclient/params"
21
 
        "gopkg.in/juju/charmstore.v5-unstable"
 
25
        charmstore "gopkg.in/juju/charmstore.v5-unstable"
22
26
        "gopkg.in/macaroon-bakery.v1/httpbakery"
 
27
        macaroon "gopkg.in/macaroon.v1"
23
28
 
 
29
        "github.com/juju/juju/api"
 
30
        "github.com/juju/juju/api/application"
 
31
        "github.com/juju/juju/api/base"
 
32
        "github.com/juju/juju/api/charms"
 
33
        jujucharmstore "github.com/juju/juju/charmstore"
24
34
        "github.com/juju/juju/cmd/modelcmd"
 
35
        "github.com/juju/juju/environs/config"
25
36
        jujutesting "github.com/juju/juju/juju/testing"
 
37
        "github.com/juju/juju/jujuclient"
 
38
        "github.com/juju/juju/jujuclient/jujuclienttesting"
 
39
        "github.com/juju/juju/resource/resourceadapters"
26
40
        "github.com/juju/juju/rpc"
27
41
        "github.com/juju/juju/state"
 
42
        "github.com/juju/juju/storage"
28
43
        "github.com/juju/juju/testcharms"
29
 
        "github.com/juju/juju/testing"
 
44
        coretesting "github.com/juju/juju/testing"
30
45
)
31
46
 
32
 
type UpgradeCharmErrorsSuite struct {
 
47
type UpgradeCharmSuite struct {
 
48
        testing.IsolationSuite
 
49
        testing.Stub
 
50
 
 
51
        deployResources    resourceadapters.DeployResourcesFunc
 
52
        resolveCharm       ResolveCharmFunc
 
53
        resolvedCharmURL   *charm.URL
 
54
        apiConnection      mockAPIConnection
 
55
        charmAdder         mockCharmAdder
 
56
        charmClient        mockCharmClient
 
57
        charmUpgradeClient mockCharmUpgradeClient
 
58
        modelConfigGetter  mockModelConfigGetter
 
59
        resourceLister     mockResourceLister
 
60
        cmd                cmd.Command
 
61
}
 
62
 
 
63
var _ = gc.Suite(&UpgradeCharmSuite{})
 
64
 
 
65
func (s *UpgradeCharmSuite) SetUpTest(c *gc.C) {
 
66
        s.IsolationSuite.SetUpTest(c)
 
67
        s.Stub.ResetCalls()
 
68
 
 
69
        // Create persistent cookies in a temporary location.
 
70
        cookieFile := filepath.Join(c.MkDir(), "cookies")
 
71
        s.PatchEnvironment("JUJU_COOKIEFILE", cookieFile)
 
72
 
 
73
        s.deployResources = func(
 
74
                applicationID string,
 
75
                chID jujucharmstore.CharmID,
 
76
                csMac *macaroon.Macaroon,
 
77
                filesAndRevisions map[string]string,
 
78
                resources map[string]charmresource.Meta,
 
79
                conn base.APICallCloser,
 
80
        ) (ids map[string]string, err error) {
 
81
                s.AddCall("DeployResources", applicationID, chID, csMac, filesAndRevisions, resources, conn)
 
82
                return nil, s.NextErr()
 
83
        }
 
84
 
 
85
        s.resolveCharm = func(
 
86
                resolveWithChannel func(*charm.URL) (*charm.URL, csclientparams.Channel, []string, error),
 
87
                conf *config.Config,
 
88
                url *charm.URL,
 
89
        ) (*charm.URL, csclientparams.Channel, []string, error) {
 
90
                s.AddCall("ResolveCharm", resolveWithChannel, conf, url)
 
91
                if err := s.NextErr(); err != nil {
 
92
                        return nil, csclientparams.NoChannel, nil, err
 
93
                }
 
94
                return s.resolvedCharmURL, csclientparams.StableChannel, []string{"quantal"}, nil
 
95
        }
 
96
 
 
97
        currentCharmURL := charm.MustParseURL("cs:quantal/foo-1")
 
98
        latestCharmURL := charm.MustParseURL("cs:quantal/foo-2")
 
99
        s.resolvedCharmURL = latestCharmURL
 
100
 
 
101
        s.apiConnection = mockAPIConnection{
 
102
                bestFacadeVersion: 2,
 
103
                serverVersion: &version.Number{
 
104
                        Major: 1,
 
105
                        Minor: 2,
 
106
                        Patch: 3,
 
107
                },
 
108
        }
 
109
        s.charmAdder = mockCharmAdder{}
 
110
        s.charmClient = mockCharmClient{
 
111
                charmInfo: &charms.CharmInfo{
 
112
                        Meta: &charm.Meta{},
 
113
                },
 
114
        }
 
115
        s.charmUpgradeClient = mockCharmUpgradeClient{charmURL: currentCharmURL}
 
116
        s.modelConfigGetter = mockModelConfigGetter{}
 
117
        s.resourceLister = mockResourceLister{}
 
118
 
 
119
        store := jujuclienttesting.NewMemStore()
 
120
        store.CurrentControllerName = "foo"
 
121
        store.Controllers["foo"] = jujuclient.ControllerDetails{}
 
122
        store.Models["foo"] = &jujuclient.ControllerModels{
 
123
                CurrentModel: "admin@local/bar",
 
124
                Models:       map[string]jujuclient.ModelDetails{"admin@local/bar": {}},
 
125
        }
 
126
        apiOpener := modelcmd.OpenFunc(func(store jujuclient.ClientStore, controller, model string) (api.Connection, error) {
 
127
                s.AddCall("OpenAPI", store, controller, model)
 
128
                return &s.apiConnection, nil
 
129
        })
 
130
 
 
131
        s.cmd = NewUpgradeCharmCommandForTest(
 
132
                store,
 
133
                apiOpener,
 
134
                s.deployResources,
 
135
                s.resolveCharm,
 
136
                func(conn api.Connection, bakeryClient *httpbakery.Client, channel csclientparams.Channel) CharmAdder {
 
137
                        s.AddCall("NewCharmAdder", conn, bakeryClient, channel)
 
138
                        s.PopNoErr()
 
139
                        return &s.charmAdder
 
140
                },
 
141
                func(conn api.Connection) CharmClient {
 
142
                        s.AddCall("NewCharmClient", conn)
 
143
                        s.PopNoErr()
 
144
                        return &s.charmClient
 
145
                },
 
146
                func(conn api.Connection) CharmUpgradeClient {
 
147
                        s.AddCall("NewCharmUpgradeClient", conn)
 
148
                        s.PopNoErr()
 
149
                        return &s.charmUpgradeClient
 
150
                },
 
151
                func(conn api.Connection) ModelConfigGetter {
 
152
                        s.AddCall("NewModelConfigGetter", conn)
 
153
                        return &s.modelConfigGetter
 
154
                },
 
155
                func(conn api.Connection) (ResourceLister, error) {
 
156
                        s.AddCall("NewResourceLister", conn)
 
157
                        return &s.resourceLister, s.NextErr()
 
158
                },
 
159
        )
 
160
}
 
161
 
 
162
func (s *UpgradeCharmSuite) runUpgradeCharm(c *gc.C, args ...string) (*cmd.Context, error) {
 
163
        return coretesting.RunCommand(c, s.cmd, args...)
 
164
}
 
165
 
 
166
func (s *UpgradeCharmSuite) TestStorageConstraints(c *gc.C) {
 
167
        _, err := s.runUpgradeCharm(c, "foo", "--storage", "bar=baz")
 
168
        c.Assert(err, jc.ErrorIsNil)
 
169
        s.charmUpgradeClient.CheckCallNames(c, "GetCharmURL", "SetCharm")
 
170
        s.charmUpgradeClient.CheckCall(c, 1, "SetCharm", application.SetCharmConfig{
 
171
                ApplicationName: "foo",
 
172
                CharmID: jujucharmstore.CharmID{
 
173
                        URL:     s.resolvedCharmURL,
 
174
                        Channel: csclientparams.StableChannel,
 
175
                },
 
176
                StorageConstraints: map[string]storage.Constraints{
 
177
                        "bar": {Pool: "baz", Count: 1},
 
178
                },
 
179
        })
 
180
}
 
181
 
 
182
func (s *UpgradeCharmSuite) TestStorageConstraintsMinFacadeVersion(c *gc.C) {
 
183
        s.apiConnection.bestFacadeVersion = 1
 
184
        _, err := s.runUpgradeCharm(c, "foo", "--storage", "bar=baz")
 
185
        c.Assert(err, gc.ErrorMatches,
 
186
                "updating storage constraints at upgrade-charm time is not supported by server version 1.2.3")
 
187
}
 
188
 
 
189
func (s *UpgradeCharmSuite) TestStorageConstraintsMinFacadeVersionNoServerVersion(c *gc.C) {
 
190
        s.apiConnection.bestFacadeVersion = 1
 
191
        s.apiConnection.serverVersion = nil
 
192
        _, err := s.runUpgradeCharm(c, "foo", "--storage", "bar=baz")
 
193
        c.Assert(err, gc.ErrorMatches,
 
194
                "updating storage constraints at upgrade-charm time is not supported by this server")
 
195
}
 
196
 
 
197
func (s *UpgradeCharmSuite) TestConfigSettings(c *gc.C) {
 
198
        tempdir := c.MkDir()
 
199
        configFile := filepath.Join(tempdir, "config.yaml")
 
200
        err := ioutil.WriteFile(configFile, []byte("foo:{}"), 0644)
 
201
        c.Assert(err, jc.ErrorIsNil)
 
202
 
 
203
        _, err = s.runUpgradeCharm(c, "foo", "--config", configFile)
 
204
        c.Assert(err, jc.ErrorIsNil)
 
205
        s.charmUpgradeClient.CheckCallNames(c, "GetCharmURL", "SetCharm")
 
206
        s.charmUpgradeClient.CheckCall(c, 1, "SetCharm", application.SetCharmConfig{
 
207
                ApplicationName: "foo",
 
208
                CharmID: jujucharmstore.CharmID{
 
209
                        URL:     s.resolvedCharmURL,
 
210
                        Channel: csclientparams.StableChannel,
 
211
                },
 
212
                ConfigSettingsYAML: "foo:{}",
 
213
        })
 
214
}
 
215
 
 
216
func (s *UpgradeCharmSuite) TestConfigSettingsMinFacadeVersion(c *gc.C) {
 
217
        tempdir := c.MkDir()
 
218
        configFile := filepath.Join(tempdir, "config.yaml")
 
219
        err := ioutil.WriteFile(configFile, []byte("foo:{}"), 0644)
 
220
        c.Assert(err, jc.ErrorIsNil)
 
221
 
 
222
        s.apiConnection.bestFacadeVersion = 1
 
223
        _, err = s.runUpgradeCharm(c, "foo", "--config", configFile)
 
224
        c.Assert(err, gc.ErrorMatches,
 
225
                "updating config at upgrade-charm time is not supported by server version 1.2.3")
 
226
}
 
227
 
 
228
type UpgradeCharmErrorsStateSuite struct {
33
229
        jujutesting.RepoSuite
34
230
        handler charmstore.HTTPCloseHandler
35
231
        srv     *httptest.Server
36
232
}
37
233
 
38
 
func (s *UpgradeCharmErrorsSuite) SetUpTest(c *gc.C) {
 
234
func (s *UpgradeCharmErrorsStateSuite) SetUpTest(c *gc.C) {
39
235
        s.RepoSuite.SetUpTest(c)
40
236
        // Set up the charm store testing server.
41
237
        handler, err := charmstore.NewServer(s.Session.DB("juju-testing"), nil, "", charmstore.ServerParams{
59
255
        })
60
256
}
61
257
 
62
 
var _ = gc.Suite(&UpgradeCharmErrorsSuite{})
 
258
var _ = gc.Suite(&UpgradeCharmErrorsStateSuite{})
63
259
 
64
260
func runUpgradeCharm(c *gc.C, args ...string) error {
65
 
        _, err := testing.RunCommand(c, NewUpgradeCharmCommand(), args...)
 
261
        _, err := coretesting.RunCommand(c, NewUpgradeCharmCommand(), args...)
66
262
        return err
67
263
}
68
264
 
69
 
func (s *UpgradeCharmErrorsSuite) TestInvalidArgs(c *gc.C) {
 
265
func (s *UpgradeCharmErrorsStateSuite) TestInvalidArgs(c *gc.C) {
70
266
        err := runUpgradeCharm(c)
71
267
        c.Assert(err, gc.ErrorMatches, "no application specified")
72
268
        err = runUpgradeCharm(c, "invalid:name")
75
271
        c.Assert(err, gc.ErrorMatches, `unrecognized args: \["bar"\]`)
76
272
}
77
273
 
78
 
func (s *UpgradeCharmErrorsSuite) TestInvalidService(c *gc.C) {
 
274
func (s *UpgradeCharmErrorsStateSuite) TestInvalidService(c *gc.C) {
79
275
        err := runUpgradeCharm(c, "phony")
80
276
        c.Assert(errors.Cause(err), gc.DeepEquals, &rpc.RequestError{
81
277
                Message: `application "phony" not found`,
83
279
        })
84
280
}
85
281
 
86
 
func (s *UpgradeCharmErrorsSuite) deployService(c *gc.C) {
 
282
func (s *UpgradeCharmErrorsStateSuite) deployService(c *gc.C) {
87
283
        ch := testcharms.Repo.ClonedDirPath(s.CharmsPath, "riak")
88
284
        err := runDeploy(c, ch, "riak", "--series", "quantal")
89
285
        c.Assert(err, jc.ErrorIsNil)
90
286
}
91
287
 
92
 
func (s *UpgradeCharmErrorsSuite) TestInvalidSwitchURL(c *gc.C) {
 
288
func (s *UpgradeCharmErrorsStateSuite) TestInvalidSwitchURL(c *gc.C) {
93
289
        s.deployService(c)
94
290
        err := runUpgradeCharm(c, "riak", "--switch=blah")
95
291
        c.Assert(err, gc.ErrorMatches, `cannot resolve URL "cs:blah": charm or bundle not found`)
98
294
        // TODO(dimitern): add tests with incompatible charms
99
295
}
100
296
 
101
 
func (s *UpgradeCharmErrorsSuite) TestNoPathFails(c *gc.C) {
 
297
func (s *UpgradeCharmErrorsStateSuite) TestNoPathFails(c *gc.C) {
102
298
        s.deployService(c)
103
299
        err := runUpgradeCharm(c, "riak")
104
300
        c.Assert(err, gc.ErrorMatches, "upgrading a local charm requires either --path or --switch")
105
301
}
106
302
 
107
 
func (s *UpgradeCharmErrorsSuite) TestSwitchAndRevisionFails(c *gc.C) {
 
303
func (s *UpgradeCharmErrorsStateSuite) TestSwitchAndRevisionFails(c *gc.C) {
108
304
        s.deployService(c)
109
305
        err := runUpgradeCharm(c, "riak", "--switch=riak", "--revision=2")
110
306
        c.Assert(err, gc.ErrorMatches, "--switch and --revision are mutually exclusive")
111
307
}
112
308
 
113
 
func (s *UpgradeCharmErrorsSuite) TestPathAndRevisionFails(c *gc.C) {
 
309
func (s *UpgradeCharmErrorsStateSuite) TestPathAndRevisionFails(c *gc.C) {
114
310
        s.deployService(c)
115
311
        err := runUpgradeCharm(c, "riak", "--path=foo", "--revision=2")
116
312
        c.Assert(err, gc.ErrorMatches, "--path and --revision are mutually exclusive")
117
313
}
118
314
 
119
 
func (s *UpgradeCharmErrorsSuite) TestSwitchAndPathFails(c *gc.C) {
 
315
func (s *UpgradeCharmErrorsStateSuite) TestSwitchAndPathFails(c *gc.C) {
120
316
        s.deployService(c)
121
317
        err := runUpgradeCharm(c, "riak", "--switch=riak", "--path=foo")
122
318
        c.Assert(err, gc.ErrorMatches, "--switch and --path are mutually exclusive")
123
319
}
124
320
 
125
 
func (s *UpgradeCharmErrorsSuite) TestInvalidRevision(c *gc.C) {
 
321
func (s *UpgradeCharmErrorsStateSuite) TestInvalidRevision(c *gc.C) {
126
322
        s.deployService(c)
127
323
        err := runUpgradeCharm(c, "riak", "--revision=blah")
128
 
        c.Assert(err, gc.ErrorMatches, `invalid value "blah" for flag --revision: strconv.ParseInt: parsing "blah": invalid syntax`)
 
324
        c.Assert(err, gc.ErrorMatches, `invalid value "blah" for flag --revision: strconv.(ParseInt|Atoi): parsing "blah": invalid syntax`)
129
325
}
130
326
 
131
 
type BaseUpgradeCharmSuite struct{}
 
327
type BaseUpgradeCharmStateSuite struct{}
132
328
 
133
 
type UpgradeCharmSuccessSuite struct {
134
 
        BaseUpgradeCharmSuite
 
329
type UpgradeCharmSuccessStateSuite struct {
 
330
        BaseUpgradeCharmStateSuite
135
331
        jujutesting.RepoSuite
136
 
        testing.CmdBlockHelper
 
332
        coretesting.CmdBlockHelper
137
333
        path string
138
334
        riak *state.Application
139
335
}
140
336
 
141
 
func (s *BaseUpgradeCharmSuite) assertUpgraded(c *gc.C, riak *state.Application, revision int, forced bool) *charm.URL {
 
337
func (s *BaseUpgradeCharmStateSuite) assertUpgraded(c *gc.C, riak *state.Application, revision int, forced bool) *charm.URL {
142
338
        err := riak.Refresh()
143
339
        c.Assert(err, jc.ErrorIsNil)
144
340
        ch, force, err := riak.Charm()
148
344
        return ch.URL()
149
345
}
150
346
 
151
 
var _ = gc.Suite(&UpgradeCharmSuccessSuite{})
 
347
var _ = gc.Suite(&UpgradeCharmSuccessStateSuite{})
152
348
 
153
 
func (s *UpgradeCharmSuccessSuite) SetUpTest(c *gc.C) {
 
349
func (s *UpgradeCharmSuccessStateSuite) SetUpTest(c *gc.C) {
154
350
        s.RepoSuite.SetUpTest(c)
155
351
        s.path = testcharms.Repo.ClonedDirPath(s.CharmsPath, "riak")
156
352
        err := runDeploy(c, s.path, "--series", "quantal")
162
358
        c.Assert(ch.Revision(), gc.Equals, 7)
163
359
        c.Assert(forced, jc.IsFalse)
164
360
 
165
 
        s.CmdBlockHelper = testing.NewCmdBlockHelper(s.APIState)
 
361
        s.CmdBlockHelper = coretesting.NewCmdBlockHelper(s.APIState)
166
362
        c.Assert(s.CmdBlockHelper, gc.NotNil)
167
363
        s.AddCleanup(func(*gc.C) { s.CmdBlockHelper.Close() })
168
364
}
169
365
 
170
 
func (s *UpgradeCharmSuccessSuite) assertLocalRevision(c *gc.C, revision int, path string) {
 
366
func (s *UpgradeCharmSuccessStateSuite) assertLocalRevision(c *gc.C, revision int, path string) {
171
367
        dir, err := charm.ReadCharmDir(path)
172
368
        c.Assert(err, jc.ErrorIsNil)
173
369
        c.Assert(dir.Revision(), gc.Equals, revision)
174
370
}
175
371
 
176
 
func (s *UpgradeCharmSuccessSuite) TestLocalRevisionUnchanged(c *gc.C) {
 
372
func (s *UpgradeCharmSuccessStateSuite) TestLocalRevisionUnchanged(c *gc.C) {
177
373
        err := runUpgradeCharm(c, "riak", "--path", s.path)
178
374
        c.Assert(err, jc.ErrorIsNil)
179
375
        curl := s.assertUpgraded(c, s.riak, 8, false)
183
379
        s.assertLocalRevision(c, 7, s.path)
184
380
}
185
381
 
186
 
func (s *UpgradeCharmSuccessSuite) TestBlockUpgradeCharm(c *gc.C) {
 
382
func (s *UpgradeCharmSuccessStateSuite) TestBlockUpgradeCharm(c *gc.C) {
187
383
        // Block operation
188
384
        s.BlockAllChanges(c, "TestBlockUpgradeCharm")
189
385
        err := runUpgradeCharm(c, "riak", "--path", s.path)
190
386
        s.AssertBlocked(c, err, ".*TestBlockUpgradeCharm.*")
191
387
}
192
388
 
193
 
func (s *UpgradeCharmSuccessSuite) TestRespectsLocalRevisionWhenPossible(c *gc.C) {
 
389
func (s *UpgradeCharmSuccessStateSuite) TestRespectsLocalRevisionWhenPossible(c *gc.C) {
194
390
        dir, err := charm.ReadCharmDir(s.path)
195
391
        c.Assert(err, jc.ErrorIsNil)
196
392
        err = dir.SetDiskRevision(42)
203
399
        s.assertLocalRevision(c, 42, s.path)
204
400
}
205
401
 
206
 
func (s *UpgradeCharmSuccessSuite) TestForcedSeriesUpgrade(c *gc.C) {
 
402
func (s *UpgradeCharmSuccessStateSuite) TestForcedSeriesUpgrade(c *gc.C) {
207
403
        path := testcharms.Repo.ClonedDirPath(c.MkDir(), "multi-series")
208
404
        err := runDeploy(c, path, "multi-series", "--series", "precise")
209
405
        c.Assert(err, jc.ErrorIsNil)
234
430
        c.Assert(ch.URL().String(), gc.Equals, "local:precise/multi-series2-8")
235
431
}
236
432
 
237
 
func (s *UpgradeCharmSuccessSuite) TestInitWithResources(c *gc.C) {
 
433
func (s *UpgradeCharmSuccessStateSuite) TestInitWithResources(c *gc.C) {
238
434
        testcharms.Repo.CharmArchivePath(s.CharmsPath, "dummy")
239
435
        dir := c.MkDir()
240
436
 
251
447
        d := upgradeCharmCommand{}
252
448
        args := []string{"dummy", "--resource", res1, "--resource", res2}
253
449
 
254
 
        err = testing.InitCommand(modelcmd.Wrap(&d), args)
 
450
        err = coretesting.InitCommand(modelcmd.Wrap(&d), args)
255
451
        c.Assert(err, jc.ErrorIsNil)
256
452
        c.Assert(d.Resources, gc.DeepEquals, map[string]string{
257
453
                "foo": foopath,
259
455
        })
260
456
}
261
457
 
262
 
func (s *UpgradeCharmSuccessSuite) TestForcedUnitsUpgrade(c *gc.C) {
 
458
func (s *UpgradeCharmSuccessStateSuite) TestForcedUnitsUpgrade(c *gc.C) {
263
459
        err := runUpgradeCharm(c, "riak", "--force-units", "--path", s.path)
264
460
        c.Assert(err, jc.ErrorIsNil)
265
461
        curl := s.assertUpgraded(c, s.riak, 8, true)
268
464
        s.assertLocalRevision(c, 7, s.path)
269
465
}
270
466
 
271
 
func (s *UpgradeCharmSuccessSuite) TestBlockForcedUnitsUpgrade(c *gc.C) {
 
467
func (s *UpgradeCharmSuccessStateSuite) TestBlockForcedUnitsUpgrade(c *gc.C) {
272
468
        // Block operation
273
469
        s.BlockAllChanges(c, "TestBlockForcedUpgrade")
274
470
        err := runUpgradeCharm(c, "riak", "--force-units", "--path", s.path)
279
475
        s.assertLocalRevision(c, 7, s.path)
280
476
}
281
477
 
282
 
func (s *UpgradeCharmSuccessSuite) TestCharmPath(c *gc.C) {
 
478
func (s *UpgradeCharmSuccessStateSuite) TestCharmPath(c *gc.C) {
283
479
        myriakPath := testcharms.Repo.ClonedDirPath(c.MkDir(), "riak")
284
480
 
285
481
        // Change the revision to 42 and upgrade to it with explicit revision.
292
488
        s.assertLocalRevision(c, 42, myriakPath)
293
489
}
294
490
 
295
 
func (s *UpgradeCharmSuccessSuite) TestCharmPathNoRevUpgrade(c *gc.C) {
 
491
func (s *UpgradeCharmSuccessStateSuite) TestCharmPathNoRevUpgrade(c *gc.C) {
296
492
        // Revision 7 is running to start with.
297
493
        myriakPath := testcharms.Repo.ClonedDirPath(c.MkDir(), "riak")
298
494
        s.assertLocalRevision(c, 7, myriakPath)
302
498
        c.Assert(curl.String(), gc.Equals, "local:quantal/riak-8")
303
499
}
304
500
 
305
 
func (s *UpgradeCharmSuccessSuite) TestCharmPathDifferentNameFails(c *gc.C) {
 
501
func (s *UpgradeCharmSuccessStateSuite) TestCharmPathDifferentNameFails(c *gc.C) {
306
502
        myriakPath := testcharms.Repo.RenamedClonedDirPath(s.CharmsPath, "riak", "myriak")
307
503
        err := runUpgradeCharm(c, "riak", "--path", myriakPath)
308
504
        c.Assert(err, gc.ErrorMatches, `cannot upgrade "riak" to "myriak"`)
309
505
}
310
506
 
311
 
type UpgradeCharmCharmStoreSuite struct {
312
 
        BaseUpgradeCharmSuite
 
507
type UpgradeCharmCharmStoreStateSuite struct {
 
508
        BaseUpgradeCharmStateSuite
313
509
        charmStoreSuite
314
510
}
315
511
 
316
 
var _ = gc.Suite(&UpgradeCharmCharmStoreSuite{})
 
512
var _ = gc.Suite(&UpgradeCharmCharmStoreStateSuite{})
317
513
 
318
514
var upgradeCharmAuthorizationTests = []struct {
319
515
        about        string
353
549
        expectError:  `cannot resolve charm URL "cs:~bob/trusty/wordpress6-47": cannot get "/~bob/trusty/wordpress6-47/meta/any\?include=id&include=supported-series&include=published": unauthorized: access denied for user "client-username"`,
354
550
}}
355
551
 
356
 
func (s *UpgradeCharmCharmStoreSuite) TestUpgradeCharmAuthorization(c *gc.C) {
 
552
func (s *UpgradeCharmCharmStoreStateSuite) TestUpgradeCharmAuthorization(c *gc.C) {
357
553
        testcharms.UploadCharm(c, s.client, "cs:~other/trusty/wordpress-0", "wordpress")
358
554
        err := runDeploy(c, "cs:~other/trusty/wordpress-0")
359
555
        c.Assert(err, jc.ErrorIsNil)
372
568
        }
373
569
}
374
570
 
375
 
func (s *UpgradeCharmCharmStoreSuite) TestSwitch(c *gc.C) {
 
571
func (s *UpgradeCharmCharmStoreStateSuite) TestSwitch(c *gc.C) {
376
572
        testcharms.UploadCharm(c, s.client, "cs:~other/trusty/riak-0", "riak")
377
573
        testcharms.UploadCharm(c, s.client, "cs:~other/trusty/anotherriak-7", "riak")
378
574
        err := runDeploy(c, "cs:~other/trusty/riak-0")
402
598
        c.Assert(curl.String(), gc.Equals, "cs:~other/trusty/anotherriak-42")
403
599
}
404
600
 
405
 
func (s *UpgradeCharmCharmStoreSuite) TestUpgradeCharmWithChannel(c *gc.C) {
 
601
func (s *UpgradeCharmCharmStoreStateSuite) TestUpgradeCharmWithChannel(c *gc.C) {
406
602
        id, ch := testcharms.UploadCharm(c, s.client, "cs:~client-username/trusty/wordpress-0", "wordpress")
407
603
        err := runDeploy(c, "cs:~client-username/trusty/wordpress-0")
408
604
        c.Assert(err, jc.ErrorIsNil)
426
622
        })
427
623
}
428
624
 
429
 
func (s *UpgradeCharmCharmStoreSuite) TestUpgradeWithTermsNotSigned(c *gc.C) {
 
625
func (s *UpgradeCharmCharmStoreStateSuite) TestUpgradeWithTermsNotSigned(c *gc.C) {
430
626
        id, ch := testcharms.UploadCharm(c, s.client, "quantal/terms1-1", "terms1")
431
627
        err := runDeploy(c, "quantal/terms1")
432
628
        c.Assert(err, jc.ErrorIsNil)
443
639
        err = runUpgradeCharm(c, "terms1")
444
640
        c.Assert(err, gc.ErrorMatches, expectedError)
445
641
}
 
642
 
 
643
type mockAPIConnection struct {
 
644
        api.Connection
 
645
        bestFacadeVersion int
 
646
        serverVersion     *version.Number
 
647
}
 
648
 
 
649
func (m *mockAPIConnection) BestFacadeVersion(name string) int {
 
650
        return m.bestFacadeVersion
 
651
}
 
652
 
 
653
func (m *mockAPIConnection) ServerVersion() (version.Number, bool) {
 
654
        if m.serverVersion != nil {
 
655
                return *m.serverVersion, true
 
656
        }
 
657
        return version.Number{}, false
 
658
}
 
659
 
 
660
func (*mockAPIConnection) Close() error {
 
661
        return nil
 
662
}
 
663
 
 
664
type mockCharmAdder struct {
 
665
        CharmAdder
 
666
        testing.Stub
 
667
}
 
668
 
 
669
func (m *mockCharmAdder) AddCharm(curl *charm.URL, channel csclientparams.Channel) error {
 
670
        m.MethodCall(m, "AddCharm", curl, channel)
 
671
        return m.NextErr()
 
672
}
 
673
 
 
674
type mockCharmClient struct {
 
675
        CharmClient
 
676
        testing.Stub
 
677
        charmInfo *charms.CharmInfo
 
678
}
 
679
 
 
680
func (m *mockCharmClient) CharmInfo(curl string) (*charms.CharmInfo, error) {
 
681
        m.MethodCall(m, "CharmInfo", curl)
 
682
        if err := m.NextErr(); err != nil {
 
683
                return nil, err
 
684
        }
 
685
        return m.charmInfo, nil
 
686
}
 
687
 
 
688
type mockCharmUpgradeClient struct {
 
689
        CharmUpgradeClient
 
690
        testing.Stub
 
691
        charmURL *charm.URL
 
692
}
 
693
 
 
694
func (m *mockCharmUpgradeClient) GetCharmURL(applicationName string) (*charm.URL, error) {
 
695
        m.MethodCall(m, "GetCharmURL", applicationName)
 
696
        return m.charmURL, m.NextErr()
 
697
}
 
698
 
 
699
func (m *mockCharmUpgradeClient) SetCharm(cfg application.SetCharmConfig) error {
 
700
        m.MethodCall(m, "SetCharm", cfg)
 
701
        return m.NextErr()
 
702
}
 
703
 
 
704
type mockModelConfigGetter struct {
 
705
        ModelConfigGetter
 
706
        testing.Stub
 
707
}
 
708
 
 
709
func (m *mockModelConfigGetter) ModelGet() (map[string]interface{}, error) {
 
710
        m.MethodCall(m, "ModelGet")
 
711
        return coretesting.FakeConfig(), m.NextErr()
 
712
}
 
713
 
 
714
type mockResourceLister struct {
 
715
        ResourceLister
 
716
        testing.Stub
 
717
}