more cleanup

Signed-off-by: George Jenkins <gvjenkins@gmail.com>
pull/13495/head
George Jenkins 10 months ago
parent 5026422da8
commit d2947185e0

@ -29,7 +29,7 @@ import (
) )
func TestDependencyBuildCmd(t *testing.T) { func TestDependencyBuildCmd(t *testing.T) {
srv := repotest.NewTempServer(t, "testdata/testcharts/*.tgz") srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testcharts/*.tgz"))
defer srv.Stop() defer srv.Stop()
rootDir := srv.Root() rootDir := srv.Root()

@ -32,7 +32,7 @@ import (
) )
func TestDependencyUpdateCmd(t *testing.T) { func TestDependencyUpdateCmd(t *testing.T) {
srv := repotest.NewTempServer(t, "testdata/testcharts/*.tgz") srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testcharts/*.tgz"))
defer srv.Stop() defer srv.Stop()
t.Logf("Listening on directory %s", srv.Root()) t.Logf("Listening on directory %s", srv.Root())
@ -148,7 +148,7 @@ func TestDependencyUpdateCmd_DoNotDeleteOldChartsOnError(t *testing.T) {
defer resetEnv()() defer resetEnv()()
ensure.HelmHome(t) ensure.HelmHome(t)
srv := repotest.NewTempServer(t, "testdata/testcharts/*.tgz") srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testcharts/*.tgz"))
defer srv.Stop() defer srv.Stop()
t.Logf("Listening on directory %s", srv.Root()) 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 { 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()) t.Logf("Listening on directory %s", srv.Root())

@ -27,7 +27,7 @@ import (
) )
func TestInstall(t *testing.T) { 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() defer srv.Stop()
srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

@ -28,7 +28,7 @@ import (
) )
func TestPullCmd(t *testing.T) { func TestPullCmd(t *testing.T) {
srv := repotest.NewTempServer(t, "testdata/testcharts/*.tgz*") srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testcharts/*.tgz*"))
defer srv.Stop() defer srv.Stop()
ociSrv, err := repotest.NewOCIServer(t, srv.Root()) ociSrv, err := repotest.NewOCIServer(t, srv.Root())
@ -249,7 +249,7 @@ func TestPullCmd(t *testing.T) {
} }
func TestPullWithCredentialsCmd(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() defer srv.Stop()
srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

@ -34,11 +34,11 @@ import (
) )
func TestRepoAddCmd(t *testing.T) { func TestRepoAddCmd(t *testing.T) {
srv := repotest.NewTempServer(t, "testdata/testserver/*.*") srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*"))
defer srv.Stop() defer srv.Stop()
// A second test server is setup to verify URL changing // 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() defer srv2.Stop()
tmpdir := filepath.Join(t.TempDir(), "path-component.yaml/data") tmpdir := filepath.Join(t.TempDir(), "path-component.yaml/data")
@ -74,7 +74,7 @@ func TestRepoAddCmd(t *testing.T) {
} }
func TestRepoAdd(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() defer ts.Stop()
rootDir := t.TempDir() rootDir := t.TempDir()
@ -125,7 +125,7 @@ func TestRepoAdd(t *testing.T) {
} }
func TestRepoAddCheckLegalName(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 ts.Stop()
defer resetEnv()() defer resetEnv()()
@ -179,7 +179,7 @@ func TestRepoAddConcurrentHiddenFile(t *testing.T) {
} }
func repoAddConcurrent(t *testing.T, testName, repoFile string) { 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() defer ts.Stop()
var wg sync.WaitGroup var wg sync.WaitGroup
@ -227,7 +227,7 @@ func TestRepoAddFileCompletion(t *testing.T) {
} }
func TestRepoAddWithPasswordFromStdin(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 srv.Stop()
defer resetEnv()() defer resetEnv()()

@ -30,7 +30,7 @@ import (
) )
func TestRepoRemove(t *testing.T) { func TestRepoRemove(t *testing.T) {
ts := repotest.NewTempServer(t, "testdata/testserver/*.*") ts := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*"))
defer ts.Stop() defer ts.Stop()
rootDir := t.TempDir() rootDir := t.TempDir()
@ -159,7 +159,7 @@ func testCacheFiles(t *testing.T, cacheIndexFile string, cacheChartsFile string,
} }
func TestRepoRemoveCompletion(t *testing.T) { func TestRepoRemoveCompletion(t *testing.T) {
ts := repotest.NewTempServer(t, "testdata/testserver/*.*") ts := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*"))
defer ts.Stop() defer ts.Stop()
rootDir := t.TempDir() rootDir := t.TempDir()

@ -106,7 +106,7 @@ func TestUpdateCustomCacheCmd(t *testing.T) {
cachePath := filepath.Join(rootDir, "updcustomcache") cachePath := filepath.Join(rootDir, "updcustomcache")
os.Mkdir(cachePath, os.ModePerm) os.Mkdir(cachePath, os.ModePerm)
ts := repotest.NewTempServer(t, "testdata/testserver/*.*") ts := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*"))
defer ts.Stop() defer ts.Stop()
o := &repoUpdateOptions{ o := &repoUpdateOptions{
@ -127,7 +127,7 @@ func TestUpdateCharts(t *testing.T) {
defer resetEnv()() defer resetEnv()()
ensure.HelmHome(t) ensure.HelmHome(t)
ts := repotest.NewTempServer(t, "testdata/testserver/*.*") ts := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*"))
defer ts.Stop() defer ts.Stop()
r, err := repo.NewChartRepository(&repo.Entry{ r, err := repo.NewChartRepository(&repo.Entry{
@ -159,7 +159,7 @@ func TestUpdateChartsFail(t *testing.T) {
defer resetEnv()() defer resetEnv()()
ensure.HelmHome(t) ensure.HelmHome(t)
ts := repotest.NewTempServer(t, "testdata/testserver/*.*") ts := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*"))
defer ts.Stop() defer ts.Stop()
var invalidURL = ts.URL() + "55" var invalidURL = ts.URL() + "55"
@ -189,7 +189,7 @@ func TestUpdateChartsFailWithError(t *testing.T) {
defer resetEnv()() defer resetEnv()()
ensure.HelmHome(t) ensure.HelmHome(t)
ts := repotest.NewTempServer(t, "testdata/testserver/*.*") ts := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testserver/*.*"))
defer ts.Stop() defer ts.Stop()
var invalidURL = ts.URL() + "55" var invalidURL = ts.URL() + "55"

@ -26,7 +26,7 @@ import (
) )
func TestShowPreReleaseChart(t *testing.T) { func TestShowPreReleaseChart(t *testing.T) {
srv := repotest.NewTempServer(t, "testdata/testcharts/*.tgz*") srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testcharts/*.tgz*"))
defer srv.Stop() defer srv.Stop()
if err := srv.LinkIndices(); err != nil { if err := srv.LinkIndices(); err != nil {

@ -171,7 +171,7 @@ func TestIsTar(t *testing.T) {
} }
func TestDownloadTo(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() defer srv.Stop()
if err := srv.CreateIndex(); err != nil { if err := srv.CreateIndex(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -218,7 +218,7 @@ func TestDownloadTo(t *testing.T) {
func TestDownloadTo_TLS(t *testing.T) { func TestDownloadTo_TLS(t *testing.T) {
// Set up mock server w/ tls enabled // 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() defer srv.Stop()
if err := srv.CreateIndex(); err != nil { if err := srv.CreateIndex(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -269,7 +269,7 @@ func TestDownloadTo_VerifyLater(t *testing.T) {
dest := t.TempDir() dest := t.TempDir()
// Set up a fake repo // Set up a fake repo
srv := repotest.NewTempServer(t, "testdata/*.tgz*") srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/*.tgz*"))
defer srv.Stop() defer srv.Stop()
if err := srv.LinkIndices(); err != nil { if err := srv.LinkIndices(); err != nil {
t.Fatal(err) t.Fatal(err)

@ -292,7 +292,7 @@ version: 0.1.0`
func TestUpdateBeforeBuild(t *testing.T) { func TestUpdateBeforeBuild(t *testing.T) {
// Set up a fake repo // Set up a fake repo
srv := repotest.NewTempServer(t, "testdata/*.tgz*") srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/*.tgz*"))
defer srv.Stop() defer srv.Stop()
if err := srv.LinkIndices(); err != nil { if err := srv.LinkIndices(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -358,7 +358,7 @@ func TestUpdateBeforeBuild(t *testing.T) {
// to be fetched. // to be fetched.
func TestUpdateWithNoRepo(t *testing.T) { func TestUpdateWithNoRepo(t *testing.T) {
// Set up a fake repo // Set up a fake repo
srv := repotest.NewTempServer(t, "testdata/*.tgz*") srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/*.tgz*"))
defer srv.Stop() defer srv.Stop()
if err := srv.LinkIndices(); err != nil { if err := srv.LinkIndices(); err != nil {
t.Fatal(err) 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. // 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) { func checkBuildWithOptionalFields(t *testing.T, chartName string, dep chart.Dependency) {
// Set up a fake repo // Set up a fake repo
srv := repotest.NewTempServer(t, "testdata/*.tgz*") srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/*.tgz*"))
defer srv.Stop() defer srv.Stop()
if err := srv.LinkIndices(); err != nil { if err := srv.LinkIndices(); err != nil {
t.Fatal(err) t.Fatal(err)

@ -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. // 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 // 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. // 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. // 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
srv := newServer(t, tdir, options...) srv := newServer(t, docrootTempDir, options...)
t.Cleanup(func() { os.RemoveAll(srv.docroot) })
if glob != "" { if srv.chartSourceGlob != "" {
if _, err := srv.CopyCharts(glob); err != nil { if _, err := srv.CopyCharts(srv.chartSourceGlob); err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
t.Cleanup(func() { os.RemoveAll(srv.docroot) })
autostartServer(t, srv)
return 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. // 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 // Use CopyCharts to move charts into the repository and then index them
// for service. // 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 // Create the server, but don't yet start it
func newServer(t *testing.T, docroot string, options ...ServerOption) *Server { func newServer(t *testing.T, docroot string, options ...ServerOption) *Server {
root, err := filepath.Abs(docroot) absdocroot, err := filepath.Abs(docroot)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
s := &Server{ s := &Server{
docroot: root, docroot: absdocroot,
autostartOption: "plain", autostartOption: "plain",
} }
for _, option := range options {
option(t, s)
}
s.srv = httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { s.srv = httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if s.middleware != nil { if s.middleware != nil {
s.middleware.ServeHTTP(w, r) 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) 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 { if err := setTestingRepository(s.URL(), filepath.Join(s.docroot, "repositories.yaml")); err != nil {
t.Fatal(err) t.Fatal(err)
} }
for _, option := range options {
option(t, s)
}
return s return s
} }
@ -154,6 +160,7 @@ type Server struct {
srv *httptest.Server srv *httptest.Server
middleware http.HandlerFunc middleware http.HandlerFunc
autostartOption string autostartOption string
chartSourceGlob string
} }
type OCIServer struct { type OCIServer struct {
@ -414,6 +421,10 @@ func (s *Server) LinkIndices() error {
// setTestingRepository sets up a testing repository.yaml with only the given URL. // setTestingRepository sets up a testing repository.yaml with only the given URL.
func setTestingRepository(url, fname string) error { func setTestingRepository(url, fname string) error {
if url == "" {
panic("no url")
}
r := repo.NewFile() r := repo.NewFile()
r.Add(&repo.Entry{ r.Add(&repo.Entry{
Name: "test", Name: "test",

@ -99,15 +99,36 @@ func TestServer(t *testing.T) {
func TestNewTempServer(t *testing.T) { func TestNewTempServer(t *testing.T) {
ensure.HelmHome(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() defer srv.Stop()
res, err := http.Head(srv.URL() + "/examplechart-0.1.0.tgz") if srv.srv.URL == "" {
res.Body.Close() t.Fatal("unstarted server")
if err != nil {
t.Error(err)
} }
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)
}
} }
} }

Loading…
Cancel
Save