Left: | ||
Right: |
OLD | NEW |
---|---|
1 // Copyright 2013 Canonical Ltd. | 1 // Copyright 2013 Canonical Ltd. |
2 // Licensed under the AGPLv3, see LICENCE file for details. | 2 // Licensed under the AGPLv3, see LICENCE file for details. |
3 | 3 |
4 package main | 4 package main |
5 | 5 |
6 import ( | 6 import ( |
7 "os" | 7 "os" |
8 | 8 |
9 gc "launchpad.net/gocheck" | 9 gc "launchpad.net/gocheck" |
10 | 10 |
11 "launchpad.net/juju-core/cmd/envcmd" | 11 "launchpad.net/juju-core/cmd/envcmd" |
12 "launchpad.net/juju-core/environs/configstore" | |
12 _ "launchpad.net/juju-core/juju" | 13 _ "launchpad.net/juju-core/juju" |
13 "launchpad.net/juju-core/testing" | 14 "launchpad.net/juju-core/testing" |
14 ) | 15 ) |
15 | 16 |
16 type SwitchSimpleSuite struct { | 17 type SwitchSimpleSuite struct { |
17 testing.FakeJujuHomeSuite | 18 testing.FakeJujuHomeSuite |
18 } | 19 } |
19 | 20 |
20 var _ = gc.Suite(&SwitchSimpleSuite{}) | 21 var _ = gc.Suite(&SwitchSimpleSuite{}) |
21 | 22 |
23 var testCreds = configstore.APICredentials{ | |
24 User: "joe", | |
25 Password: "baloney", | |
26 } | |
27 | |
28 var apiEndpoint = configstore.APIEndpoint{ | |
29 Addresses: []string{"example.com", "kremvax.ru"}, | |
30 CACert: "cert", | |
31 } | |
32 | |
33 func patchEnvWithUser(c *gc.C, envName string) { | |
34 testing.WriteEnvironments(c, testing.MultipleEnvConfig) | |
35 | |
36 // Patch APICredentials so that <envName>.jenv file is avaliable for | |
37 // switch to read the envirionment user from. | |
38 store, err := configstore.Default() | |
39 c.Assert(err, gc.IsNil) | |
40 info, err := store.CreateInfo(envName) | |
41 info.SetAPIEndpoint(apiEndpoint) | |
42 info.SetAPICredentials(testCreds) | |
43 info.Write() | |
44 } | |
45 | |
22 func (*SwitchSimpleSuite) TestNoEnvironment(c *gc.C) { | 46 func (*SwitchSimpleSuite) TestNoEnvironment(c *gc.C) { |
23 envPath := testing.HomePath(".juju", "environments.yaml") | 47 envPath := testing.HomePath(".juju", "environments.yaml") |
24 err := os.Remove(envPath) | 48 err := os.Remove(envPath) |
25 c.Assert(err, gc.IsNil) | 49 c.Assert(err, gc.IsNil) |
26 _, err = testing.RunCommand(c, &SwitchCommand{}) | 50 _, err = testing.RunCommand(c, &SwitchCommand{}) |
27 c.Assert(err, gc.ErrorMatches, "couldn't read the environment") | 51 c.Assert(err, gc.ErrorMatches, "couldn't read the environment") |
28 } | 52 } |
29 | 53 |
30 func (*SwitchSimpleSuite) TestNoDefault(c *gc.C) { | 54 func (*SwitchSimpleSuite) TestNoDefault(c *gc.C) { |
31 testing.WriteEnvironments(c, testing.MultipleEnvConfigNoDefault) | 55 testing.WriteEnvironments(c, testing.MultipleEnvConfigNoDefault) |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 c.Assert(err, gc.IsNil) | 128 c.Assert(err, gc.IsNil) |
105 c.Assert(testing.Stdout(context), gc.Equals, expectedEnvironments) | 129 c.Assert(testing.Stdout(context), gc.Equals, expectedEnvironments) |
106 } | 130 } |
107 | 131 |
108 func (*SwitchSimpleSuite) TestListEnvironmentsAndChange(c *gc.C) { | 132 func (*SwitchSimpleSuite) TestListEnvironmentsAndChange(c *gc.C) { |
109 testing.WriteEnvironments(c, testing.MultipleEnvConfig) | 133 testing.WriteEnvironments(c, testing.MultipleEnvConfig) |
110 _, err := testing.RunCommand(c, &SwitchCommand{}, "--list", "erewhemos-2 ") | 134 _, err := testing.RunCommand(c, &SwitchCommand{}, "--list", "erewhemos-2 ") |
111 c.Assert(err, gc.ErrorMatches, "cannot switch and list at the same time" ) | 135 c.Assert(err, gc.ErrorMatches, "cannot switch and list at the same time" ) |
112 } | 136 } |
113 | 137 |
138 func (*SwitchSimpleSuite) TestListEnvironmentInfoDefault(c *gc.C) { | |
139 patchEnvWithUser(c, "erewhemos") | |
140 context, err := testing.RunCommand(c, &SwitchCommand{}, "--format", "yam l") | |
141 c.Assert(err, gc.IsNil) | |
142 c.Assert(testing.Stdout(context), gc.Equals, | |
fwereade
2014/06/02 08:56:03
It's rarely a good idea to test yaml directly -- b
| |
143 "user-name: joe\n"+ | |
144 "environ-name: erewhemos\n"+ | |
145 "state-servers:\n"+ | |
146 "- example.com\n"+ | |
147 "- kremvax.ru\n") | |
148 } | |
149 | |
150 func (*SwitchSimpleSuite) TestListEnvironmentInfoYaml(c *gc.C) { | |
151 patchEnvWithUser(c, "erewhemos") | |
152 context, err := testing.RunCommand(c, &SwitchCommand{}, "--format", "yam l") | |
153 c.Assert(err, gc.IsNil) | |
154 c.Assert(testing.Stdout(context), gc.Equals, | |
fwereade
2014/06/02 08:56:03
ditto
| |
155 "user-name: joe\n"+ | |
156 "environ-name: erewhemos\n"+ | |
157 "state-servers:\n"+ | |
158 "- example.com\n"+ | |
159 "- kremvax.ru\n") | |
160 } | |
161 | |
162 func (*SwitchSimpleSuite) TestListEnvironmentInfoJson(c *gc.C) { | |
163 patchEnvWithUser(c, "erewhemos") | |
164 context, err := testing.RunCommand(c, &SwitchCommand{}, "--format", "jso n") | |
165 c.Assert(err, gc.IsNil) | |
166 c.Assert(testing.Stdout(context), gc.Equals, "{\"user-name\":\"joe\",\"e nviron-name\":\"erewhemos\",\"state-servers\":[\"example.com\",\"kremvax.ru\"]}\ n") | |
fwereade
2014/06/02 08:56:03
ditto
| |
167 } | |
168 | |
169 func (*SwitchSimpleSuite) TestListEnvironmentInfoShowOldEnv(c *gc.C) { | |
170 patchEnvWithUser(c, "erewhemos-2") | |
171 context, err := testing.RunCommand(c, &SwitchCommand{}, "erewhemos-2", " --format", "json") | |
172 c.Assert(err, gc.IsNil) | |
173 c.Assert(testing.Stdout(context), gc.Equals, "{\"user-name\":\"joe\",\"e nviron-name\":\"erewhemos-2\",\"previous-environ-name\":\"erewhemos\",\"state-se rvers\":[\"example.com\",\"kremvax.ru\"]}\n") | |
fwereade
2014/06/02 08:56:03
ditto
| |
174 } | |
175 | |
114 func (*SwitchSimpleSuite) TestTooManyParams(c *gc.C) { | 176 func (*SwitchSimpleSuite) TestTooManyParams(c *gc.C) { |
115 testing.WriteEnvironments(c, testing.MultipleEnvConfig) | 177 testing.WriteEnvironments(c, testing.MultipleEnvConfig) |
116 _, err := testing.RunCommand(c, &SwitchCommand{}, "foo", "bar") | 178 _, err := testing.RunCommand(c, &SwitchCommand{}, "foo", "bar") |
117 c.Assert(err, gc.ErrorMatches, `unrecognized args: ."bar".`) | 179 c.Assert(err, gc.ErrorMatches, `unrecognized args: ."bar".`) |
118 } | 180 } |
OLD | NEW |