|
| 1 | +package outputs |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/jmespath-community/go-jmespath/pkg/binding" |
| 8 | + "github.com/kyverno/chainsaw/pkg/apis/v1alpha1" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestProcess(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + tc binding.Bindings |
| 16 | + input any |
| 17 | + outputs []v1alpha1.Output |
| 18 | + want Outputs |
| 19 | + wantErr bool |
| 20 | + }{{ |
| 21 | + name: "empty", |
| 22 | + tc: binding.NewBindings(), |
| 23 | + input: nil, |
| 24 | + outputs: nil, |
| 25 | + want: nil, |
| 26 | + wantErr: false, |
| 27 | + }, { |
| 28 | + name: "simple", |
| 29 | + tc: binding.NewBindings(), |
| 30 | + input: nil, |
| 31 | + outputs: []v1alpha1.Output{{ |
| 32 | + Binding: v1alpha1.Binding{ |
| 33 | + Name: "foo", |
| 34 | + Value: v1alpha1.Any{Value: "bar"}, |
| 35 | + }, |
| 36 | + }}, |
| 37 | + want: Outputs{ |
| 38 | + "foo": "bar", |
| 39 | + }, |
| 40 | + wantErr: false, |
| 41 | + }, { |
| 42 | + name: "match", |
| 43 | + tc: binding.NewBindings(), |
| 44 | + input: map[string]any{}, |
| 45 | + outputs: []v1alpha1.Output{{ |
| 46 | + Match: &v1alpha1.Any{ |
| 47 | + Value: map[string]any{ |
| 48 | + "bar": "baz", |
| 49 | + }, |
| 50 | + }, |
| 51 | + Binding: v1alpha1.Binding{ |
| 52 | + Name: "foo", |
| 53 | + Value: v1alpha1.Any{Value: "bar"}, |
| 54 | + }, |
| 55 | + }}, |
| 56 | + want: nil, |
| 57 | + wantErr: false, |
| 58 | + }, { |
| 59 | + name: "match error", |
| 60 | + tc: binding.NewBindings(), |
| 61 | + input: nil, |
| 62 | + outputs: []v1alpha1.Output{{ |
| 63 | + Match: &v1alpha1.Any{ |
| 64 | + Value: map[string]any{ |
| 65 | + "($bar)": "baz", |
| 66 | + }, |
| 67 | + }, |
| 68 | + Binding: v1alpha1.Binding{ |
| 69 | + Name: "foo", |
| 70 | + Value: v1alpha1.Any{Value: "bar"}, |
| 71 | + }, |
| 72 | + }}, |
| 73 | + want: nil, |
| 74 | + wantErr: true, |
| 75 | + }, { |
| 76 | + name: "error", |
| 77 | + tc: binding.NewBindings(), |
| 78 | + input: nil, |
| 79 | + outputs: []v1alpha1.Output{{ |
| 80 | + Binding: v1alpha1.Binding{ |
| 81 | + Name: "($foo)", |
| 82 | + Value: v1alpha1.Any{Value: "bar"}, |
| 83 | + }, |
| 84 | + }}, |
| 85 | + want: nil, |
| 86 | + wantErr: true, |
| 87 | + }} |
| 88 | + for _, tt := range tests { |
| 89 | + t.Run(tt.name, func(t *testing.T) { |
| 90 | + got, err := Process(context.TODO(), tt.tc, tt.input, tt.outputs...) |
| 91 | + if tt.wantErr { |
| 92 | + assert.Error(t, err) |
| 93 | + } else { |
| 94 | + assert.NoError(t, err) |
| 95 | + } |
| 96 | + assert.Equal(t, tt.want, got) |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
0 commit comments