Skip to content

Commit

Permalink
add redis cluster manage ut
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Rookie committed Oct 9, 2024
1 parent d5097cd commit ee6db34
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 12 deletions.
16 changes: 9 additions & 7 deletions addons/redis/redis-cluster-scripts/redis-cluster-common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ retry_times=3
check_ready_times=30
retry_delay_second=2

# usage: sleep_random_second <max_time> <min_time>
sleep_random_second() {
local max_time="$1"
local min_time="$2"
local random_time=$((RANDOM % (max_time - min_time + 1) + min_time))
echo "Sleeping for $random_time seconds"
sleep "$random_time"
# usage: sleep_random_second_when_ut_mode_false <max_time> <min_time>
sleep_random_second_when_ut_mode_false() {
if [ "false" == "$ut_mode" ]; then
local max_time="$1"
local min_time="$2"
local random_time=$((RANDOM % (max_time - min_time + 1) + min_time))
echo "Sleeping for $random_time seconds"
sleep "$random_time"
fi
}

## the component names of all shard
Expand Down
16 changes: 11 additions & 5 deletions addons/redis/redis-cluster-scripts/redis-cluster-manage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -638,11 +638,11 @@ scale_in_redis_cluster_shard() {

initialize_or_scale_out_redis_cluster() {
# TODO: remove random sleep, it's a workaround for the multi components initialization parallelism issue
sleep_random_second 10 1
sleep_random_second_when_ut_mode_false 10 1

if is_empty "$KB_CLUSTER_POD_IP_LIST" || is_empty "$SERVICE_PORT"; then
echo "Error: Required environment variable KB_CLUSTER_POD_IP_LIST and SERVICE_PORT is not set." >&2
exit 1
return 1
fi

# if the cluster is not initialized, initialize it
Expand All @@ -652,17 +652,18 @@ initialize_or_scale_out_redis_cluster() {
echo "Redis Cluster initialized successfully"
else
echo "Failed to initialize Redis Cluster" >&2
exit 1
return 1
fi
else
echo "Redis Cluster already initialized, scaling out the shard..."
if scale_out_redis_cluster_shard; then
echo "Redis Cluster scale out shard successfully"
else
echo "Failed to scale out Redis Cluster shard" >&2
exit 1
return 1
fi
fi
return 0
}

# This is magic for shellspec ut framework.
Expand All @@ -685,7 +686,12 @@ if [ $# -eq 1 ]; then
exit 0
;;
--post-provision)
initialize_or_scale_out_redis_cluster
if initialize_or_scale_out_redis_cluster; then
echo "Redis Cluster initialized or scale out shard successfully"
else
echo "Failed to initialize or scale out Redis Cluster shard" >&2
exit 1
fi
exit 0
;;
--pre-terminate)
Expand Down
138 changes: 138 additions & 0 deletions addons/redis/scripts-ut-spec/redis_cluster_manage_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1681,4 +1681,142 @@ d-98x-redis-advertised-1:31318.shard-7hy@redis-shard-7hy-redis-advertised-0:3202
End
End
End

Describe "initialize_or_scale_out_redis_cluster()"
Context "when required environment variables are not set"
setup() {
export KB_CLUSTER_POD_IP_LIST=""
export SERVICE_PORT=""
}
Before "setup"

un_setup() {
unset KB_CLUSTER_POD_IP_LIST
unset SERVICE_PORT
}
After "un_setup"

It "exits with status 1 when required environment variables are not set"
When run initialize_or_scale_out_redis_cluster
The status should be failure
The stderr should include "Error: Required environment variable KB_CLUSTER_POD_IP_LIST and SERVICE_PORT is not set."
End
End

Context "when Redis Cluster is not initialized"
check_cluster_initialized() {
return 1
}

initialize_redis_cluster() {
return 0
}

setup() {
export KB_CLUSTER_POD_IP_LIST="172.42.0.1,172.42.0.2,172.42.0.3,172.42.0.4,172.42.0.5,172.42.0.6"
export SERVICE_PORT="6379"
}
Before "setup"

un_setup() {
unset KB_CLUSTER_POD_IP_LIST
unset SERVICE_PORT
}
After "un_setup"

It "initializes Redis Cluster successfully"
When run initialize_or_scale_out_redis_cluster
The status should be success
The output should include "Redis Cluster not initialized, initializing..."
The output should include "Redis Cluster initialized successfully"
End
End

Context "when Redis Cluster is already initialized"
check_cluster_initialized() {
return 0
}

scale_out_redis_cluster_shard() {
return 0
}

setup() {
export KB_CLUSTER_POD_IP_LIST="172.42.0.1,172.42.0.2,172.42.0.3,172.42.0.4,172.42.0.5,172.42.0.6"
export SERVICE_PORT="6379"
}
Before "setup"

un_setup() {
unset KB_CLUSTER_POD_IP_LIST
unset SERVICE_PORT
}
After "un_setup"

It "scales out Redis Cluster shard successfully"
When run initialize_or_scale_out_redis_cluster
The status should be success
The output should include "Redis Cluster already initialized, scaling out the shard..."
The output should include "Redis Cluster scale out shard successfully"
End
End

Context "when failed to initialize Redis Cluster"
check_cluster_initialized() {
return 1
}

initialize_redis_cluster() {
return 1
}

setup() {
export KB_CLUSTER_POD_IP_LIST="172.42.0.1,172.42.0.2,172.42.0.3,172.42.0.4,172.42.0.5,172.42.0.6"
export SERVICE_PORT="6379"
}
Before "setup"

un_setup() {
unset KB_CLUSTER_POD_IP_LIST
unset SERVICE_PORT
}
After "un_setup"

It "exits with status 1 when failed to initialize Redis Cluster"
When run initialize_or_scale_out_redis_cluster
The status should be failure
The stderr should include "Failed to initialize Redis Cluster"
The stdout should include "Redis Cluster not initialized, initializing.."
End
End

Context "when failed to scale out Redis Cluster shard"
check_cluster_initialized() {
return 0
}

scale_out_redis_cluster_shard() {
return 1
}

setup() {
export KB_CLUSTER_POD_IP_LIST="172.42.0.1,172.42.0.2,172.42.0.3,172.42.0.4,172.42.0.5,172.42.0.6"
export SERVICE_PORT="6379"
}
Before "setup"

un_setup() {
unset KB_CLUSTER_POD_IP_LIST
unset SERVICE_PORT
}
After "un_setup"

It "exits with status 1 when failed to scale out Redis Cluster shard"
When run initialize_or_scale_out_redis_cluster
The status should be failure
The stderr should include "Failed to scale out Redis Cluster shard"
The stdout should include "Redis Cluster already initialized, scaling out the shard..."
End
End
End
End

0 comments on commit ee6db34

Please sign in to comment.