Skip to content

Commit

Permalink
flux diff artifact: Add (and fix) unit tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Forster <[email protected]>
  • Loading branch information
octo committed Sep 23, 2024
1 parent 672b759 commit a3fc5a9
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 4 deletions.
71 changes: 69 additions & 2 deletions cmd/flux/diff_artifact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ package main
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -65,6 +68,7 @@ func TestDiffArtifact(t *testing.T) {
argsTpl string
pushFile string
diffFile string
diffName string
assert assertFunc
}{
{
Expand All @@ -75,14 +79,50 @@ func TestDiffArtifact(t *testing.T) {
diffFile: "./testdata/diff-artifact/deployment.yaml",
assert: assertGoldenFile("testdata/diff-artifact/success.golden"),
},
{
name: "create unified diff output by default",
url: "oci://%s/podinfo:2.0.0",
argsTpl: "diff artifact %s --path=%s",
pushFile: "./testdata/diff-artifact/deployment.yaml",
diffFile: "./testdata/diff-artifact/deployment-diff.yaml",
diffName: "deployment.yaml",
assert: assert(
assertErrorIs(ErrDiffArtifactChanged),
assertRegexp(`(?m)^- cpu: 1000m$`),
assertRegexp(`(?m)^\+ cpu: 2000m$`),
),
},
{
name: "should fail if there is a diff",
url: "oci://%s/podinfo:2.0.0",
argsTpl: "diff artifact %s --path=%s",
pushFile: "./testdata/diff-artifact/deployment.yaml",
diffFile: "./testdata/diff-artifact/deployment-diff.yaml",
assert: assertErrorIs(ErrDiffArtifactChanged),
diffName: "only-local.yaml",
assert: assert(
assertErrorIs(ErrDiffArtifactChanged),
assertRegexp(`(?m)^Only in [^:]+: deployment.yaml$`),
assertRegexp(`(?m)^Only in [^:]+: only-local.yaml$`),
),
},
{
name: "semantic diff using dyff",
url: "oci://%s/podinfo:2.0.0",
argsTpl: "diff artifact %s --path=%s --differ=dyff",
pushFile: "./testdata/diff-artifact/deployment.yaml",
diffFile: "./testdata/diff-artifact/deployment-diff.yaml",
diffName: "deployment.yaml",
assert: assert(
assertErrorIs(ErrDiffArtifactChanged),
assertRegexp(`(?m)^spec.template.spec.containers.podinfod.resources.limits.cpu$`),
assertRegexp(`(?m)^ ± value change$`),
assertRegexp(`(?m)^ - 1000m$`),
assertRegexp(`(?m)^ \+ 2000m$`),
),
},
// Attention: tests do not spawn a new process when executing commands.
// That means that the --differ flag remains set to "dyff" for
// subsequent tests.
}

ctx := ctrl.SetupSignalHandler()
Expand All @@ -99,11 +139,38 @@ func TestDiffArtifact(t *testing.T) {
t.Fatalf(fmt.Errorf("failed to push image: %w", err).Error())
}

diffFile := tt.diffFile
if tt.diffName != "" {
diffFile = makeTempFile(t, tt.diffFile, tt.diffName)
}

cmd := cmdTestCase{
args: fmt.Sprintf(tt.argsTpl, tt.url, tt.diffFile),
args: fmt.Sprintf(tt.argsTpl, tt.url, diffFile),
assert: tt.assert,
}
cmd.runTestCmd(t)
})
}
}

func makeTempFile(t *testing.T, source, basename string) string {
path := filepath.Join(t.TempDir(), basename)
out, err := os.Create(path)
if err != nil {
t.Fatal(err)
}
defer out.Close()

in, err := os.Open(source)
if err != nil {
t.Fatal(err)
}
defer in.Close()

_, err = io.Copy(out, in)
if err != nil {
t.Fatal(err)
}

return path
}
12 changes: 12 additions & 0 deletions cmd/flux/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"io"
"os"
"path/filepath"
"regexp"
"strings"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -338,6 +339,17 @@ func assertGoldenTemplateFile(goldenFile string, templateValues map[string]strin
})
}

func assertRegexp(expected string) assertFunc {
re := regexp.MustCompile(expected)

return func(output string, _ error) error {
if !re.MatchString(output) {
return fmt.Errorf("Output does not match regular expression:\nOutput:\n%s\n\nRegular expression:\n%s", output, expected)
}
return nil
}
}

type TestClusterMode int

const (
Expand Down
2 changes: 1 addition & 1 deletion cmd/flux/testdata/diff-artifact/deployment-diff.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ metadata:
labels:
kustomize.toolkit.fluxcd.io/name: podinfo
kustomize.toolkit.fluxcd.io/namespace: {{ .fluxns }}
name: podinfo-diff
name: podinfo
namespace: default
spec:
minReadySeconds: 3
Expand Down
2 changes: 1 addition & 1 deletion cmd/flux/testdata/diff-artifact/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ spec:
timeoutSeconds: 5
resources:
limits:
cpu: 2000m
cpu: 1000m
memory: 512Mi
requests:
cpu: 100m
Expand Down

0 comments on commit a3fc5a9

Please sign in to comment.