Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anujc25 committed Dec 15, 2023
1 parent 00c78b0 commit 450decc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion config/tanzu_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func GetKubeconfigForContext(contextName string, opts ...ResourceOptions) ([]byt
return nil, errors.Errorf("context must be of type: %s", configtypes.ContextTypeTanzu)
}
if rOptions.spaceName != "" && rOptions.clusterGroupName != "" {
return nil, errors.Errorf("incorrect resource options provided. Both space and clustergroup are set but only one can be set.")
return nil, errors.Errorf("incorrect resource options provided. Both space and clustergroup are set but only one can be set")
}

kc, err := kubeconfig.ReadKubeConfig(ctx.ClusterOpts.Path)
Expand Down
21 changes: 19 additions & 2 deletions config/tanzu_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestGetKubeconfigForContext(t *testing.T) {
err = SetContext(c, false)
assert.NoError(t, err)

// Test getting the kubeconfig for an arbitrary Tanzu resource
// Test getting the kubeconfig for a space within a project
kubeconfigBytes, err := GetKubeconfigForContext(c.Name, ForProject("project1"), ForSpace("space1"))
assert.NoError(t, err)
c, err = GetContext("test-tanzu")
Expand All @@ -118,7 +118,7 @@ func TestGetKubeconfigForContext(t *testing.T) {
cluster := kubeconfig.GetCluster(&kc, "tanzu-cli-mytanzu/current")
assert.Equal(t, cluster.Cluster.Server, c.ClusterOpts.Endpoint+"/project/project1/space/space1")

// Test getting the kubeconfig for an arbitrary Tanzu resource
// Test getting the kubeconfig for a project
kubeconfigBytes, err = GetKubeconfigForContext(c.Name, ForProject("project2"))
assert.NoError(t, err)
c, err = GetContext("test-tanzu")
Expand All @@ -128,6 +128,23 @@ func TestGetKubeconfigForContext(t *testing.T) {
cluster = kubeconfig.GetCluster(&kc, "tanzu-cli-mytanzu/current")
assert.Equal(t, cluster.Cluster.Server, c.ClusterOpts.Endpoint+"/project/project2")

// Test getting the kubeconfig for a clustergroup within a project
kubeconfigBytes, err = GetKubeconfigForContext(c.Name, ForProject("project2"), ForClusterGroup("clustergroup1"))
assert.NoError(t, err)
c, err = GetContext("test-tanzu")
assert.NoError(t, err)
err = yaml.Unmarshal(kubeconfigBytes, &kc)
assert.NoError(t, err)
cluster = kubeconfig.GetCluster(&kc, "tanzu-cli-mytanzu/current")
assert.Equal(t, cluster.Cluster.Server, c.ClusterOpts.Endpoint+"/project/project2/clustergroup/clustergroup1")

// Test getting the kubeconfig with incorrect resource combination (request kubeconfig for space and clustergroup)
c, err = GetContext("test-tanzu")
assert.NoError(t, err)
_, err = GetKubeconfigForContext(c.Name, ForProject("project2"), ForSpace("space1"), ForClusterGroup("clustergroup1"))
assert.Error(t, err)
assert.ErrorContains(t, err, "incorrect resource options provided. Both space and clustergroup are set but only one can be set")

// Test getting the kubeconfig for an arbitrary Tanzu resource for non Tanzu context
nonTanzuCtx, err := GetContext("test-mc")
assert.NoError(t, err)
Expand Down

0 comments on commit 450decc

Please sign in to comment.