|
| 1 | +/* |
| 2 | +Copyright 2025 The KCP Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package transformer |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/kcp-dev/api-syncagent/internal/test/diff" |
| 23 | + syncagentv1alpha1 "github.com/kcp-dev/api-syncagent/sdk/apis/syncagent/v1alpha1" |
| 24 | + "github.com/kcp-dev/api-syncagent/test/utils" |
| 25 | + |
| 26 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 27 | +) |
| 28 | + |
| 29 | +func TestCEL(t *testing.T) { |
| 30 | + commonInputObject := utils.YAMLToUnstructured(t, ` |
| 31 | +apiVersion: kcp.example.com/v1 |
| 32 | +kind: CronTab |
| 33 | +metadata: |
| 34 | + namespace: default |
| 35 | + name: my-crontab |
| 36 | +spec: |
| 37 | + cronSpec: '* * *' |
| 38 | + image: ubuntu:latest |
| 39 | +`) |
| 40 | + |
| 41 | + testcases := []struct { |
| 42 | + name string |
| 43 | + inputData *unstructured.Unstructured |
| 44 | + otherObj *unstructured.Unstructured |
| 45 | + mutation syncagentv1alpha1.ResourceCELMutation |
| 46 | + expected *unstructured.Unstructured |
| 47 | + }{ |
| 48 | + { |
| 49 | + name: "replace value at path with a fixed value of the same type", |
| 50 | + inputData: commonInputObject, |
| 51 | + mutation: syncagentv1alpha1.ResourceCELMutation{ |
| 52 | + Path: "spec.cronSpec", |
| 53 | + Expression: `"hei verden"`, |
| 54 | + }, |
| 55 | + expected: utils.YAMLToUnstructured(t, ` |
| 56 | +apiVersion: kcp.example.com/v1 |
| 57 | +kind: CronTab |
| 58 | +metadata: |
| 59 | + namespace: default |
| 60 | + name: my-crontab |
| 61 | +spec: |
| 62 | + cronSpec: "hei verden" |
| 63 | + image: ubuntu:latest |
| 64 | +`), |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "replace value at path with a fixed value of other type", |
| 68 | + inputData: commonInputObject, |
| 69 | + mutation: syncagentv1alpha1.ResourceCELMutation{ |
| 70 | + Path: "spec.cronSpec", |
| 71 | + Expression: `42`, |
| 72 | + }, |
| 73 | + expected: utils.YAMLToUnstructured(t, ` |
| 74 | +apiVersion: kcp.example.com/v1 |
| 75 | +kind: CronTab |
| 76 | +metadata: |
| 77 | + namespace: default |
| 78 | + name: my-crontab |
| 79 | +spec: |
| 80 | + cronSpec: 42 |
| 81 | + image: ubuntu:latest |
| 82 | +`), |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "access the current value at the path", |
| 86 | + inputData: commonInputObject, |
| 87 | + mutation: syncagentv1alpha1.ResourceCELMutation{ |
| 88 | + Path: "spec.cronSpec", |
| 89 | + Expression: `value + "foo"`, |
| 90 | + }, |
| 91 | + expected: utils.YAMLToUnstructured(t, ` |
| 92 | +apiVersion: kcp.example.com/v1 |
| 93 | +kind: CronTab |
| 94 | +metadata: |
| 95 | + namespace: default |
| 96 | + name: my-crontab |
| 97 | +spec: |
| 98 | + cronSpec: '* * *foo' |
| 99 | + image: ubuntu:latest |
| 100 | +`), |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "access value from the object from the other side", |
| 104 | + inputData: commonInputObject, |
| 105 | + otherObj: commonInputObject, |
| 106 | + mutation: syncagentv1alpha1.ResourceCELMutation{ |
| 107 | + Path: "spec.cronSpec", |
| 108 | + Expression: `other.spec.image`, |
| 109 | + }, |
| 110 | + expected: utils.YAMLToUnstructured(t, ` |
| 111 | +apiVersion: kcp.example.com/v1 |
| 112 | +kind: CronTab |
| 113 | +metadata: |
| 114 | + namespace: default |
| 115 | + name: my-crontab |
| 116 | +spec: |
| 117 | + cronSpec: ubuntu:latest |
| 118 | + image: ubuntu:latest |
| 119 | +`), |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + for _, testcase := range testcases { |
| 124 | + t.Run(testcase.name, func(t *testing.T) { |
| 125 | + transformer, err := NewCEL(&testcase.mutation) |
| 126 | + if err != nil { |
| 127 | + t.Fatalf("Failed to create transformer: %v", err) |
| 128 | + } |
| 129 | + |
| 130 | + transformed, err := transformer.Apply(testcase.inputData, testcase.otherObj) |
| 131 | + if err != nil { |
| 132 | + t.Fatalf("Failed to transform: %v", err) |
| 133 | + } |
| 134 | + |
| 135 | + if changes := diff.ObjectDiff(testcase.expected, transformed); changes != "" { |
| 136 | + t.Errorf("Did not get expected object:\n\n%s", changes) |
| 137 | + } |
| 138 | + }) |
| 139 | + } |
| 140 | +} |
0 commit comments