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