From c5e917366f7b75bd00679bb1cdcc1e23ba80e2b2 Mon Sep 17 00:00:00 2001 From: Thomas Stromberg Date: Thu, 9 Jan 2020 11:10:23 -0800 Subject: [PATCH 1/2] Add --dry-run option to start --- cmd/minikube/cmd/start.go | 12 ++++++++++-- pkg/minikube/out/style.go | 1 + pkg/minikube/out/style_enum.go | 1 + test/integration/functional_test.go | 27 +++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/cmd/minikube/cmd/start.go b/cmd/minikube/cmd/start.go index d1e4fbec98f9..a8a6eefd19c3 100644 --- a/cmd/minikube/cmd/start.go +++ b/cmd/minikube/cmd/start.go @@ -115,6 +115,7 @@ const ( hostDNSResolver = "host-dns-resolver" waitUntilHealthy = "wait" force = "force" + dryRun = "dry-run" interactive = "interactive" waitTimeout = "wait-timeout" nativeSSH = "native-ssh" @@ -157,6 +158,7 @@ func initMinikubeFlags() { startCmd.Flags().Bool(force, false, "Force minikube to perform possibly dangerous operations") startCmd.Flags().Bool(interactive, true, "Allow user prompts for more information") + startCmd.Flags().Bool(dryRun, false, "dry-run mode. Validates configuration, but does does not mutate system state") startCmd.Flags().Int(cpus, 2, "Number of CPUs allocated to the minikube VM.") startCmd.Flags().String(memory, defaultMemorySize, "Amount of RAM allocated to the minikube VM (format: [], where unit = b, k, m or g).") @@ -305,7 +307,7 @@ func runStart(cmd *cobra.Command, args []string) { validateUser(driverName) // No need to install a driver in download-only mode - if !viper.GetBool(downloadOnly) { + if !viper.GetBool(downloadOnly) && !viper.GetBool(dryRun) { updateDriver(driverName) } @@ -315,6 +317,12 @@ func runStart(cmd *cobra.Command, args []string) { exit.WithError("Failed to generate config", err) } + // This is about as far as we can go without overwriting config files + if viper.GetBool(dryRun) { + out.T(out.DryRun, `dry-run validation complete!`) + return + } + if !driver.BareMetal(driverName) { if err := cluster.CacheISO(config); err != nil { exit.WithError("Failed to cache ISO", err) @@ -748,7 +756,7 @@ func validateFlags(cmd *cobra.Command, drvName string) { memorySizeMB := pkgutil.CalculateSizeInMB(viper.GetString(memory)) if memorySizeMB < pkgutil.CalculateSizeInMB(minimumMemorySize) && !viper.GetBool(force) { - exit.UsageT("Requested memory allocation {{.requested_size}} is less than the minimum allowed of {{.minimum_size}}", out.V{"requested_size": memorySizeMB, "minimum_size": pkgutil.CalculateSizeInMB(minimumMemorySize)}) + exit.WithCodeT(exit.Config, "Requested memory allocation {{.requested_size}} is less than the minimum allowed of {{.minimum_size}}", out.V{"requested_size": memorySizeMB, "minimum_size": pkgutil.CalculateSizeInMB(minimumMemorySize)}) } if memorySizeMB < pkgutil.CalculateSizeInMB(defaultMemorySize) && !viper.GetBool(force) { out.T(out.Notice, "Requested memory allocation ({{.memory}}MB) is less than the default memory allocation of {{.default_memorysize}}MB. Beware that minikube might not work correctly or crash unexpectedly.", diff --git a/pkg/minikube/out/style.go b/pkg/minikube/out/style.go index b14c9d5883e2..3695ed416725 100644 --- a/pkg/minikube/out/style.go +++ b/pkg/minikube/out/style.go @@ -113,6 +113,7 @@ var styles = map[StyleEnum]style{ Unmount: {Prefix: "🔥 "}, MountOptions: {Prefix: "💾 "}, Fileserver: {Prefix: "🚀 ", OmitNewline: true}, + DryRun: {Prefix: "🏜️ "}, } // Add a prefix to a string diff --git a/pkg/minikube/out/style_enum.go b/pkg/minikube/out/style_enum.go index 0bc8ac182279..81e232b14194 100644 --- a/pkg/minikube/out/style_enum.go +++ b/pkg/minikube/out/style_enum.go @@ -84,4 +84,5 @@ const ( Empty Workaround Sparkle + DryRun ) diff --git a/test/integration/functional_test.go b/test/integration/functional_test.go index a21ec2a56b87..6677bc44c5f3 100644 --- a/test/integration/functional_test.go +++ b/test/integration/functional_test.go @@ -92,6 +92,7 @@ func TestFunctional(t *testing.T) { {"ConfigCmd", validateConfigCmd}, {"DashboardCmd", validateDashboardCmd}, {"DNS", validateDNS}, + {"DryRun", validateDryRun}, {"StatusCmd", validateStatusCmd}, {"LogsCmd", validateLogsCmd}, {"MountCmd", validateMountCmd}, @@ -309,6 +310,32 @@ func validateDNS(ctx context.Context, t *testing.T, profile string) { } } +// validateDryRun asserts that the dry-run mode quickly exits with the right code +func validateDryRun(ctx context.Context, t *testing.T, profile string) { + // dry-run mode should always be able to finish quickly + mctx, cancel := context.WithTimeout(ctx, 2*time.Second) + defer cancel() + + // Too little memory! + startArgs := append([]string{"start", "-p", profile, "--dry-run", "--memory", "250MB"}, StartArgs()...) + c := exec.CommandContext(mctx, Target(), startArgs...) + rr, err := Run(t, c) + + wantCode := 78 // exit.Config + if rr.ExitCode != wantCode { + t.Errorf("dry-run(250MB) exit code = %d, wanted = %d: %v", rr.ExitCode, wantCode, err) + } + + dctx, cancel := context.WithTimeout(ctx, 2*time.Second) + defer cancel() + startArgs = append([]string{"start", "-p", profile, "--dry-run"}, StartArgs()...) + c = exec.CommandContext(dctx, Target(), startArgs...) + rr, err = Run(t, c) + if rr.ExitCode != 0 || err != nil { + t.Errorf("dry-run exit code = %d, wanted = %d: %v", rr.ExitCode, 0, err) + } +} + // validateCacheCmd tests functionality of cache command (cache add, delete, list) func validateCacheCmd(ctx context.Context, t *testing.T, profile string) { if NoneDriver() { From b840d1ced075161e38c09315546653d993f10188 Mon Sep 17 00:00:00 2001 From: Thomas Stromberg Date: Thu, 9 Jan 2020 11:58:15 -0800 Subject: [PATCH 2/2] Call goimports --- cmd/minikube/cmd/start.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/minikube/cmd/start.go b/cmd/minikube/cmd/start.go index 470fcfd4a06c..caf27ecb3458 100644 --- a/cmd/minikube/cmd/start.go +++ b/cmd/minikube/cmd/start.go @@ -311,13 +311,13 @@ func runStart(cmd *cobra.Command, args []string) { if !viper.GetBool(dryRun) { updateDriver(driverName) } - + k8sVersion, isUpgrade := getKubernetesVersion(existing) config, err := generateCfgFromFlags(cmd, k8sVersion, driverName) if err != nil { exit.WithError("Failed to generate config", err) } - + // This is about as far as we can go without overwriting config files if viper.GetBool(dryRun) { out.T(out.DryRun, `dry-run validation complete!`)