Пример #1
0
def test_wait_for_status():
    image = DockerImage(FEDORA_MINIMAL_REPOSITORY,
                        tag=FEDORA_MINIMAL_REPOSITORY_TAG)
    cmd = DockerRunBuilder(command=['sleep', '2'])
    cont = image.run_via_binary(cmd)

    start = time.time()
    p = Probe(timeout=6, fnc=cont.get_status, expected_retval='exited')
    p.run()
    end = time.time() - start
    assert end > 2, "Probe should wait till container status is exited"
    assert end < 7, "Probe should end when container status is exited"
Пример #2
0
def test_exit_code():
    image = DockerImage(FEDORA_MINIMAL_REPOSITORY,
                        tag=FEDORA_MINIMAL_REPOSITORY_TAG)
    cmd = DockerRunBuilder(command=['sleep', '2'])
    cont = image.run_via_binary(cmd)
    assert cont.is_running() and cont.exit_code() == 0
    p = Probe(timeout=5, fnc=cont.get_status, expected_retval='exited')
    p.run()

    assert not cont.is_running() and cont.exit_code() == 0
    cmd = DockerRunBuilder(command=['bash', '-c', "exit 42"])
    cont = image.run_via_binary(cmd)
    cont.wait()
    assert cont.exit_code() == 42
Пример #3
0
def test_wait_for_status():
    with DockerBackend() as backend:
        image = backend.ImageClass(FEDORA_MINIMAL_REPOSITORY, tag=FEDORA_MINIMAL_REPOSITORY_TAG)
        cmd = ['sleep', '2']
        cont = image.run_via_binary(command=cmd)

        try:
            start = time.time()
            p = Probe(timeout=6, fnc=cont.get_status, expected_retval='exited')
            p.run()
            end = time.time() - start
            assert end > 2, "Probe should wait till container status is exited"
            assert end < 7, "Probe should end when container status is exited"
        finally:
            cont.delete(force=True)
Пример #4
0
def test_probes_port():
    port = 1234
    host = "127.0.0.1"
    probe = Probe(timeout=20, fnc=check_port, host=host, port=port)
    assert not check_port(host=host, port=port)

    bckgrnd = subprocess.Popen(["nc", "-l", str(port)], stdout=subprocess.PIPE)
    assert probe.run()
    assert not check_port(host=host, port=port)
    bckgrnd.kill()
    assert not check_port(host=host, port=port)

    subprocess.Popen(["nc", "-l", str(port)], stdout=subprocess.PIPE)
    assert probe.run()
    assert not check_port(host=host, port=port)
Пример #5
0
    def test_reach_timeout(self):
        # probe should reach timeout with long-running function calls
        # probe and caller in one thread
        start = time.time()
        probe = Probe(timeout=1, pause=0.2, fnc=snoozer, seconds=10)
        with pytest.raises(ProbeTimeout):
            probe.run()
        assert (time.time() - start) < 3, "Time elapsed is way too high"

        # in background
        start = time.time()
        probe = Probe(timeout=1, pause=0.2, fnc=snoozer, seconds=10)
        probe.run_in_background()
        with pytest.raises(ProbeTimeout):
            probe.join()
        assert (time.time() - start) < 3, "Time elapsed is way too high"
Пример #6
0
    def test_concurrency(self):
        pool = []
        for i in range(3):
            probe = Probe(timeout=10, fnc=snoozer, seconds=3)
            probe.run_in_background()
            pool.append(probe)

        for p in pool:
            assert p.is_alive()

        for p in pool:
            p.terminate()
            p.join()

        for p in pool:
            assert not p.is_alive()
Пример #7
0
    def test_reach_timeout(self):
        # probe should reach timeout with long-running function calls
        # probe and caller in one thread
        start = time.time()
        probe = Probe(timeout=3, pause=0.5, fnc=snoozer, seconds=10)
        with pytest.raises(ProbeTimeout):
            probe.run()
        assert (time.time() -
                start) > 2, "Timeout not reached with unsuccessful function"

        # in background
        start = time.time()
        probe = Probe(timeout=3, pause=0.5, fnc=snoozer, seconds=10)
        probe.run_in_background()
        with pytest.raises(ProbeTimeout):
            probe.join()
        assert (time.time() -
                start) > 2, "Timeout not reached with unsuccessful function"
