def test_interrupt_on_accept():
    with TestProcess(sys.executable, '-u', HELPER, 'test_interrupt_on_accept') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, '/tmp/manhole-')
            uds_path = re.findall(r"(/tmp/manhole-\d+)", proc.read())[0]
            wait_for_strings(proc.read, TIMEOUT, 'Waiting for new connection', 'Sending signal to manhole thread', 'Waiting for new connection')
            assert_manhole_running(proc, uds_path)
Example #2
0
def test_cli(entrypoint, tmpdir):
    args = ['127.0.0.1:0', '--password', 'foobar', '--storage', str(tmpdir)]
    with TestProcess(*entrypoint.split() + args) as process:
        wait_for_strings(process.read, TIMEOUT, "server at http://")
        print(process.read())
        t = time.time()
        port = None
        while time.time() - t < TIMEOUT and port is None:
            for conn in psutil.Process(process.proc.pid).connections():
                print(conn)
                if conn.status == psutil.CONN_LISTEN and conn.laddr[
                        0] == '127.0.0.1':
                    port = conn.laddr[1]
                    break
        if port is None:
            pytest.fail("Didn't find the listen port!")
        session = requests.Session()
        resp = session.get('http://127.0.0.1:%s/' % port)
        csrftoken, = re.findall(
            'name=[\'"]csrfmiddlewaretoken[\'"] value=[\'"](.*?)[\'"]',
            resp.text)
        resp = session.post(
            'http://127.0.0.1:%s/login/?next=/redisboard/redisserver/' % port,
            data={
                'csrfmiddlewaretoken': csrftoken,
                'username': '******',
                'password': '******',
            })
        assert '<a href="/redisboard/redisserver/1/inspect/"' in resp.text
 def terminate_and_read(client):
     proc.proc.send_signal(signal.SIGINT)
     wait_for_strings(proc.read, TIMEOUT, 'Died with KeyboardInterrupt', 'DIED.')
     for _ in range(5):
         client.sock.send(b'bogus()\n')
         time.sleep(0.05)
         print(repr(client.sock.recv(1024)))
Example #4
0
def test_trash_input():
    with TestProcess(sys.executable, __file__, 'daemon',
                     'test_simple') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, '{a1}', '{b1}',
                             'RemotePdb session open at ')
            host, port = re.findall("RemotePdb session open at (.+):(.+),",
                                    proc.read())[0]
            with TestSocket(
                    socket.create_connection((host, int(port)),
                                             timeout=TIMEOUT)) as client:
                with dump_on_error(client.read):
                    wait_for_strings(proc.read, TIMEOUT,
                                     'accepted connection from')
                    wait_for_strings(client.read, TIMEOUT, "-> print('{b2}')")
                    for i in range(100):
                        client.fh.write(b'\r\n'.join(b'print("[%d]")' %
                                                     (i * 10 + j)
                                                     for j in range(10)) +
                                        b'\r\n')
                    client.fh.write(b'continue\r\n')
                    wait_for_strings(client.read, TIMEOUT,
                                     *['[%s]' % i for i in range(1000)])

            wait_for_strings(proc.read, TIMEOUT, 'DIED.')
def test_queue_collapse():
    with TestProcess(sys.executable, helper.__file__,
                     'queue_collapse') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, 'Queues =>')
            clients = []
            for _ in range(5):
                sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
                sock.settimeout(2)
                sock.connect(UDS_PATH)
                if PY3:
                    fh = sock.makefile("rwb", buffering=0)
                else:
                    fh = sock.makefile(bufsize=0)
                fh.write(b"queue_collapse\n")
                clients.append((fh, sock))
            try:
                t1 = time.time()
                for fh, _ in clients:
                    fh.readline()
                delta = time.time() - t1
                if delta > TIMEOUT:
                    raise AssertionError(
                        'Jobs took too much time (%0.2f sec)' % delta)
                wait_for_strings(
                    proc.read, TIMEOUT, 'queue_collapse OK',
                    '%s:%s' % (pwd.getpwuid(os.getuid())[0], os.getpid()))
            finally:
                [(fh.close(), sock.close()) for fh, sock in clients]
