Skip to content

Commit 5735803

Browse files
committed
Add PyTest suite for nginx-container.
Migration matrix is: ``` TEST_LIST_APP="\ test_s2i_usage -> test_container_basics.py(TestNginxContainer.test_run_s2i_usage) test_docker_run_usage -> test_container_basics.py(TestNginxContainer.test_docker_run_usage) test_scl_usage -> test_container_basics.py(TestNginxContainer.test_scl_usage) test_application -> test_container_application.py(TestNginxApplicationContainer.test_application) test_log_output -> test_container_example_apps.py(TestNginxLogContainer.test_log_output) test_log_volume_output -> test_container_example_apps.py(TestNginxLogContainer.test_log_volume_output) test_application_user -> test_container_application.py(TestNginxApplicationContainer.test_application_user) " TEST_LIST_HOOK_APP="test_pre_init_script" -> test_container_example_apps.py(TestNginxExampleAppContainer.test_run_app_test) TEST_LIST_PERL_APP="test_perl_directive" -> test_container_example_apps.py(TestNginxExamplePerlAppContainer.test_run_app_test) TEST_LIST_MISC=run_dockerfiles_test -> test_container_basics.py(TestNginxContainer.test_dockerfiles) ``` Signed-off-by: Petr "Stone" Hracek <phracek@redhat.com>
1 parent 59d7f42 commit 5735803

File tree

5 files changed

+300
-0
lines changed

5 files changed

+300
-0
lines changed

test/constants.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
import os
2+
from pathlib import Path
3+
14
TAGS = {
25
"rhel8": "-ubi8",
36
"rhel9": "-ubi9",
47
"rhel10": "-ubi10",
58
}
9+
10+
VERSION = os.getenv("VERSION")
11+
OS = os.getenv("OS").lower()
12+
IMAGE_NAME = os.getenv("IMAGE_NAME")
13+
TEST_DIR = Path(__file__).parent.absolute()

test/run-pytest

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
#
3+
# IMAGE_NAME specifies a name of the candidate image used for testing.
4+
# The image has to be available before this script is executed.
5+
# SINGLE_VERSION specifies the major version of the MariaDB in format of X.Y
6+
# OS specifies RHEL version (e.g. OS=rhel8)
7+
#
8+
9+
THISDIR=$(dirname ${BASH_SOURCE[0]})
10+
11+
git show -s
12+
13+
PYTHON_VERSION="3.12"
14+
if [[ ! -f "/usr/bin/python$PYTHON_VERSION" ]]; then
15+
PYTHON_VERSION="3.13"
16+
fi
17+
cd "${THISDIR}" && "python${PYTHON_VERSION}" -m pytest -s -rA --showlocals -vv test_container*.py

test/test_container_application.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import pytest
2+
3+
4+
from pathlib import Path
5+
from container_ci_suite.container_lib import ContainerTestLib
6+
from container_ci_suite.engines.podman_wrapper import PodmanCLIWrapper
7+
from constants import TEST_DIR, IMAGE_NAME, VERSION
8+
9+
test_app = TEST_DIR / "test-app"
10+
11+
12+
def build_s2i_app(app_path: Path) -> ContainerTestLib:
13+
container_lib = ContainerTestLib(IMAGE_NAME)
14+
app_name = app_path.name
15+
s2i_app = container_lib.build_as_df(
16+
app_path=app_path,
17+
s2i_args="--pull-policy=never",
18+
src_image=IMAGE_NAME,
19+
dst_image=f"{IMAGE_NAME}-{app_name}"
20+
)
21+
return s2i_app
22+
23+
24+
class TestNginxApplicationContainer:
25+
26+
def setup_method(self):
27+
self.app = ContainerTestLib(image_name=IMAGE_NAME, s2i_image=True)
28+
29+
def teardown_method(self):
30+
self.app.cleanup()
31+
32+
@pytest.mark.parametrize(
33+
"container_arg",
34+
[
35+
"",
36+
"--user 12345"
37+
]
38+
)
39+
# test_application
40+
def test_application(self, container_arg):
41+
version = VERSION.replace("-micro", "")
42+
cid_file_name = "test-app"
43+
assert self.app.create_container(
44+
cid_file_name=cid_file_name,
45+
container_args=f"--user 10001 {container_arg}"
46+
)
47+
cip = self.app.get_cip(cid_file_name=cid_file_name)
48+
assert cip
49+
assert PodmanCLIWrapper.podman_run_command(
50+
f"--rm {self.app.image_name} /bin/bash -c 'nginx -v'"
51+
).startswith(f"nginx version: nginx/{version}")
52+
assert self.app.test_response(
53+
url=f"http://{cip}", expected_output="NGINX is working"
54+
)
55+
assert self.app.test_response(
56+
url=f"http://{cip}", expected_output="NGINX2 is working",
57+
host="localhost2"
58+
)
59+
assert self.app.test_response(
60+
url=f"http://{cip}", expected_output="NGINX2 is working",
61+
page="/aliased/index2.html"
62+
)
63+
assert self.app.test_response(
64+
url=f"http://{cip}", expected_code=404,
65+
page="/nginx-cfg/default.conf"
66+
)

