Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Services.ip_for method to obtain IP of container #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/pytest_docker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import subprocess
import time
import timeit
import json


def execute(command, success_codes=(0,)):
Expand Down Expand Up @@ -101,6 +102,36 @@ def wait_until_responsive(self, check, timeout, pause,
'Timeout reached while waiting on service!'
)

def ip_for(self, service):
"""Get direct IP of container"""
# List containers providing given service
containers_ids = self._docker_compose.execute(
"ps -q %s" % service).strip().splitlines()
if len(containers_ids) == 0:
raise ValueError(
"Can not find container for service '%s'" % service)
if len(containers_ids) > 1:
raise Exception(
"Multiple containers (%d) for one service '%s' detected"
% (len(containers_ids), service))

# Read out info about listed container
docker_info = json.loads(
execute("docker inspect %s" % containers_ids[0])
)
if not docker_info:
raise Exception(
"Couldn't fetch information data about container %s "
% containers_ids[0])
network_settings = docker_info[0]['NetworkSettings']
networks = network_settings['Networks']
if not networks:
raise Exception(
"Container %s doesn't have network attached"
% containers_ids[0])
for i in networks.values():
return i['IPAddress']


def str_to_list(arg):
if isinstance(arg, (list, tuple)):
Expand Down
1 change: 1 addition & 0 deletions tests/requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ flake8
mock
pytest
requests
docker-compose
5 changes: 5 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,8 @@ def test_integration(docker_ip, docker_services):
response = requests.get(url)
response.raise_for_status()
print(response.text)


def test_direct_ip(docker_ip, docker_services):
ip = docker_services.ip_for('hello')
assert len(ip.split('.')) == 4