11import os
2+ import sys
23import time
34import tempfile
45
56from pathlib import Path
67
78from container_ci_suite .container_lib import ContainerTestLib
8- from container_ci_suite .utils import ContainerTestLibUtils
9+ from container_ci_suite .utils import ContainerTestLibUtils , check_variables
910from container_ci_suite .engines .podman_wrapper import PodmanCLIWrapper
1011
12+ if not check_variables ():
13+ sys .exit (1 )
1114
1215TEST_DIR = Path (__file__ ).parent .absolute ()
1316VERSION = os .getenv ("VERSION" )
14- OS = os .getenv ("TARGET" )
17+ OS = os .getenv ("OS" ). lower ( )
1518IMAGE_NAME = os .getenv ("IMAGE_NAME" )
1619
1720
1821pre_init_test_app = TEST_DIR / "pre-init-test-app"
1922sample_test_app = TEST_DIR / "sample-test-app"
23+ self_cert_test = TEST_DIR / "self-signed-ssl"
24+
25+
26+ def build_s2i_app (app_path : Path ) -> ContainerTestLib :
27+ container_lib = ContainerTestLib (IMAGE_NAME )
28+ app_name = app_path .name
29+ s2i_app = container_lib .build_as_df (
30+ app_path = app_path ,
31+ s2i_args = "--pull-policy=never" ,
32+ src_image = IMAGE_NAME ,
33+ dst_image = f"{ IMAGE_NAME } -{ app_name } "
34+ )
35+ return s2i_app
2036
2137
2238class TestHttpdS2IPreInitContainer :
2339
2440 def setup_method (self ):
25- self .container_lib = ContainerTestLib (IMAGE_NAME )
26- print (self .container_lib )
27- app_name = pre_init_test_app .name
28- print (app_name )
29- self .s2i_app = self .container_lib .build_as_df (
30- app_path = pre_init_test_app ,
31- s2i_args = "--pull-policy=never" ,
32- src_image = IMAGE_NAME ,
33- dst_image = f"{ IMAGE_NAME } -{ app_name } "
34- )
41+ self .s2i_app = build_s2i_app (pre_init_test_app )
3542
3643 def teardown_method (self ):
3744 self .s2i_app .cleanup ()
@@ -50,14 +57,7 @@ def test_run_pre_init_test(self):
5057class TestHttpdS2ISampleAppContainer :
5158
5259 def setup_method (self ):
53- self .ci = ContainerTestLib (IMAGE_NAME )
54- app_name = sample_test_app .name
55- self .s2i_app = self .ci .build_as_df (
56- app_path = sample_test_app ,
57- s2i_args = "--pull-policy=never" ,
58- src_image = IMAGE_NAME ,
59- dst_image = f"{ IMAGE_NAME } -{ app_name } "
60- )
60+ self .s2i_app = build_s2i_app (sample_test_app )
6161
6262 def teardown_method (self ):
6363 self .s2i_app .cleanup ()
@@ -82,25 +82,18 @@ def test_sample_app(self):
8282class TestHttpdCertAgeContainer :
8383
8484 def setup_method (self ):
85- self .ci = ContainerTestLib (IMAGE_NAME )
86- app_name = sample_test_app .name
87- self .s2i_app = self .ci .build_as_df (
88- app_path = sample_test_app ,
89- s2i_args = "--pull-policy=never" ,
90- src_image = IMAGE_NAME ,
91- dst_image = f"{ IMAGE_NAME } -{ app_name } "
92- )
85+ self .s2i_app = build_s2i_app (sample_test_app )
9386
9487 def teardown_method (self ):
9588 self .s2i_app .cleanup ()
9689
97- """
98- This tests checks whether the certificate was freshly generated after the image
99- We need to make sure the certificate is generated no sooner than in assemble phase,
100- because shipping the same certs in the image would make it easy to exploit
101- Let's see how old the certificate is and compare with how old the image is
102- """
10390 def test_cert_age (self ):
91+ """
92+ This tests checks whether the certificate was freshly generated after the image
93+ We need to make sure the certificate is generated no sooner than in assemble phase,
94+ because shipping the same certs in the image would make it easy to exploit
95+ Let's see how old the certificate is and compare with how old the image is
96+ """
10497 assert self .s2i_app .create_container (cid_file_name = self .s2i_app .app_name , container_args = "--user 1000" )
10598 image_age_s = PodmanCLIWrapper .podman_inspect (
10699 field = "{{.Created}}" , src_image = IMAGE_NAME
@@ -132,4 +125,36 @@ def test_cert_age(self):
132125 # Testing presence and permissions of the generated certificate
133126 assert PodmanCLIWrapper .podman_exec_shell_command (
134127 cid_file_name = cid , cmd = "ls -l \\ $HTTPD_TLS_CERT_PATH/localhost.key"
135- )
128+ )
129+
130+ class TestHttpdS2ISslSelfSignedAppContainer :
131+
132+ def setup_method (self ):
133+ self .s2i_app = build_s2i_app (self_cert_test )
134+
135+ def teardown_method (self ):
136+ self .s2i_app .cleanup ()
137+
138+ def test_self_cert_test (self ):
139+ """
140+ Test s2i use case #3 - using own ssl certs
141+ Since we built the candidate image locally, we don't want S2I attempt to pull
142+ it from Docker hub
143+ """
144+ self .s2i_app .set_new_image (image_name = f"{ IMAGE_NAME } -{ self .s2i_app .app_name } " )
145+ assert self .s2i_app .create_container (cid_file_name = self .s2i_app .app_name , container_args = "--user 1000" )
146+ cip = self .s2i_app .get_cip (cid_file_name = self .s2i_app .app_name )
147+ assert cip
148+ assert self .s2i_app .test_response (url = cip , expected_code = 200 , expected_output = "SSL test works" )
149+ assert self .s2i_app .test_response (url = f"https://{ cip } " , port = 8443 , expected_output = "SSL test works" )
150+ server_cmd = f"openssl s_client -showcerts -servername { cip } -connect { cip } :8443 2>/dev/null"
151+ server_output = ContainerTestLibUtils .run_command (cmd = server_cmd )
152+ certificate_dir = tempfile .mkdtemp (prefix = "/tmp/server_cert_dir" )
153+ with open (Path (certificate_dir ) / "output" , mode = "wt+" ) as f :
154+ f .write (server_output )
155+ server_cert = ContainerTestLibUtils .run_command (
156+ cmd = f"openssl x509 -inform pem -noout -text -in { Path (certificate_dir )} /output"
157+ )
158+ config_cmd = f"openssl x509 -in { TEST_DIR } /{ self .s2i_app .app_name } /httpd-ssl/certs/server-cert-selfsigned.pem -inform pem -noout -text"
159+ config_cert = ContainerTestLibUtils .run_command (cmd = config_cmd )
160+ assert server_cert == config_cert
0 commit comments