github.com/grahambrereton-form3/tilt@v0.10.18/internal/k8s/env_test.go (about) 1 package k8s 2 3 import ( 4 "context" 5 "path/filepath" 6 "testing" 7 8 "github.com/mitchellh/go-homedir" 9 "github.com/stretchr/testify/assert" 10 "k8s.io/client-go/tools/clientcmd/api" 11 ) 12 13 type expectedEnv struct { 14 expected Env 15 string 16 } 17 18 type expectedConfig struct { 19 expected Env 20 input *api.Config 21 } 22 23 func TestProvideEnv(t *testing.T) { 24 minikubeContexts := map[string]*api.Context{ 25 "minikube": &api.Context{ 26 Cluster: "minikube", 27 }, 28 } 29 dockerDesktopContexts := map[string]*api.Context{ 30 "docker-for-desktop": &api.Context{ 31 Cluster: "docker-for-desktop-cluster", 32 }, 33 } 34 dockerDesktopEdgeContexts := map[string]*api.Context{ 35 "docker-for-desktop": &api.Context{ 36 Cluster: "docker-desktop", 37 }, 38 } 39 gkeContexts := map[string]*api.Context{ 40 "gke_blorg-dev_us-central1-b_blorg": &api.Context{ 41 Cluster: "gke_blorg-dev_us-central1-b_blorg", 42 }, 43 } 44 kindContexts := map[string]*api.Context{ 45 "kubernetes-admin@kind-1": &api.Context{ 46 Cluster: "kind", 47 }, 48 } 49 microK8sContexts := map[string]*api.Context{ 50 "microk8s": &api.Context{ 51 Cluster: "microk8s-cluster", 52 }, 53 } 54 55 homedir, err := homedir.Dir() 56 assert.NoError(t, err) 57 k3dContexts := map[string]*api.Context{ 58 "default": &api.Context{ 59 LocationOfOrigin: filepath.Join(homedir, ".config", "k3d", "k3s-default", "kubeconfig.yaml"), 60 Cluster: "default", 61 }, 62 } 63 kindNamedClusterContexts := map[string]*api.Context{ 64 "default": &api.Context{ 65 LocationOfOrigin: filepath.Join(homedir, ".kube", "kind-config-integration"), 66 Cluster: "integration", 67 }, 68 } 69 table := []expectedConfig{ 70 {EnvNone, &api.Config{}}, 71 {EnvUnknown, &api.Config{CurrentContext: "aws"}}, 72 {EnvMinikube, &api.Config{CurrentContext: "minikube", Contexts: minikubeContexts}}, 73 {EnvDockerDesktop, &api.Config{CurrentContext: "docker-for-desktop", Contexts: dockerDesktopContexts}}, 74 {EnvDockerDesktop, &api.Config{CurrentContext: "docker-for-desktop", Contexts: dockerDesktopEdgeContexts}}, 75 {EnvGKE, &api.Config{CurrentContext: "gke_blorg-dev_us-central1-b_blorg", Contexts: gkeContexts}}, 76 {EnvKIND, &api.Config{CurrentContext: "kubernetes-admin@kind-1", Contexts: kindContexts}}, 77 {EnvMicroK8s, &api.Config{CurrentContext: "microk8s", Contexts: microK8sContexts}}, 78 {EnvK3D, &api.Config{CurrentContext: "default", Contexts: k3dContexts}}, 79 {EnvKIND, &api.Config{CurrentContext: "default", Contexts: kindNamedClusterContexts}}, 80 } 81 82 for _, tt := range table { 83 t.Run(tt.input.CurrentContext, func(t *testing.T) { 84 actual := ProvideEnv(context.Background(), tt.input) 85 if actual != tt.expected { 86 t.Errorf("Expected %s, actual %s", tt.expected, actual) 87 } 88 }) 89 } 90 }