|  | 
|  | 1 | +package kubernetes | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"errors" | 
|  | 5 | +	"k8s.io/client-go/rest" | 
|  | 6 | +	"os" | 
|  | 7 | +	"path" | 
|  | 8 | +	"strings" | 
|  | 9 | +	"testing" | 
|  | 10 | +) | 
|  | 11 | + | 
|  | 12 | +func TestKubernetes_IsInCluster(t *testing.T) { | 
|  | 13 | +	t.Run("with explicit kubeconfig", func(t *testing.T) { | 
|  | 14 | +		k := Kubernetes{ | 
|  | 15 | +			Kubeconfig: "kubeconfig", | 
|  | 16 | +		} | 
|  | 17 | +		if k.IsInCluster() { | 
|  | 18 | +			t.Errorf("expected not in cluster, got in cluster") | 
|  | 19 | +		} | 
|  | 20 | +	}) | 
|  | 21 | +	t.Run("with empty kubeconfig and in cluster", func(t *testing.T) { | 
|  | 22 | +		originalFunction := InClusterConfig | 
|  | 23 | +		InClusterConfig = func() (*rest.Config, error) { | 
|  | 24 | +			return &rest.Config{}, nil | 
|  | 25 | +		} | 
|  | 26 | +		defer func() { | 
|  | 27 | +			InClusterConfig = originalFunction | 
|  | 28 | +		}() | 
|  | 29 | +		k := Kubernetes{ | 
|  | 30 | +			Kubeconfig: "", | 
|  | 31 | +		} | 
|  | 32 | +		if !k.IsInCluster() { | 
|  | 33 | +			t.Errorf("expected in cluster, got not in cluster") | 
|  | 34 | +		} | 
|  | 35 | +	}) | 
|  | 36 | +	t.Run("with empty kubeconfig and not in cluster (empty)", func(t *testing.T) { | 
|  | 37 | +		originalFunction := InClusterConfig | 
|  | 38 | +		InClusterConfig = func() (*rest.Config, error) { | 
|  | 39 | +			return nil, nil | 
|  | 40 | +		} | 
|  | 41 | +		defer func() { | 
|  | 42 | +			InClusterConfig = originalFunction | 
|  | 43 | +		}() | 
|  | 44 | +		k := Kubernetes{ | 
|  | 45 | +			Kubeconfig: "", | 
|  | 46 | +		} | 
|  | 47 | +		if k.IsInCluster() { | 
|  | 48 | +			t.Errorf("expected not in cluster, got in cluster") | 
|  | 49 | +		} | 
|  | 50 | +	}) | 
|  | 51 | +	t.Run("with empty kubeconfig and not in cluster (error)", func(t *testing.T) { | 
|  | 52 | +		originalFunction := InClusterConfig | 
|  | 53 | +		InClusterConfig = func() (*rest.Config, error) { | 
|  | 54 | +			return nil, errors.New("error") | 
|  | 55 | +		} | 
|  | 56 | +		defer func() { | 
|  | 57 | +			InClusterConfig = originalFunction | 
|  | 58 | +		}() | 
|  | 59 | +		k := Kubernetes{ | 
|  | 60 | +			Kubeconfig: "", | 
|  | 61 | +		} | 
|  | 62 | +		if k.IsInCluster() { | 
|  | 63 | +			t.Errorf("expected not in cluster, got in cluster") | 
|  | 64 | +		} | 
|  | 65 | +	}) | 
|  | 66 | +} | 
|  | 67 | + | 
|  | 68 | +func TestKubernetes_ResolveKubernetesConfigurations_Explicit(t *testing.T) { | 
|  | 69 | +	t.Run("with missing file", func(t *testing.T) { | 
|  | 70 | +		tempDir := t.TempDir() | 
|  | 71 | +		k := Kubernetes{Kubeconfig: path.Join(tempDir, "config")} | 
|  | 72 | +		err := resolveKubernetesConfigurations(&k) | 
|  | 73 | +		if err == nil { | 
|  | 74 | +			t.Errorf("expected error, got nil") | 
|  | 75 | +		} | 
|  | 76 | +		if !errors.Is(err, os.ErrNotExist) { | 
|  | 77 | +			t.Errorf("expected file not found error, got %v", err) | 
|  | 78 | +		} | 
|  | 79 | +		if !strings.HasSuffix(err.Error(), ": no such file or directory") { | 
|  | 80 | +			t.Errorf("expected file not found error, got %v", err) | 
|  | 81 | +		} | 
|  | 82 | +	}) | 
|  | 83 | +	t.Run("with empty file", func(t *testing.T) { | 
|  | 84 | +		tempDir := t.TempDir() | 
|  | 85 | +		kubeconfigPath := path.Join(tempDir, "config") | 
|  | 86 | +		if err := os.WriteFile(kubeconfigPath, []byte(""), 0644); err != nil { | 
|  | 87 | +			t.Fatalf("failed to create kubeconfig file: %v", err) | 
|  | 88 | +		} | 
|  | 89 | +		k := Kubernetes{Kubeconfig: kubeconfigPath} | 
|  | 90 | +		err := resolveKubernetesConfigurations(&k) | 
|  | 91 | +		if err == nil { | 
|  | 92 | +			t.Errorf("expected error, got nil") | 
|  | 93 | +		} | 
|  | 94 | +		if !strings.Contains(err.Error(), "no configuration has been provided") { | 
|  | 95 | +			t.Errorf("expected no kubeconfig error, got %v", err) | 
|  | 96 | +		} | 
|  | 97 | +	}) | 
|  | 98 | +	t.Run("with valid file", func(t *testing.T) { | 
|  | 99 | +		tempDir := t.TempDir() | 
|  | 100 | +		kubeconfigPath := path.Join(tempDir, "config") | 
|  | 101 | +		kubeconfigContent := ` | 
|  | 102 | +apiVersion: v1 | 
|  | 103 | +kind: Config | 
|  | 104 | +clusters: | 
|  | 105 | +- cluster: | 
|  | 106 | +    server: https://example.com | 
|  | 107 | +  name: example-cluster | 
|  | 108 | +contexts: | 
|  | 109 | +- context: | 
|  | 110 | +    cluster: example-cluster | 
|  | 111 | +    user: example-user | 
|  | 112 | +  name: example-context | 
|  | 113 | +current-context: example-context | 
|  | 114 | +users: | 
|  | 115 | +- name: example-user | 
|  | 116 | +  user: | 
|  | 117 | +    token: example-token | 
|  | 118 | +` | 
|  | 119 | +		if err := os.WriteFile(kubeconfigPath, []byte(kubeconfigContent), 0644); err != nil { | 
|  | 120 | +			t.Fatalf("failed to create kubeconfig file: %v", err) | 
|  | 121 | +		} | 
|  | 122 | +		k := Kubernetes{Kubeconfig: kubeconfigPath} | 
|  | 123 | +		err := resolveKubernetesConfigurations(&k) | 
|  | 124 | +		if err != nil { | 
|  | 125 | +			t.Fatalf("expected no error, got %v", err) | 
|  | 126 | +		} | 
|  | 127 | +		if k.cfg == nil { | 
|  | 128 | +			t.Errorf("expected non-nil config, got nil") | 
|  | 129 | +		} | 
|  | 130 | +		if k.cfg.Host != "https://example.com" { | 
|  | 131 | +			t.Errorf("expected host https://example.com, got %s", k.cfg.Host) | 
|  | 132 | +		} | 
|  | 133 | +	}) | 
|  | 134 | +} | 
0 commit comments