github.com/koderover/helm@v2.17.0+incompatible/pkg/helm/environment/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 environment 18 19 import ( 20 "os" 21 "strings" 22 "testing" 23 24 "k8s.io/helm/pkg/helm/helmpath" 25 26 "github.com/spf13/pflag" 27 ) 28 29 func TestEnvSettings(t *testing.T) { 30 tests := []struct { 31 name string 32 33 // input 34 args []string 35 envars map[string]string 36 37 // expected values 38 home, host, ns, kcontext, kconfig, plugins string 39 debug, tlsverify bool 40 }{ 41 { 42 name: "defaults", 43 args: []string{}, 44 home: DefaultHelmHome, 45 plugins: helmpath.Home(DefaultHelmHome).Plugins(), 46 ns: "kube-system", 47 tlsverify: false, 48 }, 49 { 50 name: "with flags set", 51 args: []string{"--home", "/foo", "--host=here", "--debug", "--tiller-namespace=myns", "--kubeconfig", "/bar"}, 52 home: "/foo", 53 plugins: helmpath.Home("/foo").Plugins(), 54 host: "here", 55 ns: "myns", 56 kconfig: "/bar", 57 debug: true, 58 tlsverify: false, 59 }, 60 { 61 name: "with envvars set", 62 args: []string{}, 63 envars: map[string]string{"HELM_HOME": "/bar", "HELM_HOST": "there", "HELM_DEBUG": "1", "TILLER_NAMESPACE": "yourns"}, 64 home: "/bar", 65 plugins: helmpath.Home("/bar").Plugins(), 66 host: "there", 67 ns: "yourns", 68 debug: true, 69 tlsverify: false, 70 }, 71 { 72 name: "with TLS envvars set", 73 args: []string{}, 74 envars: map[string]string{"HELM_HOME": "/bar", "HELM_HOST": "there", "HELM_DEBUG": "1", "TILLER_NAMESPACE": "yourns", "HELM_TLS_VERIFY": "1"}, 75 home: "/bar", 76 plugins: helmpath.Home("/bar").Plugins(), 77 host: "there", 78 ns: "yourns", 79 debug: true, 80 tlsverify: true, 81 }, 82 { 83 name: "with flags and envvars set", 84 args: []string{"--home", "/foo", "--host=here", "--debug", "--tiller-namespace=myns"}, 85 envars: map[string]string{"HELM_HOME": "/bar", "HELM_HOST": "there", "HELM_DEBUG": "1", "TILLER_NAMESPACE": "yourns", "HELM_PLUGIN": "glade"}, 86 home: "/foo", 87 plugins: "glade", 88 host: "here", 89 ns: "myns", 90 debug: true, 91 tlsverify: false, 92 }, 93 } 94 95 allEnvvars := map[string]string{ 96 "HELM_DEBUG": "", 97 "HELM_HOME": "", 98 "HELM_HOST": "", 99 "TILLER_NAMESPACE": "", 100 "HELM_PLUGIN": "", 101 "HELM_TLS_HOSTNAME": "", 102 "HELM_TLS_CA_CERT": "", 103 "HELM_TLS_CERT": "", 104 "HELM_TLS_KEY": "", 105 "HELM_TLS_VERIFY": "", 106 "HELM_TLS_ENABLE": "", 107 } 108 109 reset := resetEnv(allEnvvars) 110 defer reset() 111 112 for _, tt := range tests { 113 t.Run(tt.name, func(t *testing.T) { 114 for k, v := range tt.envars { 115 os.Setenv(k, v) 116 } 117 118 flags := pflag.NewFlagSet("testing", pflag.ContinueOnError) 119 120 settings := &EnvSettings{} 121 settings.AddFlags(flags) 122 settings.AddFlagsTLS(flags) 123 flags.Parse(tt.args) 124 125 settings.Init(flags) 126 settings.InitTLS(flags) 127 128 if settings.Home != helmpath.Home(tt.home) { 129 t.Errorf("expected home %q, got %q", tt.home, settings.Home) 130 } 131 if settings.PluginDirs() != tt.plugins { 132 t.Errorf("expected plugins %q, got %q", tt.plugins, settings.PluginDirs()) 133 } 134 if settings.TillerHost != tt.host { 135 t.Errorf("expected host %q, got %q", tt.host, settings.TillerHost) 136 } 137 if settings.Debug != tt.debug { 138 t.Errorf("expected debug %t, got %t", tt.debug, settings.Debug) 139 } 140 if settings.TillerNamespace != tt.ns { 141 t.Errorf("expected tiller-namespace %q, got %q", tt.ns, settings.TillerNamespace) 142 } 143 if settings.KubeContext != tt.kcontext { 144 t.Errorf("expected kube-context %q, got %q", tt.kcontext, settings.KubeContext) 145 } 146 if settings.KubeConfig != tt.kconfig { 147 t.Errorf("expected kubeconfig %q, got %q", tt.kconfig, settings.KubeConfig) 148 } 149 if settings.TLSVerify != tt.tlsverify { 150 t.Errorf("expected tls-verify %t, got %t", tt.tlsverify, settings.TLSVerify) 151 } 152 153 resetEnv(tt.envars) 154 }) 155 } 156 } 157 158 func resetEnv(envars map[string]string) func() { 159 origEnv := os.Environ() 160 161 // ensure any local envvars do not hose us 162 for e := range envars { 163 os.Unsetenv(e) 164 } 165 166 return func() { 167 for _, pair := range origEnv { 168 kv := strings.SplitN(pair, "=", 2) 169 os.Setenv(kv[0], kv[1]) 170 } 171 } 172 }