flux diff artifact: Check for an expected error using `errors.Is`.

This fixes the `TestDiffArtifact` unit test.

Signed-off-by: Florian Forster <fforster@gitlab.com>
pull/4916/head
Florian Forster 10 months ago
parent 72a948e8a9
commit 2c4194e0a5
No known key found for this signature in database

@ -33,6 +33,8 @@ import (
"github.com/fluxcd/flux2/v2/internal/flags" "github.com/fluxcd/flux2/v2/internal/flags"
) )
var ErrDiffArtifactChanged = errors.New("the remote and local artifact contents differ")
var diffArtifactCmd = &cobra.Command{ var diffArtifactCmd = &cobra.Command{
Use: "artifact", Use: "artifact",
Short: "Diff Artifact", Short: "Diff Artifact",
@ -124,7 +126,7 @@ func diffArtifactCmdRun(cmd *cobra.Command, args []string) error {
fmt.Print(diff) 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) { func diffArtifact(ctx context.Context, client *oci.Client, remoteURL, localPath string) (string, error) {

@ -81,7 +81,7 @@ func TestDiffArtifact(t *testing.T) {
argsTpl: "diff artifact %s --path=%s", argsTpl: "diff artifact %s --path=%s",
pushFile: "./testdata/diff-artifact/deployment.yaml", pushFile: "./testdata/diff-artifact/deployment.yaml",
diffFile: "./testdata/diff-artifact/deployment-diff.yaml", diffFile: "./testdata/diff-artifact/deployment-diff.yaml",
assert: assertError("the remote artifact contents differs from the local one"), assert: assertErrorIs(ErrDiffArtifactChanged),
}, },
} }

@ -20,6 +20,7 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"context" "context"
"errors"
"flag" "flag"
"fmt" "fmt"
"io" "io"
@ -33,7 +34,7 @@ import (
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"github.com/mattn/go-shellwords" "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" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8syaml "k8s.io/apimachinery/pkg/util/yaml" k8syaml "k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/clientcmd"
@ -115,7 +116,7 @@ func (m *testEnvKubeManager) CreateObjects(clientObjects []*unstructured.Unstruc
obj.SetResourceVersion(createObj.GetResourceVersion()) obj.SetResourceVersion(createObj.GetResourceVersion())
err = m.client.Status().Update(context.Background(), obj) err = m.client.Status().Update(context.Background(), obj)
// Updating status of static objects results in not found error. // Updating status of static objects results in not found error.
if err != nil && !errors.IsNotFound(err) { if err != nil && !k8serrors.IsNotFound(err) {
return 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. // Expect the command to succeed with the expected test output.
func assertGoldenValue(expected string) assertFunc { func assertGoldenValue(expected string) assertFunc {
return assert( return assert(

Loading…
Cancel
Save