Example #6
0
def test_sample():
    with TestProcess('coverage', 'run', 'tests/nosetests.py', '--verbose',
                     '--with-html', '--html-file=sample.html',
                     'tests/test_sample.py') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, 'Ran 9 tests in')
    output = open('sample.html').read()

    assert """<tr>
                    <td>test_sample</td>
                    <td class="failed">1</td>
                    <td class="failed">1</td>
                    <td>1</td>
                    <td>1</td>
                    <td>4</td>
                </tr>""" in output
    assert """<tr>
                    <td>test_sample.MainTestCase</td>
                    <td class="failed">1</td>
                    <td>0</td>
                    <td>0</td>
                    <td>1</td>
                    <td>2</td>
                </tr>""" in output
    assert """<tr>
                    <td>test_sample.FailedSetupTestCase</td>
                    <td>0</td>
                    <td class="failed">1</td>
                    <td>0</td>
                    <td>0</td>
                    <td>1</td>
                </tr>""" in output
    assert """<tr>
                    <td>test_sample.SecondTestCase</td>
                    <td>0</td>
                    <td>0</td>
                    <td>0</td>
                    <td>2</td>
                    <td>2</td>
                </tr>""" in output
    assert """<tr>
                <td><strong>Total</strong></td>
                <td class="failed">2</td>
                <td class="failed">2</td>
                <td>1</td>
                <td>4</td>
                <td>9</td>
            </tr>""" in output

    assert "<h2>test_sample.MainTestCase (1 failures, 0 errors)</h2>" in output
    assert '<section id="test_sample.MainTestCase:test_b">' in output
    assert '<h3>test_b: <strong>' in output
    assert '<section id="test_sample:test_b">' in output
    assert '<h3>test_b: <strong>' in output
    assert '<li><a class="success">test_a</a></li>' in output
    assert '<li><a class="failed" href="#test_sample.MainTestCase:test_b">test_b</a></li>' in output
    assert '<h2>test_sample (1 failures, 1 errors)</h2>' in output
    assert '<li><a class="success">test_a</a></li>' in output
    assert '<li><a class="failed" href="#test_sample:test_b">test_b</a></li>' in output
    assert "<h2>test_sample.FailedSetupTestCase (0 failures, 1 errors)</h2>" in output
def test_oneshot_on_usr2_error():
    with TestProcess(sys.executable, '-u', HELPER,
                     'test_oneshot_on_usr2') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(
                proc.read, TIMEOUT,
                'Not patching os.fork and os.forkpty. Oneshot activation is done by signal'
            )
            pytest.raises(AssertionError, wait_for_strings, proc.read, TIMEOUT,
                          '/tmp/manhole-')
            proc.signal(signal.SIGUSR2)
            wait_for_strings(proc.read, TIMEOUT, '/tmp/manhole-')
            uds_path = re.findall(r"(/tmp/manhole-\d+)", proc.read())[0]
            wait_for_strings(proc.read, TIMEOUT, 'Waiting for new connection')
            assert_manhole_running(
                proc,
                uds_path,
                oneshot=True,
                extra=lambda client: client.sock.send(b"raise SystemExit()\n"))

            proc.reset()
            proc.signal(signal.SIGUSR2)
            wait_for_strings(proc.read, TIMEOUT, '/tmp/manhole-')
            uds_path = re.findall(r"(/tmp/manhole-\d+)", proc.read())[0]
            wait_for_strings(proc.read, TIMEOUT, 'Waiting for new connection')
            assert_manhole_running(proc, uds_path, oneshot=True)
Example #8
0
def test_uwsgi():
    with TestProcess(
        'uwsgi',
        '--master',
        '--processes', '1',
        '--no-orphans',
        '--log-5xx',
        '--single-interpreter',
        '--shared-socket', ':0',
        '--no-default-app',
        '--manage-script-name',
        '--http', '=0',
        '--mount', '=wsgi:application',
        *['--virtualenv', os.environ['VIRTUAL_ENV']] if 'VIRTUAL_ENV' in os.environ else []
    ) as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, 'uWSGI http bound')
            port = re.findall(r"uWSGI http bound on :(\d+) fd", proc.read())[0]
            assert requests.get('http://127.0.0.1:%s/' % port).text == 'OK'

            wait_for_strings(proc.read, TIMEOUT, 'spawned uWSGI worker 1')
            pid = re.findall(r"spawned uWSGI worker 1 \(pid: (\d+), ", proc.read())[0]

            for _ in range(2):
                with open('/tmp/manhole-pid', 'w') as fh:
                    fh.write(pid)
                assert_manhole_running(proc, '/tmp/manhole-%s' % pid, oneshot=True)
Example #9
0
def app_server():
    with TestProcess("python", "app.py") as app_server:
        wait_for_strings(app_server.read, 10, "Running")
        print(app_server.read())
        yield app_server
        print("\n>>>>Teardown app_service")
        app_server.close()
