Skip to content

Commit cfd849d

Browse files
committed
Add container tests including replication
Signed-off-by: Petr "Stone" Hracek <phracek@redhat.com>
1 parent 4b1bc0f commit cfd849d

File tree

4 files changed

+155
-40
lines changed

4 files changed

+155
-40
lines changed

test/run

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
set -o nounset
1010
shopt -s nullglob
11-
1211
THISDIR=$(dirname ${BASH_SOURCE[0]})
1312
source ${THISDIR}/test-lib.sh
1413

test/test_container_password.py

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@ class TestMySqlPasswordContainer:
1313
"""
1414

1515
def setup_method(self):
16-
self.ssl_db = ContainerTestLib(image_name=VARS.IMAGE_NAME)
17-
self.ssl_db.set_new_db_type(db_type="mysql")
18-
self.db_connector = DatabaseWrapper(image_name=VARS.IMAGE_NAME, db_type="mysql")
16+
self.pwd_change = ContainerTestLib(image_name=VARS.IMAGE_NAME)
17+
self.pwd_change.set_new_db_type(db_type="mysql")
1918

2019
def teardown_method(self):
21-
self.ssl_db.cleanup()
20+
self.pwd_change.cleanup()
2221

2322
def test_password_change(self):
2423
""" """
25-
cid_file_name = "test_password_change"
24+
cid_file_name1 = "test_password_change"
2625
pwd_dir = tempfile.mkdtemp(prefix="/tmp/mysql-pwd")
2726
username = "user"
2827
password = "foo"
@@ -31,41 +30,50 @@ def test_password_change(self):
3130
f"chmod -R a+rwx {pwd_dir}",
3231
]
3332
)
34-
assert self.ssl_db.create_container(
35-
cid_file_name=cid_file_name,
33+
assert self.pwd_change.create_container(
34+
cid_file_name=cid_file_name1,
3635
container_args=[
3736
f"-e MYSQL_USER={username}",
3837
f"-e MYSQL_PASSWORD={password}",
3938
"-e MYSQL_DATABASE=db",
4039
f"-v {pwd_dir}:/var/lib/mysql/data:Z",
4140
],
4241
)
43-
cip = self.ssl_db.get_cip(cid_file_name=cid_file_name)
44-
assert cip
45-
assert self.ssl_db.test_db_connection(
46-
container_ip=cip, username=username, password=password
42+
cip1 = self.pwd_change.get_cip(cid_file_name=cid_file_name1)
43+
assert cip1
44+
assert self.pwd_change.test_db_connection(
45+
container_ip=cip1, username=username, password=password
4746
)
48-
cid = self.ssl_db.get_cid(cid_file_name=cid_file_name)
49-
assert cid
50-
PodmanCLIWrapper.call_podman_command(cmd=f"stop {cid}")
51-
cid_file_name = "test_password_change_2"
47+
cid1 = self.pwd_change.get_cid(cid_file_name=cid_file_name1)
48+
assert cid1
49+
PodmanCLIWrapper.call_podman_command(cmd=f"stop {cid1}")
50+
cid_file_name2 = "test_password_change_2"
5251
new_password = "bar"
53-
assert self.ssl_db.create_container(
54-
cid_file_name=cid_file_name,
52+
assert self.pwd_change.create_container(
53+
cid_file_name=cid_file_name2,
5554
container_args=[
5655
f"-e MYSQL_USER={username}",
5756
f"-e MYSQL_PASSWORD={new_password}",
5857
"-e MYSQL_DATABASE=db",
5958
f"-v {pwd_dir}:/var/lib/mysql/data:Z",
6059
],
6160
)
61+
cip2 = self.pwd_change.get_cip(cid_file_name=cid_file_name2)
62+
assert cip2
63+
assert self.pwd_change.test_db_connection(
64+
container_ip=cip2, username=username, password=new_password
65+
)
6266
podman_cmd = (
63-
f"--rm {VARS.IMAGE_NAME} mysql --host {cip} -u{username} -p{password}"
67+
f"--rm {VARS.IMAGE_NAME} mysql --host {cip2} -u{username} -p{password}"
6468
)
6569
output = PodmanCLIWrapper.podman_run_command(
6670
cmd=f"{podman_cmd} -e 'SELECT 1;' db",
71+
ignore_error=True,
72+
)
73+
print(output)
74+
assert f"Access denied for user '{username}'@" in output, (
75+
f"The old password {password} should not work, but it does"
6776
)
68-
assert output == "1"
6977

7078
def test_password_change_new_user_test(self):
7179
""" """
@@ -78,7 +86,7 @@ def test_password_change_new_user_test(self):
7886
f"chmod -R a+rwx {pwd_dir}",
7987
]
8088
)
81-
assert self.ssl_db.create_container(
89+
assert self.pwd_change.create_container(
8290
cid_file_name=cid_file_name,
8391
container_args=[
8492
f"-e MYSQL_USER={username1}",
@@ -87,19 +95,19 @@ def test_password_change_new_user_test(self):
8795
f"-v {pwd_dir}:/var/lib/mysql/data:Z",
8896
],
8997
)
90-
cip = self.ssl_db.get_cip(cid_file_name=cid_file_name)
91-
assert cip
92-
assert self.ssl_db.test_db_connection(
93-
container_ip=cip, username=username1, password=password1
98+
cip1 = self.pwd_change.get_cip(cid_file_name=cid_file_name)
99+
assert cip1
100+
assert self.pwd_change.test_db_connection(
101+
container_ip=cip1, username=username1, password=password1
94102
)
95-
cid = self.ssl_db.get_cid(cid_file_name=cid_file_name)
103+
cid = self.pwd_change.get_cid(cid_file_name=cid_file_name)
96104
assert cid
97105
PodmanCLIWrapper.call_podman_command(cmd=f"stop {cid}")
98106
cid_file_name = "test_password_change2"
99107
username2 = "user2"
100108
password2 = "bar"
101109
# Create second container with changed password
102-
assert self.ssl_db.create_container(
110+
assert self.pwd_change.create_container(
103111
cid_file_name=cid_file_name,
104112
container_args=[
105113
f"-e MYSQL_USER={username2}",
@@ -108,20 +116,23 @@ def test_password_change_new_user_test(self):
108116
f"-v {pwd_dir}:/var/lib/mysql/data:Z",
109117
],
110118
)
111-
cip2 = self.ssl_db.get_cip(cid_file_name=cid_file_name)
119+
cip2 = self.pwd_change.get_cip(cid_file_name=cid_file_name)
112120
assert cip2
113-
assert self.ssl_db.test_db_connection(
121+
assert self.pwd_change.test_db_connection(
114122
container_ip=cip2, username=username1, password=password1
115123
)
116-
cid2 = self.ssl_db.get_cid(cid_file_name=cid_file_name)
124+
cid2 = self.pwd_change.get_cid(cid_file_name=cid_file_name)
117125
mysql_logs = PodmanCLIWrapper.podman_logs(
118-
conatiner_id=cid2,
126+
container_id=cid2,
119127
)
120128
assert "User user2 does not exist in database" in mysql_logs
121129
podman_cmd = (
122130
f"--rm {VARS.IMAGE_NAME} mysql --host {cip2} -u{username1} -p{password2}"
123131
)
124132
output = PodmanCLIWrapper.podman_run_command(
125133
cmd=f"{podman_cmd} -e 'SELECT 1;' db",
134+
ignore_error=True,
135+
)
136+
assert f"Access denied for user '{username1}'@" in output, (
137+
f"The new password {password2} should not work, but it does"
126138
)
127-
assert output == "1"

test/test_container_replication.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import tempfile
2+
import re
3+
from time import sleep
4+
5+
from container_ci_suite.container_lib import ContainerTestLib, DatabaseWrapper
6+
from container_ci_suite.container_lib import ContainerTestLibUtils
7+
from container_ci_suite.engines.podman_wrapper import PodmanCLIWrapper
8+
9+
from conftest import VARS
10+
11+
12+
class TestMySqlReplicationContainer:
13+
"""
14+
Test MySQL container configuration.
15+
"""
16+
17+
def setup_method(self):
18+
self.replication_db = ContainerTestLib(image_name=VARS.IMAGE_NAME)
19+
self.replication_db.set_new_db_type(db_type="mysql")
20+
21+
def teardown_method(self):
22+
self.replication_db.cleanup()
23+
24+
def test_replication(self):
25+
""" """
26+
cluster_args = "-e MYSQL_SOURCE_USER=source -e MYSQL_SOURCE_PASSWORD=source -e MYSQL_DATABASE=db"
27+
source_cid = "source.cid"
28+
username = "user"
29+
password = "foo"
30+
# Run the MySQL source
31+
assert self.replication_db.create_container(
32+
cid_file_name=source_cid,
33+
container_args=[
34+
f"-e MYSQL_USER={username}",
35+
f"-e MYSQL_PASSWORD={password}",
36+
"-e MYSQL_ROOT_PASSWORD=root",
37+
"-e MYSQL_INNODB_BUFFER_POOL_SIZE=5M",
38+
],
39+
docker_args=cluster_args,
40+
command="run-mysqld-source",
41+
)
42+
source_cip = self.replication_db.get_cip(cid_file_name=source_cid)
43+
print(f"Source IP: {source_cip}")
44+
source_cid = self.replication_db.get_cid(cid_file_name=source_cid)
45+
assert source_cid
46+
# Run the MySQL replica
47+
replica_cid = "replica.cid"
48+
assert self.replication_db.create_container(
49+
cid_file_name=replica_cid,
50+
container_args=[
51+
f"-e MYSQL_SOURCE_SERVICE_NAME={source_cip}",
52+
"-e MYSQL_INNODB_BUFFER_POOL_SIZE=5M",
53+
],
54+
docker_args=cluster_args,
55+
command="run-mysqld-replica",
56+
)
57+
replica_cip = self.replication_db.get_cip(cid_file_name=replica_cid)
58+
assert replica_cip
59+
replica_cid = self.replication_db.get_cid(cid_file_name=replica_cid)
60+
assert replica_cid
61+
print(f"Replica IP: {replica_cip}")
62+
# Now wait till the SOURCE will see the REPLICA
63+
result = self.replication_db.test_db_connection(
64+
container_ip=replica_cip,
65+
username=username,
66+
password=password,
67+
)
68+
result = PodmanCLIWrapper.call_podman_command(
69+
cmd=f"exec {source_cid} mysql -e 'SHOW REPLICAS;' db",
70+
ignore_error=True,
71+
debug=True,
72+
)
73+
print(f"Showing replicas: {result}")
74+
assert replica_cip in result, (
75+
f"Replica {replica_cip} not found in SOURCE {source_cip}"
76+
)
77+
# do some real work to test replication in practice
78+
result = self.replication_db.test_db_connection(
79+
container_ip=source_cip,
80+
username=username,
81+
password=password,
82+
max_attempts=120,
83+
sql_cmd="-e 'CREATE TABLE t1 (a INT); INSERT INTO t1 VALUES (24);'",
84+
)
85+
print(f"Creating table: {result}")
86+
# let's wait for the table to be created and available for replication
87+
sleep(3)
88+
89+
result = self.replication_db.test_db_connection(
90+
container_ip=replica_cip,
91+
username=username,
92+
password=password,
93+
max_attempts=120,
94+
sql_cmd="-e 'select * from t1;'",
95+
)
96+
assert result
97+
podman_cmd = f"--rm {VARS.IMAGE_NAME} mysql --host {replica_cip} -u{username} -p{password}"
98+
table_output = PodmanCLIWrapper.podman_run_command(
99+
cmd=f"{podman_cmd} -e 'select * from t1;' db",
100+
)
101+
print(f"Selecting from table: {table_output}")
102+
assert re.search(r"^a\n^24", table_output.strip(), re.MULTILINE), (
103+
f"Replica {replica_cip} did not get value from SOURCE {source_cip}"
104+
)

test/test_container_ssl.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,17 @@ def test_ssl(self):
3232
"ssl-cert=${APP_DATA}/mysql-certs/server-cert-selfsigned.pem",
3333
]
3434
f.write("\n".join(lines))
35-
server_key_pem = f"{ssl_dir}/server-key.pem"
36-
server_req_pem = f"{ssl_dir}/server-req.pem"
37-
server_cert_selfsigned_pem = f"{ssl_dir}/server-cert-selfsigned.pem"
35+
srv_key_pem = f"{ssl_dir}/server-key.pem"
36+
srv_req_pem = f"{ssl_dir}/server-req.pem"
37+
srv_self_pem = f"{ssl_dir}/server-cert-selfsigned.pem"
38+
openssl_cmd = "openssl req -newkey rsa:2048 -nodes"
39+
openssl_cmd_new = "openssl req -new -x509 -nodes"
40+
subj = "/C=GB/ST=Berkshire/L=Newbury/O=My Server Company"
3841
ContainerTestLibUtils.run_command(
39-
cmd=f"openssl req -newkey rsa:2048 -nodes -keyout {server_key_pem} -subj "
40-
+ f"/C=GB/ST=Berkshire/L=Newbury/O=My Server Company' > {server_req_pem}"
42+
cmd=f"{openssl_cmd} -keyout {srv_key_pem} -subj '{subj}' > {srv_req_pem}"
4143
)
4244
ContainerTestLibUtils.run_command(
43-
cmd=f"openssl req -new -x509 -nodes -key {server_key_pem} -batch > "
44-
+ f"{server_cert_selfsigned_pem}"
45+
cmd=f"{openssl_cmd_new} -key {srv_key_pem} -batch > {srv_self_pem}"
4546
)
4647
assert ContainerTestLibUtils.commands_to_run(
4748
commands_to_run=[
@@ -74,7 +75,7 @@ def test_ssl(self):
7475

7576
mysql_cmd = (
7677
f"mysql --host {cip} -u{username} -p{password} --ssl-ca={ca_cert_path}"
77-
+ "-e 'show status like \"Ssl_cipher\" \\G' db"
78+
+ " -e 'show status like \"Ssl_cipher\" \\G' db"
7879
)
7980
ssl_output = PodmanCLIWrapper.podman_run_command(
8081
cmd=f"--rm -v {ssl_dir}:/opt/app-root/src/:z {VARS.IMAGE_NAME} {mysql_cmd}",

0 commit comments

Comments
 (0)