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