|
| 1 | +package schema |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + "text/template" |
| 8 | +) |
| 9 | + |
| 10 | +func TestKubeconfigTemplate(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + config *Kubeconfig |
| 14 | + wantAuth string // Expected auth section in output |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "token authentication", |
| 18 | + config: &Kubeconfig{ |
| 19 | + Current: 0, |
| 20 | + Clusters: []*KubeconfgCluster{ |
| 21 | + { |
| 22 | + Context: "test-cluster", |
| 23 | + Host: "https://kubernetes.example.com", |
| 24 | + Cert: "LS0tLS1CRUdJTi1DRVJUSUZJQ0FURS0tLS0t", |
| 25 | + Token: "test-token-12345", |
| 26 | + ExecApiVersion: "client.authentication.k8s.io/v1beta1", |
| 27 | + }, |
| 28 | + }, |
| 29 | + }, |
| 30 | + wantAuth: "token: test-token-12345", |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "exec authentication", |
| 34 | + config: &Kubeconfig{ |
| 35 | + Current: 0, |
| 36 | + Clusters: []*KubeconfgCluster{ |
| 37 | + { |
| 38 | + Context: "test-cluster", |
| 39 | + Host: "https://kubernetes.example.com", |
| 40 | + Cert: "LS0tLS1CRUdJTi1DRVJUSUZJQ0FURS0tLS0t", |
| 41 | + ExecCommand: "kubectl", |
| 42 | + ExecArgs: []string{"get-token"}, |
| 43 | + ExecApiVersion: "client.authentication.k8s.io/v1beta1", |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + wantAuth: "command: kubectl", |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "exec takes priority over token", |
| 51 | + config: &Kubeconfig{ |
| 52 | + Current: 0, |
| 53 | + Clusters: []*KubeconfgCluster{ |
| 54 | + { |
| 55 | + Context: "test-cluster", |
| 56 | + Host: "https://kubernetes.example.com", |
| 57 | + Cert: "LS0tLS1CRUdJTi1DRVJUSUZJQ0FURS0tLS0t", |
| 58 | + Token: "test-token-12345", |
| 59 | + ExecCommand: "kubectl", |
| 60 | + ExecArgs: []string{"get-token"}, |
| 61 | + ExecApiVersion: "client.authentication.k8s.io/v1beta1", |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + wantAuth: "command: kubectl", |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "no authentication", |
| 69 | + config: &Kubeconfig{ |
| 70 | + Current: 0, |
| 71 | + Clusters: []*KubeconfgCluster{ |
| 72 | + { |
| 73 | + Context: "test-cluster", |
| 74 | + Host: "https://kubernetes.example.com", |
| 75 | + Cert: "LS0tLS1CRUdJTi1DRVJUSUZJQ0FURS0tLS0t", |
| 76 | + ExecApiVersion: "client.authentication.k8s.io/v1beta1", |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + wantAuth: "", // No auth expected |
| 81 | + }, |
| 82 | + } |
| 83 | + |
| 84 | + for _, tt := range tests { |
| 85 | + t.Run(tt.name, func(t *testing.T) { |
| 86 | + // Normalize exec args |
| 87 | + for _, c := range tt.config.Clusters { |
| 88 | + c.ExecArgs = normalizeExecArgs(c.ExecArgs) |
| 89 | + } |
| 90 | + |
| 91 | + // Render template |
| 92 | + tmpl, err := template.New("k").Parse(kubeconfigTemplate) |
| 93 | + if err != nil { |
| 94 | + t.Fatalf("template.Parse() error = %v", err) |
| 95 | + } |
| 96 | + |
| 97 | + var buf bytes.Buffer |
| 98 | + err = tmpl.Execute(&buf, tt.config) |
| 99 | + if err != nil { |
| 100 | + t.Fatalf("template.Execute() error = %v", err) |
| 101 | + } |
| 102 | + |
| 103 | + output := buf.String() |
| 104 | + |
| 105 | + // Check for expected auth if specified |
| 106 | + if tt.wantAuth != "" { |
| 107 | + if !strings.Contains(output, tt.wantAuth) { |
| 108 | + t.Errorf("output missing expected auth: %q\nGot:\n%s", tt.wantAuth, output) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // Ensure valid YAML structure |
| 113 | + if !strings.Contains(output, "apiVersion: v1") { |
| 114 | + t.Errorf("output missing apiVersion") |
| 115 | + } |
| 116 | + if !strings.Contains(output, "kind: Config") { |
| 117 | + t.Errorf("output missing kind") |
| 118 | + } |
| 119 | + if !strings.Contains(output, "current-context: test-cluster") { |
| 120 | + t.Errorf("output missing current-context") |
| 121 | + } |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func TestNormalizeExecArgs(t *testing.T) { |
| 127 | + tests := []struct { |
| 128 | + name string |
| 129 | + args interface{} |
| 130 | + want []string |
| 131 | + }{ |
| 132 | + { |
| 133 | + name: "nil args", |
| 134 | + args: nil, |
| 135 | + want: nil, |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "string slice", |
| 139 | + args: []string{"arg1", "arg2"}, |
| 140 | + want: []string{"arg1", "arg2"}, |
| 141 | + }, |
| 142 | + { |
| 143 | + name: "interface slice", |
| 144 | + args: []interface{}{"arg1", "arg2", 123}, |
| 145 | + want: []string{"arg1", "arg2", "123"}, |
| 146 | + }, |
| 147 | + { |
| 148 | + name: "JSON array string", |
| 149 | + args: `["arg1", "arg2"]`, |
| 150 | + want: []string{"arg1", "arg2"}, |
| 151 | + }, |
| 152 | + { |
| 153 | + name: "Go slice string", |
| 154 | + args: "[arg1 arg2 arg3]", |
| 155 | + want: []string{"arg1", "arg2", "arg3"}, |
| 156 | + }, |
| 157 | + { |
| 158 | + name: "single string", |
| 159 | + args: "single-arg", |
| 160 | + want: []string{"single-arg"}, |
| 161 | + }, |
| 162 | + { |
| 163 | + name: "empty string", |
| 164 | + args: "", |
| 165 | + want: nil, |
| 166 | + }, |
| 167 | + } |
| 168 | + |
| 169 | + for _, tt := range tests { |
| 170 | + t.Run(tt.name, func(t *testing.T) { |
| 171 | + got := normalizeExecArgs(tt.args) |
| 172 | + if len(got) != len(tt.want) { |
| 173 | + t.Errorf("normalizeExecArgs() = %v, want %v", got, tt.want) |
| 174 | + return |
| 175 | + } |
| 176 | + for i := range got { |
| 177 | + if got[i] != tt.want[i] { |
| 178 | + t.Errorf("normalizeExecArgs()[%d] = %v, want %v", i, got[i], tt.want[i]) |
| 179 | + } |
| 180 | + } |
| 181 | + }) |
| 182 | + } |
| 183 | +} |
0 commit comments