Example #10
0
def test_queue_collapse():
    with TestProcess(sys.executable, helper.__file__, 'queue_collapse') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, 'Queues =>')
            clients = []
            for _ in range(5):
                sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
                sock.settimeout(2)
                sock.connect(UDS_PATH)
                if PY3:
                    fh = sock.makefile("rwb", buffering=0)
                else:
                    fh = sock.makefile(bufsize=0)
                fh.write(b"queue_collapse\n")
                clients.append((fh, sock))
            try:
                t1 = time.time()
                for fh, _ in clients:
                    fh.readline()
                delta = time.time() - t1
                if delta > TIMEOUT:
                    raise AssertionError('Jobs took too much time (%0.2f sec)' % delta)
                wait_for_strings(proc.read, TIMEOUT,
                                 'queue_collapse OK',
                                 '%s:%s' % (pwd.getpwuid(os.getuid())[0], os.getpid()))
            finally:
                [(fh.close(), sock.close()) for fh, sock in clients]
def check_print_tracebacks(uds_path):
    sock = connect_to_manhole(uds_path)
    with TestSocket(sock) as client:
        with dump_on_error(client.read):
            wait_for_strings(client.read, 1, ">>>")
            sock.send(b"NO_SUCH_NAME\n")
            wait_for_strings(client.read, 1, "NameError:", "name 'NO_SUCH_NAME' is not defined", ">>>")
Example #12
0
def test_no_overlap(redis_server):
    """
    This test tries to simulate contention: lots of clients trying to acquire at the same time.

    If there would be a bug that would allow two clients to hold the lock at the same time it
    would most likely regress this test.

    The code here mostly tries to parse out the pid of the process and the time when it got and
    released the lock. If there's is overlap (eg: pid1.start < pid2.start < pid1.end) then we
    got a very bad regression on our hands ...

    The subprocess being run (check helper.py) will fork bunch of processes and will try to
    syncronize them (using the builting sched) to try to acquire the lock at the same time.
    """
    with TestProcess(sys.executable, HELPER, 'test_no_overlap') as proc:
        with dump_on_error(proc.read):
            name = 'lock:foobar'
            wait_for_strings(proc.read, 10 * TIMEOUT, 'Getting %r ...' % name)
            wait_for_strings(proc.read, 10 * TIMEOUT,
                             'Got lock for %r.' % name)
            wait_for_strings(proc.read, 10 * TIMEOUT, 'Releasing %r.' % name)
            wait_for_strings(proc.read, 10 * TIMEOUT,
                             'UNLOCK_SCRIPT not cached.')
            wait_for_strings(proc.read, 10 * TIMEOUT, 'DIED.')

            class Event(object):
                pid = start = end = '?'

                def __str__(self):
                    return "Event(%s; %r => %r)" % (self.pid, self.start,
                                                    self.end)

            events = defaultdict(Event)
            for line in proc.read().splitlines():
                try:
                    pid, time, junk = line.split(' ', 2)
                    pid = int(pid)
                except ValueError:
                    continue
                if 'Got lock for' in junk:
                    events[pid].pid = pid
                    events[pid].start = time
                if 'Releasing' in junk:
                    events[pid].pid = pid
                    events[pid].end = time
            assert len(events) == 125

            # not very smart but we don't have millions of events so it's
            # ok - compare all the events with all the other events:
            for event in events.values():
                for other in events.values():
                    if other is not event:
                        try:
                            if (other.start < event.start < other.end
                                    or other.start < event.end < other.end):
                                pytest.fail('%s overlaps %s' % (event, other))
                        except Exception:
                            print("[%s/%s]" % (event, other))
                            raise
Example #13
0
def test_custom_exit_code():
    with TestProcess(sys.executable, helper.__file__, 'custom_exit_code') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, 'Queues =>')
            with connection(3) as fh:
                fh.write(b"asdf\n")
                line = fh.readline()
                json.loads(line.decode('ascii'))["exit_code"] == 123
Example #14
0
def test_activate_on_with_oneshot_on():
    with TestProcess(sys.executable, '-u', __file__, 'daemon',
                     'test_activate_on_with_oneshot_on') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(
                proc.read, TIMEOUT,
                "RuntimeError('You cannot do activation of the Manhole thread on the same signal that you want to do oneshot activation !')"
            )
