Skip to content

Commit

Permalink
flux diff artifact: Check for an expected error using errors.Is.
Browse files Browse the repository at this point in the history
This fixes the `TestDiffArtifact` unit test.

Signed-off-by: Florian Forster <[email protected]>
  • Loading branch information
octo committed Aug 8, 2024
1 parent c3b7489 commit 65a209a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 3 additions & 1 deletion cmd/flux/diff_artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/flux/diff_artifact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
}

Expand Down
14 changes: 12 additions & 2 deletions cmd/flux/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bufio"
"bytes"
"context"
"errors"
"flag"
"fmt"
"io"
Expand All @@ -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"
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 65a209a

Please sign in to comment.