|
| 1 | +/* |
| 2 | +Copyright 2024. |
| 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 v1 |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "k8s.io/apimachinery/pkg/runtime" |
| 25 | +) |
| 26 | + |
| 27 | +func mustRawConfigForValidation(v interface{}) *runtime.RawExtension { |
| 28 | + b, _ := json.Marshal(v) |
| 29 | + return &runtime.RawExtension{Raw: b} |
| 30 | +} |
| 31 | + |
| 32 | +func TestValidateTransformations_DebeziumUnwrap(t *testing.T) { |
| 33 | + baseSpec := DataFlowSpec{ |
| 34 | + Source: SourceSpec{ |
| 35 | + Type: "kafka", |
| 36 | + Config: mustRawConfigForValidation(KafkaSourceSpec{Brokers: []string{"broker:9092"}, Topic: "src"}), |
| 37 | + }, |
| 38 | + Sink: SinkSpec{ |
| 39 | + Type: "kafka", |
| 40 | + Config: mustRawConfigForValidation(KafkaSinkSpec{Brokers: []string{"broker:9092"}, Topic: "dst"}), |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + t.Run("valid config", func(t *testing.T) { |
| 45 | + spec := baseSpec |
| 46 | + spec.Transformations = []TransformationSpec{{ |
| 47 | + Type: "debeziumUnwrap", |
| 48 | + Config: mustRawConfigForValidation(DebeziumUnwrapTransformation{ |
| 49 | + InferDeleteFromTombstone: true, |
| 50 | + IncludeSourceInMetadata: true, |
| 51 | + SnapshotOperation: "update", |
| 52 | + }), |
| 53 | + }} |
| 54 | + errs := ValidateDataFlowSpec(&spec) |
| 55 | + if len(errs) != 0 { |
| 56 | + t.Fatalf("expected no validation errors, got %v", errs) |
| 57 | + } |
| 58 | + }) |
| 59 | + |
| 60 | + t.Run("missing config", func(t *testing.T) { |
| 61 | + spec := baseSpec |
| 62 | + spec.Transformations = []TransformationSpec{{Type: "debeziumUnwrap"}} |
| 63 | + errs := ValidateDataFlowSpec(&spec) |
| 64 | + if len(errs) == 0 { |
| 65 | + t.Fatal("expected validation error for missing config") |
| 66 | + } |
| 67 | + if !strings.Contains(errs.ToAggregate().Error(), "debeziumUnwrap transformation configuration is required") { |
| 68 | + t.Fatalf("unexpected error: %v", errs.ToAggregate()) |
| 69 | + } |
| 70 | + }) |
| 71 | + |
| 72 | + t.Run("invalid snapshot operation", func(t *testing.T) { |
| 73 | + spec := baseSpec |
| 74 | + spec.Transformations = []TransformationSpec{{ |
| 75 | + Type: "debeziumUnwrap", |
| 76 | + Config: mustRawConfigForValidation(DebeziumUnwrapTransformation{ |
| 77 | + SnapshotOperation: "noop", |
| 78 | + }), |
| 79 | + }} |
| 80 | + errs := ValidateDataFlowSpec(&spec) |
| 81 | + if len(errs) == 0 { |
| 82 | + t.Fatal("expected validation error for snapshotOperation") |
| 83 | + } |
| 84 | + if !strings.Contains(errs.ToAggregate().Error(), "Unsupported value") { |
| 85 | + t.Fatalf("unexpected error: %v", errs.ToAggregate()) |
| 86 | + } |
| 87 | + }) |
| 88 | +} |
0 commit comments