Example #15
0
 def terminate_and_read(client):
     proc.proc.send_signal(signal.SIGINT)
     wait_for_strings(proc.read, TIMEOUT,
                      'Died with KeyboardInterrupt', 'DIED.')
     for _ in range(5):
         client.sock.send(b'bogus()\n')
         time.sleep(0.05)
         print(repr(client.sock.recv(1024)))
def test_no_block(conn):
    with Lock(conn, "foobar"):
        with TestProcess(sys.executable, HELPER, "test_no_block") as proc:
            with dump_on_error(proc.read):
                name = "lock:foobar"
                wait_for_strings(
                    proc.read, TIMEOUT, "Getting %r ..." % name, "Failed to get %r." % name, "acquire=>False", "DIED."
                )
Example #17
0
def check_locals(uds_path):
    sock = connect_to_manhole(uds_path)
    with TestSocket(sock) as client:
        with dump_on_error(client.read):
            wait_for_strings(client.read, TIMEOUT, ">>>")
            sock.send(b"from __future__ import print_function\n"
                      b"print(k1, k2)\n")
            wait_for_strings(client.read, TIMEOUT, "v1 v2")
Example #18
0
def redis_server(scope='module'):
    try:
        os.unlink(UDS_PATH)
    except OSError:
        pass
    with TestProcess('redis-server', '--port', '0', '--unixsocket', UDS_PATH) as process:
        wait_for_strings(process.read, TIMEOUT, "Running")
        yield process
def redis_server(scope="module"):
    try:
        os.unlink(UDS_PATH)
    except OSError:
        pass
    with TestProcess("redis-server", "--port", "0", "--unixsocket", UDS_PATH) as process:
        wait_for_strings(process.read, TIMEOUT, "Running")
        yield process
Example #20
0
def check_print_tracebacks(uds_path):
    sock = connect_to_manhole(uds_path)
    with TestSocket(sock) as client:
        with dump_on_error(client.read):
            wait_for_strings(client.read, 1, ">>>")
            sock.send(b"NO_SUCH_NAME\n")
            wait_for_strings(client.read, 1, "NameError:",
                             "name 'NO_SUCH_NAME' is not defined", ">>>")
Example #21
0
def test_activate_on_with_oneshot_on():
    with TestProcess(sys.executable, '-u', HELPER,
                     'test_activate_on_with_oneshot_on') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(
                proc.read, TIMEOUT,
                "You cannot do activation of the Manhole thread on the same signal that you want to do "
                "oneshot activation !")
def check_locals(uds_path):
    sock = connect_to_manhole(uds_path)
    with TestSocket(sock) as client:
        with dump_on_error(client.read):
            wait_for_strings(client.read, 1, ">>>")
            sock.send(b"from __future__ import print_function\n"
                      b"print(k1, k2)\n")
            wait_for_strings(client.read, 1, "v1 v2")
def test_no_overlap(redis_server):
    """
    This test tries to simulate contention: lots of clients trying to acquire at the same time.

    If there would be a bug that would allow two clients to hold the lock at the same time it
    would most likely regress this test.

    The code here mostly tries to parse out the pid of the process and the time when it got and
    released the lock. If there's is overlap (eg: pid1.start < pid2.start < pid1.end) then we
    got a very bad regression on our hands ...

    The subprocess being run (check helper.py) will fork bunch of processes and will try to
    syncronize them (using the builting sched) to try to acquire the lock at the same time.
    """
    with TestProcess(sys.executable, HELPER, 'test_no_overlap') as proc:
        with dump_on_error(proc.read):
            name = 'lock:foobar'
            wait_for_strings(proc.read, 10 * TIMEOUT, 'Getting %r ...' % name)
            wait_for_strings(proc.read, 10 * TIMEOUT, 'Got lock for %r.' % name)
            wait_for_strings(proc.read, 10 * TIMEOUT, 'Releasing %r.' % name)
            wait_for_strings(proc.read, 10 * TIMEOUT, 'UNLOCK_SCRIPT not cached.')
            wait_for_strings(proc.read, 10 * TIMEOUT, 'DIED.')

            class Event(object):
                pid = start = end = '?'

                def __str__(self):
                    return "Event(%s; %r => %r)" % (self.pid, self.start, self.end)

            events = defaultdict(Event)
            for line in proc.read().splitlines():
                try:
                    pid, time, junk = line.split(' ', 2)
                    pid = int(pid)
                except ValueError:
                    continue
                if 'Got lock for' in junk:
                    events[pid].pid = pid
                    events[pid].start = time
                if 'Releasing' in junk:
                    events[pid].pid = pid
                    events[pid].end = time
            assert len(events) == 125

            # not very smart but we don't have millions of events so it's
            # ok - compare all the events with all the other events:
            for event in events.values():
                for other in events.values():
                    if other is not event:
                        try:
                            if (
                                other.start < event.start < other.end or
                                other.start < event.end < other.end
                            ):
                                pytest.fail('%s overlaps %s' % (event, other))
                        except Exception:
                            print("[%s/%s]" % (event, other))
                            raise
