Skip to content

Commit

Permalink
Add server in container test (#313)
Browse files Browse the repository at this point in the history
* Add server in container test

* Change port

* chore: auto fixes from pre-commit.com hooks

* Update fixture names

* Dictionary update

* Allow navigator to pull the container

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
cidrblock and pre-commit-ci[bot] authored Jul 18, 2024
1 parent 8a0e79a commit bd63091
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 17 deletions.
1 change: 1 addition & 0 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ arcname
autoplay
capsys
codespaces
collectonly
containerfile
devcontainer
devel
Expand Down
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"mypy-type-checker.importStrategy": "fromEnvironment",
"mypy-type-checker.reportingScope": "workspace",
"pylint.importStrategy": "fromEnvironment",
"python.testing.pytestArgs": ["tests"],
"python.testing.pytestArgs": ["tests", "--include-container"],
"python.testing.pytestEnabled": true,
"python.testing.unittestEnabled": false,
"triggerTaskOnSave.tasks": {
Expand Down
29 changes: 23 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ def pytest_addoption(parser: pytest.Parser) -> None:
parser.addoption(
"--image-name",
action="store",
default=os.environ.get("ADT_IMAGE_NAME", "ghcr.io/ansible/community-ansible-dev-tools"),
default=os.environ.get(
"ADT_IMAGE_NAME",
"ghcr.io/ansible/community-ansible-dev-tools:latest",
),
help="Container name to use. (default=ADT_IMAGE_NAME)",
)
parser.addoption(
Expand Down Expand Up @@ -175,7 +178,7 @@ def pytest_collection_modifyitems(config: pytest.Config, items: list[pytest.Item


@pytest.fixture(scope="session")
def dev_tools_server() -> str:
def server_url() -> str:
"""Run the server.
Returns:
Expand All @@ -184,13 +187,24 @@ def dev_tools_server() -> str:
return "http://localhost:8000"


@pytest.fixture(scope="session")
def server_in_container_url() -> str:
"""Run the server.
Returns:
str: The server URL.
"""
return "http://localhost:8001"


def pytest_sessionstart(session: pytest.Session) -> None:
"""Start the server.
Args:
session: The pytest session.
"""
assert session
if session.config.option.collectonly:
return

if os.environ.get("PYTEST_XDIST_WORKER"):
return
Expand All @@ -211,7 +225,8 @@ def pytest_sessionfinish(session: pytest.Session) -> None:
Args:
session: The pytest session.
"""
assert session
if session.config.option.collectonly:
return
if os.environ.get("PYTEST_XDIST_WORKER"):
return

Expand All @@ -228,6 +243,7 @@ def pytest_sessionfinish(session: pytest.Session) -> None:
-e NO_COLOR=1
--hostname=ansible-dev-container
--name={container_name}
-p 8001:8001
--security-opt "apparmor=unconfined"
--security-opt "label=disable"
--security-opt "seccomp=unconfined"
Expand All @@ -236,7 +252,7 @@ def pytest_sessionfinish(session: pytest.Session) -> None:
-v $PWD:/workdir
-v ansible-dev-tools-container-test-storage-podman:/var/lib/containers \
{image_name}
sleep infinity"""
adt server --port 8001 &"""

DOCKER_CMD = """{container_engine} run -d --rm
--cap-add=SYS_ADMIN
Expand All @@ -245,14 +261,15 @@ def pytest_sessionfinish(session: pytest.Session) -> None:
-e NO_COLOR=1
--hostname=ansible-dev-container
--name={container_name}
-p 8001:8001
--security-opt "apparmor=unconfined"
--security-opt "label=disable"
--security-opt "seccomp=unconfined"
--user=podman
-v $PWD:/workdir
-v ansible-dev-tools-container-test-storage-docker:/var/lib/containers \
{image_name}
sleep infinity"""
adt server --port 8001"""


def _start_container() -> None:
Expand Down
37 changes: 36 additions & 1 deletion tests/integration/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
from ansible_dev_tools.version_builder import PKGS

from ..conftest import Infrastructure # noqa: TID252
from .test_server_creator import test_collection_v1 as tst_collection_v1
from .test_server_creator import test_error as tst_error
from .test_server_creator import test_playbook_v1 as tst_playbook_v1


@pytest.mark.container()
Expand Down Expand Up @@ -51,7 +54,7 @@ def test_navigator_simple_c_in_c(
playbook = test_fixture_dir_container / "site.yml"
result = exec_container(
f"ansible-navigator run {playbook}"
f" --mode stdout --pp never --pae false --lf {tmp_path}/navigator.log",
f" --mode stdout --pae false --lf {tmp_path}/navigator.log",
)
assert "Success" in result.stdout
assert "ok=1" in result.stdout
Expand Down Expand Up @@ -87,3 +90,35 @@ def test_navigator_simple(
assert return_code == 0
assert "Success" in stdout
assert "ok=1" in stdout


@pytest.mark.container()
def test_error_container(server_in_container_url: str) -> None:
"""Test the error response.
Args:
server_in_container_url: The dev tools server.
"""
tst_error(server_url=server_in_container_url)


@pytest.mark.container()
def test_collection_v1_container(server_in_container_url: str, tmp_path: Path) -> None:
"""Test the collection creation.
Args:
server_in_container_url: The dev tools server.
tmp_path: The temporary directory.
"""
tst_collection_v1(server_url=server_in_container_url, tmp_path=tmp_path)


@pytest.mark.container()
def test_playbook_v1_container(server_in_container_url: str, tmp_path: Path) -> None:
"""Test the playbook creation.
Args:
server_in_container_url: The dev tools server.
tmp_path: The temporary directory.
"""
tst_playbook_v1(server_url=server_in_container_url, tmp_path=tmp_path)
18 changes: 9 additions & 9 deletions tests/integration/test_server_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@
import requests


def test_error(dev_tools_server: str) -> None:
def test_error(server_url: str) -> None:
"""Test the error response.
Args:
dev_tools_server: The server URL.
server_url: The server URL.
"""
response = requests.post(f"{dev_tools_server}/v1/creator/playbook", timeout=1)
response = requests.post(f"{server_url}/v1/creator/playbook", timeout=1)
assert response.status_code == requests.codes.get("bad_request")
assert response.text == "Missing required request body"


def test_playbook_v1(dev_tools_server: str, tmp_path: Path) -> None:
def test_playbook_v1(server_url: str, tmp_path: Path) -> None:
"""Test the playbook creation.
Args:
dev_tools_server: The server URL.
server_url: The server URL.
tmp_path: Pytest tmp_path fixture.
"""
response = requests.post(
f"{dev_tools_server}/v1/creator/playbook",
f"{server_url}/v1/creator/playbook",
json={
"project": "ansible-project",
"scm_org": "ansible",
Expand All @@ -47,15 +47,15 @@ def test_playbook_v1(dev_tools_server: str, tmp_path: Path) -> None:
)


def test_collection_v1(dev_tools_server: str, tmp_path: Path) -> None:
def test_collection_v1(server_url: str, tmp_path: Path) -> None:
"""Test the collection creation.
Args:
dev_tools_server: The server URL.
server_url: The server URL.
tmp_path: Pytest tmp_path fixture.
"""
response = requests.post(
f"{dev_tools_server}/v1/creator/collection",
f"{server_url}/v1/creator/collection",
json={
"collection": "namespace.name",
"project": "collection",
Expand Down

0 comments on commit bd63091

Please sign in to comment.