github.com/sgoings/helm@v2.0.0-alpha.2.0.20170406211108-734e92851ac3+incompatible/cmd/helm/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 main
    17  
    18  import (
    19  	"bytes"
    20  	"os"
    21  	"path/filepath"
    22  	"runtime"
    23  	"strings"
    24  	"testing"
    25  
    26  	"k8s.io/helm/pkg/helm/helmpath"
    27  
    28  	"github.com/spf13/cobra"
    29  )
    30  
    31  func TestManuallyProcessArgs(t *testing.T) {
    32  	input := []string{
    33  		"--debug",
    34  		"--foo", "bar",
    35  		"--host", "example.com",
    36  		"--kube-context", "test1",
    37  		"--home=/tmp",
    38  		"--tiller-namespace=hello",
    39  		"command",
    40  	}
    41  
    42  	expectKnown := []string{
    43  		"--debug", "--host", "example.com", "--kube-context", "test1", "--home=/tmp", "--tiller-namespace=hello",
    44  	}
    45  
    46  	expectUnknown := []string{
    47  		"--foo", "bar", "command",
    48  	}
    49  
    50  	known, unknown := manuallyProcessArgs(input)
    51  
    52  	for i, k := range known {
    53  		if k != expectKnown[i] {
    54  			t.Errorf("expected known flag %d to be %q, got %q", i, expectKnown[i], k)
    55  		}
    56  	}
    57  	for i, k := range unknown {
    58  		if k != expectUnknown[i] {
    59  			t.Errorf("expected unknown flag %d to be %q, got %q", i, expectUnknown[i], k)
    60  		}
    61  	}
    62  
    63  }
    64  
    65  func TestLoadPlugins(t *testing.T) {
    66  	// Set helm home to point to testdata
    67  	old := helmHome
    68  	helmHome = "testdata/helmhome"
    69  	defer func() {
    70  		helmHome = old
    71  	}()
    72  	hh := helmpath.Home(homePath())
    73  
    74  	out := bytes.NewBuffer(nil)
    75  	cmd := &cobra.Command{}
    76  	loadPlugins(cmd, hh, out)
    77  
    78  	envs := strings.Join([]string{
    79  		"fullenv",
    80  		hh.Plugins() + "/fullenv",
    81  		hh.Plugins(),
    82  		hh.String(),
    83  		hh.Repository(),
    84  		hh.RepositoryFile(),
    85  		hh.Cache(),
    86  		hh.LocalRepository(),
    87  		os.Args[0],
    88  	}, "\n")
    89  
    90  	// Test that the YAML file was correctly converted to a command.
    91  	tests := []struct {
    92  		use    string
    93  		short  string
    94  		long   string
    95  		expect string
    96  		args   []string
    97  	}{
    98  		{"args", "echo args", "This echos args", "-a -b -c\n", []string{"-a", "-b", "-c"}},
    99  		{"echo", "echo stuff", "This echos stuff", "hello\n", []string{}},
   100  		{"env", "env stuff", "show the env", hh.String() + "\n", []string{}},
   101  		{"fullenv", "show env vars", "show all env vars", envs + "\n", []string{}},
   102  	}
   103  
   104  	plugins := cmd.Commands()
   105  
   106  	if len(plugins) != len(tests) {
   107  		t.Fatalf("Expected %d plugins, got %d", len(tests), len(plugins))
   108  	}
   109  
   110  	for i := 0; i < len(plugins); i++ {
   111  		out.Reset()
   112  		tt := tests[i]
   113  		pp := plugins[i]
   114  		if pp.Use != tt.use {
   115  			t.Errorf("%d: Expected Use=%q, got %q", i, tt.use, pp.Use)
   116  		}
   117  		if pp.Short != tt.short {
   118  			t.Errorf("%d: Expected Use=%q, got %q", i, tt.short, pp.Short)
   119  		}
   120  		if pp.Long != tt.long {
   121  			t.Errorf("%d: Expected Use=%q, got %q", i, tt.long, pp.Long)
   122  		}
   123  
   124  		// Currently, plugins assume a Linux subsystem. Skip the execution
   125  		// tests until this is fixed
   126  		if runtime.GOOS != "windows" {
   127  			if err := pp.RunE(pp, tt.args); err != nil {
   128  				t.Errorf("Error running %s: %s", tt.use, err)
   129  			}
   130  			if out.String() != tt.expect {
   131  				t.Errorf("Expected %s to output:\n%s\ngot\n%s", tt.use, tt.expect, out.String())
   132  			}
   133  		}
   134  	}
   135  }
   136  
   137  func TestLoadPlugins_HelmNoPlugins(t *testing.T) {
   138  	os.Setenv("HELM_NO_PLUGINS", "1")
   139  	defer os.Setenv("HELM_NO_PLUGINS", "0")
   140  
   141  	// Set helm home to point to testdata
   142  	old := helmHome
   143  	helmHome = "testdata/helmhome"
   144  	defer func() {
   145  		helmHome = old
   146  	}()
   147  	hh := helmpath.Home(homePath())
   148  
   149  	out := bytes.NewBuffer(nil)
   150  	cmd := &cobra.Command{}
   151  	loadPlugins(cmd, hh, out)
   152  	plugins := cmd.Commands()
   153  
   154  	if len(plugins) != 0 {
   155  		t.Fatalf("Expected 0 plugins, got %d", len(plugins))
   156  	}
   157  }
   158  
   159  func TestSetupEnv(t *testing.T) {
   160  	name := "pequod"
   161  	hh := helmpath.Home("testdata/helmhome")
   162  	base := filepath.Join(hh.Plugins(), name)
   163  	plugdirs := hh.Plugins()
   164  	flagDebug = true
   165  	defer func() {
   166  		flagDebug = false
   167  	}()
   168  
   169  	setupEnv(name, base, plugdirs, hh)
   170  	for _, tt := range []struct {
   171  		name   string
   172  		expect string
   173  	}{
   174  		{"HELM_PLUGIN_NAME", name},
   175  		{"HELM_PLUGIN_DIR", base},
   176  		{"HELM_PLUGIN", hh.Plugins()},
   177  		{"HELM_DEBUG", "1"},
   178  		{"HELM_HOME", hh.String()},
   179  		{"HELM_PATH_REPOSITORY", hh.Repository()},
   180  		{"HELM_PATH_REPOSITORY_FILE", hh.RepositoryFile()},
   181  		{"HELM_PATH_CACHE", hh.Cache()},
   182  		{"HELM_PATH_LOCAL_REPOSITORY", hh.LocalRepository()},
   183  		{"HELM_PATH_STARTER", hh.Starters()},
   184  		{"TILLER_HOST", tillerHost},
   185  		{"TILLER_NAMESPACE", tillerNamespace},
   186  	} {
   187  		if got := os.Getenv(tt.name); got != tt.expect {
   188  			t.Errorf("Expected $%s=%q, got %q", tt.name, tt.expect, got)
   189  		}
   190  	}
   191  }