diff --git a/cmd/flux/diff_artifact.go b/cmd/flux/diff_artifact.go index 9158be04f4..88116be7d0 100644 --- a/cmd/flux/diff_artifact.go +++ b/cmd/flux/diff_artifact.go @@ -33,6 +33,8 @@ import ( "github.com/fluxcd/flux2/v2/internal/flags" ) +var ErrDiffArtifactChanged = errors.New("the remote and local artifact contents differ") + var diffArtifactCmd = &cobra.Command{ Use: "artifact", Short: "Diff Artifact", @@ -124,7 +126,7 @@ func diffArtifactCmdRun(cmd *cobra.Command, args []string) error { fmt.Print(diff) } - return fmt.Errorf("%q and %q differ", ociURL, diffArtifactArgs.path) + return fmt.Errorf("%q and %q: %w", ociURL, diffArtifactArgs.path, ErrDiffArtifactChanged) } func diffArtifact(ctx context.Context, client *oci.Client, remoteURL, localPath string) (string, error) { diff --git a/cmd/flux/diff_artifact_test.go b/cmd/flux/diff_artifact_test.go index 05e2991538..08e3c15e27 100644 --- a/cmd/flux/diff_artifact_test.go +++ b/cmd/flux/diff_artifact_test.go @@ -81,7 +81,7 @@ func TestDiffArtifact(t *testing.T) { argsTpl: "diff artifact %s --path=%s", pushFile: "./testdata/diff-artifact/deployment.yaml", diffFile: "./testdata/diff-artifact/deployment-diff.yaml", - assert: assertError("the remote artifact contents differs from the local one"), + assert: assertErrorIs(ErrDiffArtifactChanged), }, } diff --git a/cmd/flux/main_test.go b/cmd/flux/main_test.go index 5f2afe6083..a451a44a5f 100644 --- a/cmd/flux/main_test.go +++ b/cmd/flux/main_test.go @@ -20,6 +20,7 @@ import ( "bufio" "bytes" "context" + "errors" "flag" "fmt" "io" @@ -33,7 +34,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/mattn/go-shellwords" - "k8s.io/apimachinery/pkg/api/errors" + k8serrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" k8syaml "k8s.io/apimachinery/pkg/util/yaml" "k8s.io/client-go/tools/clientcmd" @@ -115,7 +116,7 @@ func (m *testEnvKubeManager) CreateObjects(clientObjects []*unstructured.Unstruc obj.SetResourceVersion(createObj.GetResourceVersion()) err = m.client.Status().Update(context.Background(), obj) // Updating status of static objects results in not found error. - if err != nil && !errors.IsNotFound(err) { + if err != nil && !k8serrors.IsNotFound(err) { return err } } @@ -272,6 +273,15 @@ func assertError(expected string) assertFunc { } } +func assertErrorIs(want error) assertFunc { + return func(_ string, got error) error { + if errors.Is(got, want) { + return nil + } + return fmt.Errorf("Expected error '%v' but got '%v'", want, got) + } +} + // Expect the command to succeed with the expected test output. func assertGoldenValue(expected string) assertFunc { return assert(