github.com/umeshredd/helm@v3.0.0-alpha.1+incompatible/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  	"strings"
    22  	"testing"
    23  
    24  	"github.com/spf13/pflag"
    25  
    26  	"helm.sh/helm/pkg/helmpath"
    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, ns, kcontext, plugins string
    39  		debug                       bool
    40  	}{
    41  		{
    42  			name:    "defaults",
    43  			home:    defaultHelmHome,
    44  			plugins: helmpath.Home(defaultHelmHome).Plugins(),
    45  			ns:      "",
    46  		},
    47  		{
    48  			name:    "with flags set",
    49  			args:    "--home /foo --debug --namespace=myns",
    50  			home:    "/foo",
    51  			plugins: helmpath.Home("/foo").Plugins(),
    52  			ns:      "myns",
    53  			debug:   true,
    54  		},
    55  		{
    56  			name:    "with envvars set",
    57  			envars:  map[string]string{"HELM_HOME": "/bar", "HELM_DEBUG": "1", "HELM_NAMESPACE": "yourns"},
    58  			home:    "/bar",
    59  			plugins: helmpath.Home("/bar").Plugins(),
    60  			ns:      "yourns",
    61  			debug:   true,
    62  		},
    63  		{
    64  			name:    "with flags and envvars set",
    65  			args:    "--home /foo --debug --namespace=myns",
    66  			envars:  map[string]string{"HELM_HOME": "/bar", "HELM_DEBUG": "1", "HELM_NAMESPACE": "yourns", "HELM_PLUGIN": "glade"},
    67  			home:    "/foo",
    68  			plugins: "glade",
    69  			ns:      "myns",
    70  			debug:   true,
    71  		},
    72  	}
    73  
    74  	for _, tt := range tests {
    75  		t.Run(tt.name, func(t *testing.T) {
    76  			defer resetEnv()()
    77  
    78  			for k, v := range tt.envars {
    79  				os.Setenv(k, v)
    80  			}
    81  
    82  			flags := pflag.NewFlagSet("testing", pflag.ContinueOnError)
    83  
    84  			settings := &EnvSettings{}
    85  			settings.AddFlags(flags)
    86  			flags.Parse(strings.Split(tt.args, " "))
    87  
    88  			settings.Init(flags)
    89  
    90  			if settings.Home != helmpath.Home(tt.home) {
    91  				t.Errorf("expected home %q, got %q", tt.home, settings.Home)
    92  			}
    93  			if settings.PluginDirs() != tt.plugins {
    94  				t.Errorf("expected plugins %q, got %q", tt.plugins, settings.PluginDirs())
    95  			}
    96  			if settings.Debug != tt.debug {
    97  				t.Errorf("expected debug %t, got %t", tt.debug, settings.Debug)
    98  			}
    99  			if settings.Namespace != tt.ns {
   100  				t.Errorf("expected namespace %q, got %q", tt.ns, settings.Namespace)
   101  			}
   102  			if settings.KubeContext != tt.kcontext {
   103  				t.Errorf("expected kube-context %q, got %q", tt.kcontext, settings.KubeContext)
   104  			}
   105  		})
   106  	}
   107  }
   108  
   109  func resetEnv() func() {
   110  	origEnv := os.Environ()
   111  
   112  	// ensure any local envvars do not hose us
   113  	for _, e := range envMap {
   114  		os.Unsetenv(e)
   115  	}
   116  
   117  	return func() {
   118  		for _, pair := range origEnv {
   119  			kv := strings.SplitN(pair, "=", 2)
   120  			os.Setenv(kv[0], kv[1])
   121  		}
   122  	}
   123  }