Skip to content

Commit

Permalink
chore: better logs for jq expression errors (argoproj#15226)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Crenshaw <[email protected]>
Signed-off-by: jmilic1 <[email protected]>
  • Loading branch information
crenshaw-dev authored and jmilic1 committed Nov 13, 2023
1 parent a451a5b commit c5dde04
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
19 changes: 17 additions & 2 deletions test/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down
18 changes: 17 additions & 1 deletion test/testutil.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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()
}
3 changes: 3 additions & 0 deletions util/argo/normalizers/diff_normalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
20 changes: 20 additions & 0 deletions util/argo/normalizers/diff_normalizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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")
}

0 comments on commit c5dde04

Please sign in to comment.