github.com/vtuson/helm@v2.8.2+incompatible/pkg/helm/environment/environment_test.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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, plugins string
    39  		debug                             bool
    40  	}{
    41  		{
    42  			name:    "defaults",
    43  			args:    []string{},
    44  			home:    DefaultHelmHome,
    45  			plugins: helmpath.Home(DefaultHelmHome).Plugins(),
    46  			ns:      "kube-system",
    47  		},
    48  		{
    49  			name:    "with flags set",
    50  			args:    []string{"--home", "/foo", "--host=here", "--debug", "--tiller-namespace=myns"},
    51  			home:    "/foo",
    52  			plugins: helmpath.Home("/foo").Plugins(),
    53  			host:    "here",
    54  			ns:      "myns",
    55  			debug:   true,
    56  		},
    57  		{
    58  			name:    "with envvars set",
    59  			args:    []string{},
    60  			envars:  map[string]string{"HELM_HOME": "/bar", "HELM_HOST": "there", "HELM_DEBUG": "1", "TILLER_NAMESPACE": "yourns"},
    61  			home:    "/bar",
    62  			plugins: helmpath.Home("/bar").Plugins(),
    63  			host:    "there",
    64  			ns:      "yourns",
    65  			debug:   true,
    66  		},
    67  		{
    68  			name:    "with flags and envvars set",
    69  			args:    []string{"--home", "/foo", "--host=here", "--debug", "--tiller-namespace=myns"},
    70  			envars:  map[string]string{"HELM_HOME": "/bar", "HELM_HOST": "there", "HELM_DEBUG": "1", "TILLER_NAMESPACE": "yourns", "HELM_PLUGIN": "glade"},
    71  			home:    "/foo",
    72  			plugins: "glade",
    73  			host:    "here",
    74  			ns:      "myns",
    75  			debug:   true,
    76  		},
    77  	}
    78  
    79  	cleanup := resetEnv()
    80  	defer cleanup()
    81  
    82  	for _, tt := range tests {
    83  		t.Run(tt.name, func(t *testing.T) {
    84  			for k, v := range tt.envars {
    85  				os.Setenv(k, v)
    86  			}
    87  
    88  			flags := pflag.NewFlagSet("testing", pflag.ContinueOnError)
    89  
    90  			settings := &EnvSettings{}
    91  			settings.AddFlags(flags)
    92  			flags.Parse(tt.args)
    93  
    94  			settings.Init(flags)
    95  
    96  			if settings.Home != helmpath.Home(tt.home) {
    97  				t.Errorf("expected home %q, got %q", tt.home, settings.Home)
    98  			}
    99  			if settings.PluginDirs() != tt.plugins {
   100  				t.Errorf("expected plugins %q, got %q", tt.plugins, settings.PluginDirs())
   101  			}
   102  			if settings.TillerHost != tt.host {
   103  				t.Errorf("expected host %q, got %q", tt.host, settings.TillerHost)
   104  			}
   105  			if settings.Debug != tt.debug {
   106  				t.Errorf("expected debug %t, got %t", tt.debug, settings.Debug)
   107  			}
   108  			if settings.TillerNamespace != tt.ns {
   109  				t.Errorf("expected tiller-namespace %q, got %q", tt.ns, settings.TillerNamespace)
   110  			}
   111  			if settings.KubeContext != tt.kcontext {
   112  				t.Errorf("expected kube-context %q, got %q", tt.kcontext, settings.KubeContext)
   113  			}
   114  
   115  			cleanup()
   116  		})
   117  	}
   118  }
   119  
   120  func resetEnv() func() {
   121  	origEnv := os.Environ()
   122  
   123  	// ensure any local envvars do not hose us
   124  	for _, e := range envMap {
   125  		os.Unsetenv(e)
   126  	}
   127  
   128  	return func() {
   129  		for _, pair := range origEnv {
   130  			kv := strings.SplitN(pair, "=", 2)
   131  			os.Setenv(kv[0], kv[1])
   132  		}
   133  	}
   134  }