forked from asyncapi-archived-repos/event-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkafka_test.go
105 lines (101 loc) · 3.4 KB
/
kafka_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package config
import (
"testing"
"github.com/asyncapi/event-gateway/kafka"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
func TestKafkaProxy_ProxyConfig(t *testing.T) {
tests := []struct {
name string
config *KafkaProxy
doc []byte
expectedProxyConfig func(*testing.T, *kafka.ProxyConfig) *kafka.ProxyConfig
expectedErr error
}{
{
name: "Valid config. Only one broker",
config: &KafkaProxy{
BrokerFromServer: "test",
// Also testing extra flags.
ExtraFlags: pipeSeparatedValues{Values: []string{"arg1=arg1value", "arg2=arg2value"}},
},
expectedProxyConfig: func(_ *testing.T, _ *kafka.ProxyConfig) *kafka.ProxyConfig {
return &kafka.ProxyConfig{
BrokersMapping: []string{"broker.mybrokers.org:9092,:9092"},
ExtraConfig: []string{"arg1=arg1value", "arg2=arg2value"},
}
},
doc: []byte(`testdata/simple-kafka.yaml`),
},
{
name: "Valid config. Only one broker + enable message validation",
config: &KafkaProxy{
BrokerFromServer: "test",
MessageValidation: MessageValidation{
Enabled: true,
},
},
expectedProxyConfig: func(t *testing.T, c *kafka.ProxyConfig) *kafka.ProxyConfig {
assert.Equal(t, []string{"broker.mybrokers.org:9092,:9092"}, c.BrokersMapping)
assert.NotNil(t, c.MessageHandler)
return nil
},
doc: []byte(`testdata/simple-kafka.yaml`),
},
{
name: "Valid config. Only one broker + Override listener port",
config: &KafkaProxy{
BrokerFromServer: "test",
},
expectedProxyConfig: func(t *testing.T, c *kafka.ProxyConfig) *kafka.ProxyConfig {
return &kafka.ProxyConfig{
BrokersMapping: []string{"broker.mybrokers.org:9092,:28002"},
}
},
doc: []byte(`testdata/override-port-kafka.yaml`),
},
{
name: "Valid config. All brokers + Override listener port",
config: &KafkaProxy{},
expectedProxyConfig: func(t *testing.T, c *kafka.ProxyConfig) *kafka.ProxyConfig {
return &kafka.ProxyConfig{
BrokersMapping: []string{"broker.mybrokers.org:9092,:28002"},
}
},
doc: []byte(`testdata/override-port-kafka.yaml`),
},
{
name: "Valid config. All brokers + multiple dial mapping",
config: &KafkaProxy{},
expectedProxyConfig: func(t *testing.T, c *kafka.ProxyConfig) *kafka.ProxyConfig {
return &kafka.ProxyConfig{
BrokersMapping: []string{"broker.mybrokers.org:9092,:9092"},
DialAddressMapping: []string{"0.0.0.0:28002,kafkaproxy.myapp.org:28002", "0.0.0.0:28003,kafkaproxy.myapp.org:28003"},
}
},
doc: []byte(`testdata/dial-mapping-kafka.yaml`),
},
{
name: "Invalid config. Both broker and proxy are the same",
config: &KafkaProxy{},
expectedErr: errors.New("broker and proxy can't listen to the same port on the same host. Broker is already listening at localhost:9092. Please configure a different listener port"),
doc: []byte(`testdata/invalid-same-address.yaml`),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
proxyConfig, err := test.config.ProxyConfig(test.doc, false)
if test.expectedErr != nil {
assert.EqualError(t, err, test.expectedErr.Error())
} else {
assert.NoError(t, err)
}
if test.expectedProxyConfig != nil {
if expectedConf := test.expectedProxyConfig(t, proxyConfig); expectedConf != nil {
assert.EqualValues(t, expectedConf, proxyConfig)
}
}
})
}
}