Skip to content

Commit

Permalink
Wait until container's ports are available before considering it created
Browse files Browse the repository at this point in the history
  • Loading branch information
WouldYouKindly committed Jun 11, 2021
1 parent e85305f commit b964a36
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [4.0.2] - 2021-06-11
### Fixed
- Wait until container's ports are available before considering it created

## [4.0.0] - 2021-03-03
### Changed
- Remove dependency on Wrike's internal library
Expand Down
12 changes: 6 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[tool.poetry]
name = "pytest_hoverfly"
version = "4.0.1"
version = "4.0.2"
description = "Simplify working with Hoverfly from pytest"
authors = ["Devops team <[email protected]>"]
authors = ["Devops team at Wrike <[email protected]>"]
repository = "https://github.com/wrike/pytest-hoverfly"
license = "MIT"
readme = "README.md"
Expand All @@ -16,9 +16,9 @@ classifiers = [
[tool.poetry.dependencies]
python = "^3.7"
pytest = ">=5.0"
requests = "^2.22.0"
docker = "^4.2.0"
typing_extensions = "^3.7.4"
requests = ">=2.22.0"
docker = ">=4.2.0"
typing_extensions = ">=3.7.4"

[tool.poetry.dev-dependencies]
flake8 = ">=3.7.7"
Expand All @@ -27,7 +27,7 @@ isort = ">=5.3"
pytest-cov = ">=2.7.1"
black = "^20.8b1"

[tool.poetry.plugins] # Optional super table
[tool.poetry.plugins]
[tool.poetry.plugins."pytest11"]
"hoverfly" = "pytest_hoverfly.pytest_hoverfly"

Expand Down
20 changes: 19 additions & 1 deletion pytest_hoverfly/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def get_container(
)

raw_container.start()
raw_container.reload()
_wait_until_ports_are_ready(raw_container, ports, timeout)

container = Hoverfly.from_container(raw_container)

Expand All @@ -119,3 +119,21 @@ def _wait_until_ready(container: Hoverfly, timeout: float) -> None:
delay *= 2
else:
raise TimeoutError(f"Container for Hoverfly did not start in {timeout}s")


def _wait_until_ports_are_ready(raw_container: Container, ports: t.Dict[str, t.Any], timeout: float) -> None:
"""Docker takes some time to allocate ports so they may not be immediately available."""
now = time.monotonic()
delay = 0.001

while (time.monotonic() - now) < timeout:
raw_container.reload()
# value of a port is either a None or an empty list when it's not ready
ready = {k: v for k, v in raw_container.ports.items() if v}
if set(ports).issubset(ready):
break
else:
time.sleep(delay)
delay *= 2
else:
raise TimeoutError(f"Docker failed to expose ports in {timeout}s")
4 changes: 1 addition & 3 deletions pytest_hoverfly/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ def del_gcloud_credentials(pair):
def ensure_simulation_dir(config) -> Path:
path = get_simulations_path(config)
if not path.exists():
raise ValueError(
"To use pytest-hoverfly you must specify " "--hoverfly-simulation-path. " f"Current value: {path}"
)
raise ValueError(f"To use pytest-hoverfly you must specify --hoverfly-simulation-path. Current value: {path}")

return path
10 changes: 9 additions & 1 deletion pytest_hoverfly/pytest_hoverfly.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@


class HoverflyMarker(te.Protocol):
def __call__(self, name: str, *, record: bool = False, stateful: bool = False) -> t.Callable[..., t.Any]:
def __call__(
self,
name: str,
*,
record: bool = False,
stateful: bool = False,
postprocess: t.Callable[[], t.Any],
enable_default_postprocessing: bool = True,
) -> t.Callable[..., t.Any]:
...


Expand Down

0 comments on commit b964a36

Please sign in to comment.