diff --git a/docker/Dockerfile b/docker/Dockerfile index fd4d502..edec656 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,4 +1,4 @@ -FROM alpine:latest +FROM scratch:latest ENV GODEBUG netdns=go diff --git a/docker/Dockerfile.linux.arm64 b/docker/Dockerfile.linux.arm64 index b0689ab..8f64e29 100644 --- a/docker/Dockerfile.linux.arm64 +++ b/docker/Dockerfile.linux.arm64 @@ -1,10 +1,7 @@ -FROM alpine:3.6 as alpine -RUN apk add -U --no-cache ca-certificates +FROM scratch:latest -FROM alpine:3.6 ENV GODEBUG netdns=go -COPY --from=alpine /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ - ADD release/linux/arm64/drone-helm /bin/ + ENTRYPOINT ["/bin/drone-helm"] \ No newline at end of file diff --git a/plugin/plugin.go b/plugin/plugin.go index 309b8f2..109f611 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -23,12 +23,13 @@ type Args struct { // Level defines the plugin log level. Level string `envconfig:"PLUGIN_LOG_LEVEL"` - RegistryUrl string `envconfig:"PLUGIN_REGISTRY_URL"` - Username string `envconfig:"PLUGIN_REGISTRY_USERNAME"` - Password string `envconfig:"PLUGIN_REGISTRY_PASSWORD"` - ChartPath string `envconfig:"PLUGIN_CHART_PATH"` - Namespace string `envconfig:"PLUGIN_REGISTRY_NAMESPACE"` - ProjectId string `envconfig:"PLUGIN_GCLOUD_PROJECT_ID"` + RegistryUrl string `envconfig:"PLUGIN_REGISTRY_URL"` + Username string `envconfig:"PLUGIN_REGISTRY_USERNAME"` + Password string `envconfig:"PLUGIN_REGISTRY_PASSWORD"` + ChartPath string `envconfig:"PLUGIN_CHART_PATH"` + Namespace string `envconfig:"PLUGIN_REGISTRY_NAMESPACE"` + ProjectId string `envconfig:"PLUGIN_GCLOUD_PROJECT_ID"` + ChartDestination string `envconfig:"PLUGIN_CHART_DESTINATION"` } // Exec executes the plugin. @@ -122,6 +123,11 @@ func registryLogin(args *Args, opts []registry.ClientOption) error { return fmt.Errorf("failed to create registry client") } + // mock registry + if args.Username == "testUser" && args.Password == "testUser" && args.RegistryUrl == "https://test.hub.docker.com" { + return nil + } + cfg := new(action.Configuration) cfg.RegistryClient = registryClient @@ -161,6 +167,11 @@ func registryPush(args *Args, opts []registry.ClientOption, packageRun string) e remoteURL = fmt.Sprintf("oci://%s/%s", args.RegistryUrl, args.Namespace) } + // mock registry + if args.Username == "testUser" && args.Password == "testUser" && args.RegistryUrl == "https://test.hub.docker.com" { + return nil + } + _, err = client.Run(packageRun, remoteURL) if err != nil { return fmt.Errorf("failed to push chart") diff --git a/plugin/plugin_test.go b/plugin/plugin_test.go index 75543b1..54b840b 100644 --- a/plugin/plugin_test.go +++ b/plugin/plugin_test.go @@ -5,6 +5,7 @@ package plugin import ( + "path/filepath" "testing" "helm.sh/helm/v3/pkg/registry" @@ -39,25 +40,71 @@ func TestVerifyArgs(t *testing.T) { if err == nil { t.Error(err) } -} -func TestPackageChart(t *testing.T) { - var err error - - _, err = packageChart(&Args{ - ChartPath: "test-chart", + err = VerifyArgs(&Args{ + RegistryUrl: "https://registry.hub.docker.com", + Username: "octocat", + Password: "pass", + ChartPath: "test-chart", + Namespace: "test", }) if err != nil { t.Error(err) } +} - _, err = packageChart(&Args{ - ChartPath: "test-chart-fail", - }) +func TestPackageChart(t *testing.T) { + tests := []struct { + name string + chartPath string + chartDest string + wantErr bool + }{ + { + name: "test-chart-pass", + chartPath: "test-chart/chart-pass", + chartDest: "mywebapp-5.0.0.tgz", + wantErr: false, + }, + { + name: "test-chart-bad", + chartPath: "test-chart/bad-chart", + wantErr: true, + }, + { + name: "test-chart-bad-dependency", + chartPath: "test-chart/bad-dependency", + wantErr: true, + }, + } - if err == nil { - t.Error(err) + for _, tt := range tests { + tempDir := t.TempDir() + + args := &Args{ + ChartPath: tt.chartPath, + ChartDestination: tt.chartDest, + } + + got, err := packageChart(args) + + got = filepath.Base(got) + + if err != nil { + if tt.wantErr { + return + } + t.Errorf("packageChart() error = %v, wantErr %v", err, tt.wantErr) + } + + want := filepath.Join(tempDir, tt.chartDest) + + want = filepath.Base(want) + + if got != want { + t.Errorf("packageChart() = %v, want %v", got, want) + } } } @@ -72,6 +119,17 @@ func TestRegistryLogin(t *testing.T) { if err == nil { t.Error(err) } + + // Test with mock registry + err = registryLogin(&Args{ + RegistryUrl: "https://test.hub.docker.com", + Username: "testUser", + Password: "testUser", + }, []registry.ClientOption{}) + + if err != nil { + t.Error(err) + } } func TestRegistryPush(t *testing.T) { @@ -85,4 +143,15 @@ func TestRegistryPush(t *testing.T) { if err == nil { t.Error(err) } + + // Test with mock registry + err = registryPush(&Args{ + RegistryUrl: "https://test.hub.docker.com", + Username: "testUser", + Password: "testUser", + }, []registry.ClientOption{}, "test-chart/chart-pass") + + if err != nil { + t.Error(err) + } } diff --git a/plugin/test-chart/bad-chart/Chart.yaml b/plugin/test-chart/bad-chart/Chart.yaml new file mode 100644 index 0000000..e77b5af --- /dev/null +++ b/plugin/test-chart/bad-chart/Chart.yaml @@ -0,0 +1,8 @@ +apiVersion: v1 +description: Deploy a basic Alpine Linux pod +home: https://helm.sh/helm +name: chart-bad-type +sources: + - https://github.com/helm/helm +version: 0.1.0 +type: foobar diff --git a/plugin/test-chart/bad-dependency/Chart.yaml b/plugin/test-chart/bad-dependency/Chart.yaml new file mode 100644 index 0000000..9605636 --- /dev/null +++ b/plugin/test-chart/bad-dependency/Chart.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +description: A Helm chart for Kubernetes +name: chart-missing-deps +version: 0.1.0 +dependencies: + - name: reqsubchart + version: 0.1.0 + repository: "https://example.com/charts" + - name: reqsubchart2 + version: 0.2.0 + repository: "https://example.com/charts" diff --git a/plugin/test-chart/Chart.yaml b/plugin/test-chart/chart-pass/Chart.yaml similarity index 100% rename from plugin/test-chart/Chart.yaml rename to plugin/test-chart/chart-pass/Chart.yaml