diff --git a/cmd/helm/dependency_build_test.go b/cmd/helm/dependency_build_test.go index 92ab252b8..391110118 100644 --- a/cmd/helm/dependency_build_test.go +++ b/cmd/helm/dependency_build_test.go @@ -29,7 +29,7 @@ import ( ) func TestDependencyBuildCmd(t *testing.T) { - srv := repotest.NewTempServer(t, "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 1713b5681..f1f092b50 100644 --- a/cmd/helm/dependency_update_test.go +++ b/cmd/helm/dependency_update_test.go @@ -32,7 +32,7 @@ import ( ) func TestDependencyUpdateCmd(t *testing.T) { - srv := repotest.NewTempServer(t, "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 +148,7 @@ func TestDependencyUpdateCmd_DoNotDeleteOldChartsOnError(t *testing.T) { defer resetEnv()() ensure.HelmHome(t) - srv := repotest.NewTempServer(t, "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 +242,7 @@ func TestDependencyUpdateCmd_WithRepoThatWasNotAdded(t *testing.T) { } func setupMockRepoServer(t *testing.T) *repotest.Server { - srv := repotest.NewTempServer(t, "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 f2a2f2366..d8a2ed9a0 100644 --- a/cmd/helm/install_test.go +++ b/cmd/helm/install_test.go @@ -27,7 +27,7 @@ import ( ) func TestInstall(t *testing.T) { - srv := repotest.NewTempServer(t, "testdata/testcharts/*.tgz*", repotest.WithBasicAuth()) + srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testcharts/*.tgz*"), repotest.WithBasicAuth()) 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 55b648102..bf73b6710 100644 --- a/cmd/helm/pull_test.go +++ b/cmd/helm/pull_test.go @@ -28,7 +28,7 @@ import ( ) func TestPullCmd(t *testing.T) { - srv := repotest.NewTempServer(t, "testdata/testcharts/*.tgz*") + srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testcharts/*.tgz*")) defer srv.Stop() ociSrv, err := repotest.NewOCIServer(t, srv.Root()) @@ -249,7 +249,7 @@ func TestPullCmd(t *testing.T) { } func TestPullWithCredentialsCmd(t *testing.T) { - srv := repotest.NewTempServer(t, "testdata/testcharts/*.tgz*", repotest.WithBasicAuth()) + srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testcharts/*.tgz*"), repotest.WithBasicAuth()) 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 7e0026470..ae8cc4b7c 100644 --- a/cmd/helm/repo_add_test.go +++ b/cmd/helm/repo_add_test.go @@ -34,11 +34,11 @@ import ( ) func TestRepoAddCmd(t *testing.T) { - srv := repotest.NewTempServer(t, "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, "testdata/testserver/*.*") + srv2 := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*")) defer srv2.Stop() tmpdir := filepath.Join(t.TempDir(), "path-component.yaml/data") @@ -74,7 +74,7 @@ func TestRepoAddCmd(t *testing.T) { } func TestRepoAdd(t *testing.T) { - ts := repotest.NewTempServer(t, "testdata/testserver/*.*") + ts := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*")) defer ts.Stop() rootDir := t.TempDir() @@ -125,7 +125,7 @@ func TestRepoAdd(t *testing.T) { } func TestRepoAddCheckLegalName(t *testing.T) { - ts := repotest.NewTempServer(t, "testdata/testserver/*.*") + ts := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*")) defer ts.Stop() defer resetEnv()() @@ -179,7 +179,7 @@ func TestRepoAddConcurrentHiddenFile(t *testing.T) { } func repoAddConcurrent(t *testing.T, testName, repoFile string) { - ts := repotest.NewTempServer(t, "testdata/testserver/*.*") + ts := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*")) defer ts.Stop() var wg sync.WaitGroup @@ -227,7 +227,7 @@ func TestRepoAddFileCompletion(t *testing.T) { } func TestRepoAddWithPasswordFromStdin(t *testing.T) { - srv := repotest.NewTempServer(t, "testdata/testserver/*.*", repotest.WithBasicAuth()) + srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*"), repotest.WithBasicAuth()) defer srv.Stop() defer resetEnv()() diff --git a/cmd/helm/repo_remove_test.go b/cmd/helm/repo_remove_test.go index 0b72b218a..92e0087a2 100644 --- a/cmd/helm/repo_remove_test.go +++ b/cmd/helm/repo_remove_test.go @@ -30,7 +30,7 @@ import ( ) func TestRepoRemove(t *testing.T) { - ts := repotest.NewTempServer(t, "testdata/testserver/*.*") + ts := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*")) defer ts.Stop() rootDir := t.TempDir() @@ -159,7 +159,7 @@ func testCacheFiles(t *testing.T, cacheIndexFile string, cacheChartsFile string, } func TestRepoRemoveCompletion(t *testing.T) { - ts := repotest.NewTempServer(t, "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 85010f030..0e4fd2d7d 100644 --- a/cmd/helm/repo_update_test.go +++ b/cmd/helm/repo_update_test.go @@ -106,7 +106,7 @@ func TestUpdateCustomCacheCmd(t *testing.T) { cachePath := filepath.Join(rootDir, "updcustomcache") os.Mkdir(cachePath, os.ModePerm) - ts := repotest.NewTempServer(t, "testdata/testserver/*.*") + ts := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*")) defer ts.Stop() o := &repoUpdateOptions{ @@ -127,7 +127,7 @@ func TestUpdateCharts(t *testing.T) { defer resetEnv()() ensure.HelmHome(t) - ts := repotest.NewTempServer(t, "testdata/testserver/*.*") + ts := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*")) defer ts.Stop() r, err := repo.NewChartRepository(&repo.Entry{ @@ -159,7 +159,7 @@ func TestUpdateChartsFail(t *testing.T) { defer resetEnv()() ensure.HelmHome(t) - ts := repotest.NewTempServer(t, "testdata/testserver/*.*") + ts := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*")) defer ts.Stop() var invalidURL = ts.URL() + "55" @@ -189,7 +189,7 @@ func TestUpdateChartsFailWithError(t *testing.T) { defer resetEnv()() ensure.HelmHome(t) - ts := repotest.NewTempServer(t, "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 d9c3208e8..afc9ef514 100644 --- a/cmd/helm/show_test.go +++ b/cmd/helm/show_test.go @@ -26,7 +26,7 @@ import ( ) func TestShowPreReleaseChart(t *testing.T) { - srv := repotest.NewTempServer(t, "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 37b8197b9..13e901ef6 100644 --- a/pkg/downloader/chart_downloader_test.go +++ b/pkg/downloader/chart_downloader_test.go @@ -171,7 +171,7 @@ func TestIsTar(t *testing.T) { } func TestDownloadTo(t *testing.T) { - srv := repotest.NewTempServer(t, "testdata/*.tgz*", repotest.WithBasicAuth()) + srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/*.tgz*"), repotest.WithBasicAuth()) defer srv.Stop() if err := srv.CreateIndex(); err != nil { t.Fatal(err) @@ -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, "testdata/*.tgz*", repotest.WithAutostart("tls")) + srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/*.tgz*"), repotest.WithAutostart("tls")) defer srv.Stop() if err := srv.CreateIndex(); err != nil { t.Fatal(err) @@ -269,7 +269,7 @@ func TestDownloadTo_VerifyLater(t *testing.T) { dest := t.TempDir() // Set up a fake repo - srv := repotest.NewTempServer(t, "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 253aa10fc..a840a41e8 100644 --- a/pkg/downloader/manager_test.go +++ b/pkg/downloader/manager_test.go @@ -292,7 +292,7 @@ version: 0.1.0` func TestUpdateBeforeBuild(t *testing.T) { // Set up a fake repo - srv := repotest.NewTempServer(t, "testdata/*.tgz*") + srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/*.tgz*")) defer srv.Stop() if err := srv.LinkIndices(); err != nil { t.Fatal(err) @@ -358,7 +358,7 @@ func TestUpdateBeforeBuild(t *testing.T) { // to be fetched. func TestUpdateWithNoRepo(t *testing.T) { // Set up a fake repo - srv := repotest.NewTempServer(t, "testdata/*.tgz*") + srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/*.tgz*")) defer srv.Stop() if err := srv.LinkIndices(); err != nil { t.Fatal(err) @@ -427,7 +427,7 @@ 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, "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 5988628fc..4253a1cc3 100644 --- a/pkg/repo/repotest/server.go +++ b/pkg/repo/repotest/server.go @@ -60,32 +60,36 @@ func WithBasicAuth() ServerOption { } } +func WithChartSourceGlob(glob string) ServerOption { + return func(t *testing.T, server *Server) { + server.chartSourceGlob = glob + } +} + // NewTempServer creates a server inside of a temp dir. // // If the passed in string is not "", it will be treated as a shell glob, and files // will be copied from that path to the server's docroot. // -// The caller is responsible for stopping the server. +// The server is started automatically. And the caller is responsible for stopping the server. // The temp dir will be removed by testing package automatically when test finished. -func NewTempServer(t *testing.T, glob string, options ...ServerOption) *Server { +func NewTempServer(t *testing.T, options ...ServerOption) *Server { - tdir, err := os.MkdirTemp("", "helm-repotest-") + docrootTempDir, err := os.MkdirTemp("", "helm-repotest-") if err != nil { t.Fatal(err) } - srv := newServer(t, tdir, options...) + srv := newServer(t, docrootTempDir, options...) + + t.Cleanup(func() { os.RemoveAll(srv.docroot) }) - if glob != "" { - if _, err := srv.CopyCharts(glob); err != nil { + if srv.chartSourceGlob != "" { + if _, err := srv.CopyCharts(srv.chartSourceGlob); err != nil { t.Fatal(err) } } - t.Cleanup(func() { os.RemoveAll(srv.docroot) }) - - autostartServer(t, srv) - return srv } @@ -93,7 +97,7 @@ func NewTempServer(t *testing.T, glob string, options ...ServerOption) *Server { // // docroot should be a temp dir managed by the caller. // -// By default the server will be started, serving files off of the docroot. +// The server is started automatically. And the caller is responsible for stopping the server. // // Use CopyCharts to move charts into the repository and then index them // for service. @@ -107,16 +111,20 @@ func NewServer(t *testing.T, docroot string, options ...ServerOption) *Server { // Create the server, but don't yet start it func newServer(t *testing.T, docroot string, options ...ServerOption) *Server { - root, err := filepath.Abs(docroot) + absdocroot, err := filepath.Abs(docroot) if err != nil { t.Fatal(err) } s := &Server{ - docroot: root, + docroot: absdocroot, autostartOption: "plain", } + for _, option := range options { + option(t, s) + } + s.srv = httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if s.middleware != nil { s.middleware.ServeHTTP(w, r) @@ -124,15 +132,13 @@ func newServer(t *testing.T, docroot string, options ...ServerOption) *Server { http.FileServer(http.Dir(s.Root())).ServeHTTP(w, r) })) - // Add the testing repository as the only repo. + autostartServer(t, s) + + // 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 { t.Fatal(err) } - for _, option := range options { - option(t, s) - } - return s } @@ -154,6 +160,7 @@ type Server struct { srv *httptest.Server middleware http.HandlerFunc autostartOption string + chartSourceGlob string } type OCIServer struct { @@ -414,6 +421,10 @@ func (s *Server) LinkIndices() error { // setTestingRepository sets up a testing repository.yaml with only the given URL. func setTestingRepository(url, fname string) error { + if url == "" { + panic("no url") + } + r := repo.NewFile() r.Add(&repo.Entry{ Name: "test", diff --git a/pkg/repo/repotest/server_test.go b/pkg/repo/repotest/server_test.go index 7949362e5..a9ce7c4bf 100644 --- a/pkg/repo/repotest/server_test.go +++ b/pkg/repo/repotest/server_test.go @@ -99,15 +99,36 @@ func TestServer(t *testing.T) { func TestNewTempServer(t *testing.T) { ensure.HelmHome(t) - srv := NewTempServer(t, "testdata/examplechart-0.1.0.tgz") + srv := NewTempServer(t, WithChartSourceGlob("testdata/examplechart-0.1.0.tgz")) defer srv.Stop() - res, err := http.Head(srv.URL() + "/examplechart-0.1.0.tgz") - res.Body.Close() - if err != nil { - t.Error(err) + if srv.srv.URL == "" { + t.Fatal("unstarted server") } - if res.StatusCode != 200 { - t.Errorf("Expected 200, got %d", res.StatusCode) + + { + res, err := http.Head(srv.URL() + "/repositories.yaml") + if err != nil { + t.Error(err) + } + + res.Body.Close() + + if res.StatusCode != 200 { + t.Errorf("Expected 200, got %d", res.StatusCode) + } + + } + + { + res, err := http.Head(srv.URL() + "/examplechart-0.1.0.tgz") + if err != nil { + t.Error(err) + } + res.Body.Close() + + if res.StatusCode != 200 { + t.Errorf("Expected 200, got %d", res.StatusCode) + } } }