~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/apiserver/migrationtarget/migrationtarget_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 2016 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package migrationtarget_test
 
5
 
 
6
import (
 
7
        "github.com/juju/errors"
 
8
        jc "github.com/juju/testing/checkers"
 
9
        "github.com/juju/utils"
 
10
        gc "gopkg.in/check.v1"
 
11
        "gopkg.in/juju/names.v2"
 
12
 
 
13
        "github.com/juju/juju/apiserver/common"
 
14
        "github.com/juju/juju/apiserver/facade/facadetest"
 
15
        "github.com/juju/juju/apiserver/migrationtarget"
 
16
        "github.com/juju/juju/apiserver/params"
 
17
        apiservertesting "github.com/juju/juju/apiserver/testing"
 
18
        "github.com/juju/juju/core/description"
 
19
        "github.com/juju/juju/provider/dummy"
 
20
        "github.com/juju/juju/state"
 
21
        statetesting "github.com/juju/juju/state/testing"
 
22
        "github.com/juju/juju/testing"
 
23
)
 
24
 
 
25
type Suite struct {
 
26
        statetesting.StateSuite
 
27
        resources  *common.Resources
 
28
        authorizer apiservertesting.FakeAuthorizer
 
29
}
 
30
 
 
31
var _ = gc.Suite(&Suite{})
 
32
 
 
33
func (s *Suite) SetUpTest(c *gc.C) {
 
34
        // Set up InitialConfig with a dummy provider configuration. This
 
35
        // is required to allow model import test to work.
 
36
        s.InitialConfig = testing.CustomModelConfig(c, dummy.SampleConfig())
 
37
 
 
38
        // The call up to StateSuite's SetUpTest uses s.InitialConfig so
 
39
        // it has to happen here.
 
40
        s.StateSuite.SetUpTest(c)
 
41
 
 
42
        s.resources = common.NewResources()
 
43
        s.AddCleanup(func(*gc.C) { s.resources.StopAll() })
 
44
 
 
45
        s.authorizer = apiservertesting.FakeAuthorizer{
 
46
                Tag: s.Owner,
 
47
        }
 
48
}
 
49
 
 
50
func (s *Suite) TestFacadeRegistered(c *gc.C) {
 
51
        factory, err := common.Facades.GetFactory("MigrationTarget", 1)
 
52
        c.Assert(err, jc.ErrorIsNil)
 
53
 
 
54
        api, err := factory(facadetest.Context{
 
55
                State_:     s.State,
 
56
                Resources_: s.resources,
 
57
                Auth_:      s.authorizer,
 
58
        })
 
59
        c.Assert(err, jc.ErrorIsNil)
 
60
        c.Assert(api, gc.FitsTypeOf, new(migrationtarget.API))
 
61
}
 
62
 
 
63
func (s *Suite) TestNotUser(c *gc.C) {
 
64
        s.authorizer.Tag = names.NewMachineTag("0")
 
65
        _, err := s.newAPI()
 
66
        c.Assert(errors.Cause(err), gc.Equals, common.ErrPerm)
 
67
}
 
68
 
 
69
func (s *Suite) TestNotControllerAdmin(c *gc.C) {
 
70
        s.authorizer.Tag = names.NewUserTag("jrandomuser")
 
71
        _, err := s.newAPI()
 
72
        c.Assert(errors.Cause(err), gc.Equals, common.ErrPerm)
 
73
}
 
74
 
 
75
func (s *Suite) importModel(c *gc.C, api *migrationtarget.API) names.ModelTag {
 
76
        uuid, bytes := s.makeExportedModel(c)
 
77
        err := api.Import(params.SerializedModel{Bytes: bytes})
 
78
        c.Assert(err, jc.ErrorIsNil)
 
79
        return names.NewModelTag(uuid)
 
80
}
 
81
 
 
82
func (s *Suite) TestImport(c *gc.C) {
 
83
        api := s.mustNewAPI(c)
 
84
        tag := s.importModel(c, api)
 
85
        // Check the model was imported.
 
86
        model, err := s.State.GetModel(tag)
 
87
        c.Assert(err, jc.ErrorIsNil)
 
88
        c.Assert(model.Name(), gc.Equals, "some-model")
 
89
        c.Assert(model.MigrationMode(), gc.Equals, state.MigrationModeImporting)
 
90
}
 
91
 
 
92
func (s *Suite) TestAbort(c *gc.C) {
 
93
        api := s.mustNewAPI(c)
 
94
        tag := s.importModel(c, api)
 
95
 
 
96
        err := api.Abort(params.ModelArgs{ModelTag: tag.String()})
 
97
        c.Assert(err, jc.ErrorIsNil)
 
98
 
 
99
        // The model should no longer exist.
 
100
        _, err = s.State.GetModel(tag)
 
101
        c.Assert(err, gc.ErrorMatches, `model not found`)
 
102
}
 
