github.com/azure-devops-engineer/helm@v3.0.0-alpha.2+incompatible/pkg/getter/plugingetter_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 getter
    17  
    18  import (
    19  	"os"
    20  	"path/filepath"
    21  	"runtime"
    22  	"strings"
    23  	"testing"
    24  
    25  	"helm.sh/helm/pkg/cli"
    26  	"helm.sh/helm/pkg/helmpath"
    27  )
    28  
    29  func hh(debug bool) cli.EnvSettings {
    30  	apath, err := filepath.Abs("./testdata")
    31  	if err != nil {
    32  		panic(err)
    33  	}
    34  	hp := helmpath.Home(apath)
    35  	return cli.EnvSettings{
    36  		Home:  hp,
    37  		Debug: debug,
    38  	}
    39  }
    40  
    41  func TestCollectPlugins(t *testing.T) {
    42  	// Reset HELM HOME to testdata.
    43  	oldhh := os.Getenv("HELM_HOME")
    44  	defer os.Setenv("HELM_HOME", oldhh)
    45  	os.Setenv("HELM_HOME", "")
    46  
    47  	env := hh(false)
    48  	p, err := collectPlugins(env)
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  
    53  	if len(p) != 2 {
    54  		t.Errorf("Expected 2 plugins, got %d: %v", len(p), p)
    55  	}
    56  
    57  	if _, err := p.ByScheme("test2"); err != nil {
    58  		t.Error(err)
    59  	}
    60  
    61  	if _, err := p.ByScheme("test"); err != nil {
    62  		t.Error(err)
    63  	}
    64  
    65  	if _, err := p.ByScheme("nosuchthing"); err == nil {
    66  		t.Fatal("did not expect protocol handler for nosuchthing")
    67  	}
    68  }
    69  
    70  func TestPluginGetter(t *testing.T) {
    71  	if runtime.GOOS == "windows" {
    72  		t.Skip("TODO: refactor this test to work on windows")
    73  	}
    74  
    75  	oldhh := os.Getenv("HELM_HOME")
    76  	defer os.Setenv("HELM_HOME", oldhh)
    77  	os.Setenv("HELM_HOME", "")
    78  
    79  	env := hh(false)
    80  	pg := NewPluginGetter("echo", env, "test", ".")
    81  	g, err := pg()
    82  	if err != nil {
    83  		t.Fatal(err)
    84  	}
    85  
    86  	data, err := g.Get("test://foo/bar")
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  
    91  	expect := "test://foo/bar"
    92  	got := strings.TrimSpace(data.String())
    93  	if got != expect {
    94  		t.Errorf("Expected %q, got %q", expect, got)
    95  	}
    96  }
    97  
    98  func TestPluginSubCommands(t *testing.T) {
    99  	if runtime.GOOS == "windows" {
   100  		t.Skip("TODO: refactor this test to work on windows")
   101  	}
   102  
   103  	oldhh := os.Getenv("HELM_HOME")
   104  	defer os.Setenv("HELM_HOME", oldhh)
   105  	os.Setenv("HELM_HOME", "")
   106  
   107  	env := hh(false)
   108  	pg := NewPluginGetter("echo -n", env, "test", ".")
   109  	g, err := pg()
   110  	if err != nil {
   111  		t.Fatal(err)
   112  	}
   113  
   114  	data, err := g.Get("test://foo/bar")
   115  	if err != nil {
   116  		t.Fatal(err)
   117  	}
   118  
   119  	expect := "   test://foo/bar"
   120  	got := data.String()
   121  	if got != expect {
   122  		t.Errorf("Expected %q, got %q", expect, got)
   123  	}
   124  }