Skip to content

Commit

Permalink
Add service env variable (#14)
Browse files Browse the repository at this point in the history
Allow specifying the host for services (fixes running on minikube)

Co-authored-by: Eugene Fominykh <eugene.fominykh@team.wrike.com>
  • Loading branch information
junqed and user-name-name committed Jan 20, 2022
1 parent 7536dfc commit e01d2a7
Show file tree
Hide file tree
Showing 10 changed files with 22 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).

## [5.0.1] - 2022-01-20
### Added
- Allow specifying the service host via `SERVICE_HOST` env var

## [5.0.0] - 2022-01-17
### Changed
- Allow specifying Docker API host via `DOCKER_HOST` env var
Expand Down
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ There are two use cases: to record a new test and to use recordings.
#### Prerequisites
You need to have [Docker](https://www.docker.com/) installed. `pytest-hoverfly` uses it under the hood to create Hoverfly instances.

Create a directory to store simulation files. Pass `--hoverfly-simulation-path` option
Create a directory to store simulation files. Pass `--hoverfly-simulation-path` option
when calling `pytest`. The path may be absolute or relative to your `pytest.ini` file.
E.g. if you have a structure like this:
```
Expand All @@ -37,10 +37,14 @@ addopts =
```

#### Without Docker Desktop
If you're using something like [lima](https://github.com/lima-vm/lima) instead of Docker Desktop, you need to specify a path to Docker API. For lima:
If you're using something like [lima](https://github.com/lima-vm/lima) instead of Docker Desktop, you need to specify a path to Docker API. For lima:

`export DOCKER_HOST=unix:///Users/<YOUR-USER>/.lima/default/sock/docker.sock`

If you're using [minikube](https://github.com/kubernetes/minikube) instead of Docker Desktop, you need to specify the service host because the exposed ports are not available on localhost. For minikube you get the service IP with `minikube ip` command and then put it in the env var:

`export SERVICE_HOST=192.168.0.xxx`

#### How to record a test
```python
from pytest_hoverfly import hoverfly
Expand All @@ -52,20 +56,20 @@ def test_google_with_hoverfly():
assert requests.get('https://google.com').status_code == 200
```

Write a test. Decorate it with `@hoverfly`, specifying a name of a file to save the simulation to.
Run the test. A Hoverfly container will be created, and `HTTP_PROXY` and `HTTPS_PROXY` env vars
will be set to point to this container. After test finishes, the resulting simulation will
Write a test. Decorate it with `@hoverfly`, specifying a name of a file to save the simulation to.
Run the test. A Hoverfly container will be created, and `HTTP_PROXY` and `HTTPS_PROXY` env vars
will be set to point to this container. After test finishes, the resulting simulation will
be exported from Hoverfly and saved to a file you specified. After test session ends, Hoverfly
container will be destroyed (unless `--hoverfly-reuse-container` is passed to pytest).

This will work for cases when a server always returns the same response for the same
request. If you need to work with stateful endpoints (e.g. wait for Teamcity build
to finish), use `@hoverfly('my-simulation, record=True, stateful=True)`. See
to finish), use `@hoverfly('my-simulation, record=True, stateful=True)`. See
[Hoverfly docs](https://docs.hoverfly.io/en/latest/pages/tutorials/basic/capturingsequences/capturingsequences.html)
for details.

#### How to use recordings
Remove `record` parameter. That's it. When you run the test, it will create a container
Remove `record` parameter. That's it. When you run the test, it will create a container
with Hoverfly, upload your simulation into it, and use it instead of a real service.

```python
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pytest_hoverfly"
version = "5.0.0"
version = "5.0.1"
description = "Simplify working with Hoverfly from pytest"
authors = ["Devops team at Wrike <devops@team.wrike.com>"]
repository = "https://github.com/wrike/pytest-hoverfly"
Expand Down
Binary file not shown.
Binary file removed pytest_hoverfly/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file removed pytest_hoverfly/__pycache__/base.cpython-39.pyc
Binary file not shown.
Binary file removed pytest_hoverfly/__pycache__/helpers.cpython-39.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
9 changes: 6 additions & 3 deletions pytest_hoverfly/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def proxy_url(self) -> str:
return f"http://{self.host}:{self.proxy_port}"

@classmethod
def from_container(cls, container: Container) -> Hoverfly:
def from_container(cls, service_host: str, container: Container) -> Hoverfly:
return Hoverfly(
host="localhost",
host=service_host,
admin_port=int(container.ports["8888/tcp"][0]["HostPort"]),
proxy_port=int(container.ports["8500/tcp"][0]["HostPort"]),
)
Expand Down Expand Up @@ -108,7 +108,10 @@ def get_container(
raw_container.start()
_wait_until_ports_are_ready(raw_container, ports, timeout)

container = Hoverfly.from_container(raw_container)
container = Hoverfly.from_container(
os.environ.get("SERVICE_HOST", "localhost"),
raw_container,
)

try:
_wait_until_ready(container, timeout)
Expand Down

0 comments on commit e01d2a7

Please sign in to comment.