Skip to content

Commit 9039f6a

Browse files
Updates
1 parent 94b07ca commit 9039f6a

File tree

1 file changed

+121
-32
lines changed

1 file changed

+121
-32
lines changed

docker-image-tests/percona-mysql-router/tests/test_router_static.py

Lines changed: 121 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,49 +13,138 @@
1313
network_name = 'innodbnet1'
1414
docker_tag = os.getenv('ROUTER_VERSION')
1515
docker_acc = os.getenv('DOCKER_ACC')
16-
docker_image = docker_acc + "/" + "percona-mysql-router" + ":" + docker_tag
16+
ps_version = os.getenv('PS_VERSION')
17+
router_docker_image = docker_acc + "/" + "percona-mysql-router" + ":" + docker_tag
18+
percona_docker_image = docker_acc + "/" + "percona-server" + ":" + ps_version
1719

20+
def create_network():
21+
subprocess.run(['sudo', 'docker', 'network', 'create', 'innodbnet'])
22+
23+
def create_mysql_config():
24+
for N in range(1, 5):
25+
with open(f'my{N}.cnf', 'w') as file:
26+
file.write(
27+
f"[mysqld]\n"
28+
f"plugin_load_add='group_replication.so'\n"
29+
f"server_id={(hash(str(time.time()) + str(N))) % 40 + 10}\n"
30+
f"binlog_checksum=NONE\n"
31+
f"enforce_gtid_consistency=ON\n"
32+
f"gtid_mode=ON\n"
33+
f"relay_log=mysql{N}-relay-bin\n"
34+
f"innodb_dedicated_server=ON\n"
35+
f"binlog_transaction_dependency_tracking=WRITESET\n"
36+
f"slave_preserve_commit_order=ON\n"
37+
f"slave_parallel_type=LOGICAL_CLOCK\n"
38+
f"transaction_write_set_extraction=XXHASH64\n"
39+
)
40+
41+
def start_mysql_containers():
42+
for N in range(1, 5):
43+
subprocess.run([
44+
'sudo', 'docker', 'run', '-d',
45+
f'--name=mysql{N}',
46+
f'--hostname=mysql{N}',
47+
'--net=innodbnet',
48+
'-v', f'/home/puneet/my{N}.cnf:/etc/my.cnf',
49+
'-e', 'MYSQL_ROOT_PASSWORD=root', percona_docker_image
50+
])
51+
time.sleep(60)
52+
53+
def create_new_user():
54+
for N in range(1, 5):
55+
subprocess.run([
56+
'sudo', 'docker', 'exec', f'mysql{N}',
57+
'mysql', '-uroot', '-proot',
58+
'-e', "CREATE USER 'inno'@'%' IDENTIFIED BY 'inno'; GRANT ALL privileges ON *.* TO 'inno'@'%' with grant option; FLUSH PRIVILEGES;"
59+
])
60+
61+
def verify_new_user():
62+
for N in range(1, 5):
63+
subprocess.run([
64+
'sudo', 'docker', 'exec', f'mysql{N}',
65+
'mysql', '-uinno', '-pinno',
66+
'-e', "SHOW VARIABLES WHERE Variable_name = 'hostname';"
67+
'-e', "SELECT user FROM mysql.user where user = 'inno';"
68+
])
69+
time.sleep(30)
70+
71+
def docker_restart():
72+
subprocess.run(['sudo', 'docker', 'restart', 'mysql1', 'mysql2', 'mysql3', 'mysql4'])
73+
time.sleep(10)
74+
75+
def create_cluster():
76+
subprocess.run([
77+
'sudo', 'docker', 'exec', 'mysql1',
78+
'mysqlsh', '-uinno', '-pinno', '--', 'dba', 'create-cluster', 'testCluster'
79+
])
80+
81+
def add_slave():
82+
subprocess.run([
83+
'sudo', 'docker', 'exec', 'mysql1',
84+
'mysqlsh', '-uinno', '-pinno', '--',
85+
'cluster', 'add-instance', '--uri=inno@mysql3', '--recoveryMethod=incremental'
86+
])
87+
time.sleep(10)
88+
subprocess.run([
89+
'sudo', 'docker', 'exec', 'mysql1',
90+
'mysqlsh', '-uinno', '-pinno', '--',
91+
'cluster', 'add-instance', '--uri=inno@mysql4', '--recoveryMethod=incremental'
92+
])
93+
94+
def router_bootstrap():
95+
subprocess.run([
96+
'sudo', 'docker', 'run', '-d',
97+
'--name', 'mysql-router',
98+
'--net=innodbnet',
99+
'-e', 'MYSQL_HOST=mysql1',
100+
'-e', 'MYSQL_PORT=3306',
101+
'-e', 'MYSQL_USER=inno',
102+
'-e', 'MYSQL_PASSWORD=inno',
103+
'-e', 'MYSQL_INNODB_CLUSTER_MEMBERS=4',
104+
router_docker_image
105+
])
106+
107+
# Get the Docker ID of the running container
108+
docker_id = subprocess.check_output(['sudo', 'docker', 'ps', '-q', '--filter', f'name={container_name}']).decode().strip()
109+
110+
# Define the pytest fixture to provide the host identifier
18111
@pytest.fixture(scope='module')
19112
def host():
20-
docker_client = docker.from_env()
21-
docker_client.networks.create(network_name)
22-
docker_id = subprocess.check_output(
23-
['docker', 'run', '--name', container_name, '--net', network_name, '-e', 'MYSQL_ROOT_PASSWORD='+ps_pwd, '-e', '--MYSQL_INNODB_CLUSTER_MEMBERS', '4', docker_image ], stderr=subprocess.STDOUT ).decode().strip()
24-
time.sleep(20)
25-
subprocess.check_call(['docker','exec','--user','root',container_name,'microdnf','install', '-y', 'net-tools'])
26-
time.sleep(20)
27-
yield testinfra.get_host("docker://root@" + docker_id)
28-
subprocess.check_call(['docker', 'rm', '-f', docker_id])
113+
yield docker_id
29114

