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 (
"context"
"fmt"
"io"
"os"
"path/filepath"
"testing"
"time"
@ -65,6 +68,7 @@ func TestDiffArtifact(t *testing.T) {
argsTpl string
pushFile string
diffFile string
diffName string
assert assertFunc
}{
{
@ -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()
@ -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
}

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

@ -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

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

Loading…
Cancel
Save