github.com/wangchanggan/helm@v0.0.0-20211020154240-11b1b7d5406d/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  	"k8s.io/helm/pkg/helm/environment"
    26  	"k8s.io/helm/pkg/helm/helmpath"
    27  )
    28  
    29  func hh(debug bool) environment.EnvSettings {
    30  	apath, err := filepath.Abs("./testdata")
    31  	if err != nil {
    32  		panic(err)
    33  	}
    34  	hp := helmpath.Home(apath)
    35  	return environment.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("test://foo/bar", "", "", "")
    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  func TestPluginSubCommands(t *testing.T) {
    98  	if runtime.GOOS == "windows" {
    99  		t.Skip("TODO: refactor this test to work on windows")
   100  	}
   101  
   102  	oldhh := os.Getenv("HELM_HOME")
   103  	defer os.Setenv("HELM_HOME", oldhh)
   104  	os.Setenv("HELM_HOME", "")
   105  
   106  	env := hh(false)
   107  	pg := newPluginGetter("echo -n", env, "test", ".")
   108  	g, err := pg("test://foo/bar", "", "", "")
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  
   113  	data, err := g.Get("test://foo/bar")
   114  	if err != nil {
   115  		t.Fatal(err)
   116  	}
   117  
   118  	expect := "   test://foo/bar"
   119  	got := data.String()
   120  	if got != expect {
   121  		t.Errorf("Expected %q, got %q", expect, got)
   122  	}
   123  }