Пример #8
0
def test_exit_code():
    with DockerBackend() as backend:
        image = backend.ImageClass(FEDORA_MINIMAL_REPOSITORY, tag=FEDORA_MINIMAL_REPOSITORY_TAG)
        cmd = ['sleep', '2']
        cont = image.run_via_binary(command=cmd)
        try:
            assert cont.is_running() and cont.exit_code() == 0
            p = Probe(timeout=5, fnc=cont.get_status, expected_retval='exited')
            p.run()
            assert not cont.is_running() and cont.exit_code() == 0
        finally:
            cont.delete(force=True)

        cmd = ['bash', '-c', "exit 42"]
        cont = image.run_via_binary(command=cmd)
        try:
            cont.wait()
            assert cont.exit_code() == 42
        finally:
            cont.delete(force=True)
Пример #9
0
def test_container_logs():
    with DockerBackend() as backend:
        image = backend.ImageClass(FEDORA_MINIMAL_REPOSITORY, tag=FEDORA_MINIMAL_REPOSITORY_TAG)
        command = ["bash", "-c", "for x in `seq 1 5`; do echo $x; done"]
        cont = image.run_via_binary(command=command)
        try:
            Probe(timeout=5, fnc=cont.get_status, expected_retval='exited').run()
            assert not cont.is_running()
            assert list(cont.logs()) == [b"1\n", b"2\n", b"3\n", b"4\n", b"5\n"]
            assert cont.logs_unicode() == "1\n2\n3\n4\n5\n"
            assert cont.logs_in_bytes() == b"1\n2\n3\n4\n5\n"
        finally:
            cont.delete(force=True)
def assert_container_creation_fails(opts, description):
    cmd = DockerRunBuilder(additional_opts=opts)
    image = get_image()
    cont = image.run_via_binary(cmd)

    p = Probe(timeout=9, fnc=cont.get_status, expected_retval='exited')
    try:
        p.run()
    except ProbeTimeout:
        actual_status = cont.get_status()
        cont.stop()
        cont.delete()
        if actual_status == 'running':
            raise RuntimeError("Container should fail with %s" % description)
        else:
            raise RuntimeError("Container reached unexpected status %s" %
                               actual_status)

    ec = cont.get_metadata()['State']['ExitCode']
    print(cont.logs())
    assert ec != 0, "Container should exit wit non-zero exit code when input is invalid"
    cont.delete()
Пример #11
0
    def test_arguments(self):
        def check_arg(arg=""):
            return arg == ARGUMENT

        # probe should be able to pass arguments to function
        start = time.time()
        probe = Probe(timeout=5, pause=0.5, fnc=check_arg, arg=ARGUMENT)
        probe.run()
        assert (time.time() - start) < 1, "Timeout exceeded"

        start = time.time()
        probe = Probe(timeout=3, pause=0.5, fnc=check_arg, arg="devil")
        with pytest.raises(ProbeTimeout):
            probe.run()
        assert (time.time() - start) > 2, "Timeout not reached with unsuccessful function"
Пример #12
0
    def test_expected_retval(self):
        def truth():
            return MESSAGE

        def lie():
            return "Box is empty"

        # probe should end when expected_retval is reached
        start = time.time()
        probe = Probe(timeout=5, pause=0.5, fnc=truth, expected_retval=MESSAGE)
        probe.run()
        assert (time.time() - start) < 1.0, "Timeout exceeded"

        # probe should reach timeout when expected_retval is not reached
        start = time.time()
        probe = Probe(timeout=3, pause=0.5, fnc=lie, expected_retval=MESSAGE)
        with pytest.raises(ProbeTimeout):
            probe.run()
        assert (time.time() - start) > 2.0, "Timeout not reached with unsuccessful function"
Пример #13
0
    def test_usage(self):
        i = S2IDockerImage(image_name, tag=image_tag)
        c = i.run_via_binary()

        def logs_received():
            return len(list(c.logs())) > 0

        try:
            c.wait()
            # even after waiting there is still a race in journal logging driver
            Probe(timeout=10, pause=0.05, count=20, fnc=logs_received).run()
            logs = [x.decode("utf-8") for x in c.logs()]
            logs = "\n".join(logs).strip()
            usage = i.usage()
            # FIXME: workaround: `docker logs` can't handle logs like these: '\n\n\n'
            assert logs.replace("\n", "") == usage.replace("\n", "")
        finally:
            c.delete()
