Skip to content

Commit

Permalink
add scripts tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xuriwuyun committed Oct 18, 2024
1 parent ac99351 commit 74b0db2
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 1 deletion.
83 changes: 83 additions & 0 deletions addons/mysql/scripts-ut-spec/configre_proxysql_spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# shellcheck shell=bash
# shellcheck disable=SC2034

# shellcheck shell=bash

Describe "ProxySQL Configuration Script Tests"

Describe "Log Function Tests"
Include ../scripts/configure-proxysql.sh

It "outputs a log message"
When call log "INFO" "Test log message"
The status should be success
The stdout should include "Test log message"
End

It "outputs a correctly formatted log message"
When call log "INFO" "Test log message"
The status should be success
The stdout should match pattern "*[0-9][0-9]/[0-9][0-9]/[0-9][0-9]*"
End
End

Describe "MySQL Exec Function Tests"
Include ../scripts/configure-proxysql.sh

It "executes MySQL command successfully"
mysql() {
echo "MySQL command executed: $@"
return 0
}
When call mysql_exec "root" "password" "localhost" "3306" "SELECT 1"
The status should be success
The stdout should match pattern "MySQL command executed: * SELECT 1"
End

It "fails to execute MySQL command"
mysql() {
echo "MySQL command failed: $@">&2
return 1
}
When call mysql_exec "root" "password" "localhost" "3306" "INVALID COMMAND"
The status should be failure
The stderr should match pattern "MySQL command failed: * INVALID COMMAND"
End
End

Describe "Wait for MySQL Function Tests"
Include ../scripts/configure-proxysql.sh
setup() {
# Mock the mysql_exec function to simulate MySQL responses
mysql_exec() {
if [ "$5" == "select 1;" ]; then
echo "1"
return 0
else
return 1
fi
}
}
Before 'setup'

It "waits for MySQL to be online"
When call wait_for_mysql "root" "password" "localhost" "3306"
The output should include "Waiting for host localhost to be online ..."
The status should be success
End

# no test for this case, as it will abort
# It "fails to wait for MySQL to be online"
# mysql_exec() {
# echo failed
# return 1
# }
# sleep() {
# }

# When call wait_for_mysql "root" "password" "localhost" "3306"
# The output should include "Server localhost start failed ..."
# The status should be failure
# End
End
End
46 changes: 46 additions & 0 deletions addons/mysql/scripts-ut-spec/proxysql_entry_spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# shellcheck shell=bash
# shellcheck disable=SC2034


# validate_shell_type_and_version defined in shellspec/spec_helper.sh used to validate the expected shell type and version this script needs to run.
# if ! validate_shell_type_and_version "bash" &>/dev/null; then
# echo "mongodb replicaset_setup_spec.sh skip all cases because dependency bash is not installed."
# exit 0
# fi

Describe "ProxySQL Entry Script Tests"

init() {
TEST_DATA_DIR="./test_data"
export FRONTEND_TLS_ENABLED="false"
}
BeforeAll "init"

cleanup() {
rm -rf $TEST_DATA_DIR
}
AfterAll 'cleanup'

Describe "Run proxysql-entry.sh with FRONTEND_TLS_ENABLED=false"

It "runs successfully"
When run source ../scripts/proxysql-entry.sh
The status should be failure
The stdout should include "Configuring proxysql ..."
The stderr should include "/scripts/proxysql/configure-proxysql.sh: No such file or directory"
End
End


Describe "Log Function Tests"
Include ../scripts/proxysql-entry.sh

It "outputs a log message"
When call log "INFO" "Test log message"
The status should be success
The stdout should include "INFO"
The stdout should include "Test log message"
End
End

End
8 changes: 7 additions & 1 deletion addons/mysql/scripts/configure-proxysql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function mysql_exec() {
local query="$5"
local exec_opt="$6"
pass_ssl=""
if [ $BACKEND_TLS_ENABLED == "true" ]; then
if [ "$BACKEND_TLS_ENABLED" == "true" ]; then
if [ $port == 3306 ]; then
pass_ssl="--ssl-ca=/var/lib/certs/ca.crt"
fi
Expand Down Expand Up @@ -63,6 +63,12 @@ function wait_for_mysql() {
fi
}

# if test by shellspec include, just return 0
if [ "${__SOURCED__:+x}" ]; then
return 0
fi


log "$MYSQL_ROOT_USER $MYSQL_ROOT_PASSWORD $BACKEND_SERVER $MYSQL_PORT"
wait_for_mysql $MYSQL_ROOT_USER $MYSQL_ROOT_PASSWORD $BACKEND_SERVER $MYSQL_PORT

Expand Down
5 changes: 5 additions & 0 deletions addons/mysql/scripts/proxysql-entry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ if [ "${1:0:1}" = '-' ]; then
CMDARG="$@"
fi

# if test by shellspec include, just return 0
if [ "${__SOURCED__:+x}" ]; then
return 0
fi

# Start ProxySQL with PID 1
exec proxysql -c /etc/custom-config/proxysql.cnf -f $CMDARG &
pid=$!
Expand Down

0 comments on commit 74b0db2

Please sign in to comment.