flux diff artifact: Add (and fix) unit tests.

Signed-off-by: Florian Forster <fforster@gitlab.com>
pull/4916/head
Florian Forster 4 months ago
parent 672b759f2e
commit a3fc5a92e4
No known key found for this signature in database

@ -22,6 +22,9 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
"io"
"os"
"path/filepath"
"testing" "testing"
"time" "time"
@ -65,6 +68,7 @@ func TestDiffArtifact(t *testing.T) {
argsTpl string argsTpl string
pushFile string pushFile string
diffFile string diffFile string
diffName string
assert assertFunc assert assertFunc
}{ }{
{ {
@ -75,14 +79,50 @@ func TestDiffArtifact(t *testing.T) {
diffFile: "./testdata/diff-artifact/deployment.yaml", diffFile: "./testdata/diff-artifact/deployment.yaml",
assert: assertGoldenFile("testdata/diff-artifact/success.golden"), 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", name: "should fail if there is a diff",
url: "oci://%s/podinfo:2.0.0", url: "oci://%s/podinfo:2.0.0",
argsTpl: "diff artifact %s --path=%s", argsTpl: "diff artifact %s --path=%s",
pushFile: "./testdata/diff-artifact/deployment.yaml", pushFile: "./testdata/diff-artifact/deployment.yaml",
diffFile: "./testdata/diff-artifact/deployment-diff.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() ctx := ctrl.SetupSignalHandler()
@ -99,11 +139,38 @@ func TestDiffArtifact(t *testing.T) {
t.Fatalf(fmt.Errorf("failed to push image: %w", err).Error()) 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{ cmd := cmdTestCase{
args: fmt.Sprintf(tt.argsTpl, tt.url, tt.diffFile), args: fmt.Sprintf(tt.argsTpl, tt.url, diffFile),
assert: tt.assert, assert: tt.assert,
} }
cmd.runTestCmd(t) 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
}

@ -26,6 +26,7 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"strings" "strings"
"sync/atomic" "sync/atomic"
"testing" "testing"
@ -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 type TestClusterMode int
const ( const (

@ -4,7 +4,7 @@ metadata:
labels: labels:
kustomize.toolkit.fluxcd.io/name: podinfo kustomize.toolkit.fluxcd.io/name: podinfo
kustomize.toolkit.fluxcd.io/namespace: {{ .fluxns }} kustomize.toolkit.fluxcd.io/namespace: {{ .fluxns }}
name: podinfo-diff name: podinfo
namespace: default namespace: default
spec: spec:
minReadySeconds: 3 minReadySeconds: 3

@ -71,7 +71,7 @@ spec:
timeoutSeconds: 5 timeoutSeconds: 5
resources: resources:
limits: limits:
cpu: 2000m cpu: 1000m
memory: 512Mi memory: 512Mi
requests: requests:
cpu: 100m cpu: 100m

Loading…
Cancel
Save