github.com/koderover/helm@v2.17.0+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 "k8s.io/helm/pkg/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/good/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  		Hooks: map[string]string{
    86  			Install: "echo installing...",
    87  		},
    88  	}
    89  
    90  	if !reflect.DeepEqual(expect, plug.Metadata) {
    91  		t.Errorf("Expected plugin metadata %v, got %v", expect, plug.Metadata)
    92  	}
    93  }
    94  
    95  func TestLoadDirDuplicateEntries(t *testing.T) {
    96  	dirname := "testdata/plugdir/bad/duplicate-entries"
    97  	if _, err := LoadDir(dirname); err == nil {
    98  		t.Errorf("successfully loaded plugin with duplicate entries when it should've failed")
    99  	}
   100  }
   101  
   102  func TestDownloader(t *testing.T) {
   103  	dirname := "testdata/plugdir/good/downloader"
   104  	plug, err := LoadDir(dirname)
   105  	if err != nil {
   106  		t.Fatalf("error loading Hello plugin: %s", err)
   107  	}
   108  
   109  	if plug.Dir != dirname {
   110  		t.Errorf("Expected dir %q, got %q", dirname, plug.Dir)
   111  	}
   112  
   113  	expect := &Metadata{
   114  		Name:        "downloader",
   115  		Version:     "1.2.3",
   116  		Usage:       "usage",
   117  		Description: "download something",
   118  		Command:     "echo Hello",
   119  		Downloaders: []Downloaders{
   120  			{
   121  				Protocols: []string{"myprotocol", "myprotocols"},
   122  				Command:   "echo Download",
   123  			},
   124  		},
   125  	}
   126  
   127  	if !reflect.DeepEqual(expect, plug.Metadata) {
   128  		t.Errorf("Expected metadata %v, got %v", expect, plug.Metadata)
   129  	}
   130  }
   131  
   132  func TestLoadAll(t *testing.T) {
   133  
   134  	// Verify that empty dir loads:
   135  	if plugs, err := LoadAll("testdata"); err != nil {
   136  		t.Fatalf("error loading dir with no plugins: %s", err)
   137  	} else if len(plugs) > 0 {
   138  		t.Fatalf("expected empty dir to have 0 plugins")
   139  	}
   140  
   141  	basedir := "testdata/plugdir/good"
   142  	plugs, err := LoadAll(basedir)
   143  	if err != nil {
   144  		t.Fatalf("Could not load %q: %s", basedir, err)
   145  	}
   146  
   147  	// This would fail if the duplicate plugin were loaded.
   148  	if l := len(plugs); l != 3 {
   149  		t.Fatalf("expected 3 plugins, found %d", l)
   150  	}
   151  
   152  	if plugs[0].Metadata.Name != "downloader" {
   153  		t.Errorf("Expected first plugin to be echo, got %q", plugs[0].Metadata.Name)
   154  	}
   155  	if plugs[1].Metadata.Name != "echo" {
   156  		t.Errorf("Expected first plugin to be echo, got %q", plugs[0].Metadata.Name)
   157  	}
   158  	if plugs[2].Metadata.Name != "hello" {
   159  		t.Errorf("Expected second plugin to be hello, got %q", plugs[1].Metadata.Name)
   160  	}
   161  }