From 08c4785a189c0fb98595eb3479b3ae18fefe60ba Mon Sep 17 00:00:00 2001 From: Kautilya Tripathi Date: Tue, 28 May 2024 13:45:43 +0530 Subject: [PATCH] backend: Add tests for rename clusters Signed-off-by: Kautilya Tripathi --- backend/cmd/headlamp_test.go | 61 ++++++++++++++++++++++++ backend/cmd/headlamp_testdata/kubeconfig | 4 ++ 2 files changed, 65 insertions(+) diff --git a/backend/cmd/headlamp_test.go b/backend/cmd/headlamp_test.go index 2c0dfcbabb..668bf7826d 100644 --- a/backend/cmd/headlamp_test.go +++ b/backend/cmd/headlamp_test.go @@ -600,3 +600,64 @@ func TestHandleClusterAPI_XForwardedHost(t *testing.T) { assert.Equal(t, http.StatusOK, rr.Code) assert.Equal(t, "OK", rr.Body.String()) } + +func TestRenameCluster(t *testing.T) { + kubeConfigByte, err := os.ReadFile("./headlamp_testdata/kubeconfig") + require.NoError(t, err) + + kubeConfig := base64.StdEncoding.EncodeToString(kubeConfigByte) + req := ClusterReq{ + KubeConfig: &kubeConfig, + } + cache := cache.New[interface{}]() + kubeConfigStore := kubeconfig.NewContextStore() + + c := HeadlampConfig{ + useInCluster: false, + kubeConfigPath: "./headlamp_testdata/kubeconfig", + enableDynamicClusters: true, + cache: cache, + kubeConfigStore: kubeConfigStore, + } + handler := createHeadlampHandler(&c) + + r, err := getResponseFromRestrictedEndpoint(handler, "POST", "/cluster", req) + if err != nil { + t.Fatal(err) + } + + clusters := c.getClusters() + + assert.Equal(t, http.StatusCreated, r.Code) + assert.Equal(t, 2, len(clusters)) + + tests := []struct { + name string + clusterReq RenameClusterRequest + expectedState int + }{ + { + name: "passStatefull", + clusterReq: RenameClusterRequest{ + NewClusterName: "minikubetestworks", + Stateless: false, + Source: "kubeconfig", + }, + expectedState: http.StatusCreated, + }, + { + name: "stateless", + clusterReq: RenameClusterRequest{ + NewClusterName: "minikubetest", + Stateless: true, + }, + expectedState: http.StatusCreated, + }, + } + + for _, tc := range tests { + r, err = getResponseFromRestrictedEndpoint(handler, "PUT", "/cluster/minikube", tc.clusterReq) + require.NoError(t, err) + assert.Equal(t, tc.expectedState, r.Code) + } +} diff --git a/backend/cmd/headlamp_testdata/kubeconfig b/backend/cmd/headlamp_testdata/kubeconfig index b8c9e90493..ac037333dd 100644 --- a/backend/cmd/headlamp_testdata/kubeconfig +++ b/backend/cmd/headlamp_testdata/kubeconfig @@ -27,6 +27,10 @@ contexts: provider: minikube.sigs.k8s.io version: v1.28.0 name: context_info + - extension: + creationTimestamp: null + customName: minikubetestworks + name: headlamp_info namespace: default user: minikube name: minikube