Skip to content

Commit

Permalink
backend: Add tests for rename clusters
Browse files Browse the repository at this point in the history
Signed-off-by: Kautilya Tripathi <ktripathi@microsoft.com>
  • Loading branch information
knrt10 committed Jun 3, 2024
1 parent 785e514 commit 08c4785
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
61 changes: 61 additions & 0 deletions backend/cmd/headlamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
4 changes: 4 additions & 0 deletions backend/cmd/headlamp_testdata/kubeconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 08c4785

Please sign in to comment.