func (*CmdSuite) TestDestroyEnvironmentCommandConfirmation(c *C) { com := new(DestroyEnvironmentCommand) c.Check(coretesting.InitCommand(com, nil), IsNil) c.Check(com.assumeYes, Equals, false) com = new(DestroyEnvironmentCommand) c.Check(coretesting.InitCommand(com, []string{"-y"}), IsNil) c.Check(com.assumeYes, Equals, true) com = new(DestroyEnvironmentCommand) c.Check(coretesting.InitCommand(com, []string{"--yes"}), IsNil) c.Check(com.assumeYes, Equals, true) var stdin, stdout bytes.Buffer ctx := cmd.DefaultContext() ctx.Stdout = &stdout ctx.Stdin = &stdin // Ensure confirmation is requested if "-y" is not specified. stdin.WriteString("n") opc, errc := runCommand(ctx, new(DestroyEnvironmentCommand)) c.Check(<-errc, ErrorMatches, "Environment destruction aborted") c.Check(<-opc, IsNil) c.Check(stdout.String(), Matches, "WARNING:.*peckham.*\\(type: dummy\\)(.|\n)*") // EOF on stdin: equivalent to answering no. stdin.Reset() stdout.Reset() opc, errc = runCommand(ctx, new(DestroyEnvironmentCommand)) c.Check(<-opc, IsNil) c.Check(<-errc, ErrorMatches, "Environment destruction aborted") // "--yes" passed: no confirmation request. stdin.Reset() stdout.Reset() opc, errc = runCommand(ctx, new(DestroyEnvironmentCommand), "--yes") c.Check(<-errc, IsNil) c.Check((<-opc).(dummy.OpDestroy).Env, Equals, "peckham") c.Check(stdout.String(), Equals, "") // Any of casing of "y" and "yes" will confirm. for _, answer := range []string{"y", "Y", "yes", "YES"} { stdin.Reset() stdout.Reset() stdin.WriteString(answer) opc, errc = runCommand(ctx, new(DestroyEnvironmentCommand)) c.Check(<-errc, IsNil) c.Check((<-opc).(dummy.OpDestroy).Env, Equals, "peckham") c.Check(stdout.String(), Matches, "WARNING:.*peckham.*\\(type: dummy\\)(.|\n)*") } }
// CheckAgentCommand is a utility function for verifying that common agent // options are handled by a Command; it returns an instance of that // command pre-parsed, with any mandatory flags added. func CheckAgentCommand(c *C, create acCreator, args []string) cmd.Command { com, conf := create() err := coretesting.InitCommand(com, args) c.Assert(conf.dataDir, Equals, "/var/lib/juju") badArgs := append(args, "--data-dir", "") com, conf = create() err = coretesting.InitCommand(com, badArgs) c.Assert(err, ErrorMatches, "--data-dir option must be set") args = append(args, "--data-dir", "jd") com, conf = create() c.Assert(coretesting.InitCommand(com, args), IsNil) c.Assert(conf.dataDir, Equals, "jd") return com }
func (s *ValidateMetadataSuite) TestInitErrors(c *gc.C) { for i, t := range validateInitErrorTests { c.Logf("test %d", i) err := coretesting.InitCommand(&ValidateImageMetadataCommand{}, t.args) c.Check(err, gc.ErrorMatches, t.err) } }
func (s *DeploySuite) TestInitErrors(c *C) { for i, t := range initErrorTests { c.Logf("test %d", i) err := coretesting.InitCommand(&DeployCommand{}, t.args) c.Assert(err, ErrorMatches, t.err) } }
func (s *AddUnitSuite) TestInitErrors(c *C) { for i, t := range initAddUnitErrorTests { c.Logf("test %d", i) err := testing.InitCommand(&AddUnitCommand{}, t.args) c.Check(err, ErrorMatches, t.err) } }
// testInit checks that a command initialises correctly // with the given set of arguments. func testInit(c *C, com cmd.Command, args []string, errPat string) { err := coretesting.InitCommand(com, args) if errPat != "" { c.Assert(err, ErrorMatches, errPat) } else { c.Assert(err, IsNil) } }
func (s *PortsSuite) TestBadArgs(c *C) { for _, name := range []string{"open-port", "close-port"} { for _, t := range badPortsTests { hctx := s.GetHookContext(c, -1, "") com, err := jujuc.NewCommand(hctx, name) c.Assert(err, IsNil) err = testing.InitCommand(com, t.args) c.Assert(err, ErrorMatches, t.err) } } }
func (s *RelationSetSuite) TestInit(c *C) { for i, t := range relationSetInitTests { c.Logf("test %d", i) hctx := s.GetHookContext(c, t.ctxrelid, "") com, err := jujuc.NewCommand(hctx, "relation-set") c.Assert(err, IsNil) err = testing.InitCommand(com, t.args) if t.err == "" { c.Assert(err, IsNil) rset := com.(*jujuc.RelationSetCommand) c.Assert(rset.RelationId, Equals, t.relid) c.Assert(rset.Settings, DeepEquals, t.settings) } else { c.Assert(err, ErrorMatches, t.err) } } }
func runCommand(com cmd.Command, args ...string) (opc chan dummy.Operation, errc chan error) { errc = make(chan error, 1) opc = make(chan dummy.Operation, 200) dummy.Listen(opc) go func() { // signal that we're done with this ops channel. defer dummy.Listen(nil) err := coretesting.InitCommand(com, args) if err != nil { errc <- err return } err = com.Run(cmd.DefaultContext()) errc <- err }() return }
func (s *BootstrapSuite) initBootstrapCommand(c *C, args ...string) (machineConf *agent.Conf, cmd *BootstrapCommand, err error) { ioutil.WriteFile(s.providerStateURLFile, []byte("test://localhost/provider-state\n"), 0600) bootConf := &agent.Conf{ DataDir: s.dataDir, OldPassword: testPasswordHash(), StateInfo: &state.Info{ Tag: "bootstrap", Addrs: []string{testing.MgoAddr}, CACert: []byte(testing.CACert), }, APIInfo: &api.Info{ Tag: "bootstrap", Addrs: []string{"0.1.2.3:1234"}, CACert: []byte(testing.CACert), }, } err = bootConf.Write() c.Assert(err, IsNil) machineConf = &agent.Conf{ DataDir: s.dataDir, OldPassword: testPasswordHash(), StateInfo: &state.Info{ Tag: "machine-0", Addrs: []string{testing.MgoAddr}, CACert: []byte(testing.CACert), }, APIInfo: &api.Info{ Tag: "machine-0", Addrs: []string{"0.1.2.3:1234"}, CACert: []byte(testing.CACert), }, } err = machineConf.Write() c.Assert(err, IsNil) cmd = &BootstrapCommand{} err = testing.InitCommand(cmd, append([]string{"--data-dir", s.dataDir}, args...)) return machineConf, cmd, err }
func initDestroyUnitCommand(args ...string) (*DestroyUnitCommand, error) { com := &DestroyUnitCommand{} return com, coretesting.InitCommand(com, args) }
func initSetCommand(args ...string) (*SetCommand, error) { com := &SetCommand{} return com, coretesting.InitCommand(com, args) }
func initUnexposeCommand(args ...string) (*UnexposeCommand, error) { com := &UnexposeCommand{} return com, coretesting.InitCommand(com, args) }
func (s *UnitGetSuite) TestUnknownArg(c *C) { com := s.createCommand(c) err := testing.InitCommand(com, []string{"private-address", "blah"}) c.Assert(err, ErrorMatches, `unrecognized args: \["blah"\]`) }
func initDefenestrate(args []string) (*cmd.SuperCommand, *TestCommand, error) { jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"}) tc := &TestCommand{Name: "defenestrate"} jc.Register(tc) return jc, tc, testing.InitCommand(jc, args) }
// initAgent initialises the given agent command with additional // arguments as provided. func (s *agentSuite) initAgent(c *C, a cmd.Command, args ...string) { args = append([]string{"--data-dir", s.DataDir()}, args...) err := coretesting.InitCommand(a, args) c.Assert(err, IsNil) }
func (s *UpgradeJujuSuite) TestUpgradeJuju(c *C) { oldVersion := version.Current uploadTools = mockUploadTools defer func() { version.Current = oldVersion uploadTools = tools.Upload }() for i, test := range upgradeJujuTests { c.Logf("\ntest %d: %s", i, test.about) s.Reset(c) // Set up apparent CLI version and initialize the command. version.Current = version.MustParseBinary(test.currentVersion) com := &UpgradeJujuCommand{} if err := coretesting.InitCommand(com, test.args); err != nil { if test.expectInitErr != "" { c.Check(err, ErrorMatches, test.expectInitErr) } else { c.Check(err, IsNil) } continue } // Set up state and environ, and run the command. cfg, err := s.State.EnvironConfig() c.Assert(err, IsNil) cfg, err = cfg.Apply(map[string]interface{}{ "agent-version": test.agentVersion, "development": test.development, }) c.Assert(err, IsNil) err = s.State.SetEnvironConfig(cfg) c.Assert(err, IsNil) for _, v := range test.private { vers := version.MustParseBinary(v) envtesting.MustUploadFakeToolsVersion(s.Conn.Environ.Storage(), vers) } for _, v := range test.public { vers := version.MustParseBinary(v) storage := s.Conn.Environ.PublicStorage().(environs.Storage) envtesting.MustUploadFakeToolsVersion(storage, vers) } err = com.Run(coretesting.Context(c)) if test.expectErr != "" { c.Check(err, ErrorMatches, test.expectErr) continue } else if !c.Check(err, IsNil) { continue } // Check expected changes to environ/state. cfg, err = s.State.EnvironConfig() c.Check(err, IsNil) agentVersion, ok := cfg.AgentVersion() c.Check(ok, Equals, true) c.Check(agentVersion, Equals, version.MustParse(test.expectVersion)) c.Check(cfg.Development(), Equals, test.development) for _, uploaded := range test.expectUploaded { vers := version.MustParseBinary(uploaded) r, err := s.Conn.Environ.Storage().Get(tools.StorageName(vers)) if !c.Check(err, IsNil) { continue } data, err := ioutil.ReadAll(r) r.Close() c.Check(err, IsNil) c.Check(string(data), Equals, uploaded) } } }
// ParseAgentCommand is a utility function that inserts the always-required args // before parsing an agent command and returning the result. func ParseAgentCommand(ac cmd.Command, args []string) error { common := []string{ "--data-dir", "jd", } return coretesting.InitCommand(ac, append(common, args...)) }
func (s *UnitGetSuite) TestUnknownSetting(c *C) { com := s.createCommand(c) err := testing.InitCommand(com, []string{"protected-address"}) c.Assert(err, ErrorMatches, `unknown setting "protected-address"`) }