github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/cmd/envcmd/environmentcommand_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package envcmd_test 5 6 import ( 7 "io" 8 "io/ioutil" 9 "os" 10 "testing" 11 12 "github.com/juju/cmd" 13 "github.com/juju/cmd/cmdtesting" 14 "github.com/juju/errors" 15 gitjujutesting "github.com/juju/testing" 16 jc "github.com/juju/testing/checkers" 17 gc "gopkg.in/check.v1" 18 19 "github.com/juju/juju/cmd/envcmd" 20 "github.com/juju/juju/environs/configstore" 21 "github.com/juju/juju/juju/osenv" 22 coretesting "github.com/juju/juju/testing" 23 "github.com/juju/juju/version" 24 ) 25 26 type EnvironmentCommandSuite struct { 27 coretesting.FakeJujuHomeSuite 28 } 29 30 var _ = gc.Suite(&EnvironmentCommandSuite{}) 31 32 func Test(t *testing.T) { gc.TestingT(t) } 33 34 func (s *EnvironmentCommandSuite) TestReadCurrentEnvironmentUnset(c *gc.C) { 35 env := envcmd.ReadCurrentEnvironment() 36 c.Assert(env, gc.Equals, "") 37 } 38 39 func (s *EnvironmentCommandSuite) TestReadCurrentEnvironmentSet(c *gc.C) { 40 err := envcmd.WriteCurrentEnvironment("fubar") 41 c.Assert(err, jc.ErrorIsNil) 42 env := envcmd.ReadCurrentEnvironment() 43 c.Assert(env, gc.Equals, "fubar") 44 } 45 46 func (s *EnvironmentCommandSuite) TestGetDefaultEnvironment(c *gc.C) { 47 env, err := envcmd.GetDefaultEnvironment() 48 c.Assert(env, gc.Equals, "erewhemos") 49 c.Assert(err, jc.ErrorIsNil) 50 } 51 52 func (s *EnvironmentCommandSuite) TestGetDefaultEnvironmentNothingSet(c *gc.C) { 53 envPath := gitjujutesting.HomePath(".juju", "environments.yaml") 54 err := os.Remove(envPath) 55 c.Assert(err, jc.ErrorIsNil) 56 env, err := envcmd.GetDefaultEnvironment() 57 c.Assert(env, gc.Equals, "") 58 c.Assert(err, jc.ErrorIsNil) 59 } 60 61 func (s *EnvironmentCommandSuite) TestGetDefaultEnvironmentCurrentEnvironmentSet(c *gc.C) { 62 err := envcmd.WriteCurrentEnvironment("fubar") 63 c.Assert(err, jc.ErrorIsNil) 64 env, err := envcmd.GetDefaultEnvironment() 65 c.Assert(env, gc.Equals, "fubar") 66 c.Assert(err, jc.ErrorIsNil) 67 } 68 69 func (s *EnvironmentCommandSuite) TestGetDefaultEnvironmentJujuEnvSet(c *gc.C) { 70 os.Setenv(osenv.JujuEnvEnvKey, "magic") 71 env, err := envcmd.GetDefaultEnvironment() 72 c.Assert(env, gc.Equals, "magic") 73 c.Assert(err, jc.ErrorIsNil) 74 } 75 76 func (s *EnvironmentCommandSuite) TestGetDefaultEnvironmentBothSet(c *gc.C) { 77 os.Setenv(osenv.JujuEnvEnvKey, "magic") 78 err := envcmd.WriteCurrentEnvironment("fubar") 79 c.Assert(err, jc.ErrorIsNil) 80 env, err := envcmd.GetDefaultEnvironment() 81 c.Assert(env, gc.Equals, "magic") 82 c.Assert(err, jc.ErrorIsNil) 83 } 84 85 func (s *EnvironmentCommandSuite) TestWriteAddsNewline(c *gc.C) { 86 err := envcmd.WriteCurrentEnvironment("fubar") 87 c.Assert(err, jc.ErrorIsNil) 88 current, err := ioutil.ReadFile(envcmd.GetCurrentEnvironmentFilePath()) 89 c.Assert(err, jc.ErrorIsNil) 90 c.Assert(string(current), gc.Equals, "fubar\n") 91 } 92 93 func (*EnvironmentCommandSuite) TestErrorWritingFile(c *gc.C) { 94 // Can't write a file over a directory. 95 os.MkdirAll(envcmd.GetCurrentEnvironmentFilePath(), 0777) 96 err := envcmd.WriteCurrentEnvironment("fubar") 97 c.Assert(err, gc.ErrorMatches, "unable to write to the environment file: .*") 98 } 99 100 func (s *EnvironmentCommandSuite) TestEnvironCommandInitExplicit(c *gc.C) { 101 // Take environment name from command line arg. 102 testEnsureEnvName(c, "explicit", "-e", "explicit") 103 } 104 105 func (s *EnvironmentCommandSuite) TestEnvironCommandInitMultipleConfigs(c *gc.C) { 106 // Take environment name from the default. 107 coretesting.WriteEnvironments(c, coretesting.MultipleEnvConfig) 108 testEnsureEnvName(c, coretesting.SampleEnvName) 109 } 110 111 func (s *EnvironmentCommandSuite) TestEnvironCommandInitSingleConfig(c *gc.C) { 112 // Take environment name from the one and only environment, 113 // even if it is not explicitly marked as default. 114 coretesting.WriteEnvironments(c, coretesting.SingleEnvConfigNoDefault) 115 testEnsureEnvName(c, coretesting.SampleEnvName) 116 } 117 118 func (s *EnvironmentCommandSuite) TestEnvironCommandInitEnvFile(c *gc.C) { 119 // If there is a current-environment file, use that. 120 err := envcmd.WriteCurrentEnvironment("fubar") 121 c.Assert(err, jc.ErrorIsNil) 122 testEnsureEnvName(c, "fubar") 123 } 124 125 func (s *EnvironmentCommandSuite) TestEnvironCommandInitNoEnvFile(c *gc.C) { 126 envPath := gitjujutesting.HomePath(".juju", "environments.yaml") 127 err := os.Remove(envPath) 128 c.Assert(err, jc.ErrorIsNil) 129 testEnsureEnvName(c, "") 130 } 131 132 func (s *EnvironmentCommandSuite) TestEnvironCommandInitMultipleConfigNoDefault(c *gc.C) { 133 // If there are multiple environments but no default, the connection name is empty. 134 coretesting.WriteEnvironments(c, coretesting.MultipleEnvConfigNoDefault) 135 testEnsureEnvName(c, "") 136 } 137 138 func (s *EnvironmentCommandSuite) TestBootstrapContext(c *gc.C) { 139 ctx := envcmd.BootstrapContext(&cmd.Context{}) 140 c.Assert(ctx.ShouldVerifyCredentials(), jc.IsTrue) 141 } 142 143 func (s *EnvironmentCommandSuite) TestBootstrapContextNoVerify(c *gc.C) { 144 ctx := envcmd.BootstrapContextNoVerify(&cmd.Context{}) 145 c.Assert(ctx.ShouldVerifyCredentials(), jc.IsFalse) 146 } 147 148 type testCommand struct { 149 envcmd.EnvCommandBase 150 } 151 152 func (c *testCommand) Info() *cmd.Info { 153 panic("should not be called") 154 } 155 156 func (c *testCommand) Run(ctx *cmd.Context) error { 157 panic("should not be called") 158 } 159 160 func initTestCommand(c *gc.C, args ...string) (*testCommand, error) { 161 cmd := new(testCommand) 162 wrapped := envcmd.Wrap(cmd) 163 return cmd, cmdtesting.InitCommand(wrapped, args) 164 } 165 166 func testEnsureEnvName(c *gc.C, expect string, args ...string) { 167 cmd, err := initTestCommand(c, args...) 168 c.Assert(err, jc.ErrorIsNil) 169 c.Assert(cmd.ConnectionName(), gc.Equals, expect) 170 } 171 172 type ConnectionEndpointSuite struct { 173 coretesting.FakeJujuHomeSuite 174 store configstore.Storage 175 endpoint configstore.APIEndpoint 176 } 177 178 var _ = gc.Suite(&ConnectionEndpointSuite{}) 179 180 func (s *ConnectionEndpointSuite) SetUpTest(c *gc.C) { 181 s.FakeHomeSuite.SetUpTest(c) 182 s.store = configstore.NewMem() 183 s.PatchValue(envcmd.GetConfigStore, func() (configstore.Storage, error) { 184 return s.store, nil 185 }) 186 newInfo := s.store.CreateInfo("env-name") 187 newInfo.SetAPICredentials(configstore.APICredentials{ 188 User: "foo", 189 Password: "foopass", 190 }) 191 s.endpoint = configstore.APIEndpoint{ 192 Addresses: []string{"0.1.2.3"}, 193 Hostnames: []string{"foo.invalid"}, 194 CACert: "certificated", 195 EnvironUUID: "fake-uuid", 196 } 197 newInfo.SetAPIEndpoint(s.endpoint) 198 err := newInfo.Write() 199 c.Assert(err, jc.ErrorIsNil) 200 } 201 202 func (s *ConnectionEndpointSuite) TestAPIEndpointInStoreCached(c *gc.C) { 203 cmd, err := initTestCommand(c, "-e", "env-name") 204 c.Assert(err, jc.ErrorIsNil) 205 endpoint, err := cmd.ConnectionEndpoint(false) 206 c.Assert(err, jc.ErrorIsNil) 207 c.Assert(endpoint, gc.DeepEquals, s.endpoint) 208 } 209 210 func (s *ConnectionEndpointSuite) TestAPIEndpointForEnvSuchName(c *gc.C) { 211 cmd, err := initTestCommand(c, "-e", "no-such-env") 212 c.Assert(err, jc.ErrorIsNil) 213 _, err = cmd.ConnectionEndpoint(false) 214 c.Assert(err, jc.Satisfies, errors.IsNotFound) 215 c.Assert(err, gc.ErrorMatches, `environment "no-such-env" not found`) 216 } 217 218 func (s *ConnectionEndpointSuite) TestAPIEndpointRefresh(c *gc.C) { 219 newEndpoint := configstore.APIEndpoint{ 220 Addresses: []string{"0.1.2.3"}, 221 Hostnames: []string{"foo.example.com"}, 222 CACert: "certificated", 223 EnvironUUID: "fake-uuid", 224 } 225 s.PatchValue(envcmd.EndpointRefresher, func(_ *envcmd.EnvCommandBase) (io.Closer, error) { 226 info, err := s.store.ReadInfo("env-name") 227 info.SetAPIEndpoint(newEndpoint) 228 err = info.Write() 229 c.Assert(err, jc.ErrorIsNil) 230 return new(closer), nil 231 }) 232 233 cmd, err := initTestCommand(c, "-e", "env-name") 234 c.Assert(err, jc.ErrorIsNil) 235 endpoint, err := cmd.ConnectionEndpoint(true) 236 c.Assert(err, jc.ErrorIsNil) 237 c.Assert(endpoint, gc.DeepEquals, newEndpoint) 238 } 239 240 type closer struct{} 241 242 func (*closer) Close() error { 243 return nil 244 } 245 246 type EnvironmentVersionSuite struct { 247 fake *fakeEnvGetter 248 } 249 250 var _ = gc.Suite(&EnvironmentVersionSuite{}) 251 252 type fakeEnvGetter struct { 253 agentVersion interface{} 254 err error 255 } 256 257 func (g *fakeEnvGetter) EnvironmentGet() (map[string]interface{}, error) { 258 if g.err != nil { 259 return nil, g.err 260 } else if g.agentVersion == nil { 261 return map[string]interface{}{}, nil 262 } else { 263 return map[string]interface{}{ 264 "agent-version": g.agentVersion, 265 }, nil 266 } 267 } 268 269 func (s *EnvironmentVersionSuite) SetUpTest(*gc.C) { 270 s.fake = new(fakeEnvGetter) 271 } 272 273 func (s *EnvironmentVersionSuite) TestApiCallFails(c *gc.C) { 274 s.fake.err = errors.New("boom") 275 _, err := envcmd.GetEnvironmentVersion(s.fake) 276 c.Assert(err, gc.ErrorMatches, "unable to retrieve environment config: boom") 277 } 278 279 func (s *EnvironmentVersionSuite) TestNoVersion(c *gc.C) { 280 _, err := envcmd.GetEnvironmentVersion(s.fake) 281 c.Assert(err, gc.ErrorMatches, "version not found in environment config") 282 } 283 284 func (s *EnvironmentVersionSuite) TestInvalidVersionType(c *gc.C) { 285 s.fake.agentVersion = 99 286 _, err := envcmd.GetEnvironmentVersion(s.fake) 287 c.Assert(err, gc.ErrorMatches, "invalid environment version type in config") 288 } 289 290 func (s *EnvironmentVersionSuite) TestInvalidVersion(c *gc.C) { 291 s.fake.agentVersion = "a.b.c" 292 _, err := envcmd.GetEnvironmentVersion(s.fake) 293 c.Assert(err, gc.ErrorMatches, "unable to parse environment version: .+") 294 } 295 296 func (s *EnvironmentVersionSuite) TestSuccess(c *gc.C) { 297 vs := "1.22.1" 298 s.fake.agentVersion = vs 299 v, err := envcmd.GetEnvironmentVersion(s.fake) 300 c.Assert(err, jc.ErrorIsNil) 301 c.Assert(v.Compare(version.MustParse(vs)), gc.Equals, 0) 302 }