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()
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"
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
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)
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)
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"
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"
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"
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"
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)
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"
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()
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()
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 "