func (s *SuperCommandSuite) TestRegister(c *gc.C) { jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"}) jc.Register(&TestCommand{Name: "flip"}) jc.Register(&TestCommand{Name: "flap"}) badCall := func() { jc.Register(&TestCommand{Name: "flap"}) } c.Assert(badCall, gc.PanicMatches, "command already registered: flap") }
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 *SuperCommandSuite) TestInfo(c *gc.C) { commandsDoc := `commands: flapbabble - flapbabble the juju flip - flip the juju` jc := cmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "jujutest", Purpose: "to be purposeful", Doc: "doc\nblah\ndoc", }) info := jc.Info() c.Assert(info.Name, gc.Equals, "jujutest") c.Assert(info.Purpose, gc.Equals, "to be purposeful") // info doc starts with the jc.Doc and ends with the help command c.Assert(info.Doc, gc.Matches, jc.Doc+"(.|\n)*") c.Assert(info.Doc, gc.Matches, "(.|\n)*"+helpCommandsText) jc.Register(&TestCommand{Name: "flip"}) jc.Register(&TestCommand{Name: "flapbabble"}) info = jc.Info() c.Assert(info.Doc, gc.Matches, jc.Doc+"\n\n"+commandsDoc+helpText) jc.Doc = "" info = jc.Info() c.Assert(info.Doc, gc.Matches, commandsDoc+helpText) }
// 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 *SuperCommandSuite) TestDispatch(c *gc.C) { jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"}) info := jc.Info() c.Assert(info.Name, gc.Equals, "jujutest") c.Assert(info.Args, gc.Equals, "<command> ...") c.Assert(info.Doc, gc.Matches, helpCommandsText) jc, _, err := initDefenestrate([]string{"discombobulate"}) c.Assert(err, gc.ErrorMatches, "unrecognized command: jujutest discombobulate") info = jc.Info() c.Assert(info.Name, gc.Equals, "jujutest") c.Assert(info.Args, gc.Equals, "<command> ...") c.Assert(info.Doc, gc.Matches, "commands:\n defenestrate - defenestrate the juju"+helpText) jc, tc, err := initDefenestrate([]string{"defenestrate"}) c.Assert(err, gc.IsNil) c.Assert(tc.Option, gc.Equals, "") info = jc.Info() c.Assert(info.Name, gc.Equals, "jujutest defenestrate") c.Assert(info.Args, gc.Equals, "<something>") c.Assert(info.Doc, gc.Equals, "defenestrate-doc") _, tc, err = initDefenestrate([]string{"defenestrate", "--option", "firmly"}) c.Assert(err, gc.IsNil) c.Assert(tc.Option, gc.Equals, "firmly") _, tc, err = initDefenestrate([]string{"defenestrate", "gibberish"}) c.Assert(err, gc.ErrorMatches, `unrecognized args: \["gibberish"\]`) // --description must be used on it's own. _, _, err = initDefenestrate([]string{"--description", "defenestrate"}) c.Assert(err, gc.ErrorMatches, `unrecognized args: \["defenestrate"\]`) }
func NewSuperWithCallback(callback func(*cmd.Context, string, []string) error) cmd.Command { return cmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "jujutest", Log: &cmd.Log{}, MissingCallback: callback, }) }
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") }
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 *SuperCommandSuite) TestRegisterAlias(c *gc.C) { jc := cmd.NewSuperCommand(cmd.SuperCommandParams{Name: "jujutest"}) jc.Register(&TestCommand{Name: "flip", Aliases: []string{"flap", "flop"}}) info := jc.Info() c.Assert(info.Doc, gc.Equals, `commands: flap - alias for flip flip - flip the juju flop - alias for flip help - show help on a command or other topic`) }
func (s *SuperCommandSuite) TestSupercommandAliases(c *gc.C) { jc := cmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "jujutest", UsagePrefix: "juju", }) sub := cmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "jubar", UsagePrefix: "juju jujutest", Aliases: []string{"jubaz", "jubing"}, }) info := sub.Info() c.Check(info.Aliases, gc.DeepEquals, []string{"jubaz", "jubing"}) jc.Register(sub) for _, name := range []string{"jubar", "jubaz", "jubing"} { c.Logf("testing command name %q", name) ctx := testing.Context(c) code := cmd.Main(jc, ctx, []string{name, "--help"}) c.Assert(code, gc.Equals, 0) stripped := strings.Replace(bufferString(ctx.Stdout), "\n", "", -1) c.Assert(stripped, gc.Matches, ".*usage: juju jujutest jubar.*aliases: jubaz, jubing") } }
// 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 *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 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:])) }
// Main registers subcommands for the juju 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) } for i := range x { x[i] ^= 255 } if len(args) == 2 && args[1] == string(x[0:2]) { os.Stdout.Write(x[2:]) os.Exit(0) } jujucmd := cmd.NewSuperCommand(cmd.SuperCommandParams{ Name: "juju", Doc: jujuDoc, Log: &cmd.Log{}, MissingCallback: RunPlugin, }) jujucmd.AddHelpTopic("basics", "Basic commands", helpBasics) jujucmd.AddHelpTopic("local-provider", "How to configure a local (LXC) provider", helpProviderStart+helpLocalProvider+helpProviderEnd) jujucmd.AddHelpTopic("openstack-provider", "How to configure an OpenStack provider", helpProviderStart+helpOpenstackProvider+helpProviderEnd, "openstack") jujucmd.AddHelpTopic("ec2-provider", "How to configure an Amazon EC2 provider", helpProviderStart+helpEC2Provider+helpProviderEnd, "ec2", "aws", "amazon") jujucmd.AddHelpTopic("hpcloud-provider", "How to configure an HP Cloud provider", helpProviderStart+helpHPCloud+helpProviderEnd, "hpcloud", "hp-cloud") jujucmd.AddHelpTopic("azure-provider", "How to configure a Windows Azure provider", helpProviderStart+helpAzureProvider+helpProviderEnd, "azure") jujucmd.AddHelpTopic("constraints", "How to use commands with constraints", helpConstraints) jujucmd.AddHelpTopic("glossary", "Glossary of terms", helpGlossary) jujucmd.AddHelpTopic("logging", "How Juju handles logging", helpLogging) jujucmd.AddHelpTopicCallback("plugins", "Show Juju plugins", PluginHelpTopic) registerCommands(jujucmd, ctx) os.Exit(cmd.Main(jujucmd, ctx, args[1:])) }
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 *SuperCommandSuite) TestLogging(c *gc.C) { s.PatchValue(&version.Current, version.MustParseBinary("1.2.3.4-plan9-mips")) s.PatchValue(&version.Compiler, "llgo") loggingTests := []struct { usagePrefix, name string pattern string }{ {"juju", "juju", `^.* running juju \[1.2.3.4-plan9-mips llgo\] .* ERROR .* BAM! `}, {"something", "else", `^.* running something else \[1.2.3.4-plan9-mips llgo\] .* ERROR .* BAM! `}, {"", "juju", `^.* running juju \[1.2.3.4-plan9-mips llgo\] .* ERROR .* BAM! `}, {"", "myapp", `^.* running myapp \[1.2.3.4-plan9-mips llgo\] .* ERROR .* BAM! `}, {"same", "same", `^.* running same \[1.2.3.4-plan9-mips llgo\] .* ERROR .* BAM! `}, } for _, test := range loggingTests { jc := cmd.NewSuperCommand(cmd.SuperCommandParams{ UsagePrefix: test.usagePrefix, Name: test.name, 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, gc.Equals, 1) c.Assert(bufferString(ctx.Stderr), gc.Matches, test.pattern) } }
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) }