|
| 1 | +import re |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from container_ci_suite.container_lib import ContainerTestLib |
| 6 | +from container_ci_suite.engines.database import DatabaseWrapper |
| 7 | + |
| 8 | +from conftest import VARS |
| 9 | + |
| 10 | + |
| 11 | +def build_s2i_app(app_path: Path) -> ContainerTestLib: |
| 12 | + container_lib = ContainerTestLib(image_name=VARS.IMAGE_NAME, db_type="postgresql") |
| 13 | + app_name = app_path.name |
| 14 | + s2i_app = container_lib.build_as_df( |
| 15 | + app_path=app_path, |
| 16 | + s2i_args="--pull-policy=never", |
| 17 | + src_image=VARS.IMAGE_NAME, |
| 18 | + dst_image=f"{VARS.IMAGE_NAME}-{app_name}", |
| 19 | + ) |
| 20 | + return s2i_app |
| 21 | + |
| 22 | + |
| 23 | +class TestPostgreSQLS2ISSLContainer: |
| 24 | + """ |
| 25 | + Test PostgreSQL container configuration. |
| 26 | + """ |
| 27 | + |
| 28 | + def setup_method(self): |
| 29 | + """ |
| 30 | + Setup the test environment. |
| 31 | + """ |
| 32 | + self.ssl_db = build_s2i_app(app_path=VARS.TEST_DIR / "examples" / "enable-ssl") |
| 33 | + self.ssl_db.db_lib.db_type = "postgresql" |
| 34 | + self.dw_api = DatabaseWrapper(image_name=VARS.IMAGE_NAME, db_type="postgresql") |
| 35 | + |
| 36 | + def teardown_method(self): |
| 37 | + """ |
| 38 | + Teardown the test environment. |
| 39 | + """ |
| 40 | + self.ssl_db.cleanup() |
| 41 | + |
| 42 | + def test_ssl(self): |
| 43 | + """ |
| 44 | + Test SSL. |
| 45 | + """ |
| 46 | + cid_ssl_name = "enable-ssl-test" |
| 47 | + admin_password = "password" |
| 48 | + |
| 49 | + assert self.ssl_db.create_container( |
| 50 | + cid_file_name=cid_ssl_name, |
| 51 | + container_args=[ |
| 52 | + f"-e POSTGRESQL_ADMIN_PASSWORD={admin_password}", |
| 53 | + ], |
| 54 | + ) |
| 55 | + ssl_cip, ssl_cid = self.ssl_db.get_cip_cid(cid_file_name=cid_ssl_name) |
| 56 | + assert ssl_cip and ssl_cid |
| 57 | + assert self.dw_api.wait_for_database( |
| 58 | + container_id=ssl_cid, |
| 59 | + command="/usr/libexec/check-container", |
| 60 | + ) |
| 61 | + assert self.dw_api.assert_login_access( |
| 62 | + container_ip=ssl_cip, |
| 63 | + username="postgres", |
| 64 | + password=admin_password, |
| 65 | + database="postgres", |
| 66 | + expected_success=True, |
| 67 | + ) |
| 68 | + |
| 69 | + output = self.dw_api.postgresql_cmd( |
| 70 | + container_ip=ssl_cip, |
| 71 | + container_id=VARS.IMAGE_NAME, |
| 72 | + username="postgres", |
| 73 | + password=admin_password, |
| 74 | + database="postgres", |
| 75 | + uri_params={"sslmode": "require"}, |
| 76 | + sql_command="-At -c 'SELECT 1;'", |
| 77 | + ) |
| 78 | + assert re.search(r"1", output), f"1 not found in {output}" |
| 79 | + |
| 80 | + |
| 81 | +class TestPostgreSQLS2IBakeDataContainer: |
| 82 | + """ |
| 83 | + Test PostgreSQL container configuration. |
| 84 | + """ |
| 85 | + |
| 86 | + def setup_method(self): |
| 87 | + """ |
| 88 | + Setup the test environment. |
| 89 | + """ |
| 90 | + self.ssl_db = build_s2i_app( |
| 91 | + app_path=VARS.TEST_DIR / "examples" / "s2i-dump-data" |
| 92 | + ) |
| 93 | + self.ssl_db.db_lib.db_type = "postgresql" |
| 94 | + self.dw_api = DatabaseWrapper(image_name=VARS.IMAGE_NAME, db_type="postgresql") |
| 95 | + |
| 96 | + def teardown_method(self): |
| 97 | + """ |
| 98 | + Teardown the test environment. |
| 99 | + """ |
| 100 | + self.ssl_db.cleanup() |
| 101 | + |
| 102 | + def test_ssl(self): |
| 103 | + """ |
| 104 | + Test SSL. |
| 105 | + """ |
| 106 | + cid_ssl_name = "bake-data-test" |
| 107 | + admin_password = "password" |
| 108 | + |
| 109 | + assert self.ssl_db.create_container( |
| 110 | + cid_file_name=cid_ssl_name, |
| 111 | + container_args=[ |
| 112 | + f"-e POSTGRESQL_ADMIN_PASSWORD={admin_password}", |
| 113 | + ], |
| 114 | + ) |
| 115 | + ssl_cip, ssl_cid = self.ssl_db.get_cip_cid(cid_file_name=cid_ssl_name) |
| 116 | + assert ssl_cip and ssl_cid |
| 117 | + assert self.dw_api.wait_for_database( |
| 118 | + container_id=ssl_cid, |
| 119 | + command="/usr/libexec/check-container", |
| 120 | + ) |
| 121 | + assert self.dw_api.assert_login_access( |
| 122 | + container_ip=ssl_cip, |
| 123 | + username="postgres", |
| 124 | + password=admin_password, |
| 125 | + database="postgres", |
| 126 | + expected_success=True, |
| 127 | + ) |
| 128 | + |
| 129 | + output = self.dw_api.postgresql_cmd( |
| 130 | + container_ip=ssl_cip, |
| 131 | + container_id=VARS.IMAGE_NAME, |
| 132 | + username="postgres", |
| 133 | + password=admin_password, |
| 134 | + database="postgres", |
| 135 | + sql_command="-At -c 'SELECT * FROM test;'", |
| 136 | + ) |
| 137 | + assert re.search(r"hello world", output), f"hello world not found in {output}" |
0 commit comments