From 35fdd97f9aafa04cac1f8ef9b9f8637c944c3032 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Thu, 5 Apr 2018 20:24:11 +0100 Subject: [PATCH] issue #164: utility to print Docker hostname for use from shell scripts. --- tests/show_docker_hostname.py | 12 ++++++++++++ tests/testlib.py | 14 +++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 tests/show_docker_hostname.py diff --git a/tests/show_docker_hostname.py b/tests/show_docker_hostname.py new file mode 100644 index 00000000..1dc1cb98 --- /dev/null +++ b/tests/show_docker_hostname.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python + +""" +For use by the Travis scripts, just print out the hostname of the Docker +daemon from the environment. +""" + +import docker +import testlib + +docker = docker.from_env(version='auto') +print testlib.get_docker_host(docker) diff --git a/tests/testlib.py b/tests/testlib.py index cac5b1e9..199e8a46 100644 --- a/tests/testlib.py +++ b/tests/testlib.py @@ -161,6 +161,14 @@ class TestCase(unittest2.TestCase): assert 0, '%r did not raise %r' % (func, exc) +def get_docker_host(docker): + if docker.api.base_url == 'http+docker://localunixsocket': + return 'localhost' + + parsed = urlparse.urlparse(docker.api.base_url) + return parsed.netloc.partition(':')[0] + + class DockerizedSshDaemon(object): def __init__(self): self.docker = docker.from_env(version='auto') @@ -177,11 +185,7 @@ class DockerizedSshDaemon(object): self.host = self.get_host() def get_host(self): - if self.docker.api.base_url == 'http+docker://localunixsocket': - return 'localhost' - - parsed = urlparse.urlparse(self.docker.api.base_url) - return parsed.netloc.partition(':')[0] + return get_docker_host(self.docker) def wait_for_sshd(self): wait_for_port(self.get_host(), int(self.port), pattern='OpenSSH')