Debian Codegraph @

ansible_collections/community/clickhouse/tests/unit/plugins/modules/test_clickhouse_quota.py

raw
1 from __future__ import absolute_import, division, print_function
2
3 __metaclass__ = type
4 import pytest
5 from ansible_collections.community.clickhouse.plugins.modules.clickhouse_quota import (
6 _APPLY_TO_REGEX,
7 _CREATE_QUOTA_REGEX,
8 _LIMITS_REGEX,
9 _VALID_NAME_REGEX,
10 ClickHouseQuota,
11 )
12 from ansible_collections.community.clickhouse.plugins.modules.clickhouse_quota import (
13 _DEFAULT_PARAMS as DEFAULT_NORMALIZE_PARAMS,
14 )
15
16
17 @pytest.mark.parametrize(
18 argnames="name,is_valid",
19 argvalues=[
20 ("", False),
21 ("test_quota", True),
22 ("test quota", True),
23 ("test-quota", True),
24 ("test.quota", True),
25 ("tést quota", True),
26 ("'test quota'", False),
27 ('"test quota"', False),
28 ("`test quota`", False),
29 ("test;quota", False),
30 ("test\0quota", False),
31 ],
32 )
33 def test_valid_name_regex(name, is_valid):
34 match = _VALID_NAME_REGEX.match(name)
35 if is_valid:
36 assert match is not None
37 else:
38 assert match is None
39
40
41 @pytest.mark.parametrize(
42 argnames="search",
43 argvalues=[
44 "",
45 "CREATE NOT_QUOTA foo",
46 ],
47 )
48 def test_negative_create_quota_regex(search):
49 match = _CREATE_QUOTA_REGEX.match(search)
50 assert match is None
51
52
53 DEFAULT_CREATE_QUOTA_GROUPS = dict(
54 name=None,
55 cluster=None,
56 keyed_by=None,
57 )
58
59
60 @pytest.mark.parametrize(
61 argnames="search,expected",
62 argvalues=[
63 ("CREATE QUOTA test_quota", dict(name="test_quota")),
64 ("CREATE QUOTA `test_quota`", dict(name="`test_quota`")),
65 ("CREATE QUOTA `test quota`", dict(name="`test quota`")),
66 ("CREATE QUOTA `tést_quota`", dict(name="`tést_quota`")),
67 ("CREATE QUOTA `tést quota`", dict(name="`tést quota`")),
68 (
69 "CREATE QUOTA test_quota ON CLUSTER test_cluster",
70 dict(name="test_quota", cluster="test_cluster"),
71 ),
72 (
73 "CREATE QUOTA test_quota ON CLUSTER `test cluster`",
74 dict(name="test_quota", cluster="`test cluster`"),
75 ),
76 (
77 "CREATE QUOTA test_quota ON CLUSTER `tést_cluster`",
78 dict(name="test_quota", cluster="`tést_cluster`"),
79 ),
80 (
81 "CREATE QUOTA test_quota ON CLUSTER `tést cluster`",
82 dict(name="test_quota", cluster="`tést cluster`"),
83 ),
84 (
85 "CREATE QUOTA `tést quota` ON CLUSTER `tést cluster`",
86 dict(name="`tést quota`", cluster="`tést cluster`"),
87 ),
88 (
89 "CREATE QUOTA test_quota KEYED BY user_name",
90 dict(name="test_quota", keyed_by="user_name"),
91 ),
92 ("CREATE QUOTA test_quota KEYED BY non_existent_key", dict(name="test_quota")),
93 (
94 "CREATE QUOTA `tést quota` KEYED BY ip_address",
95 dict(name="`tést quota`", keyed_by="ip_address"),
96 ),
97 (
98 "CREATE QUOTA `tést quota` ON CLUSTER `tést cluster` KEYED BY client_key,ip_address",
99 dict(
100 name="`tést quota`",
101 cluster="`tést cluster`",
102 keyed_by="client_key,ip_address",
103 ),
104 ),
105 ],
106 )
107 def test_create_quota_regex(search, expected):
108 match = _CREATE_QUOTA_REGEX.match(search)
109 assert match is not None
110 actual = match.groupdict()
111 assert actual == (DEFAULT_CREATE_QUOTA_GROUPS | expected)
112
113
114 DEFAULT_LIMITS_GROUPS = dict(
115 randomized=None,
116 interval_number=None,
117 interval_unit=None,
118 limit_type=None,
119 )
120
121
122 @pytest.mark.parametrize(
123 argnames="search,expected",
124 argvalues=[
125 (
126 "FOR INTERVAL 5 minute NO LIMITS",
127 {
128 "interval_number": "5",
129 "interval_unit": "minute",
130 "limit_type": "NO LIMITS",
131 },
132 ),
133 (
134 "FOR RANDOMIZED INTERVAL 0.25 year TRACKING ONLY",
135 {
136 "randomized": "RANDOMIZED",
137 "interval_number": "0.25",
138 "interval_unit": "year",
139 "limit_type": "TRACKING ONLY",
140 },
141 ),
142 (
143 "FOR INTERVAL 1 day MAX queries = 100",
144 {
145 "interval_number": "1",
146 "interval_unit": "day",
147 "limit_type": "MAX queries = 100",
148 },
149 ),
150 (
151 "FOR INTERVAL 1 day MAX query_selects = 80, query_inserts = 20",
152 {
153 "interval_number": "1",
154 "interval_unit": "day",
155 "limit_type": "MAX query_selects = 80, query_inserts = 20",
156 },
157 ),
158 (
159 "FOR RANDOMIZED INTERVAL 1000 second MAX execution_time = 100.5, failed_sequential_authentications = 10",
160 {
161 "randomized": "RANDOMIZED",
162 "interval_number": "1000",
163 "interval_unit": "second",
164 "limit_type": "MAX execution_time = 100.5, failed_sequential_authentications = 10",
165 },
166 ),
167 ],
168 )
169 def test_limits_regex(search, expected):
170 match = _LIMITS_REGEX.match(search)
171 assert match is not None
172 actual = match.groupdict()
173 assert actual == (DEFAULT_LIMITS_GROUPS | expected)
174
175
176 @pytest.mark.parametrize(
177 argnames="search,expected",
178 argvalues=[
179 (" TO DEFAULT", dict(apply_to="DEFAULT")),
180 (
181 " TO CURRENT_USER, test_user, `tést user`",
182 dict(apply_to="CURRENT_USER, test_user, `tést user`"),
183 ),
184 (
185 " TO DEFAULT, `tést user`, CURRENT_USER",
186 dict(apply_to="DEFAULT, `tést user`, CURRENT_USER"),
187 ),
188 (" TO ALL", dict(apply_to="ALL")),
189 (" TO ALL EXCEPT CURRENT_USER", dict(apply_to="ALL EXCEPT CURRENT_USER")),
190 (
191 " TO ALL EXCEPT CURRENT_USER, test_user, `tést user`",
192 dict(apply_to="ALL EXCEPT CURRENT_USER, test_user, `tést user`"),
193 ),
194 ],
195 )
196 def test_apply_to_regex(search, expected):
197 match = _APPLY_TO_REGEX.match(search)
198 assert match is not None
199 actual = match.groupdict()
200 assert actual == expected
201
202
203 DEFAULT_PARSE_PARAMS = dict(
204 cluster=None,
205 keyed_by=None,
206 limits=[],
207 )
208
209
210 @pytest.mark.parametrize(
211 argnames="create_statement,expected",
212 argvalues=[
213 (
214 "CREATE QUOTA test_quota FOR INTERVAL 1 hour TRACKING ONLY TO DEFAULT, `tést user`, CURRENT_USER",
215 {
216 "limits": [
217 {
218 "randomized_start": False,
219 "interval": "1 hour",
220 "tracking_only": True,
221 },
222 ],
223 "apply_to": ["DEFAULT", "tést user", "CURRENT_USER"],
224 "apply_to_mode": "listed_only",
225 },
226 ),
227 (
228 "CREATE QUOTA test_quota FOR RANDOMIZED INTERVAL 1 hour NO LIMITS TO ALL",
229 {
230 "limits": [
231 {
232 "randomized_start": True,
233 "interval": "1 hour",
234 "no_limits": True,
235 },
236 ],
237 "apply_to": [],
238 "apply_to_mode": "all",
239 },
240 ),
241 (
242 "CREATE QUOTA test_quota ON CLUSTER `tést cluster` KEYED BY client_key,user_name"
243 " FOR RANDOMIZED INTERVAL 1 minute MAX queries = 100, query_selects = 80, query_inserts = 10,"
244 " FOR INTERVAL 1 day MAX execution_time = 3000.5, read_rows = 1024"
245 " TO ALL EXCEPT `tést user`",
246 {
247 "cluster": "tést cluster",
248 "keyed_by": "client_key,user_name",
249 "limits": [
250 {
251 "randomized_start": True,
252 "interval": "1 minute",
253 "max": {
254 "queries": 100,
255 "query_selects": 80,
256 "query_inserts": 10,
257 },
258 },
259 {
260 "randomized_start": False,
261 "interval": "1 day",
262 "max": {
263 "execution_time": 3000.5,
264 "read_rows": 1024,
265 },
266 },
267 ],
268 "apply_to": ["tést user"],
269 "apply_to_mode": "all_except_listed",
270 },
271 ),
272 (
273 "CREATE QUOTA tracking_only KEYED BY user_name FOR INTERVAL 15 minute TRACKING ONLY TO ALL",
274 {
275 "keyed_by": "user_name",
276 "limits": [
277 {
278 "randomized_start": False,
279 "interval": "15 minute",
280 "tracking_only": True,
281 },
282 ],
283 "apply_to": [],
284 "apply_to_mode": "all",
285 },
286 ),
287 ],
288 )
289 def test_parse_create_statement(create_statement, expected):
290 actual = ClickHouseQuota._parse_create_statement(create_statement)
291 assert actual == (DEFAULT_PARSE_PARAMS | expected)
292
293
294 @pytest.mark.parametrize(
295 argnames="params,expected",
296 argvalues=[
297 ({}, {}),
298 (
299 {"apply_to": ["test_user", "current_user"]},
300 {"apply_to": ["current_user", "test_user"]},
301 ),
302 ({"apply_to_mode": "all_except_listed"}, {"apply_to_mode": "all"}),
303 ({"extra_args": "foo"}, {}),
304 ({"keyed_by": "user_name"}, {"keyed_by": "user_name"}),
305 ({"keyed_by": "client_key,user_name"}, {"keyed_by": "client_key,user_name"}),
306 ({"keyed_by": "client_key, user_name"}, {"keyed_by": "client_key,user_name"}),
307 (
308 {"limits": [{"interval": "5 minute"}, {"interval": "1 minute"}]},
309 {
310 "limits": [
311 {
312 "interval": "1 minute",
313 "randomized_start": False,
314 "max": {},
315 "no_limits": None,
316 "tracking_only": None,
317 },
318 {
319 "interval": "5 minute",
320 "randomized_start": False,
321 "max": {},
322 "no_limits": None,
323 "tracking_only": None,
324 },
325 ]
326 },
327 ),
328 (
329 {
330 "limits": [
331 {
332 "interval": "1 day",
333 "max": {
334 "queries": 10,
335 },
336 }
337 ]
338 },
339 {
340 "limits": [
341 {
342 "randomized_start": False,
343 "interval": "1 day",
344 "max": {
345 "queries": 10,
346 "query_selects": None,
347 "query_inserts": None,
348 "errors": None,
349 "result_rows": None,
350 "result_bytes": None,
351 "read_rows": None,
352 "read_bytes": None,
353 "written_bytes": None,
354 "execution_time": None,
355 "failed_sequential_authentications": None,
356 },
357 "no_limits": None,
358 "tracking_only": None,
359 }
360 ]
361 },
362 ),
363 (
364 {
365 "limits": [
366 {
367 "interval": "1 day",
368 "max": {
369 "errors": 1,
370 },
371 }
372 ]
373 },
374 {
375 "limits": [
376 {
377 "randomized_start": False,
378 "interval": "1 day",
379 "max": {
380 "errors": 1,
381 "queries": None,
382 "query_selects": None,
383 "query_inserts": None,
384 "result_rows": None,
385 "result_bytes": None,
386 "read_rows": None,
387 "read_bytes": None,
388 "written_bytes": None,
389 "execution_time": None,
390 "failed_sequential_authentications": None,
391 },
392 "no_limits": None,
393 "tracking_only": None,
394 }
395 ]
396 },
397 ),
398 (
399 {
400 "limits": [
401 {
402 "interval": "1 day",
403 "no_limits": True,
404 }
405 ]
406 },
407 {"limits": []},
408 ),
409 (
410 {
411 "limits": [
412 {
413 "interval": "1 day",
414 "tracking_only": True,
415 }
416 ]
417 },
418 {
419 "limits": [
420 {
421 "randomized_start": False,
422 "interval": "1 day",
423 "max": {},
424 "no_limits": None,
425 "tracking_only": True,
426 }
427 ]
428 },
429 ),
430 ({"limits": None}, {"limits": []}),
431 ({"apply_to": None}, {"apply_to": []}),
432 (
433 {
434 "limits": [
435 {
436 "interval": "15 minute",
437 "max": None,
438 "no_limits": None,
439 "tracking_only": True,
440 "random_extra_key": True,
441 }
442 ]
443 },
444 {
445 "limits": [
446 {
447 "interval": "15 minute",
448 "max": {},
449 "no_limits": None,
450 "tracking_only": True,
451 "randomized_start": False,
452 }
453 ]
454 },
455 ),
456 ],
457 )
458 def test_normalize(params, expected):
459 actual = ClickHouseQuota._normalize(params)
460 assert actual == (DEFAULT_NORMALIZE_PARAMS | expected)
461
462
463 DEFAULT_PARAMS = dict(
464 state="present",
465 cluster=None,
466 keyed_by=None,
467 limits=[],
468 apply_to_mode="listed_only",
469 )
470
471
472 @pytest.mark.parametrize(
473 argnames="name,params,existing_quota,expected_executed",
474 argvalues=[
475 # create queries
476 ("tést_quota", {}, None, ["CREATE QUOTA 'tést_quota'"]),
477 ("tést_quota", {}, "CREATE QUOTA `tést_quota`", []),
478 (
479 "test_quota",
480 {
481 "cluster": "test_cluster",
482 "keyed_by": "user_name",
483 "limits": [
484 {
485 "interval": "5 minute",
486 "max": {
487 "queries": 100,
488 },
489 }
490 ],
491 "apply_to": ["CURRENT_USER"],
492 },
493 None,
494 [
495 "CREATE QUOTA 'test_quota' ON CLUSTER 'test_cluster' KEYED BY user_name FOR INTERVAL 5 minute MAX queries = 100 TO CURRENT_USER"
496 ],
497 ),
498 # drop queries
499 ("test_quota", dict(state="absent"), None, []),
500 (
501 "test_quota",
502 dict(state="absent"),
503 "CREATE QUOTA 'test_quota'",
504 ["DROP QUOTA 'test_quota'"],
505 ),
506 # alter queries
507 (
508 "test_quota",
509 {
510 "cluster": "test_cluster",
511 "keyed_by": "user_name",
512 "limits": [
513 {
514 "interval": "5 minute",
515 "max": {
516 "queries": 100,
517 },
518 }
519 ],
520 "apply_to": ["CURRENT_USER"],
521 },
522 "CREATE QUOTA test_quota",
523 [
524 "ALTER QUOTA 'test_quota' ON CLUSTER 'test_cluster' KEYED BY user_name FOR INTERVAL 5 minute MAX queries = 100 TO CURRENT_USER"
525 ],
526 ),
527 (
528 "test_quota",
529 {
530 "limits": [
531 {
532 "interval": "5 minute",
533 "max": {
534 "queries": 100,
535 },
536 }
537 ],
538 "apply_to": ["test_user", "CURRENT_USER"],
539 },
540 "CREATE QUOTA test_quota FOR INTERVAL 5 minute MAX queries = 100 TO CURRENT_USER, test_user",
541 [],
542 ),
543 (
544 "tést quota",
545 {
546 "keyed_by": "client_key",
547 "limits": [
548 {
549 "randomized_start": True,
550 "interval": "5 minute",
551 "max": {
552 "queries": 100,
553 "execution_time": 1.1,
554 },
555 },
556 {
557 "interval": "1 minute",
558 "max": {
559 "errors": 10,
560 },
561 },
562 ],
563 "apply_to": ["default"],
564 },
565 "CREATE QUOTA `tést quota` KEYED BY client_key, user_name FOR INTERVAL 1 minute MAX errors = 10, "
566 "FOR RANDOMIZED INTERVAL 5 minute MAX queries = 100, execution_time = 1.1 TO default",
567 [
568 "ALTER QUOTA 'tést quota' KEYED BY client_key FOR RANDOMIZED INTERVAL 5 minute MAX queries = 100, execution_time = 1.1, "
569 "FOR INTERVAL 1 minute MAX errors = 10 TO default"
570 ],
571 ),
572 (
573 "tést quota",
574 {
575 "keyed_by": "client_key,user_name",
576 "limits": [
577 {
578 "randomized_start": True,
579 "interval": "5 minute",
580 "max": {
581 "queries": 100,
582 "execution_time": 1.1,
583 },
584 },
585 {
586 "interval": "1 minute",
587 "max": {
588 "errors": 10,
589 },
590 },
591 ],
592 "apply_to": ["default"],
593 },
594 "CREATE QUOTA `tést quota` KEYED BY client_key, user_name FOR INTERVAL 1 minute MAX errors = 10, "
595 "FOR RANDOMIZED INTERVAL 5 minute MAX queries = 100, execution_time = 1.1 TO default",
596 [],
597 ),
598 (
599 "test_quota",
600 {
601 "keyed_by": "user_name",
602 "limits": [
603 {
604 "interval": "5 minute",
605 "no_limits": True,
606 },
607 ],
608 },
609 "CREATE QUOTA test_quota KEYED BY user_name",
610 [],
611 ),
612 (
613 "tracking_only",
614 {
615 "keyed_by": "user_name",
616 "limits": [
617 {
618 "interval": "15 minute",
619 "tracking_only": True,
620 "no_limits": None,
621 "max": None,
622 },
623 ],
624 "apply_to": [],
625 "apply_to_mode": "all",
626 },
627 "CREATE QUOTA tracking_only KEYED BY user_name FOR INTERVAL 15 minute TRACKING ONLY TO ALL",
628 [],
629 ),
630 ],
631 )
632 def test_ensure_state(mocker, name, params, existing_quota, expected_executed):
633 for check_mode in (True, False):
634 client = mocker.MagicMock()
635 client.execute.return_value = ["1"] if existing_quota else []
636 module = mocker.MagicMock()
637 module._verbosity = 1
638 module.check_mode = check_mode
639 module.params = DEFAULT_PARAMS | params
640 will_run_show_create = existing_quota and module.params["state"] == "present"
641 quota = ClickHouseQuota(module, client, name)
642 client.execute.assert_called_once_with(
643 f"SELECT 1 FROM system.quotas WHERE name = '{name}' LIMIT 1"
644 )
645 client.execute.reset_mock()
646 client.execute.return_value = [[existing_quota]] if will_run_show_create else []
647 changed = quota.ensure_state()
648 if check_mode:
649 if will_run_show_create:
650 client.execute.assert_called_once_with(f"SHOW CREATE QUOTA '{name}'")
651 else:
652 client.execute.assert_not_called()
653 else:
654 expected_call_args_list = []
655 if will_run_show_create:
656 expected_call_args_list.append(((f"SHOW CREATE QUOTA '{name}'",),))
657 expected_call_args_list.extend(((sql,),) for sql in expected_executed)
658 assert client.execute.call_args_list == expected_call_args_list
659 assert quota.executed_statements == expected_executed
660 assert changed is bool(expected_executed)