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 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
|
import asyncio
import contextlib
import io
import json
import os
import pytest
import random
import re
import signal
import time
import unittest
from collections import defaultdict
from unittest.mock import patch
import aiohttp
from molotov import __version__
from molotov.api import global_setup, scenario
from molotov.run import main, run
from molotov.session import get_context
from molotov.shared.counter import Counters
from molotov.tests._grpc import service as grpc_service
from molotov.tests.statsd import run_server, stop_server
from molotov.tests.support import (
TestLoop,
catch_sleep,
co_catch_output,
coserver,
dedicatedloop,
dedicatedloop_noclose,
only_pypy,
set_args,
skip_pypy,
)
from molotov.util import json_request, request, set_timer
_HERE = os.path.dirname(__file__)
_CONFIG = os.path.join(_HERE, "molotov.json")
_RES = []
_RES2 = {}
@contextlib.contextmanager
def grpc_server():
server = grpc_service.GRPCServer()
try:
yield server
finally:
server.stop()
class TestRunner(TestLoop):
def setUp(self):
super().setUp()
_RES[:] = []
_RES2.clear()
def _get_args(self, udp_port=None):
args = self.get_args()
if udp_port is not None:
args.statsd = True
args.statsd_address = "udp://localhost:%s" % udp_port
args.scenario = "molotov.tests.test_run"
return args
@co_catch_output
@dedicatedloop_noclose
def test_redirect(self):
with coserver() as port:
@scenario(weight=10)
async def _one(session):
# redirected
async with session.get("http://localhost:%s/redirect" % port) as resp:
redirect = resp.history
assert redirect[0].status == 302
assert resp.status == 200
# not redirected
async with session.get(
"http://localhost:%s/redirect" % port, allow_redirects=False
) as resp:
redirect = resp.history
assert len(redirect) == 0
assert resp.status == 302
content = await resp.text()
assert content == ""
_RES.append(1)
args = self._get_args()
args.verbose = 2
args.max_runs = 2
run(args)
self.assertTrue(len(_RES) > 0)
@co_catch_output
@dedicatedloop_noclose
def test_runner(self):
with coserver() as port:
@global_setup()
def something_sync(args):
grab = request("http://localhost:%s" % port)
self.assertEqual(grab["status"], 200)
grab_json = json_request("http://localhost:%s/molotov.json" % port)
self.assertTrue("molotov" in grab_json["content"])
@scenario(weight=10)
async def here_one(session):
async with session.get("http://localhost:%s" % port) as resp:
await resp.text()
_RES.append(1)
@scenario(weight=90)
async def here_two(session):
statsd = get_context(session).statsd
if statsd is not None:
for _i in range(10):
statsd.increment("user.online")
await asyncio.sleep(0)
await asyncio.sleep(0.1)
_RES.append(2)
udp_proc, udp_port, udp_conn = run_server()
args = self._get_args(udp_port)
args.max_runs = 3
args.duration = 9999
run(args)
self.assertTrue(len(_RES) > 0)
received = stop_server(udp_proc, udp_conn)
self.assertTrue(len(received) > 0)
@dedicatedloop
def test_main(self):
with set_args("molotov", "-q", "-d", "1", "molotov/tests/example.py"):
main()
def _test_molotov(self, *args):
if "--duration" not in args and "-d" not in args:
args = list(args) + ["--duration", "10"]
rc = 0
with set_args("molotov", *args) as (stdout, stderr):
try:
main()
except SystemExit as e:
rc = e.code
return stdout.read().strip(), stderr.read().strip(), rc
@dedicatedloop
def test_version(self):
stdout, stderr, rc = self._test_molotov("--version")
self.assertEqual(stdout, __version__)
@dedicatedloop
def test_empty_scenario(self):
stdout, stderr, rc = self._test_molotov("")
self.assertTrue("Cannot import" in stdout)
@dedicatedloop
def test_config_no_scenario(self):
stdout, stderr, rc = self._test_molotov("--config", _CONFIG, "DONTEXIST")
wanted = "Can't find 'DONTEXIST' in the config"
self.assertTrue(wanted in stdout)
@dedicatedloop
def test_config_verbose_quiet(self):
stdout, stderr, rc = self._test_molotov("-qv", "--config", _CONFIG)
wanted = "You can't"
self.assertTrue(wanted in stdout)
@dedicatedloop
def test_config_no_scenario_found(self):
stdout, stderr, rc = self._test_molotov("molotov.tests.test_run")
wanted = "No scenario was found"
self.assertTrue(wanted in stdout)
@dedicatedloop
def test_config_no_single_mode_found(self):
@scenario(weight=10)
async def not_me(session):
_RES.append(3)
stdout, stderr, rc = self._test_molotov("-s", "blah", "molotov.tests.test_run")
wanted = "Can't find"
self.assertTrue(wanted in stdout)
@dedicatedloop
def test_name(self):
@scenario(weight=10)
async def here_three(session):
_RES.append(3)
@scenario(weight=30, name="me")
async def here_four(session):
_RES.append(4)
stdout, stderr, rc = self._test_molotov(
"-x", "--max-runs", "2", "-s", "me", "molotov.tests.test_run"
)
wanted = "SUCCESSES: 2"
self.assertTrue(wanted in stdout)
self.assertTrue(_RES, [4, 4])
@dedicatedloop
def test_single_mode(self):
@scenario(weight=10)
async def here_three(session):
_RES.append(3)
stdout, stderr, rc = self._test_molotov(
"-x", "--max-runs", "2", "-s", "here_three", "molotov.tests.test_run"
)
wanted = "SUCCESSES: 2"
self.assertTrue(wanted in stdout)
@dedicatedloop
def test_fail_mode_pass(self):
@scenario(weight=10)
async def here_three(session):
_RES.append(3)
stdout, stderr, rc = self._test_molotov(
"-x",
"--max-runs",
"2",
"--fail",
"1",
"-s",
"here_three",
"molotov.tests.test_run",
)
wanted = "SUCCESSES: 2"
self.assertTrue(wanted in stdout)
self.assertEqual(rc, 0)
@dedicatedloop
def test_fail_mode_fail(self):
@scenario(weight=10)
async def here_three(session):
raise AssertionError()
stdout, stderr, rc = self._test_molotov(
"-x",
"--max-runs",
"2",
"--fail",
"1",
"-s",
"here_three",
"molotov.tests.test_run",
)
self.assertEqual(rc, 1)
@only_pypy
@dedicatedloop
def test_uvloop_pypy(self):
@scenario(weight=10)
async def here_three(session):
_RES.append(3)
orig_import = __import__
def import_mock(name, *args):
if name == "uvloop":
raise ImportError()
return orig_import(name, *args)
with patch("builtins.__import__", side_effect=import_mock):
stdout, stderr, rc = self._test_molotov(
"-x",
"--max-runs",
"2",
"-s",
"here_three",
"--uvloop",
"molotov.tests.test_run",
)
wanted = "You can't use uvloop"
self.assertTrue(wanted in stdout)
@skip_pypy
@dedicatedloop
def test_uvloop_import_error(self):
@scenario(weight=10)
async def here_three(session):
_RES.append(3)
orig_import = __import__
def import_mock(name, *args):
if name == "uvloop":
raise ImportError()
return orig_import(name, *args)
with patch("builtins.__import__", side_effect=import_mock):
stdout, stderr, rc = self._test_molotov(
"-x",
"--max-runs",
"2",
"--console-update",
"0",
"-s",
"here_three",
"--uvloop",
"molotov.tests.test_run",
)
wanted = "You need to install uvloop"
self.assertTrue(wanted in stdout)
@skip_pypy
@dedicatedloop
def test_uvloop(self):
try:
import uvloop # noqa
except ImportError:
return
@scenario(weight=10)
async def here_three(session):
_RES.append(3)
stdout, stderr, rc = self._test_molotov(
"-x",
"--max-runs",
"2",
"-s",
"here_three",
"--uvloop",
"molotov.tests.test_run",
)
wanted = "SUCCESSES: 2"
self.assertTrue(wanted in stdout, stdout)
@dedicatedloop
def test_delay(self):
with catch_sleep() as delay:
@scenario(weight=10, delay=0.1)
async def here_three(session):
_RES.append(3)
stdout, stderr, rc = self._test_molotov(
"--delay",
"0.21",
"-x",
"--max-runs",
"3",
"-s",
"here_three",
"molotov.tests.test_run",
)
wanted = "SUCCESSES: 3"
self.assertTrue(wanted in stdout, stdout)
self.assertEqual(delay.count(0.21), 3)
@dedicatedloop
def test_rampup(self):
with catch_sleep() as delay:
@scenario(weight=10)
async def here_three(session):
_RES.append(3)
stdout, stderr, rc = self._test_molotov(
"--ramp-up",
"10",
"--workers",
"5",
"--console-update",
"0",
"-x",
"--max-runs",
"2",
"-s",
"here_three",
"molotov.tests.test_run",
)
# workers should start every 2 seconds since
# we have 5 workers and a ramp-up
# the first one starts immediatly, then each worker
# sleeps 2 seconds more.
self.assertTrue({2.0, 4.0, 6.0, 8.0}.issubset(set(delay)))
wanted = "SUCCESSES: 10"
self.assertTrue(wanted in stdout, stdout)
@dedicatedloop
@pytest.mark.skip(reason="This test is not reliable")
def test_sizing(self):
_RES2["fail"] = 0
_RES2["succ"] = 0
with catch_sleep():
@scenario()
async def sizer(session):
if random.randint(0, 20) == 1:
_RES2["fail"] += 1
raise AssertionError()
else:
_RES2["succ"] += 1
stdout, stderr, rc = self._test_molotov(
"--sizing",
"--console-update",
"0",
"--sizing-tolerance",
"5",
"-s",
"sizer",
"molotov.tests.test_run",
)
ratio = float(_RES2["fail"]) / float(_RES2["succ"]) * 100.0
self.assertTrue(ratio < 14.75 and ratio >= 4.75, ratio)
found = re.findall(r"obtained with (\d+) workers", stdout)
assert int(found[0]) > 50
@unittest.skipIf(os.name == "nt", "win32")
@dedicatedloop
def test_sizing_multiprocess(self):
counters = Counters("OK", "FAILED")
with catch_sleep():
@scenario()
async def sizer(session):
if random.randint(0, 10) == 1:
counters["FAILED"] += 1
raise AssertionError()
else:
counters["OK"] += 1
with set_args(
"molotov",
"--sizing",
"-p",
"2",
"--sizing-tolerance",
"5",
"--console-update",
"0",
"-s",
"sizer",
"molotov.tests.test_run",
) as (stdout, stderr):
try:
main()
except SystemExit:
pass
stdout, stderr = stdout.read().strip(), stderr.read().strip()
# stdout, stderr, rc = self._test_molotov()
ratio = (
float(counters["FAILED"].value) / float(counters["OK"].value) * 100.0
)
self.assertTrue(ratio >= 4.75, ratio)
@co_catch_output
@unittest.skipIf(os.name == "nt", "win32")
@dedicatedloop_noclose
@pytest.mark.skip(reason="This test is not reliable")
def test_statsd_multiprocess(self):
@scenario()
async def staty(session):
get_context(session).statsd.increment("yopla")
udp_proc, udp_port, udp_conn = run_server()
args = self._get_args(udp_port)
args.verbose = 2
args.processes = 2
args.max_runs = 5
args.duration = 1000
args.statsd = True
args.single_mode = "staty"
args.scenario = "molotov.tests.test_run"
stream = io.StringIO()
run(args, stream=stream)
received = stop_server(udp_proc, udp_conn)
# two processes making 5 run each
# we want at least 5 here
self.assertTrue(len(received) > 5)
stream.seek(0)
output = stream.read()
self.assertTrue("Happy breaking!" in output, output)
@dedicatedloop
def test_timed_sizing(self):
_RES2["fail"] = 0
_RES2["succ"] = 0
_RES2["messed"] = False
with catch_sleep():
@scenario()
async def sizer(session):
if get_context(session).worker_id == 200 and not _RES2["messed"]:
# worker 2 will mess with the timer
# since we're faking all timers, the current
# time in the test is always around 0
# so to have now() - get_timer() > 60
# we need to set a negative value here
# to trick it
set_timer(-61)
_RES2["messed"] = True
_RES2["fail"] = _RES2["succ"] = 0
if get_context(session).worker_id > 100:
# starting to introduce errors passed the 100th
if random.randint(0, 10) == 1:
_RES2["fail"] += 1
raise AssertionError()
else:
_RES2["succ"] += 1
# forces a switch
await asyncio.sleep(0)
stdout, stderr, rc = self._test_molotov(
"--sizing",
"--sizing-tolerance",
"5",
"--console-update",
"0",
"-cs",
"sizer",
"molotov.tests.test_run",
)
ratio = float(_RES2["fail"]) / float(_RES2["succ"]) * 100.0
self.assertTrue(ratio < 20.0 and ratio > 4.75, ratio)
@unittest.skipIf(os.name == "nt", "win32")
@dedicatedloop
def _test_sizing_multiprocess_interrupted(self):
counters = Counters("OK", "FAILED")
@scenario()
async def sizer(session):
if random.randint(0, 10) == 1:
counters["FAILED"] += 1
raise AssertionError()
else:
counters["OK"] += 1
async def _stop():
await asyncio.sleep(2.0)
os.kill(os.getpid(), signal.SIGINT)
asyncio.ensure_future(_stop())
stdout, stderr, rc = self._test_molotov(
"--sizing",
"-p",
"3",
"--sizing-tolerance",
"90",
"--console-update",
"0",
"-s",
"sizer",
"molotov.tests.test_run",
)
self.assertTrue("Sizing was not finished" in stdout)
@co_catch_output
@dedicatedloop
def test_use_extension(self):
ext = os.path.join(_HERE, "example5.py")
with coserver() as port:
@scenario(weight=10)
async def simpletest(session):
async with session.get("http://localhost:%s" % port) as resp:
assert resp.status == 200
stdout, stderr, rc = self._test_molotov(
"-cx",
"--max-runs",
"1",
"--use-extension=" + ext,
"-s",
"simpletest",
"molotov.tests.test_run",
)
self.assertTrue("=>" in stdout)
self.assertTrue("<=" in stdout)
@co_catch_output
@dedicatedloop
def test_use_extension_fail(self):
ext = os.path.join(_HERE, "exampleIDONTEXIST.py")
@scenario(weight=10)
async def simpletest(session):
async with session.get("http://localhost:8888") as resp:
assert resp.status == 200
with coserver():
stdout, stderr, rc = self._test_molotov(
"-cx",
"--max-runs",
"1",
"--use-extension=" + ext,
"-s",
"simpletest",
"molotov.tests.test_run",
)
self.assertTrue("Cannot import" in stdout)
@co_catch_output
@dedicatedloop
def test_use_extension_module_name(self):
ext = "molotov.tests.example5"
with coserver() as port:
@scenario(weight=10)
async def simpletest(session):
async with session.get(f"http://localhost:{port}") as resp:
assert resp.status == 200
stdout, stderr, rc = self._test_molotov(
"-cx",
"--max-runs",
"1",
"--use-extension=" + ext,
"-s",
"simpletest",
"molotov.tests.test_run",
)
self.assertTrue("=>" in stdout)
self.assertTrue("<=" in stdout)
@co_catch_output
@dedicatedloop
def test_use_extension_module_name_fail(self):
ext = "IDONTEXTSIST"
with coserver() as port:
@scenario(weight=10)
async def simpletest(session):
async with session.get(f"http://localhost:{port}") as resp:
assert resp.status == 200
stdout, stderr, rc = self._test_molotov(
"-cx",
"--max-runs",
"1",
"--use-extension=" + ext,
"-s",
"simpletest",
"molotov.tests.test_run",
)
self.assertTrue("Cannot import" in stdout)
@dedicatedloop
def test_quiet(self):
@scenario(weight=10)
async def here_three(session):
_RES.append(3)
stdout, stderr, rc = self._test_molotov(
"-cx", "--max-runs", "1", "-q", "-s", "here_three", "molotov.tests.test_run"
)
self.assertEqual(stdout, "")
self.assertEqual(stderr, "")
@co_catch_output
@dedicatedloop_noclose
def test_slow_server_force_shutdown(self):
@scenario(weight=10)
async def _one(session):
async with session.get("http://localhost:8888/slow") as resp:
assert resp.status == 200
_RES.append(1)
args = self._get_args()
args.duration = 0.1
args.verbose = 2
args.max_runs = 1
args.force_shutdown = True
start = time.time()
with coserver():
run(args)
# makes sure the test is stopped even if the server
# hangs a socket
self.assertTrue(time.time() - start < 4)
self.assertTrue(len(_RES) == 0)
@co_catch_output
@dedicatedloop_noclose
def test_slow_server_graceful(self):
args = self._get_args()
args.duration = 0.1
args.verbose = 2
args.max_runs = 1
# graceful shutdown on the other hand will wait
# for the worker completion
args.graceful_shutdown = True
start = time.time()
with coserver() as port:
@scenario(weight=10)
async def _one(session):
async with session.get(f"http://localhost:{port}/slow") as resp:
assert resp.status == 200
_RES.append(1)
run(args)
# makes sure the test finishes
self.assertTrue(time.time() - start > 5)
self.assertTrue(len(_RES) == 1)
@dedicatedloop
def test_single_run(self):
_RES = defaultdict(int)
with catch_sleep():
@scenario()
async def one(session):
_RES["one"] += 1
@scenario()
async def two(session):
_RES["two"] += 1
@scenario()
async def three(session):
_RES["three"] += 1
stdout, stderr, rc = self._test_molotov(
"--single-run",
"molotov.tests.test_run",
)
assert rc == 0
assert _RES["one"] == 1
assert _RES["two"] == 1
assert _RES["three"] == 1
@dedicatedloop
def _XXX_test_enable_dns(self, m_resolve):
m_resolve.return_value = ("http://localhost", "http://localhost", "localhost")
with catch_sleep():
@scenario()
async def one(session):
async with session.get("http://localhost"):
pass
stdout, stderr, rc = self._test_molotov(
"--single-run",
"molotov.tests.test_run",
)
m_resolve.assert_called()
@dedicatedloop
def xxx_test_disable_dns(self, m_resolve):
with catch_sleep():
@scenario()
async def one(session):
async with session.get("http://localhost"):
pass
stdout, stderr, rc = self._test_molotov(
"--disable-dns-resolve",
"--single-run",
"molotov.tests.test_run",
)
m_resolve.assert_not_called()
@co_catch_output
@dedicatedloop
def test_bug_121(self):
PASSED = [0]
with catch_sleep(), coserver() as port:
@scenario()
async def scenario_one(session):
cookies = {
"csrftoken": "sometoken",
"dtk": "1234",
"djdt": "hide",
"sessionid": "5678",
}
boundary = "----WebKitFormBoundaryFTE"
headers = {
"X-CSRFToken": "sometoken",
"Content-Type": f"multipart/form-data; boundary={boundary}",
}
data = json.dumps({"1": "xxx"})
with aiohttp.MultipartWriter(
"form-data", boundary=boundary
) as mpwriter:
mpwriter.append(
data,
{
"Content-Disposition": 'form-data; name="json"; filename="blob"',
"Content-Type": "application/json",
},
)
async with session.post(
"http://localhost:%s" % port,
data=mpwriter,
headers=headers,
cookies=cookies,
) as resp:
res = await resp.text()
assert data in res
PASSED[0] += 1
args = self._get_args()
args.verbose = 2
args.max_runs = 1
res = run(args)
assert PASSED[0] == 1
assert res["OK"] == 1
@co_catch_output
@dedicatedloop
def test_local_import(self):
test = os.path.join(_HERE, "example9.py")
with coserver():
stdout, stderr, rc = self._test_molotov("--max-runs", "1", test)
self.assertTrue("SUCCESSES: 1" in stdout, stdout)
@co_catch_output
@dedicatedloop
def test_grpc_factory(self):
test = os.path.join(_HERE, "example10.py")
with grpc_server():
stdout, stderr, rc = self._test_molotov("--max-runs", "1", test)
self.assertTrue("SUCCESSES: 1" in stdout, stdout)
|