Golang InitCommand示例

说明

golang initcommand示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: Golang

命名空间/包名称: github.com/juju/juju/testing

示例#1
文件: list_test.go 项目: bac/juju

func (s *listCommandSuite) TestInit(c *gc.C) {
	cmd := block.NewListCommand()
	err := testing.InitCommand(cmd, nil)
	c.Check(err, jc.ErrorIsNil)

	err = testing.InitCommand(cmd, []string{"anything"})
	c.Check(err.Error(), gc.Equals, `unrecognized args: ["anything"]`)
}

示例#2
文件: unset_test.go 项目: snailwalker/juju

func (s *UnsetSuite) TestInit(c *gc.C) {
	unsetCmd := environment.NewUnsetCommand(s.fake)
	// Only empty is a problem.
	err := testing.InitCommand(unsetCmd, []string{})
	c.Assert(err, gc.ErrorMatches, "no keys specified")
	// Everything else is fine.
	err = testing.InitCommand(unsetCmd, []string{"something", "weird"})
	c.Assert(err, jc.ErrorIsNil)
}

示例#3
文件: unregister_test.go 项目: bac/juju

func (s *UnregisterSuite) TestInit(c *gc.C) {
	unregisterCommand := controller.NewUnregisterCommand(s.store)

	err := testing.InitCommand(unregisterCommand, []string{})
	c.Assert(err, gc.ErrorMatches, "controller name must be specified")

	err = testing.InitCommand(unregisterCommand, []string{"foo", "bar"})
	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["bar"\]`)
}

示例#4
文件: get_test.go 项目: Pankov404/juju

func (s *GetSuite) TestInit(c *gc.C) {
	// zero or one args is fine.
	err := testing.InitCommand(&environment.GetCommand{}, nil)
	c.Check(err, jc.ErrorIsNil)
	err = testing.InitCommand(&environment.GetCommand{}, []string{"one"})
	c.Check(err, jc.ErrorIsNil)
	// More than one is not allowed.
	err = testing.InitCommand(&environment.GetCommand{}, []string{"one", "two"})
	c.Check(err, gc.ErrorMatches, `unrecognized args: \["two"\]`)
}

示例#5
文件: getconfig_test.go 项目: bac/juju

func (s *GetConfigSuite) TestInit(c *gc.C) {
	// zero or one args is fine.
	err := testing.InitCommand(controller.NewGetConfigCommandForTest(&fakeControllerAPI{}, s.store), nil)
	c.Check(err, jc.ErrorIsNil)
	err = testing.InitCommand(controller.NewGetConfigCommandForTest(&fakeControllerAPI{}, s.store), []string{"one"})
	c.Check(err, jc.ErrorIsNil)
	// More than one is not allowed.
	err = testing.InitCommand(controller.NewGetConfigCommandForTest(&fakeControllerAPI{}, s.store), []string{"one", "two"})
	c.Check(err, gc.ErrorMatches, `unrecognized args: \["two"\]`)
}

示例#6
文件: grantrevoke_test.go 项目: bac/juju

// TestInitRevokeAddModel checks that both the documented 'add-model' access and
// the backwards-compatible 'addmodel' work to revoke the AddModel permission.
func (s *grantSuite) TestInitRevokeAddModel(c *gc.C) {
	wrappedCmd, revokeCmd := model.NewRevokeCommandForTest(s.fake, s.store)
	// The documented case, add-model.
	err := testing.InitCommand(wrappedCmd, []string{"bob", "add-model"})
	c.Check(err, jc.ErrorIsNil)

	// The backwards-compatible case, addmodel.
	err = testing.InitCommand(wrappedCmd, []string{"bob", "addmodel"})
	c.Check(err, jc.ErrorIsNil)
	c.Assert(revokeCmd.Access, gc.Equals, "add-model")
}

示例#7

func (s *RegisterSuite) TestInit(c *gc.C) {
	registerCommand := controller.NewRegisterCommandForTest(nil, nil, nil)

	err := testing.InitCommand(registerCommand, []string{})
	c.Assert(err, gc.ErrorMatches, "registration data missing")

	err = testing.InitCommand(registerCommand, []string{"foo"})
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(registerCommand.EncodedData, gc.Equals, "foo")

	err = testing.InitCommand(registerCommand, []string{"foo", "bar"})
	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["bar"\]`)
}

示例#8
文件: login_test.go 项目: andrewdmcleod/juju

