github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/cmd/juju/switch_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package main 5 6 import ( 7 "os" 8 9 gitjujutesting "github.com/juju/testing" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/cmd/envcmd" 14 "github.com/juju/juju/environs/configstore" 15 _ "github.com/juju/juju/juju" 16 "github.com/juju/juju/testing" 17 ) 18 19 type SwitchSimpleSuite struct { 20 testing.FakeJujuHomeSuite 21 } 22 23 var _ = gc.Suite(&SwitchSimpleSuite{}) 24 25 func (*SwitchSimpleSuite) TestNoEnvironment(c *gc.C) { 26 envPath := gitjujutesting.HomePath(".juju", "environments.yaml") 27 err := os.Remove(envPath) 28 c.Assert(err, jc.ErrorIsNil) 29 _, err = testing.RunCommand(c, &SwitchCommand{}) 30 c.Assert(err, gc.ErrorMatches, "couldn't read the environment") 31 } 32 33 func (*SwitchSimpleSuite) TestNoDefault(c *gc.C) { 34 testing.WriteEnvironments(c, testing.MultipleEnvConfigNoDefault) 35 _, err := testing.RunCommand(c, &SwitchCommand{}) 36 c.Assert(err, gc.ErrorMatches, "no currently specified environment") 37 } 38 39 func (*SwitchSimpleSuite) TestShowsDefault(c *gc.C) { 40 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 41 context, err := testing.RunCommand(c, &SwitchCommand{}) 42 c.Assert(err, jc.ErrorIsNil) 43 c.Assert(testing.Stdout(context), gc.Equals, "erewhemos\n") 44 } 45 46 func (s *SwitchSimpleSuite) TestCurrentEnvironmentHasPrecidence(c *gc.C) { 47 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 48 s.FakeHomeSuite.Home.AddFiles(c, gitjujutesting.TestFile{".juju/current-environment", "fubar"}) 49 context, err := testing.RunCommand(c, &SwitchCommand{}) 50 c.Assert(err, jc.ErrorIsNil) 51 c.Assert(testing.Stdout(context), gc.Equals, "fubar\n") 52 } 53 54 func (*SwitchSimpleSuite) TestShowsJujuEnv(c *gc.C) { 55 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 56 os.Setenv("JUJU_ENV", "using-env") 57 context, err := testing.RunCommand(c, &SwitchCommand{}) 58 c.Assert(err, jc.ErrorIsNil) 59 c.Assert(testing.Stdout(context), gc.Equals, "using-env\n") 60 } 61 62 func (s *SwitchSimpleSuite) TestJujuEnvOverCurrentEnvironment(c *gc.C) { 63 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 64 s.FakeHomeSuite.Home.AddFiles(c, gitjujutesting.TestFile{".juju/current-environment", "fubar"}) 65 os.Setenv("JUJU_ENV", "using-env") 66 context, err := testing.RunCommand(c, &SwitchCommand{}) 67 c.Assert(err, jc.ErrorIsNil) 68 c.Assert(testing.Stdout(context), gc.Equals, "using-env\n") 69 } 70 71 func (*SwitchSimpleSuite) TestSettingWritesFile(c *gc.C) { 72 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 73 context, err := testing.RunCommand(c, &SwitchCommand{}, "erewhemos-2") 74 c.Assert(err, jc.ErrorIsNil) 75 c.Assert(testing.Stdout(context), gc.Equals, "erewhemos -> erewhemos-2\n") 76 c.Assert(envcmd.ReadCurrentEnvironment(), gc.Equals, "erewhemos-2") 77 } 78 79 func (*SwitchSimpleSuite) TestSettingToUnknown(c *gc.C) { 80 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 81 _, err := testing.RunCommand(c, &SwitchCommand{}, "unknown") 82 c.Assert(err, gc.ErrorMatches, `"unknown" is not a name of an existing defined environment`) 83 } 84 85 func (*SwitchSimpleSuite) TestSettingWhenJujuEnvSet(c *gc.C) { 86 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 87 os.Setenv("JUJU_ENV", "using-env") 88 _, err := testing.RunCommand(c, &SwitchCommand{}, "erewhemos-2") 89 c.Assert(err, gc.ErrorMatches, `cannot switch when JUJU_ENV is overriding the environment \(set to "using-env"\)`) 90 } 91 92 const expectedEnvironments = `erewhemos 93 erewhemos-2 94 ` 95 96 func (*SwitchSimpleSuite) TestListEnvironments(c *gc.C) { 97 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 98 context, err := testing.RunCommand(c, &SwitchCommand{}, "--list") 99 c.Assert(err, jc.ErrorIsNil) 100 c.Assert(testing.Stdout(context), gc.Equals, expectedEnvironments) 101 } 102 103 func (s *SwitchSimpleSuite) TestListEnvironmentsWithConfigstore(c *gc.C) { 104 memstore := configstore.NewMem() 105 s.PatchValue(&configstore.Default, func() (configstore.Storage, error) { 106 return memstore, nil 107 }) 108 info := memstore.CreateInfo("testing") 109 err := info.Write() 110 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 111 context, err := testing.RunCommand(c, &SwitchCommand{}, "--list") 112 c.Assert(err, jc.ErrorIsNil) 113 expected := expectedEnvironments + "testing\n" 114 c.Assert(testing.Stdout(context), gc.Equals, expected) 115 } 116 117 func (*SwitchSimpleSuite) TestListEnvironmentsOSJujuEnvSet(c *gc.C) { 118 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 119 os.Setenv("JUJU_ENV", "using-env") 120 context, err := testing.RunCommand(c, &SwitchCommand{}, "--list") 121 c.Assert(err, jc.ErrorIsNil) 122 c.Assert(testing.Stdout(context), gc.Equals, expectedEnvironments) 123 } 124 125 func (*SwitchSimpleSuite) TestListEnvironmentsAndChange(c *gc.C) { 126 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 127 _, err := testing.RunCommand(c, &SwitchCommand{}, "--list", "erewhemos-2") 128 c.Assert(err, gc.ErrorMatches, "cannot switch and list at the same time") 129 } 130 131 func (*SwitchSimpleSuite) TestTooManyParams(c *gc.C) { 132 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 133 _, err := testing.RunCommand(c, &SwitchCommand{}, "foo", "bar") 134 c.Assert(err, gc.ErrorMatches, `unrecognized args: ."bar".`) 135 }