github.com/zoumo/helm@v2.5.0+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 // 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/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 TestDownloader(t *testing.T) {
    96  	dirname := "testdata/plugdir/downloader"
    97  	plug, err := LoadDir(dirname)
    98  	if err != nil {
    99  		t.Fatalf("error loading Hello plugin: %s", err)
   100  	}
   101  
   102  	if plug.Dir != dirname {
   103  		t.Errorf("Expected dir %q, got %q", dirname, plug.Dir)
   104  	}
   105  
   106  	expect := &Metadata{
   107  		Name:        "downloader",
   108  		Version:     "1.2.3",
   109  		Usage:       "usage",
   110  		Description: "download something",
   111  		Command:     "echo Hello",
   112  		Downloaders: []Downloaders{
   113  			{
   114  				Protocols: []string{"myprotocol", "myprotocols"},
   115  				Command:   "echo Download",
   116  			},
   117  		},
   118  	}
   119  
   120  	if !reflect.DeepEqual(expect, plug.Metadata) {
   121  		t.Errorf("Expected metadata %v, got %v", expect, plug.Metadata)
   122  	}
   123  }
   124  
   125  func TestLoadAll(t *testing.T) {
   126  
   127  	// Verify that empty dir loads:
   128  	if plugs, err := LoadAll("testdata"); err != nil {
   129  		t.Fatalf("error loading dir with no plugins: %s", err)
   130  	} else if len(plugs) > 0 {
   131  		t.Fatalf("expected empty dir to have 0 plugins")
   132  	}
   133  
   134  	basedir := "testdata/plugdir"
   135  	plugs, err := LoadAll(basedir)
   136  	if err != nil {
   137  		t.Fatalf("Could not load %q: %s", basedir, err)
   138  	}
   139  
   140  	if l := len(plugs); l != 3 {
   141  		t.Fatalf("expected 3 plugins, found %d", l)
   142  	}
   143  
   144  	if plugs[0].Metadata.Name != "downloader" {
   145  		t.Errorf("Expected first plugin to be echo, got %q", plugs[0].Metadata.Name)
   146  	}
   147  	if plugs[1].Metadata.Name != "echo" {
   148  		t.Errorf("Expected first plugin to be echo, got %q", plugs[0].Metadata.Name)
   149  	}
   150  	if plugs[2].Metadata.Name != "hello" {
   151  		t.Errorf("Expected second plugin to be hello, got %q", plugs[1].Metadata.Name)
   152  	}
   153  }