github.com/sgoings/helm@v2.0.0-alpha.2.0.20170406211108-734e92851ac3+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 TestLoadAll(t *testing.T) {
    96  
    97  	// Verify that empty dir loads:
    98  	if plugs, err := LoadAll("testdata"); err != nil {
    99  		t.Fatalf("error loading dir with no plugins: %s", err)
   100  	} else if len(plugs) > 0 {
   101  		t.Fatalf("expected empty dir to have 0 plugins")
   102  	}
   103  
   104  	basedir := "testdata/plugdir"
   105  	plugs, err := LoadAll(basedir)
   106  	if err != nil {
   107  		t.Fatalf("Could not load %q: %s", basedir, err)
   108  	}
   109  
   110  	if l := len(plugs); l != 2 {
   111  		t.Fatalf("expected 2 plugins, found %d", l)
   112  	}
   113  
   114  	if plugs[0].Metadata.Name != "echo" {
   115  		t.Errorf("Expected first plugin to be echo, got %q", plugs[0].Metadata.Name)
   116  	}
   117  	if plugs[1].Metadata.Name != "hello" {
   118  		t.Errorf("Expected second plugin to be hello, got %q", plugs[1].Metadata.Name)
   119  	}
   120  }