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) {
srv := repotest.NewTempServer(t, "testdata/testcharts/*.tgz")
srv := repotest.NewTempServer(t, repotest.WithChartSourceGlob("testdata/testcharts/*.tgz"))
defer srv.Stop()
rootDir := srv.Root()

@ -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())

@ -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) {

@ -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) {

@ -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()()

@ -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()

@ -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"

@ -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 {

@ -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)

@ -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)

@ -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",

@ -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")
if srv.srv.URL == "" {
t.Fatal("unstarted server")
}
{
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