|
| 1 | +import shutil |
| 2 | +import tempfile |
| 3 | + |
| 4 | +from container_ci_suite.container_lib import ContainerTestLib |
| 5 | +from container_ci_suite.container_lib import ContainerTestLibUtils |
| 6 | +from container_ci_suite.engines.podman_wrapper import PodmanCLIWrapper |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +from conftest import VARS |
| 10 | + |
| 11 | + |
| 12 | +def build_s2i_app(app_path: Path) -> ContainerTestLib: |
| 13 | + container_lib = ContainerTestLib(VARS.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=VARS.IMAGE_NAME, |
| 19 | + dst_image=f"{VARS.IMAGE_NAME}-{app_name}", |
| 20 | + ) |
| 21 | + return s2i_app |
| 22 | + |
| 23 | + |
| 24 | +class TestMySqlBasicsContainer: |
| 25 | + """ |
| 26 | + Test MySQL container configuration. |
| 27 | + """ |
| 28 | + |
| 29 | + def setup_method(self): |
| 30 | + self.s2i_db = build_s2i_app(app_path=VARS.TEST_DIR / "test-app") |
| 31 | + self.s2i_db.set_new_db_type(db_type="mysql") |
| 32 | + |
| 33 | + def teardown_method(self): |
| 34 | + self.s2i_db.cleanup() |
| 35 | + |
| 36 | + def test_s2i_usage(self): |
| 37 | + """ |
| 38 | + Test container creation fails with invalid combinations of arguments. |
| 39 | + """ |
| 40 | + cid_config_build = "s2i_config_build" |
| 41 | + self.s2i_db.assert_container_creation_fails( |
| 42 | + cid_file_name=cid_config_build, |
| 43 | + command="", |
| 44 | + container_args=[ |
| 45 | + "-e MYSQL_USER=root", |
| 46 | + "-e MYSQL_PASSWORD=pass", |
| 47 | + "-e MYSQL_DATABASE=db", |
| 48 | + "-e MYSQL_ROOT_PASSWORD=pass", |
| 49 | + ], |
| 50 | + ) |
| 51 | + assert self.s2i_db.create_container( |
| 52 | + cid_file_name=cid_config_build, |
| 53 | + container_args=[ |
| 54 | + "-e MYSQL_USER=config_test_user", |
| 55 | + "-e MYSQL_PASSWORD=config_test_user", |
| 56 | + "-e MYSQL_DATABASE=db", |
| 57 | + "-e MYSQL_OPERATIONS_USER=operations_user", |
| 58 | + "-e MYSQL_OPERATIONS_PASSWORD=operations_user", |
| 59 | + ], |
| 60 | + ) |
| 61 | + cip = self.s2i_db.get_cip(cid_file_name=cid_config_build) |
| 62 | + assert cip |
| 63 | + assert self.s2i_db.test_db_connection( |
| 64 | + container_ip=cip, username="operations_user", password="operations_user" |
| 65 | + ) |
| 66 | + cid = self.s2i_db.get_cid(cid_file_name=cid_config_build) |
| 67 | + db_configuration = PodmanCLIWrapper.podman_exec_shell_command( |
| 68 | + cid_file_name=cid, |
| 69 | + cmd="cat /etc/my.cnf /etc/my.cnf.d/*", |
| 70 | + ) |
| 71 | + assert db_configuration |
| 72 | + PodmanCLIWrapper.call_podman_command(cmd=f"stop {cid}") |
| 73 | + |
| 74 | + def test_s2i_usage_with_mount(self): |
| 75 | + """ |
| 76 | + Test container creation fails with invalid combinations of arguments. |
| 77 | + """ |
| 78 | + data_dir = tempfile.mkdtemp(prefix="/tmp/mysql-test_data") |
| 79 | + shutil.copytree(VARS.TEST_DIR / "test-app", f"{data_dir}/test-app") |
| 80 | + assert ContainerTestLibUtils.commands_to_run( |
| 81 | + commands_to_run=[ |
| 82 | + f"chown -R 27:27 {data_dir}/test-app", |
| 83 | + ] |
| 84 | + ) |
| 85 | + cid_s2i_test_mount = "s2i_test_mount" |
| 86 | + self.s2i_db.create_container( |
| 87 | + cid_file_name=cid_s2i_test_mount, |
| 88 | + container_args=[ |
| 89 | + "-e MYSQL_USER=config_test_user", |
| 90 | + "-e MYSQL_PASSWORD=config_test_user", |
| 91 | + "-e MYSQL_DATABASE=db", |
| 92 | + "-e MYSQL_OPERATIONS_USER=operations_user", |
| 93 | + "-e MYSQL_OPERATIONS_PASSWORD=operations_pass", |
| 94 | + f"-v {data_dir}/test-app:/opt/app-root/src/:z", |
| 95 | + ], |
| 96 | + ) |
| 97 | + cip_test_mount = self.s2i_db.get_cip(cid_file_name=cid_s2i_test_mount) |
| 98 | + assert cip_test_mount |
| 99 | + assert self.s2i_db.test_db_connection( |
| 100 | + container_ip=cip_test_mount, |
| 101 | + username="operations_user", |
| 102 | + password="operations_pass", |
| 103 | + max_attempts=10, |
| 104 | + ) |
| 105 | + cid = self.s2i_db.get_cid(cid_file_name=cid_s2i_test_mount) |
| 106 | + assert cid |
| 107 | + db_configuration = PodmanCLIWrapper.podman_exec_shell_command( |
| 108 | + cid_file_name=cid, |
| 109 | + cmd="cat /etc/my.cnf /etc/my.cnf.d/*", |
| 110 | + ) |
| 111 | + assert db_configuration |
| 112 | + PodmanCLIWrapper.call_podman_command(cmd=f"stop {cid}") |
| 113 | + shutil.rmtree(data_dir) |
0 commit comments