Пример #1
0
 def test_install_certificate(self):
     print("building image tls-test")
     conu.DockerImage.build(".",
                            tag="tls-test",
                            dockerfile="Dockerfile-tlstest")
     with conu.DockerBackend() as backend:
         try:
             image = backend.ImageClass("tls-test")
             container = image.run_via_binary(
                 conu.DockerRunBuilder(["bash"], additional_opts=["-it"]))
             cert_file, key_file = mockdock.generate_certificate(
                 ["google.com"])
             mockdock.install_certificate_debian(cert_file.name, container)
             with container.mount() as fs:
                 certificates_conf = fs.read_file(
                     "/etc/ca-certificates.conf")
                 self.assertEqual(
                     os.path.basename(cert_file.name) in certificates_conf,
                     True)
                 self.assertEqual(
                     fs.file_is_present(
                         os.path.join("/usr/share/ca-certificates/",
                                      os.path.basename(cert_file.name))),
                     True)
         finally:
             container.kill()
             container.delete()
Пример #2
0
def container():
  backend = conu.DockerBackend(logging_level = logging.DEBUG).__enter__()
  image = backend.ImageClass(imagename, tag = tag)
  container = image.run_via_binary()
  yield container
  container.delete(force = True)
  backend._clean()
Пример #3
0
#!/usr/bin/env python3

import conu

backend = conu.DockerBackend()

image_nginx = backend.ImageClass('docker.io/library/nginx')

container = image_nginx.run_via_binary()

input()
backend.cleanup_images()
backend.cleanup_containers()
Пример #4
0
import conu

IMAGE = "registry.fedoraproject.org/f27/tools"
TAG = "latest"
PORT = 5000

with conu.DockerBackend() as backend:
    image = backend.ImageClass("flaskapp_container")
    options = ["-p", "5000:5000"]
    container = image.run_via_binary(additional_opts=options)

    try:
        assert container.is_running()

        container.wait_for_port(PORT)
        http_response = container.http_request(path="/", port=PORT)
        assert http_response.ok
        assert "Hello Container World!" in http_response.content.decode(
            "utf-8")

        logs = [line for line in container.logs()]
        assert b'"GET / HTTP/1.1" 200 -' in logs[-1]

    finally:
        container.stop()
        container.delete()
Пример #5
0
# SPDX-License-Identifier: MIT
#
"""
These are the tests that verify that our project is released correctly. It only makes sense
to run them after a release.

Just to mess with your brain: this script is using conu to smoke-test conu release in a container.
"""

import logging
import os

import conu
import pytest

BACKEND = conu.DockerBackend(logging_level=logging.DEBUG)


def get_conu_version_from_git():
    version = {}
    # __file__/../../conu/version.py
    project_dir = os.path.abspath(
        os.path.join(os.path.dirname(__file__), "../../"))
    with open(os.path.join(project_dir, "conu", "version.py")) as fp:
        exec(fp.read(), version)
    return version["__version__"]


def run_in_container(img_name, img_tag, script):
    i = BACKEND.ImageClass(img_name, tag=img_tag)
    try:
Пример #6
0
    def test_server(self):
        print("building image tls-test")
        conu.DockerImage.build(".",
                               tag="tls-test",
                               dockerfile="Dockerfile-tlstest")
        print("building image mockdock")
        conu.DockerImage.build(".", tag="mockdock", dockerfile="Dockerfile")

        domains = ["google.com", "domain.org"]

        # This dictionary specify the responses given for different queries.
        # All elements all optional.
        config = json.dumps({
            "google.com/": {
                "data": '{"response": "OK"}\n',
                "code": 200,
                "content-type": "application/json"
            },
            "domain.org:1010/path": {
                "data": "Okay response\n",
                "code": 200
            }
        })

        # Generate a certificate with subject alternative names for the
        # specified domains.
        cert_file, key_file = mockdock.generate_certificate(domains)

        image_name = "mockdock"
        image_tag = "latest"
        with conu.DockerBackend() as backend:
            try:
                server_image = backend.ImageClass(image_name, tag=image_tag)
                docker_options = mockdock.get_docker_options(
                    cert_file.name, key_file.name)
                server_container = server_image.run_via_binary(
                    volumes=[(cert_file.name, cert_file.name),
                             (key_file.name, key_file.name)],
                    additional_opts=[
                        "-e", "CONFIG_DATA={}".format(config), "-e",
                        "EXTRA_PORTS=[1010]"
                    ] + docker_options)
                server_container.wait_for_port(80)
                server_container.wait_for_port(1010)
                container_ips = server_container.get_IPv4s()

                client_image = backend.ImageClass("tls-test")
                # Use the docker --dns argument to specify the server
                # container as dns resolver.
                docker_run = conu.DockerRunBuilder(["bash"],
                                                   additional_opts=[
                                                       "-it", "-u", "root",
                                                       "--dns",
                                                       container_ips[0]
                                                   ])
                client_container = client_image.run_via_binary(docker_run)
                # Install the generated certificate in the server container.
                # This method is debian-specific.
                mockdock.install_certificate_debian(cert_file.name,
                                                    client_container)

                result1 = client_container.execute(
                    ["curl", "-vi", "google.com"])
                result1 = b" ".join(r for r in result1)
                print("Testing {} in {}".format(b'{"response": "OK"}',
                                                result1))
                self.assertEqual(b'{"response": "OK"}' in result1, True)
                result2 = client_container.execute(
                    ["curl", "-vi", "https://google.com"])
                result2 = b" ".join(r for r in result2)
                print("Testing {} in {}".format(b'{"response": "OK"}',
                                                result2))
                self.assertEqual(b'{"response": "OK"}' in result2, True)
                result3 = client_container.execute(
                    ["curl", "-vi", "domain.org:1010/path"])
                result3 = b" ".join(r for r in result3)
                print("Testing {} in {}".format(b"Okay response", result3))
                self.assertEqual(b"Okay response" in result3, True)
                result4 = client_container.execute(
                    ["curl", "-vi", "https://domain.org"])
                result4 = b" ".join(r for r in result4)
                print("Testing {} in {}".format(b"Not Found", result4))
                self.assertEqual(b"Not Found" in result4, True)
            finally:
                server_container.kill()
                server_container.delete()
                client_container.kill()
                client_container.delete()