Exemplo n.º 1
0
def test_docker_build_with_dockerfile():
    docker = DockerClient()

    dockerfile = open(
        os.path.dirname(os.path.realpath(__file__)) + "/Dockerfile").read()

    docker.build(dockerfile=dockerfile, tag="my_container_2")
    out = docker.images("my_container_2")
    pprint(out)
    assert len(out) == 1
    assert out[0]['RepoTags'][0] == 'my_container_2:latest'
Exemplo n.º 2
0
def test_docker_pull():
    name = "selenium/hub:2.53.0"
    docker = DockerClient()
    if docker.image_exists(name):
        docker.remove_image(name, True)
    docker.pull_image(name)
    assert docker.image_exists(name)
Exemplo n.º 3
0
def test_docker_build():
    dockerfile = """
                FROM busybox:buildroot-2014.02
                MAINTAINER first last, [email protected]
                VOLUME /data
                CMD ["/bin/sh"]
                """

    docker = DockerClient()
    docker.build(dockerfile=dockerfile, tag="my_container")
    out = docker.images("my_container")
    pprint(out)
    assert len(out) == 1
    assert out[0]['RepoTags'][0] == 'my_container:latest'
Exemplo n.º 4
0
 def __init__(self, image):
     self.env = {}
     self.ports = {}
     self.volumes = {}
     self.image = image
     self._docker = DockerClient()
     self._container = None
     self._command = None
     self._name = None
Exemplo n.º 5
0
class DockerContainer(object):
    def __init__(self, image, version):
        self.env = {}
        self.ports = {}
        self._docker = DockerClient()
        self.image = "{}:{}".format(image, version)
        self._container = None

    def add_env(self, key, value):
        self.env[key] = value
        return self

    def expose_port(self, container, host=None):
        self.ports[container] = host
        return self

    def _configure(self):
        pass

    def start(self):
        self._configure()
        self._container = self._docker.run(self.image,
                                           detach=True,
                                           environment=self.env,
                                           ports=self.ports,
                                           publish_all_ports=True)
        return self

    def stop(self):
        self._container.remove(force=True)

    def __enter__(self):
        return self.start()

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.stop()

    def get_container_host_ip(self) -> str:
        return "0.0.0.0"

    def get_exposed_port(self, port) -> str:
        return self._docker.port(self._container.id, port)
Exemplo n.º 6
0
 def __init__(self, image, **kwargs):
     self._docker = DockerClient()
     self._container = None
     self._kwargs = {
         'detach': True,
         'environment': {}, 
         'ports': {}, 
         'volumes': {}, 
         'image': image,
         **kwargs,
         }
Exemplo n.º 7
0
def test_docker_port():
    docker = DockerClient()
    container = docker.run('selenium/hub:2.53.0', name='selenium-hub')
    print docker.port(container, 4444)
    print "End"
Exemplo n.º 8
0
def test_docker_build_from_path():
    docker = DockerClient()
    docker.build_from_path(os.path.dirname(os.path.realpath(__file__)),
                           tag="video_service")
Exemplo n.º 9
0
def test_docker_run():
    docker = DockerClient()
    docker.stop_all()
    docker.run('selenium/hub:2.53.0',
               bind_ports={4444: 4444},
               name='selenium-hub')
    docker.run('selenium/node-firefox:2.53.0', links={'selenium-hub': 'hub'})
    containers = docker.get_running_containers()
    assert len(containers) >= 2
    docker.stop_all()
    assert len(docker.get_running_containers()) == 0
Exemplo n.º 10
0
 def __init__(self, image_name, version, container_name):
     self._docker = DockerClient()
     self._config = ContainerConfig(image_name=image_name,
                                    version=version,
                                    container_name=container_name)
     self._container = None
Exemplo n.º 11
0
def test_compose_wait_for_container_ready():
    with DockerCompose("tests") as compose:
        docker = DockerClient()
        compose.wait_for("http://%s:4444/wd/hub" % docker.host())
Exemplo n.º 12
0
def test_docker_stop_all():
    docker = DockerClient()
    docker.run(image='selenium/hub:2.53.0',
               bind_ports={4444: 4444},
               name='selenium-hub')
    docker.stop_all()
Exemplo n.º 13
0
def test_docker_image_exists():
    docker = DockerClient()
    docker.pull_image("selenium/node-chrome:latest")
    assert docker.image_exists("selenium/node-chrome:latest")
Exemplo n.º 14
0
 def __init__(self, image, version):
     self.env = {}
     self.ports = {}
     self._docker = DockerClient()
     self.image = "{}:{}".format(image, version)
     self._container = None
Exemplo n.º 15
0
def test_docker_inspect():
    docker = DockerClient()
    container = docker.run('selenium/hub:2.53.0', name='selenium-hub')
    pprint(docker.inspect(container))
Exemplo n.º 16
0
def test_docker_get_containers():
    docker = DockerClient()
    img = docker.get_containers(all=True, filters={"name": "selenium-hub"})
    assert len(img) >= 0
Exemplo n.º 17
0
def test_docker_get_running_containers():
    docker = DockerClient()
    print(docker.get_running_containers())
Exemplo n.º 18
0
def test_can_get_logs():
    with DockerCompose("tests") as compose:
        docker = DockerClient()
        compose.wait_for("http://%s:4444/wd/hub" % docker.host())
        stdout, stderr = compose.get_logs()
        assert stdout, 'There should be something on stdout'
Exemplo n.º 19
0
class DockerContainer(object):
    def __init__(self, image_name, version, container_name):
        self._docker = DockerClient()
        self._config = ContainerConfig(image_name=image_name,
                                       version=version,
                                       container_name=container_name)
        self._container = None

    def __enter__(self):
        return self.start()

    def __exit__(self, type, value, traceback):
        self.stop()

    def start(self):
        self._container = self._docker.run(
            image=self._config.image,
            bind_ports=self._config.port_bindings,
            env=self._config.environment,
            links=self._config.container_links,
            name=self._config.container_name,
            volumes=self._config.volumes)
        self.print_ports()
        return self

    def print_ports(self):
        logging.warning("Container port mappings {}".format(
            self.inspect()['NetworkSettings']['Ports']))

    def stop(self):
        self._docker.stop(self._container)

    def get_host_info(self, port):
        info = self._docker.port(self._container, port)
        return info[0]

    def get_host_port(self, port):
        return self.get_host_info(port)['HostPort']

    def get_host_ip(self, port):
        return self.get_host_info(port)['HostIp']

    @property
    def container_name(self):
        return self._config.container_name

    def get_env(self, key):
        return self._config.environment[key]

    def add_env(self, key, value):
        self._config.add_env(key, value)

    def bind_ports(self, host, container):
        self._config.bind_ports(host, container)

    def mount_volume(self, host, container):
        self._config.mount_volume(host, container)

    def link_containers(self, target, current):
        self._config.link_containers(target, current)

    def inspect(self):
        return self._docker.inspect(self._container)