github.com/canthefason/helm@v2.2.1-0.20170221172616-16b043b8d505+incompatible/cmd/helm/plugins_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 package main 17 18 import ( 19 "bytes" 20 "os" 21 "path/filepath" 22 "runtime" 23 "strings" 24 "testing" 25 26 "k8s.io/helm/cmd/helm/helmpath" 27 28 "github.com/spf13/cobra" 29 ) 30 31 func TestManuallyProcessArgs(t *testing.T) { 32 input := []string{ 33 "--debug", 34 "--foo", "bar", 35 "--host", "example.com", 36 "--kube-context", "test1", 37 "--home=/tmp", 38 "--tiller-namespace=hello", 39 "command", 40 } 41 42 expectKnown := []string{ 43 "--debug", "--host", "example.com", "--kube-context", "test1", "--home=/tmp", "--tiller-namespace=hello", 44 } 45 46 expectUnknown := []string{ 47 "--foo", "bar", "command", 48 } 49 50 known, unknown := manuallyProcessArgs(input) 51 52 for i, k := range known { 53 if k != expectKnown[i] { 54 t.Errorf("expected known flag %d to be %q, got %q", i, expectKnown[i], k) 55 } 56 } 57 for i, k := range unknown { 58 if k != expectUnknown[i] { 59 t.Errorf("expected unknown flag %d to be %q, got %q", i, expectUnknown[i], k) 60 } 61 } 62 63 } 64 65 func TestLoadPlugins(t *testing.T) { 66 // Set helm home to point to testdata 67 old := helmHome 68 helmHome = "testdata/helmhome" 69 defer func() { 70 helmHome = old 71 }() 72 hh := helmpath.Home(homePath()) 73 74 out := bytes.NewBuffer(nil) 75 cmd := &cobra.Command{} 76 loadPlugins(cmd, hh, out) 77 78 envs := strings.Join([]string{ 79 "fullenv", 80 hh.Plugins() + "/fullenv", 81 hh.Plugins(), 82 hh.String(), 83 hh.Repository(), 84 hh.RepositoryFile(), 85 hh.Cache(), 86 hh.LocalRepository(), 87 os.Args[0], 88 }, "\n") 89 90 // Test that the YAML file was correctly converted to a command. 91 tests := []struct { 92 use string 93 short string 94 long string 95 expect string 96 args []string 97 }{ 98 {"args", "echo args", "This echos args", "-a -b -c\n", []string{"-a", "-b", "-c"}}, 99 {"echo", "echo stuff", "This echos stuff", "hello\n", []string{}}, 100 {"env", "env stuff", "show the env", hh.String() + "\n", []string{}}, 101 {"fullenv", "show env vars", "show all env vars", envs + "\n", []string{}}, 102 } 103 104 plugins := cmd.Commands() 105 106 if len(plugins) != len(tests) { 107 t.Fatalf("Expected %d plugins, got %d", len(tests), len(plugins)) 108 } 109 110 for i := 0; i < len(plugins); i++ { 111 out.Reset() 112 tt := tests[i] 113 pp := plugins[i] 114 if pp.Use != tt.use { 115 t.Errorf("%d: Expected Use=%q, got %q", i, tt.use, pp.Use) 116 } 117 if pp.Short != tt.short { 118 t.Errorf("%d: Expected Use=%q, got %q", i, tt.short, pp.Short) 119 } 120 if pp.Long != tt.long { 121 t.Errorf("%d: Expected Use=%q, got %q", i, tt.long, pp.Long) 122 } 123 124 // Currently, plugins assume a Linux subsystem. Skip the execution 125 // tests until this is fixed 126 if runtime.GOOS != "windows" { 127 if err := pp.RunE(pp, tt.args); err != nil { 128 t.Errorf("Error running %s: %s", tt.use, err) 129 } 130 if out.String() != tt.expect { 131 t.Errorf("Expected %s to output:\n%s\ngot\n%s", tt.use, tt.expect, out.String()) 132 } 133 } 134 } 135 } 136 137 func TestSetupEnv(t *testing.T) { 138 name := "pequod" 139 hh := helmpath.Home("testdata/helmhome") 140 base := filepath.Join(hh.Plugins(), name) 141 plugdirs := hh.Plugins() 142 flagDebug = true 143 defer func() { 144 flagDebug = false 145 }() 146 147 setupEnv(name, base, plugdirs, hh) 148 for _, tt := range []struct { 149 name string 150 expect string 151 }{ 152 {"HELM_PLUGIN_NAME", name}, 153 {"HELM_PLUGIN_DIR", base}, 154 {"HELM_PLUGIN", hh.Plugins()}, 155 {"HELM_DEBUG", "1"}, 156 {"HELM_HOME", hh.String()}, 157 {"HELM_PATH_REPOSITORY", hh.Repository()}, 158 {"HELM_PATH_REPOSITORY_FILE", hh.RepositoryFile()}, 159 {"HELM_PATH_CACHE", hh.Cache()}, 160 {"HELM_PATH_LOCAL_REPOSITORY", hh.LocalRepository()}, 161 {"HELM_PATH_STARTER", hh.Starters()}, 162 {"TILLER_HOST", tillerHost}, 163 {"TILLER_NAMESPACE", tillerNamespace}, 164 } { 165 if got := os.Getenv(tt.name); got != tt.expect { 166 t.Errorf("Expected $%s=%q, got %q", tt.name, tt.expect, got) 167 } 168 } 169 }