github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/cmd/juju/cmd_test.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package main 5 6 import ( 7 "os" 8 9 "github.com/juju/cmd" 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/juju/osenv" 15 "github.com/juju/juju/juju/testing" 16 coretesting "github.com/juju/juju/testing" 17 ) 18 19 type CmdSuite struct { 20 testing.JujuConnSuite 21 } 22 23 var _ = gc.Suite(&CmdSuite{}) 24 25 const envConfig = ` 26 default: 27 peckham 28 environments: 29 peckham: 30 type: dummy 31 state-server: false 32 admin-secret: arble 33 authorized-keys: i-am-a-key 34 default-series: raring 35 walthamstow: 36 type: dummy 37 state-server: false 38 authorized-keys: i-am-a-key 39 brokenenv: 40 type: dummy 41 broken: Bootstrap Destroy 42 state-server: false 43 authorized-keys: i-am-a-key 44 agent-stream: proposed 45 devenv: 46 type: dummy 47 state-server: false 48 admin-secret: arble 49 authorized-keys: i-am-a-key 50 default-series: raring 51 agent-stream: proposed 52 ` 53 54 func (s *CmdSuite) SetUpTest(c *gc.C) { 55 s.JujuConnSuite.SetUpTest(c) 56 coretesting.WriteEnvironments(c, envConfig, "peckham", "walthamstow", "brokenenv") 57 } 58 59 func (s *CmdSuite) TearDownTest(c *gc.C) { 60 s.JujuConnSuite.TearDownTest(c) 61 } 62 63 // testInit checks that a command initialises correctly 64 // with the given set of arguments. 65 func testInit(c *gc.C, com cmd.Command, args []string, errPat string) { 66 err := coretesting.InitCommand(com, args) 67 if errPat != "" { 68 c.Assert(err, gc.ErrorMatches, errPat) 69 } else { 70 c.Assert(err, jc.ErrorIsNil) 71 } 72 } 73 74 type HasConnectionName interface { 75 ConnectionName() string 76 } 77 78 // assertEnvName asserts that the Command is using 79 // the given environment name. 80 // Since every command has a different type, 81 // we use reflection to look at the value of the 82 // Conn field in the value. 83 func assertEnvName(c *gc.C, com cmd.Command, name string) { 84 i, ok := com.(HasConnectionName) 85 c.Assert(ok, jc.IsTrue) 86 c.Assert(i.ConnectionName(), gc.Equals, name) 87 } 88 89 // All members of EnvironmentInitTests are tested for the -environment and -e 90 // flags, and that extra arguments will cause parsing to fail. 91 var EnvironmentInitTests = []func() (envcmd.EnvironCommand, []string){ 92 func() (envcmd.EnvironCommand, []string) { return new(BootstrapCommand), nil }, 93 func() (envcmd.EnvironCommand, []string) { 94 return new(DeployCommand), []string{"charm-name", "service-name"} 95 }, 96 func() (envcmd.EnvironCommand, []string) { return new(StatusCommand), nil }, 97 } 98 99 // TestEnvironmentInit tests that all commands which accept 100 // the --environment variable initialise their 101 // environment name correctly. 102 func (*CmdSuite) TestEnvironmentInit(c *gc.C) { 103 for i, cmdFunc := range EnvironmentInitTests { 104 c.Logf("test %d", i) 105 com, args := cmdFunc() 106 testInit(c, envcmd.Wrap(com), args, "") 107 assertEnvName(c, com, "peckham") 108 109 com, args = cmdFunc() 110 testInit(c, envcmd.Wrap(com), append(args, "-e", "walthamstow"), "") 111 assertEnvName(c, com, "walthamstow") 112 113 com, args = cmdFunc() 114 testInit(c, envcmd.Wrap(com), append(args, "--environment", "walthamstow"), "") 115 assertEnvName(c, com, "walthamstow") 116 117 // JUJU_ENV is the final place the environment can be overriden 118 com, args = cmdFunc() 119 oldenv := os.Getenv(osenv.JujuEnvEnvKey) 120 os.Setenv(osenv.JujuEnvEnvKey, "walthamstow") 121 testInit(c, envcmd.Wrap(com), args, "") 122 os.Setenv(osenv.JujuEnvEnvKey, oldenv) 123 assertEnvName(c, com, "walthamstow") 124 } 125 } 126 127 var deployTests = []struct { 128 args []string 129 com *DeployCommand 130 }{ 131 { 132 []string{"charm-name"}, 133 &DeployCommand{}, 134 }, { 135 []string{"charm-name", "service-name"}, 136 &DeployCommand{ServiceName: "service-name"}, 137 }, { 138 []string{"--repository", "/path/to/another-repo", "charm-name"}, 139 &DeployCommand{RepoPath: "/path/to/another-repo"}, 140 }, { 141 []string{"--upgrade", "charm-name"}, 142 &DeployCommand{BumpRevision: true}, 143 }, { 144 []string{"-u", "charm-name"}, 145 &DeployCommand{BumpRevision: true}, 146 }, { 147 []string{"--num-units", "33", "charm-name"}, 148 &DeployCommand{UnitCommandBase: UnitCommandBase{NumUnits: 33}}, 149 }, { 150 []string{"-n", "104", "charm-name"}, 151 &DeployCommand{UnitCommandBase: UnitCommandBase{NumUnits: 104}}, 152 }, 153 } 154 155 func initExpectations(com *DeployCommand) { 156 if com.CharmName == "" { 157 com.CharmName = "charm-name" 158 } 159 if com.NumUnits == 0 { 160 com.NumUnits = 1 161 } 162 if com.RepoPath == "" { 163 com.RepoPath = "/path/to/repo" 164 } 165 com.SetEnvName("peckham") 166 } 167 168 func initDeployCommand(args ...string) (*DeployCommand, error) { 169 com := &DeployCommand{} 170 return com, coretesting.InitCommand(envcmd.Wrap(com), args) 171 } 172 173 func (*CmdSuite) TestDeployCommandInit(c *gc.C) { 174 defer os.Setenv(osenv.JujuRepositoryEnvKey, os.Getenv(osenv.JujuRepositoryEnvKey)) 175 os.Setenv(osenv.JujuRepositoryEnvKey, "/path/to/repo") 176 177 for _, t := range deployTests { 178 initExpectations(t.com) 179 com, err := initDeployCommand(t.args...) 180 c.Assert(err, jc.ErrorIsNil) 181 c.Assert(com, gc.DeepEquals, t.com) 182 } 183 184 // test relative --config path 185 ctx := coretesting.Context(c) 186 expected := []byte("test: data") 187 path := ctx.AbsPath("testconfig.yaml") 188 file, err := os.Create(path) 189 c.Assert(err, jc.ErrorIsNil) 190 _, err = file.Write(expected) 191 c.Assert(err, jc.ErrorIsNil) 192 file.Close() 193 194 com, err := initDeployCommand("--config", "testconfig.yaml", "charm-name") 195 c.Assert(err, jc.ErrorIsNil) 196 actual, err := com.Config.Read(ctx) 197 c.Assert(err, jc.ErrorIsNil) 198 c.Assert(expected, gc.DeepEquals, actual) 199 200 // missing args 201 _, err = initDeployCommand() 202 c.Assert(err, gc.ErrorMatches, "no charm specified") 203 204 // environment tested elsewhere 205 } 206 207 func initAddUnitCommand(args ...string) (*AddUnitCommand, error) { 208 com := &AddUnitCommand{} 209 return com, coretesting.InitCommand(com, args) 210 } 211 212 func (*CmdSuite) TestAddUnitCommandInit(c *gc.C) { 213 // missing args 214 _, err := initAddUnitCommand() 215 c.Assert(err, gc.ErrorMatches, "no service specified") 216 217 // bad unit count 218 _, err = initDeployCommand("charm-name", "--num-units", "0") 219 c.Assert(err, gc.ErrorMatches, "--num-units must be a positive integer") 220 _, err = initDeployCommand("charm-name", "-n", "0") 221 c.Assert(err, gc.ErrorMatches, "--num-units must be a positive integer") 222 223 // environment tested elsewhere 224 } 225 226 func initExposeCommand(args ...string) (*ExposeCommand, error) { 227 com := &ExposeCommand{} 228 return com, coretesting.InitCommand(com, args) 229 } 230 231 func (*CmdSuite) TestExposeCommandInit(c *gc.C) { 232 // missing args 233 _, err := initExposeCommand() 234 c.Assert(err, gc.ErrorMatches, "no service name specified") 235 236 // environment tested elsewhere 237 } 238 239 func initUnexposeCommand(args ...string) (*UnexposeCommand, error) { 240 com := &UnexposeCommand{} 241 return com, coretesting.InitCommand(com, args) 242 } 243 244 func (*CmdSuite) TestUnexposeCommandInit(c *gc.C) { 245 // missing args 246 _, err := initUnexposeCommand() 247 c.Assert(err, gc.ErrorMatches, "no service name specified") 248 249 // environment tested elsewhere 250 } 251 252 func initSSHCommand(args ...string) (*SSHCommand, error) { 253 com := &SSHCommand{} 254 return com, coretesting.InitCommand(com, args) 255 } 256 257 func (*CmdSuite) TestSSHCommandInit(c *gc.C) { 258 // missing args 259 _, err := initSSHCommand() 260 c.Assert(err, gc.ErrorMatches, "no target name specified") 261 } 262 263 func initSCPCommand(args ...string) (*SCPCommand, error) { 264 com := &SCPCommand{} 265 return com, coretesting.InitCommand(com, args) 266 } 267 268 func (*CmdSuite) TestSCPCommandInit(c *gc.C) { 269 // missing args 270 _, err := initSCPCommand() 271 c.Assert(err, gc.ErrorMatches, "at least two arguments required") 272 273 // not enough args 274 _, err = initSCPCommand("mysql/0:foo") 275 c.Assert(err, gc.ErrorMatches, "at least two arguments required") 276 } 277 278 func initGetCommand(args ...string) (*GetCommand, error) { 279 com := &GetCommand{} 280 return com, coretesting.InitCommand(com, args) 281 } 282 283 func (*CmdSuite) TestGetCommandInit(c *gc.C) { 284 // missing args 285 _, err := initGetCommand() 286 c.Assert(err, gc.ErrorMatches, "no service name specified") 287 } 288 289 func initSetCommand(args ...string) (*SetCommand, error) { 290 com := &SetCommand{} 291 return com, coretesting.InitCommand(com, args) 292 } 293 294 func (*CmdSuite) TestSetCommandInit(c *gc.C) { 295 // missing args 296 _, err := initSetCommand() 297 c.Assert(err, gc.ErrorMatches, "no service name specified") 298 // missing service name 299 _, err = initSetCommand("name=cow") 300 c.Assert(err, gc.ErrorMatches, "no service name specified") 301 302 // test --config path 303 expected := []byte("this: is some test data") 304 ctx := coretesting.Context(c) 305 path := ctx.AbsPath("testconfig.yaml") 306 file, err := os.Create(path) 307 c.Assert(err, jc.ErrorIsNil) 308 _, err = file.Write(expected) 309 c.Assert(err, jc.ErrorIsNil) 310 file.Close() 311 com, err := initSetCommand("--config", "testconfig.yaml", "service") 312 c.Assert(err, jc.ErrorIsNil) 313 c.Assert(com.SettingsYAML.Path, gc.Equals, "testconfig.yaml") 314 actual, err := com.SettingsYAML.Read(ctx) 315 c.Assert(err, jc.ErrorIsNil) 316 c.Assert(actual, gc.DeepEquals, expected) 317 318 // --config path, but no service 319 com, err = initSetCommand("--config", "testconfig") 320 c.Assert(err, gc.ErrorMatches, "no service name specified") 321 322 // --config and options specified 323 com, err = initSetCommand("service", "--config", "testconfig", "bees=") 324 c.Assert(err, gc.ErrorMatches, "cannot specify --config when using key=value arguments") 325 } 326 327 func initUnsetCommand(args ...string) (*UnsetCommand, error) { 328 com := &UnsetCommand{} 329 return com, coretesting.InitCommand(com, args) 330 } 331 332 func (*CmdSuite) TestUnsetCommandInit(c *gc.C) { 333 // missing args 334 _, err := initUnsetCommand() 335 c.Assert(err, gc.ErrorMatches, "no service name specified") 336 } 337 338 func initRemoveUnitCommand(args ...string) (*RemoveUnitCommand, error) { 339 com := &RemoveUnitCommand{} 340 return com, coretesting.InitCommand(com, args) 341 } 342 343 func (*CmdSuite) TestRemoveUnitCommandInit(c *gc.C) { 344 // missing args 345 _, err := initRemoveUnitCommand() 346 c.Assert(err, gc.ErrorMatches, "no units specified") 347 // not a unit 348 _, err = initRemoveUnitCommand("seven/nine") 349 c.Assert(err, gc.ErrorMatches, `invalid unit name "seven/nine"`) 350 }