Skip to content

Commit 721229d

Browse files
authored
Merge pull request #65 from fluxcd/artifact-mock-srv
2 parents bc558eb + 0bc28f7 commit 721229d

File tree

7 files changed

+121
-26
lines changed

7 files changed

+121
-26
lines changed

controllers/gitrepository_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import (
3939
"k8s.io/apimachinery/pkg/types"
4040

4141
sourcev1 "github.com/fluxcd/source-controller/api/v1alpha1"
42-
"github.com/fluxcd/source-controller/internal/testserver"
42+
"github.com/fluxcd/source-controller/pkg/testserver"
4343
)
4444

4545
var _ = Describe("GitRepositoryReconciler", func() {

controllers/helmchart_controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
"k8s.io/apimachinery/pkg/types"
3232

3333
sourcev1 "github.com/fluxcd/source-controller/api/v1alpha1"
34-
"github.com/fluxcd/source-controller/internal/testserver"
34+
"github.com/fluxcd/source-controller/pkg/testserver"
3535
)
3636

3737
var _ = Describe("HelmChartReconciler", func() {
@@ -46,7 +46,7 @@ var _ = Describe("HelmChartReconciler", func() {
4646
Context("HelmChart", func() {
4747
var (
4848
namespace *corev1.Namespace
49-
helmServer *testserver.Helm
49+
helmServer *testserver.HelmServer
5050
err error
5151
)
5252

controllers/helmrepository_controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
"k8s.io/apimachinery/pkg/types"
3232

3333
sourcev1 "github.com/fluxcd/source-controller/api/v1alpha1"
34-
"github.com/fluxcd/source-controller/internal/testserver"
34+
"github.com/fluxcd/source-controller/pkg/testserver"
3535
)
3636

3737
var _ = Describe("HelmRepositoryReconciler", func() {
@@ -45,7 +45,7 @@ var _ = Describe("HelmRepositoryReconciler", func() {
4545
Context("HelmRepository", func() {
4646
var (
4747
namespace *corev1.Namespace
48-
helmServer *testserver.Helm
48+
helmServer *testserver.HelmServer
4949
err error
5050
)
5151

pkg/testserver/artifact.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
Copyright 2020 The Flux CD contributors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package testserver
18+
19+
import (
20+
"archive/tar"
21+
"compress/gzip"
22+
"crypto/sha1"
23+
"errors"
24+
"fmt"
25+
"os"
26+
"path"
27+
"path/filepath"
28+
)
29+
30+
func NewTempArtifactServer() (*ArtifactServer, error) {
31+
server, err := NewTempHTTPServer()
32+
if err != nil {
33+
return nil, err
34+
}
35+
artifact := &ArtifactServer{server}
36+
return artifact, nil
37+
}
38+
39+
type ArtifactServer struct {
40+
*HTTPServer
41+
}
42+
43+
type File struct {
44+
Name string
45+
Body string
46+
}
47+
48+
// ArtifactFromBytes creates a tar.gz artifact from the given files and
49+
// returns the file name of the artifact.
50+
func (s *ArtifactServer) ArtifactFromBytes(files []File) (string, error) {
51+
fileName := calculateArtifactName(files)
52+
filePath := filepath.Join(s.docroot, fileName)
53+
gzFile, err := os.Create(filePath)
54+
if err != nil {
55+
return "", err
56+
}
57+
defer gzFile.Close()
58+
59+
gw := gzip.NewWriter(gzFile)
60+
defer gw.Close()
61+
62+
tw := tar.NewWriter(gw)
63+
defer tw.Close()
64+
65+
for _, f := range files {
66+
hdr := &tar.Header{
67+
Name: f.Name,
68+
Mode: 0600,
69+
Size: int64(len(f.Body)),
70+
}
71+
if err := tw.WriteHeader(hdr); err != nil {
72+
return "", err
73+
}
74+
if _, err := tw.Write([]byte(f.Body)); err != nil {
75+
return "", err
76+
}
77+
}
78+
return fileName, nil
79+
}
80+
81+
// URLForFile returns the URL the given file can be reached at or
82+
// an error if the server has not been started.
83+
func (s *ArtifactServer) URLForFile(file string) (string, error) {
84+
if s.URL() == "" {
85+
return "", errors.New("server must be started to be able to determine the URL of the given file")
86+
}
87+
return path.Join(s.URL(), file), nil
88+
}
89+
90+
func calculateArtifactName(files []File) string {
91+
h := sha1.New()
92+
for _, f := range files {
93+
h.Write([]byte(f.Body))
94+
}
95+
return fmt.Sprintf("%x.tar.gz", h.Sum(nil))
96+
}
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,39 @@ import (
2525
"sigs.k8s.io/yaml"
2626
)
2727

28-
func NewTempHelmServer() (*Helm, error) {
28+
func NewTempHelmServer() (*HelmServer, error) {
2929
server, err := NewTempHTTPServer()
3030
if err != nil {
3131
return nil, err
3232
}
33-
helm := &Helm{server}
33+
helm := &HelmServer{server}
3434
return helm, nil
3535
}
3636

37-
type Helm struct {
38-
*HTTP
37+
type HelmServer struct {
38+
*HTTPServer
3939
}
4040

41-
func (s *Helm) GenerateIndex() error {
42-
index, err := repo.IndexDirectory(s.HTTP.docroot, s.HTTP.URL())
41+
func (s *HelmServer) GenerateIndex() error {
42+
index, err := repo.IndexDirectory(s.HTTPServer.docroot, s.HTTPServer.URL())
4343
if err != nil {
4444
return err
4545
}
4646
d, err := yaml.Marshal(index)
4747
if err != nil {
4848
return err
4949
}
50-
f := filepath.Join(s.HTTP.docroot, "index.yaml")
50+
f := filepath.Join(s.HTTPServer.docroot, "index.yaml")
5151
return ioutil.WriteFile(f, d, 0644)
5252
}
5353

54-
func (s *Helm) PackageChart(path string) error {
54+
func (s *HelmServer) PackageChart(path string) error {
5555
return s.PackageChartWithVersion(path, "")
5656
}
5757

58-
func (s *Helm) PackageChartWithVersion(path, version string) error {
58+
func (s *HelmServer) PackageChartWithVersion(path, version string) error {
5959
pkg := action.NewPackage()
60-
pkg.Destination = s.HTTP.docroot
60+
pkg.Destination = s.HTTPServer.docroot
6161
pkg.Version = version
6262
_, err := pkg.Run(path, nil)
6363
return err
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"path/filepath"
2626
)
2727

28-
func NewTempHTTPServer() (*HTTP, error) {
28+
func NewTempHTTPServer() (*HTTPServer, error) {
2929
tmpDir, err := ioutil.TempDir("", "http-test-")
3030
if err != nil {
3131
return nil, err
@@ -34,28 +34,28 @@ func NewTempHTTPServer() (*HTTP, error) {
3434
return srv, nil
3535
}
3636

37-
func NewHTTPServer(docroot string) *HTTP {
37+
func NewHTTPServer(docroot string) *HTTPServer {
3838
root, err := filepath.Abs(docroot)
3939
if err != nil {
4040
panic(err)
4141
}
42-
return &HTTP{
42+
return &HTTPServer{
4343
docroot: root,
4444
}
4545
}
4646

47-
type HTTP struct {
47+
type HTTPServer struct {
4848
docroot string
4949
middleware func(http.Handler) http.Handler
5050
server *httptest.Server
5151
}
5252

53-
func (s *HTTP) WithMiddleware(m func(handler http.Handler) http.Handler) *HTTP {
53+
func (s *HTTPServer) WithMiddleware(m func(handler http.Handler) http.Handler) *HTTPServer {
5454
s.middleware = m
5555
return s
5656
}
5757

58-
func (s *HTTP) Start() {
58+
func (s *HTTPServer) Start() {
5959
s.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
6060
handler := http.FileServer(http.Dir(s.docroot))
6161
if s.middleware != nil {
@@ -66,7 +66,7 @@ func (s *HTTP) Start() {
6666
}))
6767
}
6868

69-
func (s *HTTP) StartTLS(cert, key, ca []byte) error {
69+
func (s *HTTPServer) StartTLS(cert, key, ca []byte) error {
7070
s.server = httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
7171
handler := http.FileServer(http.Dir(s.docroot))
7272
if s.middleware != nil {
@@ -88,25 +88,24 @@ func (s *HTTP) StartTLS(cert, key, ca []byte) error {
8888
cp.AppendCertsFromPEM(ca)
8989
config.RootCAs = cp
9090

91-
config.BuildNameToCertificate()
9291
config.ServerName = "example.com"
9392
s.server.TLS = &config
9493

9594
s.server.StartTLS()
9695
return nil
9796
}
9897

99-
func (s *HTTP) Stop() {
98+
func (s *HTTPServer) Stop() {
10099
if s.server != nil {
101100
s.server.Close()
102101
}
103102
}
104103

105-
func (s *HTTP) Root() string {
104+
func (s *HTTPServer) Root() string {
106105
return s.docroot
107106
}
108107

109-
func (s *HTTP) URL() string {
108+
func (s *HTTPServer) URL() string {
110109
if s.server != nil {
111110
return s.server.URL
112111
}

0 commit comments

Comments
 (0)