github.com/darkowlzz/helm@v2.5.1-0.20171213183701-6707fe0468d4+incompatible/pkg/plugin/installer/http_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  	"bytes"
    20  	"encoding/base64"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"k8s.io/helm/pkg/helm/helmpath"
    24  	"os"
    25  	"testing"
    26  )
    27  
    28  var _ Installer = new(HTTPInstaller)
    29  
    30  // Fake http client
    31  type TestHTTPGetter struct {
    32  	MockResponse *bytes.Buffer
    33  	MockError    error
    34  }
    35  
    36  func (t *TestHTTPGetter) Get(href string) (*bytes.Buffer, error) { return t.MockResponse, t.MockError }
    37  
    38  // Fake plugin tarball data
    39  var fakePluginB64 = "H4sIAKRj51kAA+3UX0vCUBgGcC9jn+Iwuk3Peza3GeyiUlJQkcogCOzgli7dJm4TvYk+a5+k479UqquUCJ/fLs549sLO2TnvWnJa9aXnjwujYdYLovxMhsPcfnHOLdNkOXthM/IVQQYjg2yyLLJ4kXGhLp5j0z3P41tZksqxmspL3B/O+j/XtZu1y8rdYzkOZRCxduKPk53ny6Wwz/GfIIf1As8lxzGJSmoHNLJZphKHG4YpTCE0wVk3DULfpSJ3DMMqkj3P5JfMYLdX1Vr9Ie/5E5cstcdC8K04iGLX5HaJuKpWL17F0TCIBi5pf/0pjtLhun5j3f9v6r7wfnI/H0eNp9d1/5P6Gez0vzo7wsoxfrAZbTny/o9k6J8z/VkO/LPlWdC1iVpbEEcq5nmeJ13LEtmbV0k2r2PrOs9PuuNglC5rL1Y5S/syXRQmutaNw1BGnnp8Wq3UG51WvX1da3bKtZtCN/R09DwAAAAAAAAAAAAAAAAAAADAb30AoMczDwAoAAA="
    40  
    41  func TestStripName(t *testing.T) {
    42  	if stripPluginName("fake-plugin-0.0.1.tar.gz") != "fake-plugin" {
    43  		t.Errorf("name does not match expected value")
    44  	}
    45  	if stripPluginName("fake-plugin-0.0.1.tgz") != "fake-plugin" {
    46  		t.Errorf("name does not match expected value")
    47  	}
    48  	if stripPluginName("fake-plugin.tgz") != "fake-plugin" {
    49  		t.Errorf("name does not match expected value")
    50  	}
    51  	if stripPluginName("fake-plugin.tar.gz") != "fake-plugin" {
    52  		t.Errorf("name does not match expected value")
    53  	}
    54  }
    55  
    56  func TestHTTPInstaller(t *testing.T) {
    57  	source := "https://repo.localdomain/plugins/fake-plugin-0.0.1.tar.gz"
    58  	hh, err := ioutil.TempDir("", "helm-home-")
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  	defer os.RemoveAll(hh)
    63  
    64  	home := helmpath.Home(hh)
    65  	if err := os.MkdirAll(home.Plugins(), 0755); err != nil {
    66  		t.Fatalf("Could not create %s: %s", home.Plugins(), err)
    67  	}
    68  
    69  	i, err := NewForSource(source, "0.0.1", home)
    70  	if err != nil {
    71  		t.Errorf("unexpected error: %s", err)
    72  	}
    73  
    74  	// ensure a HTTPInstaller was returned
    75  	httpInstaller, ok := i.(*HTTPInstaller)
    76  	if !ok {
    77  		t.Error("expected a HTTPInstaller")
    78  	}
    79  
    80  	// inject fake http client responding with minimal plugin tarball
    81  	mockTgz, err := base64.StdEncoding.DecodeString(fakePluginB64)
    82  	if err != nil {
    83  		t.Fatalf("Could not decode fake tgz plugin: %s", err)
    84  	}
    85  
    86  	httpInstaller.getter = &TestHTTPGetter{
    87  		MockResponse: bytes.NewBuffer(mockTgz),
    88  	}
    89  
    90  	// install the plugin
    91  	if err := Install(i); err != nil {
    92  		t.Error(err)
    93  	}
    94  	if i.Path() != home.Path("plugins", "fake-plugin") {
    95  		t.Errorf("expected path '$HELM_HOME/plugins/fake-plugin', got %q", i.Path())
    96  	}
    97  
    98  	// Install again to test plugin exists error
    99  	if err := Install(i); err == nil {
   100  		t.Error("expected error for plugin exists, got none")
   101  	} else if err.Error() != "plugin already exists" {
   102  		t.Errorf("expected error for plugin exists, got (%v)", err)
   103  	}
   104  
   105  }
   106  
   107  func TestHTTPInstallerNonExistentVersion(t *testing.T) {
   108  	source := "https://repo.localdomain/plugins/fake-plugin-0.0.2.tar.gz"
   109  	hh, err := ioutil.TempDir("", "helm-home-")
   110  	if err != nil {
   111  		t.Fatal(err)
   112  	}
   113  	defer os.RemoveAll(hh)
   114  
   115  	home := helmpath.Home(hh)
   116  	if err := os.MkdirAll(home.Plugins(), 0755); err != nil {
   117  		t.Fatalf("Could not create %s: %s", home.Plugins(), err)
   118  	}
   119  
   120  	i, err := NewForSource(source, "0.0.2", home)
   121  	if err != nil {
   122  		t.Errorf("unexpected error: %s", err)
   123  	}
   124  
   125  	// ensure a HTTPInstaller was returned
   126  	httpInstaller, ok := i.(*HTTPInstaller)
   127  	if !ok {
   128  		t.Error("expected a HTTPInstaller")
   129  	}
   130  
   131  	// inject fake http client responding with error
   132  	httpInstaller.getter = &TestHTTPGetter{
   133  		MockError: fmt.Errorf("failed to download plugin for some reason"),
   134  	}
   135  
   136  	// attempt to install the plugin
   137  	if err := Install(i); err == nil {
   138  		t.Error("expected error from http client")
   139  	}
   140  
   141  }
   142  
   143  func TestHTTPInstallerUpdate(t *testing.T) {
   144  	source := "https://repo.localdomain/plugins/fake-plugin-0.0.1.tar.gz"
   145  	hh, err := ioutil.TempDir("", "helm-home-")
   146  	if err != nil {
   147  		t.Fatal(err)
   148  	}
   149  	defer os.RemoveAll(hh)
   150  
   151  	home := helmpath.Home(hh)
   152  	if err := os.MkdirAll(home.Plugins(), 0755); err != nil {
   153  		t.Fatalf("Could not create %s: %s", home.Plugins(), err)
   154  	}
   155  
   156  	i, err := NewForSource(source, "0.0.1", home)
   157  	if err != nil {
   158  		t.Errorf("unexpected error: %s", err)
   159  	}
   160  
   161  	// ensure a HTTPInstaller was returned
   162  	httpInstaller, ok := i.(*HTTPInstaller)
   163  	if !ok {
   164  		t.Error("expected a HTTPInstaller")
   165  	}
   166  
   167  	// inject fake http client responding with minimal plugin tarball
   168  	mockTgz, err := base64.StdEncoding.DecodeString(fakePluginB64)
   169  	if err != nil {
   170  		t.Fatalf("Could not decode fake tgz plugin: %s", err)
   171  	}
   172  
   173  	httpInstaller.getter = &TestHTTPGetter{
   174  		MockResponse: bytes.NewBuffer(mockTgz),
   175  	}
   176  
   177  	// install the plugin before updating
   178  	if err := Install(i); err != nil {
   179  		t.Error(err)
   180  	}
   181  	if i.Path() != home.Path("plugins", "fake-plugin") {
   182  		t.Errorf("expected path '$HELM_HOME/plugins/fake-plugin', got %q", i.Path())
   183  	}
   184  
   185  	// Update plugin, should fail because it is not implemented
   186  	if err := Update(i); err == nil {
   187  		t.Error("update method not implemented for http installer")
   188  	}
   189  }