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) }
// 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 }
func (s *DeploySuite) TestInitErrors(c *gc.C) { for i, t := range initErrorTests { c.Logf("test %d", i) err := coretesting.InitCommand(&DeployCommand{}, t.args) c.Assert(err, gc.ErrorMatches, t.err) } }
func (s *DeleteCharmSuite) TestInit(c *gc.C) { config := &DeleteCharmCommand{} err := testing.InitCommand(config, []string{"--config", "/etc/charmd.conf", "--url", "cs:go"}) c.Assert(err, gc.IsNil) c.Assert(config.ConfigPath, gc.Equals, "/etc/charmd.conf") c.Assert(config.Url, gc.Equals, "cs:go") }
func (s *ValidateToolsMetadataSuite) TestInitErrors(c *gc.C) { for i, t := range validateInitToolsErrorTests { c.Logf("test %d", i) err := coretesting.InitCommand(&ValidateToolsMetadataCommand{}, t.args) c.Check(err, gc.ErrorMatches, t.err) } }
func (s *AddUnitSuite) TestInitErrors(c *gc.C) { for i, t := range initAddUnitErrorTests { c.Logf("test %d", i) err := testing.InitCommand(&AddUnitCommand{}, t.args) c.Check(err, gc.ErrorMatches, t.err) } }
// testInit checks that a command initialises correctly // with the given set of arguments. func testInit(c *gc.C, com cmd.Command, args []string, errPat string) { err := coretesting.InitCommand(com, args) if errPat != "" { c.Assert(err, gc.ErrorMatches, errPat) } else { c.Assert(err, gc.IsNil) } }
func (s *PortsSuite) TestBadArgs(c *gc.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, gc.IsNil) err = testing.InitCommand(com, t.args) c.Assert(err, gc.ErrorMatches, t.err) } } }
func (*RunTestSuite) TestWrongArgs(c *gc.C) { for i, test := range []struct { title string args []string errMatch string unit string commands string avoidContext bool }{{ title: "no args", errMatch: "missing unit-name", }, { title: "one arg", args: []string{"foo"}, errMatch: "missing commands", }, { title: "more than two arg", args: []string{"foo", "bar", "baz"}, errMatch: `unrecognized args: \["baz"\]`, }, { title: "unit and command assignment", args: []string{"unit-name", "command"}, unit: "unit-name", commands: "command", }, { title: "unit id converted to tag", args: []string{"foo/1", "command"}, unit: "unit-foo-1", commands: "command", }, { title: "execute not in a context", args: []string{"--no-context", "command"}, commands: "command", avoidContext: true, }, } { c.Logf("\n%d: %s", i, test.title) runCommand := &RunCommand{} err := testing.InitCommand(runCommand, test.args) if test.errMatch == "" { c.Assert(err, gc.IsNil) c.Assert(runCommand.unit, gc.Equals, test.unit) c.Assert(runCommand.commands, gc.Equals, test.commands) c.Assert(runCommand.noContext, gc.Equals, test.avoidContext) } else { c.Assert(err, gc.ErrorMatches, test.errMatch) } } }
func (s *ConfigSuite) TestReadConfig(c *gc.C) { confDir := c.MkDir() f, err := os.Create(path.Join(confDir, "charmd.conf")) c.Assert(err, gc.IsNil) cfgPath := f.Name() { defer f.Close() fmt.Fprint(f, testConfig) } config := &SomeConfigCommand{} args := []string{"--config", cfgPath} err = testing.InitCommand(config, args) c.Assert(err, gc.IsNil) _, err = testing.RunCommand(c, config, args) c.Assert(err, gc.IsNil) c.Assert(config.Config, gc.NotNil) c.Assert(config.Config.MongoURL, gc.Equals, "localhost:23456") }
func (s *RelationSetSuite) TestInit(c *gc.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, gc.IsNil) err = testing.InitCommand(com, t.args) if t.err == "" { c.Assert(err, gc.IsNil) rset := com.(*jujuc.RelationSetCommand) c.Assert(rset.RelationId, gc.Equals, t.relid) settings := t.settings if settings == nil { settings = map[string]string{} } c.Assert(rset.Settings, gc.DeepEquals, settings) } else { c.Assert(err, gc.ErrorMatches, t.err) } } }
func (s *BootstrapSuite) initBootstrapCommand(c *gc.C, jobs []params.MachineJob, args ...string) (machineConf agent.ConfigSetterWriter, cmd *BootstrapCommand, err error) { if len(jobs) == 0 { // Add default jobs. jobs = []params.MachineJob{ params.JobManageEnviron, params.JobHostUnits, } } // NOTE: the old test used an equivalent of the NewAgentConfig, but it // really should be using NewStateMachineConfig. agentParams := agent.AgentConfigParams{ LogDir: s.logDir, DataDir: s.dataDir, Jobs: jobs, Tag: "machine-0", UpgradedToVersion: version.Current.Number, Password: testPasswordHash(), Nonce: state.BootstrapNonce, StateAddresses: []string{testing.MgoServer.Addr()}, APIAddresses: []string{"0.1.2.3:1234"}, CACert: testing.CACert, Values: map[string]string{agent.Namespace: "foobar"}, } servingInfo := params.StateServingInfo{ Cert: "some cert", PrivateKey: "some key", APIPort: 3737, StatePort: testing.MgoServer.Port(), } machineConf, err = agent.NewStateMachineConfig(agentParams, servingInfo) c.Assert(err, gc.IsNil) err = machineConf.Write() c.Assert(err, gc.IsNil) cmd = &BootstrapCommand{} err = testing.InitCommand(cmd, append([]string{"--data-dir", s.dataDir}, args...)) return machineConf, cmd, err }
func runCommand(ctx *cmd.Context, com cmd.Command, args ...string) (opc chan dummy.Operation, errc chan error) { if ctx == nil { panic("ctx == nil") } 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(ctx) errc <- err }() return }
func (s *OwnerGetSuite) TestUnknownArg(c *gc.C) { com := s.createCommand(c) err := testing.InitCommand(com, []string{"tag", "blah"}) c.Assert(err, gc.ErrorMatches, `unrecognized args: \["blah"\]`) }
func (s *OwnerGetSuite) TestUnknownSetting(c *gc.C) { com := s.createCommand(c) err := testing.InitCommand(com, []string{"unknown-setting"}) c.Assert(err, gc.ErrorMatches, `unknown setting "unknown-setting"`) }
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) }
func (s *DebugLogSuite) TestArgParsing(c *gc.C) { for i, test := range []struct { args []string expected api.DebugLogParams errMatch string }{ { expected: api.DebugLogParams{ Backlog: 10, }, }, { args: []string{"-n0"}, }, { args: []string{"--lines=50"}, expected: api.DebugLogParams{ Backlog: 50, }, }, { args: []string{"-l", "foo"}, errMatch: `level value "foo" is not one of "TRACE", "DEBUG", "INFO", "WARNING", "ERROR"`, }, { args: []string{"--level=INFO"}, expected: api.DebugLogParams{ Backlog: 10, Level: loggo.INFO, }, }, { args: []string{"--include", "machine-1", "-i", "machine-2"}, expected: api.DebugLogParams{ IncludeEntity: []string{"machine-1", "machine-2"}, Backlog: 10, }, }, { args: []string{"--exclude", "machine-1", "-x", "machine-2"}, expected: api.DebugLogParams{ ExcludeEntity: []string{"machine-1", "machine-2"}, Backlog: 10, }, }, { args: []string{"--include-module", "juju.foo", "--include-module", "unit"}, expected: api.DebugLogParams{ IncludeModule: []string{"juju.foo", "unit"}, Backlog: 10, }, }, { args: []string{"--exclude-module", "juju.foo", "--exclude-module", "unit"}, expected: api.DebugLogParams{ ExcludeModule: []string{"juju.foo", "unit"}, Backlog: 10, }, }, { args: []string{"--replay"}, expected: api.DebugLogParams{ Backlog: 10, Replay: true, }, }, { args: []string{"--limit", "100"}, expected: api.DebugLogParams{ Backlog: 10, Limit: 100, }, }, } { c.Logf("test %v", i) command := &DebugLogCommand{} err := testing.InitCommand(command, test.args) if test.errMatch == "" { c.Check(err, gc.IsNil) c.Check(command.params, jc.DeepEquals, test.expected) } else { c.Check(err, gc.ErrorMatches, test.errMatch) } } }
func initExposeCommand(args ...string) (*ExposeCommand, error) { com := &ExposeCommand{} return com, coretesting.InitCommand(com, args) }
// 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 *UpgradeJujuSuite) TestUpgradeJuju(c *gc.C) { s.PatchValue(&sync.BuildToolsTarball, s.getMockBuildTools(c)) oldVersion := version.Current defer func() { version.Current = oldVersion }() 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, gc.ErrorMatches, test.expectInitErr) } else { c.Check(err, gc.IsNil) } continue } // Set up state and environ, and run the command. toolsDir := c.MkDir() updateAttrs := map[string]interface{}{ "agent-version": test.agentVersion, "tools-metadata-url": "file://" + toolsDir, } err := s.State.UpdateEnvironConfig(updateAttrs, nil, nil) c.Assert(err, gc.IsNil) versions := make([]version.Binary, len(test.tools)) for i, v := range test.tools { versions[i] = version.MustParseBinary(v) } if len(versions) > 0 { envtesting.MustUploadFakeToolsVersions(s.Conn.Environ.Storage(), versions...) stor, err := filestorage.NewFileStorageWriter(toolsDir) c.Assert(err, gc.IsNil) envtesting.MustUploadFakeToolsVersions(stor, versions...) } err = com.Run(coretesting.Context(c)) if test.expectErr != "" { c.Check(err, gc.ErrorMatches, test.expectErr) continue } else if !c.Check(err, gc.IsNil) { continue } // Check expected changes to environ/state. cfg, err := s.State.EnvironConfig() c.Check(err, gc.IsNil) agentVersion, ok := cfg.AgentVersion() c.Check(ok, gc.Equals, true) c.Check(agentVersion, gc.Equals, version.MustParse(test.expectVersion)) for _, uploaded := range test.expectUploaded { // Substitute latest LTS for placeholder in expected series for uploaded tools uploaded = strings.Replace(uploaded, "%LTS%", config.LatestLtsSeries(), 1) vers := version.MustParseBinary(uploaded) r, err := storage.Get(s.Conn.Environ.Storage(), envtools.StorageName(vers)) if !c.Check(err, gc.IsNil) { continue } data, err := ioutil.ReadAll(r) r.Close() c.Check(err, gc.IsNil) expectContent := version.Current expectContent.Number = agentVersion checkToolsContent(c, data, "jujud contents "+expectContent.String()) } } }
func initUnsetCommand(args ...string) (*UnsetCommand, error) { com := &UnsetCommand{} return com, coretesting.InitCommand(com, args) }
func initSCPCommand(args ...string) (*SCPCommand, error) { com := &SCPCommand{} return com, coretesting.InitCommand(com, args) }
func (s *UnitGetSuite) TestUnknownSetting(c *gc.C) { com := s.createCommand(c) err := testing.InitCommand(com, []string{"protected-address"}) c.Assert(err, gc.ErrorMatches, `unknown setting "protected-address"`) }
// initAgent initialises the given agent command with additional // arguments as provided. func (s *agentSuite) initAgent(c *gc.C, a cmd.Command, args ...string) { args = append([]string{"--data-dir", s.DataDir()}, args...) err := coretesting.InitCommand(a, args) c.Assert(err, gc.IsNil) }
func initDestroyUnitCommand(args ...string) (*DestroyUnitCommand, error) { com := &DestroyUnitCommand{} return com, coretesting.InitCommand(com, args) }
func initAddUnitCommand(args ...string) (*AddUnitCommand, error) { com := &AddUnitCommand{} return com, coretesting.InitCommand(com, args) }