Skip to content

Commit

Permalink
test: Add e2e tests for helm bundle functionality (#184)
Browse files Browse the repository at this point in the history
* test: Add e2e tests for helm bundle functionality

* build: Use build tags to separate out e2e tests

* fix: Add another option to unknown host test output

* fix: Correct expected test output after glob additions

* test: Add e2e tests for serve and push with TLS

Unfortunately helm client does not support custom CA certificates or
self-signed certs so the validation has been disabled, but the e2e tests
still add some value.
  • Loading branch information
jimmidyson authored Sep 14, 2022
1 parent 0dd24e7 commit da08e73
Show file tree
Hide file tree
Showing 21 changed files with 787 additions and 64 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ jobs:
with:
test-results: test.json

e2e-test:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3

- name: Install asdf
uses: asdf-vm/actions/setup@v1

- name: Install go
run: make install-tool.golang

- name: Run e2e tests
run: make e2e-test

lint:
runs-on: ubuntu-latest
steps:
Expand Down
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ dist
*.swp

# go coverage output
coverage.out
coverage.html
coverage*.out
coverage*.html

test.json
junit-report.xml
junit-e2e.xml
report-e2e.json
openapi_violations.report

.idea/
Expand Down
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ goreleaser 1.11.2
golang 1.19
helm 3.9.4
pre-commit 2.20.0
ginkgo 2.1.6
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ served via `registry:2`.
#### Serving a Helm chart bundle

```shell
mindthegap serve helm-bundle --helm-charts-bundle <path/to/helm-charts.tar> \
mindthegap serve helm-bundle --helm-bundle <path/to/helm-charts.tar> \
[--listen-address <addr>] \
[--list-port <port>] \
[--tls-cert-file <path/to/cert/file> --tls-private-key-file <path/to/key/file>]
Expand Down
4 changes: 4 additions & 0 deletions cmd/mindthegap/create/helmbundle/helm_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os"
"path/filepath"
"sort"

"github.com/spf13/cobra"
"helm.sh/helm/v3/pkg/action"
Expand Down Expand Up @@ -124,6 +125,8 @@ func NewCommand(out output.Output) *cobra.Command {

for repoName, repoConfig := range cfg.Repositories {
for chartName, chartVersions := range repoConfig.Charts {
sort.Strings(chartVersions)

out.StartOperation(
fmt.Sprintf(
"Fetching Helm chart %s (versions %v) from %s (%s)",
Expand All @@ -149,6 +152,7 @@ func NewCommand(out output.Output) *cobra.Command {
repoConfig.RepoURL,
chartName,
chartVersion,
[]helm.ConfigOpt{helm.RegistryClientConfigOpt()},
opts...,
)
if err != nil {
Expand Down
42 changes: 21 additions & 21 deletions cmd/mindthegap/push/helmbundle/helm_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import (
func NewCommand(out output.Output) *cobra.Command {
var (
helmBundleFiles []string
destRepository string
destRegistry string
destRegistrySkipTLSVerify bool
destRepositoryUsername string
destRepositoryPassword string
destRegistryUsername string
destRegistryPassword string
)

cmd := &cobra.Command{
Expand Down Expand Up @@ -76,18 +76,18 @@ func NewCommand(out output.Output) *cobra.Command {
skopeoOpts := []skopeo.SkopeoOption{
skopeo.PreserveDigests(),
}
if destRepositoryUsername != "" && destRepositoryPassword != "" {
if destRegistryUsername != "" && destRegistryPassword != "" {
skopeoOpts = append(
skopeoOpts,
skopeo.DestCredentials(
destRepositoryUsername,
destRepositoryPassword,
destRegistryUsername,
destRegistryPassword,
),
)
} else {
skopeoStdout, skopeoStderr, err := skopeoRunner.AttemptToLoginToRegistry(
context.Background(),
destRepository,
destRegistry,
)
if err != nil {
out.Infof("---skopeo stdout---:\n%s", skopeoStdout)
Expand All @@ -100,7 +100,7 @@ func NewCommand(out output.Output) *cobra.Command {

// Determine type of destination registry.
var prePushFuncs []prePushFunc
if ecr.IsECRRegistry(destRepository) {
if ecr.IsECRRegistry(destRegistry) {
prePushFuncs = append(
prePushFuncs,
ecr.EnsureRepositoryExistsFunc(""),
Expand All @@ -110,7 +110,7 @@ func NewCommand(out output.Output) *cobra.Command {
return pushOCIArtifacts(
cfg,
fmt.Sprintf("%s/charts", reg.Address()),
destRepository,
destRegistry,
skopeoOpts,
destRegistrySkipTLSVerify,
out,
Expand All @@ -123,14 +123,14 @@ func NewCommand(out output.Output) *cobra.Command {
cmd.Flags().StringSliceVar(&helmBundleFiles, "helm-bundle", nil,
"Tarball containing list of Helm charts to push. Can also be a glob pattern.")
_ = cmd.MarkFlagRequired("helm-bundle")
cmd.Flags().StringVar(&destRepository, "to-repository", "", "Repository to push images to")
_ = cmd.MarkFlagRequired("to-repository")
cmd.Flags().BoolVar(&destRegistrySkipTLSVerify, "to-repository-insecure-skip-tls-verify", false,
"Skip TLS verification of repository to push images to (use for http repositories)")
cmd.Flags().StringVar(&destRepositoryUsername, "to-repository-username", "",
cmd.Flags().StringVar(&destRegistry, "to-registry", "", "Registry to push images to")
_ = cmd.MarkFlagRequired("to-registry")
cmd.Flags().BoolVar(&destRegistrySkipTLSVerify, "to-registry-insecure-skip-tls-verify", false,
"Skip TLS verification of registry to push images to (use for http registries)")
cmd.Flags().StringVar(&destRegistryUsername, "to-registry-username", "",
"Username to use to log in to destination repository")
cmd.Flags().StringVar(&destRepositoryPassword, "to-repository-password", "",
"Password to use to log in to destination repository")
cmd.Flags().StringVar(&destRegistryPassword, "to-registry-password", "",
"Password to use to log in to destination registry")

return cmd
}
Expand All @@ -139,7 +139,7 @@ type prePushFunc func(destRegistry, imageName string, imageTags ...string) error

func pushOCIArtifacts(
cfg config.HelmChartsConfig,
sourceRepository, destRepository string,
sourceRegistry, destRegistry string,
skopeoOpts []skopeo.SkopeoOption,
destRegistrySkipTLSVerify bool,
out output.Output,
Expand All @@ -164,7 +164,7 @@ func pushOCIArtifacts(
chartVersions := repoConfig.Charts[chartName]

for _, prePush := range prePushFuncs {
if err := prePush("", destRepository); err != nil {
if err := prePush("", destRegistry); err != nil {
return fmt.Errorf("pre-push func failed: %w", err)
}
}
Expand All @@ -173,12 +173,12 @@ func pushOCIArtifacts(
out.StartOperation(
fmt.Sprintf("Copying %s:%s (from bundle) to %s/%s:%s",
chartName, chartVersion,
destRepository, chartName, chartVersion,
destRegistry, chartName, chartVersion,
),
)
skopeoStdout, skopeoStderr, err := skopeoRunner.Copy(context.TODO(),
fmt.Sprintf("docker://%s/%s:%s", sourceRepository, chartName, chartVersion),
fmt.Sprintf("docker://%s/%s:%s", destRepository, chartName, chartVersion),
fmt.Sprintf("docker://%s/%s:%s", sourceRegistry, chartName, chartVersion),
fmt.Sprintf("docker://%s/%s:%s", destRegistry, chartName, chartVersion),
append(
skopeoOpts, skopeo.All(),
)...,
Expand Down
21 changes: 14 additions & 7 deletions cmd/mindthegap/serve/helmbundle/helm_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package helmbundle

import (
"fmt"
"net/http"
"os"
"path/filepath"

Expand All @@ -18,7 +19,7 @@ import (
"github.com/mesosphere/mindthegap/docker/registry"
)

func NewCommand(out output.Output) *cobra.Command {
func NewCommand(out output.Output) (cmd *cobra.Command, stopCh chan struct{}) {
var (
helmBundleFiles []string
listenAddress string
Expand All @@ -27,7 +28,9 @@ func NewCommand(out output.Output) *cobra.Command {
tlsKey string
)

cmd := &cobra.Command{
stopCh = make(chan struct{})

cmd = &cobra.Command{
Use: "helm-bundle",
Short: "Serve a Helm chart repository in an OCI registry from Helm chart bundles",
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -74,10 +77,14 @@ func NewCommand(out output.Output) *cobra.Command {
}
out.EndOperation(true)
out.Infof("Listening on %s\n", reg.Address())
if err := reg.ListenAndServe(); err != nil {
out.Error(err, "error serving Docker registry")
os.Exit(2)
}

go func() {
if err := reg.ListenAndServe(); err != nil && err != http.ErrServerClosed {
out.Error(err, "error serving Docker registry")
os.Exit(2)
}
}()
<-stopCh

return nil
},
Expand All @@ -92,5 +99,5 @@ func NewCommand(out output.Output) *cobra.Command {
cmd.Flags().StringVar(&tlsCertificate, "tls-cert-file", "", "TLS certificate file")
cmd.Flags().StringVar(&tlsKey, "tls-private-key-file", "", "TLS private key file")

return cmd
return cmd, stopCh
}
5 changes: 4 additions & 1 deletion cmd/mindthegap/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func NewCommand(out output.Output) *cobra.Command {
}

cmd.AddCommand(imagebundle.NewCommand(out))
cmd.AddCommand(helmbundle.NewCommand(out))

helmBundleCmd, _ := helmbundle.NewCommand(out)
cmd.AddCommand(helmBundleCmd)

return cmd
}
37 changes: 29 additions & 8 deletions docker/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ package registry
import (
"bytes"
"context"
"errors"
"fmt"
"net/http"
"strings"
"text/template"
"time"

"github.com/distribution/distribution/v3/configuration"
"github.com/distribution/distribution/v3/registry"
"github.com/distribution/distribution/v3/registry/handlers"
_ "github.com/distribution/distribution/v3/registry/storage/driver/filesystem"
"github.com/phayes/freeport"
"github.com/sirupsen/logrus"
)

type Config struct {
Expand Down Expand Up @@ -99,7 +103,7 @@ log:

type Registry struct {
config *configuration.Configuration
delegate *registry.Registry
delegate *http.Server
address string
}

Expand All @@ -109,11 +113,13 @@ func NewRegistry(cfg Config) (*Registry, error) {
return nil, err
}

registryConfig.Log.Level = "fatal"
logrus.SetLevel(logrus.FatalLevel)
regHandler := handlers.NewApp(context.Background(), registryConfig)

reg, err := registry.NewRegistry(context.TODO(), registryConfig)
if err != nil {
return nil, fmt.Errorf("failed to create registry: %w", err)
reg := &http.Server{
Addr: registryConfig.HTTP.Addr,
Handler: regHandler,
ReadHeaderTimeout: 1 * time.Second,
}

return &Registry{
Expand All @@ -127,6 +133,21 @@ func (r Registry) Address() string {
return r.address
}

func (r *Registry) ListenAndServe() error {
return r.delegate.ListenAndServe()
func (r Registry) Shutdown(ctx context.Context) error {
return r.delegate.Shutdown(ctx)
}

func (r Registry) ListenAndServe() error {
var err error
if r.config.HTTP.TLS.Certificate != "" && r.config.HTTP.TLS.Key != "" {
err = r.delegate.ListenAndServeTLS(r.config.HTTP.TLS.Certificate, r.config.HTTP.TLS.Key)
} else {
err = r.delegate.ListenAndServe()
}

if err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
}

return nil
}
12 changes: 3 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ require (
github.com/hashicorp/go-getter v1.6.2
github.com/mesosphere/dkp-cli-runtime/core v0.5.2
github.com/mholt/archiver/v3 v3.5.1
github.com/onsi/ginkgo/v2 v2.1.6
github.com/onsi/gomega v1.20.2
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.5.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.0
Expand All @@ -38,7 +41,6 @@ require (
github.com/Masterminds/squirrel v1.5.3 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535 // indirect
github.com/aws/aws-sdk-go v1.43.16 // indirect
Expand All @@ -55,10 +57,6 @@ require (
github.com/aws/smithy-go v1.13.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/bshuster-repo/logrus-logstash-hook v1.0.0 // indirect
github.com/bugsnag/bugsnag-go v0.0.0-20141110184014-b1d153021fcd // indirect
github.com/bugsnag/osext v0.0.0-20130617224835-0dd3f918b21b // indirect
github.com/bugsnag/panicwrap v0.0.0-20151223152923-e2c28503fcd0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/chai2010/gettext-go v0.0.0-20160711120539-c6fed771bfd5 // indirect
github.com/containerd/containerd v1.6.6 // indirect
Expand Down Expand Up @@ -152,17 +150,13 @@ require (
github.com/russross/blackfriday v1.6.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect
github.com/yvasiyarov/go-metrics v0.0.0-20140926110328-57bccd1ccd43 // indirect
github.com/yvasiyarov/gorelic v0.0.0-20141212073537-a9bba5b9ab50 // indirect
github.com/yvasiyarov/newrelic_platform_go v0.0.0-20140908184405-b21fdbd4370f // indirect
go.opencensus.io v0.23.0 // indirect
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
Expand Down
Loading

0 comments on commit da08e73

Please sign in to comment.