func (s *BootstrapSuite) TestAutoSync(c *gc.C) { // Prepare a mock storage for testing and store the // dummy tools in there. restore := createToolsStore(c) defer restore() // Change the tools location to be the test location and also // the version and ensure their later restoring. origVersion := version.Current version.Current.Number = version.MustParse("1.2.3") defer func() { version.Current = origVersion }() // Create home with dummy provider and remove all // of its tools. env, fake := makeEmptyFakeHome(c) defer fake.Restore() // Bootstrap the environment now detects the missing // tools and automatically synchronizes them from the // storage above. ctx := coretesting.Context(c) code := cmd.Main(&BootstrapCommand{}, ctx, nil) c.Check(code, gc.Equals, 0) // Now check the available tools which are the 1.0.0 tools. checkTools(c, env, v100All) }
func (s *RelationListSuite) TestRelationList(c *C) { for i, t := range relationListTests { c.Logf("test %d: %s", i, t.summary) hctx := s.GetHookContext(c, t.relid, "") setMembers(hctx.rels[0], t.members0) setMembers(hctx.rels[1], t.members1) com, err := jujuc.NewCommand(hctx, "relation-list") c.Assert(err, IsNil) ctx := testing.Context(c) code := cmd.Main(com, ctx, t.args) c.Logf(bufferString(ctx.Stderr)) c.Assert(code, Equals, t.code) if code == 0 { c.Assert(bufferString(ctx.Stderr), Equals, "") expect := t.out if expect != "" { expect = expect + "\n" } c.Assert(bufferString(ctx.Stdout), Equals, expect) } else { c.Assert(bufferString(ctx.Stdout), Equals, "") expect := fmt.Sprintf(`(.|\n)*error: %s\n`, t.out) c.Assert(bufferString(ctx.Stderr), Matches, expect) } } }
func (s *RelationSetSuite) TestRun(c *C) { hctx := s.GetHookContext(c, 0, "") for i, t := range relationSetRunTests { c.Logf("test %d", i) pristine := Settings{"pristine": "untouched"} hctx.rels[0].units["u/0"] = pristine basic := Settings{"base": "value"} hctx.rels[1].units["u/0"] = basic // Run the command. com, err := jujuc.NewCommand(hctx, "relation-set") c.Assert(err, IsNil) rset := com.(*jujuc.RelationSetCommand) rset.RelationId = 1 rset.Settings = t.change ctx := testing.Context(c) err = com.Run(ctx) c.Assert(err, IsNil) // Check changes. c.Assert(hctx.rels[0].units["u/0"], DeepEquals, pristine) c.Assert(hctx.rels[1].units["u/0"], DeepEquals, t.expect) } }
func (s *CmdSuite) TestMainSuccess(c *C) { ctx := testing.Context(c) result := cmd.Main(&TestCommand{Name: "verb"}, ctx, []string{"--option", "success!"}) c.Assert(result, Equals, 0) c.Assert(bufferString(ctx.Stdout), Equals, "success!\n") c.Assert(bufferString(ctx.Stderr), Equals, "") }
func (s *RelationListSuite) TestRelationListHelp(c *C) { template := ` usage: relation-list [options] purpose: list relation units options: --format (= smart) specify output format (json|smart|yaml) -o, --output (= "") specify an output file -r (= %s) specify a relation by id %s`[1:] for relid, t := range map[int]struct { usage, doc string }{ -1: {"", "\n-r must be specified when not in a relation hook\n"}, 0: {"peer0:0", ""}, } { c.Logf("test relid %d", relid) hctx := s.GetHookContext(c, relid, "") com, err := jujuc.NewCommand(hctx, "relation-list") c.Assert(err, IsNil) ctx := testing.Context(c) code := cmd.Main(com, ctx, []string{"--help"}) c.Assert(code, Equals, 0) expect := fmt.Sprintf(template, t.usage, t.doc) c.Assert(bufferString(ctx.Stdout), Equals, expect) c.Assert(bufferString(ctx.Stderr), Equals, "") } }
func (s *SSHSuite) TestSSHCommand(c *C) { m := s.makeMachines(3, c) ch := coretesting.Charms.Dir("dummy") curl := charm.MustParseURL( fmt.Sprintf("local:series/%s-%d", ch.Meta().Name, ch.Revision()), ) bundleURL, err := url.Parse("http://bundles.testing.invalid/dummy-1") c.Assert(err, IsNil) dummy, err := s.State.AddCharm(ch, curl, bundleURL, "dummy-1-sha256") c.Assert(err, IsNil) srv, err := s.State.AddService("mysql", dummy) c.Assert(err, IsNil) s.addUnit(srv, m[0], c) srv, err = s.State.AddService("mongodb", dummy) c.Assert(err, IsNil) s.addUnit(srv, m[1], c) s.addUnit(srv, m[2], c) for _, t := range sshTests { c.Logf("testing juju ssh %s", t.args) ctx := coretesting.Context(c) code := cmd.Main(&SSHCommand{}, ctx, t.args) c.Check(code, Equals, 0) c.Check(ctx.Stderr.(*bytes.Buffer).String(), Equals, "") c.Check(ctx.Stdout.(*bytes.Buffer).String(), Equals, t.result) } }
func (s *CmdSuite) TestMainRunSilentError(c *C) { ctx := testing.Context(c) result := cmd.Main(&TestCommand{Name: "verb"}, ctx, []string{"--option", "silent-error"}) c.Assert(result, Equals, 1) c.Assert(bufferString(ctx.Stdout), Equals, "") c.Assert(bufferString(ctx.Stderr), Equals, "") }
func (s *ConfigSuite) TestGetConfig(c *C) { sch := s.AddTestingCharm(c, "dummy") svc, err := s.State.AddService("dummy-service", sch) c.Assert(err, IsNil) err = svc.UpdateConfigSettings(charm.Settings{"title": "Nearly There"}) c.Assert(err, IsNil) for _, t := range getTests { ctx := coretesting.Context(c) code := cmd.Main(&GetCommand{}, ctx, []string{t.service}) c.Check(code, Equals, 0) c.Assert(ctx.Stderr.(*bytes.Buffer).String(), Equals, "") // round trip via goyaml to avoid being sucked into a quagmire of // map[interface{}]interface{} vs map[string]interface{}. This is // also required if we add json support to this command. buf, err := goyaml.Marshal(t.expected) c.Assert(err, IsNil) expected := make(map[string]interface{}) err = goyaml.Unmarshal(buf, &expected) c.Assert(err, IsNil) actual := make(map[string]interface{}) err = goyaml.Unmarshal(ctx.Stdout.(*bytes.Buffer).Bytes(), &actual) c.Assert(err, IsNil) c.Assert(actual, DeepEquals, expected) } }
func runStatus(c *C, args ...string) (code int, stdout, stderr []byte) { ctx := coretesting.Context(c) code = cmd.Main(&StatusCommand{}, ctx, args) stdout = ctx.Stdout.(*bytes.Buffer).Bytes() stderr = ctx.Stderr.(*bytes.Buffer).Bytes() return }
func (s *RelationIdsSuite) TestHelp(c *C) { template := ` usage: %s purpose: list all relation ids with the given relation name options: --format (= smart) specify output format (json|smart|yaml) -o, --output (= "") specify an output file %s`[1:] for relid, t := range map[int]struct { usage, doc string }{ -1: {"relation-ids [options] <name>", ""}, 0: {"relation-ids [options] [<name>]", "\nCurrent default relation name is \"x\".\n"}, 3: {"relation-ids [options] [<name>]", "\nCurrent default relation name is \"y\".\n"}, } { c.Logf("relid %d", relid) hctx := s.GetHookContext(c, relid, "") com, err := jujuc.NewCommand(hctx, "relation-ids") c.Assert(err, IsNil) ctx := testing.Context(c) code := cmd.Main(com, ctx, []string{"--help"}) c.Assert(code, Equals, 0) expect := fmt.Sprintf(template, t.usage, t.doc) c.Assert(bufferString(ctx.Stdout), Equals, expect) c.Assert(bufferString(ctx.Stderr), Equals, "") } }
func (*CmdSuite) TestDeployCommandInit(c *C) { defer os.Setenv("JUJU_REPOSITORY", os.Getenv("JUJU_REPOSITORY")) os.Setenv("JUJU_REPOSITORY", "/path/to/repo") for _, t := range deployTests { initExpectations(t.com) com, err := initDeployCommand(t.args...) c.Assert(err, IsNil) c.Assert(com, DeepEquals, t.com) } // test relative --config path ctx := coretesting.Context(c) expected := []byte("test: data") path := ctx.AbsPath("testconfig.yaml") file, err := os.Create(path) c.Assert(err, IsNil) _, err = file.Write(expected) c.Assert(err, IsNil) file.Close() com, err := initDeployCommand("--config", "testconfig.yaml", "charm-name") c.Assert(err, IsNil) actual, err := com.Config.Read(ctx) c.Assert(err, IsNil) c.Assert(expected, DeepEquals, actual) // missing args _, err = initDeployCommand() c.Assert(err, ErrorMatches, "no charm specified") // environment tested elsewhere }
func (*CmdSuite) TestSetCommandInit(c *C) { // missing args _, err := initSetCommand() c.Assert(err, ErrorMatches, "no service name specified") // missing service name _, err = initSetCommand("name=cow") c.Assert(err, ErrorMatches, "no service name specified") // test --config path expected := []byte("this: is some test data") ctx := coretesting.Context(c) path := ctx.AbsPath("testconfig.yaml") file, err := os.Create(path) c.Assert(err, IsNil) _, err = file.Write(expected) c.Assert(err, IsNil) file.Close() com, err := initSetCommand("--config", "testconfig.yaml", "service") c.Assert(err, IsNil) c.Assert(com.SettingsYAML.Path, Equals, "testconfig.yaml") actual, err := com.SettingsYAML.Read(ctx) c.Assert(err, IsNil) c.Assert(actual, DeepEquals, expected) // --config path, but no service com, err = initSetCommand("--config", "testconfig") c.Assert(err, ErrorMatches, "no service name specified") // --config and options specified com, err = initSetCommand("service", "--config", "testconfig", "bees=") c.Assert(err, ErrorMatches, "cannot specify --config when using key=value arguments") }
func (s *SuperCommandSuite) TestDescription(c *C) { jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest", Purpose: "blow up the death star"}) jc.Register(&TestCommand{Name: "blah"}) ctx := testing.Context(c) code := cmd.Main(jc, ctx, []string{"blah", "--description"}) c.Assert(code, Equals, 0) c.Assert(bufferString(ctx.Stdout), Equals, "blow up the death star\n") }
func (suite *PluginSuite) TestRunPluginExisingDashE(c *C) { suite.makePlugin("foo", 0755) ctx := testing.Context(c) err := RunPlugin(ctx, "foo", []string{"-e plugins-rock some params"}) c.Assert(err, IsNil) c.Assert(testing.Stdout(ctx), Equals, "foo plugins-rock some params\n") c.Assert(testing.Stderr(ctx), Equals, "") }
func (suite *PluginSuite) TestRunPluginWithFailing(c *C) { suite.makeFailingPlugin("foo", 2) ctx := testing.Context(c) err := RunPlugin(ctx, "foo", []string{"some params"}) c.Assert(err, ErrorMatches, "exit status 2") c.Assert(testing.Stdout(ctx), Equals, "failing\n") c.Assert(testing.Stderr(ctx), Equals, "") }
func (s *LogSuite) TestStderr(c *C) { l := &cmd.Log{Verbose: true, Config: "<root>=INFO"} ctx := testing.Context(c) err := l.Start(ctx) c.Assert(err, IsNil) log.Infof("hello") c.Assert(bufferString(ctx.Stderr), Matches, `^.* INFO .* hello\n`) }
func runCmdLine(c *C, com cmd.Command, args ...string) (code int, stdout, stderr string) { ctx := coretesting.Context(c) code = cmd.Main(com, ctx, args) stdout = ctx.Stdout.(*bytes.Buffer).String() stderr = ctx.Stderr.(*bytes.Buffer).String() c.Logf("args: %#v\ncode: %d\nstdout: %q\nstderr: %q", args, code, stdout, stderr) return }
func (s *LogSuite) TestDebugSetsLogLevel(c *C) { l := &cmd.Log{Debug: true} ctx := testing.Context(c) err := l.Start(ctx) c.Assert(err, IsNil) c.Assert(loggo.GetLogger("").LogLevel(), Equals, loggo.DEBUG) }
func (s *LogSuite) TestVerboseSetsLogLevel(c *C) { l := &cmd.Log{Verbose: true} ctx := testing.Context(c) err := l.Start(ctx) c.Assert(err, IsNil) c.Assert(loggo.GetLogger("").LogLevel(), Equals, loggo.INFO) }
func (s *ImageMetadataSuite) TestImageMetadataBadArgs(c *gc.C) { defer testing.MakeEmptyFakeHome(c).Restore() for i, t := range errTests { c.Logf("test: %d", i) ctx := testing.Context(c) code := cmd.Main(&ImageMetadataCommand{}, ctx, t.args) c.Check(code, gc.Equals, 2) } }
func (s *SuperCommandSuite) TestHelpWithPrefix(c *C) { jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest", UsagePrefix: "juju"}) jc.Register(&TestCommand{Name: "blah"}) ctx := testing.Context(c) code := cmd.Main(jc, ctx, []string{"blah", "--help"}) c.Assert(code, Equals, 0) stripped := strings.Replace(bufferString(ctx.Stdout), "\n", "", -1) c.Assert(stripped, Matches, ".*usage: juju jujutest blah.*blah-doc.*") }
func (s *ConfigGetSuite) TestAllPlusKey(c *C) { hctx := s.GetHookContext(c, -1, "") com, err := jujuc.NewCommand(hctx, "config-get") c.Assert(err, IsNil) ctx := testing.Context(c) code := cmd.Main(com, ctx, []string{"--all", "--format", "json", "monsters"}) c.Assert(code, Equals, 2) c.Assert(bufferString(ctx.Stderr), Equals, "error: cannot use argument --all together with key \"monsters\"\n") }
func (s *CmdSuite) TestStdin(c *C) { const phrase = "Do you, Juju?" ctx := testing.Context(c) ctx.Stdin = bytes.NewBuffer([]byte(phrase)) result := cmd.Main(&TestCommand{Name: "verb"}, ctx, []string{"--option", "echo"}) c.Assert(result, Equals, 0) c.Assert(bufferString(ctx.Stdout), Equals, phrase) c.Assert(bufferString(ctx.Stderr), Equals, "") }
func (s *CmdSuite) TestMainHelp(c *C) { for _, arg := range []string{"-h", "--help"} { ctx := testing.Context(c) result := cmd.Main(&TestCommand{Name: "verb"}, ctx, []string{arg}) c.Assert(result, Equals, 0) c.Assert(bufferString(ctx.Stdout), Equals, fullHelp) c.Assert(bufferString(ctx.Stderr), Equals, "") } }
func (*BootstrapSuite) TestMissingEnvironment(c *C) { defer testing.MakeFakeHomeNoEnvironments(c, "empty").Restore() ctx := testing.Context(c) code := cmd.Main(&BootstrapCommand{}, ctx, nil) c.Check(code, Equals, 1) errStr := ctx.Stderr.(*bytes.Buffer).String() strippedErr := strings.Replace(errStr, "\n", "", -1) c.Assert(strippedErr, Matches, ".*No juju environment configuration file exists.*") }
func (suite *PluginSuite) TestRunPluginExisingJujuEnv(c *C) { suite.makePlugin("foo", 0755) os.Setenv("JUJU_ENV", "omg") ctx := testing.Context(c) err := RunPlugin(ctx, "foo", []string{"some params"}) c.Assert(err, IsNil) c.Assert(testing.Stdout(ctx), Equals, "foo omg some params\n") c.Assert(testing.Stderr(ctx), Equals, "") }
func (s *UnitGetSuite) TestOutputFormat(c *C) { for _, t := range unitGetTests { com := s.createCommand(c) ctx := testing.Context(c) code := cmd.Main(com, ctx, t.args) c.Assert(code, Equals, 0) c.Assert(bufferString(ctx.Stderr), Equals, "") c.Assert(bufferString(ctx.Stdout), Matches, t.out) } }
func (s *ImageMetadataSuite) TestImageMetadataFilesDefaultArch(c *gc.C) { defer testing.MakeEmptyFakeHome(c).Restore() ctx := testing.Context(c) code := cmd.Main( &ImageMetadataCommand{}, ctx, []string{"-i", "1234", "-r", "region", "-u", "endpoint", "-s", "raring"}) c.Assert(code, gc.Equals, 0) errOut := ctx.Stdout.(*bytes.Buffer).String() s.assertCommandOutput(c, errOut, "raring", "amd64", defaultIndexFileName, defaultImageFileName) }
func (s *SuperCommandSuite) TestLogging(c *C) { jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest", Log: &cmd.Log{}}) jc.Register(&TestCommand{Name: "blah"}) ctx := testing.Context(c) code := cmd.Main(jc, ctx, []string{"blah", "--option", "error", "--debug"}) c.Assert(code, Equals, 1) c.Assert(bufferString(ctx.Stderr), Matches, `^.* ERROR .* command failed: BAM! error: BAM! `) }
func (s *CmdSuite) TestMainInitError(c *C) { for _, t := range initErrorTests { ctx := testing.Context(c) result := cmd.Main(t.c, ctx, []string{"--unknown"}) c.Assert(result, Equals, 2) c.Assert(bufferString(ctx.Stdout), Equals, "") expected := "error: flag provided but not defined: --unknown\n" c.Assert(bufferString(ctx.Stderr), Equals, expected) } }