github.com/x-helm/helm@v3.0.0-beta.3+incompatible/pkg/plugin/installer/http_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  	"bytes"
    20  	"encoding/base64"
    21  	"os"
    22  	"testing"
    23  
    24  	"github.com/pkg/errors"
    25  
    26  	"helm.sh/helm/internal/test/ensure"
    27  	"helm.sh/helm/pkg/getter"
    28  	"helm.sh/helm/pkg/helmpath"
    29  )
    30  
    31  var _ Installer = new(HTTPInstaller)
    32  
    33  // Fake http client
    34  type TestHTTPGetter struct {
    35  	MockResponse *bytes.Buffer
    36  	MockError    error
    37  }
    38  
    39  func (t *TestHTTPGetter) Get(href string, _ ...getter.Option) (*bytes.Buffer, error) {
    40  	return t.MockResponse, t.MockError
    41  }
    42  
    43  // Fake plugin tarball data
    44  var fakePluginB64 = "H4sIAKRj51kAA+3UX0vCUBgGcC9jn+Iwuk3Peza3GeyiUlJQkcogCOzgli7dJm4TvYk+a5+k479UqquUCJ/fLs549sLO2TnvWnJa9aXnjwujYdYLovxMhsPcfnHOLdNkOXthM/IVQQYjg2yyLLJ4kXGhLp5j0z3P41tZksqxmspL3B/O+j/XtZu1y8rdYzkOZRCxduKPk53ny6Wwz/GfIIf1As8lxzGJSmoHNLJZphKHG4YpTCE0wVk3DULfpSJ3DMMqkj3P5JfMYLdX1Vr9Ie/5E5cstcdC8K04iGLX5HaJuKpWL17F0TCIBi5pf/0pjtLhun5j3f9v6r7wfnI/H0eNp9d1/5P6Gez0vzo7wsoxfrAZbTny/o9k6J8z/VkO/LPlWdC1iVpbEEcq5nmeJ13LEtmbV0k2r2PrOs9PuuNglC5rL1Y5S/syXRQmutaNw1BGnnp8Wq3UG51WvX1da3bKtZtCN/R09DwAAAAAAAAAAAAAAAAAAADAb30AoMczDwAoAAA="
    45  
    46  func TestStripName(t *testing.T) {
    47  	if stripPluginName("fake-plugin-0.0.1.tar.gz") != "fake-plugin" {
    48  		t.Errorf("name does not match expected value")
    49  	}
    50  	if stripPluginName("fake-plugin-0.0.1.tgz") != "fake-plugin" {
    51  		t.Errorf("name does not match expected value")
    52  	}
    53  	if stripPluginName("fake-plugin.tgz") != "fake-plugin" {
    54  		t.Errorf("name does not match expected value")
    55  	}
    56  	if stripPluginName("fake-plugin.tar.gz") != "fake-plugin" {
    57  		t.Errorf("name does not match expected value")
    58  	}
    59  }
    60  
    61  func TestHTTPInstaller(t *testing.T) {
    62  	defer ensure.HelmHome(t)()
    63  	source := "https://repo.localdomain/plugins/fake-plugin-0.0.1.tar.gz"
    64  
    65  	if err := os.MkdirAll(helmpath.DataPath("plugins"), 0755); err != nil {
    66  		t.Fatalf("Could not create %s: %s", helmpath.DataPath("plugins"), err)
    67  	}
    68  
    69  	i, err := NewForSource(source, "0.0.1")
    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() != helmpath.DataPath("plugins", "fake-plugin") {
    95  		t.Errorf("expected path '$XDG_CONFIG_HOME/helm/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  	defer ensure.HelmHome(t)()
   109  	source := "https://repo.localdomain/plugins/fake-plugin-0.0.2.tar.gz"
   110  
   111  	if err := os.MkdirAll(helmpath.DataPath("plugins"), 0755); err != nil {
   112  		t.Fatalf("Could not create %s: %s", helmpath.DataPath("plugins"), err)
   113  	}
   114  
   115  	i, err := NewForSource(source, "0.0.2")
   116  	if err != nil {
   117  		t.Errorf("unexpected error: %s", err)
   118  	}
   119  
   120  	// ensure a HTTPInstaller was returned
   121  	httpInstaller, ok := i.(*HTTPInstaller)
   122  	if !ok {
   123  		t.Error("expected a HTTPInstaller")
   124  	}
   125  
   126  	// inject fake http client responding with error
   127  	httpInstaller.getter = &TestHTTPGetter{
   128  		MockError: errors.Errorf("failed to download plugin for some reason"),
   129  	}
   130  
   131  	// attempt to install the plugin
   132  	if err := Install(i); err == nil {
   133  		t.Error("expected error from http client")
   134  	}
   135  
   136  }
   137  
   138  func TestHTTPInstallerUpdate(t *testing.T) {
   139  	source := "https://repo.localdomain/plugins/fake-plugin-0.0.1.tar.gz"
   140  	defer ensure.HelmHome(t)()
   141  
   142  	if err := os.MkdirAll(helmpath.DataPath("plugins"), 0755); err != nil {
   143  		t.Fatalf("Could not create %s: %s", helmpath.DataPath("plugins"), err)
   144  	}
   145  
   146  	i, err := NewForSource(source, "0.0.1")
   147  	if err != nil {
   148  		t.Errorf("unexpected error: %s", err)
   149  	}
   150  
   151  	// ensure a HTTPInstaller was returned
   152  	httpInstaller, ok := i.(*HTTPInstaller)
   153  	if !ok {
   154  		t.Error("expected a HTTPInstaller")
   155  	}
   156  
   157  	// inject fake http client responding with minimal plugin tarball
   158  	mockTgz, err := base64.StdEncoding.DecodeString(fakePluginB64)
   159  	if err != nil {
   160  		t.Fatalf("Could not decode fake tgz plugin: %s", err)
   161  	}
   162  
   163  	httpInstaller.getter = &TestHTTPGetter{
   164  		MockResponse: bytes.NewBuffer(mockTgz),
   165  	}
   166  
   167  	// install the plugin before updating
   168  	if err := Install(i); err != nil {
   169  		t.Error(err)
   170  	}
   171  	if i.Path() != helmpath.DataPath("plugins", "fake-plugin") {
   172  		t.Errorf("expected path '$XDG_CONFIG_HOME/helm/plugins/fake-plugin', got %q", i.Path())
   173  	}
   174  
   175  	// Update plugin, should fail because it is not implemented
   176  	if err := Update(i); err == nil {
   177  		t.Error("update method not implemented for http installer")
   178  	}
   179  }