|
1 | 1 | # -*- coding: utf-8 -*-
|
2 | 2 |
|
3 |
| - |
4 |
| -import attr |
| 3 | +import functools |
5 | 4 | import os
|
6 |
| -import pytest |
7 | 5 | import re
|
| 6 | +import shlex |
8 | 7 | import subprocess
|
9 | 8 | import time
|
10 | 9 | import timeit
|
11 | 10 |
|
| 11 | +import attr |
| 12 | +import pytest |
| 13 | + |
12 | 14 |
|
13 | 15 | def execute(command, success_codes=(0,)):
|
14 | 16 | """Run a shell command."""
|
@@ -68,9 +70,22 @@ def port_for(self, service, port):
|
68 | 70 | if cache is not None:
|
69 | 71 | return cache
|
70 | 72 |
|
71 |
| - output = self._docker_compose.execute( |
72 |
| - 'port %s %d' % (service, port,) |
| 73 | + get_port = functools.partial( |
| 74 | + self._docker_compose.execute, |
| 75 | + 'port %s %d' % (service, port,), |
73 | 76 | )
|
| 77 | + |
| 78 | + def check_if_up(): |
| 79 | + try: |
| 80 | + get_port() |
| 81 | + return True |
| 82 | + except Exception as ex: |
| 83 | + if 'No container found' in ex.args[0]: |
| 84 | + return False |
| 85 | + raise ex |
| 86 | + self.wait_until_responsive(check_if_up, 5, 0.1) |
| 87 | + |
| 88 | + output = get_port() |
74 | 89 | endpoint = output.strip()
|
75 | 90 | if not endpoint:
|
76 | 91 | raise ValueError(
|
@@ -114,11 +129,20 @@ class DockerComposeExecutor(object):
|
114 | 129 | _compose_project_name = attr.ib()
|
115 | 130 |
|
116 | 131 | def execute(self, subcommand):
|
| 132 | + command = '{} {}'.format(self._get_command_prefix(), subcommand) |
| 133 | + return execute(command) |
| 134 | + |
| 135 | + def get_up(self): |
| 136 | + prepped_command = '{} up --build'.format(self._get_command_prefix()) |
| 137 | + subprocess.Popen(shlex.split(prepped_command)) |
| 138 | + |
| 139 | + def _get_command_prefix(self): |
117 | 140 | command = "docker-compose"
|
118 | 141 | for compose_file in self._compose_files:
|
119 | 142 | command += ' -f "{}"'.format(compose_file)
|
120 |
| - command += ' -p "{}" {}'.format(self._compose_project_name, subcommand) |
121 |
| - return execute(command) |
| 143 | + command += ' -p "{}"'.format(self._compose_project_name) |
| 144 | + return command |
| 145 | + |
122 | 146 |
|
123 | 147 |
|
124 | 148 | @pytest.fixture(scope='session')
|
@@ -175,7 +199,7 @@ def docker_services(
|
175 | 199 | return
|
176 | 200 |
|
177 | 201 | # Spawn containers.
|
178 |
| - docker_compose.execute('up --build -d') |
| 202 | + docker_compose.get_up() |
179 | 203 |
|
180 | 204 | # Let test(s) run.
|
181 | 205 | yield Services(docker_compose)
|
|
0 commit comments