Use testserver package from fluxcd/pkg

This commit is contained in:
Hidde Beydals 2020-08-17 21:53:30 +02:00
parent 720fef496e
commit 9d947b8efa
9 changed files with 9 additions and 396 deletions

View File

@ -38,8 +38,9 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"github.com/fluxcd/pkg/testserver"
sourcev1 "github.com/fluxcd/source-controller/api/v1alpha1"
"github.com/fluxcd/source-controller/pkg/testserver"
)
var _ = Describe("GitRepositoryReconciler", func() {

View File

@ -30,8 +30,9 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"github.com/fluxcd/pkg/testserver"
sourcev1 "github.com/fluxcd/source-controller/api/v1alpha1"
"github.com/fluxcd/source-controller/pkg/testserver"
)
var _ = Describe("HelmChartReconciler", func() {

View File

@ -30,8 +30,9 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"github.com/fluxcd/pkg/testserver"
sourcev1 "github.com/fluxcd/source-controller/api/v1alpha1"
"github.com/fluxcd/source-controller/pkg/testserver"
)
var _ = Describe("HelmRepositoryReconciler", func() {

3
go.mod
View File

@ -9,19 +9,18 @@ require (
github.com/fluxcd/pkg/lockedfile v0.0.5
github.com/fluxcd/pkg/recorder v0.0.5
github.com/fluxcd/pkg/ssh v0.0.5
github.com/fluxcd/pkg/testserver v0.0.1
github.com/fluxcd/source-controller/api v0.0.0-00010101000000-000000000000
github.com/go-git/go-billy/v5 v5.0.0
github.com/go-git/go-git/v5 v5.1.0
github.com/go-logr/logr v0.1.0
github.com/onsi/ginkgo v1.12.1
github.com/onsi/gomega v1.10.1
github.com/sosedoff/gitkit v0.2.1-0.20191202022816-7182d43c6254
go.uber.org/zap v1.13.0
helm.sh/helm/v3 v3.3.0
k8s.io/api v0.18.4
k8s.io/apimachinery v0.18.4
k8s.io/client-go v0.18.4
rsc.io/letsencrypt v0.0.3 // indirect
sigs.k8s.io/controller-runtime v0.6.1
sigs.k8s.io/yaml v1.2.0
)

2
go.sum
View File

@ -205,6 +205,8 @@ github.com/fluxcd/pkg/recorder v0.0.5 h1:D8qfupahIvh6ncCMn2yTHsrzG91S05sp4zdpsbK
github.com/fluxcd/pkg/recorder v0.0.5/go.mod h1:2UG6EroZ6ZbqmqoL8k/cQMe09e6A36WyH4t4UDUGyuU=
github.com/fluxcd/pkg/ssh v0.0.5 h1:rnbFZ7voy2JBlUfMbfyqArX2FYaLNpDhccGFC3qW83A=
github.com/fluxcd/pkg/ssh v0.0.5/go.mod h1:7jXPdXZpc0ttMNz2kD9QuMi3RNn/e0DOFbj0Tij/+Hs=
github.com/fluxcd/pkg/testserver v0.0.1 h1:fZFDZ1GCo4yoN+t8vlaqBfyFRv8PtbwMMJYpguVRz/Y=
github.com/fluxcd/pkg/testserver v0.0.1/go.mod h1:n+13cVubIdlFFkjqwdz7ZIdII3/08IiizWzSsKajANM=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db/go.mod h1:7dvUGVsVBjqR7JHJk0brhHOZYGmfBYOrK0ZhYMEtBr4=

View File

@ -1,95 +0,0 @@
/*
Copyright 2020 The Flux CD contributors.
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 testserver
import (
"archive/tar"
"compress/gzip"
"crypto/sha1"
"errors"
"fmt"
"os"
"path/filepath"
)
func NewTempArtifactServer() (*ArtifactServer, error) {
server, err := NewTempHTTPServer()
if err != nil {
return nil, err
}
artifact := &ArtifactServer{server}
return artifact, nil
}
type ArtifactServer struct {
*HTTPServer
}
type File struct {
Name string
Body string
}
// ArtifactFromFiles creates a tar.gz artifact from the given files and
// returns the file name of the artifact.
func (s *ArtifactServer) ArtifactFromFiles(files []File) (string, error) {
fileName := calculateArtifactName(files)
filePath := filepath.Join(s.docroot, fileName)
gzFile, err := os.Create(filePath)
if err != nil {
return "", err
}
defer gzFile.Close()
gw := gzip.NewWriter(gzFile)
defer gw.Close()
tw := tar.NewWriter(gw)
defer tw.Close()
for _, f := range files {
hdr := &tar.Header{
Name: f.Name,
Mode: 0600,
Size: int64(len(f.Body)),
}
if err := tw.WriteHeader(hdr); err != nil {
return "", err
}
if _, err := tw.Write([]byte(f.Body)); err != nil {
return "", err
}
}
return fileName, nil
}
// URLForFile returns the URL the given file can be reached at or
// an error if the server has not been started.
func (s *ArtifactServer) URLForFile(file string) (string, error) {
if s.URL() == "" {
return "", errors.New("server must be started to be able to determine the URL of the given file")
}
return fmt.Sprintf("%s/%s", s.URL(), file), nil
}
func calculateArtifactName(files []File) string {
h := sha1.New()
for _, f := range files {
h.Write([]byte(f.Body))
}
return fmt.Sprintf("%x.tar.gz", h.Sum(nil))
}

View File

@ -1,119 +0,0 @@
/*
Copyright 2020 The Flux CD contributors.
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 testserver
import (
"io/ioutil"
"net/http/httptest"
"path/filepath"
"github.com/sosedoff/gitkit"
)
// NewTempGitServer returns a GitServer with a newly created temp
// dir as repository docroot.
func NewTempGitServer() (*GitServer, error) {
tmpDir, err := ioutil.TempDir("", "git-server-test-")
if err != nil {
return nil, err
}
srv := NewGitServer(tmpDir)
return srv, nil
}
// NewGitServer returns a GitServer with the given repository docroot
// set.
func NewGitServer(docroot string) *GitServer {
root, err := filepath.Abs(docroot)
if err != nil {
panic(err)
}
return &GitServer{
config: gitkit.Config{Dir: root},
}
}
// GitServer is a git server for testing purposes.
// It can serve git repositories over HTTP and SSH.
type GitServer struct {
config gitkit.Config
httpServer *httptest.Server
sshServer *gitkit.SSH
}
// AutoCreate enables the automatic creation of a non-existing Git
// repository on push.
func (s *GitServer) AutoCreate() *GitServer {
s.config.AutoCreate = true
return s
}
// StartHTTP starts a new HTTP git server with the current configuration.
func (s *GitServer) StartHTTP() error {
s.StopHTTP()
service := gitkit.New(s.config)
if err := service.Setup(); err != nil {
return err
}
s.httpServer = httptest.NewServer(service)
return nil
}
// StopHTTP stops the HTTP git server.
func (s *GitServer) StopHTTP() {
if s.httpServer != nil {
s.httpServer.Close()
}
return
}
// StartSSH starts a new SSH git server with the current configuration.
func (s *GitServer) StartSSH() error {
_ = s.StopSSH()
s.sshServer = gitkit.NewSSH(s.config)
// :0 should result in an OS assigned free port
return s.sshServer.ListenAndServe(":0")
}
// StopSSH stops the SSH git server.
func (s *GitServer) StopSSH() error {
if s.sshServer != nil {
return s.sshServer.Stop()
}
return nil
}
// Root returns the repositories root directory.
func (s *GitServer) Root() string {
return s.config.Dir
}
// HTTPAddress returns the address of the HTTP git server.
func (s *GitServer) HTTPAddress() string {
if s.httpServer != nil {
return s.httpServer.URL
}
return ""
}
// SSHAddress returns the address of the SSH git server.
func (s *GitServer) SSHAddress() string {
if s.sshServer != nil {
return s.sshServer.Address()
}
return ""
}

View File

@ -1,64 +0,0 @@
/*
Copyright 2020 The Flux CD contributors.
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 testserver
import (
"io/ioutil"
"path/filepath"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/repo"
"sigs.k8s.io/yaml"
)
func NewTempHelmServer() (*HelmServer, error) {
server, err := NewTempHTTPServer()
if err != nil {
return nil, err
}
helm := &HelmServer{server}
return helm, nil
}
type HelmServer struct {
*HTTPServer
}
func (s *HelmServer) GenerateIndex() error {
index, err := repo.IndexDirectory(s.HTTPServer.docroot, s.HTTPServer.URL())
if err != nil {
return err
}
d, err := yaml.Marshal(index)
if err != nil {
return err
}
f := filepath.Join(s.HTTPServer.docroot, "index.yaml")
return ioutil.WriteFile(f, d, 0644)
}
func (s *HelmServer) PackageChart(path string) error {
return s.PackageChartWithVersion(path, "")
}
func (s *HelmServer) PackageChartWithVersion(path, version string) error {
pkg := action.NewPackage()
pkg.Destination = s.HTTPServer.docroot
pkg.Version = version
_, err := pkg.Run(path, nil)
return err
}

View File

@ -1,113 +0,0 @@
/*
Copyright 2020 The Flux CD contributors.
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 testserver
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"net/http"
"net/http/httptest"
"path/filepath"
)
func NewTempHTTPServer() (*HTTPServer, error) {
tmpDir, err := ioutil.TempDir("", "http-test-")
if err != nil {
return nil, err
}
srv := NewHTTPServer(tmpDir)
return srv, nil
}
func NewHTTPServer(docroot string) *HTTPServer {
root, err := filepath.Abs(docroot)
if err != nil {
panic(err)
}
return &HTTPServer{
docroot: root,
}
}
type HTTPServer struct {
docroot string
middleware func(http.Handler) http.Handler
server *httptest.Server
}
func (s *HTTPServer) WithMiddleware(m func(handler http.Handler) http.Handler) *HTTPServer {
s.middleware = m
return s
}
func (s *HTTPServer) Start() {
s.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handler := http.FileServer(http.Dir(s.docroot))
if s.middleware != nil {
s.middleware(handler).ServeHTTP(w, r)
return
}
handler.ServeHTTP(w, r)
}))
}
func (s *HTTPServer) StartTLS(cert, key, ca []byte) error {
s.server = httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handler := http.FileServer(http.Dir(s.docroot))
if s.middleware != nil {
s.middleware(handler).ServeHTTP(w, r)
return
}
handler.ServeHTTP(w, r)
}))
config := tls.Config{}
keyPair, err := tls.X509KeyPair(cert, key)
if err != nil {
return err
}
config.Certificates = []tls.Certificate{keyPair}
cp := x509.NewCertPool()
cp.AppendCertsFromPEM(ca)
config.RootCAs = cp
config.ServerName = "example.com"
s.server.TLS = &config
s.server.StartTLS()
return nil
}
func (s *HTTPServer) Stop() {
if s.server != nil {
s.server.Close()
}
}
func (s *HTTPServer) Root() string {
return s.docroot
}
func (s *HTTPServer) URL() string {
if s.server != nil {
return s.server.URL
}
return ""
}