github.com/qsis/helm@v3.0.0-beta.3+incompatible/pkg/plugin/installer/vcs_installer_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 installer // import "helm.sh/helm/pkg/plugin/installer"
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"github.com/Masterminds/vcs"
    25  
    26  	"helm.sh/helm/internal/test/ensure"
    27  	"helm.sh/helm/pkg/helmpath"
    28  )
    29  
    30  var _ Installer = new(VCSInstaller)
    31  
    32  type testRepo struct {
    33  	local, remote, current string
    34  	tags, branches         []string
    35  	err                    error
    36  	vcs.Repo
    37  }
    38  
    39  func (r *testRepo) LocalPath() string           { return r.local }
    40  func (r *testRepo) Remote() string              { return r.remote }
    41  func (r *testRepo) Update() error               { return r.err }
    42  func (r *testRepo) Get() error                  { return r.err }
    43  func (r *testRepo) IsReference(string) bool     { return false }
    44  func (r *testRepo) Tags() ([]string, error)     { return r.tags, r.err }
    45  func (r *testRepo) Branches() ([]string, error) { return r.branches, r.err }
    46  func (r *testRepo) UpdateVersion(version string) error {
    47  	r.current = version
    48  	return r.err
    49  }
    50  
    51  func TestVCSInstaller(t *testing.T) {
    52  	defer ensure.HelmHome(t)()
    53  
    54  	if err := os.MkdirAll(helmpath.DataPath("plugins"), 0755); err != nil {
    55  		t.Fatalf("Could not create %s: %s", helmpath.DataPath("plugins"), err)
    56  	}
    57  
    58  	source := "https://github.com/adamreese/helm-env"
    59  	testRepoPath, _ := filepath.Abs("../testdata/plugdir/echo")
    60  	repo := &testRepo{
    61  		local: testRepoPath,
    62  		tags:  []string{"0.1.0", "0.1.1"},
    63  	}
    64  
    65  	i, err := NewForSource(source, "~0.1.0")
    66  	if err != nil {
    67  		t.Fatalf("unexpected error: %s", err)
    68  	}
    69  
    70  	// ensure a VCSInstaller was returned
    71  	vcsInstaller, ok := i.(*VCSInstaller)
    72  	if !ok {
    73  		t.Fatal("expected a VCSInstaller")
    74  	}
    75  
    76  	// set the testRepo in the VCSInstaller
    77  	vcsInstaller.Repo = repo
    78  
    79  	if err := Install(i); err != nil {
    80  		t.Fatal(err)
    81  	}
    82  	if repo.current != "0.1.1" {
    83  		t.Errorf("expected version '0.1.1', got %q", repo.current)
    84  	}
    85  	if i.Path() != helmpath.DataPath("plugins", "helm-env") {
    86  		t.Errorf("expected path '$XDG_CONFIG_HOME/helm/plugins/helm-env', got %q", i.Path())
    87  	}
    88  
    89  	// Install again to test plugin exists error
    90  	if err := Install(i); err == nil {
    91  		t.Error("expected error for plugin exists, got none")
    92  	} else if err.Error() != "plugin already exists" {
    93  		t.Errorf("expected error for plugin exists, got (%v)", err)
    94  	}
    95  
    96  	// Testing FindSource method, expect error because plugin code is not a cloned repository
    97  	if _, err := FindSource(i.Path()); err == nil {
    98  		t.Error("expected error for inability to find plugin source, got none")
    99  	} else if err.Error() != "cannot get information about plugin source" {
   100  		t.Errorf("expected error for inability to find plugin source, got (%v)", err)
   101  	}
   102  }
   103  
   104  func TestVCSInstallerNonExistentVersion(t *testing.T) {
   105  	defer ensure.HelmHome(t)()
   106  
   107  	source := "https://github.com/adamreese/helm-env"
   108  	version := "0.2.0"
   109  
   110  	i, err := NewForSource(source, version)
   111  	if err != nil {
   112  		t.Fatalf("unexpected error: %s", err)
   113  	}
   114  
   115  	// ensure a VCSInstaller was returned
   116  	_, ok := i.(*VCSInstaller)
   117  	if !ok {
   118  		t.Fatal("expected a VCSInstaller")
   119  	}
   120  
   121  	if err := Install(i); err == nil {
   122  		t.Error("expected error for version does not exists, got none")
   123  	} else if err.Error() != fmt.Sprintf("requested version %q does not exist for plugin %q", version, source) {
   124  		t.Errorf("expected error for version does not exists, got (%v)", err)
   125  	}
   126  }
   127  func TestVCSInstallerUpdate(t *testing.T) {
   128  	defer ensure.HelmHome(t)()
   129  
   130  	source := "https://github.com/adamreese/helm-env"
   131  
   132  	i, err := NewForSource(source, "")
   133  	if err != nil {
   134  		t.Fatalf("unexpected error: %s", err)
   135  	}
   136  
   137  	// ensure a VCSInstaller was returned
   138  	_, ok := i.(*VCSInstaller)
   139  	if !ok {
   140  		t.Fatal("expected a VCSInstaller")
   141  	}
   142  
   143  	if err := Update(i); err == nil {
   144  		t.Fatal("expected error for plugin does not exist, got none")
   145  	} else if err.Error() != "plugin does not exist" {
   146  		t.Fatalf("expected error for plugin does not exist, got (%v)", err)
   147  	}
   148  
   149  	// Install plugin before update
   150  	if err := Install(i); err != nil {
   151  		t.Fatal(err)
   152  	}
   153  
   154  	// Test FindSource method for positive result
   155  	pluginInfo, err := FindSource(i.Path())
   156  	if err != nil {
   157  		t.Fatal(err)
   158  	}
   159  
   160  	repoRemote := pluginInfo.(*VCSInstaller).Repo.Remote()
   161  	if repoRemote != source {
   162  		t.Fatalf("invalid source found, expected %q got %q", source, repoRemote)
   163  	}
   164  
   165  	// Update plugin
   166  	if err := Update(i); err != nil {
   167  		t.Fatal(err)
   168  	}
   169  
   170  	// Test update failure
   171  	os.Remove(filepath.Join(i.Path(), "plugin.yaml"))
   172  	// Testing update for error
   173  	if err := Update(i); err == nil {
   174  		t.Error("expected error for plugin modified, got none")
   175  	} else if err.Error() != "plugin repo was modified" {
   176  		t.Errorf("expected error for plugin modified, got (%v)", err)
   177  	}
   178  
   179  }