github.com/cloudposse/helm@v2.2.3+incompatible/pkg/plugin/plugin_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 plugin 17 18 import ( 19 "reflect" 20 "testing" 21 ) 22 23 func TestPrepareCommand(t *testing.T) { 24 p := &Plugin{ 25 Dir: "/tmp", // Unused 26 Metadata: &Metadata{ 27 Name: "test", 28 Command: "echo -n foo", 29 }, 30 } 31 argv := []string{"--debug", "--foo", "bar"} 32 33 cmd, args := p.PrepareCommand(argv) 34 if cmd != "echo" { 35 t.Errorf("Expected echo, got %q", cmd) 36 } 37 38 if l := len(args); l != 5 { 39 t.Errorf("expected 5 args, got %d", l) 40 } 41 42 expect := []string{"-n", "foo", "--debug", "--foo", "bar"} 43 for i := 0; i < len(args); i++ { 44 if expect[i] != args[i] { 45 t.Errorf("Expected arg=%q, got %q", expect[i], args[i]) 46 } 47 } 48 49 // Test with IgnoreFlags. This should omit --debug, --foo, bar 50 p.Metadata.IgnoreFlags = true 51 cmd, args = p.PrepareCommand(argv) 52 if cmd != "echo" { 53 t.Errorf("Expected echo, got %q", cmd) 54 } 55 if l := len(args); l != 2 { 56 t.Errorf("expected 2 args, got %d", l) 57 } 58 expect = []string{"-n", "foo"} 59 for i := 0; i < len(args); i++ { 60 if expect[i] != args[i] { 61 t.Errorf("Expected arg=%q, got %q", expect[i], args[i]) 62 } 63 } 64 } 65 66 func TestLoadDir(t *testing.T) { 67 dirname := "testdata/plugdir/hello" 68 plug, err := LoadDir(dirname) 69 if err != nil { 70 t.Fatalf("error loading Hello plugin: %s", err) 71 } 72 73 if plug.Dir != dirname { 74 t.Errorf("Expected dir %q, got %q", dirname, plug.Dir) 75 } 76 77 expect := Metadata{ 78 Name: "hello", 79 Version: "0.1.0", 80 Usage: "usage", 81 Description: "description", 82 Command: "$HELM_PLUGIN_SELF/hello.sh", 83 UseTunnel: true, 84 IgnoreFlags: true, 85 } 86 87 if reflect.DeepEqual(expect, plug.Metadata) { 88 t.Errorf("Expected name %v, got %v", expect, plug.Metadata) 89 } 90 } 91 92 func TestLoadAll(t *testing.T) { 93 94 // Verify that empty dir loads: 95 if plugs, err := LoadAll("testdata"); err != nil { 96 t.Fatalf("error loading dir with no plugins: %s", err) 97 } else if len(plugs) > 0 { 98 t.Fatalf("expected empty dir to have 0 plugins") 99 } 100 101 basedir := "testdata/plugdir" 102 plugs, err := LoadAll(basedir) 103 if err != nil { 104 t.Fatalf("Could not load %q: %s", basedir, err) 105 } 106 107 if l := len(plugs); l != 2 { 108 t.Fatalf("expected 2 plugins, found %d", l) 109 } 110 111 if plugs[0].Metadata.Name != "echo" { 112 t.Errorf("Expected first plugin to be echo, got %q", plugs[0].Metadata.Name) 113 } 114 if plugs[1].Metadata.Name != "hello" { 115 t.Errorf("Expected second plugin to be hello, got %q", plugs[1].Metadata.Name) 116 } 117 }