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