github.com/zoumo/helm@v2.5.0+incompatible/pkg/plugin/installer/vcs_installer_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 installer // import "k8s.io/helm/pkg/plugin/installer"
    17  
    18  import (
    19  	"fmt"
    20  	"io/ioutil"
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	"k8s.io/helm/pkg/helm/helmpath"
    26  
    27  	"github.com/Masterminds/vcs"
    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  	hh, err := ioutil.TempDir("", "helm-home-")
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  	defer os.RemoveAll(hh)
    57  
    58  	home := helmpath.Home(hh)
    59  	if err := os.MkdirAll(home.Plugins(), 0755); err != nil {
    60  		t.Fatalf("Could not create %s: %s", home.Plugins(), err)
    61  	}
    62  
    63  	source := "https://github.com/adamreese/helm-env"
    64  	testRepoPath, _ := filepath.Abs("../testdata/plugdir/echo")
    65  	repo := &testRepo{
    66  		local: testRepoPath,
    67  		tags:  []string{"0.1.0", "0.1.1"},
    68  	}
    69  
    70  	i, err := NewForSource(source, "~0.1.0", home)
    71  	if err != nil {
    72  		t.Errorf("unexpected error: %s", err)
    73  	}
    74  
    75  	// ensure a VCSInstaller was returned
    76  	vcsInstaller, ok := i.(*VCSInstaller)
    77  	if !ok {
    78  		t.Error("expected a VCSInstaller")
    79  	}
    80  
    81  	// set the testRepo in the VCSInstaller
    82  	vcsInstaller.Repo = repo
    83  
    84  	if err := Install(i); err != nil {
    85  		t.Error(err)
    86  	}
    87  	if repo.current != "0.1.1" {
    88  		t.Errorf("expected version '0.1.1', got %q", repo.current)
    89  	}
    90  	if i.Path() != home.Path("plugins", "helm-env") {
    91  		t.Errorf("expected path '$HELM_HOME/plugins/helm-env', got %q", i.Path())
    92  	}
    93  
    94  	// Install again to test plugin exists error
    95  	if err := Install(i); err == nil {
    96  		t.Error("expected error for plugin exists, got none")
    97  	} else if err.Error() != "plugin already exists" {
    98  		t.Errorf("expected error for plugin exists, got (%v)", err)
    99  	}
   100  
   101  	//Testing FindSource method, expect error because plugin code is not a cloned repository
   102  	if _, err := FindSource(i.Path(), home); err == nil {
   103  		t.Error("expected error for inability to find plugin source, got none")
   104  	} else if err.Error() != "cannot get information about plugin source" {
   105  		t.Errorf("expected error for inability to find plugin source, got (%v)", err)
   106  	}
   107  }
   108  
   109  func TestVCSInstallerNonExistentVersion(t *testing.T) {
   110  	hh, err := ioutil.TempDir("", "helm-home-")
   111  	if err != nil {
   112  		t.Fatal(err)
   113  	}
   114  	defer os.RemoveAll(hh)
   115  
   116  	home := helmpath.Home(hh)
   117  	if err := os.MkdirAll(home.Plugins(), 0755); err != nil {
   118  		t.Fatalf("Could not create %s: %s", home.Plugins(), err)
   119  	}
   120  
   121  	source := "https://github.com/adamreese/helm-env"
   122  	version := "0.2.0"
   123  
   124  	i, err := NewForSource(source, version, home)
   125  	if err != nil {
   126  		t.Errorf("unexpected error: %s", err)
   127  	}
   128  
   129  	// ensure a VCSInstaller was returned
   130  	_, ok := i.(*VCSInstaller)
   131  	if !ok {
   132  		t.Error("expected a VCSInstaller")
   133  	}
   134  
   135  	if err := Install(i); err == nil {
   136  		t.Error("expected error for version does not exists, got none")
   137  	} else if err.Error() != fmt.Sprintf("requested version %q does not exist for plugin %q", version, source) {
   138  		t.Errorf("expected error for version does not exists, got (%v)", err)
   139  	}
   140  }
   141  func TestVCSInstallerUpdate(t *testing.T) {
   142  
   143  	hh, err := ioutil.TempDir("", "helm-home-")
   144  	if err != nil {
   145  		t.Fatal(err)
   146  	}
   147  	defer os.RemoveAll(hh)
   148  
   149  	home := helmpath.Home(hh)
   150  	if err := os.MkdirAll(home.Plugins(), 0755); err != nil {
   151  		t.Fatalf("Could not create %s: %s", home.Plugins(), err)
   152  	}
   153  
   154  	source := "https://github.com/adamreese/helm-env"
   155  
   156  	i, err := NewForSource(source, "", home)
   157  	if err != nil {
   158  		t.Errorf("unexpected error: %s", err)
   159  	}
   160  
   161  	// ensure a VCSInstaller was returned
   162  	_, ok := i.(*VCSInstaller)
   163  	if !ok {
   164  		t.Error("expected a VCSInstaller")
   165  	}
   166  
   167  	if err := Update(i); err == nil {
   168  		t.Error("expected error for plugin does not exist, got none")
   169  	} else if err.Error() != "plugin does not exist" {
   170  		t.Errorf("expected error for plugin does not exist, got (%v)", err)
   171  	}
   172  
   173  	// Install plugin before update
   174  	if err := Install(i); err != nil {
   175  		t.Error(err)
   176  	}
   177  
   178  	// Test FindSource method for positive result
   179  	pluginInfo, err := FindSource(i.Path(), home)
   180  	if err != nil {
   181  		t.Error(err)
   182  	}
   183  
   184  	repoRemote := pluginInfo.(*VCSInstaller).Repo.Remote()
   185  	if repoRemote != source {
   186  		t.Errorf("invalid source found, expected %q got %q", source, repoRemote)
   187  	}
   188  
   189  	// Update plugin
   190  	if err := Update(i); err != nil {
   191  		t.Error(err)
   192  	}
   193  
   194  	// Test update failure
   195  	os.Remove(filepath.Join(i.Path(), "plugin.yaml"))
   196  	// Testing update for error
   197  	if err := Update(i); err == nil {
   198  		t.Error("expected error for plugin modified, got none")
   199  	} else if err.Error() != "plugin repo was modified" {
   200  		t.Errorf("expected error for plugin modified, got (%v)", err)
   201  	}
   202  
   203  }