Example #24
0
def test_cover_looponfail(testdir, monkeypatch):
    testdir.makepyfile(mod=MODULE)
    testdir.makeconftest(CONFTEST)
    script = testdir.makepyfile(BASIC_TEST)

    monkeypatch.setattr(testdir, "run", lambda *args: TestProcess(*map(str, args)))
    with testdir.runpytest("-v", "--cov=%s" % script.dirpath(), "--looponfail", script) as process:
        with dump_on_error(process.read):
            wait_for_strings(process.read, 30, "Stmts   Miss  Cover")  # 30 seconds
Example #25
0
def test_locmem():
    with TestProcess('django-admin.py',
                     'runserver', '127.0.0.1:0', '--traceback', '--noreload', '--nothreading',
                     env=dict(os.environ, UWSGI_CACHE_FALLBACK='y')) as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, '127.0.0.1')
            port = get_ports(proc.proc.pid)
            url = "http://127.0.0.1:%s" % port
            assertions(url)
def test_sample():
    with TestProcess(
        'coverage', 'run', 'tests/nosetests.py', '--verbose', '--with-html', '--html-file=sample.html',
        'tests/test_sample.py'
    ) as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, 'Ran 9 tests in')
    output = open('sample.html').read()

    assert """<tr>
                    <td>test_sample</td>
                    <td>1</td>
                    <td>1</td>
                    <td>1</td>
                    <td>4</td>
                </tr>""" in output
    assert """<tr>
                    <td>test_sample.MainTestCase</td>
                    <td>1</td>
                    <td>0</td>
                    <td>1</td>
                    <td>2</td>
                </tr>""" in output
    assert """<tr>
                    <td>test_sample.FailedSetupTestCase</td>
                    <td>0</td>
                    <td>0</td>
                    <td>0</td>
                    <td>1</td>
                </tr>""" in output
    assert """<tr>
                    <td>test_sample.SecondTestCase</td>
                    <td>0</td>
                    <td>0</td>
                    <td>2</td>
                    <td>2</td>
                </tr>""" in output
    assert """<tr>
                <td><strong>Total</strong></td>
                <td>2</td>
                <td>1</td>
                <td>4</td>
                <td>9</td>
            </tr>""" in output


    assert "<h2>test_sample.MainTestCase (1 failures, 0 errors)</h2>" in output
    assert '<section id="test_sample.MainTestCase:test_b">' in output
    assert '<h3>test_b: <strong>' in output
    assert '<section id="test_sample:test_b">' in output
    assert '<h3>test_b: <strong>' in output
    assert '<li><a class="success">test_a</a></li>' in output
    assert '<li><a class="failed" href="#test_sample.MainTestCase:test_b">test_b</a></li>' in output
    assert '<h2>test_sample (1 failures, 1 errors)</h2>' in output
    assert '<li><a class="success">test_a</a></li>' in output
    assert '<li><a class="failed" href="#test_sample:test_b">test_b</a></li>' in output
    assert "<h2>test_sample.FailedSetupTestCase (0 failures, 1 errors)</h2>" in output
def test_custom_exit_code():
    with TestProcess(sys.executable, helper.__file__,
                     'custom_exit_code') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, 'Queues =>')
            with connection(3) as fh:
                fh.write(b"asdf\n")
                line = fh.readline()
                json.loads(line.decode('ascii'))["exit_code"] == 123
def test_simple():
    with TestProcess(sys.executable, HELPER, 'test_simple') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, '/tmp/manhole-')
            uds_path = re.findall(r"(/tmp/manhole-\d+)", proc.read())[0]
            wait_for_strings(proc.read, TIMEOUT, 'Waiting for new connection')
            for _ in range(20):
                proc.reset()
                assert_manhole_running(proc, uds_path)
Example #29
0
def test_simple():
    with TestProcess(sys.executable, HELPER, 'test_simple') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, '/tmp/manhole-')
            uds_path = re.findall(r"(/tmp/manhole-\d+)", proc.read())[0]
            wait_for_strings(proc.read, TIMEOUT, 'Waiting for new connection')
            for _ in range(20):
                proc.reset()
                assert_manhole_running(proc, uds_path)
