From e57a3f8718da5e268b6c6196270cab80cf4b1dfa Mon Sep 17 00:00:00 2001 From: Steven Powell Date: Fri, 29 Sep 2023 13:03:13 -0700 Subject: [PATCH 1/3] Fix enabling & disabling addons with non-existing cluster --- cmd/minikube/cmd/config/disable.go | 2 +- cmd/minikube/cmd/config/enable.go | 8 +++--- test/integration/addons_test.go | 42 ++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/cmd/minikube/cmd/config/disable.go b/cmd/minikube/cmd/config/disable.go index b70befbcb193..4a3635c18834 100644 --- a/cmd/minikube/cmd/config/disable.go +++ b/cmd/minikube/cmd/config/disable.go @@ -35,6 +35,7 @@ var addonsDisableCmd = &cobra.Command{ if len(args) != 1 { exit.Message(reason.Usage, "usage: minikube addons disable ADDON_NAME") } + _, cc := mustload.Partial(ClusterFlagValue()) err := addons.VerifyNotPaused(ClusterFlagValue(), false) if err != nil { exit.Error(reason.InternalAddonDisablePaused, "disable failed", err) @@ -43,7 +44,6 @@ var addonsDisableCmd = &cobra.Command{ if addon == "heapster" { exit.Message(reason.AddonUnsupported, "The heapster addon is depreciated. please try to disable metrics-server instead") } - _, cc := mustload.Partial(ClusterFlagValue()) validAddon, ok := assets.Addons[addon] if !ok { exit.Message(reason.AddonUnsupported, `"'{{.minikube_addon}}' is not a valid minikube addon`, out.V{"minikube_addon": addon}) diff --git a/cmd/minikube/cmd/config/enable.go b/cmd/minikube/cmd/config/enable.go index 9c1572715109..e04a6f75c786 100644 --- a/cmd/minikube/cmd/config/enable.go +++ b/cmd/minikube/cmd/config/enable.go @@ -26,6 +26,7 @@ import ( "k8s.io/minikube/pkg/minikube/config" "k8s.io/minikube/pkg/minikube/constants" "k8s.io/minikube/pkg/minikube/exit" + "k8s.io/minikube/pkg/minikube/mustload" "k8s.io/minikube/pkg/minikube/out" "k8s.io/minikube/pkg/minikube/reason" "k8s.io/minikube/pkg/minikube/style" @@ -40,15 +41,12 @@ var addonsEnableCmd = &cobra.Command{ if len(args) != 1 { exit.Message(reason.Usage, "usage: minikube addons enable ADDON_NAME") } - cc, err := config.Load(ClusterFlagValue()) - if err != nil && !config.IsNotExist(err) { - out.ErrT(style.Sad, `Unable to load config: {{.error}}`, out.V{"error": err}) - } + _, cc := mustload.Partial(ClusterFlagValue()) if cc.KubernetesConfig.KubernetesVersion == constants.NoKubernetesVersion { exit.Message(reason.Usage, "You cannot enable addons on a cluster without Kubernetes, to enable Kubernetes on your cluster, run: minikube start --kubernetes-version=stable") } - err = addons.VerifyNotPaused(ClusterFlagValue(), true) + err := addons.VerifyNotPaused(ClusterFlagValue(), true) if err != nil { exit.Error(reason.InternalAddonEnablePaused, "enabled failed", err) } diff --git a/test/integration/addons_test.go b/test/integration/addons_test.go index 3421c37c0452..aab58163e6a6 100644 --- a/test/integration/addons_test.go +++ b/test/integration/addons_test.go @@ -50,6 +50,26 @@ func TestAddons(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), Minutes(40)) defer Cleanup(t, profile, cancel) + t.Run("PreSetupTests", func(t *testing.T) { + tests := []struct { + name string + validator validateFunc + }{ + {"EnablingAddonOnNonExistingCluster", validateEnablingAddonOnNonExistingCluster}, + {"DisablingAddonOnNonExistingCluster", validateDisablingAddonOnNonExistingCluster}, + } + for _, tc := range tests { + tc := tc + if ctx.Err() == context.DeadlineExceeded { + t.Fatalf("Unable to run more tests (deadline exceeded)") + } + t.Run(tc.name, func(t *testing.T) { + MaybeParallel(t) + tc.validator(ctx, t, profile) + }) + } + }) + setupSucceeded := t.Run("Setup", func(t *testing.T) { // Set an env var to point to our dummy credentials file // don't use t.Setenv because we sometimes manually unset the env var later manually @@ -900,3 +920,25 @@ func validateLocalPathAddon(ctx context.Context, t *testing.T, profile string) { t.Errorf("failed to disable storage-provisioner-rancher addon: args %q: %v", rr.Command(), err) } } + +// validateEnablingAddonOnNonExistingCluster tests enabling an addon on a non-existing cluster +func validateEnablingAddonOnNonExistingCluster(ctx context.Context, t *testing.T, profile string) { + rr, err := Run(t, exec.CommandContext(ctx, Target(), "addons", "enable", "dashboard", "-p", profile)) + if err == nil { + t.Fatalf("enabling addon succeeded when it shouldn't have: %s", rr.Output()) + } + if !strings.Contains(err.Error(), "To start a cluster, run") { + t.Fatalf("unexpected error was returned: %v", err) + } +} + +// validateDisablingAddonOnNonExistingCluster tests disabling an addon on a non-existing cluster +func validateDisablingAddonOnNonExistingCluster(ctx context.Context, t *testing.T, profile string) { + rr, err := Run(t, exec.CommandContext(ctx, Target(), "addons", "disable", "dashboard", "-p", profile)) + if err == nil { + t.Fatalf("disabling addon succeeded when it shouldn't have: %s", rr.Output()) + } + if !strings.Contains(err.Error(), "To start a cluster, run") { + t.Fatalf("unexpected error was returned: %v", err) + } +} From b0c445878175c300c575029a0c5ebe62cf76414e Mon Sep 17 00:00:00 2001 From: Steven Powell Date: Fri, 29 Sep 2023 13:07:25 -0700 Subject: [PATCH 2/3] fix test name --- test/integration/addons_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/addons_test.go b/test/integration/addons_test.go index aab58163e6a6..b5f24038f8bc 100644 --- a/test/integration/addons_test.go +++ b/test/integration/addons_test.go @@ -50,7 +50,7 @@ func TestAddons(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), Minutes(40)) defer Cleanup(t, profile, cancel) - t.Run("PreSetupTests", func(t *testing.T) { + t.Run("PreSetup", func(t *testing.T) { tests := []struct { name string validator validateFunc From 8c806400eb25be51692f37a313316592cd145f8e Mon Sep 17 00:00:00 2001 From: Steven Powell Date: Tue, 3 Oct 2023 09:29:07 -0700 Subject: [PATCH 3/3] fix checkout output --- test/integration/addons_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration/addons_test.go b/test/integration/addons_test.go index b5f24038f8bc..0ebc38bc871b 100644 --- a/test/integration/addons_test.go +++ b/test/integration/addons_test.go @@ -927,7 +927,7 @@ func validateEnablingAddonOnNonExistingCluster(ctx context.Context, t *testing.T if err == nil { t.Fatalf("enabling addon succeeded when it shouldn't have: %s", rr.Output()) } - if !strings.Contains(err.Error(), "To start a cluster, run") { + if !strings.Contains(rr.Output(), "To start a cluster, run") { t.Fatalf("unexpected error was returned: %v", err) } } @@ -938,7 +938,7 @@ func validateDisablingAddonOnNonExistingCluster(ctx context.Context, t *testing. if err == nil { t.Fatalf("disabling addon succeeded when it shouldn't have: %s", rr.Output()) } - if !strings.Contains(err.Error(), "To start a cluster, run") { + if !strings.Contains(rr.Output(), "To start a cluster, run") { t.Fatalf("unexpected error was returned: %v", err) } }