From b263e14fa8c603babf2908989672f849ab1ae5cc Mon Sep 17 00:00:00 2001 From: Hidde Beydals Date: Wed, 15 Feb 2023 13:11:37 +0100 Subject: [PATCH] tests: only provide template values when used As otherwise the `.golden` values can not be automatically updated using `-update` as documented in `CONTRIBUTING.md`. Also ensure we do not use `defer` but rather `t.Cleanup` in tests, as this will always be called even if e.g. `t.Fatal` absruptly stops the test. Signed-off-by: Hidde Beydals --- cmd/flux/build_artifact_test.go | 2 +- cmd/flux/build_kustomization_test.go | 3 +-- cmd/flux/diff_kustomization_test.go | 4 +++- cmd/flux/export_test.go | 28 +++++++++++++++++++++------- cmd/flux/helmrelease_test.go | 26 ++++++++++++++++++-------- cmd/flux/image_test.go | 14 +++++++------- cmd/flux/install_test.go | 4 +--- cmd/flux/kustomization_test.go | 26 ++++++++++++++++++-------- cmd/flux/source_oci_test.go | 25 +++++++++++++++++-------- 9 files changed, 87 insertions(+), 45 deletions(-) diff --git a/cmd/flux/build_artifact_test.go b/cmd/flux/build_artifact_test.go index 37ed8f02..ba84186c 100644 --- a/cmd/flux/build_artifact_test.go +++ b/cmd/flux/build_artifact_test.go @@ -54,7 +54,7 @@ data: tmpFile, err := saveReaderToFile(strings.NewReader(tt.string)) g.Expect(err).To(BeNil()) - defer os.Remove(tmpFile) + t.Cleanup(func() { _ = os.Remove(tmpFile) }) b, err := os.ReadFile(tmpFile) if tt.expectErr { diff --git a/cmd/flux/build_kustomization_test.go b/cmd/flux/build_kustomization_test.go index 5d78a7cb..b4078195 100644 --- a/cmd/flux/build_kustomization_test.go +++ b/cmd/flux/build_kustomization_test.go @@ -171,8 +171,7 @@ spec: if err != nil { t.Fatal(err) } - - defer os.Remove("./testdata/build-kustomization/podinfo.yaml") + t.Cleanup(func() { _ = os.Remove("./testdata/build-kustomization/podinfo.yaml") }) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/cmd/flux/diff_kustomization_test.go b/cmd/flux/diff_kustomization_test.go index 3c7a3071..1c92268c 100644 --- a/cmd/flux/diff_kustomization_test.go +++ b/cmd/flux/diff_kustomization_test.go @@ -109,7 +109,9 @@ func TestDiffKustomization(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if tt.objectFile != "" { - resourceManager.ApplyAll(context.Background(), createObjectFromFile(tt.objectFile, tmpl, t), ssa.DefaultApplyOptions()) + if _, err := resourceManager.ApplyAll(context.Background(), createObjectFromFile(tt.objectFile, tmpl, t), ssa.DefaultApplyOptions()); err != nil { + t.Error(err) + } } cmd := cmdTestCase{ args: tt.args + " -n " + tmpl["fluxns"], diff --git a/cmd/flux/export_test.go b/cmd/flux/export_test.go index dc59caa2..85ae174a 100644 --- a/cmd/flux/export_test.go +++ b/cmd/flux/export_test.go @@ -8,78 +8,92 @@ import ( ) func TestExport(t *testing.T) { + namespace := allocateNamespace("flux-system") + + objectFile := "testdata/export/objects.yaml" + tmpl := map[string]string{ + "fluxns": namespace, + } + testEnv.CreateObjectFile(objectFile, tmpl, t) + cases := []struct { name string arg string goldenFile string + tmpl map[string]string }{ { "alert-provider", "export alert-provider slack", "testdata/export/provider.yaml", + tmpl, }, { "alert", "export alert flux-system", "testdata/export/alert.yaml", + tmpl, }, { "image policy", "export image policy flux-system", "testdata/export/image-policy.yaml", + tmpl, }, { "image repository", "export image repository flux-system", "testdata/export/image-repo.yaml", + tmpl, }, { "image update", "export image update flux-system", "testdata/export/image-update.yaml", + tmpl, }, { "source git", "export source git flux-system", "testdata/export/git-repo.yaml", + tmpl, }, { "source helm", "export source helm flux-system", "testdata/export/helm-repo.yaml", + tmpl, }, { "receiver", "export receiver flux-system", "testdata/export/receiver.yaml", + tmpl, }, { "kustomization", "export kustomization flux-system", "testdata/export/ks.yaml", + tmpl, }, { "helmrelease", "export helmrelease flux-system", "testdata/export/helm-release.yaml", + tmpl, }, { "bucket", "export source bucket flux-system", "testdata/export/bucket.yaml", + tmpl, }, } - 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"], + args: tt.arg + " -n=" + namespace, assert: assertGoldenTemplateFile(tt.goldenFile, tmpl), } diff --git a/cmd/flux/helmrelease_test.go b/cmd/flux/helmrelease_test.go index dcb9ff58..2596ca48 100644 --- a/cmd/flux/helmrelease_test.go +++ b/cmd/flux/helmrelease_test.go @@ -22,51 +22,61 @@ package main import "testing" func TestHelmReleaseFromGit(t *testing.T) { + namespace := allocateNamespace("thrfg") + del, err := setupTestNamespace(namespace) + if err != nil { + t.Fatal(err) + } + t.Cleanup(del) + + tmpl := map[string]string{"ns": namespace} + cases := []struct { args string goldenFile string + tmpl map[string]string }{ { "create source git thrfg --url=https://github.com/stefanprodan/podinfo --branch=main --tag=6.0.0", "testdata/helmrelease/create_source_git.golden", + nil, }, { "create helmrelease thrfg --source=GitRepository/thrfg --chart=./charts/podinfo", "testdata/helmrelease/create_helmrelease_from_git.golden", + nil, }, { "get helmrelease thrfg", "testdata/helmrelease/get_helmrelease_from_git.golden", + nil, }, { "reconcile helmrelease thrfg --with-source", "testdata/helmrelease/reconcile_helmrelease_from_git.golden", + tmpl, }, { "suspend helmrelease thrfg", "testdata/helmrelease/suspend_helmrelease_from_git.golden", + tmpl, }, { "resume helmrelease thrfg", "testdata/helmrelease/resume_helmrelease_from_git.golden", + tmpl, }, { "delete helmrelease thrfg --silent", "testdata/helmrelease/delete_helmrelease_from_git.golden", + tmpl, }, } - namespace := allocateNamespace("thrfg") - del, err := setupTestNamespace(namespace) - if err != nil { - t.Fatal(err) - } - defer del() - for _, tc := range cases { cmd := cmdTestCase{ args: tc.args + " -n=" + namespace, - assert: assertGoldenTemplateFile(tc.goldenFile, map[string]string{"ns": namespace}), + assert: assertGoldenTemplateFile(tc.goldenFile, tc.tmpl), } cmd.runTestCmd(t) } diff --git a/cmd/flux/image_test.go b/cmd/flux/image_test.go index 155326fa..7b705283 100644 --- a/cmd/flux/image_test.go +++ b/cmd/flux/image_test.go @@ -22,6 +22,13 @@ package main import "testing" func TestImageScanning(t *testing.T) { + namespace := allocateNamespace("tis") + del, err := setupTestNamespace(namespace) + if err != nil { + t.Fatal(err) + } + t.Cleanup(del) + cases := []struct { args string goldenFile string @@ -48,13 +55,6 @@ func TestImageScanning(t *testing.T) { }, } - namespace := allocateNamespace("tis") - del, err := setupTestNamespace(namespace) - if err != nil { - t.Fatal(err) - } - defer del() - for _, tc := range cases { cmd := cmdTestCase{ args: tc.args + " -n=" + namespace, diff --git a/cmd/flux/install_test.go b/cmd/flux/install_test.go index a0251206..f10f7d62 100644 --- a/cmd/flux/install_test.go +++ b/cmd/flux/install_test.go @@ -25,9 +25,7 @@ func TestInstall(t *testing.T) { // Given that this test uses an invalid namespace, it ensures // to restore whatever value it had previously. currentNamespace := *kubeconfigArgs.Namespace - defer func() { - *kubeconfigArgs.Namespace = currentNamespace - }() + t.Cleanup(func() { *kubeconfigArgs.Namespace = currentNamespace }) tests := []struct { name string diff --git a/cmd/flux/kustomization_test.go b/cmd/flux/kustomization_test.go index f2a16fb0..a08936f3 100644 --- a/cmd/flux/kustomization_test.go +++ b/cmd/flux/kustomization_test.go @@ -22,51 +22,61 @@ package main import "testing" func TestKustomizationFromGit(t *testing.T) { + namespace := allocateNamespace("tkfg") + del, err := setupTestNamespace(namespace) + if err != nil { + t.Fatal(err) + } + t.Cleanup(del) + + tmpl := map[string]string{"ns": namespace} + cases := []struct { args string goldenFile string + tmpl map[string]string }{ { "create source git tkfg --url=https://github.com/stefanprodan/podinfo --branch=main --tag=6.0.0", "testdata/kustomization/create_source_git.golden", + nil, }, { "create kustomization tkfg --source=tkfg --path=./deploy/overlays/dev --prune=true --interval=5m --health-check=Deployment/frontend.dev --health-check=Deployment/backend.dev --health-check-timeout=3m", "testdata/kustomization/create_kustomization_from_git.golden", + nil, }, { "get kustomization tkfg", "testdata/kustomization/get_kustomization_from_git.golden", + nil, }, { "reconcile kustomization tkfg --with-source", "testdata/kustomization/reconcile_kustomization_from_git.golden", + tmpl, }, { "suspend kustomization tkfg", "testdata/kustomization/suspend_kustomization_from_git.golden", + tmpl, }, { "resume kustomization tkfg", "testdata/kustomization/resume_kustomization_from_git.golden", + tmpl, }, { "delete kustomization tkfg --silent", "testdata/kustomization/delete_kustomization_from_git.golden", + tmpl, }, } - namespace := allocateNamespace("tkfg") - del, err := setupTestNamespace(namespace) - if err != nil { - t.Fatal(err) - } - defer del() - for _, tc := range cases { cmd := cmdTestCase{ args: tc.args + " -n=" + namespace, - assert: assertGoldenTemplateFile(tc.goldenFile, map[string]string{"ns": namespace}), + assert: assertGoldenTemplateFile(tc.goldenFile, tc.tmpl), } cmd.runTestCmd(t) } diff --git a/cmd/flux/source_oci_test.go b/cmd/flux/source_oci_test.go index c0f8125c..12afa957 100644 --- a/cmd/flux/source_oci_test.go +++ b/cmd/flux/source_oci_test.go @@ -24,47 +24,56 @@ import ( ) func TestSourceOCI(t *testing.T) { + namespace := allocateNamespace("oci-test") + del, err := setupTestNamespace(namespace) + if err != nil { + t.Fatal(err) + } + t.Cleanup(del) + + tmpl := map[string]string{"ns": namespace} + cases := []struct { args string goldenFile string + tmpl map[string]string }{ { "create source oci thrfg --url=oci://ghcr.io/stefanprodan/manifests/podinfo --tag=6.1.6 --interval 10m", "testdata/oci/create_source_oci.golden", + nil, }, { "get source oci thrfg", "testdata/oci/get_oci.golden", + nil, }, { "reconcile source oci thrfg", "testdata/oci/reconcile_oci.golden", + tmpl, }, { "suspend source oci thrfg", "testdata/oci/suspend_oci.golden", + tmpl, }, { "resume source oci thrfg", "testdata/oci/resume_oci.golden", + tmpl, }, { "delete source oci thrfg --silent", "testdata/oci/delete_oci.golden", + tmpl, }, } - namespace := allocateNamespace("oci-test") - del, err := setupTestNamespace(namespace) - if err != nil { - t.Fatal(err) - } - defer del() - for _, tc := range cases { cmd := cmdTestCase{ args: tc.args + " -n=" + namespace, - assert: assertGoldenTemplateFile(tc.goldenFile, map[string]string{"ns": namespace}), + assert: assertGoldenTemplateFile(tc.goldenFile, tc.tmpl), } cmd.runTestCmd(t) }