diff --git a/test/testdata.go b/test/testdata.go index b9b3a65f9ac47..2c5c6178317e3 100644 --- a/test/testdata.go +++ b/test/testdata.go @@ -4,9 +4,8 @@ import ( "context" "github.com/alicebob/miniredis/v2" - "github.com/redis/go-redis/v9" - "github.com/argoproj/gitops-engine/pkg/utils/testing" + "github.com/redis/go-redis/v9" apiv1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -86,6 +85,22 @@ func NewDeployment() *unstructured.Unstructured { return testing.Unstructured(DeploymentManifest) } +var ConfigMapManifest = ` +{ + "apiVersion": "v1", + "kind": "ConfigMap", + "metadata": { + "name": "my-configmap", + }, + "data": { + "config.yaml": "auth: token\nconfig:field" + } +}` + +func NewConfigMap() *unstructured.Unstructured { + return testing.Unstructured(ConfigMapManifest) +} + func NewFakeConfigMap() *apiv1.ConfigMap { cm := apiv1.ConfigMap{ TypeMeta: metav1.TypeMeta{ diff --git a/test/testutil.go b/test/testutil.go index 268d739d7b9f4..34264772fa54f 100644 --- a/test/testutil.go +++ b/test/testutil.go @@ -1,15 +1,16 @@ package test import ( + "bytes" "context" "encoding/json" "fmt" - "log" "net" "os" "testing" "time" + log "github.com/sirupsen/logrus" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/client-go/tools/cache" "sigs.k8s.io/yaml" @@ -107,3 +108,18 @@ func GetTestDir(t *testing.T) string { } return cwd } + +// CaptureLogEntries captures log entries generated by the logger and return it as string +func CaptureLogEntries(run func()) string { + f := log.StandardLogger().Formatter + log.SetFormatter(&log.TextFormatter{DisableColors: true}) + defer log.SetFormatter(f) + output := bytes.NewBuffer(nil) + log.SetOutput(output) + log.SetLevel(log.DebugLevel) + defer log.SetOutput(os.Stdout) + + run() + + return output.String() +} diff --git a/util/argo/normalizers/diff_normalizer.go b/util/argo/normalizers/diff_normalizer.go index 6eae329d186d8..af2d69fb2488a 100644 --- a/util/argo/normalizers/diff_normalizer.go +++ b/util/argo/normalizers/diff_normalizer.go @@ -72,6 +72,9 @@ func (np *jqNormalizerPatch) Apply(data []byte) ([]byte, error) { if !ok { return nil, fmt.Errorf("JQ patch did not return any data") } + if err, ok = first.(error); ok { + return nil, fmt.Errorf("JQ patch returned error: %w", err) + } _, ok = iter.Next() if ok { return nil, fmt.Errorf("JQ patch returned multiple objects") diff --git a/util/argo/normalizers/diff_normalizer_test.go b/util/argo/normalizers/diff_normalizer_test.go index 99f0ec3ff9db3..1b8c2bcdcebca 100644 --- a/util/argo/normalizers/diff_normalizer_test.go +++ b/util/argo/normalizers/diff_normalizer_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "sigs.k8s.io/yaml" @@ -252,3 +253,22 @@ func TestNormalizeExpectedErrorAreSilenced(t *testing.T) { assert.True(t, shouldLogError(fmt.Errorf("An error that should not be ignored"))) } + +func TestJQPathExpressionReturnsHelpfulError(t *testing.T) { + normalizer, err := NewIgnoreNormalizer([]v1alpha1.ResourceIgnoreDifferences{{ + Kind: "ConfigMap", + // This is a really wild expression, but it does trigger the desired error. + JQPathExpressions: []string{`.nothing) | .data["config.yaml"] |= (fromjson | del(.auth) | tojson`}, + }}, nil) + + assert.NoError(t, err) + + configMap := test.NewConfigMap() + require.NoError(t, err) + + out := test.CaptureLogEntries(func() { + err = normalizer.Normalize(configMap) + require.NoError(t, err) + }) + assert.Contains(t, out, "fromjson cannot be applied") +}