...

Source file src/helm.sh/helm/v3/pkg/plugin/installer/vcs_installer_test.go

Documentation: helm.sh/helm/v3/pkg/plugin/installer

     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/v3/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/v3/internal/test/ensure"
    27  	"helm.sh/helm/v3/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  	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/good/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.Fatalf("expected version '0.1.1', got %q", repo.current)
    84  	}
    85  	if i.Path() != helmpath.DataPath("plugins", "helm-env") {
    86  		t.Fatalf("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.Fatalf("expected error for plugin exists, got none")
    92  	} else if err.Error() != "plugin already exists" {
    93  		t.Fatalf("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.Fatalf("expected error for inability to find plugin source, got none")
    99  	} else if err.Error() != "cannot get information about plugin source" {
   100  		t.Fatalf("expected error for inability to find plugin source, got (%v)", err)
   101  	}
   102  }
   103  
   104  func TestVCSInstallerNonExistentVersion(t *testing.T) {
   105  	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  	if _, ok := i.(*VCSInstaller); !ok {
   117  		t.Fatal("expected a VCSInstaller")
   118  	}
   119  
   120  	if err := Install(i); err == nil {
   121  		t.Fatalf("expected error for version does not exists, got none")
   122  	} else if err.Error() != fmt.Sprintf("requested version %q does not exist for plugin %q", version, source) {
   123  		t.Fatalf("expected error for version does not exists, got (%v)", err)
   124  	}
   125  }
   126  func TestVCSInstallerUpdate(t *testing.T) {
   127  	ensure.HelmHome(t)
   128  
   129  	source := "https://github.com/adamreese/helm-env"
   130  
   131  	i, err := NewForSource(source, "")
   132  	if err != nil {
   133  		t.Fatalf("unexpected error: %s", err)
   134  	}
   135  
   136  	// ensure a VCSInstaller was returned
   137  	if _, ok := i.(*VCSInstaller); !ok {
   138  		t.Fatal("expected a VCSInstaller")
   139  	}
   140  
   141  	if err := Update(i); err == nil {
   142  		t.Fatal("expected error for plugin does not exist, got none")
   143  	} else if err.Error() != "plugin does not exist" {
   144  		t.Fatalf("expected error for plugin does not exist, got (%v)", err)
   145  	}
   146  
   147  	// Install plugin before update
   148  	if err := Install(i); err != nil {
   149  		t.Fatal(err)
   150  	}
   151  
   152  	// Test FindSource method for positive result
   153  	pluginInfo, err := FindSource(i.Path())
   154  	if err != nil {
   155  		t.Fatal(err)
   156  	}
   157  
   158  	vcsInstaller := pluginInfo.(*VCSInstaller)
   159  
   160  	repoRemote := 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  	if err := os.Remove(filepath.Join(vcsInstaller.Repo.LocalPath(), "plugin.yaml")); err != nil {
   172  		t.Fatal(err)
   173  	}
   174  	// Testing update for error
   175  	if err := Update(vcsInstaller); err == nil {
   176  		t.Fatalf("expected error for plugin modified, got none")
   177  	} else if err.Error() != "plugin repo was modified" {
   178  		t.Fatalf("expected error for plugin modified, got (%v)", err)
   179  	}
   180  
   181  }
   182  

View as plain text