Skip to content

Commit 0b9003c

Browse files
committed
Update code based on the already reviewed mariadb-container
sclorg/mariadb-container#311 Signed-off-by: Petr "Stone" Hracek <phracek@redhat.com>
1 parent cf87fd8 commit 0b9003c

File tree

5 files changed

+267
-265
lines changed

5 files changed

+267
-265
lines changed

test/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
VERSION = os.getenv("VERSION")
3232
OS = os.getenv("TARGET").lower()
3333
TEST_APP = TEST_DIR / "test-app"
34-
VERY_LONG_DB_NAME = "very_long_database_name_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
35-
VERY_LONG_USER_NAME = "very_long_user_name_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
34+
VERY_LONG_DB_NAME = "very_long_database_name_" + "x" * 40
35+
VERY_LONG_USER_NAME = "very_long_user_name_" + "x" * 40
3636
VARS = Vars(
3737
OS=OS,
3838
VERSION=VERSION,

test/test_container_basics.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,28 @@ class TestMySqlBasicsContainer:
2727
"""
2828

2929
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")
30+
"""
31+
Setup the test environment.
32+
"""
33+
self.app_image = build_s2i_app(app_path=VARS.TEST_DIR / "test-app")
34+
self.app_image.set_new_db_type(db_type="mysql")
3235

3336
def teardown_method(self):
34-
self.s2i_db.cleanup()
37+
"""
38+
Teardown the test environment.
39+
"""
40+
self.app_image.cleanup()
3541

3642
def test_s2i_usage(self):
3743
"""
38-
Test container creation fails with invalid combinations of arguments.
44+
Test if MySQL container failed in case of invalid combinations.
45+
Steps are:
46+
1. Test if the container creation fails with invalid combinations of arguments
47+
2. Test if the container creation succeeds with valid combinations of arguments
48+
3. Test if the database connection works
3949
"""
40-
cid_config_build = "s2i_config_build"
41-
self.s2i_db.assert_container_creation_fails(
50+
cid_config_build = "s2i_usage_build"
51+
self.app_image.assert_container_creation_fails(
4252
cid_file_name=cid_config_build,
4353
command="",
4454
container_args=[
@@ -48,7 +58,7 @@ def test_s2i_usage(self):
4858
"-e MYSQL_ROOT_PASSWORD=pass",
4959
],
5060
)
51-
assert self.s2i_db.create_container(
61+
assert self.app_image.create_container(
5262
cid_file_name=cid_config_build,
5363
container_args=[
5464
"-e MYSQL_USER=config_test_user",
@@ -58,22 +68,20 @@ def test_s2i_usage(self):
5868
"-e MYSQL_OPERATIONS_PASSWORD=operations_user",
5969
],
6070
)
61-
cip = self.s2i_db.get_cip(cid_file_name=cid_config_build)
62-
assert cip
63-
assert self.s2i_db.test_db_connection(
71+
cip, cid = self.app_image.get_cip_cid(cid_file_name=cid_config_build)
72+
assert cip, cid
73+
assert self.app_image.test_db_connection(
6474
container_ip=cip, username="operations_user", password="operations_user"
6575
)
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
7276
PodmanCLIWrapper.call_podman_command(cmd=f"stop {cid}")
7377

7478
def test_s2i_usage_with_mount(self):
7579
"""
76-
Test container creation fails with invalid combinations of arguments.
80+
Test if the MySQL container works properly with mounted application directory.
81+
Steps are:
82+
1. Copy the test-app directory to a temporary directory and set proper permissions
83+
2. Create a container with the mounted directory
84+
3. Test if the database connection works with the operations user
7785
"""
7886
data_dir = tempfile.mkdtemp(prefix="/tmp/mysql-test_data")
7987
shutil.copytree(VARS.TEST_DIR / "test-app", f"{data_dir}/test-app")
@@ -83,7 +91,7 @@ def test_s2i_usage_with_mount(self):
8391
]
8492
)
8593
cid_s2i_test_mount = "s2i_test_mount"
86-
self.s2i_db.create_container(
94+
self.app_image.create_container(
8795
cid_file_name=cid_s2i_test_mount,
8896
container_args=[
8997
"-e MYSQL_USER=config_test_user",
@@ -94,15 +102,13 @@ def test_s2i_usage_with_mount(self):
94102
f"-v {data_dir}/test-app:/opt/app-root/src/:z",
95103
],
96104
)
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,
105+
cip, cid = self.app_image.get_cip_cid(cid_file_name=cid_s2i_test_mount)
106+
assert cip, cid
107+
assert self.app_image.test_db_connection(
108+
container_ip=cip,
101109
username="operations_user",
102110
password="operations_pass",
103111
max_attempts=10,
104112
)
105-
cid = self.s2i_db.get_cid(cid_file_name=cid_s2i_test_mount)
106-
assert cid
107113
PodmanCLIWrapper.call_podman_command(cmd=f"stop {cid}")
108114
shutil.rmtree(data_dir)

test/test_container_configuration.py

Lines changed: 93 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -52,60 +52,81 @@ def test_try_image_invalid_combinations(self, container_args):
5252
)
5353

5454
@pytest.mark.parametrize(
55-
"container_args",
55+
"mysql_user, mysql_password, mysql_database, mysql_root_password",
5656
[
57-
["-e", "MYSQL_USER=user", "-e", "MYSQL_PASSWORD=pass"],
57+
["user", "pass", "", ""],
5858
[
59-
"-e MYSQL_USER=$invalid",
60-
"-e MYSQL_PASSWORD=pass",
61-
"-e MYSQL_DATABASE=db",
62-
"-e MYSQL_ROOT_PASSWORD=root_pass",
59+
"$invalid",
60+
"pass",
61+
"db",
62+
"root_pass",
6363
],
6464
[
65-
f"-e MYSQL_USER={VARS.VERY_LONG_USER_NAME}",
66-
"-e MYSQL_PASSWORD=pass",
67-
"-e MYSQL_DATABASE=db",
68-
"-e MYSQL_ROOT_PASSWORD=root_pass",
65+
VARS.VERY_LONG_USER_NAME,
66+
"pass",
67+
"db",
68+
"root_pass",
6969
],
7070
[
71-
"-e MYSQL_USER=user",
72-
"-e MYSQL_PASSWORD=",
73-
"-e MYSQL_DATABASE=db",
74-
"-e MYSQL_ROOT_PASSWORD=root_pass",
71+
"user",
72+
"",
73+
"db",
74+
"root_pass",
7575
],
7676
[
77-
"-e MYSQL_USER=user",
78-
"-e MYSQL_PASSWORD=pass",
79-
"-e MYSQL_DATABASE=$invalid",
80-
"-e MYSQL_ROOT_PASSWORD=root_pass",
77+
"user",
78+
"pass",
79+
"$invalid",
80+
"root_pass",
8181
],
8282
[
83-
"-e MYSQL_USER=user",
84-
"-e MYSQL_PASSWORD=pass",
85-
f"-e MYSQL_DATABASE={VARS.VERY_LONG_DB_NAME}",
86-
"-e MYSQL_ROOT_PASSWORD=root_pass",
83+
"user",
84+
"pass",
85+
VARS.VERY_LONG_DB_NAME,
86+
"root_pass",
8787
],
8888
[
89-
"-e MYSQL_USER=user",
90-
"-e MYSQL_PASSWORD=pass",
91-
"-e MYSQL_DATABASE=db",
92-
"-e MYSQL_ROOT_PASSWORD=",
89+
"user",
90+
"pass",
91+
"db",
92+
"",
9393
],
9494
[
95-
"-e MYSQL_USER=root",
96-
"-e MYSQL_PASSWORD=pass",
97-
"-e MYSQL_DATABASE=db",
98-
"-e MYSQL_ROOT_PASSWORD=pass",
95+
"root",
96+
"pass",
97+
"db",
98+
"pass",
9999
],
100100
],
101101
)
102-
def test_invalid_configuration_tests(self, container_args):
102+
def test_invalid_configuration_tests(
103+
self, mysql_user, mysql_password, mysql_database, mysql_root_password
104+
):
103105
"""
104106
Test invalid configuration combinations for MySQL container.
105107
"""
106108
cid_config_test = "invalid_configuration_tests"
109+
mysql_user_arg = f"-e MYSQL_USER={mysql_user}" if mysql_user else ""
110+
mysql_password_arg = (
111+
f"-e MYSQL_PASSWORD={mysql_password}" if mysql_password else ""
112+
)
113+
mysql_database_arg = (
114+
f"-e MYSQL_DATABASE={mysql_database}" if mysql_database else ""
115+
)
116+
mysql_root_password_arg = (
117+
f"-e MYSQL_ROOT_PASSWORD={mysql_root_password}"
118+
if mysql_root_password
119+
else ""
120+
)
107121
assert self.db.assert_container_creation_fails(
108-
cid_file_name=cid_config_test, container_args=container_args, command=""
122+
cid_file_name=cid_config_test,
123+
container_args=[
124+
mysql_user_arg,
125+
mysql_password_arg,
126+
mysql_database_arg,
127+
mysql_root_password_arg,
128+
],
129+
command="",
109130
)
110131

111132

@@ -119,7 +140,6 @@ def setup_method(self):
119140
Setup the test environment.
120141
"""
121142
self.db_config = ContainerTestLib(image_name=VARS.IMAGE_NAME)
122-
self.db_config.set_new_db_type(db_type="mysql")
123143
self.db_api = DatabaseWrapper(image_name=VARS.IMAGE_NAME)
124144

125145
def teardown_method(self):
@@ -131,6 +151,12 @@ def teardown_method(self):
131151
def test_configuration_auto_calculated_settings(self):
132152
"""
133153
Test MySQL container configuration auto-calculated settings.
154+
Steps are:
155+
1. Create a container with the given arguments
156+
2. Check if the container is created successfully
157+
3. Check if the database connection works
158+
4. Check if the database configurations are correct
159+
5. Stop the container
134160
"""
135161
cid_config_test = "auto-config_test"
136162
username = "config_test_user"
@@ -146,30 +172,29 @@ def test_configuration_auto_calculated_settings(self):
146172
],
147173
docker_args="--memory=512m",
148174
)
149-
cip = self.db_config.get_cip(cid_file_name=cid_config_test)
150-
assert cip
175+
cip, cid = self.db_config.get_cip_cid(cid_file_name=cid_config_test)
176+
assert cip, cid
151177
assert self.db_config.test_db_connection(
152178
container_ip=cip,
153179
username=username,
154180
password=password,
155181
max_attempts=10,
156182
)
157-
cid = self.db_config.get_cid(cid_file_name=cid_config_test)
158183
db_configuration = PodmanCLIWrapper.podman_exec_shell_command(
159184
cid_file_name=cid,
160185
cmd="cat /etc/my.cnf /etc/my.cnf.d/*",
161186
)
162-
words = [
163-
"key_buffer_size\\s*=\\s*51M",
164-
"read_buffer_size\\s*=\\s*25M",
165-
"innodb_buffer_pool_size\\s*=\\s*256M",
166-
"innodb_log_file_size\\s*=\\s*76M",
167-
"innodb_log_buffer_size\\s*=\\s*76M",
168-
"authentication_policy\\s*=\\s*'caching_sha2_password,,'",
187+
expected_values = [
188+
r"key_buffer_size\s*=\s*51M",
189+
r"read_buffer_size\s*=\s*25M",
190+
r"innodb_buffer_pool_size\s*=\s*256M",
191+
r"innodb_log_file_size\s*=\s*76M",
192+
r"innodb_log_buffer_size\s*=\s*76M",
193+
r"authentication_policy\s*=\s*'caching_sha2_password,,'",
169194
]
170-
for word in words:
171-
assert re.search(word, db_configuration), (
172-
f"Word {word} not found in {db_configuration}"
195+
for value in expected_values:
196+
assert re.search(value, db_configuration), (
197+
f"Expected value {value} not found in {db_configuration}"
173198
)
174199
# do some real work to test replication in practice
175200
self.db_api.run_sql_command(
@@ -192,6 +217,7 @@ def test_configuration_auto_calculated_settings(self):
192217
assert "COLLATE=latin2_czech_cs" in show_table_output
193218
PodmanCLIWrapper.call_podman_command(cmd=f"stop {cid}")
194219

220+
# FIX
195221
def test_configuration_options_settings(self):
196222
"""
197223
Test MySQL container configuration options.
@@ -218,44 +244,39 @@ def test_configuration_options_settings(self):
218244
"--env MYSQL_INNODB_BUFFER_POOL_SIZE=16M",
219245
"--env MYSQL_INNODB_LOG_FILE_SIZE=4M",
220246
"--env MYSQL_INNODB_LOG_BUFFER_SIZE=4M",
221-
"--env MYSQL_AUTHENTICATION_POLICY=sha256_password",
247+
"--env MYSQL_AUTHENTICATION_POLICY=sha256_password,,",
248+
"--env WORKAROUND_DOCKER_BUG_14203=",
222249
],
223250
)
224-
cip = self.db_config.get_cip(cid_file_name=cid_config_test)
225-
assert cip
226-
assert self.db_config.test_db_connection(
227-
container_ip=cip, username=username, password=password
228-
)
229-
cip = self.db_config.get_cip(cid_file_name=cid_config_test)
230-
assert cip
251+
cip, cid = self.db_config.get_cip_cid(cid_file_name=cid_config_test)
252+
assert cip, cid
231253
assert self.db_config.test_db_connection(
232254
container_ip=cip,
233255
username=username,
234256
password=password,
235257
max_attempts=10,
236258
)
237-
cid = self.db_config.get_cid(cid_file_name=cid_config_test)
238259
db_configuration = PodmanCLIWrapper.podman_exec_shell_command(
239260
cid_file_name=cid,
240261
cmd="cat /etc/my.cnf /etc/my.cnf.d/*",
241262
)
242-
words = [
243-
"lower_case_table_names\\s*=\\s*1",
244-
"general_log\\s*=\\s*1",
245-
"max_connections\\s*=\\s*1337",
246-
"ft_min_word_len\\s*=\\s*8",
247-
"ft_max_word_len\\s*=\\s*15",
248-
"max_allowed_packet\\s*=\\s*10M",
249-
"table_open_cache\\s*=\\s*100",
250-
"sort_buffer_size\\s*=\\s*256K",
251-
"key_buffer_size\\s*=\\s*16M",
252-
"read_buffer_size\\s*=\\s*16M",
253-
"innodb_log_file_size\\s*=\\s*4M",
254-
"innodb_log_buffer_size\\s*=\\s*4M",
255-
"authentication_policy\\s*=\\s*'sha256_password'",
263+
expected_values = [
264+
r"lower_case_table_names\s*=\s*1",
265+
r"general_log\s*=\s*1",
266+
r"max_connections\s*=\s*1337",
267+
r"ft_min_word_len\s*=\s*8",
268+
r"ft_max_word_len\s*=\s*15",
269+
r"max_allowed_packet\s*=\s*10M",
270+
r"table_open_cache\s*=\s*100",
271+
r"sort_buffer_size\s*=\s*256K",
272+
r"key_buffer_size\s*=\s*16M",
273+
r"read_buffer_size\s*=\s*16M",
274+
r"innodb_log_file_size\s*=\s*4M",
275+
r"innodb_log_buffer_size\s*=\s*4M",
276+
r"authentication_policy\s*=\s*'sha256_password,,'",
256277
]
257-
for word in words:
258-
assert re.search(word, db_configuration), (
259-
f"Word {word} not found in {db_configuration}"
278+
for value in expected_values:
279+
assert re.search(value, db_configuration), (
280+
f"Expected value {value} not found in {db_configuration}"
260281
)
261282
PodmanCLIWrapper.call_podman_command(cmd=f"stop {cid}")

0 commit comments

Comments
 (0)