Files
SODEW/sodew-bash-main/libs/commands/test.sh
T
2026-08-18 09:40:08 +02:00

216 lines
7.1 KiB
Bash

testLabel="[Test]"
# Detect current master pod by parsing INFO replication.
function redisReplicaMasterGet() {
local output error
if ! run output error k3sPodList "$redisKube"; then
appError "$error"
return 1
fi
while read -r pod; do
if ! run output error redisPodExecCli "$pod" INFO replication; then
appError "$pod | $error"
continue
fi
if echo "$output" | grep -q "role:master"; then
echo "$pod"
return 0
fi
done < <(awk 'NF' <<<"$output")
appError "Master node not found"
return 1
}
# Test MariaDB replica
function testMariadbReplica() {
local database=$(uuidgen)
if ! run output error mariadbMasterRootQuery "CREATE DATABASE \`$database\`;"; then
printDanger "$testLabel $mariadbMasterKube | Database: $database | Create: $error"
return 1
else
printSuccess "$testLabel $mariadbMasterKube | Database: $database | Create: OK"
fi
local databaseMaster
if ! run databaseMaster error mariadbMasterRootQuery "SHOW DATABASES LIKE '$database';"; then
printDanger "$testLabel "$mariadbMasterKube" | Database list: $error"
else
if [[ "$databaseMaster" == "$database" ]]; then
printSuccess "$testLabel $mariadbMasterKube | Database: $database | Exists"
else
printDanger "$testLabel $mariadbMasterKube | Database: $database | Not found"
fi
fi
local databaseSlave
if ! run databaseSlave error mariadbSlaveRootQuery "SHOW DATABASES LIKE '$database';"; then
printDanger "$testLabel "$mariadbSlaveKube" | Database list: $error"
else
if [[ "$databaseSlave" == "$database" ]]; then
printSuccess "$testLabel $mariadbSlaveKube | Database: $database | Exists"
else
printDanger "$testLabel $mariadbSlaveKube | Database: $database | Not found"
fi
fi
if ! run output error mariadbMasterRootQuery "DROP DATABASE \`$database\`;"; then
printDanger "$testLabel $mariadbMasterKube | Database: $database | Drop: $error"
return 1
else
printSuccess "$testLabel $mariadbMasterKube | Database: $database | Drop: OK"
fi
}
# Test Redis cluster
function testRedisCluster() {
local key=$(uuidgen)
local value=$(uuidgen)
local podMaster podList error
if ! run podList error k3sPodList "$redisKube"; then
printDanger "$testLabel k3sPodList: $error"
return 1
fi
if ! run podMaster error redisReplicaMasterGet; then
printDanger "$testLabel redisReplicaMasterGet: $error"
return 1
fi
if ! run output error redisPodExecCli "$podMaster" SET "$key" "$value"; then
printDanger "$testLabel $podMaster | Key: $key | Set: $error"
return 1
else
printSuccess "$testLabel $podMaster | Key: $key | Set: $value"
fi
while read pod; do
if ! run output error redisPodExecCli "$pod" GET "$key"; then
printDanger "$testLabel $pod | Key: $key | Get: $error"
continue
fi
if [[ "$output" == "$value" ]]; then
printSuccess "$testLabel $pod | Key: $key | Get: $output"
else
printDanger "$testLabel $pod | Key: $key | Get: $output"
fi
done < <(awk 'NF' <<<"$podList")
if ! run output error redisPodExecCli "$podMaster" DEL "$key"; then
printDanger "$testLabel $podMaster | Key: $key | Del: $error"
return 1
else
printSuccess "$testLabel $podMaster | Key: $key | Del: OK"
fi
}
# Test create site
function testSite() {
local domain
domain="test-$(stringRandom 16 'a-z').test"
if ! run output error cmdSiteAddDefault "$domain"; then
printDanger "$testLabel Domain: $domain | Create: $error"
else
printSuccess "$testLabel Domain: $domain | Create: OK"
cmdOpenlitespeedRestart
if ! run output error systemHostAdd "$domain"; then
printDanger "$testLabel Domain: $domain | Host add: $error"
else
printSuccess "$testLabel Domain: $domain | Host add: OK"
fi
local code
if ! run code error curl -s -L -o /dev/null -w "%{http_code}" "$domain"; then
printDanger "$testLabel Domain: $domain | Curl: $code: $error"
else
if [[ "$code" == "200" ]]; then
printSuccess "$testLabel Domain: $domain | Curl: $code"
else
printWarning "$testLabel Domain: $domain | Curl: $code"
fi
fi
if ! run output error systemHostRemove "$domain"; then
printDanger "$testLabel Domain: $domain | Host remove: $error"
else
printSuccess "$testLabel Domain: $domain | Host remove: OK"
fi
fi
if ! run output error cmdSiteRemove "$domain"; then
printDanger "$testLabel Domain: $domain | Site remove: $error"
else
printSuccess "$testLabel Domain: $domain | Site remove: OK"
fi
cmdOpenlitespeedRestart
}
# Test create site Wordpress
function testSiteWordpress() {
local domain
domain="test-$(stringRandom 16 'a-z').test"
if ! run output error cmdSiteAddWordpress $domain; then
printDanger "$testLabel Domain: $domain | Create: $error"
else
printSuccess "$testLabel Domain: $domain | Create: OK"
cmdOpenlitespeedRestart
if ! run output error systemHostAdd "$domain"; then
printDanger "$testLabel Domain: $domain | Host add: $error"
else
printSuccess "$testLabel Domain: $domain | Host add: OK"
fi
local code
if ! run code error curl -s -L -o /dev/null -w "%{http_code}" "$domain"; then
printDanger "$testLabel Domain: $domain | Curl: $code: $error"
else
if [[ "$code" == "200" ]]; then
printSuccess "$testLabel Domain: $domain | Curl: $code"
else
printWarning "$testLabel Domain: $domain | Curl: $code"
fi
fi
if ! run output error systemHostRemove "$domain"; then
printDanger "$testLabel Domain: $domain | Host remove: $error"
else
printSuccess "$testLabel Domain: $domain | Host remove: OK"
fi
fi
if ! run output error cmdSiteRemove "$domain"; then
printDanger "$testLabel Domain: $domain | Site remove: $error"
else
printSuccess "$testLabel Domain: $domain | Site remove: OK"
fi
cmdOpenlitespeedRestart
}
# testRedisCluster
# local key="kube:haproxy:uuid" uuid
# uuid=$(uuidgen)
# if run output error redisExecCli -h redis-master -p 6379 SET "$key" "$uuid"; then
# printInfo "$redisHaproxyLabel Set $key | $uuid"
# else
# printDanger "$redisHaproxyLabel Set $key: $error"
# fi
# if run output error redisExecCli -h redis-master -p 6379 GET "$key"; then
# printInfo "$redisHaproxyLabel Get $key | $output"
# if [[ "$uuid" != "$output" ]]; then
# printDanger "$redisHaproxyLabel Validation failed"
# else
# printSuccess "$redisHaproxyLabel Validation success"
# fi
# else
# printDanger "$redisHaproxyLabel Get $key: $error"
# fi