test: add mysql server e2e test

This commit is contained in:
hengyoush
2024-10-26 02:20:52 +08:00
parent c92c1209ca
commit b145539a0f
2 changed files with 64 additions and 21 deletions

View File

@@ -1,4 +1,6 @@
import mysql.connector
import time
import sys
from mysql.connector import Error
def create_connection(host_name, user_name, user_password):
@@ -23,24 +25,29 @@ def create_database(connection, database_name):
except Error as e:
print(f"The error '{e}' occurred")
def execute_query_in_database(connection, database_name, query):
def execute_query_in_database(connection, database_name, query, count):
try:
cursor = connection.cursor()
cursor.execute(f"USE {database_name}")
cursor.execute(query)
for i in range(count):
cursor.execute(query)
# If it's a SELECT statement, fetch the results
if query.strip().upper().startswith('SELECT'):
result = cursor.fetchall()
for row in result:
print(row)
else:
# For other statements like INSERT, UPDATE, DELETE, commit the transaction
connection.commit()
print("Query executed successfully")
# 休眠
time.sleep(0.5)
# If it's a SELECT statement, fetch the results
if query.strip().upper().startswith('SELECT'):
result = cursor.fetchall()
for row in result:
print(row)
else:
# For other statements like INSERT, UPDATE, DELETE, commit the transaction
connection.commit()
print("Query executed successfully")
except Error as e:
print(f"The error '{e}' occurred")
count = int(sys.argv[1])
# Replace with your MySQL server credentials
connection = create_connection("localhost", "root", "123456")
@@ -52,16 +59,17 @@ create_database(connection, database_name)
# Example SQL query to execute in the newly created database
query = "CREATE TABLE example_table (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100))"
execute_query_in_database(connection, database_name, query)
execute_query_in_database(connection, database_name, query,1)
# Another example query to insert data
insert_query = "INSERT INTO example_table (name) VALUES ('John Doe')"
execute_query_in_database(connection, database_name, insert_query)
execute_query_in_database(connection, database_name, insert_query,1)
# Example SELECT query to retrieve data
select_query = "SELECT * FROM example_table"
execute_query_in_database(connection, database_name, select_query)
execute_query_in_database(connection, database_name, select_query, count)
time.sleep(1)
# Close the connection
if connection.is_connected():
connection.close()

View File

@@ -5,9 +5,11 @@ set -ex
CMD="$1"
DOCKER_REGISTRY="$2"
FILE_PREFIX="/tmp/kyanos"
LNAME="${FILE_PREFIX}_mysql.log"
CLIENT_LNAME="${FILE_PREFIX}_mysql_client.log"
SERVER_LNAME="${FILE_PREFIX}_mysql_server.log"
function test_mysql() {
function test_mysql_server() {
if [ -z "$DOCKER_REGISTRY" ]; then
IMAGE_NAME="mysql:5.7.19"
else
@@ -27,20 +29,53 @@ function test_mysql() {
export cid1
echo $cid1
timeout 30 ${CMD} watch --debug-output mysql --remote-ports 3306 2>&1 | tee "${LNAME}" &
timeout 30 ${CMD} watch --debug-output mysql --local-ports 3306 2>&1 | tee "${SERVER_LNAME}" &
sleep 10
python3 ./testdata/query_mysql.py
python3 ./testdata/query_mysql.py 5
wait
cat "${LNAME}"
cat "${SERVER_LNAME}"
# docker rm -f $cid1 || true
cat "${SERVER_LNAME}" | grep 'SELECT' | grep 'rows = 1'
# check_time_detail_completed_with_last_lines "${LNAME}" 1
# check_patterns_in_file_with_last_lines "${LNAME}" "Resultset rows = 1" 1
}
function test_mysql_client() {
if [ -z "$DOCKER_REGISTRY" ]; then
IMAGE_NAME="mysql:5.7.19"
else
IMAGE_NAME=$DOCKER_REGISTRY"/library/mysql:5.7.19"
fi
docker pull "$IMAGE_NAME"
mkdir -p /opt/docker_v/mysql/conf
pushd /opt/docker_v/mysql/conf
touch my.cnf
printf "[mysqld]\nskip_ssl" > my.cnf
popd
pip install --break-system-packages mysql-connector-python || true
cname='test-mysql'
docker rm -f $cname || true
cid1=$(docker run --name $cname -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -v /opt/docker_v/mysql/conf:/etc/mysql/conf.d -d "$IMAGE_NAME")
export cid1
echo $cid1
timeout 30 ${CMD} watch --debug-output mysql --remote-ports 3306 2>&1 | tee "${CLIENT_LNAME}" &
sleep 10
python3 ./testdata/query_mysql.py 5
wait
cat "${CLIENT_LNAME}"
docker rm -f $cid1 || true
cat "${LNAME}" | grep 'SELECT' | grep 'rows = 1'
cat "${CLIENT_LNAME}" | grep 'SELECT' | grep 'rows = 1'
# check_time_detail_completed_with_last_lines "${LNAME}" 1
# check_patterns_in_file_with_last_lines "${LNAME}" "Resultset rows = 1" 1
}
function main() {
test_mysql
test_mysql_client
test_mysql_server
}
main