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

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/api/controller/controller_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:
16
16
        "github.com/juju/juju/api/controller"
17
17
        "github.com/juju/juju/apiserver/common"
18
18
        "github.com/juju/juju/apiserver/params"
 
19
        "github.com/juju/juju/environs"
19
20
        jujutesting "github.com/juju/testing"
20
21
        "github.com/juju/utils"
21
22
)
27
28
var _ = gc.Suite(&Suite{})
28
29
 
29
30
func (s *Suite) TestInitiateMigration(c *gc.C) {
30
 
        client, stub := makeClient(params.InitiateMigrationResults{
31
 
                Results: []params.InitiateMigrationResult{{
32
 
                        MigrationId: "id",
33
 
                }},
34
 
        })
35
 
        spec := makeSpec()
36
 
        id, err := client.InitiateMigration(spec)
37
 
        c.Assert(err, jc.ErrorIsNil)
38
 
        c.Check(id, gc.Equals, "id")
39
 
        stub.CheckCalls(c, []jujutesting.StubCall{
40
 
                {"Controller.InitiateMigration", []interface{}{specToArgs(spec)}},
41
 
        })
 
31
        s.checkInitiateMigration(c, makeSpec())
42
32
}
43
33
 
44
34
func (s *Suite) TestInitiateMigrationExternalControl(c *gc.C) {
45
 
        client, stub := makeClient(params.InitiateMigrationResults{
46
 
                Results: []params.InitiateMigrationResult{{
47
 
                        MigrationId: "id",
48
 
                }},
49
 
        })
50
35
        spec := makeSpec()
51
36
        spec.ExternalControl = true
52
 
        _, err := client.InitiateMigration(spec)
 
37
        s.checkInitiateMigration(c, spec)
 
38
}
 
39
 
 
40
func (s *Suite) TestInitiateMigrationSkipPrechecks(c *gc.C) {
 
41
        spec := makeSpec()
 
42
        spec.SkipInitialPrechecks = true
 
43
        s.checkInitiateMigration(c, spec)
 
44
}
 
45
 
 
46
func (s *Suite) checkInitiateMigration(c *gc.C, spec controller.MigrationSpec) {
 
47
        client, stub := makeClient(params.InitiateMigrationResults{
 
48
                Results: []params.InitiateMigrationResult{{
 
49
                        MigrationId: "id",
 
50
                }},
 
51
        })
 
52
        id, err := client.InitiateMigration(spec)
53
53
        c.Assert(err, jc.ErrorIsNil)
 
54
        c.Check(id, gc.Equals, "id")
54
55
        stub.CheckCalls(c, []jujutesting.StubCall{
55
56
                {"Controller.InitiateMigration", []interface{}{specToArgs(spec)}},
56
57
        })
69
70
                Specs: []params.MigrationSpec{{
70
71
                        ModelTag: names.NewModelTag(spec.ModelUUID).String(),
71
72
                        TargetInfo: params.MigrationTargetInfo{
72
 
                                ControllerTag: names.NewModelTag(spec.TargetControllerUUID).String(),
 
73
                                ControllerTag: names.NewControllerTag(spec.TargetControllerUUID).String(),
73
74
                                Addrs:         spec.TargetAddrs,
74
75
                                CACert:        spec.TargetCACert,
75
76
                                AuthTag:       names.NewUserTag(spec.TargetUser).String(),
76
77
                                Password:      spec.TargetPassword,
77
78
                                Macaroons:     string(macsJSON),
78
79
                        },
79
 
                        ExternalControl: spec.ExternalControl,
 
80
                        ExternalControl:      spec.ExternalControl,
 
81
                        SkipInitialPrechecks: spec.SkipInitialPrechecks,
80
82
                }},
81
83
        }
82
84
}
124
126
        c.Check(stub.Calls(), gc.HasLen, 0) // API call shouldn't have happened
125
127
}
126
128
 
 
129
func (s *Suite) TestHostedModelConfigs_CallError(c *gc.C) {
 
130
        apiCaller := apitesting.APICallerFunc(func(string, int, string, string, interface{}, interface{}) error {
 
131
                return errors.New("boom")
 
132
        })
 
133
        client := controller.NewClient(apiCaller)
 
134
        config, err := client.HostedModelConfigs()
 
135
        c.Check(config, gc.HasLen, 0)
 
136
        c.Check(err, gc.ErrorMatches, "boom")
 
137
}
 
138
 
 
139
func (s *Suite) TestHostedModelConfigs_FormatResults(c *gc.C) {
 
140
        apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
 
141
                c.Assert(objType, gc.Equals, "Controller")
 
142
                c.Assert(request, gc.Equals, "HostedModelConfigs")
 
143
                c.Assert(arg, gc.IsNil)
 
144
                out := result.(*params.HostedModelConfigsResults)
 
145
                c.Assert(out, gc.NotNil)
 
146
                *out = params.HostedModelConfigsResults{
 
147
                        Models: []params.HostedModelConfig{
 
148
                                {
 
149
                                        Name:     "first",
 
150
                                        OwnerTag: "user-foo@bar",
 
151
                                        Config: map[string]interface{}{
 
152
                                                "name": "first",
 
153
                                        },
 
154
                                        CloudSpec: &params.CloudSpec{
 
155
                                                Type: "magic",
 
156
                                                Name: "first",
 
157
                                        },
 
158
                                }, {
 
159
                                        Name:     "second",
 
160
                                        OwnerTag: "bad-tag",
 
161
                                }, {
 
162
                                        Name:     "third",
 
163
                                        OwnerTag: "user-foo@bar",
 
164
                                        Config: map[string]interface{}{
 
165
                                                "name": "third",
 
166
                                        },
 
167
                                        CloudSpec: &params.CloudSpec{
 
168
                                                Name: "third",
 
169
                                        },
 
170
                                },
 
171
                        },
 
172
                }
 
173
                return nil
 
174
        })
 
175
        client := controller.NewClient(apiCaller)
 
176
        config, err := client.HostedModelConfigs()
 
177
        c.Assert(config, gc.HasLen, 3)
 
178
        c.Assert(err, jc.ErrorIsNil)
 
179
        first := config[0]
 
180
        c.Assert(first.Name, gc.Equals, "first")
 
181
        c.Assert(first.Owner, gc.Equals, names.NewUserTag("foo@bar"))
 
182
        c.Assert(first.Config, gc.DeepEquals, map[string]interface{}{
 
183
                "name": "first",
 
184
        })
 
185
        c.Assert(first.CloudSpec, gc.DeepEquals, environs.CloudSpec{
 
186
                Type: "magic",
 
187
                Name: "first",
 
188
        })
 
189
        second := config[1]
 
190
        c.Assert(second.Name, gc.Equals, "second")
 
191
        c.Assert(second.Error.Error(), gc.Equals, `"bad-tag" is not a valid tag`)
 
192
        third := config[2]
 
193
        c.Assert(third.Name, gc.Equals, "third")
 
194
        c.Assert(third.Error.Error(), gc.Equals, "validating CloudSpec: empty Type not valid")
 
195
}
 
196
 
127
197
func makeClient(results params.InitiateMigrationResults) (
128
198
        *controller.Client, *jujutesting.Stub,
129
199
) {