Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: only provide template values when used #3592

Merged
merged 3 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/flux/build_artifact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions cmd/flux/build_kustomization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ spec:
if err != nil {
t.Fatal(err)
}

Copy link
Member Author

@hiddeco hiddeco Feb 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is more here that could be done, as using ./testdata/ for something temporary seems wrong.

I think we should instead use t.TempDir() as a target for the file that is been written. Which would prevent having to think about garbage collecting it later on.

This is however beyond the time I have available now, as I was merely trying to fix things I ran into while working on #3587.

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) {
Expand Down
4 changes: 3 additions & 1 deletion cmd/flux/diff_kustomization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
28 changes: 21 additions & 7 deletions cmd/flux/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand Down
26 changes: 18 additions & 8 deletions cmd/flux/helmrelease_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,51 +22,61 @@ package main
import "testing"

func TestHelmReleaseFromGit(t *testing.T) {
namespace := allocateNamespace("thrfg")
del, err := execSetupTestNamespace(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)
}
Expand Down
14 changes: 7 additions & 7 deletions cmd/flux/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ package main
import "testing"

func TestImageScanning(t *testing.T) {
namespace := allocateNamespace("tis")
del, err := execSetupTestNamespace(namespace)
if err != nil {
t.Fatal(err)
}
t.Cleanup(del)

cases := []struct {
args string
goldenFile string
Expand All @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions cmd/flux/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 18 additions & 8 deletions cmd/flux/kustomization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,51 +22,61 @@ package main
import "testing"

func TestKustomizationFromGit(t *testing.T) {
namespace := allocateNamespace("tkfg")
del, err := execSetupTestNamespace(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)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/flux/main_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestMain(m *testing.M) {
os.Exit(code)
}

func setupTestNamespace(namespace string) (func(), error) {
func execSetupTestNamespace(namespace string) (func(), error) {
kubectlArgs := []string{"create", "namespace", namespace}
_, err := utils.ExecKubectlCommand(context.TODO(), utils.ModeStderrOS, *kubeconfigArgs.KubeConfig, *kubeconfigArgs.Context, kubectlArgs...)
if err != nil {
Expand Down
25 changes: 17 additions & 8 deletions cmd/flux/source_oci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,47 +24,56 @@ import (
)

func TestSourceOCI(t *testing.T) {
namespace := allocateNamespace("oci-test")
del, err := execSetupTestNamespace(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)
}
Expand Down
Loading