Skip to content

Commit 65a209a

Browse files
committed
flux diff artifact: Check for an expected error using errors.Is.
This fixes the `TestDiffArtifact` unit test. Signed-off-by: Florian Forster <[email protected]>
1 parent c3b7489 commit 65a209a

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

cmd/flux/diff_artifact.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import (
3333
"github.com/fluxcd/flux2/v2/internal/flags"
3434
)
3535

36+
var ErrDiffArtifactChanged = errors.New("the remote and local artifact contents differ")
37+
3638
var diffArtifactCmd = &cobra.Command{
3739
Use: "artifact",
3840
Short: "Diff Artifact",
@@ -124,7 +126,7 @@ func diffArtifactCmdRun(cmd *cobra.Command, args []string) error {
124126
fmt.Print(diff)
125127
}
126128

127-
return fmt.Errorf("%q and %q differ", ociURL, diffArtifactArgs.path)
129+
return fmt.Errorf("%q and %q: %w", ociURL, diffArtifactArgs.path, ErrDiffArtifactChanged)
128130
}
129131

130132
func diffArtifact(ctx context.Context, client *oci.Client, remoteURL, localPath string) (string, error) {

cmd/flux/diff_artifact_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func TestDiffArtifact(t *testing.T) {
8181
argsTpl: "diff artifact %s --path=%s",
8282
pushFile: "./testdata/diff-artifact/deployment.yaml",
8383
diffFile: "./testdata/diff-artifact/deployment-diff.yaml",
84-
assert: assertError("the remote artifact contents differs from the local one"),
84+
assert: assertErrorIs(ErrDiffArtifactChanged),
8585
},
8686
}
8787

cmd/flux/main_test.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bufio"
2121
"bytes"
2222
"context"
23+
"errors"
2324
"flag"
2425
"fmt"
2526
"io"
@@ -33,7 +34,7 @@ import (
3334

3435
"github.com/google/go-cmp/cmp"
3536
"github.com/mattn/go-shellwords"
36-
"k8s.io/apimachinery/pkg/api/errors"
37+
k8serrors "k8s.io/apimachinery/pkg/api/errors"
3738
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3839
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
3940
"k8s.io/client-go/tools/clientcmd"
@@ -115,7 +116,7 @@ func (m *testEnvKubeManager) CreateObjects(clientObjects []*unstructured.Unstruc
115116
obj.SetResourceVersion(createObj.GetResourceVersion())
116117
err = m.client.Status().Update(context.Background(), obj)
117118
// Updating status of static objects results in not found error.
118-
if err != nil && !errors.IsNotFound(err) {
119+
if err != nil && !k8serrors.IsNotFound(err) {
119120
return err
120121
}
121122
}
@@ -272,6 +273,15 @@ func assertError(expected string) assertFunc {
272273
}
273274
}
274275

276+
func assertErrorIs(want error) assertFunc {
277+
return func(_ string, got error) error {
278+
if errors.Is(got, want) {
279+
return nil
280+
}
281+
return fmt.Errorf("Expected error '%v' but got '%v'", want, got)
282+
}
283+
}
284+
275285
// Expect the command to succeed with the expected test output.
276286
func assertGoldenValue(expected string) assertFunc {
277287
return assert(

0 commit comments

Comments
 (0)