-
Notifications
You must be signed in to change notification settings - Fork 97
[WIP] RHOAIENG-9707 ci: dynamic testing of container images with pytest #629
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
Open
jiridanek
wants to merge
5
commits into
opendatahub-io:main
Choose a base branch
from
jiridanek:jd_helpful_error
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4f5d68a
[WIP] RHOAIENG-9707 ci: dynamic testing of container images with pytest
jiridanek 84db714
fixup, add gitignore for pytest logs
jiridanek cd01164
fixup, explanatory comments
jiridanek fbb1f90
fixup, rename test file so pytest discovers it
jiridanek 4b32eb2
wip
jiridanek 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import os | ||
|
||
import pathlib | ||
|
||
# Absolute path to the top level directory | ||
ROOT_PATH = pathlib.Path(__file__).parent.parent | ||
|
||
# Disable Dagger telemetry and PaaS offering | ||
os.environ["DO_NOT_TRACK"]= "1" | ||
os.environ["NOTHANKS"]= "1" |
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,18 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
import testcontainers.core.container | ||
import testcontainers.core.config | ||
|
||
if TYPE_CHECKING: | ||
from pytest import ExitCode, Session | ||
|
||
# We'd get selinux violations with podman otherwise, so either ryuk must be privileged, or we need to disable selinux. | ||
# https://github.com/testcontainers/testcontainers-java/issues/2088#issuecomment-1169830358 | ||
testcontainers.core.config.testcontainers_config.ryuk_privileged = True | ||
|
||
|
||
# https://docs.pytest.org/en/latest/reference/reference.html#pytest.hookspec.pytest_sessionfinish | ||
def pytest_sessionfinish(session: Session, exitstatus: int | ExitCode) -> None: | ||
testcontainers.core.container.Reaper.delete_instance() |
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,116 @@ | ||
from __future__ import annotations | ||
|
||
import io | ||
import logging | ||
import os.path | ||
import sys | ||
import tarfile | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from docker.models.containers import Container | ||
|
||
|
||
def container_cp(container: Container, src: str, dst: str, | ||
user: int | None = None, group: int | None = None) -> None: | ||
""" | ||
Copies a directory into a container | ||
From https://stackoverflow.com/questions/46390309/how-to-copy-a-file-from-host-to-container-using-docker-py-docker-sdk | ||
""" | ||
fh = io.BytesIO() | ||
tar = tarfile.open(fileobj=fh, mode="w:gz") | ||
|
||
tar_filter = None | ||
if user or group: | ||
def tar_filter(f: tarfile.TarInfo) -> tarfile.TarInfo: | ||
if user: | ||
f.uid = user | ||
if group: | ||
f.gid = group | ||
return f | ||
|
||
logging.debug(f"Adding {src=} to archive {dst=}") | ||
try: | ||
tar.add(src, arcname=os.path.basename(src), filter=tar_filter) | ||
finally: | ||
tar.close() | ||
|
||
fh.seek(0) | ||
container.put_archive(dst, fh) | ||
|
||
|
||
def container_exec( | ||
container: Container, | ||
cmd: str | list[str], | ||
stdout: bool = True, | ||
stderr: bool = True, | ||
stdin: bool = False, | ||
tty: bool = False, | ||
privileged: bool = False, | ||
user: str = "", | ||
detach: bool = False, | ||
stream: bool = False, | ||
socket: bool = False, | ||
environment: dict[str, str] | None = None, | ||
workdir: str | None = None, | ||
) -> ContainerExec: | ||
""" | ||
An enhanced version of #docker.Container.exec_run() which returns an object | ||
that can be properly inspected for the status of the executed commands. | ||
Usage example: | ||
result = tools.container_exec(container, cmd, stream=True, **kwargs) | ||
res = result.communicate(line_prefix=b'--> ') | ||
if res != 0: | ||
error('exit code {!r}'.format(res)) | ||
From https://github.com/docker/docker-py/issues/1989 | ||
""" | ||
|
||
exec_id = container.client.api.exec_create( | ||
container.id, | ||
cmd, | ||
stdout=stdout, | ||
stderr=stderr, | ||
stdin=stdin, | ||
tty=tty, | ||
privileged=privileged, | ||
user=user, | ||
environment=environment, | ||
workdir=workdir, | ||
)["Id"] | ||
|
||
output = container.client.api.exec_start(exec_id, detach=detach, tty=tty, stream=stream, socket=socket) | ||
|
||
return ContainerExec(container.client, exec_id, output) | ||
|
||
|
||
class ContainerExec: | ||
def __init__(self, client, id, output): | ||
self.client = client | ||
self.id = id | ||
self.output = output | ||
|
||
def inspect(self): | ||
return self.client.api.exec_inspect(self.id) | ||
|
||
def poll(self): | ||
return self.inspect()["ExitCode"] | ||
|
||
def communicate(self, line_prefix=b""): | ||
for data in self.output: | ||
if not data: | ||
continue | ||
offset = 0 | ||
while offset < len(data): | ||
sys.stdout.buffer.write(line_prefix) | ||
nl = data.find(b"\n", offset) | ||
if nl >= 0: | ||
slice = data[offset: nl + 1] | ||
offset = nl + 1 | ||
else: | ||
slice = data[offset:] | ||
offset += len(slice) | ||
sys.stdout.buffer.write(slice) | ||
sys.stdout.flush() | ||
while self.poll() is None: | ||
raise RuntimeError("Hm could that really happen?") | ||
return self.poll() |
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 @@ | ||
pytest-logs.txt |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Use context manager for tarfile resource management.
The tarfile should be properly closed using a context manager to ensure resource cleanup.
📝 Committable suggestion
🧰 Tools
🪛 Ruff (0.11.9)
21-21: Use a context manager for opening files
(SIM115)
🪛 Pylint (3.3.7)
[error] 25-25: function already defined line 23
(E0102)
[refactor] 21-21: Consider using 'with' for resource-allocating operations
(R1732)
🤖 Prompt for AI Agents