github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cmd/juju/commands/switch_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package commands 5 6 import ( 7 "os" 8 "runtime" 9 10 gitjujutesting "github.com/juju/testing" 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 14 "github.com/juju/juju/cmd/envcmd" 15 "github.com/juju/juju/environs/configstore" 16 "github.com/juju/juju/feature" 17 _ "github.com/juju/juju/juju" 18 "github.com/juju/juju/testing" 19 ) 20 21 type SwitchSimpleSuite struct { 22 testing.FakeJujuHomeSuite 23 } 24 25 var _ = gc.Suite(&SwitchSimpleSuite{}) 26 27 func (s *SwitchSimpleSuite) TestNoEnvironmentReadsConfigStore(c *gc.C) { 28 envPath := gitjujutesting.HomePath(".juju", "environments.yaml") 29 err := os.Remove(envPath) 30 c.Assert(err, jc.ErrorIsNil) 31 s.addTestSystem(c) 32 context, err := testing.RunCommand(c, &SwitchCommand{}, "--list") 33 c.Assert(err, jc.ErrorIsNil) 34 c.Assert(testing.Stdout(context), gc.Equals, "a-system (system)\n") 35 } 36 37 func (s *SwitchSimpleSuite) TestErrorReadingEnvironmentsFile(c *gc.C) { 38 if runtime.GOOS == "windows" { 39 c.Skip("bug 1496997: os.Chmod doesn't exist on windows, checking this on one platform is sufficent to test this case") 40 } 41 42 envPath := gitjujutesting.HomePath(".juju", "environments.yaml") 43 err := os.Chmod(envPath, 0) 44 c.Assert(err, jc.ErrorIsNil) 45 s.addTestSystem(c) 46 _, err = testing.RunCommand(c, &SwitchCommand{}, "--list") 47 c.Assert(err, gc.ErrorMatches, "couldn't read the environment: open .*: permission denied") 48 } 49 50 func (*SwitchSimpleSuite) TestNoDefault(c *gc.C) { 51 testing.WriteEnvironments(c, testing.MultipleEnvConfigNoDefault) 52 _, err := testing.RunCommand(c, &SwitchCommand{}) 53 c.Assert(err, gc.ErrorMatches, "no currently specified environment") 54 } 55 56 func (*SwitchSimpleSuite) TestNoDefaultNoEnvironmentsFile(c *gc.C) { 57 envPath := gitjujutesting.HomePath(".juju", "environments.yaml") 58 err := os.Remove(envPath) 59 c.Assert(err, jc.ErrorIsNil) 60 _, err = testing.RunCommand(c, &SwitchCommand{}) 61 c.Assert(err, gc.ErrorMatches, "no currently specified environment") 62 } 63 64 func (*SwitchSimpleSuite) TestShowsDefault(c *gc.C) { 65 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 66 context, err := testing.RunCommand(c, &SwitchCommand{}) 67 c.Assert(err, jc.ErrorIsNil) 68 c.Assert(testing.Stdout(context), gc.Equals, "erewhemos\n") 69 } 70 71 func (s *SwitchSimpleSuite) TestCurrentEnvironmentHasPrecedence(c *gc.C) { 72 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 73 envcmd.WriteCurrentEnvironment("fubar") 74 context, err := testing.RunCommand(c, &SwitchCommand{}) 75 c.Assert(err, jc.ErrorIsNil) 76 c.Assert(testing.Stdout(context), gc.Equals, "fubar\n") 77 } 78 79 func (s *SwitchSimpleSuite) TestCurrentSystemHasPrecedence(c *gc.C) { 80 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 81 envcmd.WriteCurrentSystem("fubar") 82 context, err := testing.RunCommand(c, &SwitchCommand{}) 83 c.Assert(err, jc.ErrorIsNil) 84 c.Assert(testing.Stdout(context), gc.Equals, "fubar (system)\n") 85 } 86 87 func (*SwitchSimpleSuite) TestShowsJujuEnv(c *gc.C) { 88 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 89 os.Setenv("JUJU_ENV", "using-env") 90 context, err := testing.RunCommand(c, &SwitchCommand{}) 91 c.Assert(err, jc.ErrorIsNil) 92 c.Assert(testing.Stdout(context), gc.Equals, "using-env\n") 93 } 94 95 func (s *SwitchSimpleSuite) TestJujuEnvOverCurrentEnvironment(c *gc.C) { 96 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 97 s.FakeHomeSuite.Home.AddFiles(c, gitjujutesting.TestFile{".juju/current-environment", "fubar"}) 98 os.Setenv("JUJU_ENV", "using-env") 99 context, err := testing.RunCommand(c, &SwitchCommand{}) 100 c.Assert(err, jc.ErrorIsNil) 101 c.Assert(testing.Stdout(context), gc.Equals, "using-env\n") 102 } 103 104 func (*SwitchSimpleSuite) TestSettingWritesFile(c *gc.C) { 105 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 106 context, err := testing.RunCommand(c, &SwitchCommand{}, "erewhemos-2") 107 c.Assert(err, jc.ErrorIsNil) 108 c.Assert(testing.Stderr(context), gc.Equals, "-> erewhemos-2\n") 109 currentEnv, err := envcmd.ReadCurrentEnvironment() 110 c.Assert(err, jc.ErrorIsNil) 111 c.Assert(currentEnv, gc.Equals, "erewhemos-2") 112 } 113 114 func (s *SwitchSimpleSuite) addTestSystem(c *gc.C) { 115 // First set up a system in the config store. 116 s.SetFeatureFlags(feature.JES) 117 store, err := configstore.Default() 118 c.Assert(err, jc.ErrorIsNil) 119 info := store.CreateInfo("a-system") 120 info.SetAPIEndpoint(configstore.APIEndpoint{ 121 Addresses: []string{"localhost"}, 122 CACert: testing.CACert, 123 ServerUUID: "server-uuid", 124 }) 125 err = info.Write() 126 c.Assert(err, jc.ErrorIsNil) 127 } 128 129 func (s *SwitchSimpleSuite) TestSettingWritesSystemFile(c *gc.C) { 130 s.addTestSystem(c) 131 context, err := testing.RunCommand(c, &SwitchCommand{}, "a-system") 132 c.Assert(err, jc.ErrorIsNil) 133 c.Assert(testing.Stderr(context), gc.Equals, "-> a-system (system)\n") 134 currSystem, err := envcmd.ReadCurrentSystem() 135 c.Assert(err, jc.ErrorIsNil) 136 c.Assert(currSystem, gc.Equals, "a-system") 137 } 138 139 func (s *SwitchSimpleSuite) TestListWithSystem(c *gc.C) { 140 s.addTestSystem(c) 141 context, err := testing.RunCommand(c, &SwitchCommand{}, "--list") 142 c.Assert(err, jc.ErrorIsNil) 143 c.Assert(testing.Stdout(context), gc.Equals, ` 144 a-system (system) 145 erewhemos 146 `[1:]) 147 } 148 149 func (*SwitchSimpleSuite) TestSettingToUnknown(c *gc.C) { 150 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 151 _, err := testing.RunCommand(c, &SwitchCommand{}, "unknown") 152 c.Assert(err, gc.ErrorMatches, `"unknown" is not a name of an existing defined environment or system`) 153 } 154 155 func (*SwitchSimpleSuite) TestSettingWhenJujuEnvSet(c *gc.C) { 156 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 157 os.Setenv("JUJU_ENV", "using-env") 158 _, err := testing.RunCommand(c, &SwitchCommand{}, "erewhemos-2") 159 c.Assert(err, gc.ErrorMatches, `cannot switch when JUJU_ENV is overriding the environment \(set to "using-env"\)`) 160 } 161 162 const expectedEnvironments = `erewhemos 163 erewhemos-2 164 ` 165 166 func (*SwitchSimpleSuite) TestListEnvironments(c *gc.C) { 167 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 168 context, err := testing.RunCommand(c, &SwitchCommand{}, "--list") 169 c.Assert(err, jc.ErrorIsNil) 170 c.Assert(testing.Stdout(context), gc.Equals, expectedEnvironments) 171 } 172 173 func (s *SwitchSimpleSuite) TestListEnvironmentsWithConfigstore(c *gc.C) { 174 memstore := configstore.NewMem() 175 s.PatchValue(&configstore.Default, func() (configstore.Storage, error) { 176 return memstore, nil 177 }) 178 info := memstore.CreateInfo("testing") 179 err := info.Write() 180 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 181 context, err := testing.RunCommand(c, &SwitchCommand{}, "--list") 182 c.Assert(err, jc.ErrorIsNil) 183 expected := expectedEnvironments + "testing\n" 184 c.Assert(testing.Stdout(context), gc.Equals, expected) 185 } 186 187 func (*SwitchSimpleSuite) TestListEnvironmentsOSJujuEnvSet(c *gc.C) { 188 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 189 os.Setenv("JUJU_ENV", "using-env") 190 context, err := testing.RunCommand(c, &SwitchCommand{}, "--list") 191 c.Assert(err, jc.ErrorIsNil) 192 c.Assert(testing.Stdout(context), gc.Equals, expectedEnvironments) 193 } 194 195 func (*SwitchSimpleSuite) TestListEnvironmentsAndChange(c *gc.C) { 196 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 197 _, err := testing.RunCommand(c, &SwitchCommand{}, "--list", "erewhemos-2") 198 c.Assert(err, gc.ErrorMatches, "cannot switch and list at the same time") 199 } 200 201 func (*SwitchSimpleSuite) TestTooManyParams(c *gc.C) { 202 testing.WriteEnvironments(c, testing.MultipleEnvConfig) 203 _, err := testing.RunCommand(c, &SwitchCommand{}, "foo", "bar") 204 c.Assert(err, gc.ErrorMatches, `unrecognized args: ."bar".`) 205 }