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 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
|
from datetime import datetime, timezone, timedelta
from haproxy import commands
import os
from time import tzset
import pytest
def check_output(cmd, output, expected, capsys):
"""Validate the output of commands."""
name = cmd.command_line_name().upper()
cmd.results(output=output)
output_text = capsys.readouterr().out
if output == 'json':
assert f'{{"{name}": {expected}}}' in output_text
else:
assert f'{name}\n====' in output_text
assert f'====\n{expected}\n' in output_text
@pytest.mark.parametrize(
('klass', 'expected'),
[
(commands.StatusCodesCounter, 'status_codes_counter'),
(commands.AverageResponseTime, 'average_response_time'),
(commands.Counter, 'counter'),
(commands.IpCounter, 'ip_counter'),
],
)
def test_commands_names(klass, expected):
"""Check that the command line name of command classes are generated correctly."""
assert klass.command_line_name() == expected
def test_counter_results():
"""Test the Counter command.
It plain and simply counts all the lines passed to it.
"""
cmd = commands.Counter()
assert cmd.raw_results() == 0
for x in range(3):
cmd(x)
assert cmd.raw_results() == 3
@pytest.mark.parametrize('output', [None, 'json'])
def test_counter_output(capsys, output):
"""Test the Counter command.
It plain and simply counts all the lines passed to it.
"""
cmd = commands.Counter()
for x in range(3):
cmd(x)
check_output(cmd, output, 3, capsys)
def test_http_methods_results(line_factory):
"""Test the HTTPMethods command.
It creates a breakdown of how many times each HTTP verb has been used.
"""
cmd = commands.HttpMethods()
assert cmd.raw_results() == {}
for verb, count in (('POST', 4), ('GET', 3), ('PUT', 2)):
line = line_factory(http_request=f'{verb} /path/to/image HTTP/1.1')
for _ in range(count):
cmd(line)
results = cmd.raw_results()
assert len(results) == 3
assert results['POST'] == 4
assert results['GET'] == 3
assert results['PUT'] == 2
@pytest.mark.parametrize(
('output', 'expected'),
[(None, '- PUT: 2\n- GET: 1'), ('json', '[{"PUT": 2}, {"GET": 1}]')],
)
def test_http_methods_output(line_factory, capsys, output, expected):
"""Test the HTTPMethods command.
It creates a breakdown of how many times each HTTP verb has been used.
"""
cmd = commands.HttpMethods()
for verb, count in (('GET', 1), ('PUT', 2)):
line = line_factory(http_request=f'{verb} /path/to/image HTTP/1.1')
for _ in range(count):
cmd(line)
check_output(cmd, output, expected, capsys)
def test_ip_counter_results(line_factory):
"""Test the IpCounter command.
It creates a breakdown of how many times each IP has been used.
"""
cmd = commands.IpCounter()
assert cmd.raw_results() == {}
for ip, count in (('192.168.0.1', 4), ('172.4.3.2', 3), ('8.7.6.5', 2)):
line = line_factory(headers=f' {{{ip}}}')
for _ in range(count):
cmd(line)
results = cmd.raw_results()
assert len(results) == 3
assert results['192.168.0.1'] == 4
assert results['172.4.3.2'] == 3
assert results['8.7.6.5'] == 2
@pytest.mark.parametrize(
('output', 'expected'),
[
(None, '- 172.4.3.2: 3\n- 8.7.6.5: 2'),
('json', '[{"172.4.3.2": 3}, {"8.7.6.5": 2}]'),
],
)
def test_ip_counter_output(line_factory, capsys, output, expected):
"""Test the IpCounter command.
It creates a breakdown of how many times each IP has been used.
"""
cmd = commands.IpCounter()
for ip, count in (('172.4.3.2', 3), ('8.7.6.5', 2)):
line = line_factory(headers=f' {{{ip}}}')
for _ in range(count):
cmd(line)
check_output(cmd, output, expected, capsys)
def test_top_ips_results(line_factory):
"""Test the TopIps command.
It lists the 10 most used IPs, and how much where they used.
"""
cmd = commands.TopIps()
assert cmd.raw_results() == []
for ip, count in ((f'192.168.0.{x}', x) for x in range(11)):
line = line_factory(headers=f' {{{ip}}}')
for _ in range(count):
cmd(line)
results = cmd.raw_results()
assert len(results) == 10
assert results[0] == ('192.168.0.10', 10)
assert results[1] == ('192.168.0.9', 9)
assert results[2] == ('192.168.0.8', 8)
assert results[3] == ('192.168.0.7', 7)
assert results[4] == ('192.168.0.6', 6)
assert results[5] == ('192.168.0.5', 5)
assert results[6] == ('192.168.0.4', 4)
assert results[7] == ('192.168.0.3', 3)
assert results[8] == ('192.168.0.2', 2)
assert results[9] == ('192.168.0.1', 1)
def test_top_ips_print_results(line_factory):
"""Test the TopIps command.
Ensure that when they are printed, only 10 results are shown.
"""
cmd = commands.TopIps()
for ip, count in ((f'192.168.0.{x}', x) for x in range(14)):
line = line_factory(headers=f' {{{ip}}}')
for _ in range(count):
cmd(line)
results = cmd.print_data()
results = [x for x in results.split('\n') if x]
assert len(results) == 10
assert results[0] == '- 192.168.0.13: 13'
assert results[-1] == '- 192.168.0.4: 4'
@pytest.mark.parametrize(
('output', 'expected'),
[
(None, '- 192.168.0.2: 2\n- 192.168.0.1: 1'),
('json', '[{"192.168.0.2": 2}, {"192.168.0.1": 1}]'),
],
)
def test_top_ips_output(line_factory, capsys, output, expected):
"""Test the TopIps command.
It lists the 10 most used IPs, and how much where they used.
"""
cmd = commands.TopIps()
assert cmd.raw_results() == []
for ip, count in ((f'192.168.0.{x}', x) for x in range(3)):
line = line_factory(headers=f' {{{ip}}}')
for _ in range(count):
cmd(line)
check_output(cmd, output, expected, capsys)
def test_status_codes_counter_results(line_factory):
"""Test the StatusCodesCounter command.
It creates a breakdown of which status codes have been used and how many each.
"""
cmd = commands.StatusCodesCounter()
assert cmd.raw_results() == {}
for status_code, count in (('200', 4), ('301', 3), ('500', 2)):
line = line_factory(status=status_code)
for _ in range(count):
cmd(line)
results = cmd.raw_results()
assert len(results) == 3
assert results['200'] == 4
assert results['301'] == 3
assert results['500'] == 2
@pytest.mark.parametrize(
('output', 'expected'),
[(None, '- 301: 3\n- 500: 2'), ('json', '[{"301": 3}, {"500": 2}]')],
)
def test_status_codes_counter_output(line_factory, capsys, output, expected):
"""Test the StatusCodesCounter command.
It creates a breakdown of which status codes have been used and how many each.
"""
cmd = commands.StatusCodesCounter()
for status_code, count in (('301', 3), ('500', 2)):
line = line_factory(status=status_code)
for _ in range(count):
cmd(line)
check_output(cmd, output, expected, capsys)
def test_request_path_counter_results(line_factory):
"""Test the RequestPathCounter command.
It creates a breakdown of how many times each URL path has been used.
"""
cmd = commands.RequestPathCounter()
assert cmd.raw_results() == {}
for path, count in (('/image/one', 4), ('/video/two', 3), ('/article/three', 2)):
line = line_factory(http_request=f'GET {path} HTTP/1.1')
for _ in range(count):
cmd(line)
results = cmd.raw_results()
assert len(results) == 3
assert results['/image/one'] == 4
assert results['/video/two'] == 3
assert results['/article/three'] == 2
@pytest.mark.parametrize(
('output', 'expected'),
[
(None, '- /video/two: 3\n- /article/three: 2'),
('json', '[{"/video/two": 3}, {"/article/three": 2}]'),
],
)
def test_request_path_counter_output(line_factory, capsys, output, expected):
"""Test the RequestPathCounter command.
It creates a breakdown of how many times each URL path has been used.
"""
cmd = commands.RequestPathCounter()
for path, count in (('/video/two', 3), ('/article/three', 2)):
line = line_factory(http_request=f'GET {path} HTTP/1.1')
for _ in range(count):
cmd(line)
check_output(cmd, output, expected, capsys)
def test_slow_requests_results(line_factory):
"""Test the SlowRequests command.
It lists all requests that took more than 1000 milliseconds to respond.
"""
cmd = commands.SlowRequests()
assert cmd.raw_results() == []
for total_time in (1003, 987, 456, 2013, 45000, 1000, 3200, 999):
cmd(line_factory(tr=total_time))
results = cmd.raw_results()
assert results == [1000, 1003, 2013, 3200, 45000]
@pytest.mark.parametrize(
('output', 'expected'),
[
(None, [1000, 1003, 2013, 3200, 45000]),
('json', '[1000, 1003, 2013, 3200, 45000]'),
],
)
def test_slow_requests_output(line_factory, capsys, output, expected):
"""Test the SlowRequests command.
It lists all requests that took more than 1000 milliseconds to respond.
"""
cmd = commands.SlowRequests()
for total_time in (1003, 987, 456, 2013, 45000, 1000, 3200, 999):
cmd(line_factory(tr=total_time))
check_output(cmd, output, expected, capsys)
def test_top_request_paths_results(line_factory):
"""Test the TopRequestPaths command.
It lists the 10 most used URL paths, and how much where they used.
"""
cmd = commands.TopRequestPaths()
assert cmd.raw_results() == []
for path, count in ((f'/file/{x}', x) for x in range(11)):
line = line_factory(http_request=f'GET {path} HTTP/1.1')
for _ in range(count):
cmd(line)
results = cmd.raw_results()
assert len(results) == 10
assert results[0] == ('/file/10', 10)
assert results[1] == ('/file/9', 9)
assert results[2] == ('/file/8', 8)
assert results[3] == ('/file/7', 7)
assert results[4] == ('/file/6', 6)
assert results[5] == ('/file/5', 5)
assert results[6] == ('/file/4', 4)
assert results[7] == ('/file/3', 3)
assert results[8] == ('/file/2', 2)
assert results[9] == ('/file/1', 1)
def test_top_request_paths_print_results(line_factory):
"""Test the TopRequestPaths command.
Ensure that when they are printed, only 10 results are shown.
"""
cmd = commands.TopRequestPaths()
for path, count in ((f'/file/{x}', x) for x in range(14)):
line = line_factory(http_request=f'GET {path} HTTP/1.1')
for _ in range(count):
cmd(line)
results = cmd.print_data()
results = [x for x in results.split('\n') if x]
assert len(results) == 10
assert results[0] == '- /file/13: 13'
assert results[-1] == '- /file/4: 4'
@pytest.mark.parametrize(
('output', 'expected'),
[
(None, '- /file/2: 2\n- /file/1: 1'),
('json', '[{"/file/2": 2}, {"/file/1": 1}]'),
],
)
def test_top_request_paths_output(line_factory, capsys, output, expected):
"""Test the TopRequestPaths command.
It lists the 10 most used URL paths, and how much where they used.
"""
cmd = commands.TopRequestPaths()
for path, count in ((f'/file/{x}', x) for x in range(3)):
line = line_factory(http_request=f'GET {path} HTTP/1.1')
for _ in range(count):
cmd(line)
check_output(cmd, output, expected, capsys)
def test_slow_requests_counter_results(line_factory):
"""Test the SlowRequestsCounter command.
It counts how many requests took more than 1000 milliseconds to complete.
"""
cmd = commands.SlowRequestsCounter()
assert cmd.raw_results() == 0
for total_time in (1003, 987, 456, 2013, 45000, 1000, 3200, 999):
cmd(line_factory(tr=total_time))
results = cmd.raw_results()
assert results == 5
@pytest.mark.parametrize('output', [None, 'json'])
def test_slow_requests_counter_output(line_factory, capsys, output):
"""Test the SlowRequestsCounter command.
It counts how many requests took more than 1000 milliseconds to complete.
"""
cmd = commands.SlowRequestsCounter()
for total_time in (1003, 987, 456, 2013, 45000, 1000, 3200, 999):
cmd(line_factory(tr=total_time))
check_output(cmd, output, 5, capsys)
@pytest.mark.parametrize(
('series', 'average'),
[
((1003, 987, 456, 2013, 1000, 3200, 999), 1379.71),
((110, -1, 110), 110), # aborted connections are ignored
((45, 30, 0), 25), # responses that take 0 milliseconds are still counted
],
)
def test_average_response_time_results(line_factory, series, average):
"""Test the AverageResponseTime command.
Returns the average response time of all valid requests.
"""
cmd = commands.AverageResponseTime()
assert cmd.raw_results() == 0.0
for total_time in series:
cmd(line_factory(tr=total_time))
results = cmd.raw_results()
assert results == average
@pytest.mark.parametrize('output', [None, 'json'])
def test_average_response_time_output(line_factory, capsys, output):
"""Test the AverageResponseTime command.
Returns the average response time of all valid requests.
"""
cmd = commands.AverageResponseTime()
for total_time in (
40,
30,
):
cmd(line_factory(tr=total_time))
check_output(cmd, output, 35.0, capsys)
@pytest.mark.parametrize(
('series', 'average'),
[
((1003, 987, 456, 2013, 1000, 3200, 999), 1379.71),
((110, -1, 110), 110), # aborted connections are ignored
((45, 30, 0), 25), # requests that do not wait at all are still counted
],
)
def test_average_waiting_time_results(line_factory, series, average):
"""Test the AverageWaitingTime command.
Returns the average time requests had to wait to get processed.
"""
cmd = commands.AverageWaitingTime()
assert cmd.raw_results() == 0.0
for wait_time in series:
cmd(line_factory(tw=wait_time))
results = cmd.raw_results()
assert results == average
@pytest.mark.parametrize('output', [None, 'json'])
def test_average_waiting_time_output(line_factory, capsys, output):
"""Test the AverageWaitingTime command.
Returns the average time requests had to wait to get processed.
"""
cmd = commands.AverageWaitingTime()
for wait_time in (40, 30):
cmd(line_factory(tw=wait_time))
check_output(cmd, output, 35.0, capsys)
def test_server_load_results(line_factory):
"""Test the ServerLoad command.
It creates a breakdown of how many requests each server processed.
"""
cmd = commands.ServerLoad()
assert cmd.raw_results() == {}
for name, count in (('server4', 4), ('server3', 3), ('server5', 5)):
line = line_factory(server_name=name)
for _ in range(count):
cmd(line)
results = cmd.raw_results()
assert len(results) == 3
assert results['server5'] == 5
assert results['server4'] == 4
assert results['server3'] == 3
@pytest.mark.parametrize(
('output', 'expected'),
[
(None, '- server5: 5\n- server3: 3'),
('json', '[{"server5": 5}, {"server3": 3}]'),
],
)
def test_server_load_output(line_factory, capsys, output, expected):
"""Test the ServerLoad command.
It creates a breakdown of how many requests each server processed.
"""
cmd = commands.ServerLoad()
for name, count in (('server3', 3), ('server5', 5)):
line = line_factory(server_name=name)
for _ in range(count):
cmd(line)
check_output(cmd, output, expected, capsys)
def test_queue_peaks_no_lines_results(line_factory):
"""Test the QueuePeaks command.
If there are no log lines processed, nothing should be returned.
"""
cmd = commands.QueuePeaks()
assert cmd.raw_results() == []
def test_queue_peaks_no_queues(line_factory):
"""Test the QueuePeaks command.
If there are no log lines processed, nothing should be returned.
"""
cmd = commands.QueuePeaks()
now = datetime.now()
for second in range(4):
accept_date = now.replace(second=second).strftime('%d/%b/%Y:%H:%M:%S.%f')
cmd(line_factory(queue_backend=0, accept_date=accept_date))
assert len(cmd.requests) == 4
assert cmd.raw_results() == []
@pytest.mark.parametrize(
('date', 'expected_key'),
[
('10/Dec/2019:15:40:12.12345', 1575988812.12345),
('15/Jan/2017:05:23:05.456', 1484454185.456),
('15/Jan/2017:05:23:05.0', 1484454185.0),
],
)
def test_queue_peaks_generated_keys(line_factory, date, expected_key):
"""Test the QueuePeaks command.
Check how the keys for the requests dictionary are generated.
"""
os.environ['TZ'] = 'UTC-01'
tzset()
cmd = commands.QueuePeaks()
cmd(line_factory(queue_backend=0, accept_date=date))
keys = list(cmd.requests.keys())
assert keys[0] == expected_key
def test_queue_peaks_details(line_factory):
"""Test the QueuePeaks command.
Check the information returned for each peak.
"""
os.environ['TZ'] = 'UTC-01'
tzset()
cmd = commands.QueuePeaks()
for microseconds, queue in enumerate([0, 4, 7, 8, 19, 4, 0]):
line = line_factory(
queue_backend=queue, accept_date=f'15/Jan/2017:05:23:05.{microseconds}'
)
cmd(line)
day = datetime(year=2017, month=1, day=15, hour=5, minute=23, second=5)
results = cmd.raw_results()
assert len(results) == 1
peak_info = results[0]
assert peak_info['peak'] == 19
assert peak_info['span'] == 5
assert peak_info['started'] == day.replace(microsecond=100000)
assert peak_info['finished'] == day.replace(microsecond=600000)
def test_queue_peaks_multiple_sorted(line_factory):
"""Test the QueuePeaks command.
Peaks information are returned sorted by date.
"""
os.environ['TZ'] = 'UTC-01'
tzset()
cmd = commands.QueuePeaks()
for microseconds, queue in enumerate([0, 4, 0, 0, 19, 4, 0]):
line = line_factory(
queue_backend=queue, accept_date=f'15/Jan/2017:05:23:05.{microseconds}'
)
cmd(line)
day = datetime(year=2017, month=1, day=15, hour=5, minute=23, second=5)
results = cmd.raw_results()
assert len(results) == 2
assert results[0]['peak'] == 4
assert results[0]['started'] == day.replace(microsecond=100000)
assert results[1]['peak'] == 19
assert results[1]['started'] == day.replace(microsecond=400000)
def test_queue_peaks_already_started(line_factory):
"""Test the QueuePeaks command.
Check that QueuePeaks handles the corner case of a peak that has already started.
"""
os.environ['TZ'] = 'UTC-01'
tzset()
cmd = commands.QueuePeaks()
for microseconds, queue in enumerate([4, 19, 0]):
line = line_factory(
queue_backend=queue, accept_date=f'15/Jan/2017:05:23:05.{microseconds}'
)
cmd(line)
day = datetime(year=2017, month=1, day=15, hour=5, minute=23, second=5)
results = cmd.raw_results()
assert len(results) == 1
peak_info = results[0]
assert peak_info['peak'] == 19
assert peak_info['span'] == 2
assert peak_info['started'] == day
assert peak_info['finished'] == day.replace(microsecond=200000)
def test_queue_peaks_did_not_finish(line_factory):
"""Test the QueuePeaks command.
Check that QueuePeaks handles the corner case of a peak that does not finish.
"""
os.environ['TZ'] = 'UTC-01'
tzset()
cmd = commands.QueuePeaks()
for microseconds, queue in enumerate([4, 19, 12]):
line = line_factory(
queue_backend=queue, accept_date=f'15/Jan/2017:05:23:05.{microseconds}'
)
cmd(line)
day = datetime(year=2017, month=1, day=15, hour=5, minute=23, second=5)
results = cmd.raw_results()
assert len(results) == 1
peak_info = results[0]
assert peak_info['peak'] == 19
assert peak_info['span'] == 3
assert peak_info['started'] == day
assert peak_info['finished'] == day.replace(microsecond=200000)
@pytest.mark.parametrize(
('output', 'expected'),
[
(
None,
'- peak: 4 - span: 1 - started: 2017-01-15T05:23:05.100000 - finished: 2017-01-15T05:23:05.200000\n'
'- peak: 19 - span: 2 - started: 2017-01-15T05:23:05.400000 - finished: 2017-01-15T05:23:05.600000',
),
(
'json',
'[{"peak": 4, "span": 1, "started": "2017-01-15T05:23:05.100000", "finished": "2017-01-15T05:23:05.200000"}, '
'{"peak": 19, "span": 2, "started": "2017-01-15T05:23:05.400000", "finished": "2017-01-15T05:23:05.600000"}]',
),
],
)
def test_queue_peaks_output(line_factory, capsys, output, expected):
"""Test the QueuePeaks command.
Peaks information are returned sorted by date.
"""
os.environ['TZ'] = 'UTC-01'
tzset()
cmd = commands.QueuePeaks()
for microseconds, queue in enumerate([0, 4, 0, 0, 19, 4, 0]):
line = line_factory(
queue_backend=queue, accept_date=f'15/Jan/2017:05:23:05.{microseconds}'
)
cmd(line)
check_output(cmd, output, expected, capsys)
def test_connection_type_results(line_factory):
"""Test the ConnectionType command.
It counts how many requests have been made by SSL, and which ones not.
"""
cmd = commands.ConnectionType()
assert cmd.raw_results() == (0, 0)
for path, count in (('/Virtual:443/something', 4), ('/something', 2)):
line = line_factory(http_request=f'GET {path} HTTP/1.1')
for _ in range(count):
cmd(line)
assert cmd.raw_results() == (4, 2)
@pytest.mark.parametrize(
('output', 'expected'),
[(None, '- https: 4\n- http: 2'), ('json', '[{"https": 4}, {"http": 2}]')],
)
def test_connection_type_output(line_factory, capsys, output, expected):
"""Test the ConnectionType command.
It counts how many requests have been made by SSL, and which ones not.
"""
cmd = commands.ConnectionType()
for path, count in (('/Virtual:443/something', 4), ('/something', 2)):
line = line_factory(http_request=f'GET {path} HTTP/1.1')
for _ in range(count):
cmd(line)
check_output(cmd, output, expected, capsys)
def test_requests_per_minute_results(line_factory):
"""Test the RequestsPerMinute command.
It counts how many requests have been made per minute.
"""
cmd = commands.RequestsPerMinute()
assert cmd.raw_results() == []
now = datetime.now()
# to avoid leaping into the next/previous minute with the timedeltas below
now = now.replace(second=30)
microseconds = timedelta(microseconds=200)
seconds = timedelta(seconds=5)
minutes = timedelta(minutes=5)
hours = timedelta(hours=2)
dates = [
now,
now + microseconds,
now - microseconds,
now + seconds,
now - seconds,
now + minutes,
now - minutes,
now + hours,
now - hours,
]
for time in dates:
cmd(line_factory(accept_date=f'{time:%d/%b/%Y:%H:%M:%S.%f}'))
results = cmd.raw_results()
assert len(results) == 5
assert results[0][1] == 1
assert results[1][1] == 1
assert results[2][1] == 5 # now and the +- microseconds and +- seconds
assert results[3][1] == 1
assert results[4][1] == 1
@pytest.mark.parametrize('output', [None, 'json'])
def test_requests_per_minute_output(line_factory, capsys, output):
"""Test the RequestsPerMinute command.
It counts how many requests have been made per minute.
"""
cmd = commands.RequestsPerMinute()
now = datetime.now()
for time in (now, now + timedelta(hours=2)):
cmd(line_factory(accept_date=f'{time:%d/%b/%Y:%H:%M:%S.%f}'))
name = cmd.command_line_name().upper()
cmd.results(output=output)
output_text = capsys.readouterr().out
if output == 'json':
assert f'{{"{name}": ' in output_text
# this is quite fuzzy to not have to fiddle with the date formatting
# change it once we hit 2030 :)
assert ':00": 1}, {"202' in output_text
else:
assert f'{name}\n====' in output_text
# this is quite fuzzy to not have to fiddle with the date formatting
assert ':00: 1\n- ' in output_text
def test_requests_per_hour_results(line_factory):
"""Test the RequestsPerHour command.
It counts how many requests have been made per hour.
"""
cmd = commands.RequestsPerHour()
assert cmd.raw_results() == []
specific_date = datetime(year=2022, month=12, day=3, hour=14, minute=10, second=30)
minutes = timedelta(minutes=5)
hours = timedelta(hours=2)
dates = [
specific_date,
specific_date + minutes,
specific_date - minutes,
specific_date + hours,
specific_date - hours,
specific_date + hours * 2,
specific_date - hours * 2,
]
for time in dates:
cmd(line_factory(accept_date=f'{time:%d/%b/%Y:%H:%M:%S.%f}'))
results = cmd.raw_results()
assert len(results) == 5
assert results[0][1] == 1
assert results[1][1] == 1
assert results[2][1] == 3 # now and the +- minutes
assert results[3][1] == 1
assert results[4][1] == 1
@pytest.mark.parametrize('output', [None, 'json'])
def test_requests_per_hour_output(line_factory, capsys, output):
"""Test the RequestsPerHour command.
It counts how many requests have been made per hour.
"""
cmd = commands.RequestsPerHour()
now = datetime.now()
for time in (now, now + timedelta(hours=2)):
cmd(line_factory(accept_date=f'{time:%d/%b/%Y:%H:%M:%S.%f}'))
name = cmd.command_line_name().upper()
cmd.results(output=output)
output_text = capsys.readouterr().out
if output == 'json':
assert f'{{"{name}": ' in output_text
# this is quite fuzzy to not have to fiddle with the date formatting
# change it once we hit 2030 :)
assert ':00": 1}, {"202' in output_text
else:
assert f'{name}\n====' in output_text
# this is quite fuzzy to not have to fiddle with the date formatting
assert ':00: 1\n- ' in output_text
def test_print_results_and_output(line_factory, capsys):
"""Test the Print command.
It simply prints the verbatim line.
"""
cmd = commands.Print()
assert cmd.raw_results() is None
for path in ('/first-thing-to-do', '/second/thing/to-do'):
cmd(line_factory(http_request=f'GET {path} HTTP/1.1'))
assert cmd.raw_results() is None
output_text = capsys.readouterr().out
lines = output_text.split('\n')
assert len(lines) == 3
assert '/first-thing-to-do' in lines[0]
assert '/second/thing/to-do' in lines[1]
assert lines[2] == ''
|