Skip to content

Commit b74d5b3

Browse files
Add wait strategy behavior to NginxContainer testcontainers#395
1 parent 2dbc38e commit b74d5b3

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

nginx/testcontainers/nginx/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@
1010
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1111
# License for the specific language governing permissions and limitations
1212
# under the License.
13+
import urllib.error
14+
import urllib.parse
15+
import urllib.request
16+
1317
from testcontainers.core.container import DockerContainer
1418
from testcontainers.core.utils import raise_for_deprecated_parameter
19+
from testcontainers.core.waiting_utils import wait_container_is_ready
1520

1621

1722
class NginxContainer(DockerContainer):
@@ -20,3 +25,17 @@ def __init__(self, image: str = "nginx:latest", port: int = 80, **kwargs) -> Non
2025
super(NginxContainer, self).__init__(image, **kwargs)
2126
self.port = port
2227
self.with_exposed_ports(self.port)
28+
29+
def start(self) -> 'NginxContainer':
30+
super().start()
31+
32+
host = self.get_container_host_ip()
33+
port = str(self.get_exposed_port(self.port))
34+
self._connect(host, port)
35+
36+
return self
37+
38+
@wait_container_is_ready(urllib.error.URLError)
39+
def _connect(self, host: str, port: str) -> None:
40+
url = urllib.parse.urlunsplit(('http', f'{host}:{port}', '', '', ''))
41+
urllib.request.urlopen(url, timeout=1)

0 commit comments

Comments
 (0)