cleanup_repotest_server
George Jenkins 2 months ago
parent e81d56a247
commit faa0007b58

@ -108,12 +108,12 @@ func NewTempServer(t *testing.T, options ...ServerOption) *Server {
//
// Use CopyCharts to move charts into the repository and then index them
// for service.
func NewServer(t *testing.T, docroot string, options ...ServerOption) *Server {
srv := newServer(t, docroot, options...)
srv.Start()
return srv
}
//func NewServer(t *testing.T, docroot string, options ...ServerOption) *Server {
// srv := newServer(t, docroot, options...)
// srv.Start()
//
// return srv
//}
// Create the server, but don't yet start it
func newServer(t *testing.T, docroot string, options ...ServerOption) *Server {
@ -137,7 +137,7 @@ func newServer(t *testing.T, docroot string, options ...ServerOption) *Server {
http.FileServer(http.Dir(s.Root())).ServeHTTP(w, r)
}))
s.Start()
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 {
@ -369,7 +369,7 @@ func (s *Server) CreateIndex() error {
return os.WriteFile(ifile, d, 0644)
}
func (s *Server) Start() {
func (s *Server) start() {
if s.useTLS {
insecure := false

@ -36,6 +36,7 @@ func TestServer(t *testing.T) {
rootDir := t.TempDir()
srv := newServer(t, rootDir)
srv.Start()
defer srv.Stop()
c, err := srv.CopyCharts("testdata/*.tgz")
@ -135,6 +136,51 @@ func TestNewTempServer(t *testing.T) {
t.Errorf("Expected 200, got %d", res.StatusCode)
}
}
res, err := http.Get(srv.URL() + "/examplechart-0.1.0.tgz")
res.Body.Close()
if err != nil {
t.Fatal(err)
}
if res.ContentLength < 500 {
t.Errorf("Expected at least 500 bytes of data, got %d", res.ContentLength)
}
res, err = http.Get(srv.URL() + "/index.yaml")
if err != nil {
t.Fatal(err)
}
data, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
}
m := repo.NewIndexFile()
if err := yaml.Unmarshal(data, m); err != nil {
t.Fatal(err)
}
if l := len(m.Entries); l != 1 {
t.Fatalf("Expected 1 entry, got %d", l)
}
expect := "examplechart"
if !m.Has(expect, "0.1.0") {
t.Errorf("missing %q", expect)
}
res, err = http.Get(srv.URL() + "/index.yaml-nosuchthing")
res.Body.Close()
if err != nil {
t.Fatal(err)
}
if res.StatusCode != 404 {
t.Fatalf("Expected 404, got %d", res.StatusCode)
}
}
func TestNewTempServer_TLS(t *testing.T) {

@ -1,70 +0,0 @@
/*
Copyright The Helm Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package testdata
import (
"crypto/tls"
"crypto/x509"
"embed"
"github.com/pkg/errors"
)
//go:embed rootca.crt rootca.key crt.pem key.pem
var tlsFiles embed.FS
func ReadTLSConfig(insecureSkipTLSverify bool) (*tls.Config, error) {
config := tls.Config{
InsecureSkipVerify: insecureSkipTLSverify,
}
certFile := "crt.pem"
keyFile := "key.pem"
caFile := "rootca.crt"
certPEMBlock, err := tlsFiles.ReadFile(certFile)
if err != nil {
return nil, errors.Wrapf(err, "unable to read cert file: file=%q", certFile)
}
keyPEMBlock, err := tlsFiles.ReadFile(keyFile)
if err != nil {
return nil, errors.Wrapf(err, "unable to read key file: file=%q", keyFile)
}
cert, err := tls.X509KeyPair(certPEMBlock, keyPEMBlock)
if err != nil {
return nil, err
}
config.Certificates = []tls.Certificate{cert}
tlsFiles.ReadFile("rootca.crt")
b, err := tlsFiles.ReadFile(caFile)
if err != nil {
return nil, errors.Wrapf(err, "unable to read CA file: caFile=%q", caFile)
}
cp := x509.NewCertPool()
if !cp.AppendCertsFromPEM(b) {
return nil, errors.Wrapf(err, "failed to append certificates from file: caFile=%q", caFile)
}
config.RootCAs = cp
return &config, nil
}

@ -1,29 +0,0 @@
package testdata
import (
"crypto/x509"
"net"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestReadTLSConfig(t *testing.T) {
insecureSkipVerify := false
tlsConfig, err := ReadTLSConfig(insecureSkipVerify)
require.Nil(t, err)
assert.Equal(t, insecureSkipVerify, tlsConfig.InsecureSkipVerify)
require.Len(t, tlsConfig.Certificates, 1)
require.Len(t, tlsConfig.Certificates[0].Certificate, 1)
leaf, err := x509.ParseCertificate(tlsConfig.Certificates[0].Certificate[0])
assert.Nil(t, err)
assert.Equal(t, []string{"helm.sh"}, leaf.DNSNames)
assert.Equal(t, []net.IP{{127, 0, 0, 1}}, leaf.IPAddresses)
}
Loading…
Cancel
Save