103
 
 
104
func (s *Suite) TestAbortNotATag(c *gc.C) {
 
105
        api := s.mustNewAPI(c)
 
106
        err := api.Abort(params.ModelArgs{ModelTag: "not-a-tag"})
 
107
        c.Assert(err, gc.ErrorMatches, `"not-a-tag" is not a valid tag`)
 
108
}
 
109
 
 
110
func (s *Suite) TestAbortMissingEnv(c *gc.C) {
 
111
        api := s.mustNewAPI(c)
 
112
        newUUID := utils.MustNewUUID().String()
 
113
        err := api.Abort(params.ModelArgs{ModelTag: names.NewModelTag(newUUID).String()})
 
114
        c.Assert(err, gc.ErrorMatches, `model not found`)
 
115
}
 
116
 
 
117
func (s *Suite) TestAbortNotImportingEnv(c *gc.C) {
 
118
        st := s.Factory.MakeModel(c, nil)
 
119
        defer st.Close()
 
120
        model, err := st.Model()
 
121
        c.Assert(err, jc.ErrorIsNil)
 
122
 
 
123
        api := s.mustNewAPI(c)
 
124
        err = api.Abort(params.ModelArgs{ModelTag: model.ModelTag().String()})
 
125
        c.Assert(err, gc.ErrorMatches, `migration mode for the model is not importing`)
 
126
}
 
127
 
 
128
func (s *Suite) TestActivate(c *gc.C) {
 
129
        api := s.mustNewAPI(c)
 
130
        tag := s.importModel(c, api)
 
131
 
 
132
        err := api.Activate(params.ModelArgs{ModelTag: tag.String()})
 
133
        c.Assert(err, jc.ErrorIsNil)
 
134
 
 
135
        // The model should no longer exist.
 
136
        model, err := s.State.GetModel(tag)
 
137
        c.Assert(err, jc.ErrorIsNil)
 
138
        c.Assert(model.MigrationMode(), gc.Equals, state.MigrationModeActive)
 
139
}
 
140
 
 
141
func (s *Suite) TestActivateNotATag(c *gc.C) {
 
142
        api := s.mustNewAPI(c)
 
143
        err := api.Activate(params.ModelArgs{ModelTag: "not-a-tag"})
 
144
        c.Assert(err, gc.ErrorMatches, `"not-a-tag" is not a valid tag`)
 
145
}
 
146
 
 
147
func (s *Suite) TestActivateMissingEnv(c *gc.C) {
 
148
        api := s.mustNewAPI(c)
 
149
        newUUID := utils.MustNewUUID().String()
 
150
        err := api.Activate(params.ModelArgs{ModelTag: names.NewModelTag(newUUID).String()})
 
151
        c.Assert(err, gc.ErrorMatches, `model not found`)
 
152
}
 
153
 
 
154
func (s *Suite) TestActivateNotImportingEnv(c *gc.C) {
 
155
        st := s.Factory.MakeModel(c, nil)
 
156
        defer st.Close()
 
157
        model, err := st.Model()
 
158
        c.Assert(err, jc.ErrorIsNil)
 
159
 
 
160
        api := s.mustNewAPI(c)
 
161
        err = api.Activate(params.ModelArgs{ModelTag: model.ModelTag().String()})
 
162
        c.Assert(err, gc.ErrorMatches, `migration mode for the model is not importing`)
 
163
}
 
164
 
 
165
func (s *Suite) newAPI() (*migrationtarget.API, error) {
 
166
        return migrationtarget.NewAPI(s.State, s.resources, s.authorizer)
 
167
}
 
168
 
 
169
func (s *Suite) mustNewAPI(c *gc.C) *migrationtarget.API {
 
170
        api, err := s.newAPI()
 
171
        c.Assert(err, jc.ErrorIsNil)
 
172
        return api
 
173
}
 
174
 
 
175
func (s *Suite) makeExportedModel(c *gc.C) (string, []byte) {
 
176
        model, err := s.State.Export()
 
177
        c.Assert(err, jc.ErrorIsNil)
 
178
 
 
179
        newUUID := utils.MustNewUUID().String()
 
180
        model.UpdateConfig(map[string]interface{}{
 
181
                "name": "some-model",
 
182
                "uuid": newUUID,
 
183
        })
 
184
 
 
185
        bytes, err := description.Serialize(model)
 
186
        c.Assert(err, jc.ErrorIsNil)
 
187
        return newUUID, bytes
 
188
}