Пример #14
0
    def test_exception(self):

        # probe and caller in one thread
        # probe should ignore expected_exceptions
        probe = Probe(timeout=1,
                      pause=0.2,
                      expected_exceptions=ValueError,
                      fnc=value_err_raise)
        with pytest.raises(ProbeTimeout):
            probe.run()

        # probe should not ignore exceptions other than expected exceptions
        probe = Probe(timeout=1,
                      pause=0.2,
                      expected_exceptions=ImportError,
                      fnc=value_err_raise)
        with pytest.raises(ValueError):
            probe.run()

        probe = Probe(timeout=1, pause=0.2, fnc=value_err_raise)
        with pytest.raises(ValueError):
            probe.run()

        # run in background
        # probe should ignore expected_exceptions
        probe = Probe(timeout=1,
                      pause=0.2,
                      expected_exceptions=ValueError,
                      fnc=value_err_raise)
        probe.run_in_background()
        with pytest.raises(ProbeTimeout):
            probe.join()

        # probe should not ignore exceptions other than expected exceptions
        probe = Probe(timeout=1,
                      pause=0.2,
                      expected_exceptions=ImportError,
                      fnc=value_err_raise)
        probe.run_in_background()
        with pytest.raises(ValueError):
            probe.join()

        probe = Probe(timeout=1, pause=0.2, fnc=value_err_raise)
        probe.run_in_background()
        with pytest.raises(ValueError):
            probe.join()
Пример #15
0
    def test_in_backgroud(self):
        probe = Probe(timeout=1, pause=0.5, fnc=snoozer, seconds=0.1)

        probe.run()
        assert not probe.is_alive()

        probe.run_in_background()
        assert probe.is_alive()
        probe.terminate()
        probe.join()
        assert not probe.is_alive()
Пример #16
0
    def test_count(self):
        def say_no():
            time.sleep(1)
            return False

        start = time.time()
        probe = Probe(timeout=5, count=1, pause=0.5, fnc=say_no)
        with pytest.raises(CountExceeded):
            probe.run()
        assert (time.time() -
                start) < 2, "Probe should end after one allowed try"

        start = time.time()
        probe = Probe(timeout=3, count=10, pause=0.5, fnc=say_no)
        with pytest.raises(ProbeTimeout):
            probe.run()
        assert (time.time() - start) > 3, "Probe should reach timeout"

        start = time.time()
        probe = Probe(timeout=5, count=0, pause=0.5, fnc=value_err_raise)
        with pytest.raises(CountExceeded):
            probe.run()
        assert (time.time() - start
                ) < 2, "Probe should always end successfully with count=0 "
Пример #17
0
    def test_exception(self):

        # probe and caller in one thread
        # probe should ignore expected_exceptions
        start = time.time()
        probe = Probe(timeout=3,
                      pause=0.5,
                      expected_exceptions=ValueError,
                      fnc=value_err_raise)
        with pytest.raises(ProbeTimeout):
            probe.run()
        assert (time.time() -
                start) > 2, "Timeout not reached with unsuccessful function"

        # probe should not ignore exceptions other than expected exceptions
        start = time.time()
        probe = Probe(timeout=5,
                      pause=0.5,
                      expected_exceptions=ImportError,
                      fnc=value_err_raise)
        with pytest.raises(ValueError):
            probe.run()
        assert (time.time() - start) < 1, "Timeout exceeded"

        start = time.time()
        probe = Probe(timeout=5, pause=0.5, fnc=value_err_raise)
        with pytest.raises(ValueError):
            probe.run()
        assert (time.time() - start) < 1, "Timeout exceeded"

        # run in background
        # probe should ignore expected_exceptions
        start = time.time()
        probe = Probe(timeout=3,
                      pause=0.5,
                      expected_exceptions=ValueError,
                      fnc=value_err_raise)
        probe.run_in_background()
        with pytest.raises(ProbeTimeout):
            probe.join()
        assert (time.time() -
                start) > 2, "Timeout not reached with unsuccessful function"

        # probe should not ignore exceptions other than expected exceptions
        start = time.time()
        probe = Probe(timeout=5,
                      pause=0.5,
                      expected_exceptions=ImportError,
                      fnc=value_err_raise)
        probe.run_in_background()
        with pytest.raises(ValueError):
            probe.join()
        assert (time.time() - start) < 1, "Timeout exceeded"

        start = time.time()
        probe = Probe(timeout=5, pause=0.5, fnc=value_err_raise)
        probe.run_in_background()
        with pytest.raises(ValueError):
            probe.join()
        assert (time.time() - start) < 1, "Timeout exceeded"