From 1f3b4cfa0427683c8e057962b479f1039091086f Mon Sep 17 00:00:00 2001 From: Suleiman Dibirov Date: Thu, 1 Aug 2024 18:32:30 +0300 Subject: [PATCH] fix: Add binary plugin url support Signed-off-by: Suleiman Dibirov --- pkg/plugin/installer/http_installer_test.go | 29 ++++++++++++++------- pkg/plugin/installer/installer.go | 8 ++++-- pkg/plugin/installer/installer_test.go | 10 ++++++- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/pkg/plugin/installer/http_installer_test.go b/pkg/plugin/installer/http_installer_test.go index c5af1f2cc..329841a24 100644 --- a/pkg/plugin/installer/http_installer_test.go +++ b/pkg/plugin/installer/http_installer_test.go @@ -66,22 +66,27 @@ func TestStripName(t *testing.T) { } } -func mockArchiveServer() *httptest.Server { +func mockArchiveServer(extensionToContentType map[string]string) *httptest.Server { return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if !strings.HasSuffix(r.URL.Path, ".tar.gz") { - w.Header().Add("Content-Type", "text/html") - fmt.Fprintln(w, "broken") - return + for ext, contentType := range extensionToContentType { + if strings.HasSuffix(r.URL.Path, ext) { + w.Header().Add("Content-Type", contentType) + fmt.Fprintln(w, "test") + return + } } - w.Header().Add("Content-Type", "application/gzip") - fmt.Fprintln(w, "test") + + w.Header().Add("Content-Type", "text/html") + fmt.Fprintln(w, "broken") })) } func TestHTTPInstaller(t *testing.T) { ensure.HelmHome(t) - srv := mockArchiveServer() + srv := mockArchiveServer(map[string]string{ + ".tar.gz": "application/gzip", + }) defer srv.Close() source := srv.URL + "/plugins/fake-plugin-0.0.1.tar.gz" @@ -129,7 +134,9 @@ func TestHTTPInstaller(t *testing.T) { func TestHTTPInstallerNonExistentVersion(t *testing.T) { ensure.HelmHome(t) - srv := mockArchiveServer() + srv := mockArchiveServer(map[string]string{ + ".tar.gz": "application/gzip", + }) defer srv.Close() source := srv.URL + "/plugins/fake-plugin-0.0.1.tar.gz" @@ -161,7 +168,9 @@ func TestHTTPInstallerNonExistentVersion(t *testing.T) { } func TestHTTPInstallerUpdate(t *testing.T) { - srv := mockArchiveServer() + srv := mockArchiveServer(map[string]string{ + ".tar.gz": "application/gzip", + }) defer srv.Close() source := srv.URL + "/plugins/fake-plugin-0.0.1.tar.gz" ensure.HelmHome(t) diff --git a/pkg/plugin/installer/installer.go b/pkg/plugin/installer/installer.go index 5fad58f99..327b66185 100644 --- a/pkg/plugin/installer/installer.go +++ b/pkg/plugin/installer/installer.go @@ -107,8 +107,12 @@ func isRemoteHTTPArchive(source string) bool { contentType := res.Header.Get("content-type") foundSuffix, ok := mediaTypeToExtension(contentType) if !ok { - // Media type not recognized - return false + if strings.HasSuffix(source, ".tar.gz") && contentType == "application/octet-stream" { + foundSuffix = ".tar.gz" + } else { + // Media type not recognized + return false + } } for suffix := range Extractors { diff --git a/pkg/plugin/installer/installer_test.go b/pkg/plugin/installer/installer_test.go index a11464924..c008b5542 100644 --- a/pkg/plugin/installer/installer_test.go +++ b/pkg/plugin/installer/installer_test.go @@ -18,9 +18,13 @@ package installer import "testing" func TestIsRemoteHTTPArchive(t *testing.T) { - srv := mockArchiveServer() + srv := mockArchiveServer(map[string]string{ + ".tar.gz": "application/gzip", + ".binary.tar.gz": "application/octet-stream", + }) defer srv.Close() source := srv.URL + "/plugins/fake-plugin-0.0.1.tar.gz" + binarySource := srv.URL + "/plugins/fake-plugin-0.0.1.binary.tar.gz" if isRemoteHTTPArchive("/not/a/URL") { t.Errorf("Expected non-URL to return false") @@ -37,4 +41,8 @@ func TestIsRemoteHTTPArchive(t *testing.T) { if isRemoteHTTPArchive(source + "-not-an-extension") { t.Error("Expected media type match to fail") } + + if !isRemoteHTTPArchive(binarySource) { + t.Errorf("Expected %q to be a valid archive URL", source) + } }