Example #30
0
def test_interrupt_on_accept():
    with TestProcess(sys.executable, '-u', __file__, 'daemon',
                     'test_interrupt_on_accept') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, '/tmp/manhole-')
            uds_path = re.findall(r"(/tmp/manhole-\d+)", proc.read())[0]
            wait_for_strings(proc.read, TIMEOUT, 'Waiting for new connection',
                             'Sending signal to manhole thread',
                             'Waiting for new connection')
            assert_manhole_running(proc, uds_path)
Example #31
0
def test_simple(redis_server):
    with TestProcess(sys.executable, HELPER, 'test_simple') as proc:
        with dump_on_error(proc.read):
            name = 'lock:foobar'
            wait_for_strings(proc.read, TIMEOUT,
                'Getting %r ...' % name,
                'Got lock for %r.' % name,
                'Releasing %r.' % name,
                'UNLOCK_SCRIPT not cached.',
                'DIED.',
            )
Example #32
0
def test_timeout_acquired(conn):
    with TestProcess(sys.executable, HELPER, 'test_timeout') as proc:
        with dump_on_error(proc.read):
            name = 'lock:foobar'
            wait_for_strings(
                proc.read, TIMEOUT,
                'Getting %r ...' % name,
                'Got lock for %r.' % name,
            )
            lock = Lock(conn, "foobar")
            assert lock.acquire(timeout=2)
Example #33
0
def test_no_block(redis_server):
    with Lock(StrictRedis(unix_socket_path=UDS_PATH), "foobar"):
        with TestProcess(sys.executable, HELPER, 'test_no_block') as proc:
            with dump_on_error(proc.read):
                name = 'lock:foobar'
                wait_for_strings(proc.read, TIMEOUT,
                    'Getting %r ...' % name,
                    'Failed to get %r.' % name,
                    'acquire=>False',
                    'DIED.',
                )
Example #34
0
def redis_process():
    socket_path = tempfile.mktemp('.sock')
    try:
        with TestProcess('redis-server', '--port', '0', '--unixsocket', socket_path) as process:
            wait_for_strings(process.read, TIMEOUT, "Running")
            yield socket_path
    finally:
        try:
            os.unlink(socket_path)
        except Exception:
            pass
def test_timeout_acquired(conn):
    with TestProcess(sys.executable, HELPER, 'test_timeout') as proc:
        with dump_on_error(proc.read):
            name = 'lock:foobar'
            wait_for_strings(
                proc.read, TIMEOUT,
                'Getting %r ...' % name,
                'Got lock for %r.' % name,
            )
            lock = Lock(conn, "foobar")
            assert lock.acquire(timeout=2)
Example #36
0
def test_prespawned():
    with TestProcess(sys.executable, helper.__file__, 'simple') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, 'Queues =>')
            response = client.request(helper.PATH, b"foobar")
            assert response.exit_code == 0
            wait_for_strings(proc.read, TIMEOUT,
                             '%s:%s' % (pwd.getpwuid(os.getuid())[0], os.getpid()),
                             'JOB foobar EXECUTED',
                             'completed. Passing back results to',
                             'Queues => 0 workspaces')
Example #37
0
def redis_process():
    socket_path = tempfile.mktemp('.sock')
    try:
        with TestProcess('redis-server', '--port', '0', '--unixsocket', socket_path) as process:
            wait_for_strings(process.read, TIMEOUT, "Running")
            yield socket_path
    finally:
        try:
            os.unlink(socket_path)
        except Exception:
            pass
Example #38
0
def test_incomplete_request():
    with TestProcess(sys.executable, helper.__file__, 'simple') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, 'Queues =>')
            with connection(2) as fh:
                fh.write(b"first")
                line = fh.readline()
                assert line == b''
                wait_for_strings(proc.read, TIMEOUT,
                                 'Failed to read request from client %s:%s' % (
                                     pwd.getpwuid(os.getuid())[0], os.getpid()))
Example #39
0
def test_no_block(conn):
    with Lock(conn, "foobar"):
        with TestProcess(sys.executable, HELPER, 'test_no_block') as proc:
            with dump_on_error(proc.read):
                name = 'lock:foobar'
                wait_for_strings(
                    proc.read, TIMEOUT,
                    'Getting %r ...' % name,
                    'Failed to get %r.' % name,
                    'acquire=>False',
                    'DIED.',
                )
