Skip to content

Commit

Permalink
Added unit test for mergeConfigFile function
Browse files Browse the repository at this point in the history
Signed-off-by: Gunjan Vyas <[email protected]>
  • Loading branch information
vyasgun authored and cfergeau committed Dec 11, 2023
1 parent 382412c commit 6d02b38
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/crc/machine/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ func writeKubeconfig(ip string, clusterConfig *types.ClusterConfig, ingressHTTPS

func getGlobalKubeConfig() (string, *api.Config, error) {
kubeconfig := getGlobalKubeConfigPath()
return getKubeConfigFromFile(kubeconfig)
}

func getKubeConfigFromFile(kubeconfig string) (string, *api.Config, error) {
config, err := clientcmd.LoadFromFile(kubeconfig)
if err != nil && !os.IsNotExist(err) {
return "", nil, err
Expand Down Expand Up @@ -246,7 +250,12 @@ func contains(arr []string, str string) bool {
}

func mergeKubeConfigFile(kubeConfigFile string) error {
globalConfigPath, globalConf, err := getGlobalKubeConfig()
return mergeConfigHelper(kubeConfigFile, getGlobalKubeConfigPath())
}

func mergeConfigHelper(kubeConfigFile, globalConfigFile string) error {

globalConfigPath, globalConf, err := getKubeConfigFromFile(globalConfigFile)
if err != nil {
return err
}
Expand Down
62 changes: 62 additions & 0 deletions pkg/crc/machine/kubeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"

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

var dummyKubeconfigFileContent = `apiVersion: v1
Expand Down Expand Up @@ -59,3 +61,63 @@ func TestUpdateUserCaAndKeyToKubeconfig(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "dummycert", userClientCA)
}

func createTempKubeConfig(config *api.Config) (string, error) {
tempFile, err := os.CreateTemp("", "kubeconfig-")
if err != nil {
return "", err
}
path := tempFile.Name()

err = clientcmd.WriteToFile(*config, path)
if err != nil {
os.Remove(path)
return "", err
}

return path, nil
}

func Test_mergeKubeConfigFile(t *testing.T) {
// Define two simple configurations
primaryConfig := api.NewConfig()
primaryConfig.Clusters["primary-cluster"] = &api.Cluster{Server: "https://primary.example.com"}
primaryConfig.AuthInfos["primary-user"] = &api.AuthInfo{Token: "primary-token"}
primaryConfig.Contexts["primary-context"] = &api.Context{Cluster: "primary-cluster", AuthInfo: "primary-user"}
primaryConfig.CurrentContext = "primary-context"

secondaryConfig := api.NewConfig()
secondaryConfig.Clusters["secondary-cluster"] = &api.Cluster{Server: "https://secondary.example.com"}
secondaryConfig.AuthInfos["secondary-user"] = &api.AuthInfo{Token: "secondary-token"}
secondaryConfig.Contexts["secondary-context"] = &api.Context{Cluster: "secondary-cluster", AuthInfo: "secondary-user"}
secondaryConfig.CurrentContext = "secondary-context"

// Create temporary kubeconfig files for the primary and secondary configurations
primaryConfigPath, err := createTempKubeConfig(primaryConfig)
assert.NoError(t, err, "failed to create temporary kubeconfig file")
defer os.Remove(primaryConfigPath)

secondaryConfigPath, err := createTempKubeConfig(secondaryConfig)
assert.NoError(t, err, "failed to create temporary kubeconfig file")
defer os.Remove(secondaryConfigPath)

err = mergeConfigHelper(secondaryConfigPath, primaryConfigPath)
assert.NoError(t, err, "failed to modify kubeconfig")

// Load the modified kubeconfig to ensure it was merged correctly
mergedConfig, err := clientcmd.LoadFromFile(primaryConfigPath)
assert.NoError(t, err, "failed to load merged kubeconfig")

for _, config := range []*api.Config{primaryConfig, secondaryConfig} {

for key := range config.AuthInfos {
assert.Contains(t, mergedConfig.AuthInfos, key, "expected authInfo not found")
}
for key := range config.Clusters {
assert.Contains(t, mergedConfig.Clusters, key, "expected cluster not found")
}
for key := range config.Contexts {
assert.Contains(t, mergedConfig.Contexts, key, "expected context not found")
}
}
}

0 comments on commit 6d02b38

Please sign in to comment.