-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathadmin.lisp
390 lines (343 loc) · 16.7 KB
/
admin.lisp
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
;;; Copyright (C) 2018-2020 Sahil Kang <sahil.kang@asilaycomputing.com>
;;; Copyright 2023-2024 Google LLC
;;;
;;; This file is part of cl-rdkafka.
;;;
;;; cl-rdkafka is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; cl-rdkafka is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with cl-rdkafka. If not, see <http://www.gnu.org/licenses/>.
(in-package #:cl-user)
(defpackage #:test/high-level/admin
(:use #:cl #:1am #:test))
(in-package #:test/high-level/admin)
(defun make-sed-commmand (topic)
(format nil "sed -En 's/^\\s+topic\\s+~S\\s+with\\s+([0-9]+).*/\\1/p'" topic))
(defun make-kcat-command (bootstrap-servers topic)
(let ((sed (make-sed-commmand topic)))
(format nil "kcat -b '~A' -L -t '~A' | ~A" bootstrap-servers topic sed)))
(defun get-partitions (bootstrap-servers topic)
(parse-integer
(uiop:run-program
(make-kcat-command bootstrap-servers topic)
:force-shell t
:output 'string)))
(test create-topic-with-consumer
(with-topics ((topic "create-topic-with-consumer" t))
(let ((consumer (make-instance
'kf:consumer
:conf (list "bootstrap.servers" *bootstrap-servers*)))
(partitions 3))
(is (string= topic (kf::create-topic consumer
topic
:partitions partitions
:timeout-ms 5000)))
(sleep 2)
(is (= partitions (get-partitions *bootstrap-servers* topic))))))
(test create-topic-with-producer
(with-topics ((topic "create-topic-with-producer" t))
(let ((producer (make-instance
'kf:producer
:conf (list "bootstrap.servers" *bootstrap-servers*)))
(partitions 7))
(is (string= topic (kf::create-topic producer
topic
:partitions partitions
:timeout-ms 5000)))
(sleep 2)
(is (= partitions (get-partitions *bootstrap-servers* topic))))))
(test create-topic-validatep
(with-topics ((topic "create-topic-with-validate-only" t))
(let ((consumer (make-instance
'kf:consumer
:conf (list "bootstrap.servers" *bootstrap-servers*)))
(partitions 4))
(is (string= topic (kf::create-topic consumer
topic
:partitions partitions
:timeout-ms 5000
:validate-only-p t)))
(sleep 2)
(is (= 0 (get-partitions *bootstrap-servers* topic))))))
(test delete-topic-with-consumer
(with-topics ((topic "delete-topic-with-consumer" t))
(let ((consumer (make-instance
'kf:consumer
:conf (list "bootstrap.servers" *bootstrap-servers*)))
(partitions 7))
(is (string= topic (kf::create-topic consumer
topic
:partitions partitions
:timeout-ms 5000)))
(sleep 2)
(is (= partitions (get-partitions *bootstrap-servers* topic)))
(is (string= topic (kf::delete-topic consumer topic :timeout-ms 5000)))
(sleep 2)
(is (= 0 (get-partitions *bootstrap-servers* topic))))))
(test delete-topic-with-producer
(with-topics ((topic "delete-topic-with-producer" t))
(let ((producer (make-instance
'kf:producer
:conf (list "bootstrap.servers" *bootstrap-servers*)))
(partitions 4))
(is (string= topic (kf::create-topic producer
topic
:partitions partitions
:timeout-ms 5000)))
(sleep 2)
(is (= partitions (get-partitions *bootstrap-servers* topic)))
(is (string= topic (kf::delete-topic producer topic :timeout-ms 5000)))
(sleep 2)
(is (= 0 (get-partitions *bootstrap-servers* topic))))))
(test create-partitions-with-consumer
(with-topics ((topic "create-partitions-with-consumer" t))
(let ((consumer (make-instance
'kf:consumer
:conf (list "bootstrap.servers" *bootstrap-servers*)))
(old-partitions 7)
(new-partitions 10))
(is (string= topic (kf::create-topic consumer
topic
:partitions old-partitions)))
(sleep 2)
(is (= old-partitions (get-partitions *bootstrap-servers* topic)))
(is (= new-partitions (kf::create-partitions consumer topic new-partitions)))
(sleep 2)
(is (= new-partitions (get-partitions *bootstrap-servers* topic))))))
(test create-partitions-with-producer
(with-topics ((topic "create-partitions-with-producer" t))
(let ((producer (make-instance
'kf:producer
:conf (list "bootstrap.servers" *bootstrap-servers*)))
(old-partitions 11)
(new-partitions 20))
(is (string= topic (kf::create-topic producer
topic
:partitions old-partitions)))
(sleep 2)
(is (= old-partitions (get-partitions *bootstrap-servers* topic)))
(is (= new-partitions (kf::create-partitions producer topic new-partitions)))
(sleep 2)
(is (= new-partitions (get-partitions *bootstrap-servers* topic))))))
(test describe-topic-with-consumer
(with-topics ((topic "describe-topic-with-consumer"))
(let ((consumer (make-instance
'kf:consumer
:conf (list "bootstrap.servers" *bootstrap-servers*))))
(is (string= "CreateTime"
(cdr (assoc "message.timestamp.type"
(kf::describe-config consumer topic :topic)
:test #'string=)))))))
(test describe-topic-with-producer
(with-topics ((topic "describe-topic-with-producer"))
(let ((producer (make-instance
'kf:producer
:conf (list "bootstrap.servers" *bootstrap-servers*))))
(is (string= "CreateTime"
(cdr (assoc "message.timestamp.type"
(kf::describe-config producer topic :topic)
:test #'string=)))))))
(test describe-broker-with-consumer
(destructuring-bind (host port)
(uiop:split-string *bootstrap-servers* :separator ":")
(let* ((consumer (make-instance
'kf:consumer
:conf (list "bootstrap.servers" *bootstrap-servers*)))
(config (kf::describe-config consumer "1001" :broker)))
(is (string= host
(cdr (assoc "advertised.host.name" config :test #'string=))))
(is (string= port
(cdr (assoc "advertised.port" config :test #'string=)))))))
(test describe-broker-with-producer
(destructuring-bind (host port)
(uiop:split-string *bootstrap-servers* :separator ":")
(let* ((producer (make-instance
'kf:producer
:conf (list "bootstrap.servers" *bootstrap-servers*)))
(config (kf::describe-config producer "1001" :broker)))
(is (string= host
(cdr (assoc "advertised.host.name" config :test #'string=))))
(is (string= port
(cdr (assoc "advertised.port" config :test #'string=)))))))
(test alter-topic-with-consumer
(with-topics ((topic "alter-topic-with-consumer"))
(let* ((consumer (make-instance
'kf:consumer
:conf (list "bootstrap.servers" *bootstrap-servers*)))
(get-actual (lambda ()
(cdr (assoc "message.timestamp.type"
(kf::describe-config consumer topic :topic)
:test #'string=)))))
(is (string= "CreateTime" (funcall get-actual)))
(kf::alter-config consumer
topic
'(("message.timestamp.type" . "LogAppendTime")))
(sleep 2)
(is (string= "LogAppendTime" (funcall get-actual))))))
(test alter-topic-with-producer
(with-topics ((topic "alter-topic-with-producer"))
(let* ((producer (make-instance
'kf:producer
:conf (list "bootstrap.servers" *bootstrap-servers*)))
(get-actual (lambda ()
(cdr (assoc "message.timestamp.type"
(kf::describe-config producer topic :topic)
:test #'string=)))))
(is (string= "CreateTime" (funcall get-actual)))
(kf::alter-config producer
topic
'(("message.timestamp.type" . "LogAppendTime")))
(sleep 2)
(is (string= "LogAppendTime" (funcall get-actual))))))
(test get-topic-with-consumer
(with-topics ((topic "get-topic-with-consumer"))
(let* ((consumer (make-instance
'kf:consumer
:conf (list "bootstrap.servers" *bootstrap-servers*)))
(config (kf::get-conf consumer)))
(is (string= "cl-rdkafka"
(cdr (assoc "client.software.name" config :test #'string=))))
(let ((version (format nil "v~A-librdkafka-v~A"
(asdf:component-version
(asdf:find-system 'cl-rdkafka))
(cl-rdkafka/ll:rd-kafka-version-str))))
(is (string= version
(cdr (assoc "client.software.version" config
:test #'string=))))))))
(test get-topic-with-producer
(with-topics ((topic "get-topic-with-producer"))
(let* ((producer (make-instance
'kf:producer
:conf (list "bootstrap.servers" *bootstrap-servers*)))
(config (kf::get-conf producer)))
(is (string= "cl-rdkafka"
(cdr (assoc "client.software.name" config :test #'string=))))
(let ((version (format nil "v~A-librdkafka-v~A"
(asdf:component-version
(asdf:find-system 'cl-rdkafka))
(cl-rdkafka/ll:rd-kafka-version-str))))
(is (string= version
(cdr (assoc "client.software.version" config
:test #'string=))))))))
(test cluster-metadata-with-consumer
(with-topics ((topic "cluster-metadata-with-consumer"))
(destructuring-bind (host port)
(uiop:split-string *bootstrap-servers* :separator ":")
(let ((consumer (make-instance
'kf:consumer
:conf (list "bootstrap.servers" *bootstrap-servers*)))
(broker-name (format nil "~A/1001" *bootstrap-servers*)))
(is (equal `((:originating-broker . ((:id . 1001)
(:name . ,broker-name)))
(:broker-metadata . (((:id . 1001)
(:host . ,host)
(:port . ,(parse-integer port)))))
(:topic-metadata . (((:topic . ,topic)
(:partitions . (((:id . 0)
(:leader . 1001)
(:replicas . (1001))
(:in-sync-replicas . (1001)))))))))
(kf::cluster-metadata consumer topic)))))))
(test cluster-metadata-with-producer
(with-topics ((topic "cluster-metadata-with-producer"))
(destructuring-bind (host port)
(uiop:split-string *bootstrap-servers* :separator ":")
(let ((producer (make-instance
'kf:producer
:conf (list "bootstrap.servers" *bootstrap-servers*)))
(broker-name (format nil "~A/1001" *bootstrap-servers*)))
(is (equal `((:originating-broker . ((:id . 1001)
(:name . ,broker-name)))
(:broker-metadata . (((:id . 1001)
(:host . ,host)
(:port . ,(parse-integer port)))))
(:topic-metadata . (((:topic . ,topic)
(:partitions . (((:id . 0)
(:leader . 1001)
(:replicas . (1001))
(:in-sync-replicas . (1001)))))))))
(kf::cluster-metadata producer topic)))))))
(test group-info-with-consumer
(with-topics ((topic "group-info-with-consumer"))
(let* ((group-1 "group-info-with-consumer-group-1")
(group-2 "group-info-with-consumer-group-2")
(consumer-1 (make-instance
'kf:consumer
:conf (list "bootstrap.servers" *bootstrap-servers*
"group.id" group-1)))
(consumer-2 (make-instance
'kf:consumer
:conf (list "bootstrap.servers" *bootstrap-servers*
"group.id" group-2))))
(kf:subscribe consumer-1 (list topic))
(kf:subscribe consumer-2 (list topic))
(sleep 5)
(let ((group-info (first (kf::group-info consumer-1 group-1 :timeout-ms 10000))))
(is (string= group-1 (cdr (assoc :group group-info)))))
(let ((group-info (kf::group-info consumer-1 nil :timeout-ms 10000)))
(is (< 1 (length group-info)))
(is (find group-1
group-info
:test #'string=
:key (lambda (alist)
(cdr (assoc :group alist)))))
(is (find group-2
group-info
:test #'string=
:key (lambda (alist)
(cdr (assoc :group alist)))))))))
(test group-info-with-producer
(with-topics ((topic "group-info-with-producer"))
(let* ((group-1 "group-info-with-producer-group-1")
(group-2 "group-info-with-producer-group-2")
(consumer-1 (make-instance
'kf:consumer
:conf (list "bootstrap.servers" *bootstrap-servers*
"group.id" group-1)))
(consumer-2 (make-instance
'kf:consumer
:conf (list "bootstrap.servers" *bootstrap-servers*
"group.id" group-2)))
(producer (make-instance
'kf:producer
:conf (list "bootstrap.servers" *bootstrap-servers*))))
(kf:subscribe consumer-1 (list topic))
(kf:subscribe consumer-2 (list topic))
(sleep 5)
(let ((group-info (first (kf::group-info producer group-1 :timeout-ms 10000))))
(is (string= group-1 (cdr (assoc :group group-info)))))
(let ((group-info (kf::group-info producer nil :timeout-ms 10000)))
(is (< 1 (length group-info)))
(is (find group-1
group-info
:test #'string=
:key (lambda (alist)
(cdr (assoc :group alist)))))
(is (find group-2
group-info
:test #'string=
:key (lambda (alist)
(cdr (assoc :group alist)))))))))
(test cluster-id
(let* ((conf (list "bootstrap.servers" *bootstrap-servers*))
(consumer (make-instance 'kf:consumer :conf conf))
(producer (make-instance 'kf:producer :conf conf))
(lhs (kf::cluster-id consumer))
(rhs (kf::cluster-id producer)))
(is (string= lhs rhs))
(is (not (zerop (length lhs))))))
(test controller-id
(let* ((conf (list "bootstrap.servers" *bootstrap-servers*))
(consumer (make-instance 'kf:consumer :conf conf))
(producer (make-instance 'kf:producer :conf conf))
(lhs (kf::controller-id consumer))
(rhs (kf::controller-id producer)))
(is (= lhs rhs))))