def test_get_port_mappings(): image = DockerImage(FEDORA_MINIMAL_REPOSITORY, tag=FEDORA_MINIMAL_REPOSITORY_TAG) # the container needs to be running in order to get port mappings command = DockerRunBuilder(additional_opts=["-p", "321:123", "-i"]) container = image.run_via_binary(command) try: mappings = container.get_port_mappings(123) assert len(mappings) == 1 assert mappings == [{"HostIp": '0.0.0.0', "HostPort": '321'}] mappings = container.get_port_mappings() assert len(mappings) == 1 assert mappings == { '123/tcp': [{ 'HostIp': '0.0.0.0', 'HostPort': '321' }] } finally: container.delete(force=True) command = DockerRunBuilder() container = image.run_via_binary(command) try: mappings = container.get_port_mappings(123) assert not mappings finally: container.delete(force=True)
def test_networking_scenario(): """ Listen via netcat in one container, send a secret message to the container via another one. """ image = DockerImage(THE_HELPER_IMAGE) r1 = DockerRunBuilder(command=["nc", "-l", "-k", "0.0.0.0", "1234"]) cont = image.run_via_binary(r1) # FIXME: wait time.sleep(0.2) assert cont.is_running() assert cont.get_IPv4s() assert cont.is_port_open(1234) assert not cont.is_port_open(2345) secret_text = b"gardener-did-it" r2 = DockerRunBuilder(command=["nc", cont.get_IPv4s()[0], "1234"]) r2.options = ["-i", "--rm"] cont2 = image.run_via_binary_in_foreground( r2, popen_params={"stdin": subprocess.PIPE}, container_name="test-container") # FIXME: wait time.sleep(1) assert "" == cont.logs().decode("utf-8").strip() assert cont2.is_running() assert cont.is_running() cont2.popen_instance.communicate(input=secret_text + b"\n") # give container time to process time.sleep(1) cont.stop() assert not cont2.is_running() assert not cont.is_running() assert secret_text == cont.logs().strip() cont.delete()
def test_container_create_failed(): image = DockerImage(FEDORA_MINIMAL_REPOSITORY, tag=FEDORA_MINIMAL_REPOSITORY_TAG) # should raise an exc, there is no such command: waldo; we need to find waldo first with pytest.raises(ConuException): image.run_via_binary(DockerRunBuilder(command=["waldo"])) c = image.run_via_binary_in_foreground(DockerRunBuilder(command=["waldo"])) c.popen_instance.communicate() assert c.popen_instance.returncode > 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"
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_copy_from(tmpdir): image = DockerImage(FEDORA_MINIMAL_REPOSITORY, tag=FEDORA_MINIMAL_REPOSITORY_TAG) c = image.run_via_binary( DockerRunBuilder(command=["cat"], additional_opts=["-i", "-t"])) try: c.copy_from("/etc/fedora-release", str(tmpdir)) with open(os.path.join(str(tmpdir), "fedora-release")) as fd: assert fd.read() == "Fedora release 26 (Twenty Six)\n" c.copy_from("/etc", str(tmpdir)) os.path.exists(os.path.join(str(tmpdir), "passwd")) finally: c.stop() c.delete()
def test_copy_to(tmpdir): content = b"gardener did it" p = tmpdir.join("secret") p.write(content) image = DockerImage(FEDORA_MINIMAL_REPOSITORY, tag=FEDORA_MINIMAL_REPOSITORY_TAG) c = image.run_via_binary( DockerRunBuilder(command=["cat"], additional_opts=["-i", "-t"])) try: c.copy_to(str(p), "/") assert content == c.execute(["cat", "/secret"]) finally: c.stop() c.delete()
def test_container(): """ Basic tests of interacting with a container """ image = DockerImage(FEDORA_MINIMAL_REPOSITORY, tag=FEDORA_MINIMAL_REPOSITORY_TAG) c = image.run_via_binary( DockerRunBuilder(command=["cat"], additional_opts=["-i", "-t"])) try: assert "Config" in c.inspect() assert "Config" in c.get_metadata() assert c.get_id() == str(c) assert repr(c) assert isinstance(c.get_id(), string_types) finally: c.stop() c.delete()
def test_cleanup_containers(): with DockerBackend(logging_level=logging.DEBUG) as backend: # cleaning up from previous runs backend.cleanup_containers() client = get_client() container_sum = len(client.containers(all=True)) image = DockerImage(FEDORA_MINIMAL_REPOSITORY, tag=FEDORA_MINIMAL_REPOSITORY_TAG) command = ["ls"] additional_opts = ["-i", "-t"] for i in range(3): image.run_via_binary(command=command, additional_opts=additional_opts) assert container_sum+3 == len(client.containers(all=True)) backend.cleanup_containers() assert container_sum == len(client.containers(all=True))
def test_http_client(): image = DockerImage(FEDORA_REPOSITORY) c = image.run_via_binary( DockerRunBuilder( command=["python3", "-m", "http.server", "--bind", "0.0.0.0 8000" ])) c.start() time.sleep(1) # FIXME: replace by wait once available assert c.is_running() r = c.http_request(port="8000") assert "<!DOCTYPE HTML PUBLIC" in r.content.decode("utf-8") assert r.ok r2 = c.http_request(path="/etc", port="8000") assert "<!DOCTYPE HTML PUBLIC" in r2.content.decode("utf-8") assert "passwd" in r2.content.decode("utf-8") assert r2.ok c.stop() c.delete()
def test_image(): """ Basic tests of interacting with image: inspect, tag, remove """ image = DockerImage(FEDORA_MINIMAL_REPOSITORY, tag=FEDORA_MINIMAL_REPOSITORY_TAG) assert "Config" in image.inspect() assert "Config" in image.get_metadata() assert "fedora-minimal:26" in image.get_full_name() assert "registry.fedoraproject.org/fedora-minimal:26" == str(image) assert "DockerImage(repository=%s, tag=%s)" % ( FEDORA_MINIMAL_REPOSITORY, FEDORA_MINIMAL_REPOSITORY_TAG) == repr(image) assert isinstance(image.get_id(), string_types) new_image = image.tag_image(tag="test") new_image.rmi(via_name=True)
def test_get_port_mappings(): image_name = "registry.fedoraproject.org/fedora" image_tag = "27" image = DockerImage(image_name, image_tag) command = DockerRunBuilder(additional_opts=["-p", "321:123"]) try: image.get_metadata() except Exception: image.pull() container = image.run_via_binary(command) try: mappings = container.get_port_mappings(123) assert len(mappings) == 1 assert mappings == [{"HostIp": '0.0.0.0', "HostPort": '321'}] mappings = container.get_port_mappings() assert len(mappings) == 1 assert mappings == { '123/tcp': [{ 'HostIp': '0.0.0.0', 'HostPort': '321' }] } finally: container.stop() container.delete() command = DockerRunBuilder() container = image.run_via_binary(command) try: mappings = container.get_port_mappings(123) assert not mappings finally: container.stop() container.delete()
from conu import DockerImage, DockerContainer, DockerRunBuilder image = DockerImage('centos/httpd-24-centos7') image.pull() command = DockerRunBuilder(additional_opts=["-p", "8080:8080"]) container = image.run_via_binary(command) container.wait_for_port(port=8080, timeout=-1) container.stop() container.delete()
from conu import DockerImage, DockerRunBuilder image = DockerImage('fedora', tag='26') image.pull() command = DockerRunBuilder(command=["ls"], additional_opts=["-i", "-t"]) container = image.run_via_binary(command) container.stop() container.delete()