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 <hello@hidde.co>
pull/3592/head
Hidde Beydals 2 years ago
parent 1e67d75848
commit b263e14fa8

@ -54,7 +54,7 @@ data:
tmpFile, err := saveReaderToFile(strings.NewReader(tt.string)) tmpFile, err := saveReaderToFile(strings.NewReader(tt.string))
g.Expect(err).To(BeNil()) g.Expect(err).To(BeNil())
defer os.Remove(tmpFile) t.Cleanup(func() { _ = os.Remove(tmpFile) })
b, err := os.ReadFile(tmpFile) b, err := os.ReadFile(tmpFile)
if tt.expectErr { if tt.expectErr {

@ -171,8 +171,7 @@ spec:
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
t.Cleanup(func() { _ = os.Remove("./testdata/build-kustomization/podinfo.yaml") })
defer os.Remove("./testdata/build-kustomization/podinfo.yaml")
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {

@ -109,7 +109,9 @@ func TestDiffKustomization(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if tt.objectFile != "" { 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{ cmd := cmdTestCase{
args: tt.args + " -n " + tmpl["fluxns"], args: tt.args + " -n " + tmpl["fluxns"],

@ -8,78 +8,92 @@ import (
) )
func TestExport(t *testing.T) { 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 { cases := []struct {
name string name string
arg string arg string
goldenFile string goldenFile string
tmpl map[string]string
}{ }{
{ {
"alert-provider", "alert-provider",
"export alert-provider slack", "export alert-provider slack",
"testdata/export/provider.yaml", "testdata/export/provider.yaml",
tmpl,
}, },
{ {
"alert", "alert",
"export alert flux-system", "export alert flux-system",
"testdata/export/alert.yaml", "testdata/export/alert.yaml",
tmpl,
}, },
{ {
"image policy", "image policy",
"export image policy flux-system", "export image policy flux-system",
"testdata/export/image-policy.yaml", "testdata/export/image-policy.yaml",
tmpl,
}, },
{ {
"image repository", "image repository",
"export image repository flux-system", "export image repository flux-system",
"testdata/export/image-repo.yaml", "testdata/export/image-repo.yaml",
tmpl,
}, },
{ {
"image update", "image update",
"export image update flux-system", "export image update flux-system",
"testdata/export/image-update.yaml", "testdata/export/image-update.yaml",
tmpl,
}, },
{ {
"source git", "source git",
"export source git flux-system", "export source git flux-system",
"testdata/export/git-repo.yaml", "testdata/export/git-repo.yaml",
tmpl,
}, },
{ {
"source helm", "source helm",
"export source helm flux-system", "export source helm flux-system",
"testdata/export/helm-repo.yaml", "testdata/export/helm-repo.yaml",
tmpl,
}, },
{ {
"receiver", "receiver",
"export receiver flux-system", "export receiver flux-system",
"testdata/export/receiver.yaml", "testdata/export/receiver.yaml",
tmpl,
}, },
{ {
"kustomization", "kustomization",
"export kustomization flux-system", "export kustomization flux-system",
"testdata/export/ks.yaml", "testdata/export/ks.yaml",
tmpl,
}, },
{ {
"helmrelease", "helmrelease",
"export helmrelease flux-system", "export helmrelease flux-system",
"testdata/export/helm-release.yaml", "testdata/export/helm-release.yaml",
tmpl,
}, },
{ {
"bucket", "bucket",
"export source bucket flux-system", "export source bucket flux-system",
"testdata/export/bucket.yaml", "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 { for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
cmd := cmdTestCase{ cmd := cmdTestCase{
args: tt.arg + " -n=" + tmpl["fluxns"], args: tt.arg + " -n=" + namespace,
assert: assertGoldenTemplateFile(tt.goldenFile, tmpl), assert: assertGoldenTemplateFile(tt.goldenFile, tmpl),
} }

@ -22,51 +22,61 @@ package main
import "testing" import "testing"
func TestHelmReleaseFromGit(t *testing.T) { 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 { cases := []struct {
args string args string
goldenFile string goldenFile string
tmpl map[string]string
}{ }{
{ {
"create source git thrfg --url=https://github.com/stefanprodan/podinfo --branch=main --tag=6.0.0", "create source git thrfg --url=https://github.com/stefanprodan/podinfo --branch=main --tag=6.0.0",
"testdata/helmrelease/create_source_git.golden", "testdata/helmrelease/create_source_git.golden",
nil,
}, },
{ {
"create helmrelease thrfg --source=GitRepository/thrfg --chart=./charts/podinfo", "create helmrelease thrfg --source=GitRepository/thrfg --chart=./charts/podinfo",
"testdata/helmrelease/create_helmrelease_from_git.golden", "testdata/helmrelease/create_helmrelease_from_git.golden",
nil,
}, },
{ {
"get helmrelease thrfg", "get helmrelease thrfg",
"testdata/helmrelease/get_helmrelease_from_git.golden", "testdata/helmrelease/get_helmrelease_from_git.golden",
nil,
}, },
{ {
"reconcile helmrelease thrfg --with-source", "reconcile helmrelease thrfg --with-source",
"testdata/helmrelease/reconcile_helmrelease_from_git.golden", "testdata/helmrelease/reconcile_helmrelease_from_git.golden",
tmpl,
}, },
{ {
"suspend helmrelease thrfg", "suspend helmrelease thrfg",
"testdata/helmrelease/suspend_helmrelease_from_git.golden", "testdata/helmrelease/suspend_helmrelease_from_git.golden",
tmpl,
}, },
{ {
"resume helmrelease thrfg", "resume helmrelease thrfg",
"testdata/helmrelease/resume_helmrelease_from_git.golden", "testdata/helmrelease/resume_helmrelease_from_git.golden",
tmpl,
}, },
{ {
"delete helmrelease thrfg --silent", "delete helmrelease thrfg --silent",
"testdata/helmrelease/delete_helmrelease_from_git.golden", "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 { for _, tc := range cases {
cmd := cmdTestCase{ cmd := cmdTestCase{
args: tc.args + " -n=" + namespace, args: tc.args + " -n=" + namespace,
assert: assertGoldenTemplateFile(tc.goldenFile, map[string]string{"ns": namespace}), assert: assertGoldenTemplateFile(tc.goldenFile, tc.tmpl),
} }
cmd.runTestCmd(t) cmd.runTestCmd(t)
} }

@ -22,6 +22,13 @@ package main
import "testing" import "testing"
func TestImageScanning(t *testing.T) { func TestImageScanning(t *testing.T) {
namespace := allocateNamespace("tis")
del, err := setupTestNamespace(namespace)
if err != nil {
t.Fatal(err)
}
t.Cleanup(del)
cases := []struct { cases := []struct {
args string args string
goldenFile 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 { for _, tc := range cases {
cmd := cmdTestCase{ cmd := cmdTestCase{
args: tc.args + " -n=" + namespace, args: tc.args + " -n=" + namespace,

@ -25,9 +25,7 @@ func TestInstall(t *testing.T) {
// Given that this test uses an invalid namespace, it ensures // Given that this test uses an invalid namespace, it ensures
// to restore whatever value it had previously. // to restore whatever value it had previously.
currentNamespace := *kubeconfigArgs.Namespace currentNamespace := *kubeconfigArgs.Namespace
defer func() { t.Cleanup(func() { *kubeconfigArgs.Namespace = currentNamespace })
*kubeconfigArgs.Namespace = currentNamespace
}()
tests := []struct { tests := []struct {
name string name string

@ -22,51 +22,61 @@ package main
import "testing" import "testing"
func TestKustomizationFromGit(t *testing.T) { 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 { cases := []struct {
args string args string
goldenFile string goldenFile string
tmpl map[string]string
}{ }{
{ {
"create source git tkfg --url=https://github.com/stefanprodan/podinfo --branch=main --tag=6.0.0", "create source git tkfg --url=https://github.com/stefanprodan/podinfo --branch=main --tag=6.0.0",
"testdata/kustomization/create_source_git.golden", "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", "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", "testdata/kustomization/create_kustomization_from_git.golden",
nil,
}, },
{ {
"get kustomization tkfg", "get kustomization tkfg",
"testdata/kustomization/get_kustomization_from_git.golden", "testdata/kustomization/get_kustomization_from_git.golden",
nil,
}, },
{ {
"reconcile kustomization tkfg --with-source", "reconcile kustomization tkfg --with-source",
"testdata/kustomization/reconcile_kustomization_from_git.golden", "testdata/kustomization/reconcile_kustomization_from_git.golden",
tmpl,
}, },
{ {
"suspend kustomization tkfg", "suspend kustomization tkfg",
"testdata/kustomization/suspend_kustomization_from_git.golden", "testdata/kustomization/suspend_kustomization_from_git.golden",
tmpl,
}, },
{ {
"resume kustomization tkfg", "resume kustomization tkfg",
"testdata/kustomization/resume_kustomization_from_git.golden", "testdata/kustomization/resume_kustomization_from_git.golden",
tmpl,
}, },
{ {
"delete kustomization tkfg --silent", "delete kustomization tkfg --silent",
"testdata/kustomization/delete_kustomization_from_git.golden", "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 { for _, tc := range cases {
cmd := cmdTestCase{ cmd := cmdTestCase{
args: tc.args + " -n=" + namespace, args: tc.args + " -n=" + namespace,
assert: assertGoldenTemplateFile(tc.goldenFile, map[string]string{"ns": namespace}), assert: assertGoldenTemplateFile(tc.goldenFile, tc.tmpl),
} }
cmd.runTestCmd(t) cmd.runTestCmd(t)
} }

@ -24,47 +24,56 @@ import (
) )
func TestSourceOCI(t *testing.T) { 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 { cases := []struct {
args string args string
goldenFile string goldenFile string
tmpl map[string]string
}{ }{
{ {
"create source oci thrfg --url=oci://ghcr.io/stefanprodan/manifests/podinfo --tag=6.1.6 --interval 10m", "create source oci thrfg --url=oci://ghcr.io/stefanprodan/manifests/podinfo --tag=6.1.6 --interval 10m",
"testdata/oci/create_source_oci.golden", "testdata/oci/create_source_oci.golden",
nil,
}, },
{ {
"get source oci thrfg", "get source oci thrfg",
"testdata/oci/get_oci.golden", "testdata/oci/get_oci.golden",
nil,
}, },
{ {
"reconcile source oci thrfg", "reconcile source oci thrfg",
"testdata/oci/reconcile_oci.golden", "testdata/oci/reconcile_oci.golden",
tmpl,
}, },
{ {
"suspend source oci thrfg", "suspend source oci thrfg",
"testdata/oci/suspend_oci.golden", "testdata/oci/suspend_oci.golden",
tmpl,
}, },
{ {
"resume source oci thrfg", "resume source oci thrfg",
"testdata/oci/resume_oci.golden", "testdata/oci/resume_oci.golden",
tmpl,
}, },
{ {
"delete source oci thrfg --silent", "delete source oci thrfg --silent",
"testdata/oci/delete_oci.golden", "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 { for _, tc := range cases {
cmd := cmdTestCase{ cmd := cmdTestCase{
args: tc.args + " -n=" + namespace, args: tc.args + " -n=" + namespace,
assert: assertGoldenTemplateFile(tc.goldenFile, map[string]string{"ns": namespace}), assert: assertGoldenTemplateFile(tc.goldenFile, tc.tmpl),
} }
cmd.runTestCmd(t) cmd.runTestCmd(t)
} }

Loading…
Cancel
Save