diff --git a/cmd/flux/export.go b/cmd/flux/export.go index 99c11db1..173bb763 100644 --- a/cmd/flux/export.go +++ b/cmd/flux/export.go @@ -113,8 +113,8 @@ func printExport(export interface{}) error { if err != nil { return err } - fmt.Println("---") - fmt.Println(resourceToString(data)) + rootCmd.Println("---") + rootCmd.Println(resourceToString(data)) return nil } diff --git a/cmd/flux/export_test.go b/cmd/flux/export_test.go new file mode 100644 index 00000000..74b9bf5d --- /dev/null +++ b/cmd/flux/export_test.go @@ -0,0 +1,88 @@ +// +build unit + +package main + +import ( + "testing" +) + +func TestExport(t *testing.T) { + cases := []struct { + name string + arg string + goldenFile string + }{ + { + "alert-provider", + "export alert-provider slack", + "testdata/export/provider.yaml", + }, + { + "alert", + "export alert flux-system", + "testdata/export/alert.yaml", + }, + { + "image policy", + "export image policy flux-system", + "testdata/export/image-policy.yaml", + }, + { + "image repository", + "export image repository flux-system", + "testdata/export/image-repo.yaml", + }, + { + "image update", + "export image update flux-system", + "testdata/export/image-update.yaml", + }, + { + "source git", + "export source git flux-system", + "testdata/export/git-repo.yaml", + }, + { + "source helm", + "export source helm flux-system", + "testdata/export/helm-repo.yaml", + }, + { + "receiver", + "export receiver flux-system", + "testdata/export/receiver.yaml", + }, + { + "kustomization", + "export kustomization flux-system", + "testdata/export/ks.yaml", + }, + { + "helmrelease", + "export helmrelease flux-system", + "testdata/export/helm-release.yaml", + }, + { + "bucket", + "export source bucket flux-system", + "testdata/export/bucket.yaml", + }, + } + + objectFile := "testdata/export/objects.yaml" + tmpl := map[string]string{ + "fluxns": allocateNamespace("flux-system"), + } + testEnv.CreateObjectFile(objectFile, tmpl, t) + + for _, tt := range cases { + t.Run(tt.name, func(t *testing.T) { + cmd := cmdTestCase{ + args: tt.arg + " -n=" + tmpl["fluxns"], + assert: assertGoldenTemplateFile(tt.goldenFile, tmpl), + } + + cmd.runTestCmd(t) + }) + } +} diff --git a/cmd/flux/testdata/export/alert.yaml b/cmd/flux/testdata/export/alert.yaml new file mode 100644 index 00000000..a8a7883a --- /dev/null +++ b/cmd/flux/testdata/export/alert.yaml @@ -0,0 +1,17 @@ +--- +apiVersion: notification.toolkit.fluxcd.io/v1beta1 +kind: Alert +metadata: + name: flux-system + namespace: {{ .fluxns }} +spec: + eventSeverity: info + eventSources: + - kind: GitRepository + name: '*' + - kind: Kustomization + name: '*' + providerRef: + name: slack + summary: Slacktest Notification + diff --git a/cmd/flux/testdata/export/bucket.yaml b/cmd/flux/testdata/export/bucket.yaml new file mode 100644 index 00000000..c998daf4 --- /dev/null +++ b/cmd/flux/testdata/export/bucket.yaml @@ -0,0 +1,14 @@ +--- +apiVersion: source.toolkit.fluxcd.io/v1beta1 +kind: Bucket +metadata: + name: flux-system + namespace: {{ .fluxns }} +spec: + bucketName: podinfo + endpoint: s3.amazonaws.com + interval: 5m0s + provider: aws + region: us-east-1 + timeout: 30s + diff --git a/cmd/flux/testdata/export/git-repo.yaml b/cmd/flux/testdata/export/git-repo.yaml new file mode 100644 index 00000000..ba7c3b6c --- /dev/null +++ b/cmd/flux/testdata/export/git-repo.yaml @@ -0,0 +1,16 @@ +--- +apiVersion: source.toolkit.fluxcd.io/v1beta1 +kind: GitRepository +metadata: + name: flux-system + namespace: {{ .fluxns }} +spec: + gitImplementation: go-git + interval: 5m0s + ref: + branch: main + secretRef: + name: flux-system + timeout: 20s + url: ssh://git@github.com/example/repo + diff --git a/cmd/flux/testdata/export/helm-release.yaml b/cmd/flux/testdata/export/helm-release.yaml new file mode 100644 index 00000000..8f861110 --- /dev/null +++ b/cmd/flux/testdata/export/helm-release.yaml @@ -0,0 +1,18 @@ +--- +apiVersion: helm.toolkit.fluxcd.io/v2beta1 +kind: HelmRelease +metadata: + name: flux-system + namespace: {{ .fluxns }} +spec: + chart: + spec: + chart: podinfo + reconcileStrategy: ChartVersion + sourceRef: + kind: HelmRepository + name: flux-systen + namespace: {{ .fluxns }} + version: '*' + interval: 5m0s + diff --git a/cmd/flux/testdata/export/helm-repo.yaml b/cmd/flux/testdata/export/helm-repo.yaml new file mode 100644 index 00000000..1503ebf8 --- /dev/null +++ b/cmd/flux/testdata/export/helm-repo.yaml @@ -0,0 +1,11 @@ +--- +apiVersion: source.toolkit.fluxcd.io/v1beta1 +kind: HelmRepository +metadata: + name: flux-system + namespace: {{ .fluxns }} +spec: + interval: 5m0s + timeout: 1m0s + url: https://stefanprodan.github.io/podinfo + diff --git a/cmd/flux/testdata/export/image-policy.yaml b/cmd/flux/testdata/export/image-policy.yaml new file mode 100644 index 00000000..4a1bcbca --- /dev/null +++ b/cmd/flux/testdata/export/image-policy.yaml @@ -0,0 +1,13 @@ +--- +apiVersion: image.toolkit.fluxcd.io/v1beta1 +kind: ImagePolicy +metadata: + name: flux-system + namespace: {{ .fluxns }} +spec: + imageRepositoryRef: + name: flux-system + policy: + semver: + range: 5.0.x + diff --git a/cmd/flux/testdata/export/image-repo.yaml b/cmd/flux/testdata/export/image-repo.yaml new file mode 100644 index 00000000..959126be --- /dev/null +++ b/cmd/flux/testdata/export/image-repo.yaml @@ -0,0 +1,10 @@ +--- +apiVersion: image.toolkit.fluxcd.io/v1beta1 +kind: ImageRepository +metadata: + name: flux-system + namespace: {{ .fluxns }} +spec: + image: ghcr.io/test/podinfo + interval: 1m0s + diff --git a/cmd/flux/testdata/export/image-update.yaml b/cmd/flux/testdata/export/image-update.yaml new file mode 100644 index 00000000..35566278 --- /dev/null +++ b/cmd/flux/testdata/export/image-update.yaml @@ -0,0 +1,20 @@ +--- +apiVersion: image.toolkit.fluxcd.io/v1beta1 +kind: ImageUpdateAutomation +metadata: + name: flux-system + namespace: {{ .fluxns }} +spec: + git: + commit: + author: + email: fluxcdbot@users.noreply.github.com + name: fluxcdbot + interval: 1m0s + sourceRef: + kind: GitRepository + name: flux-system + update: + path: ./clusters/my-cluster + strategy: Setters + diff --git a/cmd/flux/testdata/export/ks.yaml b/cmd/flux/testdata/export/ks.yaml new file mode 100644 index 00000000..cd6085d2 --- /dev/null +++ b/cmd/flux/testdata/export/ks.yaml @@ -0,0 +1,14 @@ +--- +apiVersion: kustomize.toolkit.fluxcd.io/v1beta2 +kind: Kustomization +metadata: + name: flux-system + namespace: {{ .fluxns }} +spec: + interval: 5m0s + path: ./infrastructure/ + prune: true + sourceRef: + kind: GitRepository + name: flux-system + diff --git a/cmd/flux/testdata/export/objects.yaml b/cmd/flux/testdata/export/objects.yaml new file mode 100644 index 00000000..6d46e187 --- /dev/null +++ b/cmd/flux/testdata/export/objects.yaml @@ -0,0 +1,153 @@ +--- +apiVersion: v1 +kind: Namespace +metadata: + name: {{ .fluxns }} +--- +apiVersion: notification.toolkit.fluxcd.io/v1beta1 +kind: Provider +metadata: + name: slack + namespace: {{ .fluxns }} +spec: + type: slack + channel: 'A channel with spacess' + address: https://hooks.slack.com/services/mock +--- +apiVersion: notification.toolkit.fluxcd.io/v1beta1 +kind: Alert +metadata: + name: flux-system + namespace: {{ .fluxns }} +spec: + summary: "Slacktest Notification" + providerRef: + name: slack + eventSeverity: info + eventSources: + - kind: "GitRepository" + name: "*" + - kind: "Kustomization" + name: "*" +--- +apiVersion: image.toolkit.fluxcd.io/v1beta1 +kind: ImageRepository +metadata: + name: flux-system + namespace: {{ .fluxns }} +spec: + image: ghcr.io/test/podinfo + interval: 1m0s +--- +apiVersion: image.toolkit.fluxcd.io/v1beta1 +kind: ImagePolicy +metadata: + name: flux-system + namespace: {{ .fluxns }} +spec: + imageRepositoryRef: + name: flux-system + policy: + semver: + range: 5.0.x +--- +apiVersion: image.toolkit.fluxcd.io/v1beta1 +kind: ImageUpdateAutomation +metadata: + name: flux-system + namespace: {{ .fluxns }} +spec: + interval: 1m0s + sourceRef: + kind: GitRepository + name: flux-system + git: + commit: + author: + email: fluxcdbot@users.noreply.github.com + name: fluxcdbot + messageTemplate: '{{range .Updated.Images}}{{println .}}{{end}}' + update: + path: ./clusters/my-cluster + strategy: Setters +--- +apiVersion: source.toolkit.fluxcd.io/v1beta1 +kind: GitRepository +metadata: + name: flux-system + namespace: {{ .fluxns }} +spec: + ref: + branch: main + secretRef: + name: flux-system + interval: 5m + url: ssh://git@github.com/example/repo +--- +apiVersion: kustomize.toolkit.fluxcd.io/v1beta1 +kind: Kustomization +metadata: + name: flux-system + namespace: {{ .fluxns }} +spec: + path: ./infrastructure/ + sourceRef: + kind: GitRepository + name: flux-system + interval: 5m + prune: true +--- +apiVersion: notification.toolkit.fluxcd.io/v1beta1 +kind: Receiver +metadata: + name: flux-system + namespace: {{ .fluxns }} +spec: + type: github + events: + - "ping" + - "push" + secretRef: + name: webhook-token + resources: + - kind: GitRepository + name: flux-system + namespace: flux-system +--- +apiVersion: source.toolkit.fluxcd.io/v1beta1 +kind: HelmRepository +metadata: + name: flux-system + namespace: {{ .fluxns }} +spec: + interval: 5m + timeout: 1m0s + url: https://stefanprodan.github.io/podinfo +--- +apiVersion: helm.toolkit.fluxcd.io/v2beta1 +kind: HelmRelease +metadata: + name: flux-system + namespace: {{ .fluxns }} +spec: + interval: 5m + chart: + spec: + chart: podinfo + sourceRef: + kind: HelmRepository + name: flux-systen + namespace: {{ .fluxns }} +--- +apiVersion: source.toolkit.fluxcd.io/v1beta1 +kind: Bucket +metadata: + name: flux-system + namespace: {{ .fluxns }} +spec: + interval: 5m + provider: aws + bucketName: podinfo + endpoint: s3.amazonaws.com + region: us-east-1 + timeout: 30s diff --git a/cmd/flux/testdata/export/provider.yaml b/cmd/flux/testdata/export/provider.yaml new file mode 100644 index 00000000..ce91902f --- /dev/null +++ b/cmd/flux/testdata/export/provider.yaml @@ -0,0 +1,11 @@ +--- +apiVersion: notification.toolkit.fluxcd.io/v1beta1 +kind: Provider +metadata: + name: slack + namespace: {{ .fluxns }} +spec: + address: https://hooks.slack.com/services/mock + channel: A channel with spacess + type: slack + diff --git a/cmd/flux/testdata/export/receiver.yaml b/cmd/flux/testdata/export/receiver.yaml new file mode 100644 index 00000000..e4fefadb --- /dev/null +++ b/cmd/flux/testdata/export/receiver.yaml @@ -0,0 +1,18 @@ +--- +apiVersion: notification.toolkit.fluxcd.io/v1beta1 +kind: Receiver +metadata: + name: flux-system + namespace: {{ .fluxns }} +spec: + events: + - ping + - push + resources: + - kind: GitRepository + name: flux-system + namespace: flux-system + secretRef: + name: webhook-token + type: github +