more cleanup

Signed-off-by: George Jenkins <gvjenkins@gmail.com>
pull/13495/head
George Jenkins 10 months ago
parent 5e35c3b8e3
commit 1b872edf63

@ -27,16 +27,9 @@ import (
) )
func TestInstall(t *testing.T) { func TestInstall(t *testing.T) {
srv := repotest.NewTempServer(t, "testdata/testcharts/*.tgz*") srv := repotest.NewTempServer(t, "testdata/testcharts/*.tgz*", repotest.WithBasicAuth())
defer srv.Stop() defer srv.Stop()
srv.WithMiddleware(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)
}
}))
srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.FileServer(http.Dir(srv.Root())).ServeHTTP(w, r) http.FileServer(http.Dir(srv.Root())).ServeHTTP(w, r)
})) }))

@ -249,16 +249,9 @@ func TestPullCmd(t *testing.T) {
} }
func TestPullWithCredentialsCmd(t *testing.T) { func TestPullWithCredentialsCmd(t *testing.T) {
srv := repotest.NewTempServer(t, "testdata/testcharts/*.tgz*") srv := repotest.NewTempServer(t, "testdata/testcharts/*.tgz*", repotest.WithBasicAuth())
defer srv.Stop() defer srv.Stop()
srv.WithMiddleware(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)
}
}))
srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.FileServer(http.Dir(srv.Root())).ServeHTTP(w, r) http.FileServer(http.Dir(srv.Root())).ServeHTTP(w, r)
})) }))

@ -218,9 +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*") srv := repotest.NewTempServer(t, "testdata/*.tgz*", repotest.WithAutostart("tls"))
srv.Stop()
srv.StartTLS()
defer srv.Stop() defer srv.Stop()
if err := srv.CreateIndex(); err != nil { if err := srv.CreateIndex(); err != nil {
t.Fatal(err) t.Fatal(err)

@ -43,21 +43,20 @@ import (
type ServerOption func(*testing.T, *Server) type ServerOption func(*testing.T, *Server)
func WithNoAutostart() ServerOption { func WithAutostart(autostartOption string) ServerOption {
return func(_ *testing.T, server *Server) { return func(_ *testing.T, server *Server) {
server.autostart = false server.autostartOption = autostartOption
} }
} }
func WithBasicAuth() ServerOption { func WithBasicAuth() ServerOption {
return func(t *testing.T, server *Server) { return func(t *testing.T, server *Server) {
server.WithMiddleware(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { server.middleware = http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth() username, password, ok := r.BasicAuth()
if !ok || username != "username" || password != "password" { 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) t.Errorf("Expected request to use basic auth and for username == 'username' and password == 'password', got '%v', '%s', '%s'", ok, username, password)
} }
})) })
} }
} }
@ -85,9 +84,7 @@ func NewTempServer(t *testing.T, glob string, options ...ServerOption) *Server {
t.Cleanup(func() { os.RemoveAll(srv.docroot) }) t.Cleanup(func() { os.RemoveAll(srv.docroot) })
if srv.autostart { autostartServer(t, srv)
srv.Start()
}
return srv return srv
} }
@ -103,9 +100,7 @@ func NewTempServer(t *testing.T, glob string, options ...ServerOption) *Server {
func NewServer(t *testing.T, docroot string, options ...ServerOption) *Server { func NewServer(t *testing.T, docroot string, options ...ServerOption) *Server {
srv := newServer(t, docroot, options...) srv := newServer(t, docroot, options...)
if srv.autostart { autostartServer(t, srv)
srv.Start()
}
return srv return srv
} }
@ -118,8 +113,8 @@ func newServer(t *testing.T, docroot string, options ...ServerOption) *Server {
} }
srv := &Server{ srv := &Server{
docroot: root, docroot: root,
autostart: true, autostartOption: "plain",
} }
// Add the testing repository as the only repo. // Add the testing repository as the only repo.
@ -134,18 +129,24 @@ func newServer(t *testing.T, docroot string, options ...ServerOption) *Server {
return srv return srv
} }
// Server is an implementation of a repository server for testing. func autostartServer(t *testing.T, srv *Server) {
type Server struct { switch srv.autostartOption {
docroot string case "none":
srv *httptest.Server case "plain":
middleware http.HandlerFunc srv.Start()
autostart bool case "tls":
srv.StartTLS()
default:
t.Fatalf("Invalid autostart option: %s", srv.autostartOption)
}
} }
// WithMiddleware injects middleware in front of the server. This can be used to inject // Server is an implementation of a repository server for testing.
// additional functionality like layering in an authentication frontend. type Server struct {
func (s *Server) WithMiddleware(middleware http.HandlerFunc) { docroot string
s.middleware = middleware srv *httptest.Server
middleware http.HandlerFunc
autostartOption string
} }
type OCIServer struct { type OCIServer struct {

Loading…
Cancel
Save