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) {
srv := repotest.NewTempServer(t, "testdata/testcharts/*.tgz*")
srv := repotest.NewTempServer(t, "testdata/testcharts/*.tgz*", repotest.WithBasicAuth())
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) {
http.FileServer(http.Dir(srv.Root())).ServeHTTP(w, r)
}))

@ -249,16 +249,9 @@ func TestPullCmd(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()
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) {
http.FileServer(http.Dir(srv.Root())).ServeHTTP(w, r)
}))

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

@ -43,21 +43,20 @@ import (
type ServerOption func(*testing.T, *Server)
func WithNoAutostart() ServerOption {
func WithAutostart(autostartOption string) ServerOption {
return func(_ *testing.T, server *Server) {
server.autostart = false
server.autostartOption = autostartOption
}
}
func WithBasicAuth() ServerOption {
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()
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)
}
}))
})
}
}
@ -85,9 +84,7 @@ func NewTempServer(t *testing.T, glob string, options ...ServerOption) *Server {
t.Cleanup(func() { os.RemoveAll(srv.docroot) })
if srv.autostart {
srv.Start()
}
autostartServer(t, 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 {
srv := newServer(t, docroot, options...)
if srv.autostart {
srv.Start()
}
autostartServer(t, srv)
return srv
}
@ -118,8 +113,8 @@ func newServer(t *testing.T, docroot string, options ...ServerOption) *Server {
}
srv := &Server{
docroot: root,
autostart: true,
docroot: root,
autostartOption: "plain",
}
// Add the testing repository as the only repo.
@ -134,18 +129,24 @@ func newServer(t *testing.T, docroot string, options ...ServerOption) *Server {
return srv
}
// Server is an implementation of a repository server for testing.
type Server struct {
docroot string
srv *httptest.Server
middleware http.HandlerFunc
autostart bool
func autostartServer(t *testing.T, srv *Server) {
switch srv.autostartOption {
case "none":
case "plain":
srv.Start()
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
// additional functionality like layering in an authentication frontend.
func (s *Server) WithMiddleware(middleware http.HandlerFunc) {
s.middleware = middleware
// Server is an implementation of a repository server for testing.
type Server struct {
docroot string
srv *httptest.Server
middleware http.HandlerFunc
autostartOption string
}
type OCIServer struct {

Loading…
Cancel
Save