forked from osbuild/bootc-image-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: add basic integration testing
This commit adds basic integration testing for the project. It is pytest based and can run both locally or via `tmt` [0] which will spin up a clean VM and run the tests inside. [0] https://github.com/teemtee/tmt
- Loading branch information
Showing
7 changed files
with
141 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
summary: Run all tests inside a VM environment | ||
provision: | ||
how: virtual | ||
image: fedora:39 | ||
prepare: | ||
how: install | ||
package: | ||
- podman | ||
- pytest | ||
- python3-flake8 | ||
execute: | ||
how: tmt | ||
script: pytest -s -vv |
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 @@ | ||
Integration tests for osbuild-deploy-container | ||
---------------------------------------------- | ||
|
||
This directory contans integration tests for osbuild-deploy-container. | ||
They can be run in two ways: | ||
1. On the local machine by just running `sudo pytest -s -v` | ||
2. Via `tmt` [0] which will spin up a clean VM and run the tests inside: | ||
|
||
tmt run -vvv | ||
|
||
|
||
[0] https://github.com/teemtee/tmt |
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,11 @@ | ||
import os | ||
import pathlib | ||
import subprocess | ||
|
||
|
||
def test_flake8(): | ||
p = pathlib.Path(__file__).parent | ||
# TODO: use all static checks from osbuild instead | ||
subprocess.check_call( | ||
["flake8", "--ignore=E402", "--max-line-length=120", | ||
os.fspath(p)]) |
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,67 @@ | ||
import json | ||
import os | ||
import pathlib | ||
import subprocess | ||
|
||
import pytest | ||
|
||
# local test utils | ||
import testutil | ||
|
||
|
||
@pytest.fixture(name="output_path") | ||
def output_path_fixture(tmp_path): | ||
output_path = tmp_path / "output" | ||
output_path.mkdir(exist_ok=True) | ||
return output_path | ||
|
||
|
||
@pytest.fixture(name="config_json") | ||
def config_json_fixture(output_path): | ||
CFG = { | ||
"blueprint": { | ||
"customizations": { | ||
"user": [ | ||
{ | ||
"name": "test", | ||
"password": "password", | ||
"groups": ["wheel"], | ||
}, | ||
], | ||
}, | ||
}, | ||
} | ||
config_json_path = output_path / "config.json" | ||
config_json_path.write_text(json.dumps(CFG), encoding="utf-8") | ||
return config_json_path | ||
|
||
|
||
@pytest.mark.skipif(os.getuid() != 0, reason="needs root") | ||
@pytest.mark.skipif(not testutil.has_executable("podman"), reason="need podman") | ||
def test_smoke(output_path, config_json): | ||
# build local container | ||
subprocess.check_call([ | ||
"podman", "build", | ||
"-f", "Containerfile", | ||
"-t", "osbuild-deploy-container-test", | ||
]) | ||
cursor = testutil.journal_cursor() | ||
# and run container to deploy an image into output/disk.qcow2 | ||
subprocess.check_call([ | ||
"podman", "run", "--rm", | ||
"--privileged", | ||
"--security-opt", "label=type:unconfined_t", | ||
"-v", f"{output_path}:/output", | ||
"osbuild-deploy-container-test", | ||
"quay.io/centos-bootc/centos-bootc:stream9", | ||
"--config", "/output/config.json", | ||
]) | ||
# check that there are no denials | ||
# TODO: actually check this once https://github.com/osbuild/images/pull/287 | ||
# is merged | ||
journal_output = testutil.journal_after_cursor(cursor) | ||
assert journal_output != "" | ||
generated_img = pathlib.Path(output_path) / "qcow2/disk.qcow2" | ||
assert generated_img.exists(), f"output file missing, dir content: {os.listdir(os.fspath(output_path))}" | ||
# TODO: boot and do basic checks, see | ||
# https://github.com/osbuild/osbuild-deploy-container/compare/main...mvo5:integration-test?expand=1 |
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,17 @@ | ||
import shutil | ||
import subprocess | ||
|
||
|
||
def journal_cursor(): | ||
output = subprocess.check_output(["journalctl", "-n0", "--show-cursor"], encoding="utf-8").strip() | ||
cursor = output.split("\n")[-1] | ||
return cursor.split("cursor: ")[-1] | ||
|
||
|
||
def journal_after_cursor(cursor): | ||
output = subprocess.check_output(["journalctl", f"--after-cursor={cursor}"]) | ||
return output | ||
|
||
|
||
def has_executable(name): | ||
return shutil.which(name) is not None |