-
Notifications
You must be signed in to change notification settings - Fork 650
add: container test plugin for pytest #17372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Empty file.
Empty file.
27 changes: 27 additions & 0 deletions
27
base/images/tests/cases/runtime/container-base/test_basic.py
This file contains hidden or 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,27 @@ | ||
| # SPDX-License-Identifier: MIT | ||
| """Basic runtime tests for container-base images. | ||
|
|
||
| Validate fundamental container behavior by exec-ing commands into a | ||
| running container. Each test gets a fresh container instance via the | ||
| ``container_exec_shell`` fixture (see conftest.py). | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
|
|
||
| def test_shell_accessible(container_exec_shell) -> None: | ||
| """Container shell must be functional via exec.""" | ||
| result = container_exec_shell("echo hello-from-container") | ||
| assert result.exit_code == 0, ( | ||
| f"Shell exec failed (exit_code={result.exit_code}): {result.output}" | ||
| ) | ||
| assert "hello-from-container" in result.output | ||
|
|
||
|
|
||
| def test_dns_resolution(container_exec_shell) -> None: | ||
| """Container must be able to resolve localhost via DNS.""" | ||
| result = container_exec_shell("getent hosts localhost") | ||
| assert result.exit_code == 0, ( | ||
| f"DNS resolution failed (exit_code={result.exit_code}): {result.output}" | ||
| ) | ||
| assert "localhost" in result.output |
5 changes: 5 additions & 0 deletions
5
base/images/tests/cases/runtime/container-base/test_nginx/Dockerfile
This file contains hidden or 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,5 @@ | ||
| ARG BASE_IMAGE | ||
| FROM ${BASE_IMAGE} | ||
| RUN dnf install -y nginx curl && dnf clean all | ||
| COPY nginx.conf /etc/nginx/nginx.conf | ||
| EXPOSE 80 |
27 changes: 27 additions & 0 deletions
27
base/images/tests/cases/runtime/container-base/test_nginx/nginx.conf
This file contains hidden or 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,27 @@ | ||
| worker_processes auto; | ||
| error_log /var/log/nginx/error.log warn; | ||
| pid /run/nginx.pid; | ||
|
|
||
| events { | ||
| worker_connections 128; | ||
| } | ||
|
|
||
| http { | ||
| include /etc/nginx/mime.types; | ||
| default_type application/octet-stream; | ||
|
|
||
| server { | ||
| listen 80; | ||
| server_name localhost; | ||
|
|
||
| location / { | ||
| return 200 "OK\n"; | ||
| add_header Content-Type text/plain; | ||
| } | ||
|
|
||
| location /health { | ||
| return 200 "healthy\n"; | ||
| add_header Content-Type text/plain; | ||
| } | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
base/images/tests/cases/runtime/container-base/test_nginx/test_nginx.py
This file contains hidden or 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,45 @@ | ||
| # SPDX-License-Identifier: MIT | ||
| """Validate nginx works on the container-base image. | ||
|
|
||
| Uses ``@pytest.mark.dockerfile()`` to build a custom image with | ||
| nginx installed on top of the image-under-test. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import time | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| def wait_for_http(container_exec_shell, url: str): | ||
| """Poll until an HTTP endpoint responds successfully.""" | ||
| result = None | ||
| for _ in range(5): | ||
| result = container_exec_shell(f"curl -sf {url}") | ||
| if result.exit_code == 0: | ||
| return result | ||
| time.sleep(1) | ||
|
|
||
| assert result is not None | ||
| return result | ||
|
|
||
|
|
||
| @pytest.mark.dockerfile() | ||
| def test_nginx_config_valid(container_exec_shell) -> None: | ||
| """nginx configuration must pass validation.""" | ||
| result = container_exec_shell("nginx -t") | ||
| assert result.exit_code == 0, f"nginx -t failed: {result.output}" | ||
| assert "syntax is ok" in result.output | ||
| assert "test is successful" in result.output | ||
|
|
||
|
|
||
| @pytest.mark.dockerfile() | ||
| def test_nginx_health_endpoint(container_exec_shell) -> None: | ||
| """nginx /health endpoint must return 200.""" | ||
| start = container_exec_shell("nginx") | ||
| assert start.exit_code == 0, f"nginx failed to start: {start.output}" | ||
|
|
||
| result = wait_for_http(container_exec_shell, "http://localhost:80/health") | ||
| assert result.exit_code == 0, f"health check failed: {result.output}" | ||
| assert "healthy" in result.output |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.