Skip to content

Commit

Permalink
Expanded test coverage for ekssupport_test.TestGetContextName with re…
Browse files Browse the repository at this point in the history
…spect to non-ARN context names; added SetConnectedToCluster to tearDown to make test results more predictable.
  • Loading branch information
comrumino committed Sep 19, 2024
1 parent f7aa2d9 commit 79869ab
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
66 changes: 66 additions & 0 deletions cloudsupport/v1/ekssupport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import (
"strings"
"testing"

"github.com/kubescape/k8s-interface/k8sinterface"
"github.com/stretchr/testify/assert"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)

func TestGetContextName(t *testing.T) {
defer tearDown()

// Test ARN context names
mockname1 := "arn:aws:eks:eu-north-1:123456789:cluster-test-cluster"
eksSupport := NewEKSSupport()
name := eksSupport.GetContextName(mockname1)
Expand All @@ -25,6 +30,67 @@ func TestGetContextName(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "eu-north-1", region)

// Test non-ARN context names (i.e., cluster name is context name)
tests := []struct {
name string
config *clientcmdapi.Config
connected bool
cluster string
expectedContextName string
}{
{
name: "Config is empty, return empty string",
config: &clientcmdapi.Config{
CurrentContext: "d34db33f",
Clusters: map[string]*clientcmdapi.Cluster{
"d34db33f": {
Server: "https://my-server.local",
},
},
},
connected: true,
cluster: "my-cluster",
expectedContextName: "",
},
{
name: "Context name is cluster name, connected",
config: &clientcmdapi.Config{
CurrentContext: "my-cluster",
Clusters: map[string]*clientcmdapi.Cluster{
"my-cluster": {
Server: "https://my-server.local",
},
},
},
connected: true,
cluster: "my-cluster",
expectedContextName: "my-cluster",
},
{
name: "Context name is cluster name, not connected",
config: &clientcmdapi.Config{
CurrentContext: "my-cluster",
Clusters: map[string]*clientcmdapi.Cluster{
"my-cluster": {
Server: "https://my-server.local",
},
},
},
connected: false,
cluster: "my-cluster",
expectedContextName: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k8sinterface.SetConnectedToCluster(tt.connected)
k8sinterface.SetClientConfigAPI(tt.config)
actualContextName := eksSupport.GetContextName(tt.cluster)
assert.Equal(t, tt.expectedContextName, actualContextName)
})
}

}

func TestGetRegion(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions k8sinterface/k8sconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func TestGetContext(t *testing.T) {
}

func tearDown() {
SetConnectedToCluster(true)
SetClientConfigAPI(nil)
SetClusterContextName("")
SetConfigClusterServerName("")
Expand Down

0 comments on commit 79869ab

Please sign in to comment.