-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconfigs_test.go
More file actions
175 lines (165 loc) · 5.33 KB
/
configs_test.go
File metadata and controls
175 lines (165 loc) · 5.33 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package confluent
import (
"github.com/stretchr/testify/assert"
"io"
"net/http"
"testing"
)
func TestConfigs_GetTopicConfigs(t *testing.T) {
mock := MockHttpClient{}
mk := MockKafkaClient{}
mock.DoRequestFn = func(method string, uri string, reqBody io.Reader) (responseBody []byte, statusCode int, status string, err error) {
assert.Equal(t, http.MethodGet, method, "Expected method 'GET', got %s", method)
assert.Equal(t, "/kafka/v3/clusters/cluster-1/topics/topic-1/configs", uri)
return []byte(`
{
"kind": "KafkaTopicConfigList",
"metadata": {
"self": "http://localhost:9391/v3/clusters/cluster-1/topics/topic-1/configs",
"next": null
},
"data": [
{
"kind": "KafkaTopicConfig",
"metadata": {
"self": "http://localhost:9391/v3/clusters/cluster-1/topics/topic-1/configs/cleanup.policy",
"resource_name": "crn:///kafka=cluster-1/topic=topic-1/config=cleanup.policy"
},
"cluster_id": "cluster-1",
"topic_name": "topic-1",
"name": "cleanup.policy",
"value": "compact",
"is_default": false,
"is_read_only": false,
"is_sensitive": false,
"source": "DYNAMIC_TOPIC_CONFIG",
"synonyms": [
{
"name": "cleanup.policy",
"value": "compact",
"source": "DYNAMIC_TOPIC_CONFIG"
},
{
"name": "cleanup.policy",
"value": "delete",
"source": "DEFAULT_CONFIG"
}
]
},
{
"kind": "KafkaTopicConfig",
"metadata": {
"self": "http://localhost:9391/v3/clusters/cluster-1/topics/topic-1/configs/compression.type",
"resource_name": "crn:///kafka=cluster-1/topic=topic-1/config=compression.type"
},
"cluster_id": "cluster-1",
"topic_name": "topic-1",
"name": "compression.type",
"value": "gzip",
"is_default": false,
"is_read_only": false,
"is_sensitive": false,
"source": "DYNAMIC_TOPIC_CONFIG",
"synonyms": [
{
"name": "compression.type",
"value": "gzip",
"source": "DYNAMIC_TOPIC_CONFIG"
},
{
"name": "compression.type",
"value": "producer",
"source": "DEFAULT_CONFIG"
}
]
}
]
}
`), 200, "200 OK", nil
}
clusterAdmin, _ := mk.NewSaramaClusterAdmin()
c := NewClient(&mock, &mk, clusterAdmin)
configs, err := c.GetTopicConfigs("cluster-1", "topic-1")
if assert.NoError(t, err) {
assert.Equal(t, 2, len(configs))
assert.Equal(t, "cluster-1", configs[0].ClusterId)
}
}
func TestConfigs_GetTopicConfigsFailWithResourceNotFound(t *testing.T) {
mock := MockHttpClient{}
mk := MockKafkaClient{}
mock.DoRequestFn = func(method string, uri string, reqBody io.Reader) (responseBody []byte, statusCode int, status string, err error) {
assert.Equal(t, http.MethodGet, method, "Expected method 'GET', got %s", method)
assert.Equal(t, "/kafka/v3/clusters/cluster-1/topics/topic-1/configs", uri)
return nil, 404, "404 Not Found", nil
}
clusterAdmin, _ := mk.NewSaramaClusterAdmin()
c := NewClient(&mock, &mk, clusterAdmin)
_, err := c.GetTopicConfigs("cluster-1", "topic-1")
if assert.NotNil(t, err) {
assert.Contains(t, err.Error(), "404 Not Found")
}
}
func TestConfigs_GetTopicConfigsFailWithWrongData(t *testing.T) {
mock := MockHttpClient{}
mk := MockKafkaClient{}
mock.DoRequestFn = func(method string, uri string, reqBody io.Reader) (responseBody []byte, statusCode int, status string, err error) {
assert.Equal(t, http.MethodGet, method, "Expected method 'GET', got %s", method)
assert.Equal(t, "/kafka/v3/clusters/cluster-1/topics/topic-1/configs", uri)
return []byte("<"), 200, "200 OK", nil
}
clusterAdmin, _ := mk.NewSaramaClusterAdmin()
c := NewClient(&mock, &mk, clusterAdmin)
_, err := c.GetTopicConfigs("cluster-1", "topic-1")
if assert.NotNil(t, err) {
assert.Contains(t, err.Error(), "invalid character '<'")
}
}
func TestConfigs_UpdateTopicConfigs(t *testing.T) {
mock := MockHttpClient{}
mk := MockKafkaClient{}
mock.DoRequestFn = func(method string, uri string, reqBody io.Reader) (responseBody []byte, statusCode int, status string, err error) {
assert.Equal(t, http.MethodPost, method, "Expected method 'POST', got %s", method)
assert.Equal(t, "/kafka/v3/clusters/cluster-1/topics/topic-1/configs:alter", uri)
return nil, 204, "204 Accepted", nil
}
clusterAdmin, _ := mk.NewSaramaClusterAdmin()
c := NewClient(&mock, &mk, clusterAdmin)
mockSynonyms := []TopicConfig{
{
Name: "cleanup.policy",
Value: "DELETE",
},
{
Name: "compression.type",
Value: "gzip",
},
}
err := c.UpdateTopicConfigs("cluster-1", "topic-1", mockSynonyms)
assert.Nil(t, err)
}
func TestConfigs_UpdateTopicConfigsFailWithTopicNotFound(t *testing.T) {
mock := MockHttpClient{}
mk := MockKafkaClient{}
mock.DoRequestFn = func(method string, uri string, reqBody io.Reader) (responseBody []byte, statusCode int, status string, err error) {
assert.Equal(t, http.MethodPost, method, "Expected method 'POST', got %s", method)
assert.Equal(t, "/kafka/v3/clusters/cluster-1/topics/topic-1/configs:alter", uri)
return nil, 404, "404 Not Found", nil
}
clusterAdmin, _ := mk.NewSaramaClusterAdmin()
c := NewClient(&mock, &mk, clusterAdmin)
mockSynonyms := []TopicConfig{
{
Name: "cleanup.policy",
Value: "DELETE",
},
{
Name: "compression.type",
Value: "gzip",
},
}
err := c.UpdateTopicConfigs("cluster-1", "topic-1", mockSynonyms)
if assert.NotNil(t, err) {
assert.Contains(t, err.Error(), "404 Not Found")
}
}