func (s *LoginSuite) TestInit(c *gc.C) {
	loginCommand := system.NewLoginCommand(nil, nil)

	err := testing.InitCommand(loginCommand, []string{})
	c.Assert(err, gc.ErrorMatches, "no name specified")

	err = testing.InitCommand(loginCommand, []string{"foo"})
	c.Assert(err, jc.ErrorIsNil)
	c.Assert(loginCommand.Name, gc.Equals, "foo")

	err = testing.InitCommand(loginCommand, []string{"foo", "bar"})
	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["bar"\]`)
}

示例#9
文件: destroyenvironment_test.go 项目: jiasir/juju

func (*destroyEnvSuite) TestDestroyEnvironmentCommandConfirmationFlag(c *gc.C) {
	com := new(DestroyEnvironmentCommand)
	c.Check(coretesting.InitCommand(com, []string{"dummyenv"}), gc.IsNil)
	c.Check(com.assumeYes, gc.Equals, false)

	com = new(DestroyEnvironmentCommand)
	c.Check(coretesting.InitCommand(com, []string{"dummyenv", "-y"}), gc.IsNil)
	c.Check(com.assumeYes, gc.Equals, true)

	com = new(DestroyEnvironmentCommand)
	c.Check(coretesting.InitCommand(com, []string{"dummyenv", "--yes"}), gc.IsNil)
	c.Check(com.assumeYes, gc.Equals, true)
}

示例#10
文件: destroyenvironment_test.go 项目: snailwalker/juju

func (*destroyEnvSuite) TestDestroyEnvironmentCommandConfirmationFlag(c *gc.C) {
	wrappedCom, com := NewDestroyEnvironmentCommand()
	c.Check(coretesting.InitCommand(wrappedCom, []string{"dummyenv"}), gc.IsNil)
	c.Check(com.assumeYes, jc.IsFalse)

	wrappedCom, com = NewDestroyEnvironmentCommand()
	c.Check(coretesting.InitCommand(wrappedCom, []string{"dummyenv", "-y"}), gc.IsNil)
	c.Check(com.assumeYes, jc.IsTrue)

	wrappedCom, com = NewDestroyEnvironmentCommand()
	c.Check(coretesting.InitCommand(wrappedCom, []string{"dummyenv", "--yes"}), gc.IsNil)
	c.Check(com.assumeYes, jc.IsTrue)
}

示例#11
文件: unshare_test.go 项目: exekias/juju

func (s *unshareSuite) TestInit(c *gc.C) {
	wrappedCommand, unshareCmd := model.NewUnshareCommandForTest(s.fake)
	err := testing.InitCommand(wrappedCommand, []string{})
	c.Assert(err, gc.ErrorMatches, "no users specified")

	err = testing.InitCommand(wrappedCommand, []string{"not valid/0"})
	c.Assert(err, gc.ErrorMatches, `invalid username: "not valid/0"`)

	err = testing.InitCommand(wrappedCommand, []string{"[email protected]", "sam"})
	c.Assert(err, jc.ErrorIsNil)

	c.Assert(unshareCmd.Users[0], gc.Equals, names.NewUserTag("[email protected]"))
	c.Assert(unshareCmd.Users[1], gc.Equals, names.NewUserTag("sam"))
}

示例#12
文件: grantrevoke_test.go 项目: bac/juju

func (s *grantSuite) TestInit(c *gc.C) {
	wrappedCmd, grantCmd := model.NewGrantCommandForTest(s.fake, s.store)
	err := testing.InitCommand(wrappedCmd, []string{})
	c.Assert(err, gc.ErrorMatches, "no user specified")

	err = testing.InitCommand(wrappedCmd, []string{"bob", "read", "model1", "model2"})
	c.Assert(err, jc.ErrorIsNil)

	c.Assert(grantCmd.User, gc.Equals, "bob")
	c.Assert(grantCmd.ModelNames, jc.DeepEquals, []string{"model1", "model2"})

	err = testing.InitCommand(wrappedCmd, []string{})
	c.Assert(err, gc.ErrorMatches, `no user specified`)
}

示例#13
文件: unshare_test.go 项目: Pankov404/juju

func (s *unshareSuite) TestInit(c *gc.C) {
	unshareCmd := &environment.UnshareCommand{}
	err := testing.InitCommand(unshareCmd, []string{})
	c.Assert(err, gc.ErrorMatches, "no users specified")

	err = testing.InitCommand(unshareCmd, []string{"not valid/0"})
	c.Assert(err, gc.ErrorMatches, `invalid username: "not valid/0"`)

	err = testing.InitCommand(unshareCmd, []string{"[email protected]", "sam"})
	c.Assert(err, jc.ErrorIsNil)

	c.Assert(unshareCmd.Users[0], gc.Equals, names.NewUserTag("[email protected]"))
	c.Assert(unshareCmd.Users[1], gc.Equals, names.NewUserTag("sam"))
}

示例#14
文件: agent_test.go 项目: rogpeppe/juju

// 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 *gc.C, create acCreator, args []string) cmd.Command {
	com, conf := create()
	err := coretesting.InitCommand(com, args)
	c.Assert(conf.dataDir, gc.Equals, "/var/lib/juju")
	badArgs := append(args, "--data-dir", "")
	com, conf = create()
	err = coretesting.InitCommand(com, badArgs)
	c.Assert(err, gc.ErrorMatches, "--data-dir option must be set")

	args = append(args, "--data-dir", "jd")
	com, conf = create()
	c.Assert(coretesting.InitCommand(com, args), gc.IsNil)
	c.Assert(conf.dataDir, gc.Equals, "jd")
	return com
}

示例#15
文件: cmd_juju_controller_test.go 项目: imoapps/juju

func (s *cmdControllerSuite) run(c *gc.C, args ...string) *cmd.Context {
	context := testing.Context(c)
	command := commands.NewJujuCommand(context)
	c.Assert(testing.InitCommand(command, args), jc.ErrorIsNil)
	c.Assert(command.Run(context), jc.ErrorIsNil)
	return context
}

示例#16
文件: login_test.go 项目: kat-co/juju

func (s *LoginCommandSuite) TestInit(c *gc.C) {
	for i, test := range []struct {
		args        []string
		user        string
		errorString string
	}{
		{
		// no args is fine
		}, {
			args: []string{"foobar"},
			user: "foobar",
		}, {
			args:        []string{"--foobar"},
			errorString: "flag provided but not defined: --foobar",
		}, {
			args:        []string{"foobar", "extra"},
			errorString: `unrecognized args: \["extra"\]`,
		},
	} {
		c.Logf("test %d", i)
		wrappedCommand, command := user.NewLoginCommandForTest(nil, s.store)
		err := coretesting.InitCommand(wrappedCommand, test.args)
		if test.errorString == "" {
			c.Check(command.User, gc.Equals, test.user)
		} else {
			c.Check(err, gc.ErrorMatches, test.errorString)
		}
	}
}

示例#17
文件: login_test.go 项目: kat-co/juju

func (s *LoginCommandSuite) run(c *gc.C, stdin string, args ...string) (*cmd.Context, juju.NewAPIConnectionParams, error) {
	var argsOut juju.NewAPIConnectionParams
	cmd, _ := user.NewLoginCommandForTest(func(args juju.NewAPIConnectionParams) (user.LoginAPI, user.ConnectionAPI, error) {
		argsOut = args
		// The account details are modified in place, so take a copy.
		accountDetails := *argsOut.AccountDetails
		argsOut.AccountDetails = &accountDetails
		if s.loginErr != nil {
			err := s.loginErr
			s.loginErr = nil
			return nil, nil, err
		}
		return s.mockAPI, s.mockAPI, nil
	}, s.store)
	ctx := coretesting.Context(c)
	if stdin == "" {
		stdin = "sekrit\n"
	}
	ctx.Stdin = strings.NewReader(stdin)
	err := coretesting.InitCommand(cmd, args)
	if err != nil {
		return nil, argsOut, err
	}
	err = cmd.Run(ctx)
	return ctx, argsOut, err
}

示例#18
文件: disenable_test.go 项目: snailwalker/juju

func (s *DisableUserSuite) testInit(c *gc.C, wrappedCommand cmd.Command, command *user.DisenableUserBase) {
	for i, test := range []struct {
		args     []string
		errMatch string
		user     string
		enable   bool
	}{
		{
			errMatch: "no username supplied",
		}, {
			args:     []string{"username", "password"},
			errMatch: `unrecognized args: \["password"\]`,
		}, {
			args: []string{"username"},
			user: "username",
		},
	} {
		c.Logf("test %d, args %v", i, test.args)
		err := testing.InitCommand(wrappedCommand, test.args)
		if test.errMatch == "" {
			c.Assert(err, jc.ErrorIsNil)
			c.Assert(command.User, gc.Equals, test.user)
		} else {
			c.Assert(err, gc.ErrorMatches, test.errMatch)
		}
	}
}

示例#19

func (s *addCredentialSuite) assertAddFileCredential(c *gc.C, input, fileKey string) {
	dir := c.MkDir()
	filename := filepath.Join(dir, "jsonfile")
	err := ioutil.WriteFile(filename, []byte{}, 0600)
	c.Assert(err, jc.ErrorIsNil)

	stdin := strings.NewReader(fmt.Sprintf(input, filename))
	addCmd := cloud.NewAddCredentialCommandForTest(s.store, s.cloudByNameFunc)
	err = testing.InitCommand(addCmd, []string{"somecloud"})
	c.Assert(err, jc.ErrorIsNil)
	ctx := testing.ContextForDir(c, dir)
	ctx.Stdin = stdin
	err = addCmd.Run(ctx)
	c.Assert(err, jc.ErrorIsNil)

	c.Assert(s.store.Credentials, jc.DeepEquals, map[string]jujucloud.CloudCredential{
		"somecloud": {
			AuthCredentials: map[string]jujucloud.Credential{
				"fred": jujucloud.NewCredential(s.authTypes[0], map[string]string{
					fileKey: filename,
				}),
			},
		},
	})
}

示例#20
文件: constraints_test.go 项目: Pankov404/juju

func (s *ServiceConstraintsCommandsSuite) TestSetInit(c *gc.C) {
	for _, test := range []struct {
		args []string
		err  string
	}{
		{
			args: []string{"--service", "mysql", "mem=4G"},
			err:  `flag provided but not defined: --service`,
		}, {
			args: []string{"-s", "mysql", "mem=4G"},
			err:  `flag provided but not defined: -s`,
		}, {
			args: []string{},
			err:  `no service name specified`,
		}, {
			args: []string{"mysql", "="},
			err:  `malformed constraint "="`,
		}, {
			args: []string{"cpu-power=250"},
			err:  `invalid service name "cpu-power=250"`,
		}, {
			args: []string{"mysql", "cpu-power=250"},
		},
	} {
		err := testing.InitCommand(&service.ServiceSetConstraintsCommand{}, test.args)
		if test.err == "" {
			c.Check(err, jc.ErrorIsNil)
		} else {
			c.Check(err, gc.ErrorMatches, test.err)
		}
	}
}

示例#21
文件: unit_test.go 项目: imoapps/juju

func (s *UnitSuite) TestParseUnknown(c *gc.C) {
	err := coretesting.InitCommand(NewUnitAgent(nil, nil), []string{
		"--unit-name", "wordpress/1",
		"thundering typhoons",
	})
	c.Check(err, gc.ErrorMatches, `unrecognized args: \["thundering typhoons"\]`)
}

示例#22
文件: constraints_test.go 项目: exekias/juju

func (s *ServiceConstraintsCommandsSuite) TestGetInit(c *gc.C) {
	for _, test := range []struct {
		args []string
		err  string
	}{
		{
			args: []string{"-s", "mysql"},
			err:  `flag provided but not defined: -s`,
		}, {
			args: []string{"--service", "mysql"},
			err:  `flag provided but not defined: --service`,
		}, {
			args: []string{},
			err:  `no service name specified`,
		}, {
			args: []string{"mysql-0"},
			err:  `invalid service name "mysql-0"`,
		}, {
			args: []string{"mysql"},
		},
	} {
		err := testing.InitCommand(service.NewServiceGetConstraintsCommand(), test.args)
		if test.err == "" {
			c.Check(err, jc.ErrorIsNil)
		} else {
			c.Check(err, gc.ErrorMatches, test.err)
		}
	}
}

示例#23
文件: remove_test.go 项目: bac/juju

func (s *RemoveUserCommandSuite) TestInit(c *gc.C) {
	table := []struct {
		args        []string
		confirm     bool
		errorString string
	}{{
		confirm:     false,
		errorString: "no username supplied",
	}, {
		args:        []string{"--yes"},
		confirm:     true,
		errorString: "no username supplied",
	}, {
		args:    []string{"--yes", "jjam"},
		confirm: true,
	}}
	for _, test := range table {
		wrappedCommand, command := user.NewRemoveCommandForTest(s.mockAPI, s.store)
		err := testing.InitCommand(wrappedCommand, test.args)
		c.Check(command.ConfirmDelete, jc.DeepEquals, test.confirm)
		if test.errorString == "" {
			c.Check(err, jc.ErrorIsNil)
		} else {
			c.Check(err, gc.ErrorMatches, test.errorString)
		}
	}
}

示例#24
文件: fetch_test.go 项目: AlexisBruemmer/juju

func (s *FetchSuite) TestInit(c *gc.C) {
	tests := []struct {
		should      string
		args        []string
		expectError string
	}{{
		should:      "fail with missing arg",
		args:        []string{},
		expectError: "no action ID specified",
	}, {
		should:      "fail with multiple args",
		args:        []string{"12345", "54321"},
		expectError: `unrecognized args: \["54321"\]`,
	}}

	for i, t := range tests {
		for _, modelFlag := range s.modelFlags {
			c.Logf("test %d: it should %s: juju actions fetch %s", i,
				t.should, strings.Join(t.args, " "))
			cmd, _ := action.NewFetchCommand(s.store)
			args := append([]string{modelFlag, "admin"}, t.args...)
			err := testing.InitCommand(cmd, args)
			if t.expectError != "" {
				c.Check(err, gc.ErrorMatches, t.expectError)
			}
		}
	}
}

示例#25
文件: credentials_test.go 项目: claudiu-coblis/juju

func (s *CredentialsCommandSuite) TestInit(c *gc.C) {
	for i, test := range []struct {
		args        []string
		outPath     string
		errorString string
	}{
		{
		// no args is fine
		}, {
			args:    []string{"--output=foo.bar"},
			outPath: "foo.bar",
		}, {
			args:    []string{"-o", "foo.bar"},
			outPath: "foo.bar",
		}, {
			args:        []string{"foobar"},
			errorString: `unrecognized args: \["foobar"\]`,
		},
	} {
		c.Logf("test %d", i)
		command := &user.CredentialsCommand{}
		err := testing.InitCommand(command, test.args)
		if test.errorString == "" {
			c.Check(command.OutPath, gc.Equals, test.outPath)
		} else {
			c.Check(err, gc.ErrorMatches, test.errorString)
		}
	}
}

示例#26
文件: addunit_test.go 项目: zhouqt/juju

func (s *AddUnitSuite) TestInitErrors(c *gc.C) {
	for i, t := range initAddUnitErrorTests {
		c.Logf("test %d", i)
		err := testing.InitCommand(envcmd.Wrap(&AddUnitCommand{}), t.args)
		c.Check(err, gc.ErrorMatches, t.err)
	}
}

示例#27
文件: deploy_test.go 项目: snailwalker/juju

func (s *DeploySuite) TestInitErrors(c *gc.C) {
	for i, t := range initErrorTests {
		c.Logf("test %d", i)
		err := coretesting.InitCommand(envcmd.Wrap(&deployCommand{}), t.args)
		c.Assert(err, gc.ErrorMatches, t.err)
	}
}

示例#28
文件: enablecommand_test.go 项目: bac/juju

func (s *enableCommandSuite) TestInit(c *gc.C) {
	for _, test := range []struct {
		args []string
		err  string
	}{
		{
			err: "missing command set (all, destroy-model, remove-object)",
		}, {
			args: []string{"other"},
			err:  "bad command set, valid options: all, destroy-model, remove-object",
		}, {
			args: []string{"all"},
		}, {
			args: []string{"destroy-model"},
		}, {
			args: []string{"remove-object"},
		}, {
			args: []string{"all", "extra"},
			err:  `unrecognized args: ["extra"]`,
		},
	} {
		cmd := block.NewEnableCommand()
		err := testing.InitCommand(cmd, test.args)
		if test.err == "" {
			c.Check(err, jc.ErrorIsNil)
		} else {
			c.Check(err.Error(), gc.Equals, test.err)
		}
	}
}

示例#29
文件: validateimagemetadata_test.go 项目: kat-co/juju

func (s *ValidateImageMetadataSuite) TestInitErrors(c *gc.C) {
	for i, t := range validateInitImageErrorTests {
		c.Logf("test %d", i)
		err := coretesting.InitCommand(newValidateImageMetadataCommand(), t.args)
		c.Check(err, gc.ErrorMatches, t.err)
	}
}

示例#30
文件: kill_test.go 项目: bac/juju

func (s *KillSuite) TestKillDurationFlags(c *gc.C) {
	for i, test := range []struct {
		args     []string
		expected time.Duration
		err      string
	}{
		{
			expected: 5 * time.Minute,
		}, {
			args:     []string{"-t", "2m"},
			expected: 2 * time.Minute,
		}, {
			args:     []string{"--timeout", "2m"},
			expected: 2 * time.Minute,
		}, {
			args:     []string{"-t", "0"},
			expected: 0,
		},
	} {
		c.Logf("duration test %d", i)
		wrapped, inner := s.newKillCommandBoth()
		args := append([]string{"test1"}, test.args...)
		err := coretesting.InitCommand(wrapped, args)
		if test.err == "" {
			c.Check(err, jc.ErrorIsNil)
			c.Check(controller.KillTimeout(c, inner), gc.Equals, test.expected)
		} else {
			c.Check(err, gc.ErrorMatches, test.err)
		}
	}
}

展开阅读全文