test/test_container_basics.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import pytest
2+
3+
from container_ci_suite.container_lib import ContainerTestLib
4+
from container_ci_suite.engines.podman_wrapper import PodmanCLIWrapper
5+
from container_ci_suite.dockerfile_processor import DockerfileProcessor
6+
7+
from constants import TEST_DIR, IMAGE_NAME, VERSION
8+
9+
10+
class TestNginxContainer:
11+
12+
def setup_method(self):
13+
self.app = ContainerTestLib(image_name=IMAGE_NAME, s2i_image=True)
14+
15+
def teardown_method(self):
16+
self.app.cleanup()
17+
18+
def test_run_s2i_usage(self, app):
19+
output = app.s2i_usage()
20+
assert output != ""
21+
22+
def test_docker_run_usage(self):
23+
assert PodmanCLIWrapper.call_podman_command(
24+
cmd=f"run --rm {IMAGE_NAME} &>/dev/null",
25+
return_output=False
26+
) == 0
27+
28+
def test_scl_usage(self):
29+
version = VERSION.replace("-micro", "")
30+
assert PodmanCLIWrapper.podman_run_command(
31+
f"--rm {IMAGE_NAME} /bin/bash -c 'nginx -v'"
32+
).startswith(f"nginx version: nginx/{version}")
33+
34+
@pytest.mark.parametrize(
35+
"dockerfile",
36+
[
37+
"Dockerfile",
38+
"Dockerfile.s2i"
39+
]
40+
)
41+
def test_dockerfiles(self, app, dockerfile):
42+
version = VERSION.replace("-micro", "")
43+
dp = DockerfileProcessor(dockerfile_path=f"{TEST_DIR}/examples/{dockerfile}")
44+
dp.update_env_in_dockerfile(version=version, what_to_replace="ENV NGINX_VERSION")
45+
dp.update_variable_in_dockerfile(version=version, variable="NGINX_VERSION")
46+
new_docker_file = dp.create_temp_dockerfile()
47+
48+
assert app.build_test_container(
49+
dockerfile=new_docker_file, app_url="https://github.com/sclorg/nginx-container.git",
50+
app_dir="nginx-container"
51+
)
52+
assert app.test_app_dockerfile()
53+
cip = app.get_cip()
54+
assert cip
55+
assert app.test_response(url=cip, expected_code=200,
56+
expected_output="NGINX is working")
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import pytest
2+
import re
3+
4+
from pathlib import Path
5+
6+
from container_ci_suite.container_lib import ContainerTestLib
7+
from container_ci_suite.engines.podman_wrapper import PodmanCLIWrapper
8+
from container_ci_suite.engines.container import ContainerImage
9+
10+
from constants import TEST_DIR, IMAGE_NAME, VERSION
11+
12+
test_app = TEST_DIR / "test-app"
13+
perl_test_app = TEST_DIR / "perl-test-app"
14+
start_hook_test_app = TEST_DIR / "start-hook-test-app"
15+
16+
17+
def build_s2i_app(app_path: Path) -> ContainerTestLib:
18+
container_lib = ContainerTestLib(IMAGE_NAME)
19+
app_name = app_path.name
20+
s2i_app = container_lib.build_as_df(
21+
app_path=app_path,
22+
s2i_args="--pull-policy=never",
23+
src_image=IMAGE_NAME,
24+
dst_image=f"{IMAGE_NAME}-{app_name}"
25+
)
26+
s2i_app.set_new_image(image_name=f"{IMAGE_NAME}-{app_name}")
27+
return s2i_app
28+
29+
30+
class TestNginxExampleAppContainer:
31+
32+
def setup_method(self):
33+
self.s2i_app = build_s2i_app(start_hook_test_app)
34+
35+
def teardown_method(self):
36+
self.s2i_app.cleanup()
37+
38+
def test_run_app_test(self, example_app_test):
39+
version = VERSION.replace("-micro", "")
40+
cid_file_name = example_app_test.app_name
41+
assert self.s2i_app.create_container(cid_file_name=cid_file_name, container_args="--user 10001")
42+
assert ContainerImage.wait_for_cid(cid_file_name=cid_file_name)
43+
cid = self.s2i_app.get_cid(cid_file_name=cid_file_name)
44+
assert cid
45+
cip = self.s2i_app.get_cip(cid_file_name=cid_file_name)
46+
assert cip
47+
command = PodmanCLIWrapper.podman_get_file_content(
48+
cid_file_name=cid, filename="/opt/app-root/etc/nginx.d/default.conf"
49+
)
50+
assert re.search("resolver", command)
51+
assert not re.search("DNS_SERVER", command)
52+
assert PodmanCLIWrapper.podman_run_command(
53+
f"--rm {example_app_test.image_name} /bin/bash -c 'nginx -v'"
54+
).startswith(f"nginx version: nginx/{version}")
55+
assert self.s2i_app.test_response(
56+
url=f"http://{cip}", expected_output="NGINX is working"
57+
)
58+
assert self.s2i_app.test_response(
59+
url=f"http://{cip}", expected_output="NGINX2 is working",
60+
host="localhost2"
61+
)
62+
assert self.s2i_app.test_response(
63+
url=f"http://{cip}", expected_code=404,
64+
page="/nginx-cfg/default.conf"
65+
)
66+
67+
68+
class TestNginxExamplePerlAppContainer:
69+
70+
def setup_method(self):
71+
self.s2i_app = build_s2i_app(perl_test_app)
72+
73+
def teardown_method(self):
74+
self.s2i_app.cleanup()
75+
76+
def test_run_app_test(self):
77+
if VERSION.endswith("-micro"):
78+
pytest.skip("Run the chosen tests (not for micro variant which lacks perl)")
79+
cid_file_name = self.s2i_app.app_name
80+
self.s2i_app.set_new_image(image_name=f"{IMAGE_NAME}-{cid_file_name}")
81+
assert self.s2i_app.create_container(cid_file_name=cid_file_name, container_args="--user 10001")
82+
cid = self.s2i_app.get_cid(cid_file_name=cid_file_name)
83+
assert cid
84+
cip = self.s2i_app.get_cip(cid_file_name=cid_file_name)
85+
assert cip
86+
perl_version = PodmanCLIWrapper.podman_exec_shell_command(cid_file_name=cid, cmd="perl -e 'print \"$^V\"'")
87+
assert self.s2i_app.test_response(
88+
url=f"http://{cip}", port=8080,
89+
expected_output=f"X-Perl-Version: {perl_version}"
90+
)
91+
assert self.s2i_app.test_response(
92+
url=f"http://{cip}", port=8080, page="/perl", expected_output="Perl location handler is working"
93+
)
94+
95+
class TestNginxLogContainer:
96+
97+
def setup_method(self):
98+
self.s2i_app = build_s2i_app(test_app)
99+
100+
def teardown_method(self):
101+
self.s2i_app.cleanup()
102+
103+
# test_log_output
104+
def test_log_output(self, s2i_log_test):
105+
cid_file_name = "test-app"
106+
s2i_log_test.set_new_image(image_name=f"{IMAGE_NAME}-{cid_file_name}")
107+
assert self.s2i_app.create_container(
108+
cid_file_name=cid_file_name,
109+
container_args="--user 10001"
110+
)
111+
cid = self.s2i_app.get_cid(cid_file_name=cid_file_name)
112+
assert cid
113+
cip = self.s2i_app.get_cip(cid_file_name=cid_file_name)
114+
assert self.s2i_app.test_response(
115+
url=f"http://{cip}", port=8080, expected_output="NGINX is working"
116+
)
117+
assert '"GET / HTTP/1.1" 200' in s2i_log_test.get_logs(cid_file_name=cid_file_name)
118+
assert self.s2i_app.test_response(
119+
url=f"http://{cip}", port=8080, page="/nothing-at-all", expected_code=404
120+
)
121+
logs = self.s2i_app.get_logs(cid_file_name=cid_file_name)
122+
assert logs
123+
assert re.search("open.*failed.*No such file or directory", logs)
124+
125+
# test_log_volume_output
126+
def test_log_volume_output(self, s2i_log_test):
127+
cid_file_name = "test-app"
128+
s2i_log_test.set_new_image(image_name=f"{IMAGE_NAME}-{cid_file_name}")
129+
assert self.s2i_app.create_container(
130+
cid_file_name=cid_file_name,
131+
container_args="-e NGINX_LOG_TO_VOLUME=y --user 10001"
132+
)
133+
cid = self.s2i_app.get_cid(cid_file_name=cid_file_name)
134+
assert cid
135+
cip = self.s2i_app.get_cip(cid_file_name=cid_file_name)
136+
assert self.s2i_app.test_response(url=f"http://{cip}", port=8080, expected_output="NGINX is working")
137+
assert '"GET / HTTP/1.1" 200' in PodmanCLIWrapper.podman_get_file_content(
138+
cid_file_name=cid,
139+
filename="/var/log/nginx/access.log"
140+
)
141+
assert self.s2i_app.test_response(
142+
url=f"http://{cip}", port=8080, page="/nothing-at-all", expected_code=404
143+
)
144+
assert '"GET /nothing-at-all HTTP/1.1" 404' in PodmanCLIWrapper.podman_get_file_content(
145+
cid_file_name=cid,
146+
filename="/var/log/nginx/access.log"
147+
)
148+
logs_to_check = PodmanCLIWrapper.podman_get_file_content(
149+
cid_file_name=cid,
150+
filename="/var/log/nginx/error.log"
151+
)
152+
assert re.search("open.*failed.*No such file or directory", logs_to_check)
153+

0 commit comments

Comments
 (0)