Skip to content

Commit

Permalink
Add more complex tests
Browse files Browse the repository at this point in the history
  • Loading branch information
burmanm committed Apr 23, 2024
1 parent f324ac4 commit f61cbf1
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions pkg/kubernetes/config_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,61 @@
package kubernetes

import (
"math/rand"
"os/user"
"path/filepath"
"reflect"
"strings"
"testing"

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

func TestLoadConfigurationContext(t *testing.T) {
t.Skip("requires kind cluster with defaults to be running")
require := require.New(t)

u, err := user.Current()
conf, err := clientcmd.NewDefaultClientConfigLoadingRules().Load()
require.NoError(err)
cfgFile := filepath.Join(u.HomeDir, ".kube", "config")

restConfig, err := LoadClientConfig(cfgFile, "kind-kind")
if _, found := conf.Contexts["kind-kind"]; !found {
t.Skip("kind-kind context not found in kubeconfig")
}

restConfig, err := LoadClientConfig(defaultConfigPath(t), "kind-kind")
require.NoError(err)

// mTLS auth has KeyData & CertData
require.NotNil(restConfig.KeyData)
require.NotNil(restConfig.CertData)
require.NotNil(restConfig.CAData)
require.True(strings.HasPrefix(restConfig.Host, "https://127.0.0.1"))
}

func TestVerifyContextWasChanged(t *testing.T) {
require := require.New(t)

conf, err := clientcmd.NewDefaultClientConfigLoadingRules().Load()
require.NoError(err)

if len(conf.Contexts) < 1 {
t.Skip("no context found in kubeconfig")
}

keys := reflect.ValueOf(conf.Contexts).MapKeys()
randomContextName := keys[rand.Intn(len(keys))].String()

randomContext := conf.Contexts[randomContextName]
randomServer := conf.Clusters[randomContext.Cluster].Server

restConfig, err := LoadClientConfig(defaultConfigPath(t), randomContextName)
require.NoError(err)
require.NotNil(restConfig.CAData)
require.Equal(randomServer, restConfig.Host)
}

func defaultConfigPath(t *testing.T) string {
u, err := user.Current()
require.NoError(t, err)
return filepath.Join(u.HomeDir, ".kube", "config")
}

0 comments on commit f61cbf1

Please sign in to comment.