github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/pkg/cli/environment_test.go (about) 1 /* 2 Copyright The Helm Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package cli 18 19 import ( 20 "os" 21 "reflect" 22 "strings" 23 "testing" 24 25 "github.com/spf13/pflag" 26 ) 27 28 func TestSetNamespace(t *testing.T) { 29 settings := New() 30 31 if settings.namespace != "" { 32 t.Errorf("Expected empty namespace, got %s", settings.namespace) 33 } 34 35 settings.SetNamespace("testns") 36 if settings.namespace != "testns" { 37 t.Errorf("Expected namespace testns, got %s", settings.namespace) 38 } 39 40 } 41 42 func TestEnvSettings(t *testing.T) { 43 tests := []struct { 44 name string 45 46 // input 47 args string 48 envvars map[string]string 49 50 // expected values 51 ns, kcontext string 52 debug bool 53 maxhistory int 54 kubeAsUser string 55 kubeAsGroups []string 56 kubeCaFile string 57 kubeInsecure bool 58 kubeTLSServer string 59 burstLimit int 60 }{ 61 { 62 name: "defaults", 63 ns: "default", 64 maxhistory: defaultMaxHistory, 65 burstLimit: defaultBurstLimit, 66 }, 67 { 68 name: "with flags set", 69 args: "--debug --namespace=myns --kube-as-user=poro --kube-as-group=admins --kube-as-group=teatime --kube-as-group=snackeaters --kube-ca-file=/tmp/ca.crt --burst-limit 100 --kube-insecure-skip-tls-verify=true --kube-tls-server-name=example.org", 70 ns: "myns", 71 debug: true, 72 maxhistory: defaultMaxHistory, 73 burstLimit: 100, 74 kubeAsUser: "poro", 75 kubeAsGroups: []string{"admins", "teatime", "snackeaters"}, 76 kubeCaFile: "/tmp/ca.crt", 77 kubeTLSServer: "example.org", 78 kubeInsecure: true, 79 }, 80 { 81 name: "with envvars set", 82 envvars: map[string]string{"HELM_DEBUG": "1", "HELM_NAMESPACE": "yourns", "HELM_KUBEASUSER": "pikachu", "HELM_KUBEASGROUPS": ",,,operators,snackeaters,partyanimals", "HELM_MAX_HISTORY": "5", "HELM_KUBECAFILE": "/tmp/ca.crt", "HELM_BURST_LIMIT": "150", "HELM_KUBEINSECURE_SKIP_TLS_VERIFY": "true", "HELM_KUBETLS_SERVER_NAME": "example.org"}, 83 ns: "yourns", 84 maxhistory: 5, 85 burstLimit: 150, 86 debug: true, 87 kubeAsUser: "pikachu", 88 kubeAsGroups: []string{"operators", "snackeaters", "partyanimals"}, 89 kubeCaFile: "/tmp/ca.crt", 90 kubeTLSServer: "example.org", 91 kubeInsecure: true, 92 }, 93 { 94 name: "with flags and envvars set", 95 args: "--debug --namespace=myns --kube-as-user=poro --kube-as-group=admins --kube-as-group=teatime --kube-as-group=snackeaters --kube-ca-file=/my/ca.crt --burst-limit 175 --kube-insecure-skip-tls-verify=true --kube-tls-server-name=example.org", 96 envvars: map[string]string{"HELM_DEBUG": "1", "HELM_NAMESPACE": "yourns", "HELM_KUBEASUSER": "pikachu", "HELM_KUBEASGROUPS": ",,,operators,snackeaters,partyanimals", "HELM_MAX_HISTORY": "5", "HELM_KUBECAFILE": "/tmp/ca.crt", "HELM_BURST_LIMIT": "200", "HELM_KUBEINSECURE_SKIP_TLS_VERIFY": "true", "HELM_KUBETLS_SERVER_NAME": "example.org"}, 97 ns: "myns", 98 debug: true, 99 maxhistory: 5, 100 burstLimit: 175, 101 kubeAsUser: "poro", 102 kubeAsGroups: []string{"admins", "teatime", "snackeaters"}, 103 kubeCaFile: "/my/ca.crt", 104 kubeTLSServer: "example.org", 105 kubeInsecure: true, 106 }, 107 } 108 109 for _, tt := range tests { 110 t.Run(tt.name, func(t *testing.T) { 111 defer resetEnv()() 112 113 for k, v := range tt.envvars { 114 os.Setenv(k, v) 115 } 116 117 flags := pflag.NewFlagSet("testing", pflag.ContinueOnError) 118 119 settings := New() 120 settings.AddFlags(flags) 121 flags.Parse(strings.Split(tt.args, " ")) 122 123 if settings.Debug != tt.debug { 124 t.Errorf("expected debug %t, got %t", tt.debug, settings.Debug) 125 } 126 if settings.Namespace() != tt.ns { 127 t.Errorf("expected namespace %q, got %q", tt.ns, settings.Namespace()) 128 } 129 if settings.KubeContext != tt.kcontext { 130 t.Errorf("expected kube-context %q, got %q", tt.kcontext, settings.KubeContext) 131 } 132 if settings.MaxHistory != tt.maxhistory { 133 t.Errorf("expected maxHistory %d, got %d", tt.maxhistory, settings.MaxHistory) 134 } 135 if tt.kubeAsUser != settings.KubeAsUser { 136 t.Errorf("expected kAsUser %q, got %q", tt.kubeAsUser, settings.KubeAsUser) 137 } 138 if !reflect.DeepEqual(tt.kubeAsGroups, settings.KubeAsGroups) { 139 t.Errorf("expected kAsGroups %+v, got %+v", len(tt.kubeAsGroups), len(settings.KubeAsGroups)) 140 } 141 if tt.kubeCaFile != settings.KubeCaFile { 142 t.Errorf("expected kCaFile %q, got %q", tt.kubeCaFile, settings.KubeCaFile) 143 } 144 if tt.burstLimit != settings.BurstLimit { 145 t.Errorf("expected BurstLimit %d, got %d", tt.burstLimit, settings.BurstLimit) 146 } 147 if tt.kubeInsecure != settings.KubeInsecureSkipTLSVerify { 148 t.Errorf("expected kubeInsecure %t, got %t", tt.kubeInsecure, settings.KubeInsecureSkipTLSVerify) 149 } 150 if tt.kubeTLSServer != settings.KubeTLSServerName { 151 t.Errorf("expected kubeTLSServer %q, got %q", tt.kubeTLSServer, settings.KubeTLSServerName) 152 } 153 }) 154 } 155 } 156 157 func TestEnvOrBool(t *testing.T) { 158 const envName = "TEST_ENV_OR_BOOL" 159 tests := []struct { 160 name string 161 env string 162 val string 163 def bool 164 expected bool 165 }{ 166 { 167 name: "unset with default false", 168 def: false, 169 expected: false, 170 }, 171 { 172 name: "unset with default true", 173 def: true, 174 expected: true, 175 }, 176 { 177 name: "blank env with default false", 178 env: envName, 179 def: false, 180 expected: false, 181 }, 182 { 183 name: "blank env with default true", 184 env: envName, 185 def: true, 186 expected: true, 187 }, 188 { 189 name: "env true with default false", 190 env: envName, 191 val: "true", 192 def: false, 193 expected: true, 194 }, 195 { 196 name: "env false with default true", 197 env: envName, 198 val: "false", 199 def: true, 200 expected: false, 201 }, 202 { 203 name: "env fails parsing with default true", 204 env: envName, 205 val: "NOT_A_BOOL", 206 def: true, 207 expected: true, 208 }, 209 { 210 name: "env fails parsing with default false", 211 env: envName, 212 val: "NOT_A_BOOL", 213 def: false, 214 expected: false, 215 }, 216 } 217 218 for _, tt := range tests { 219 t.Run(tt.name, func(t *testing.T) { 220 if tt.env != "" { 221 t.Cleanup(func() { 222 os.Unsetenv(tt.env) 223 }) 224 os.Setenv(tt.env, tt.val) 225 } 226 actual := envBoolOr(tt.env, tt.def) 227 if actual != tt.expected { 228 t.Errorf("expected result %t, got %t", tt.expected, actual) 229 } 230 }) 231 } 232 } 233 234 func resetEnv() func() { 235 origEnv := os.Environ() 236 237 // ensure any local envvars do not hose us 238 for e := range New().EnvVars() { 239 os.Unsetenv(e) 240 } 241 242 return func() { 243 for _, pair := range origEnv { 244 kv := strings.SplitN(pair, "=", 2) 245 os.Setenv(kv[0], kv[1]) 246 } 247 } 248 }