func (s *collectMetricsSuite) TestCollectMetricsFailsOnNonLocalCharm(c *gc.C) { runClient := &testRunClient{} serviceClient := &testServiceClient{} serviceClient.charmURL = "cs:quantal/charm" s.PatchValue(metricsdebug.NewAPIConn, noConn) s.PatchValue(metricsdebug.NewRunClient, metricsdebug.NewRunClientFnc(runClient)) s.PatchValue(metricsdebug.NewServiceClient, metricsdebug.NewServiceClientFnc(serviceClient)) _, err := coretesting.RunCommand(c, metricsdebug.NewCollectMetricsCommand(), "foobar") c.Assert(err, gc.ErrorMatches, `"foobar" is not a local charm`) runClient.CheckCallNames(c, "Close") }
// registerCommands registers commands in the specified registry. func registerCommands(r commandRegistry, ctx *cmd.Context) { // Creation commands. r.Register(newBootstrapCommand()) r.Register(application.NewAddRelationCommand()) // Destruction commands. r.Register(application.NewRemoveRelationCommand()) r.Register(application.NewRemoveServiceCommand()) r.Register(application.NewRemoveUnitCommand()) // Reporting commands. r.Register(status.NewStatusCommand()) r.Register(newSwitchCommand()) r.Register(status.NewStatusHistoryCommand()) // Error resolution and debugging commands. r.Register(newRunCommand()) r.Register(newSCPCommand()) r.Register(newSSHCommand()) r.Register(newResolvedCommand()) r.Register(newDebugLogCommand()) r.Register(newDebugHooksCommand()) // Configuration commands. r.Register(model.NewModelGetConstraintsCommand()) r.Register(model.NewModelSetConstraintsCommand()) r.Register(newSyncToolsCommand()) r.Register(newUpgradeJujuCommand(nil)) r.Register(application.NewUpgradeCharmCommand()) // Charm tool commands. r.Register(newHelpToolCommand()) r.Register(charmcmd.NewSuperCommand()) // Manage backups. r.Register(backups.NewCreateCommand()) r.Register(backups.NewDownloadCommand()) r.Register(backups.NewShowCommand()) r.Register(backups.NewListCommand()) r.Register(backups.NewRemoveCommand()) r.Register(backups.NewRestoreCommand()) r.Register(backups.NewUploadCommand()) // Manage authorized ssh keys. r.Register(NewAddKeysCommand()) r.Register(NewRemoveKeysCommand()) r.Register(NewImportKeysCommand()) r.Register(NewListKeysCommand()) // Manage users and access r.Register(user.NewAddCommand()) r.Register(user.NewChangePasswordCommand()) r.Register(user.NewShowUserCommand()) r.Register(user.NewListCommand()) r.Register(user.NewEnableCommand()) r.Register(user.NewDisableCommand()) r.Register(user.NewLoginCommand()) r.Register(user.NewLogoutCommand()) r.Register(user.NewRemoveCommand()) r.Register(user.NewWhoAmICommand()) // Manage cached images r.Register(cachedimages.NewRemoveCommand()) r.Register(cachedimages.NewListCommand()) // Manage machines r.Register(machine.NewAddCommand()) r.Register(machine.NewRemoveCommand()) r.Register(machine.NewListMachinesCommand()) r.Register(machine.NewShowMachineCommand()) // Manage model r.Register(model.NewConfigCommand()) r.Register(model.NewDefaultsCommand()) r.Register(model.NewRetryProvisioningCommand()) r.Register(model.NewDestroyCommand()) r.Register(model.NewGrantCommand()) r.Register(model.NewRevokeCommand()) r.Register(model.NewShowCommand()) if featureflag.Enabled(feature.Migration) { r.Register(newMigrateCommand()) } if featureflag.Enabled(feature.DeveloperMode) { r.Register(model.NewDumpCommand()) r.Register(model.NewDumpDBCommand()) } // Manage and control actions r.Register(action.NewStatusCommand()) r.Register(action.NewRunCommand()) r.Register(action.NewShowOutputCommand()) r.Register(action.NewListCommand()) // Manage controller availability r.Register(newEnableHACommand()) // Manage and control services r.Register(application.NewAddUnitCommand()) r.Register(application.NewConfigCommand()) r.Register(application.NewDefaultDeployCommand()) r.Register(application.NewExposeCommand()) r.Register(application.NewUnexposeCommand()) r.Register(application.NewServiceGetConstraintsCommand()) r.Register(application.NewServiceSetConstraintsCommand()) // Operation protection commands r.Register(block.NewDisableCommand()) r.Register(block.NewListCommand()) r.Register(block.NewEnableCommand()) // Manage storage r.Register(storage.NewAddCommand()) r.Register(storage.NewListCommand()) r.Register(storage.NewPoolCreateCommand()) r.Register(storage.NewPoolListCommand()) r.Register(storage.NewShowCommand()) // Manage spaces r.Register(space.NewAddCommand()) r.Register(space.NewListCommand()) if featureflag.Enabled(feature.PostNetCLIMVP) { r.Register(space.NewRemoveCommand()) r.Register(space.NewUpdateCommand()) r.Register(space.NewRenameCommand()) } // Manage subnets r.Register(subnet.NewAddCommand()) r.Register(subnet.NewListCommand()) if featureflag.Enabled(feature.PostNetCLIMVP) { r.Register(subnet.NewCreateCommand()) r.Register(subnet.NewRemoveCommand()) } // Manage controllers r.Register(controller.NewAddModelCommand()) r.Register(controller.NewDestroyCommand()) r.Register(controller.NewListModelsCommand()) r.Register(controller.NewKillCommand()) r.Register(controller.NewListControllersCommand()) r.Register(controller.NewRegisterCommand()) r.Register(controller.NewUnregisterCommand(jujuclient.NewFileClientStore())) r.Register(controller.NewEnableDestroyControllerCommand()) r.Register(controller.NewShowControllerCommand()) r.Register(controller.NewGetConfigCommand()) // Debug Metrics r.Register(metricsdebug.New()) r.Register(metricsdebug.NewCollectMetricsCommand()) r.Register(setmeterstatus.New()) // Manage clouds and credentials r.Register(cloud.NewUpdateCloudsCommand()) r.Register(cloud.NewListCloudsCommand()) r.Register(cloud.NewListRegionsCommand()) r.Register(cloud.NewShowCloudCommand()) r.Register(cloud.NewAddCloudCommand()) r.Register(cloud.NewRemoveCloudCommand()) r.Register(cloud.NewListCredentialsCommand()) r.Register(cloud.NewDetectCredentialsCommand()) r.Register(cloud.NewSetDefaultRegionCommand()) r.Register(cloud.NewSetDefaultCredentialCommand()) r.Register(cloud.NewAddCredentialCommand()) r.Register(cloud.NewRemoveCredentialCommand()) r.Register(cloud.NewUpdateCredentialCommand()) // Juju GUI commands. r.Register(gui.NewGUICommand()) r.Register(gui.NewUpgradeGUICommand()) // Commands registered elsewhere. for _, newCommand := range registeredCommands { command := newCommand() r.Register(command) } for _, newCommand := range registeredEnvCommands { command := newCommand() r.Register(modelcmd.Wrap(command)) } rcmd.RegisterAll(r) }
func (s *collectMetricsSuite) TestCollectMetrics(c *gc.C) { runClient := &testRunClient{} cleanup := testing.PatchValue(metricsdebug.NewRunClient, metricsdebug.NewRunClientFnc(runClient)) defer cleanup() tests := []struct { about string args []string stdout string results [][]params.RunResult err string }{{ about: "missing args", err: "you need to specify a unit or service.", }, { about: "invalid service name", args: []string{"service_1-0"}, err: `"service_1-0" is not a valid unit or service`, }, { about: "all is well", args: []string{"uptime"}, results: [][]params.RunResult{ []params.RunResult{{ UnitId: "uptime/0", ExecResponse: exec.ExecResponse{ Stdout: []byte("ok"), }, }}, []params.RunResult{{ UnitId: "uptime/0", ExecResponse: exec.ExecResponse{ Stdout: []byte("ok"), }, }}, }, }, { about: "fail to collect metrics", args: []string{"wordpress"}, results: [][]params.RunResult{ []params.RunResult{{ UnitId: "wordpress/0", ExecResponse: exec.ExecResponse{ Stderr: []byte("nc: unix connect failed: No such file or directory"), }, }}, }, stdout: "failed to collect metrics for unit wordpress/0: not a metered charm\n", }, { about: "fail to send metrics", args: []string{"uptime"}, results: [][]params.RunResult{ []params.RunResult{{ UnitId: "uptime/0", ExecResponse: exec.ExecResponse{ Stdout: []byte("ok"), }, }}, []params.RunResult{{ UnitId: "uptime/0", ExecResponse: exec.ExecResponse{ Stderr: []byte("an embarrassing error"), }, }}, }, stdout: "failed to send metrics for unit uptime/0: an embarrassing error\n", }, } for i, test := range tests { c.Logf("running test %d: %v", i, test.about) runClient.reset() if test.results != nil { runClient.results = test.results } ctx, err := coretesting.RunCommand(c, metricsdebug.NewCollectMetricsCommand(), test.args...) if test.err != "" { c.Assert(err, gc.ErrorMatches, test.err) } else { c.Assert(err, jc.ErrorIsNil) c.Assert(coretesting.Stdout(ctx), gc.Matches, test.stdout) } } }
func (s *collectMetricsSuite) TestCollectMetrics(c *gc.C) { runClient := &testRunClient{} serviceClient := &testServiceClient{} serviceClient.charmURL = "local:quantal/charm" s.PatchValue(metricsdebug.NewAPIConn, noConn) s.PatchValue(metricsdebug.NewRunClient, metricsdebug.NewRunClientFnc(runClient)) s.PatchValue(metricsdebug.NewServiceClient, metricsdebug.NewServiceClientFnc(serviceClient)) actionTag1 := names.NewActionTag("01234567-89ab-cdef-0123-456789abcdef") actionTag2 := names.NewActionTag("11234567-89ab-cdef-0123-456789abcdef") tests := []struct { about string args []string stdout string results [][]params.ActionResult actionMap map[string]params.ActionResult err string }{{ about: "missing args", err: "you need to specify a unit or application.", }, { about: "invalid application name", args: []string{"application_1-0"}, err: `"application_1-0" is not a valid unit or application`, }, { about: "all is well", args: []string{"uptime"}, results: [][]params.ActionResult{ []params.ActionResult{{ Action: ¶ms.Action{ Tag: actionTag1.String(), }, }}, []params.ActionResult{{ Action: ¶ms.Action{ Tag: actionTag2.String(), }, }}, }, actionMap: map[string]params.ActionResult{ actionTag1.Id(): params.ActionResult{ Action: ¶ms.Action{ Tag: actionTag1.String(), Receiver: "unit-uptime-0", }, Output: map[string]interface{}{ "Stdout": "ok", "Stderr": "", }, }, actionTag2.Id(): params.ActionResult{ Action: ¶ms.Action{ Tag: actionTag2.String(), }, Output: map[string]interface{}{ "Stdout": "ok", "Stderr": "", }, }, }, }, { about: "invalid tag returned", args: []string{"uptime"}, results: [][]params.ActionResult{ []params.ActionResult{{ Action: ¶ms.Action{ Tag: "invalid", }, }}, }, stdout: `failed to collect metrics: "invalid" is not a valid tag\n`, }, { about: "no action found", args: []string{"uptime"}, results: [][]params.ActionResult{ []params.ActionResult{{ Action: ¶ms.Action{ Tag: actionTag1.String(), }, }}, }, stdout: "failed to collect metrics: plm\n", }, { about: "fail to parse result", args: []string{"uptime"}, results: [][]params.ActionResult{ []params.ActionResult{{ Action: ¶ms.Action{ Tag: actionTag1.String(), }, }}, }, actionMap: map[string]params.ActionResult{ actionTag1.Id(): params.ActionResult{}, }, stdout: "failed to collect metrics: could not read stdout\n", }, { about: "no results on sendResults", args: []string{"uptime"}, results: [][]params.ActionResult{ []params.ActionResult{{ Action: ¶ms.Action{ Tag: actionTag1.String(), }, }}, }, actionMap: map[string]params.ActionResult{ actionTag1.Id(): params.ActionResult{ Action: ¶ms.Action{ Tag: actionTag2.String(), Receiver: "unit-uptime-0", }, Output: map[string]interface{}{ "Stdout": "ok", "Stderr": "", }, }, }, stdout: "failed to send metrics for unit uptime/0: no results\n", }, { about: "too many sendResults", args: []string{"uptime"}, results: [][]params.ActionResult{ []params.ActionResult{{ Action: ¶ms.Action{ Tag: actionTag1.String(), }, }}, []params.ActionResult{{ Action: ¶ms.Action{ Tag: actionTag1.String(), }, }, { Action: ¶ms.Action{ Tag: actionTag2.String(), }, }}, }, actionMap: map[string]params.ActionResult{ actionTag1.Id(): params.ActionResult{ Action: ¶ms.Action{ Tag: actionTag2.String(), Receiver: "unit-uptime-0", }, Output: map[string]interface{}{ "Stdout": "ok", "Stderr": "", }, }, }, stdout: "failed to send metrics for unit uptime/0\n", }, { about: "sendResults error", args: []string{"uptime"}, results: [][]params.ActionResult{ []params.ActionResult{{ Action: ¶ms.Action{ Tag: actionTag1.String(), }, }}, []params.ActionResult{{ Error: ¶ms.Error{ Message: "permission denied", }, }}, }, actionMap: map[string]params.ActionResult{ actionTag1.Id(): params.ActionResult{ Action: ¶ms.Action{ Tag: actionTag2.String(), Receiver: "unit-uptime-0", }, Output: map[string]interface{}{ "Stdout": "ok", "Stderr": "", }, }, }, stdout: "failed to send metrics for unit uptime/0: permission denied\n", }, { about: "couldn't get sendResults action", args: []string{"uptime"}, results: [][]params.ActionResult{ []params.ActionResult{{ Action: ¶ms.Action{ Tag: actionTag1.String(), }, }}, []params.ActionResult{{ Action: ¶ms.Action{ Tag: actionTag2.String(), }, }}, }, actionMap: map[string]params.ActionResult{ actionTag1.Id(): params.ActionResult{ Action: ¶ms.Action{ Tag: actionTag2.String(), Receiver: "unit-uptime-0", }, Output: map[string]interface{}{ "Stdout": "ok", "Stderr": "", }, }, }, stdout: "failed to send metrics for unit uptime/0: plm\n", }, { about: "couldn't parse sendResults action", args: []string{"uptime"}, results: [][]params.ActionResult{ []params.ActionResult{{ Action: ¶ms.Action{ Tag: actionTag1.String(), }, }}, []params.ActionResult{{ Action: ¶ms.Action{ Tag: actionTag2.String(), }, }}, }, actionMap: map[string]params.ActionResult{ actionTag1.Id(): params.ActionResult{ Action: ¶ms.Action{ Tag: actionTag2.String(), Receiver: "unit-uptime-0", }, Output: map[string]interface{}{ "Stdout": "ok", "Stderr": "", }, }, actionTag2.Id(): params.ActionResult{}, }, stdout: "failed to send metrics for unit uptime/0: could not read stdout\n", }, { about: "sendResults action stderr", args: []string{"uptime"}, results: [][]params.ActionResult{ []params.ActionResult{{ Action: ¶ms.Action{ Tag: actionTag1.String(), }, }}, []params.ActionResult{{ Action: ¶ms.Action{ Tag: actionTag2.String(), }, }}, }, actionMap: map[string]params.ActionResult{ actionTag1.Id(): params.ActionResult{ Action: ¶ms.Action{ Tag: actionTag2.String(), Receiver: "unit-uptime-0", }, Output: map[string]interface{}{ "Stdout": "ok", "Stderr": "", }, }, actionTag2.Id(): params.ActionResult{ Action: ¶ms.Action{ Tag: actionTag2.String(), Receiver: "unit-uptime-0", }, Output: map[string]interface{}{ "Stdout": "garbage", "Stderr": "kek", }, }, }, stdout: "failed to send metrics for unit uptime/0: kek\n", }} for i, test := range tests { c.Logf("running test %d: %v", i, test.about) runClient.reset() if test.results != nil { runClient.results = test.results } metricsdebug.PatchGetActionResult(s.PatchValue, test.actionMap) ctx, err := coretesting.RunCommand(c, metricsdebug.NewCollectMetricsCommand(), test.args...) if test.err != "" { c.Assert(err, gc.ErrorMatches, test.err) } else { c.Assert(err, jc.ErrorIsNil) c.Assert(coretesting.Stdout(ctx), gc.Matches, test.stdout) } } }
// registerCommands registers commands in the specified registry. func registerCommands(r commandRegistry, ctx *cmd.Context) { // Creation commands. r.Register(newBootstrapCommand()) r.Register(service.NewAddRelationCommand()) // Destruction commands. r.Register(service.NewRemoveRelationCommand()) r.Register(service.NewRemoveServiceCommand()) r.Register(service.NewRemoveUnitCommand()) // Reporting commands. r.Register(status.NewStatusCommand()) r.Register(newSwitchCommand()) r.Register(newEndpointCommand()) r.Register(newAPIInfoCommand()) r.Register(status.NewStatusHistoryCommand()) // Error resolution and debugging commands. r.Register(newRunCommand()) r.Register(newSCPCommand()) r.Register(newSSHCommand()) r.Register(newResolvedCommand()) r.Register(newDebugLogCommand()) r.Register(newDebugHooksCommand()) // Configuration commands. r.Register(newInitCommand()) r.Register(model.NewModelGetConstraintsCommand()) r.Register(model.NewModelSetConstraintsCommand()) r.Register(newSyncToolsCommand()) r.Register(newUpgradeJujuCommand(nil)) r.Register(service.NewUpgradeCharmCommand()) // Charm publishing commands. r.Register(newPublishCommand()) // Charm tool commands. r.Register(newHelpToolCommand()) r.Register(charmcmd.NewSuperCommand()) // Manage backups. r.Register(backups.NewSuperCommand()) r.RegisterSuperAlias("create-backup", "backups", "create", nil) r.RegisterSuperAlias("restore-backup", "backups", "restore", nil) // Manage authorized ssh keys. r.Register(NewAddKeysCommand()) r.Register(NewRemoveKeysCommand()) r.Register(NewImportKeysCommand()) r.Register(NewListKeysCommand()) // Manage users and access r.Register(user.NewAddCommand()) r.Register(user.NewChangePasswordCommand()) r.Register(user.NewCredentialsCommand()) r.Register(user.NewShowUserCommand()) r.Register(user.NewListCommand()) r.Register(user.NewEnableCommand()) r.Register(user.NewDisableCommand()) // Manage cached images r.Register(cachedimages.NewSuperCommand()) // Manage machines r.Register(machine.NewAddCommand()) r.Register(machine.NewRemoveCommand()) r.Register(machine.NewListMachinesCommand()) r.Register(machine.NewShowMachineCommand()) // Manage model r.Register(model.NewGetCommand()) r.Register(model.NewSetCommand()) r.Register(model.NewUnsetCommand()) r.Register(model.NewRetryProvisioningCommand()) r.Register(model.NewDestroyCommand()) r.Register(model.NewShareCommand()) r.Register(model.NewUnshareCommand()) r.Register(model.NewUsersCommand()) // Manage and control actions r.Register(action.NewSuperCommand()) r.RegisterSuperAlias("run-action", "action", "do", nil) r.RegisterSuperAlias("list-actions", "action", "defined", nil) r.RegisterSuperAlias("show-action-output", "action", "fetch", nil) r.RegisterSuperAlias("show-action-status", "action", "status", nil) // Manage controller availability r.Register(newEnableHACommand()) // Manage and control services r.Register(service.NewAddUnitCommand()) r.Register(service.NewGetCommand()) r.Register(service.NewSetCommand()) r.Register(service.NewDeployCommand()) r.Register(service.NewExposeCommand()) r.Register(service.NewUnexposeCommand()) r.Register(service.NewServiceGetConstraintsCommand()) r.Register(service.NewServiceSetConstraintsCommand()) // Operation protection commands r.Register(block.NewSuperBlockCommand()) r.Register(block.NewUnblockCommand()) // Manage storage r.Register(storage.NewSuperCommand()) r.RegisterSuperAlias("list-storage", "storage", "list", nil) r.RegisterSuperAlias("show-storage", "storage", "show", nil) r.RegisterSuperAlias("add-storage", "storage", "add", nil) // Manage spaces r.Register(space.NewSuperCommand()) r.RegisterSuperAlias("add-space", "space", "create", nil) r.RegisterSuperAlias("list-spaces", "space", "list", nil) // Manage subnets r.Register(subnet.NewSuperCommand()) r.RegisterSuperAlias("add-subnet", "subnet", "add", nil) // Manage controllers r.Register(controller.NewCreateModelCommand()) r.Register(controller.NewDestroyCommand()) r.Register(controller.NewModelsCommand()) r.Register(controller.NewKillCommand()) r.Register(controller.NewListCommand()) r.Register(controller.NewListBlocksCommand()) r.Register(controller.NewLoginCommand()) r.Register(controller.NewRemoveBlocksCommand()) r.Register(controller.NewUseModelCommand()) // Debug Metrics r.Register(metricsdebug.New()) r.Register(metricsdebug.NewCollectMetricsCommand()) r.Register(setmeterstatus.New()) // Commands registered elsewhere. for _, newCommand := range registeredCommands { command := newCommand() r.Register(command) } for _, newCommand := range registeredEnvCommands { command := newCommand() r.Register(modelcmd.Wrap(command)) } rcmd.RegisterAll(r) }