From e81d56a2476a23f1ee7114ec2b32ec0027b8383f Mon Sep 17 00:00:00 2001 From: George Jenkins Date: Fri, 6 Dec 2024 21:46:56 -0500 Subject: [PATCH] more --- cmd/helm/dependency_build_test.go | 5 +++- cmd/helm/dependency_update_test.go | 15 +++++++++--- cmd/helm/install_test.go | 6 ++++- cmd/helm/pull_test.go | 11 +++++++-- cmd/helm/repo_add_test.go | 31 ++++++++++++++++++++----- cmd/helm/repo_remove_test.go | 11 +++++++-- cmd/helm/repo_update_test.go | 20 ++++++++++++---- cmd/helm/show_test.go | 5 +++- pkg/downloader/chart_downloader_test.go | 17 +++++++++++--- pkg/downloader/manager_test.go | 15 +++++++++--- pkg/repo/repotest/server.go | 28 ++++++++++++++-------- pkg/repo/repotest/server_test.go | 11 +++++++-- 12 files changed, 137 insertions(+), 38 deletions(-) diff --git a/cmd/helm/dependency_build_test.go b/cmd/helm/dependency_build_test.go index 391110118..b709d70de 100644 --- a/cmd/helm/dependency_build_test.go +++ b/cmd/helm/dependency_build_test.go @@ -29,7 +29,10 @@ import ( ) func TestDependencyBuildCmd(t *testing.T) { - srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testcharts/*.tgz")) + srv := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/testcharts/*.tgz"), + ) defer srv.Stop() rootDir := srv.Root() diff --git a/cmd/helm/dependency_update_test.go b/cmd/helm/dependency_update_test.go index f1f092b50..5f8ddc7ec 100644 --- a/cmd/helm/dependency_update_test.go +++ b/cmd/helm/dependency_update_test.go @@ -32,7 +32,10 @@ import ( ) func TestDependencyUpdateCmd(t *testing.T) { - srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testcharts/*.tgz")) + srv := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/testcharts/*.tgz"), + ) defer srv.Stop() t.Logf("Listening on directory %s", srv.Root()) @@ -148,7 +151,10 @@ func TestDependencyUpdateCmd_DoNotDeleteOldChartsOnError(t *testing.T) { defer resetEnv()() ensure.HelmHome(t) - srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testcharts/*.tgz")) + srv := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/testcharts/*.tgz"), + ) defer srv.Stop() t.Logf("Listening on directory %s", srv.Root()) @@ -242,7 +248,10 @@ func TestDependencyUpdateCmd_WithRepoThatWasNotAdded(t *testing.T) { } func setupMockRepoServer(t *testing.T) *repotest.Server { - srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testcharts/*.tgz")) + srv := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/testcharts/*.tgz"), + ) t.Logf("Listening on directory %s", srv.Root()) diff --git a/cmd/helm/install_test.go b/cmd/helm/install_test.go index d8a2ed9a0..c3ea9c089 100644 --- a/cmd/helm/install_test.go +++ b/cmd/helm/install_test.go @@ -27,7 +27,11 @@ import ( ) func TestInstall(t *testing.T) { - srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testcharts/*.tgz*"), repotest.WithBasicAuth()) + srv := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/testcharts/*.tgz*"), + repotest.WithMiddleware(repotest.BasicAuthMiddleware(t)), + ) defer srv.Stop() srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/cmd/helm/pull_test.go b/cmd/helm/pull_test.go index bf73b6710..08f97b5e3 100644 --- a/cmd/helm/pull_test.go +++ b/cmd/helm/pull_test.go @@ -28,7 +28,10 @@ import ( ) func TestPullCmd(t *testing.T) { - srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testcharts/*.tgz*")) + srv := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/testcharts/*.tgz*"), + ) defer srv.Stop() ociSrv, err := repotest.NewOCIServer(t, srv.Root()) @@ -249,7 +252,11 @@ func TestPullCmd(t *testing.T) { } func TestPullWithCredentialsCmd(t *testing.T) { - srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testcharts/*.tgz*"), repotest.WithBasicAuth()) + srv := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/testcharts/*.tgz*"), + repotest.WithMiddleware(repotest.BasicAuthMiddleware(t)), + ) defer srv.Stop() srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/cmd/helm/repo_add_test.go b/cmd/helm/repo_add_test.go index ae8cc4b7c..58efe2b22 100644 --- a/cmd/helm/repo_add_test.go +++ b/cmd/helm/repo_add_test.go @@ -34,11 +34,17 @@ import ( ) func TestRepoAddCmd(t *testing.T) { - srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*")) + srv := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/testserver/*.*"), + ) defer srv.Stop() // A second test server is setup to verify URL changing - srv2 := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*")) + srv2 := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/testserver/*.*"), + ) defer srv2.Stop() tmpdir := filepath.Join(t.TempDir(), "path-component.yaml/data") @@ -74,7 +80,10 @@ func TestRepoAddCmd(t *testing.T) { } func TestRepoAdd(t *testing.T) { - ts := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*")) + ts := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/testserver/*.*"), + ) defer ts.Stop() rootDir := t.TempDir() @@ -125,7 +134,10 @@ func TestRepoAdd(t *testing.T) { } func TestRepoAddCheckLegalName(t *testing.T) { - ts := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*")) + ts := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/testserver/*.*"), + ) defer ts.Stop() defer resetEnv()() @@ -179,7 +191,10 @@ func TestRepoAddConcurrentHiddenFile(t *testing.T) { } func repoAddConcurrent(t *testing.T, testName, repoFile string) { - ts := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*")) + ts := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/testserver/*.*"), + ) defer ts.Stop() var wg sync.WaitGroup @@ -227,7 +242,11 @@ func TestRepoAddFileCompletion(t *testing.T) { } func TestRepoAddWithPasswordFromStdin(t *testing.T) { - srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*"), repotest.WithBasicAuth()) + srv := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/testserver/*.*"), + repotest.WithMiddleware(repotest.BasicAuthMiddleware(t)), + ) defer srv.Stop() defer resetEnv()() diff --git a/cmd/helm/repo_remove_test.go b/cmd/helm/repo_remove_test.go index 92e0087a2..89590cf18 100644 --- a/cmd/helm/repo_remove_test.go +++ b/cmd/helm/repo_remove_test.go @@ -30,7 +30,10 @@ import ( ) func TestRepoRemove(t *testing.T) { - ts := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*")) + ts := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/testserver/*.*"), + ) defer ts.Stop() rootDir := t.TempDir() @@ -159,7 +162,11 @@ func testCacheFiles(t *testing.T, cacheIndexFile string, cacheChartsFile string, } func TestRepoRemoveCompletion(t *testing.T) { - ts := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*")) + ts := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/testserver/*.*"), + ) + defer ts.Stop() rootDir := t.TempDir() diff --git a/cmd/helm/repo_update_test.go b/cmd/helm/repo_update_test.go index 0e4fd2d7d..a1ecfe87b 100644 --- a/cmd/helm/repo_update_test.go +++ b/cmd/helm/repo_update_test.go @@ -106,7 +106,11 @@ func TestUpdateCustomCacheCmd(t *testing.T) { cachePath := filepath.Join(rootDir, "updcustomcache") os.Mkdir(cachePath, os.ModePerm) - ts := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*")) + ts := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/testserver/*.*"), + ) + defer ts.Stop() o := &repoUpdateOptions{ @@ -127,7 +131,9 @@ func TestUpdateCharts(t *testing.T) { defer resetEnv()() ensure.HelmHome(t) - ts := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*")) + ts := repotest.NewTempServer(t, + repotest.WithChartSourceGlob("testdata/testserver/*.*"), + ) defer ts.Stop() r, err := repo.NewChartRepository(&repo.Entry{ @@ -159,7 +165,10 @@ func TestUpdateChartsFail(t *testing.T) { defer resetEnv()() ensure.HelmHome(t) - ts := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*")) + ts := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/testserver/*.*"), + ) defer ts.Stop() var invalidURL = ts.URL() + "55" @@ -189,7 +198,10 @@ func TestUpdateChartsFailWithError(t *testing.T) { defer resetEnv()() ensure.HelmHome(t) - ts := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*")) + ts := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/testserver/*.*"), + ) defer ts.Stop() var invalidURL = ts.URL() + "55" diff --git a/cmd/helm/show_test.go b/cmd/helm/show_test.go index afc9ef514..83bd9d4c2 100644 --- a/cmd/helm/show_test.go +++ b/cmd/helm/show_test.go @@ -26,7 +26,10 @@ import ( ) func TestShowPreReleaseChart(t *testing.T) { - srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testcharts/*.tgz*")) + srv := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/testcharts/*.tgz*"), + ) defer srv.Stop() if err := srv.LinkIndices(); err != nil { diff --git a/pkg/downloader/chart_downloader_test.go b/pkg/downloader/chart_downloader_test.go index 0902a1e17..49b76f80c 100644 --- a/pkg/downloader/chart_downloader_test.go +++ b/pkg/downloader/chart_downloader_test.go @@ -171,7 +171,11 @@ func TestIsTar(t *testing.T) { } func TestDownloadTo(t *testing.T) { - srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/*.tgz*"), repotest.WithBasicAuth()) + srv := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/*.tgz*"), + repotest.WithMiddleware(repotest.BasicAuthMiddleware(t)), + ) defer srv.Stop() if err := srv.CreateIndex(); err != nil { t.Fatal(err) @@ -218,7 +222,11 @@ 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.WithTLS()) + srv := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/*.tgz*"), + repotest.WithTLS(), + ) defer srv.Stop() if err := srv.CreateIndex(); err != nil { t.Fatal(err) @@ -269,7 +277,10 @@ func TestDownloadTo_VerifyLater(t *testing.T) { dest := t.TempDir() // Set up a fake repo - srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/*.tgz*")) + srv := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/*.tgz*"), + ) defer srv.Stop() if err := srv.LinkIndices(); err != nil { t.Fatal(err) diff --git a/pkg/downloader/manager_test.go b/pkg/downloader/manager_test.go index a840a41e8..914417af9 100644 --- a/pkg/downloader/manager_test.go +++ b/pkg/downloader/manager_test.go @@ -292,7 +292,10 @@ version: 0.1.0` func TestUpdateBeforeBuild(t *testing.T) { // Set up a fake repo - srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/*.tgz*")) + srv := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/*.tgz*"), + ) defer srv.Stop() if err := srv.LinkIndices(); err != nil { t.Fatal(err) @@ -358,7 +361,10 @@ func TestUpdateBeforeBuild(t *testing.T) { // to be fetched. func TestUpdateWithNoRepo(t *testing.T) { // Set up a fake repo - srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/*.tgz*")) + srv := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/*.tgz*"), + ) defer srv.Stop() if err := srv.LinkIndices(); err != nil { t.Fatal(err) @@ -427,7 +433,10 @@ func TestUpdateWithNoRepo(t *testing.T) { // If each of these main fields (name, version, repository) is not supplied by dep param, default value will be used. func checkBuildWithOptionalFields(t *testing.T, chartName string, dep chart.Dependency) { // Set up a fake repo - srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/*.tgz*")) + srv := repotest.NewTempServer( + t, + repotest.WithChartSourceGlob("testdata/*.tgz*"), + ) defer srv.Stop() if err := srv.LinkIndices(); err != nil { t.Fatal(err) diff --git a/pkg/repo/repotest/server.go b/pkg/repo/repotest/server.go index d2a15d92c..6af3d3769 100644 --- a/pkg/repo/repotest/server.go +++ b/pkg/repo/repotest/server.go @@ -33,14 +33,23 @@ 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" ) +func BasicAuthMiddleware(t *testing.T) http.HandlerFunc { + return http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { + username, password, ok := r.BasicAuth() + if !ok || username != "username" || password != "password" { + t.Errorf("Expected request to use basic auth and for username == 'username' and password == 'password', got '%v', '%s', '%s'", ok, username, password) + } + }) +} + type ServerOption func(*testing.T, *Server) func WithTLS() ServerOption { @@ -49,14 +58,9 @@ func WithTLS() ServerOption { } } -func WithBasicAuth() ServerOption { - return func(t *testing.T, server *Server) { - server.middleware = http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { - username, password, ok := r.BasicAuth() - if !ok || username != "username" || password != "password" { - t.Errorf("Expected request to use basic auth and for username == 'username' and password == 'password', got '%v', '%s', '%s'", ok, username, password) - } - }) +func WithMiddleware(middleware http.HandlerFunc) ServerOption { + return func(_ *testing.T, server *Server) { + server.middleware = middleware } } @@ -369,10 +373,14 @@ func (s *Server) Start() { if s.useTLS { insecure := false - tlsConf, err := testdata.ReadTLSConfig(insecure) + cd := "../../testdata" + ca, pub, priv := filepath.Join(cd, "rootca.crt"), filepath.Join(cd, "crt.pem"), filepath.Join(cd, "key.pem") + + tlsConf, err := tlsutil.NewClientTLS(pub, priv, ca, insecure) if err != nil { panic(err) } + tlsConf.ServerName = "helm.sh" s.srv.TLS = tlsConf s.srv.StartTLS() diff --git a/pkg/repo/repotest/server_test.go b/pkg/repo/repotest/server_test.go index cb052c2fc..01415a05b 100644 --- a/pkg/repo/repotest/server_test.go +++ b/pkg/repo/repotest/server_test.go @@ -100,7 +100,10 @@ func TestServer(t *testing.T) { func TestNewTempServer(t *testing.T) { ensure.HelmHome(t) - srv := NewTempServer(t, WithChartSourceGlob("testdata/examplechart-0.1.0.tgz")) + srv := NewTempServer( + t, + WithChartSourceGlob("testdata/examplechart-0.1.0.tgz"), + ) defer srv.Stop() if srv.srv.URL == "" { @@ -137,7 +140,11 @@ 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()) + srv := NewTempServer( + t, + WithChartSourceGlob("testdata/examplechart-0.1.0.tgz"), + WithTLS(), + ) defer srv.Stop() if !strings.HasPrefix(srv.URL(), "https://") {