|
| 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 | + "testing" |
| 21 | + |
| 22 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 23 | +) |
| 24 | + |
| 25 | +func TestValidateKafkaSecurityProtocol(t *testing.T) { |
| 26 | + path := field.NewPath("spec").Child("source").Child("config").Child("securityProtocol") |
| 27 | + sasl := &SASLConfig{ |
| 28 | + Mechanism: "scram-sha-256", |
| 29 | + Username: "user", |
| 30 | + Password: "pass", |
| 31 | + } |
| 32 | + tls := &TLSConfig{InsecureSkipVerify: true} |
| 33 | + |
| 34 | + tests := []struct { |
| 35 | + name string |
| 36 | + protocol string |
| 37 | + tls *TLSConfig |
| 38 | + sasl *SASLConfig |
| 39 | + wantErr bool |
| 40 | + }{ |
| 41 | + {"empty protocol", "", nil, sasl, false}, |
| 42 | + {"SASL_PLAINTEXT valid", "SASL_PLAINTEXT", nil, sasl, false}, |
| 43 | + {"sasl-plaintext normalized", "sasl-plaintext", nil, sasl, false}, |
| 44 | + {"SASL_SSL valid", "SASL_SSL", tls, sasl, false}, |
| 45 | + {"SSL valid", "SSL", tls, nil, false}, |
| 46 | + {"PLAINTEXT valid", "PLAINTEXT", nil, nil, false}, |
| 47 | + {"unknown protocol", "WSS", nil, nil, true}, |
| 48 | + {"SASL_PLAINTEXT without sasl", "SASL_PLAINTEXT", nil, nil, true}, |
| 49 | + {"SASL_PLAINTEXT with tls", "SASL_PLAINTEXT", tls, sasl, true}, |
| 50 | + {"SASL_SSL without tls", "SASL_SSL", nil, sasl, true}, |
| 51 | + {"SASL_SSL without sasl", "SASL_SSL", tls, nil, true}, |
| 52 | + {"SSL without tls", "SSL", nil, nil, true}, |
| 53 | + {"SSL with sasl", "SSL", tls, sasl, true}, |
| 54 | + {"PLAINTEXT with sasl", "PLAINTEXT", nil, sasl, true}, |
| 55 | + {"PLAINTEXT with tls", "PLAINTEXT", tls, nil, true}, |
| 56 | + } |
| 57 | + |
| 58 | + for _, tt := range tests { |
| 59 | + t.Run(tt.name, func(t *testing.T) { |
| 60 | + errs := validateKafkaSecurityProtocol(tt.protocol, tt.tls, tt.sasl, path) |
| 61 | + if tt.wantErr && len(errs) == 0 { |
| 62 | + t.Fatal("expected validation error") |
| 63 | + } |
| 64 | + if !tt.wantErr && len(errs) != 0 { |
| 65 | + t.Fatalf("expected no errors, got %v", errs) |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestValidateKafkaSource_SecurityProtocol(t *testing.T) { |
| 72 | + path := field.NewPath("spec").Child("source").Child("config") |
| 73 | + spec := &KafkaSourceSpec{ |
| 74 | + Brokers: []string{"broker:9092"}, |
| 75 | + Topic: "t", |
| 76 | + SecurityProtocol: "SASL_PLAINTEXT", |
| 77 | + SASL: &SASLConfig{ |
| 78 | + Mechanism: "scram-sha-256", |
| 79 | + Username: "user", |
| 80 | + Password: "pass", |
| 81 | + }, |
| 82 | + } |
| 83 | + if errs := validateKafkaSource(spec, path); len(errs) != 0 { |
| 84 | + t.Fatalf("expected no errors, got %v", errs) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestValidateKafkaSink_SecurityProtocol(t *testing.T) { |
| 89 | + path := field.NewPath("spec").Child("sink").Child("config") |
| 90 | + spec := &KafkaSinkSpec{ |
| 91 | + Brokers: []string{"broker:9092"}, |
| 92 | + Topic: "t", |
| 93 | + SecurityProtocol: "SASL_PLAINTEXT", |
| 94 | + SASL: &SASLConfig{ |
| 95 | + Mechanism: "scram-sha-256", |
| 96 | + Username: "user", |
| 97 | + Password: "pass", |
| 98 | + }, |
| 99 | + } |
| 100 | + if errs := validateKafkaSink(spec, path); len(errs) != 0 { |
| 101 | + t.Fatalf("expected no errors, got %v", errs) |
| 102 | + } |
| 103 | +} |
0 commit comments