Skip to content

Commit

Permalink
add redis cluster common ut
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Rookie committed Oct 9, 2024
1 parent 9080762 commit a0ae9ed
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 27 deletions.
55 changes: 28 additions & 27 deletions addons/redis/redis-cluster-scripts/redis-cluster-common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,17 @@ sleep_random_second_when_ut_mode_false() {
## the value format of ALL_SHARDS_COMPONENT_SHORT_NAMES is like "shard-98x:shard-98x,shard-cq7:shard-cq7,shard-hy7:shard-hy7"
## return the component names of all shards with the format "shard-98x,shard-cq7,shard-hy7"
get_all_shards_components() {
local all_shards_components
local all_shards_components=""
if is_empty "$ALL_SHARDS_COMPONENT_SHORT_NAMES"; then
echo "Error: Required environment variable ALL_SHARDS_COMPONENT_SHORT_NAMES is not set." >&2
return 1
fi
all_shards_component_shortname_pairs=$(split "$ALL_SHARDS_COMPONENT_SHORT_NAMES" ",")
for pair in $all_shards_component_shortname_pairs; do
shard_name=$(split "$pair" ":")
all_shards_components="$all_shards_components,$shard_name"
IFS=',' read -ra all_shards_component_shortname_pairs <<< "$ALL_SHARDS_COMPONENT_SHORT_NAMES"
for pair in "${all_shards_component_shortname_pairs[@]}"; do
IFS=':' read -r shard_name _ <<< "$pair"
all_shards_components="${all_shards_components},${shard_name}"
done
all_shards_components="${all_shards_components#,}"
echo "$all_shards_components"
return 0
}
Expand All @@ -58,20 +59,20 @@ get_all_shards_components() {
## - ALL_SHARDS_POD_NAME_LIST_SHARD_98X="redis-shard-98x-0,redis-shard-98x-1"
## - ALL_SHARDS_POD_NAME_LIST_SHARD_CQ7="redis-shard-cq7-0,redis-shard-cq7-1"
## - ALL_SHARDS_POD_NAME_LIST_SHARD_HY7="redis-shard-hy7-0,redis-shard-hy7-1"
## return the pod names of all shards combined with ","
get_all_shards_pods() {
## list all Envs name prefix with ALL_SHARDS_POD_NAME_LIST and get them value combined with ","
local all_shards_pods
envs=$(env | grep "^ALL_SHARDS_POD_NAME_LIST" | awk -F '=' '{print $2}')
while read -r line; do
## remove the \n at the end of the string
line=$(echo "$line" | tr -d '\n')

## remove the , at the beginning of the string
if is_empty "$all_shards_pods"; then
all_shards_pods="${line}"
continue
local envs
local all_shards_pods=""
envs=$(env | grep "^ALL_SHARDS_POD_NAME_LIST" | sort)
while IFS='=' read -r env_name env_value; do
if ! is_empty "$env_value"; then
if is_empty "$all_shards_pods"; then
all_shards_pods="$env_value"
else
all_shards_pods="$all_shards_pods,$env_value"
fi
fi
all_shards_pods="$all_shards_pods,${line}"
done <<< "$envs"
echo "$all_shards_pods"
return 0
Expand All @@ -81,20 +82,20 @@ get_all_shards_pods() {
## - ALL_SHARDS_POD_FQDN_LIST_SHARD_98X="redis-shard-98x-0.redis-shard-98x-headless.default.cluster.local,redis-shard-98x-1.redis-shard-98x-headless.default.cluster.local"
## - ALL_SHARDS_POD_FQDN_LIST_SHARD_CQ7="redis-shard-cq7-0.redis-shard-cq7-headless.default.cluster.local,redis-shard-cq7-1.redis-shard-cq7-headless.default.cluster.local"
## - ALL_SHARDS_POD_FQDN_LIST_SHARD_HY7="redis-shard-hy7-0.redis-shard-hy7-headless.default.cluster.local,redis-shard-hy7-1.redis-shard-hy7-headless.default.cluster.local"
## return the pod fqdn list for all shard pod combined with ","
get_all_shards_pod_fqdns() {
## list all Envs name prefix with ALL_SHARDS_POD_FQDN_LIST and get them value combined with ","
local all_shards_pod_fqdns
envs=$(env | grep "^ALL_SHARDS_POD_FQDN_LIST" | awk -F '=' '{print $2}')
while read -r line; do
## remove the \n at the end of the string
line=$(echo "$line" | tr -d '\n')

## remove the , at the beginning of the string
if is_empty "$all_shards_pod_fqdns"; then
all_shards_pod_fqdns="${line}"
continue
local envs
local all_shards_pod_fqdns=""
envs=$(env | grep "^ALL_SHARDS_POD_FQDN_LIST" | sort)
while IFS='=' read -r env_name env_value; do
if [[ -n "$env_value" ]]; then
if [[ -z "$all_shards_pod_fqdns" ]]; then
all_shards_pod_fqdns="$env_value"
else
all_shards_pod_fqdns="$all_shards_pod_fqdns,$env_value"
fi
fi
all_shards_pod_fqdns="$all_shards_pod_fqdns,${line}"
done <<< "$envs"
echo "$all_shards_pod_fqdns"
return 0
Expand Down
105 changes: 105 additions & 0 deletions addons/redis/scripts-ut-spec/redis_cluster_common_spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# 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" 4 &>/dev/null; then
echo "redis_cluster_common_spec.sh skip cases because dependency bash version 4 or higher is not installed."
exit 0
fi

source ./utils.sh

# The unit test needs to rely on the common library functions defined in kblib.
# Therefore, we first dynamically generate the required common library files from the kblib library chart.
common_library_file="./common.sh"
generate_common_library $common_library_file

Describe "Redis Cluster Common Bash Script Tests"
# load the scripts to be tested and dependencies
Include $common_library_file
Include ../redis-cluster-scripts/redis-cluster-common.sh

init() {
# set ut_mode to true to hack control flow in the script
ut_mode="true"
}
BeforeAll "init"

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

Describe "get_all_shards_components()"
Context "when ALL_SHARDS_COMPONENT_SHORT_NAMES is not set"
It "returns 1 when ALL_SHARDS_COMPONENT_SHORT_NAMES is not set"
When call get_all_shards_components
The status should be failure
The stderr should include "Error: Required environment variable ALL_SHARDS_COMPONENT_SHORT_NAMES is not set."
End
End

Context "when ALL_SHARDS_COMPONENT_SHORT_NAMES is set"
setup() {
export ALL_SHARDS_COMPONENT_SHORT_NAMES="shard-98x:shard-98x,shard-cq7:shard-cq7,shard-hy7:shard-hy7"
}
Before "setup"

un_setup() {
unset ALL_SHARDS_COMPONENT_SHORT_NAMES
}
After "un_setup"

It "returns all shard components"
When call get_all_shards_components
The status should be success
The output should eq "shard-98x,shard-cq7,shard-hy7"
End
End
End

Describe "get_all_shards_pods()"
setup() {
export ALL_SHARDS_POD_NAME_LIST_SHARD_98X="redis-shard-98x-0,redis-shard-98x-1"
export ALL_SHARDS_POD_NAME_LIST_SHARD_CQ7="redis-shard-cq7-0,redis-shard-cq7-1"
export ALL_SHARDS_POD_NAME_LIST_SHARD_HY7="redis-shard-hy7-0,redis-shard-hy7-1"
}
Before "setup"

un_setup() {
unset ALL_SHARDS_POD_NAME_LIST_SHARD_98X
unset ALL_SHARDS_POD_NAME_LIST_SHARD_CQ7
unset ALL_SHARDS_POD_NAME_LIST_SHARD_HY7
}
After "un_setup"

It "returns all shard pods"
When call get_all_shards_pods
The status should be success
The output should eq "redis-shard-98x-0,redis-shard-98x-1,redis-shard-cq7-0,redis-shard-cq7-1,redis-shard-hy7-0,redis-shard-hy7-1"
End
End

Describe "get_all_shards_pod_fqdns()"
setup() {
export ALL_SHARDS_POD_FQDN_LIST_SHARD_98X="redis-shard-98x-0.redis-shard-98x-headless.default.cluster.local,redis-shard-98x-1.redis-shard-98x-headless.default.cluster.local"
export ALL_SHARDS_POD_FQDN_LIST_SHARD_CQ7="redis-shard-cq7-0.redis-shard-cq7-headless.default.cluster.local,redis-shard-cq7-1.redis-shard-cq7-headless.default.cluster.local"
export ALL_SHARDS_POD_FQDN_LIST_SHARD_HY7="redis-shard-hy7-0.redis-shard-hy7-headless.default.cluster.local,redis-shard-hy7-1.redis-shard-hy7-headless.default.cluster.local"
}
Before "setup"

un_setup() {
unset ALL_SHARDS_POD_FQDN_LIST_SHARD_98X
unset ALL_SHARDS_POD_FQDN_LIST_SHARD_CQ7
unset ALL_SHARDS_POD_FQDN_LIST_SHARD_HY7
}
After "un_setup"

It "returns all shard pod FQDNs"
When call get_all_shards_pod_fqdns
The status should be success
The output should eq "redis-shard-98x-0.redis-shard-98x-headless.default.cluster.local,redis-shard-98x-1.redis-shard-98x-headless.default.cluster.local,redis-shard-cq7-0.redis-shard-cq7-headless.default.cluster.local,redis-shard-cq7-1.redis-shard-cq7-headless.default.cluster.local,redis-shard-hy7-0.redis-shard-hy7-headless.default.cluster.local,redis-shard-hy7-1.redis-shard-hy7-headless.default.cluster.local"
End
End

End

0 comments on commit a0ae9ed

Please sign in to comment.