github.com/phroggyy/helm@v3.0.0-beta.3+incompatible/cmd/helm/plugin_test.go (about) 1 /* 2 Copyright The Helm Authors. 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 "runtime" 22 "strings" 23 "testing" 24 25 "github.com/spf13/cobra" 26 ) 27 28 func TestManuallyProcessArgs(t *testing.T) { 29 input := []string{ 30 "--debug", 31 "--foo", "bar", 32 "--context", "test1", 33 "--home=/tmp", 34 "command", 35 } 36 37 expectKnown := []string{ 38 "--debug", "--context", "test1", 39 } 40 41 expectUnknown := []string{ 42 "--foo", "bar", "--home=/tmp", "command", 43 } 44 45 known, unknown := manuallyProcessArgs(input) 46 47 for i, k := range known { 48 if k != expectKnown[i] { 49 t.Errorf("expected known flag %d to be %q, got %q", i, expectKnown[i], k) 50 } 51 } 52 for i, k := range unknown { 53 if k != expectUnknown[i] { 54 t.Errorf("expected unknown flag %d to be %q, got %q", i, expectUnknown[i], k) 55 } 56 } 57 58 } 59 60 func TestLoadPlugins(t *testing.T) { 61 settings.PluginsDirectory = "testdata/helmhome/helm/plugins" 62 settings.RepositoryConfig = "testdata/helmhome/helm/repositories.yaml" 63 settings.RepositoryCache = "testdata/helmhome/helm/repository" 64 65 var ( 66 out bytes.Buffer 67 cmd cobra.Command 68 ) 69 loadPlugins(&cmd, &out) 70 71 envs := strings.Join([]string{ 72 "fullenv", 73 "testdata/helmhome/helm/plugins/fullenv", 74 "testdata/helmhome/helm/plugins", 75 "testdata/helmhome/helm/repositories.yaml", 76 "testdata/helmhome/helm/repository", 77 os.Args[0], 78 }, "\n") 79 80 // Test that the YAML file was correctly converted to a command. 81 tests := []struct { 82 use string 83 short string 84 long string 85 expect string 86 args []string 87 }{ 88 {"args", "echo args", "This echos args", "-a -b -c\n", []string{"-a", "-b", "-c"}}, 89 {"echo", "echo stuff", "This echos stuff", "hello\n", []string{}}, 90 {"env", "env stuff", "show the env", "env\n", []string{}}, 91 {"fullenv", "show env vars", "show all env vars", envs + "\n", []string{}}, 92 } 93 94 plugins := cmd.Commands() 95 96 if len(plugins) != len(tests) { 97 t.Fatalf("Expected %d plugins, got %d", len(tests), len(plugins)) 98 } 99 100 for i := 0; i < len(plugins); i++ { 101 out.Reset() 102 tt := tests[i] 103 pp := plugins[i] 104 if pp.Use != tt.use { 105 t.Errorf("%d: Expected Use=%q, got %q", i, tt.use, pp.Use) 106 } 107 if pp.Short != tt.short { 108 t.Errorf("%d: Expected Use=%q, got %q", i, tt.short, pp.Short) 109 } 110 if pp.Long != tt.long { 111 t.Errorf("%d: Expected Use=%q, got %q", i, tt.long, pp.Long) 112 } 113 114 // Currently, plugins assume a Linux subsystem. Skip the execution 115 // tests until this is fixed 116 if runtime.GOOS != "windows" { 117 if err := pp.RunE(pp, tt.args); err != nil { 118 t.Errorf("Error running %s: %+v", tt.use, err) 119 } 120 if out.String() != tt.expect { 121 t.Errorf("Expected %s to output:\n%s\ngot\n%s", tt.use, tt.expect, out.String()) 122 } 123 } 124 } 125 } 126 127 func TestLoadPlugins_HelmNoPlugins(t *testing.T) { 128 settings.PluginsDirectory = "testdata/helmhome/helm/plugins" 129 settings.RepositoryConfig = "testdata/helmhome/helm/repository" 130 131 os.Setenv("HELM_NO_PLUGINS", "1") 132 133 out := bytes.NewBuffer(nil) 134 cmd := &cobra.Command{} 135 loadPlugins(cmd, out) 136 plugins := cmd.Commands() 137 138 if len(plugins) != 0 { 139 t.Fatalf("Expected 0 plugins, got %d", len(plugins)) 140 } 141 }