From 1d4bdfa5784579f3ce3b7f57ef698b465b28ae10 Mon Sep 17 00:00:00 2001 From: George Jenkins Date: Mon, 2 Dec 2024 12:57:24 -0500 Subject: [PATCH] more cleanup Signed-off-by: George Jenkins --- pkg/downloader/chart_downloader_test.go | 2 +- pkg/repo/repotest/server.go | 52 +++++++++---------------- pkg/repo/repotest/server_test.go | 12 ++++++ 3 files changed, 31 insertions(+), 35 deletions(-) diff --git a/pkg/downloader/chart_downloader_test.go b/pkg/downloader/chart_downloader_test.go index 13e901ef6..0902a1e17 100644 --- a/pkg/downloader/chart_downloader_test.go +++ b/pkg/downloader/chart_downloader_test.go @@ -218,7 +218,7 @@ func TestDownloadTo(t *testing.T) { func TestDownloadTo_TLS(t *testing.T) { // Set up mock server w/ tls enabled - srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/*.tgz*"), repotest.WithAutostart("tls")) + srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/*.tgz*"), repotest.WithTLS()) defer srv.Stop() if err := srv.CreateIndex(); err != nil { t.Fatal(err) diff --git a/pkg/repo/repotest/server.go b/pkg/repo/repotest/server.go index 4253a1cc3..9fde6d277 100644 --- a/pkg/repo/repotest/server.go +++ b/pkg/repo/repotest/server.go @@ -33,19 +33,19 @@ import ( "golang.org/x/crypto/bcrypt" "sigs.k8s.io/yaml" - "helm.sh/helm/v3/internal/tlsutil" "helm.sh/helm/v3/pkg/chart" "helm.sh/helm/v3/pkg/chart/loader" "helm.sh/helm/v3/pkg/chartutil" ociRegistry "helm.sh/helm/v3/pkg/registry" "helm.sh/helm/v3/pkg/repo" + "helm.sh/helm/v3/testdata" ) type ServerOption func(*testing.T, *Server) -func WithAutostart(autostartOption string) ServerOption { +func WithTLS() ServerOption { return func(_ *testing.T, server *Server) { - server.autostartOption = autostartOption + server.useTLS = true } } @@ -103,8 +103,7 @@ func NewTempServer(t *testing.T, options ...ServerOption) *Server { // for service. func NewServer(t *testing.T, docroot string, options ...ServerOption) *Server { srv := newServer(t, docroot, options...) - - autostartServer(t, srv) + srv.Start() return srv } @@ -117,8 +116,7 @@ func newServer(t *testing.T, docroot string, options ...ServerOption) *Server { } s := &Server{ - docroot: absdocroot, - autostartOption: "plain", + docroot: absdocroot, } for _, option := range options { @@ -132,7 +130,7 @@ func newServer(t *testing.T, docroot string, options ...ServerOption) *Server { http.FileServer(http.Dir(s.Root())).ServeHTTP(w, r) })) - autostartServer(t, s) + s.Start() // Add the testing repository as the only repo. Server must be started for the server's URL to be valid if err := setTestingRepository(s.URL(), filepath.Join(s.docroot, "repositories.yaml")); err != nil { @@ -142,24 +140,12 @@ func newServer(t *testing.T, docroot string, options ...ServerOption) *Server { return s } -func autostartServer(t *testing.T, srv *Server) { - switch srv.autostartOption { - case "none": - case "plain": - srv.Start() - case "tls": - srv.StartTLS() - default: - t.Fatalf("Invalid autostart option: %s", srv.autostartOption) - } -} - // Server is an implementation of a repository server for testing. type Server struct { docroot string srv *httptest.Server middleware http.HandlerFunc - autostartOption string + useTLS bool chartSourceGlob string } @@ -377,21 +363,19 @@ func (s *Server) CreateIndex() error { } func (s *Server) Start() { - s.srv.Start() -} + if s.useTLS { + insecure := false -func (s *Server) StartTLS() { - cd := "../../testdata" - ca, pub, priv := filepath.Join(cd, "rootca.crt"), filepath.Join(cd, "crt.pem"), filepath.Join(cd, "key.pem") - insecure := false - - tlsConf, err := tlsutil.NewClientTLS(pub, priv, ca, insecure) - if err != nil { - panic(err) + tlsConf, err := testdata.ReadTLSConfig(insecure) + if err != nil { + panic(err) + } + tlsConf.ServerName = "helm.sh" + s.srv.TLS = tlsConf + s.srv.StartTLS() + } else { + s.srv.Start() } - tlsConf.ServerName = "helm.sh" - s.srv.TLS = tlsConf - s.srv.StartTLS() } // Stop stops the server and closes all connections. diff --git a/pkg/repo/repotest/server_test.go b/pkg/repo/repotest/server_test.go index a9ce7c4bf..cb052c2fc 100644 --- a/pkg/repo/repotest/server_test.go +++ b/pkg/repo/repotest/server_test.go @@ -19,6 +19,7 @@ import ( "io" "net/http" "path/filepath" + "strings" "testing" "sigs.k8s.io/yaml" @@ -132,3 +133,14 @@ func TestNewTempServer(t *testing.T) { } } } + +func TestNewTempServer_TLS(t *testing.T) { + ensure.HelmHome(t) + + srv := NewTempServer(t, WithChartSourceGlob("testdata/examplechart-0.1.0.tgz"), WithTLS()) + defer srv.Stop() + + if !strings.HasPrefix(srv.URL(), "https://") { + t.Fatal("non-TLS server") + } +}