30-
class TestRouterEnvironment:
31-
def test_mysqlsh_version(self, host):
32-
assert host.check_output("mysqlsh --version") == 'mysqlsh Ver '+ROUTER_VERSION+' for Linux on x86_64 - for MySQL '+PS_VERSION+' (Source distribution)'
115+
# Create an instance of the Host class
116+
class Host:
117+
def check_output(self, command):
118+
result = subprocess.run(['docker', 'exec', docker_id, 'bash', '-c', command], capture_output=True, text=True)
119+
return result.stdout.strip()
33120

34-
def test_mysqlrouter_version(self, host):
35-
assert host.check_output("mysqlrouter --version") == 'MySQL Router Ver '+PS_VERSION+' for Linux on x86_64 (Percona Server (GPL), Release 25, Revision '+Revision+')'
121+
# Instantiate an instance of the Host class
122+
host = Host()
36123

37-
def test_binaries_exist(self, host):
38-
router_binary="/tmp/mysqlrouter"
39-
assert host.file(router_binary).exists
40-
assert oct(host.file(router_binary).mode) == '0o755'
124+
class TestRouterEnvironment:
125+
def test_mysqlrouter_version(self, host):
126+
command = "mysqlrouter --version"
127+
output = host.check_output(command)
128+
assert "8.0.33" in output
41129

42-
def test_http_port_6447(self, host):
43-
assert host.socket('tcp://127.0.0.1:6447').is_listening
130+
def test_mysqlsh_version(self, host):
131+
command = "mysqlsh --version"
132+
output = host.check_output(command)
133+
assert "8.0.33-25" in output
44134

45-
def test_raft_port_6446(self, host):
46-
assert host.socket('tcp://127.0.0.1:6446').is_listening
135+
def test_mysqlrouter_directory_permissions(self, host):
136+
assert host.file('/var/lib/mysqlrouter').user == 'mysqlrouter'
137+
assert host.file('/var/lib/mysqlrouter').group == 'mysqlrouter'
138+
assert oct(host.file('/var/lib/mysqlrouter').mode) == '0o755'
47139

48140
def test_mysql_user(self, host):
49141
assert host.user('mysql').exists
50142
assert host.user('mysql').uid == 1001
51143
assert host.user('mysql').gid == 1001
52144
assert 'mysql' in host.user('mysql').groups
53-
54-
def test_mysql_group(self, host):
55-
assert host.group('mysql').exists
56-
assert host.group('mysql').gid == 1001
57-
58-
def test_router_permissions(self, host):
59-
assert host.file('/var/lib/mysqlrouter').user == 'mysql'
60-
assert host.file('/var/lib/mysqlrouter').group == 'mysql'
61-
assert oct(host.file('/var/lib/mysqlrouter').mode) == '0o755'
145+
146+
def test_mysql_user(self, host):
147+
mysql_user = host.user('mysql')
148+
print(f"Username: {mysql_user.name}, UID: {mysql_user.uid}")
149+
assert mysql_user.exists
150+
assert mysql_user.uid == 1001

0 commit comments

Comments
 (0)