Skip to content

Commit

Permalink
refactor: updates the E2E test implementation
Browse files Browse the repository at this point in the history
Replaces Flask and uses WireMock with a JSON mapping
Removes E2E from CI (to be used locally)
Uses podman play kube to build and teardown the pod and container

Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 committed Oct 20, 2023
1 parent 64be4d5 commit 3f45c11
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 167 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,4 @@ jobs:
python-version: ${{ matrix.python-version }}

- name: Run tests
run: |
make test-unit
make test-integration
run: make test
35 changes: 0 additions & 35 deletions .github/workflows/e2e.yml

This file was deleted.

12 changes: 4 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,15 @@ format:
.PHONY: format

test:
@poetry run pytest $(TESTS) --cov --cov-config=pyproject.toml --cov-report=xml
@poetry run pytest --cov --cov-config=pyproject.toml --cov-report=xml
.PHONY: test

test-unit:
@poetry run pytest $(TESTS)/$(PYMODULE) --cov --cov-config=pyproject.toml --cov-report=xml
test-slow:
@poetry run pytest --slow --cov --cov-config=pyproject.toml --cov-report=xml
.PHONY: test-unit

test-integration:
@poetry run pytest $(TESTS)/$(INTEGRATION) --cov --cov-config=pyproject.toml --cov-report=xml
.PHONY: test-integration

test-e2e:
@poetry run pytest $(TESTS)/$(E2E) --cov --cov-config=pyproject.toml --cov-report=xml
@poetry run pytest $(TESTS)/$(E2E) --slow --cov --cov-config=pyproject.toml --cov-report=xml
.PHONY: test-e2e

test-code-cov:
Expand Down
19 changes: 18 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ pre-commit = "^3.4.0"
[tool.poetry.group.tests.dependencies]
pytest = "^7.3.2"
pytest-cov = "^4.1.0"
flask = "^3.0.0"

pytest-skip-slow = "^0.0.5"

[tool.coverage.run]
branch = true
Expand Down
10 changes: 2 additions & 8 deletions tests/e2e/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
FROM python:3.8-slim
FROM docker.io/wiremock/wiremock:3.2.0-2

WORKDIR /app

COPY mock_api.py /app

RUN pip install -no-cache-dir Flask==3.0.0

CMD ["python3", "mock_api.py"]
COPY mappings/ /home/wiremock/mappings/
12 changes: 11 additions & 1 deletion tests/e2e/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@ The end to end test are used to verify the CLI from a user perspective running t

Podman is used to build and manage the deployed containers.
The trestlebot container images and container images for the mock API server is built and the mock API server is started in the pod.
Flask is used to mock Git server endpoints for testing.

WireMock is used to mock the Git server endpoints.

## TODO
- Have the option to use pre-built trestlebot container images from a registry instead of building them locally.

## Prerequisites

- [Podman](https://podman.io/docs/installation)
- [Python 3](https://www.python.org/downloads/)
- [Poetry](https://python-poetry.org/docs/#installation)

## Resources

- `mappings` - Contains JSON mappings used with WireMock to mock the Git server endpoints.
- `play-kube.yml` - Contains the Kubernetes resources used to deploy the mock API server in a pod.
- `Dockerfile` - Contains the Dockerfile used to build the mock server container image.

## Running the tests

> Note: This must be run from the project root directory.
Expand Down
48 changes: 48 additions & 0 deletions tests/e2e/mappings/mapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"mappings": [
{
"request": {
"method": "GET",
"url": "/test.git/HEAD"
},
"response": {
"status": 200,
"body": "ref: refs/heads/test\n",
"headers": {
"Content-Type": "application/x-git-advertisement"
}
}
},
{
"request": {
"method": "GET",
"urlPattern": "/test.git/info/refs.*",
"queryParameters": {
"service": {
"equalTo": "git-receive-pack"
}
}
},
"response": {
"status": 200,
"body": "3e84c924d2574c95e8a7e8d7a76530b95d16f784\trefs/heads/test\n",
"headers": {
"Content-Type": "application/x-git-advertisement"
}
}
},
{
"request": {
"method": "POST",
"url": "/test.git/git-receive-pack"
},
"response": {
"status": 200,
"body": "0000ACK refs/heads/test\n0000",
"headers": {
"Content-Type": "application/x-git-receive-pack-result"
}
}
}
]
}
90 changes: 0 additions & 90 deletions tests/e2e/mock_api.py

This file was deleted.

12 changes: 12 additions & 0 deletions tests/e2e/play-kube.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: Pod
metadata:
name: trestlebot-e2e-pod
labels:
app: trestlebot-e2e
spec:
containers:
- name: mock-server-container
image: localhost/mock-server:latest
ports:
- containerPort: 8080
23 changes: 5 additions & 18 deletions tests/e2e/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

image_name = "localhost/trestlebot:latest"
mock_server_image_name = "localhost/mock-server:latest"
pod_name = "trestlebot"
pod_name = "trestlebot-e2e-pod"
e2e_context = "tests/e2e"
file = "Dockerfile"

Expand Down Expand Up @@ -108,33 +108,20 @@ def podman_setup() -> YieldFixture[int]:
)

# Create a pod
subprocess.run(["podman", "pod", "create", "--name", pod_name], check=True)
# Run the mock server in the pod
response = subprocess.run(
[
"podman",
"run",
"--pod",
pod_name,
"-d",
"--name",
"mock-server",
mock_server_image_name,
]
)
response = subprocess.run(["podman", "play", "kube", f"{e2e_context}/play-kube.yml"], check=True)
yield response.returncode

# Clean up the container image, pod and mock server
try:
subprocess.run(["podman", "rm", "-f", "mock-server"], check=True)
subprocess.run(["podman", "pod", "rm", "-f", pod_name], check=True)
subprocess.run(["podman", "play", "kube", "--down", f"{e2e_context}/play-kube.yml"], check=True)
subprocess.run(["podman", "rmi", image_name], check=True)
subprocess.run(["podman", "rmi", mock_server_image_name], check=True)
except subprocess.CalledProcessError as e:
raise RuntimeError(f"Failed to clean up podman resources: {e}")


# Run each test case
@pytest.mark.slow
@pytest.mark.parametrize("command_args, response", test_cases)
def test_rules_transform_e2e(
tmp_repo: Tuple[str, Repo],
Expand Down Expand Up @@ -165,7 +152,7 @@ def test_rules_transform_e2e(
# Setup the rules directory
setup_rules_view(tmp_repo_path, "test-comp")

remote_url = "http://localhost:5000/test.git"
remote_url = "http://localhost:8080/test.git"
repo.create_remote("origin", url=remote_url)

# Build the command to be run in the shell
Expand Down

0 comments on commit 3f45c11

Please sign in to comment.