Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add scripts UT for mysql #1150

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions addons/mysql/scripts-ut-spec/init_mysql_instance_for_orc_spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# shellcheck shell=bash
# shellcheck disable=SC2034

# validate shell version
if ! validate_shell_type_and_version "bash" 4 &>/dev/null; then
echo "init_mysql_instance_for_orc_spec.sh skip cases because dependency bash version 4 or higher is not installed."
exit 0
fi

source ./utils.sh

# Generate common library
common_library_file="./common.sh"
generate_common_library $common_library_file

Describe "MySQL Instance Initialization Tests"
Include $common_library_file
Include ../scripts/init-mysql-instance-for-orc.sh

init() {
ut_mode="true"
}
BeforeAll "init"

cleanup() {
rm -f $common_library_file
}
AfterAll 'cleanup'

Describe "validate_env_vars()"
Context "when required environment variables are not set"
It "exits with error when MYSQL_ROOT_USER is not set"
unset MYSQL_ROOT_USER
export MYSQL_ROOT_PASSWORD="password"
export ORC_TOPOLOGY_USER="orc"
export ORC_TOPOLOGY_PASSWORD="password"

When run validate_env_vars
The status should be failure
The stderr should include "Required environment variables MYSQL_ROOT_USER or MYSQL_ROOT_PASSWORD not set"
End
End
End

Describe "create_orc_user()"
Context "when mysql command succeeds"
mysql() {
return 0
}

It "creates orchestrator user successfully"
When call create_orc_user
The status should be success
The output should include "Created orchestrator user successfully"
End
End

Context "when mysql command fails"
mysql() {
return 1
}

It "exits with error when mysql command fails"
When run create_orc_user
The status should be failure
The stderr should include "Failed to create orchestrator user"
End
End
End

Describe "wait_for_mysql()"
Context "when mysql becomes available"
mysqladmin() {
return 0
}

It "succeeds when mysql is available"
When call wait_for_mysql
The status should be success
The output should include "MySQL is now available"
End
End

Context "when mysql timeout occurs"
mysqladmin() {
return 1
}

It "exits with error on timeout"
When run wait_for_mysql
The status should be failure
The stderr should include "Timeout waiting for MySQL to be available"
End
End
End

Describe "get_master_from_orc()"
Context "when orchestrator returns valid topology"
orchestrator-client() {
echo "[ok,ok,5.7.21,rw,mod,master,GTID,GTIDMOD] master-1:3306"
}

It "parses master info successfully"
When call get_master_from_orc
The status should be success
The variable master_from_orc should eq "master-1"
End
End

Context "when orchestrator returns error"
orchestrator-client() {
echo "ERROR: cluster not found"
}

It "returns without error on orchestrator failure"
When call get_master_from_orc
The status should be success
The variable master_from_orc should be undefined
End
End
End

Describe "setup_replication()"
Context "when mysql commands succeed"
mysql() {
return 0
}

It "configures replication successfully"
When call setup_replication "master-1"
The status should be success
The output should include "Configured replication successfully"
End
End

Context "when mysql commands fail"
mysql() {
return 1
}

It "exits with error when mysql commands fail"
When run setup_replication "master-1"
The status should be failure
The stderr should include "Failed to configure replication"
End
End
End

End
152 changes: 152 additions & 0 deletions addons/mysql/scripts-ut-spec/mysql_orchestrator_register_spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# shellcheck shell=bash
# shellcheck disable=SC2034

# validate shell version
if ! validate_shell_type_and_version "bash" 4 &>/dev/null; then
echo "mysql_orchestrator_register_spec.sh skip cases because dependency bash version 4 or higher is not installed."
exit 0
fi

source ./utils.sh

# Generate common library
common_library_file="./common.sh"
generate_common_library $common_library_file

Describe "MySQL Orchestrator Registration Tests"
Include $common_library_file
Include ../scripts/mysql-orchestrator-register.sh

init() {
ut_mode="true"
}
BeforeAll "init"

cleanup() {
rm -f $common_library_file
}
AfterAll 'cleanup'

Describe "validate_env_vars()"
Context "when required environment variables are not set"
It "exits with error when ORC_ENDPOINTS is not set"
unset ORC_ENDPOINTS
export ORC_PORTS="3000"
export MYSQL_POD_FQDN_LIST="mysql-0"
export KB_CLUSTER_COMP_NAME="test"
export KB_NAMESPACE="default"

When run validate_env_vars
The status should be failure
The stderr should include "Required environment variables ORC_ENDPOINTS or ORC_PORTS not set"
End

It "exits with error when MYSQL_POD_FQDN_LIST is not set"
export ORC_ENDPOINTS="orc:3000"
export ORC_PORTS="3000"
unset MYSQL_POD_FQDN_LIST
export KB_CLUSTER_COMP_NAME="test"
export KB_NAMESPACE="default"

When run validate_env_vars
The status should be failure
The stderr should include "Required environment variable MYSQL_POD_FQDN_LIST not set"
End
End

Context "when all required environment variables are set"
It "succeeds with valid environment variables"
export ORC_ENDPOINTS="orc:3000"
export ORC_PORTS="3000"
export MYSQL_POD_FQDN_LIST="mysql-0"
export KB_CLUSTER_COMP_NAME="test"
export KB_NAMESPACE="default"

When call validate_env_vars
The status should be success
End
End
End

Describe "get_orchestrator_endpoint()"
It "returns correct endpoint"
export ORC_ENDPOINTS="orc.default:3000"
export ORC_PORTS="3306"

When call get_orchestrator_endpoint
The output should eq "orc.default:3306"
End
End

Describe "get_first_mysql_instance()"
It "returns correct first instance FQDN"
export MYSQL_POD_FQDN_LIST="mysql-0,mysql-1,mysql-2"
export KB_CLUSTER_COMP_NAME="test"
export KB_NAMESPACE="default"

When call get_first_mysql_instance
The output should eq "test-mysql-0.default"
End
End

Describe "register_to_orchestrator()"
Context "when registration succeeds"
curl() {
echo "200"
}

It "successfully registers instance"
When call register_to_orchestrator "test-mysql-0.default"
The status should be success
The output should include "Registration successful for test-mysql-0.default"
End
End

Context "when registration times out"
curl() {
echo "404"
}

It "exits with error on timeout"
When run register_to_orchestrator "test-mysql-0.default"
The status should be failure
The stderr should include "Timeout waiting for test-mysql-0.default to become available"
End
End
End

Describe "register_first_mysql_instance()"
Context "when registration succeeds"
curl() {
echo "200"
}

It "successfully registers first instance"
export ORC_ENDPOINTS="orc:3000"
export ORC_PORTS="3306"
export MYSQL_POD_FQDN_LIST="mysql-0,mysql-1"
export KB_CLUSTER_COMP_NAME="test"
export KB_NAMESPACE="default"

When call register_first_mysql_instance
The status should be success
The output should include "First MySQL instance registered successfully"
End
End

Context "when getting first instance fails"
It "exits with error when MYSQL_POD_FQDN_LIST is empty"
export ORC_ENDPOINTS="orc:3000"
export ORC_PORTS="3306"
export MYSQL_POD_FQDN_LIST=""
export KB_CLUSTER_COMP_NAME="test"
export KB_NAMESPACE="default"

When run register_first_mysql_instance
The status should be failure
The stderr should include "Failed to get first MySQL instance FQDN"
End
End
End

End
Loading
Loading