more cleanup

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

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

@ -33,19 +33,19 @@ import (
"golang.org/x/crypto/bcrypt"
"sigs.k8s.io/yaml"
"helm.sh/helm/v3/internal/tlsutil"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
ociRegistry "helm.sh/helm/v3/pkg/registry"
"helm.sh/helm/v3/pkg/repo"
"helm.sh/helm/v3/testdata"
)
type ServerOption func(*testing.T, *Server)
func WithAutostart(autostartOption string) ServerOption {
func WithTLS() ServerOption {
return func(_ *testing.T, server *Server) {
server.autostartOption = autostartOption
server.useTLS = true
}
}
@ -103,8 +103,7 @@ func NewTempServer(t *testing.T, options ...ServerOption) *Server {
// for service.
func NewServer(t *testing.T, docroot string, options ...ServerOption) *Server {
srv := newServer(t, docroot, options...)
autostartServer(t, srv)
srv.Start()
return srv
}
@ -117,8 +116,7 @@ func newServer(t *testing.T, docroot string, options ...ServerOption) *Server {
}
s := &Server{
docroot: absdocroot,
autostartOption: "plain",
docroot: absdocroot,
}
for _, option := range options {
@ -132,7 +130,7 @@ func newServer(t *testing.T, docroot string, options ...ServerOption) *Server {
http.FileServer(http.Dir(s.Root())).ServeHTTP(w, r)
}))
autostartServer(t, s)
s.Start()
// 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 {
@ -142,24 +140,12 @@ func newServer(t *testing.T, docroot string, options ...ServerOption) *Server {
return s
}
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)
}
}
// Server is an implementation of a repository server for testing.
type Server struct {
docroot string
srv *httptest.Server
middleware http.HandlerFunc
autostartOption string
useTLS bool
chartSourceGlob string
}
@ -377,21 +363,19 @@ func (s *Server) CreateIndex() error {
}
func (s *Server) Start() {
s.srv.Start()
}
if s.useTLS {
insecure := false
func (s *Server) StartTLS() {
cd := "../../testdata"
ca, pub, priv := filepath.Join(cd, "rootca.crt"), filepath.Join(cd, "crt.pem"), filepath.Join(cd, "key.pem")
insecure := false
tlsConf, err := tlsutil.NewClientTLS(pub, priv, ca, insecure)
if err != nil {
panic(err)
tlsConf, err := testdata.ReadTLSConfig(insecure)
if err != nil {
panic(err)
}
tlsConf.ServerName = "helm.sh"
s.srv.TLS = tlsConf
s.srv.StartTLS()
} else {
s.srv.Start()
}
tlsConf.ServerName = "helm.sh"
s.srv.TLS = tlsConf
s.srv.StartTLS()
}
// Stop stops the server and closes all connections.

@ -19,6 +19,7 @@ import (
"io"
"net/http"
"path/filepath"
"strings"
"testing"
"sigs.k8s.io/yaml"
@ -132,3 +133,14 @@ func TestNewTempServer(t *testing.T) {
}
}
}
func TestNewTempServer_TLS(t *testing.T) {
ensure.HelmHome(t)
srv := NewTempServer(t, WithChartSourceGlob("testdata/examplechart-0.1.0.tgz"), WithTLS())
defer srv.Stop()
if !strings.HasPrefix(srv.URL(), "https://") {
t.Fatal("non-TLS server")
}
}

Loading…
Cancel
Save