-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: adds initial end to end tests for rules transform
Adds a mock api to provide static responses for git client Adds container images build and removal fixture Adds initial command test case and response Signed-off-by: Jennifer Power <[email protected]>
- Loading branch information
Showing
11 changed files
with
436 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
requires = ['poetry-core>=1.2.0', 'wheel',] | ||
build-backend = 'poetry.core.masonry.api' | ||
|
||
|
||
[tool.poetry] | ||
name = 'trestlebot' | ||
version = '0.1.0' | ||
|
@@ -10,11 +11,13 @@ description = "trestle-bot assists users in leveraging Compliance-Trestle in aut | |
authors = ["Jennifer Power <[email protected]>",] | ||
|
||
include = ['LICENSE'] | ||
exclude = ['tests/', 'docs/'] | ||
license = 'Apache-2.0' | ||
readme = 'README.md' | ||
|
||
repository = 'https://github.com/RedHatProductSecurity/trestle-bot' | ||
|
||
|
||
[tool.poetry.scripts] | ||
trestlebot-autosync = "trestlebot.entrypoints.autosync:main" | ||
trestlebot-rules-transform = "trestlebot.entrypoints.rule_transform:main" | ||
|
@@ -40,6 +43,7 @@ pre-commit = "^3.4.0" | |
[tool.poetry.group.tests.dependencies] | ||
pytest = "^7.3.2" | ||
pytest-cov = "^4.1.0" | ||
pytest-skip-slow = "^0.0.5" | ||
|
||
[tool.coverage.run] | ||
branch = true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
FROM docker.io/wiremock/wiremock:3.2.0-2 | ||
|
||
COPY mappings/ /home/wiremock/mappings/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# End to End Testing | ||
|
||
The end to end tests are used to verify the CLI from a user perspective running trestle-bot in a container. | ||
|
||
Podman is used to build and manage the deployed containers. | ||
The trestlebot container image and container image for the mock API server are built and the mock API server is started in the pod. | ||
WireMock is used to mock the Git server endpoints. | ||
|
||
## TODO | ||
- Have the option to use pre-built trestle-bot 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. | ||
### Run all tests | ||
|
||
```bash | ||
make test-e2e | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
#!/usr/bin/python | ||
|
||
# Copyright 2023 Red Hat, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import argparse | ||
import pathlib | ||
import subprocess | ||
from typing import Dict, List, Tuple | ||
|
||
import pytest | ||
from git.repo import Repo | ||
from trestle.core.commands.init import InitCmd | ||
|
||
from tests.conftest import YieldFixture | ||
from tests.testutils import args_dict_to_list, setup_rules_view | ||
from trestlebot.const import RULES_VIEW_DIR | ||
|
||
|
||
image_name = "localhost/trestlebot:latest" | ||
mock_server_image_name = "localhost/mock-server:latest" | ||
pod_name = "trestlebot-e2e-pod" | ||
e2e_context = "tests/e2e" | ||
file = "Dockerfile" | ||
|
||
|
||
# Define test cases and expected outcomes | ||
test_cases: List[Tuple[Dict[str, str], int]] = [ | ||
( | ||
{ | ||
"branch": "test", | ||
"rules-view-path": RULES_VIEW_DIR, | ||
"committer-name": "test", | ||
"committer-email": "[email protected]", | ||
"file-patterns": ".", | ||
}, | ||
0, | ||
), | ||
( | ||
{ | ||
"branch": "test", | ||
"rules-view-path": RULES_VIEW_DIR, | ||
"file-patterns": ".", | ||
}, | ||
2, | ||
), | ||
] | ||
|
||
|
||
def build_image_command(data_path: str, command_args: Dict[str, str]) -> List[str]: | ||
"""Build a command to be run in the shell.""" | ||
return [ | ||
"podman", | ||
"run", | ||
"--pod", | ||
pod_name, | ||
"--entrypoint", | ||
"trestlebot-rules-transform", | ||
"--rm", | ||
"-v", | ||
f"{data_path}:/trestle", | ||
"-w", | ||
"/trestle", | ||
image_name, | ||
*args_dict_to_list(command_args), | ||
] | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def podman_setup() -> YieldFixture[int]: | ||
"""Build the trestlebot container image and run the mock server in a pod.""" | ||
# Build the container image | ||
subprocess.run( | ||
[ | ||
"podman", | ||
"build", | ||
"-f", | ||
file, | ||
"-t", | ||
image_name, | ||
], | ||
check=True, | ||
) | ||
|
||
# Build mock server container image | ||
subprocess.run( | ||
[ | ||
"podman", | ||
"build", | ||
"-f", | ||
f"{e2e_context}/{file}", | ||
"-t", | ||
mock_server_image_name, | ||
e2e_context, | ||
], | ||
check=True, | ||
) | ||
|
||
# Create a pod | ||
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", "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], | ||
podman_setup: int, | ||
command_args: Dict[str, str], | ||
response: int, | ||
) -> None: | ||
"""Test the trestlebot rules transform command.""" | ||
# Check that the container image was built successfully | ||
# and the mock server is running | ||
assert podman_setup == 0 | ||
|
||
tmp_repo_str, repo = tmp_repo | ||
|
||
tmp_repo_path = pathlib.Path(tmp_repo_str) | ||
|
||
# Create a trestle workspace in the temporary git repository | ||
args = argparse.Namespace( | ||
verbose=0, | ||
trestle_root=tmp_repo_path, | ||
full=True, | ||
local=False, | ||
govdocs=False, | ||
) | ||
init = InitCmd() | ||
init._run(args) | ||
|
||
# Setup the rules directory | ||
setup_rules_view(tmp_repo_path, "test-comp") | ||
|
||
remote_url = "http://localhost:8080/test.git" | ||
repo.create_remote("origin", url=remote_url) | ||
|
||
# Build the command to be run in the shell | ||
command = build_image_command(tmp_repo_str, command_args) | ||
|
||
# Run the command | ||
run_response = subprocess.run(command, cwd=tmp_repo_path) | ||
|
||
# Get subprocess response | ||
assert run_response.returncode == response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.