github.com/umeshredd/helm@v3.0.0-alpha.1+incompatible/pkg/plugin/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 plugin // import "helm.sh/helm/pkg/plugin" 17 18 import ( 19 "reflect" 20 "runtime" 21 "testing" 22 ) 23 24 func checkCommand(p *Plugin, extraArgs []string, osStrCmp string, t *testing.T) { 25 cmd, args, err := p.PrepareCommand(extraArgs) 26 if err != nil { 27 t.Errorf(err.Error()) 28 } 29 if cmd != "echo" { 30 t.Errorf("Expected echo, got %q", cmd) 31 } 32 33 if l := len(args); l != 5 { 34 t.Errorf("expected 5 args, got %d", l) 35 } 36 37 expect := []string{"-n", osStrCmp, "--debug", "--foo", "bar"} 38 for i := 0; i < len(args); i++ { 39 if expect[i] != args[i] { 40 t.Errorf("Expected arg=%q, got %q", expect[i], args[i]) 41 } 42 } 43 44 // Test with IgnoreFlags. This should omit --debug, --foo, bar 45 p.Metadata.IgnoreFlags = true 46 cmd, args, err = p.PrepareCommand(extraArgs) 47 if err != nil { 48 t.Errorf(err.Error()) 49 } 50 if cmd != "echo" { 51 t.Errorf("Expected echo, got %q", cmd) 52 } 53 if l := len(args); l != 2 { 54 t.Errorf("expected 2 args, got %d", l) 55 } 56 expect = []string{"-n", osStrCmp} 57 for i := 0; i < len(args); i++ { 58 if expect[i] != args[i] { 59 t.Errorf("Expected arg=%q, got %q", expect[i], args[i]) 60 } 61 } 62 } 63 64 func TestPrepareCommand(t *testing.T) { 65 p := &Plugin{ 66 Dir: "/tmp", // Unused 67 Metadata: &Metadata{ 68 Name: "test", 69 Command: "echo -n foo", 70 }, 71 } 72 argv := []string{"--debug", "--foo", "bar"} 73 74 checkCommand(p, argv, "foo", t) 75 } 76 77 func TestPlatformPrepareCommand(t *testing.T) { 78 p := &Plugin{ 79 Dir: "/tmp", // Unused 80 Metadata: &Metadata{ 81 Name: "test", 82 Command: "echo -n os-arch", 83 PlatformCommand: []PlatformCommand{ 84 {OperatingSystem: "linux", Architecture: "i386", Command: "echo -n linux-i386"}, 85 {OperatingSystem: "linux", Architecture: "amd64", Command: "echo -n linux-amd64"}, 86 {OperatingSystem: "windows", Architecture: "amd64", Command: "echo -n win-64"}, 87 }, 88 }, 89 } 90 argv := []string{"--debug", "--foo", "bar"} 91 var osStrCmp string 92 os := runtime.GOOS 93 arch := runtime.GOARCH 94 if os == "linux" && arch == "i386" { 95 osStrCmp = "linux-i386" 96 } else if os == "linux" && arch == "amd64" { 97 osStrCmp = "linux-amd64" 98 } else if os == "windows" && arch == "amd64" { 99 osStrCmp = "win-64" 100 } else { 101 osStrCmp = "os-arch" 102 } 103 104 checkCommand(p, argv, osStrCmp, t) 105 } 106 107 func TestPartialPlatformPrepareCommand(t *testing.T) { 108 p := &Plugin{ 109 Dir: "/tmp", // Unused 110 Metadata: &Metadata{ 111 Name: "test", 112 Command: "echo -n os-arch", 113 PlatformCommand: []PlatformCommand{ 114 {OperatingSystem: "linux", Architecture: "i386", Command: "echo -n linux-i386"}, 115 {OperatingSystem: "windows", Architecture: "amd64", Command: "echo -n win-64"}, 116 }, 117 }, 118 } 119 argv := []string{"--debug", "--foo", "bar"} 120 var osStrCmp string 121 os := runtime.GOOS 122 arch := runtime.GOARCH 123 if os == "linux" { 124 osStrCmp = "linux-i386" 125 } else if os == "windows" && arch == "amd64" { 126 osStrCmp = "win-64" 127 } else { 128 osStrCmp = "os-arch" 129 } 130 131 checkCommand(p, argv, osStrCmp, t) 132 } 133 134 func TestNoPrepareCommand(t *testing.T) { 135 p := &Plugin{ 136 Dir: "/tmp", // Unused 137 Metadata: &Metadata{ 138 Name: "test", 139 }, 140 } 141 argv := []string{"--debug", "--foo", "bar"} 142 143 _, _, err := p.PrepareCommand(argv) 144 if err == nil { 145 t.Errorf("Expected error to be returned") 146 } 147 } 148 149 func TestNoMatchPrepareCommand(t *testing.T) { 150 p := &Plugin{ 151 Dir: "/tmp", // Unused 152 Metadata: &Metadata{ 153 Name: "test", 154 PlatformCommand: []PlatformCommand{ 155 {OperatingSystem: "no-os", Architecture: "amd64", Command: "echo -n linux-i386"}, 156 }, 157 }, 158 } 159 argv := []string{"--debug", "--foo", "bar"} 160 161 _, _, err := p.PrepareCommand(argv) 162 if err == nil { 163 t.Errorf("Expected error to be returned") 164 } 165 } 166 167 func TestLoadDir(t *testing.T) { 168 dirname := "testdata/plugdir/hello" 169 plug, err := LoadDir(dirname) 170 if err != nil { 171 t.Fatalf("error loading Hello plugin: %s", err) 172 } 173 174 if plug.Dir != dirname { 175 t.Errorf("Expected dir %q, got %q", dirname, plug.Dir) 176 } 177 178 expect := &Metadata{ 179 Name: "hello", 180 Version: "0.1.0", 181 Usage: "usage", 182 Description: "description", 183 Command: "$HELM_PLUGIN_SELF/hello.sh", 184 IgnoreFlags: true, 185 Hooks: map[string]string{ 186 Install: "echo installing...", 187 }, 188 } 189 190 if !reflect.DeepEqual(expect, plug.Metadata) { 191 t.Errorf("Expected plugin metadata %v, got %v", expect, plug.Metadata) 192 } 193 } 194 195 func TestDownloader(t *testing.T) { 196 dirname := "testdata/plugdir/downloader" 197 plug, err := LoadDir(dirname) 198 if err != nil { 199 t.Fatalf("error loading Hello plugin: %s", err) 200 } 201 202 if plug.Dir != dirname { 203 t.Errorf("Expected dir %q, got %q", dirname, plug.Dir) 204 } 205 206 expect := &Metadata{ 207 Name: "downloader", 208 Version: "1.2.3", 209 Usage: "usage", 210 Description: "download something", 211 Command: "echo Hello", 212 Downloaders: []Downloaders{ 213 { 214 Protocols: []string{"myprotocol", "myprotocols"}, 215 Command: "echo Download", 216 }, 217 }, 218 } 219 220 if !reflect.DeepEqual(expect, plug.Metadata) { 221 t.Errorf("Expected metadata %v, got %v", expect, plug.Metadata) 222 } 223 } 224 225 func TestLoadAll(t *testing.T) { 226 227 // Verify that empty dir loads: 228 if plugs, err := LoadAll("testdata"); err != nil { 229 t.Fatalf("error loading dir with no plugins: %s", err) 230 } else if len(plugs) > 0 { 231 t.Fatalf("expected empty dir to have 0 plugins") 232 } 233 234 basedir := "testdata/plugdir" 235 plugs, err := LoadAll(basedir) 236 if err != nil { 237 t.Fatalf("Could not load %q: %s", basedir, err) 238 } 239 240 if l := len(plugs); l != 3 { 241 t.Fatalf("expected 3 plugins, found %d", l) 242 } 243 244 if plugs[0].Metadata.Name != "downloader" { 245 t.Errorf("Expected first plugin to be echo, got %q", plugs[0].Metadata.Name) 246 } 247 if plugs[1].Metadata.Name != "echo" { 248 t.Errorf("Expected first plugin to be echo, got %q", plugs[0].Metadata.Name) 249 } 250 if plugs[2].Metadata.Name != "hello" { 251 t.Errorf("Expected second plugin to be hello, got %q", plugs[1].Metadata.Name) 252 } 253 }