Skip to content

Commit a3fc5a9

Browse files
committed
flux diff artifact: Add (and fix) unit tests.
Signed-off-by: Florian Forster <[email protected]>
1 parent 672b759 commit a3fc5a9

File tree

4 files changed

+83
-4
lines changed

4 files changed

+83
-4
lines changed

cmd/flux/diff_artifact_test.go

+69-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ package main
2222
import (
2323
"context"
2424
"fmt"
25+
"io"
26+
"os"
27+
"path/filepath"
2528
"testing"
2629
"time"
2730

@@ -65,6 +68,7 @@ func TestDiffArtifact(t *testing.T) {
6568
argsTpl string
6669
pushFile string
6770
diffFile string
71+
diffName string
6872
assert assertFunc
6973
}{
7074
{
@@ -75,14 +79,50 @@ func TestDiffArtifact(t *testing.T) {
7579
diffFile: "./testdata/diff-artifact/deployment.yaml",
7680
assert: assertGoldenFile("testdata/diff-artifact/success.golden"),
7781
},
82+
{
83+
name: "create unified diff output by default",
84+
url: "oci://%s/podinfo:2.0.0",
85+
argsTpl: "diff artifact %s --path=%s",
86+
pushFile: "./testdata/diff-artifact/deployment.yaml",
87+
diffFile: "./testdata/diff-artifact/deployment-diff.yaml",
88+
diffName: "deployment.yaml",
89+
assert: assert(
90+
assertErrorIs(ErrDiffArtifactChanged),
91+
assertRegexp(`(?m)^- cpu: 1000m$`),
92+
assertRegexp(`(?m)^\+ cpu: 2000m$`),
93+
),
94+
},
7895
{
7996
name: "should fail if there is a diff",
8097
url: "oci://%s/podinfo:2.0.0",
8198
argsTpl: "diff artifact %s --path=%s",
8299
pushFile: "./testdata/diff-artifact/deployment.yaml",
83100
diffFile: "./testdata/diff-artifact/deployment-diff.yaml",
84-
assert: assertErrorIs(ErrDiffArtifactChanged),
101+
diffName: "only-local.yaml",
102+
assert: assert(
103+
assertErrorIs(ErrDiffArtifactChanged),
104+
assertRegexp(`(?m)^Only in [^:]+: deployment.yaml$`),
105+
assertRegexp(`(?m)^Only in [^:]+: only-local.yaml$`),
106+
),
85107
},
108+
{
109+
name: "semantic diff using dyff",
110+
url: "oci://%s/podinfo:2.0.0",
111+
argsTpl: "diff artifact %s --path=%s --differ=dyff",
112+
pushFile: "./testdata/diff-artifact/deployment.yaml",
113+
diffFile: "./testdata/diff-artifact/deployment-diff.yaml",
114+
diffName: "deployment.yaml",
115+
assert: assert(
116+
assertErrorIs(ErrDiffArtifactChanged),
117+
assertRegexp(`(?m)^spec.template.spec.containers.podinfod.resources.limits.cpu$`),
118+
assertRegexp(`(?m)^ ± value change$`),
119+
assertRegexp(`(?m)^ - 1000m$`),
120+
assertRegexp(`(?m)^ \+ 2000m$`),
121+
),
122+
},
123+
// Attention: tests do not spawn a new process when executing commands.
124+
// That means that the --differ flag remains set to "dyff" for
125+
// subsequent tests.
86126
}
87127

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

142+
diffFile := tt.diffFile
143+
if tt.diffName != "" {
144+
diffFile = makeTempFile(t, tt.diffFile, tt.diffName)
145+
}
146+
102147
cmd := cmdTestCase{
103-
args: fmt.Sprintf(tt.argsTpl, tt.url, tt.diffFile),
148+
args: fmt.Sprintf(tt.argsTpl, tt.url, diffFile),
104149
assert: tt.assert,
105150
}
106151
cmd.runTestCmd(t)
107152
})
108153
}
109154
}
155+
156+
func makeTempFile(t *testing.T, source, basename string) string {
157+
path := filepath.Join(t.TempDir(), basename)
158+
out, err := os.Create(path)
159+
if err != nil {
160+
t.Fatal(err)
161+
}
162+
defer out.Close()
163+
164+
in, err := os.Open(source)
165+
if err != nil {
166+
t.Fatal(err)
167+
}
168+
defer in.Close()
169+
170+
_, err = io.Copy(out, in)
171+
if err != nil {
172+
t.Fatal(err)
173+
}
174+
175+
return path
176+
}

cmd/flux/main_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"io"
2727
"os"
2828
"path/filepath"
29+
"regexp"
2930
"strings"
3031
"sync/atomic"
3132
"testing"
@@ -338,6 +339,17 @@ func assertGoldenTemplateFile(goldenFile string, templateValues map[string]strin
338339
})
339340
}
340341

342+
func assertRegexp(expected string) assertFunc {
343+
re := regexp.MustCompile(expected)
344+
345+
return func(output string, _ error) error {
346+
if !re.MatchString(output) {
347+
return fmt.Errorf("Output does not match regular expression:\nOutput:\n%s\n\nRegular expression:\n%s", output, expected)
348+
}
349+
return nil
350+
}
351+
}
352+
341353
type TestClusterMode int
342354

343355
const (

cmd/flux/testdata/diff-artifact/deployment-diff.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ metadata:
44
labels:
55
kustomize.toolkit.fluxcd.io/name: podinfo
66
kustomize.toolkit.fluxcd.io/namespace: {{ .fluxns }}
7-
name: podinfo-diff
7+
name: podinfo
88
namespace: default
99
spec:
1010
minReadySeconds: 3

cmd/flux/testdata/diff-artifact/deployment.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ spec:
7171
timeoutSeconds: 5
7272
resources:
7373
limits:
74-
cpu: 2000m
74+
cpu: 1000m
7575
memory: 512Mi
7676
requests:
7777
cpu: 100m

0 commit comments

Comments
 (0)