def test_incomplete_request():
    with TestProcess(sys.executable, helper.__file__, 'simple') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, 'Queues =>')
            with connection(2) as fh:
                fh.write(b"first")
                line = fh.readline()
                assert line == b''
                wait_for_strings(
                    proc.read, TIMEOUT,
                    'Failed to read request from client %s:%s' %
                    (pwd.getpwuid(os.getuid())[0], os.getpid()))
Example #41
0
def test_uwsgi():
    with TestProcess('uwsgi',
                     '--http-socket', '127.0.0.1:0',
                     '--module', 'test_project.wsgi',
                     '--master',
                     '--cache-expire-freq', '1',
                     '--cache2', 'name=foobar,items=20') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, 'bound to TCP address 127.0.0.1')
            url, = re.findall(r"bound to TCP address (127.0.0.1:\d+) ", proc.read())
            url = "http://" + url
            assertions(url)
def test_no_block(conn):
    with Lock(conn, "foobar"):
        with TestProcess(sys.executable, HELPER, 'test_no_block') as proc:
            with dump_on_error(proc.read):
                name = 'lock:foobar'
                wait_for_strings(
                    proc.read, TIMEOUT,
                    'Getting %r ...' % name,
                    'Failed to get %r.' % name,
                    'acquire=>False',
                    'DIED.',
                )
Example #43
0
def test_interrupt_on_accept():
    with TestProcess(sys.executable, '-u', HELPER,
                     'test_interrupt_on_accept') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, '/tmp/manhole-')
            uds_path = re.findall(r"(/tmp/manhole-\d+)", proc.read())[0]
            only_on_old_python = ['Waiting for new connection'
                                  ] if sys.version_info < (3, 5) else []
            wait_for_strings(proc.read, TIMEOUT, 'Waiting for new connection',
                             'Sending signal to manhole thread',
                             *only_on_old_python)
            assert_manhole_running(proc, uds_path)
def test_uwsgi():
    with TestProcess('uwsgi',
                     '--http-socket', '127.0.0.1:0',
                     '--module', 'test_project.wsgi',
                     '--cache2', 'name=foobar,items=10') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, 'bound to TCP address 127.0.0.1')
            url, = re.findall(r"bound to TCP address (127.0.0.1:\d+) ", proc.read())
            url = "http://" + url
            assert requests.get(url + '/set/1/2').text == 'ok'
            assert requests.get(url + '/get/1').text == '2'
            assert requests.get(url + '/clear').text == 'ok'
            assert requests.get(url + '/get/1').text == 'None'
def test_timeout():
    with TestProcess(sys.executable, helper.__file__, 'timeout') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, 'Queues =>')
            with connection(3) as fh:
                fh.write(b"foobar\n")
                line = fh.readline()
                json.loads(line.decode('ascii'))["exit_code"] == 14
                wait_for_strings(
                    proc.read, TIMEOUT,
                    '%s:%s' % (pwd.getpwuid(os.getuid())[0], os.getpid()),
                    'timeout STARTED', 'completed. Passing back results to')
                assert 'timeout FAIL' not in proc.read()
def test_fail():
    with TestProcess(sys.executable, helper.__file__, 'fail') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, 'Queues =>')
            with connection() as fh:
                fh.write(b"first")
                fh.write(b"-second\n")
                line = fh.readline()
                assert b'"exit_code": 255' in line
                wait_for_strings(
                    proc.read, TIMEOUT,
                    '%s:%s' % (pwd.getpwuid(os.getuid())[0], os.getpid()),
                    'Failed task', 'Exception: FAIL', 'Queues => 0 workspaces')
Example #47
0
def test_timeout():
    with TestProcess(sys.executable, helper.__file__, 'timeout') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, 'Queues =>')
            with connection(3) as fh:
                fh.write(b"foobar\n")
                line = fh.readline()
                json.loads(line.decode('ascii'))["exit_code"] == 14
                wait_for_strings(proc.read, TIMEOUT,
                                 '%s:%s' % (pwd.getpwuid(os.getuid())[0], os.getpid()),
                                 'timeout STARTED',
                                 'completed. Passing back results to')
                assert 'timeout FAIL' not in proc.read()
def test_simple(redis_server):
    with TestProcess(sys.executable, HELPER, "test_simple") as proc:
        with dump_on_error(proc.read):
            name = "lock:foobar"
            wait_for_strings(
                proc.read,
                TIMEOUT,
                "Getting %r ..." % name,
                "Got lock for %r." % name,
                "Releasing %r." % name,
                "UNLOCK_SCRIPT not cached.",
                "DIED.",
            )
