Skip to content

Commit

Permalink
test: add unit test for AddToLocal (#685)
Browse files Browse the repository at this point in the history
* test: add unit test for AddToLocal
  • Loading branch information
sunny0826 authored Apr 23, 2023
1 parent a4c571f commit 27ad7f8
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions cmd/add_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package cmd

import (
"os"
"testing"

"k8s.io/client-go/tools/clientcmd"

clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)

Expand All @@ -23,6 +26,7 @@ var (
"federal-context": {AuthInfo: "red-user", Cluster: "cow-cluster", Namespace: "hammer-ns"},
"not-exist-context": {AuthInfo: "not-exist", Cluster: "not-exist", Namespace: "not-exist-ns"},
},
CurrentContext: "root-context",
}
oldTestConfig = clientcmdapi.Config{
AuthInfos: map[string]*clientcmdapi.AuthInfo{
Expand Down Expand Up @@ -221,3 +225,58 @@ func TestKubeConfig_handleContexts(t *testing.T) {
})
}
}

func TestAddToLocal(t *testing.T) {
localFile, err := os.CreateTemp("", "local-kubeconfig-*.yaml")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
cfgFile = "test"
// Create a new temporary file
tempFile, err := os.CreateTemp("", "temp-kubeconfig-*.yaml")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tempFile.Name())
defer os.Remove(localFile.Name())

// Write an initial empty config to the temp file
emptyConfig := clientcmdapi.NewConfig()
err = clientcmd.WriteToFile(*emptyConfig, tempFile.Name())
if err != nil {
t.Fatalf("Failed to write empty config to temp file: %v", err)
}
tempFile.Close()

err = clientcmd.WriteToFile(addTestConfig, localFile.Name())
if err != nil {
t.Fatalf("Failed to write empty config to temp file: %v", err)
}
localFile.Close()

cfgFile = localFile.Name()

// Mock configuration
newConfig := &clientcmdapi.Config{
Clusters: map[string]*clientcmdapi.Cluster{"test-cluster": {Server: "https://test-cluster"}},
AuthInfos: map[string]*clientcmdapi.AuthInfo{"test-authinfo": {Token: "black-token"}},
Contexts: map[string]*clientcmdapi.Context{"test-context": {AuthInfo: "test-authinfo", Cluster: "test-cluster", Namespace: "hammer-ns"}},
CurrentContext: "test-context",
}

// Test AddToLocal function
err = AddToLocal(newConfig, tempFile.Name(), "", true)
if err != nil {
t.Fatalf("Failed to add to local: %v", err)
}

// Read the file and check if the new configuration is added
loadedConfig, err := clientcmd.LoadFromFile(localFile.Name())
if err != nil {
t.Fatalf("Failed to load config from file: %v", err)
}

if _, ok := loadedConfig.Contexts["test-context"]; !ok {
t.Fatalf("Failed to find 'test-context' in the loaded config")
}
}

0 comments on commit 27ad7f8

Please sign in to comment.