forked from kedacore/keda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkafka_scaler_test.go
85 lines (75 loc) · 2.73 KB
/
kafka_scaler_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package scalers
import (
"reflect"
"testing"
)
type parseKafkaMetadataTestData struct {
metadata map[string]string
isError bool
numBrokers int
brokers []string
group string
topic string
}
// A complete valid metadata example for reference
var validMetadata = map[string]string{
"brokerList": "broker1:9092,broker2:9092",
"consumerGroup": "my-group",
"topic": "my-topic",
}
// A complete valid authParams example for sasl, with username and passwd
var validWithAuthParams = map[string]string{
"authMode": "sasl_plaintext",
"username": "admin",
"passwd": "admin",
}
// A complete valid authParams example for sasl, without username and passwd
var validWithoutAuthParams = map[string]string{}
var parseKafkaMetadataTestDataset = []parseKafkaMetadataTestData{
{map[string]string{}, true, 0, nil, "", ""},
{map[string]string{"brokerList": "foobar:9092"}, true, 1, []string{"foobar:9092"}, "", ""},
{map[string]string{"brokerList": "foo:9092,bar:9092"}, true, 2, []string{"foo:9092", "bar:9092"}, "", ""},
{map[string]string{"brokerList": "a", "consumerGroup": "my-group"}, true, 1, []string{"a"}, "my-group", ""},
}
func TestGetBrokers(t *testing.T) {
for _, testData := range parseKafkaMetadataTestDataset {
meta, err := parseKafkaMetadata(nil, testData.metadata, validWithAuthParams)
if err != nil && !testData.isError {
t.Error("Expected success but got error", err)
}
if testData.isError && err == nil {
t.Error("Expected error but got success")
}
if len(meta.brokers) != testData.numBrokers {
t.Errorf("Expected %d brokers but got %d\n", testData.numBrokers, len(meta.brokers))
}
if !reflect.DeepEqual(testData.brokers, meta.brokers) {
t.Errorf("Expected %v but got %v\n", testData.brokers, meta.brokers)
}
if meta.group != testData.group {
t.Errorf("Expected group %s but got %s\n", testData.group, meta.group)
}
if meta.topic != testData.topic {
t.Errorf("Expected topic %s but got %s\n", testData.topic, meta.topic)
}
meta, err = parseKafkaMetadata(nil, testData.metadata, validWithoutAuthParams)
if err != nil && !testData.isError {
t.Error("Expected success but got error", err)
}
if testData.isError && err == nil {
t.Error("Expected error but got success")
}
if len(meta.brokers) != testData.numBrokers {
t.Errorf("Expected %d brokers but got %d\n", testData.numBrokers, len(meta.brokers))
}
if !reflect.DeepEqual(testData.brokers, meta.brokers) {
t.Errorf("Expected %v but got %v\n", testData.brokers, meta.brokers)
}
if meta.group != testData.group {
t.Errorf("Expected group %s but got %s\n", testData.group, meta.group)
}
if meta.topic != testData.topic {
t.Errorf("Expected topic %s but got %s\n", testData.topic, meta.topic)
}
}
}