Example #49
0
def test_simple():
    with TestProcess(sys.executable, helper.__file__, 'simple') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, 'Queues =>')
            with connection() as fh:
                fh.write(b"first")
                fh.write(b"-second\n")
                line = fh.readline()
                assert b'"exit_code": 0' in line
                wait_for_strings(proc.read, TIMEOUT,
                                 '%s:%s' % (pwd.getpwuid(os.getuid())[0], os.getpid()),
                                 'JOB first-second EXECUTED',
                                 'completed. Passing back results to',
                                 'Queues => 0 workspaces')
Example #50
0
def test_fail():
    with TestProcess(sys.executable, helper.__file__, 'fail') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, 'Queues =>')
            with connection() as fh:
                fh.write(b"first")
                fh.write(b"-second\n")
                line = fh.readline()
                assert b'"exit_code": 255' in line
                wait_for_strings(proc.read, TIMEOUT,
                                 '%s:%s' % (pwd.getpwuid(os.getuid())[0], os.getpid()),
                                 'Failed task',
                                 'Exception: FAIL',
                                 'Queues => 0 workspaces')
def test_uwsgi():
    with TestProcess('uwsgi', '--http-socket', '127.0.0.1:0', '--module',
                     'test_project.wsgi', '--cache2',
                     'name=foobar,items=10') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT,
                             'bound to TCP address 127.0.0.1')
            url, = re.findall(r"bound to TCP address (127.0.0.1:\d+) ",
                              proc.read())
            url = "http://" + url
            assert requests.get(url + '/set/1/2').text == 'ok'
            assert requests.get(url + '/get/1').text == '2'
            assert requests.get(url + '/clear').text == 'ok'
            assert requests.get(url + '/get/1').text == 'None'
Example #52
0
def assert_manhole_running(proc, uds_path, oneshot=False, extra=None):
    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    sock.settimeout(0.5)
    for i in range(TIMEOUT):
        try:
            sock.connect(uds_path)
            break
        except Exception as exc:
            print('Failed to connect to %s: %s' % (uds_path, exc))
            time.sleep(1)
            if i + 1 == TIMEOUT:
                raise
    try:
        with TestSocket(sock) as client:
            with dump_on_error(client.read):
                wait_for_strings(client.read, TIMEOUT, "ProcessID", "ThreadID", ">>>")
                sock.send(b"print('FOOBAR')\n")
                wait_for_strings(client.read, TIMEOUT, "FOOBAR")
                wait_for_strings(proc.read, TIMEOUT, 'UID:%s' % os.getuid())
                if extra:
                    extra(sock)
                sock.shutdown(socket.SHUT_RDWR)
    finally:
        sock.close()
    wait_for_strings(proc.read, TIMEOUT, 'Cleaned up.', *[] if oneshot else ['Waiting for new connection'])
def test_socket_path_with_fork():
    with TestProcess(sys.executable, '-u', HELPER, 'test_socket_path_with_fork') as proc:
        with dump_on_error(proc.read):
            wait_for_strings(proc.read, TIMEOUT, 'Not patching os.fork and os.forkpty. Using user socket path')
            wait_for_strings(proc.read, TIMEOUT, 'Waiting for new connection')
            sock = connect_to_manhole(SOCKET_PATH)
            with TestSocket(sock) as client:
                with dump_on_error(client.read):
                    wait_for_strings(client.read, TIMEOUT, "ProcessID", "ThreadID", ">>>")
                    sock.send(b"print('BEFORE FORK')\n")
                    wait_for_strings(client.read, TIMEOUT, "BEFORE FORK")
                    time.sleep(2)
                    sock.send(b"print('AFTER FORK')\n")
                    wait_for_strings(client.read, TIMEOUT, "AFTER FORK")
Example #54
0
def test_cover_looponfail(testdir, monkeypatch):
    testdir.makepyfile(mod=MODULE)
    testdir.makeconftest(CONFTEST)
    script = testdir.makepyfile(BASIC_TEST)

    monkeypatch.setattr(testdir, 'run',
                        lambda *args: _TestProcess(*map(str, args)))
    with testdir.runpytest('-v', '--cov=%s' % script.dirpath(), '--looponfail',
                           script) as process:
        with dump_on_error(process.read):
            wait_for_strings(
                process.read,
                30,  # 30 seconds
                'Stmts   Miss  Cover')