From 473120648f1b00a546dc1afd436ee49a0700927a Mon Sep 17 00:00:00 2001 From: Priya Wadhwa Date: Wed, 28 Oct 2020 11:30:11 -0700 Subject: [PATCH 1/6] Add JSON output to minikube stop --- cmd/minikube/cmd/start.go | 4 ++-- cmd/minikube/cmd/start_flags.go | 4 ++-- cmd/minikube/cmd/stop.go | 2 ++ 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/cmd/minikube/cmd/start.go b/cmd/minikube/cmd/start.go index 909b37d7340e..a773eec84406 100644 --- a/cmd/minikube/cmd/start.go +++ b/cmd/minikube/cmd/start.go @@ -128,7 +128,7 @@ func platform() string { func runStart(cmd *cobra.Command, args []string) { register.SetEventLogPath(localpath.EventLog(ClusterFlagValue())) - out.SetJSON(viper.GetString(startOutput) == "json") + out.SetJSON(viper.GetString(outputFormat) == "json") displayVersion(version.GetVersion()) // No need to do the update check if no one is going to see it @@ -1074,7 +1074,7 @@ func validateFlags(cmd *cobra.Command, drvName string) { } } - if s := viper.GetString(startOutput); s != "text" && s != "json" { + if s := viper.GetString(outputFormat); s != "text" && s != "json" { exit.Message(reason.Usage, "Sorry, please set the --output flag to one of the following valid options: [text,json]") } diff --git a/cmd/minikube/cmd/start_flags.go b/cmd/minikube/cmd/start_flags.go index 922248bae84c..dce9ad68fdd4 100644 --- a/cmd/minikube/cmd/start_flags.go +++ b/cmd/minikube/cmd/start_flags.go @@ -106,7 +106,7 @@ const ( deleteOnFailure = "delete-on-failure" forceSystemd = "force-systemd" kicBaseImage = "base-image" - startOutput = "output" + outputFormat = "output" ports = "ports" ) @@ -147,7 +147,7 @@ func initMinikubeFlags() { startCmd.Flags().Bool(preload, true, "If set, download tarball of preloaded images if available to improve start time. Defaults to true.") startCmd.Flags().Bool(deleteOnFailure, false, "If set, delete the current cluster if start fails and try again. Defaults to false.") startCmd.Flags().Bool(forceSystemd, false, "If set, force the container runtime to use sytemd as cgroup manager. Currently available for docker and crio. Defaults to false.") - startCmd.Flags().StringP(startOutput, "o", "text", "Format to print stdout in. Options include: [text,json]") + startCmd.Flags().StringP(outputFormat, "o", "text", "Format to print stdout in. Options include: [text,json]") } // initKubernetesFlags inits the commandline flags for Kubernetes related options diff --git a/cmd/minikube/cmd/stop.go b/cmd/minikube/cmd/stop.go index 1079749f3d15..145dfb2832f9 100644 --- a/cmd/minikube/cmd/stop.go +++ b/cmd/minikube/cmd/stop.go @@ -55,6 +55,7 @@ var stopCmd = &cobra.Command{ func init() { stopCmd.Flags().BoolVar(&stopAll, "all", false, "Set flag to stop all profiles (clusters)") stopCmd.Flags().BoolVar(&keepActive, "keep-context-active", false, "keep the kube-context active after cluster is stopped. Defaults to false.") + stopCmd.Flags().StringP(outputFormat, "o", "text", "Format to print stdout in. Options include: [text,json]") if err := viper.GetViper().BindPFlags(stopCmd.Flags()); err != nil { exit.Error(reason.InternalFlagsBind, "unable to bind flags", err) @@ -63,6 +64,7 @@ func init() { // runStop handles the executes the flow of "minikube stop" func runStop(cmd *cobra.Command, args []string) { + out.SetJSON(viper.GetString(outputFormat) == "json") register.SetEventLogPath(localpath.EventLog(ClusterFlagValue())) register.Reg.SetStep(register.Stopping) From a2fcd8e37bd3be583056fe4f93f580bb7847bdc3 Mon Sep 17 00:00:00 2001 From: Priya Wadhwa Date: Wed, 28 Oct 2020 12:54:18 -0700 Subject: [PATCH 2/6] update docs --- site/content/en/docs/commands/stop.md | 1 + 1 file changed, 1 insertion(+) diff --git a/site/content/en/docs/commands/stop.md b/site/content/en/docs/commands/stop.md index 1fbadc44c302..c0f0286ddbac 100644 --- a/site/content/en/docs/commands/stop.md +++ b/site/content/en/docs/commands/stop.md @@ -23,6 +23,7 @@ minikube stop [flags] --all Set flag to stop all profiles (clusters) -h, --help help for stop --keep-context-active keep the kube-context active after cluster is stopped. Defaults to false. + -o, --output string Format to print stdout in. Options include: [text,json] (default "text") ``` ### Options inherited from parent commands From 39ab9ffc5b94269692bbc61d063c7d2411ceceb6 Mon Sep 17 00:00:00 2001 From: Priya Wadhwa Date: Wed, 28 Oct 2020 13:16:17 -0700 Subject: [PATCH 3/6] Add --output flags to pause and unpause as well --- cmd/minikube/cmd/pause.go | 2 ++ cmd/minikube/cmd/start.go | 4 ++-- cmd/minikube/cmd/start_flags.go | 7 +++++-- cmd/minikube/cmd/stop.go | 4 ++-- cmd/minikube/cmd/unpause.go | 2 ++ 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/cmd/minikube/cmd/pause.go b/cmd/minikube/cmd/pause.go index 7cfa359f80ee..55e44147b222 100644 --- a/cmd/minikube/cmd/pause.go +++ b/cmd/minikube/cmd/pause.go @@ -50,6 +50,7 @@ var pauseCmd = &cobra.Command{ } func runPause(cmd *cobra.Command, args []string) { + out.SetJSON(outputFormat == "json") co := mustload.Running(ClusterFlagValue()) register.SetEventLogPath(localpath.EventLog(ClusterFlagValue())) register.Reg.SetStep(register.Pausing) @@ -105,4 +106,5 @@ func runPause(cmd *cobra.Command, args []string) { func init() { pauseCmd.Flags().StringSliceVarP(&namespaces, "--namespaces", "n", constants.DefaultNamespaces, "namespaces to pause") pauseCmd.Flags().BoolVarP(&allNamespaces, "all-namespaces", "A", false, "If set, pause all namespaces") + pauseCmd.Flags().StringVarP(&outputFormat, "output", "o", "text", "Format to print stdout in. Options include: [text,json]") } diff --git a/cmd/minikube/cmd/start.go b/cmd/minikube/cmd/start.go index a773eec84406..7caad3890fd9 100644 --- a/cmd/minikube/cmd/start.go +++ b/cmd/minikube/cmd/start.go @@ -128,7 +128,7 @@ func platform() string { func runStart(cmd *cobra.Command, args []string) { register.SetEventLogPath(localpath.EventLog(ClusterFlagValue())) - out.SetJSON(viper.GetString(outputFormat) == "json") + out.SetJSON(outputFormat == "json") displayVersion(version.GetVersion()) // No need to do the update check if no one is going to see it @@ -1074,7 +1074,7 @@ func validateFlags(cmd *cobra.Command, drvName string) { } } - if s := viper.GetString(outputFormat); s != "text" && s != "json" { + if outputFormat != "text" && outputFormat != "json" { exit.Message(reason.Usage, "Sorry, please set the --output flag to one of the following valid options: [text,json]") } diff --git a/cmd/minikube/cmd/start_flags.go b/cmd/minikube/cmd/start_flags.go index dce9ad68fdd4..dc80fe263ded 100644 --- a/cmd/minikube/cmd/start_flags.go +++ b/cmd/minikube/cmd/start_flags.go @@ -106,10 +106,13 @@ const ( deleteOnFailure = "delete-on-failure" forceSystemd = "force-systemd" kicBaseImage = "base-image" - outputFormat = "output" ports = "ports" ) +var ( + outputFormat string +) + // initMinikubeFlags includes commandline flags for minikube. func initMinikubeFlags() { viper.SetEnvPrefix(minikubeEnvPrefix) @@ -147,7 +150,7 @@ func initMinikubeFlags() { startCmd.Flags().Bool(preload, true, "If set, download tarball of preloaded images if available to improve start time. Defaults to true.") startCmd.Flags().Bool(deleteOnFailure, false, "If set, delete the current cluster if start fails and try again. Defaults to false.") startCmd.Flags().Bool(forceSystemd, false, "If set, force the container runtime to use sytemd as cgroup manager. Currently available for docker and crio. Defaults to false.") - startCmd.Flags().StringP(outputFormat, "o", "text", "Format to print stdout in. Options include: [text,json]") + startCmd.Flags().StringVarP(&outputFormat, "output", "o", "text", "Format to print stdout in. Options include: [text,json]") } // initKubernetesFlags inits the commandline flags for Kubernetes related options diff --git a/cmd/minikube/cmd/stop.go b/cmd/minikube/cmd/stop.go index 145dfb2832f9..d2a4f0228756 100644 --- a/cmd/minikube/cmd/stop.go +++ b/cmd/minikube/cmd/stop.go @@ -55,7 +55,7 @@ var stopCmd = &cobra.Command{ func init() { stopCmd.Flags().BoolVar(&stopAll, "all", false, "Set flag to stop all profiles (clusters)") stopCmd.Flags().BoolVar(&keepActive, "keep-context-active", false, "keep the kube-context active after cluster is stopped. Defaults to false.") - stopCmd.Flags().StringP(outputFormat, "o", "text", "Format to print stdout in. Options include: [text,json]") + stopCmd.Flags().StringVarP(&outputFormat, "output", "o", "text", "Format to print stdout in. Options include: [text,json]") if err := viper.GetViper().BindPFlags(stopCmd.Flags()); err != nil { exit.Error(reason.InternalFlagsBind, "unable to bind flags", err) @@ -64,7 +64,7 @@ func init() { // runStop handles the executes the flow of "minikube stop" func runStop(cmd *cobra.Command, args []string) { - out.SetJSON(viper.GetString(outputFormat) == "json") + out.SetJSON(outputFormat == "json") register.SetEventLogPath(localpath.EventLog(ClusterFlagValue())) register.Reg.SetStep(register.Stopping) diff --git a/cmd/minikube/cmd/unpause.go b/cmd/minikube/cmd/unpause.go index 7d0b53de178d..69aa1329bd11 100644 --- a/cmd/minikube/cmd/unpause.go +++ b/cmd/minikube/cmd/unpause.go @@ -46,6 +46,7 @@ var unpauseCmd = &cobra.Command{ register.SetEventLogPath(localpath.EventLog(cname)) co := mustload.Running(cname) + out.SetJSON(outputFormat == "json") register.Reg.SetStep(register.Unpausing) klog.Infof("namespaces: %v keys: %v", namespaces, viper.AllSettings()) @@ -106,4 +107,5 @@ var unpauseCmd = &cobra.Command{ func init() { unpauseCmd.Flags().StringSliceVarP(&namespaces, "--namespaces", "n", constants.DefaultNamespaces, "namespaces to unpause") unpauseCmd.Flags().BoolVarP(&allNamespaces, "all-namespaces", "A", false, "If set, unpause all namespaces") + unpauseCmd.Flags().StringVarP(&outputFormat, "output", "o", "text", "Format to print stdout in. Options include: [text,json]") } From d9dc4efa4d41368eecf4071f6252a1c0ab469f22 Mon Sep 17 00:00:00 2001 From: Priya Wadhwa Date: Wed, 28 Oct 2020 13:16:25 -0700 Subject: [PATCH 4/6] update docs --- site/content/en/docs/commands/pause.md | 1 + site/content/en/docs/commands/unpause.md | 1 + 2 files changed, 2 insertions(+) diff --git a/site/content/en/docs/commands/pause.md b/site/content/en/docs/commands/pause.md index 49bd6686d279..df307b78baca 100644 --- a/site/content/en/docs/commands/pause.md +++ b/site/content/en/docs/commands/pause.md @@ -23,6 +23,7 @@ minikube pause [flags] -n, ----namespaces strings namespaces to pause (default [kube-system,kubernetes-dashboard,storage-gluster,istio-operator]) -A, --all-namespaces If set, pause all namespaces -h, --help help for pause + -o, --output string Format to print stdout in. Options include: [text,json] (default "text") ``` ### Options inherited from parent commands diff --git a/site/content/en/docs/commands/unpause.md b/site/content/en/docs/commands/unpause.md index 6fb1eda3b592..1cab85ed0f83 100644 --- a/site/content/en/docs/commands/unpause.md +++ b/site/content/en/docs/commands/unpause.md @@ -23,6 +23,7 @@ minikube unpause [flags] -n, ----namespaces strings namespaces to unpause (default [kube-system,kubernetes-dashboard,storage-gluster,istio-operator]) -A, --all-namespaces If set, unpause all namespaces -h, --help help for unpause + -o, --output string Format to print stdout in. Options include: [text,json] (default "text") ``` ### Options inherited from parent commands From 35c33c1ac6d3e6a7155e4cdabf2de7f4cfbed335 Mon Sep 17 00:00:00 2001 From: Priya Wadhwa Date: Wed, 28 Oct 2020 13:58:53 -0700 Subject: [PATCH 5/6] Add stop, pause and unpause to integration test --- pkg/minikube/machine/stop.go | 1 + pkg/minikube/out/register/register.go | 5 +- test/integration/json_output_test.go | 74 +++++++++++++++++---------- 3 files changed, 51 insertions(+), 29 deletions(-) diff --git a/pkg/minikube/machine/stop.go b/pkg/minikube/machine/stop.go index 4309a8cdae04..54fb0f8331be 100644 --- a/pkg/minikube/machine/stop.go +++ b/pkg/minikube/machine/stop.go @@ -80,6 +80,7 @@ func trySSHPowerOff(h *host.Host) error { return nil } + register.Reg.SetStep(register.PowerOff) out.T(style.Shutdown, `Powering off "{{.profile_name}}" via SSH ...`, out.V{"profile_name": h.Name}) // differnet for kic because RunSSHCommand is not implemented by kic if driver.IsKIC(h.DriverName) { diff --git a/pkg/minikube/out/register/register.go b/pkg/minikube/out/register/register.go index f5e316c1cbfb..5672e7ad3021 100644 --- a/pkg/minikube/out/register/register.go +++ b/pkg/minikube/out/register/register.go @@ -39,6 +39,7 @@ const ( Done RegStep = "Done" Stopping RegStep = "Stopping" + PowerOff RegStep = "PowerOff" Deleting RegStep = "Deleting" Pausing RegStep = "Pausing" Unpausing RegStep = "Unpausing" @@ -78,7 +79,7 @@ func init() { Done, }, - Stopping: {Stopping, Done}, + Stopping: {Stopping, PowerOff, Done}, Pausing: {Pausing, Done}, Unpausing: {Unpausing, Done}, Deleting: {Deleting, Stopping, Deleting, Done}, @@ -126,5 +127,3 @@ func (r *Register) SetStep(s RegStep) { r.current = s } - -// recordStep records the current step diff --git a/test/integration/json_output_test.go b/test/integration/json_output_test.go index b5085dffd7b2..e5033dce7454 100644 --- a/test/integration/json_output_test.go +++ b/test/integration/json_output_test.go @@ -36,34 +36,56 @@ func TestJSONOutput(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), Minutes(40)) defer Cleanup(t, profile, cancel) - startArgs := []string{"start", "-p", profile, "--memory=2200", "--output=json", "--wait=true"} - startArgs = append(startArgs, StartArgs()...) - - rr, err := Run(t, exec.CommandContext(ctx, Target(), startArgs...)) - if err != nil { - t.Errorf("failed to clean up: args %q: %v", rr.Command(), err) - } - - ces, err := cloudEvents(t, rr) - if err != nil { - t.Fatalf("converting to cloud events: %v\n", err) + tests := []struct { + command string + args []string + }{ + { + command: "start", + args: append([]string{"--memory=2200", "--wait=true"}, StartArgs()...), + }, { + command: "pause", + }, { + command: "unpause", + }, { + command: "stop", + }, } - type validateJSONOutputFunc func(context.Context, *testing.T, []*cloudEvent) - t.Run("serial", func(t *testing.T) { - serialTests := []struct { - name string - validator validateJSONOutputFunc - }{ - {"DistinctCurrentSteps", validateDistinctCurrentSteps}, - {"IncreasingCurrentSteps", validateIncreasingCurrentSteps}, - } - for _, stc := range serialTests { - t.Run(stc.name, func(t *testing.T) { - stc.validator(ctx, t, ces) + for _, test := range tests { + t.Run(test.command, func(t *testing.T) { + args := []string{test.command, "-p", profile, "--output=json"} + args = append(args, test.args...) + + rr, err := Run(t, exec.CommandContext(ctx, Target(), args...)) + if err != nil { + t.Errorf("failed to clean up: args %q: %v", rr.Command(), err) + } + + ces, err := cloudEvents(t, rr) + if err != nil { + t.Fatalf("converting to cloud events: %v\n", err) + } + + type validateJSONOutputFunc func(context.Context, *testing.T, []*cloudEvent) + t.Run("parallel", func(t *testing.T) { + parallelTests := []struct { + name string + validator validateJSONOutputFunc + }{ + {"DistinctCurrentSteps", validateDistinctCurrentSteps}, + {"IncreasingCurrentSteps", validateIncreasingCurrentSteps}, + } + for _, stc := range parallelTests { + stc := stc + t.Run(stc.name, func(t *testing.T) { + MaybeParallel(t) + stc.validator(ctx, t, ces) + }) + } }) - } - }) + }) + } } // make sure each step has a distinct step number @@ -100,7 +122,7 @@ func validateIncreasingCurrentSteps(ctx context.Context, t *testing.T, ces []*cl } } -func TestJSONOutputError(t *testing.T) { +func TestJxSONOutputError(t *testing.T) { profile := UniqueProfileName("json-output-error") ctx, cancel := context.WithTimeout(context.Background(), Minutes(2)) defer Cleanup(t, profile, cancel) From 91629c22c96fa42ca10b2f4da2c4f0d2aea9c213 Mon Sep 17 00:00:00 2001 From: Priya Wadhwa Date: Wed, 28 Oct 2020 14:03:11 -0700 Subject: [PATCH 6/6] remove debugging --- test/integration/json_output_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/json_output_test.go b/test/integration/json_output_test.go index e5033dce7454..01dfa6feb341 100644 --- a/test/integration/json_output_test.go +++ b/test/integration/json_output_test.go @@ -122,7 +122,7 @@ func validateIncreasingCurrentSteps(ctx context.Context, t *testing.T, ces []*cl } } -func TestJxSONOutputError(t *testing.T) { +func TestJSONOutputError(t *testing.T) { profile := UniqueProfileName("json-output-error") ctx, cancel := context.WithTimeout(context.Background(), Minutes(2)) defer Cleanup(t, profile, cancel)