func (s *SSHSuite) testSSHCommandHostAddressRetry(c *gc.C, proxy bool) { m := s.makeMachines(1, c, false) ctx := coretesting.Context(c) var called int next := func() bool { called++ return called < 2 } attemptStarter := &callbackAttemptStarter{next: next} s.PatchValue(&sshHostFromTargetAttemptStrategy, attemptStarter) // Ensure that the ssh command waits for a public address, or the attempt // strategy's Done method returns false. args := []string{"--proxy=" + fmt.Sprint(proxy), "0"} code := cmd.Main(&SSHCommand{}, ctx, args) c.Check(code, gc.Equals, 1) c.Assert(called, gc.Equals, 2) called = 0 attemptStarter.next = func() bool { called++ if called > 1 { s.setAddresses(m[0], c) } return true } code = cmd.Main(&SSHCommand{}, ctx, args) c.Check(code, gc.Equals, 0) c.Assert(called, gc.Equals, 2) }
func (s *RelationListSuite) TestRelationListHelp(c *gc.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, gc.IsNil) ctx := testing.Context(c) code := cmd.Main(com, ctx, []string{"--help"}) c.Assert(code, gc.Equals, 0) expect := fmt.Sprintf(template, t.usage, t.doc) c.Assert(bufferString(ctx.Stdout), gc.Equals, expect) c.Assert(bufferString(ctx.Stderr), gc.Equals, "") } }
// Main registers subcommands for the juju-metadata executable, and hands over control // to the cmd package. This function is not redundant with main, because it // provides an entry point for testing with arbitrary command line arguments. func Main(args []string) { ctx, err := cmd.DefaultContext() if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(2) } if err := juju.InitJujuHome(); err != nil { fmt.Fprintf(os.Stderr, "error: %s\n", err) os.Exit(2) } metadatacmd := cmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "metadata", UsagePrefix: "juju", Doc: metadataDoc, Purpose: "tools for generating and validating image and tools metadata", Log: &cmd.Log{}}) metadatacmd.Register(envcmd.Wrap(&ValidateImageMetadataCommand{})) metadatacmd.Register(envcmd.Wrap(&ImageMetadataCommand{})) metadatacmd.Register(envcmd.Wrap(&ToolsMetadataCommand{})) metadatacmd.Register(envcmd.Wrap(&ValidateToolsMetadataCommand{})) metadatacmd.Register(&SignMetadataCommand{}) os.Exit(cmd.Main(metadatacmd, ctx, args[1:])) }
func (s *SSHSuite) TestSSHCommand(c *gc.C) { m := s.makeMachines(3, c, true) ch := coretesting.Charms.Dir("dummy") curl := charm.MustParseURL( fmt.Sprintf("local:quantal/%s-%d", ch.Meta().Name, ch.Revision()), ) bundleURL, err := url.Parse("http://bundles.testing.invalid/dummy-1") c.Assert(err, gc.IsNil) dummy, err := s.State.AddCharm(ch, curl, bundleURL, "dummy-1-sha256") c.Assert(err, gc.IsNil) srv := s.AddTestingService(c, "mysql", dummy) s.addUnit(srv, m[0], c) srv = s.AddTestingService(c, "mongodb", dummy) s.addUnit(srv, m[1], c) s.addUnit(srv, m[2], c) for i, t := range sshTests { c.Logf("test %d: %s -> %s\n", i, t.about, t.args) ctx := coretesting.Context(c) jujucmd := cmd.NewSuperCommand(cmd.SuperCommandParams{}) jujucmd.Register(envcmd.Wrap(&SSHCommand{})) code := cmd.Main(jujucmd, ctx, t.args) c.Check(code, gc.Equals, 0) c.Check(ctx.Stderr.(*bytes.Buffer).String(), gc.Equals, "") c.Check(ctx.Stdout.(*bytes.Buffer).String(), gc.Equals, t.result) } }
func (s *RelationListSuite) TestRelationList(c *gc.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, gc.IsNil) ctx := testing.Context(c) code := cmd.Main(com, ctx, t.args) c.Logf(bufferString(ctx.Stderr)) c.Assert(code, gc.Equals, t.code) if code == 0 { c.Assert(bufferString(ctx.Stderr), gc.Equals, "") expect := t.out if expect != "" { expect = expect + "\n" } c.Assert(bufferString(ctx.Stdout), gc.Equals, expect) } else { c.Assert(bufferString(ctx.Stdout), gc.Equals, "") expect := fmt.Sprintf(`(.|\n)*error: %s\n`, t.out) c.Assert(bufferString(ctx.Stderr), gc.Matches, expect) } } }
func (s *SuperCommandSuite) TestDescription(c *gc.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, gc.Equals, 0) c.Assert(bufferString(ctx.Stdout), gc.Equals, "blow up the death star\n") }
// assertSetSuccess sets configuration options and checks the expected settings. func assertSetSuccess(c *gc.C, dir string, svc *state.Service, args []string, expect charm.Settings) { ctx := coretesting.ContextForDir(c, dir) code := cmd.Main(envcmd.Wrap(&SetCommand{}), ctx, append([]string{"dummy-service"}, args...)) c.Check(code, gc.Equals, 0) settings, err := svc.ConfigSettings() c.Assert(err, gc.IsNil) c.Assert(settings, gc.DeepEquals, expect) }
func (s *SuperCommandSuite) TestHelpWithPrefix(c *gc.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, gc.Equals, 0) stripped := strings.Replace(bufferString(ctx.Stdout), "\n", "", -1) c.Assert(stripped, gc.Matches, ".*usage: juju jujutest blah.*blah-doc.*") }
func (s *ConfigGetSuite) TestAllPlusKey(c *gc.C) { hctx := s.GetHookContext(c, -1, "") com, err := jujuc.NewCommand(hctx, "config-get") c.Assert(err, gc.IsNil) ctx := testing.Context(c) code := cmd.Main(com, ctx, []string{"--all", "--format", "json", "monsters"}) c.Assert(code, gc.Equals, 2) c.Assert(bufferString(ctx.Stderr), gc.Equals, "error: cannot use argument --all together with key \"monsters\"\n") }
func (s *UnitGetSuite) TestOutputFormat(c *gc.C) { for _, t := range unitGetTests { com := s.createCommand(c) ctx := testing.Context(c) code := cmd.Main(com, ctx, t.args) c.Assert(code, gc.Equals, 0) c.Assert(bufferString(ctx.Stderr), gc.Equals, "") c.Assert(bufferString(ctx.Stdout), gc.Matches, t.out) } }
func (s *ValidateImageMetadataSuite) TestEc2LocalMetadataNoMatch(c *gc.C) { s.setupEc2LocalMetadata(c, "us-east-1", "") ctx := coretesting.Context(c) code := cmd.Main( envcmd.Wrap(&ValidateImageMetadataCommand{}), ctx, []string{ "-p", "ec2", "-s", "raring", "-r", "us-west-1", "-u", "https://ec2.us-west-1.amazonaws.com", "-d", s.metadataDir}, ) c.Assert(code, gc.Equals, 1) code = cmd.Main( envcmd.Wrap(&ValidateImageMetadataCommand{}), ctx, []string{ "-p", "ec2", "-s", "precise", "-r", "region", "-u", "https://ec2.region.amazonaws.com", "-d", s.metadataDir}, ) c.Assert(code, gc.Equals, 1) errOut := ctx.Stderr.(*bytes.Buffer).String() strippedOut := strings.Replace(errOut, "\n", "", -1) c.Check(strippedOut, gc.Matches, `.*Resolve Metadata:.*`) }
func (s *SuperCommandSuite) TestMissingCallbackErrors(c *gc.C) { callback := func(ctx *cmd.Context, subcommand string, args []string) error { return fmt.Errorf("command not found %q", subcommand) } ctx := testing.Context(c) code := cmd.Main(NewSuperWithCallback(callback), ctx, []string{"foo"}) c.Assert(code, gc.Equals, 1) c.Assert(testing.Stdout(ctx), gc.Equals, "") c.Assert(testing.Stderr(ctx), gc.Equals, "ERROR command not found \"foo\"\n") }
func (s *UnitGetSuite) TestOutputPath(c *gc.C) { com := s.createCommand(c) ctx := testing.Context(c) code := cmd.Main(com, ctx, []string{"--output", "some-file", "private-address"}) c.Assert(code, gc.Equals, 0) c.Assert(bufferString(ctx.Stderr), gc.Equals, "") c.Assert(bufferString(ctx.Stdout), gc.Equals, "") content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "some-file")) c.Assert(err, gc.IsNil) c.Assert(string(content), gc.Equals, "192.168.0.99\n") }
func (s *SuperCommandSuite) TestVersionFlag(c *gc.C) { jc := cmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "jujutest", Purpose: "to be purposeful", Doc: "doc\nblah\ndoc", }) testVersionFlagCommand := &testVersionFlagCommand{} jc.Register(&cmd.VersionCommand{}) jc.Register(testVersionFlagCommand) var stdout, stderr bytes.Buffer ctx := &cmd.Context{ Stdout: &stdout, Stderr: &stderr, } // baseline: juju version code := cmd.Main(jc, ctx, []string{"version"}) c.Check(code, gc.Equals, 0) baselineStderr := stderr.String() baselineStdout := stdout.String() stderr.Reset() stdout.Reset() // juju --version output should match that of juju version. code = cmd.Main(jc, ctx, []string{"--version"}) c.Check(code, gc.Equals, 0) c.Assert(stderr.String(), gc.Equals, baselineStderr) c.Assert(stdout.String(), gc.Equals, baselineStdout) stderr.Reset() stdout.Reset() // juju test --version should update testVersionFlagCommand.version, // and there should be no output. The --version flag on the 'test' // subcommand has a different type to the "juju --version" flag. code = cmd.Main(jc, ctx, []string{"test", "--version=abc.123"}) c.Check(code, gc.Equals, 0) c.Assert(stderr.String(), gc.Equals, "") c.Assert(stdout.String(), gc.Equals, "") c.Assert(testVersionFlagCommand.version, gc.Equals, "abc.123") }
func (s *RelationGetSuite) TestOutputPath(c *gc.C) { hctx := s.GetHookContext(c, 1, "m/0") com, err := jujuc.NewCommand(hctx, "relation-get") c.Assert(err, gc.IsNil) ctx := testing.Context(c) code := cmd.Main(com, ctx, []string{"--output", "some-file", "pew"}) c.Assert(code, gc.Equals, 0) c.Assert(bufferString(ctx.Stderr), gc.Equals, "") c.Assert(bufferString(ctx.Stdout), gc.Equals, "") content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "some-file")) c.Assert(err, gc.IsNil) c.Assert(string(content), gc.Equals, "pew\npew\n\n") }
func (s *SSHSuite) TestSSHCommandEnvironProxySSH(c *gc.C) { s.makeMachines(1, c, true) // Setting proxy-ssh=false in the environment overrides --proxy. err := s.State.UpdateEnvironConfig(map[string]interface{}{"proxy-ssh": false}, nil, nil) c.Assert(err, gc.IsNil) ctx := coretesting.Context(c) jujucmd := cmd.NewSuperCommand(cmd.SuperCommandParams{}) jujucmd.Register(&SSHCommand{}) code := cmd.Main(jujucmd, ctx, []string{"ssh", "0"}) c.Check(code, gc.Equals, 0) c.Check(ctx.Stderr.(*bytes.Buffer).String(), gc.Equals, "") c.Check(ctx.Stdout.(*bytes.Buffer).String(), gc.Equals, sshArgsNoProxy+"[email protected]\n") }
func (s *PortsSuite) TestOpenCloseDeprecation(c *gc.C) { hctx := s.GetHookContext(c, -1, "") for _, t := range portsFormatDeprectaionTests { name := t.cmd[0] com, err := jujuc.NewCommand(hctx, name) c.Assert(err, gc.IsNil) ctx := testing.Context(c) code := cmd.Main(com, ctx, t.cmd[1:]) c.Assert(code, gc.Equals, 0) c.Assert(testing.Stdout(ctx), gc.Equals, "") c.Assert(testing.Stderr(ctx), gc.Equals, "--format flag deprecated for command \""+name+"\"") } }
func (s *PortsSuite) TestOpenClose(c *gc.C) { hctx := s.GetHookContext(c, -1, "") for _, t := range portsTests { com, err := jujuc.NewCommand(hctx, t.cmd[0]) c.Assert(err, gc.IsNil) ctx := testing.Context(c) code := cmd.Main(com, ctx, t.cmd[1:]) c.Assert(code, gc.Equals, 0) c.Assert(bufferString(ctx.Stdout), gc.Equals, "") c.Assert(bufferString(ctx.Stderr), gc.Equals, "") c.Assert(hctx.ports, gc.DeepEquals, t.expect) } }
func (s *SuperCommandSuite) TestMissingCallbackContextWiredIn(c *gc.C) { callback := func(ctx *cmd.Context, subcommand string, args []string) error { fmt.Fprintf(ctx.Stdout, "this is std out") fmt.Fprintf(ctx.Stderr, "this is std err") return nil } ctx := testing.Context(c) code := cmd.Main(NewSuperWithCallback(callback), ctx, []string{"foo", "bar", "baz", "--debug"}) c.Assert(code, gc.Equals, 0) c.Assert(testing.Stdout(ctx), gc.Equals, "this is std out") c.Assert(testing.Stderr(ctx), gc.Equals, "this is std err") }
func (s *ValidateImageMetadataSuite) TestOpenstackLocalMetadataNoMatch(c *gc.C) { s.makeLocalMetadata(c, "1234", "region-2", "raring", "some-auth-url", "") ctx := coretesting.Context(c) code := cmd.Main( envcmd.Wrap(&ValidateImageMetadataCommand{}), ctx, []string{ "-p", "openstack", "-s", "precise", "-r", "region-2", "-u", "some-auth-url", "-d", s.metadataDir}, ) c.Assert(code, gc.Equals, 1) errOut := ctx.Stderr.(*bytes.Buffer).String() strippedOut := strings.Replace(errOut, "\n", "", -1) c.Check(strippedOut, gc.Matches, `.*Resolve Metadata:.*`) code = cmd.Main( envcmd.Wrap(&ValidateImageMetadataCommand{}), ctx, []string{ "-p", "openstack", "-s", "raring", "-r", "region-3", "-u", "some-auth-url", "-d", s.metadataDir}, ) c.Assert(code, gc.Equals, 1) errOut = ctx.Stderr.(*bytes.Buffer).String() strippedOut = strings.Replace(errOut, "\n", "", -1) c.Check(strippedOut, gc.Matches, `.*Resolve Metadata:.*`) }
func (s *ConfigGetSuite) TestOutputFormatKey(c *gc.C) { for i, t := range configGetKeyTests { c.Logf("test %d: %#v", i, t.args) hctx := s.GetHookContext(c, -1, "") com, err := jujuc.NewCommand(hctx, "config-get") c.Assert(err, gc.IsNil) ctx := testing.Context(c) code := cmd.Main(com, ctx, t.args) c.Assert(code, gc.Equals, 0) c.Assert(bufferString(ctx.Stderr), gc.Equals, "") c.Assert(bufferString(ctx.Stdout), gc.Matches, t.out) } }
func (s *ValidateImageMetadataSuite) assertEc2LocalMetadataUsingEnvironment(c *gc.C, stream string) { s.setupEc2LocalMetadata(c, "us-east-1", stream) ctx := coretesting.Context(c) code := cmd.Main( envcmd.Wrap(&ValidateImageMetadataCommand{}), ctx, []string{"-e", "ec2", "-d", s.metadataDir, "-m", stream}, ) c.Assert(code, gc.Equals, 0) errOut := ctx.Stdout.(*bytes.Buffer).String() strippedOut := strings.Replace(errOut, "\n", "", -1) c.Check( strippedOut, gc.Matches, `ImageIds:.*"1234".*Region:.*us-east-1.*Resolve Metadata:.*source: local metadata directory.*`) }
// Main registers subcommands for the jujud executable, and hands over control // to the cmd package. func jujuDMain(args []string, ctx *cmd.Context) (code int, err error) { jujud := cmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "jujud", Doc: jujudDoc, Log: &cmd.Log{Factory: &writerFactory{}}, }) jujud.Register(&BootstrapCommand{}) jujud.Register(&MachineAgent{}) jujud.Register(&UnitAgent{}) jujud.Register(&cmd.VersionCommand{}) code = cmd.Main(jujud, ctx, args[1:]) return code, nil }
func (s *ValidateImageMetadataSuite) TestOpenstackLocalMetadataWithManualParams(c *gc.C) { s.makeLocalMetadata(c, "1234", "region-2", "raring", "some-auth-url", "") ctx := coretesting.Context(c) code := cmd.Main( envcmd.Wrap(&ValidateImageMetadataCommand{}), ctx, []string{ "-p", "openstack", "-s", "raring", "-r", "region-2", "-u", "some-auth-url", "-d", s.metadataDir}, ) c.Assert(code, gc.Equals, 0) errOut := ctx.Stdout.(*bytes.Buffer).String() strippedOut := strings.Replace(errOut, "\n", "", -1) c.Check( strippedOut, gc.Matches, `ImageIds:.*"1234".*Region:.*region-2.*Resolve Metadata:.*source: local metadata directory.*`) }
func (s *ValidateImageMetadataSuite) TestEc2LocalMetadataWithManualParams(c *gc.C) { s.setupEc2LocalMetadata(c, "us-west-1", "") ctx := coretesting.Context(c) code := cmd.Main( envcmd.Wrap(&ValidateImageMetadataCommand{}), ctx, []string{ "-p", "ec2", "-s", "precise", "-r", "us-west-1", "-u", "https://ec2.us-west-1.amazonaws.com", "-d", s.metadataDir}, ) c.Assert(code, gc.Equals, 0) errOut := ctx.Stdout.(*bytes.Buffer).String() strippedOut := strings.Replace(errOut, "\n", "", -1) c.Check( strippedOut, gc.Matches, `ImageIds:.*"1234".*Region:.*us-west-1.*Resolve Metadata:.*source: local metadata directory.*`) }
func (s *ValidateImageMetadataSuite) TestEc2LocalMetadataUsingIncompleteEnvironment(c *gc.C) { s.PatchEnvironment("AWS_ACCESS_KEY_ID", "") s.PatchEnvironment("AWS_SECRET_ACCESS_KEY", "") s.PatchEnvironment("EC2_ACCESS_KEY", "") s.PatchEnvironment("EC2_SECRET_KEY", "") s.setupEc2LocalMetadata(c, "us-east-1", "") ctx := coretesting.Context(c) code := cmd.Main( envcmd.Wrap(&ValidateImageMetadataCommand{}), ctx, []string{"-e", "ec2", "-d", s.metadataDir}, ) c.Assert(code, gc.Equals, 1) errOut := ctx.Stderr.(*bytes.Buffer).String() strippedOut := strings.Replace(errOut, "\n", "", -1) c.Check(strippedOut, gc.Matches, `error: environment has no access-key or secret-key`) }
func (s *SignMetadataSuite) TestSignMetadata(c *gc.C) { topLevel := c.MkDir() keyfile := filepath.Join(topLevel, "privatekey.asc") err := ioutil.WriteFile(keyfile, []byte(sstesting.SignedMetadataPrivateKey), 0644) c.Assert(err, gc.IsNil) setupJsonFiles(c, topLevel) ctx := coretesting.Context(c) code := cmd.Main( &SignMetadataCommand{}, ctx, []string{"-d", topLevel, "-k", keyfile, "-p", sstesting.PrivateKeyPassphrase}) c.Assert(code, gc.Equals, 0) output := ctx.Stdout.(*bytes.Buffer).String() c.Assert(output, gc.Matches, expectedLoggingOutput) assertSignedFiles(c, topLevel) }
func (s *OwnerGetSuite) TestHelp(c *gc.C) { com := s.createCommand(c) ctx := testing.Context(c) code := cmd.Main(com, ctx, []string{"--help"}) c.Assert(code, gc.Equals, 0) c.Assert(bufferString(ctx.Stdout), gc.Equals, `usage: owner-get [options] <setting> purpose: print information about the owner of the service. The only valid value for <setting> is currently tag options: --format (= smart) specify output format (json|smart|yaml) -o, --output (= "") specify an output file `) c.Assert(bufferString(ctx.Stderr), gc.Equals, "") }
func main() { ctx, err := cmd.DefaultContext() if err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) os.Exit(2) } admcmd := cmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "charm-admin", Log: &cmd.Log{}, }) admcmd.Register(&DeleteCharmCommand{}) os.Exit(cmd.Main(admcmd, ctx, os.Args[1:])) }
func (s *UnitGetSuite) TestHelp(c *gc.C) { com := s.createCommand(c) ctx := testing.Context(c) code := cmd.Main(com, ctx, []string{"--help"}) c.Assert(code, gc.Equals, 0) c.Assert(bufferString(ctx.Stdout), gc.Equals, `usage: unit-get [options] <setting> purpose: print public-address or private-address options: --format (= smart) specify output format (json|smart|yaml) -o, --output (= "") specify an output file `) c.Assert(bufferString(ctx.Stderr), gc.Equals, "") }