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 8, 2024
1 parent 8e8d3dc commit 48ea09f
Show file tree
Hide file tree
Showing 2 changed files with 255 additions and 9 deletions.
29 changes: 20 additions & 9 deletions addons/redis/redis-cluster-scripts/redis-cluster-manage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,20 @@ init_current_comp_default_nodes_for_scale_out() {
min_lexicographical_pod_name=$(min_lexicographical_order_pod "$KB_CLUSTER_COMPONENT_POD_NAME_LIST")
min_lexicographical_pod_ordinal=$(extract_ordinal_from_object_name "$min_lexicographical_pod_name")
if is_empty "$min_lexicographical_pod_ordinal"; then
echo "Failed to get the ordinal of the min lexicographical pod $min_lexicographical_pod_name in init_current_comp_default_nodes_for_scale_out"
exit 1
echo "Failed to get the ordinal of the min lexicographical pod $min_lexicographical_pod_name in init_current_comp_default_nodes_for_scale_out" >&2
return 1
fi
for pod_name in $(echo "$KB_CLUSTER_COMPONENT_POD_NAME_LIST" | tr ',' ' '); do
pod_name_ordinal=$(extract_ordinal_from_object_name "$pod_name")
## if the CURRENT_SHARD_ADVERTISED_PORT is set, use the advertised port
## the value format of CURRENT_SHARD_ADVERTISED_PORT is "pod1Svc:nodeport1,pod2Svc:nodeport2,..."
if ! is_empty "$CURRENT_SHARD_ADVERTISED_PORT"; then
old_ifs="$IFS"
IFS=','
set -f
read -ra advertised_infos <<< "$CURRENT_SHARD_ADVERTISED_PORT"
set +f
IFS="$old_ifs"
found_advertised_port=false
for advertised_info in "${advertised_infos[@]}"; do
advertised_svc=$(echo "$advertised_info" | cut -d':' -f1)
Expand All @@ -278,8 +283,8 @@ init_current_comp_default_nodes_for_scale_out() {
if [ "$pod_name_ordinal" == "$advertised_svc_ordinal" ]; then
pod_host_ip=$(parse_host_ip_from_built_in_envs "$pod_name" "$KB_CLUSTER_COMPONENT_POD_NAME_LIST" "$KB_CLUSTER_COMPONENT_POD_HOST_IP_LIST")
if is_empty "$pod_host_ip"; then
echo "Failed to get the host ip of the pod $pod_name"
exit 1
echo "Failed to get the host ip of the pod $pod_name" >&2
return 1
fi
if equals "$pod_name_ordinal" "$min_lexicographical_pod_ordinal"; then
scale_out_shard_default_primary_node["$pod_name"]="$pod_host_ip:$advertised_port"
Expand All @@ -291,16 +296,16 @@ init_current_comp_default_nodes_for_scale_out() {
fi
done
if [ "$found_advertised_port" = false ]; then
echo "Advertised port not found for pod $pod_name"
exit 1
echo "Advertised port not found for pod $pod_name" >&2
return 1
fi
else
local pod_fqdn
local port=$SERVICE_PORT
pod_fqdn=$(get_target_pod_fqdn_from_pod_fqdn_vars "$CURRENT_SHARD_POD_FQDN_LIST" "$pod_name")
if is_empty "$pod_fqdn"; then
echo "Error: Failed to get current pod: $pod_name fqdn from current shard pod fqdn list: $CURRENT_SHARD_POD_FQDN_LIST. Exiting."
exit 1
echo "Error: Failed to get current pod: $pod_name fqdn from current shard pod fqdn list: $CURRENT_SHARD_POD_FQDN_LIST. Exiting." >&2
return 1
fi
if equals "$pod_name_ordinal" "$min_lexicographical_pod_ordinal"; then
scale_out_shard_default_primary_node["$pod_name"]="$pod_fqdn:$port"
Expand All @@ -309,6 +314,7 @@ init_current_comp_default_nodes_for_scale_out() {
fi
fi
done
return 0
}

# initialize the redis cluster primary and secondary nodes, use the min lexicographical pod of each shard as the primary nodes by default.
Expand Down Expand Up @@ -480,7 +486,12 @@ scale_out_redis_cluster_shard() {
fi

init_other_components_and_pods_info "$CURRENT_SHARD_COMPONENT_SHORT_NAME" "$KB_CLUSTER_POD_IP_LIST" "$KB_CLUSTER_POD_NAME_LIST" "$KB_CLUSTER_COMPONENT_LIST" "$KB_CLUSTER_COMPONENT_DELETING_LIST" "$KB_CLUSTER_COMPONENT_UNDELETED_LIST"
init_current_comp_default_nodes_for_scale_out
if init_current_comp_default_nodes_for_scale_out; then
echo "Redis cluster scale out shard default primary and secondary nodes successfully"
else
echo "Failed to initialize the default primary and secondary nodes for scale out"
exit 1
fi

# check the current component shard whether is already scaled out
if [ ${#scale_out_shard_default_primary_node[@]} -eq 0 ]; then
Expand Down
235 changes: 235 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 @@ -202,4 +202,239 @@ Describe "Redis Cluster Manage Bash Script Tests"
End
End

Describe "init_current_comp_default_nodes_for_scale_out()"
Context "when using advertised ports"
min_lexicographical_order_pod() {
echo "redis-shard-sxj-0"
}

parse_host_ip_from_built_in_envs() {
case "$1" in
"redis-shard-sxj-0")
echo "10.42.0.1"
;;
"redis-shard-sxj-1")
echo "10.42.0.2"
;;
esac
}

setup() {
declare -gA scale_out_shard_default_primary_node
declare -gA scale_out_shard_default_other_nodes
export KB_CLUSTER_COMPONENT_POD_NAME_LIST="redis-shard-sxj-0,redis-shard-sxj-1"
export CURRENT_SHARD_ADVERTISED_PORT="redis-shard-sxj-0:31000,redis-shard-sxj-1:31001"
}
Before "setup"

un_setup() {
unset KB_CLUSTER_COMPONENT_POD_NAME_LIST
unset CURRENT_SHARD_ADVERTISED_PORT
}
After "un_setup"

It "initializes default nodes correctly when using advertised ports"
When call init_current_comp_default_nodes_for_scale_out
The status should be success
The variable scale_out_shard_default_primary_node['redis-shard-sxj-0'] should equal "10.42.0.1:31000"
The variable scale_out_shard_default_other_nodes['redis-shard-sxj-1'] should equal "10.42.0.2:31001"
End
End

Context "when not using advertised ports"
min_lexicographical_order_pod() {
echo "redis-shard-sxj-0"
}

get_target_pod_fqdn_from_pod_fqdn_vars() {
case "$1" in
"redis-shard-sxj-0.redis-shard-sxj-headless.default.svc.cluster.local,redis-shard-sxj-1.redis-shard-sxj-headless.default.svc.cluster.local")
case "$2" in
"redis-shard-sxj-0")
echo "redis-shard-sxj-0.redis-shard-sxj-headless.default.svc.cluster.local"
;;
"redis-shard-sxj-1")
echo "redis-shard-sxj-1.redis-shard-sxj-headless.default.svc.cluster.local"
;;
esac
;;
esac
}

setup() {
declare -gA scale_out_shard_default_primary_node
declare -gA scale_out_shard_default_other_nodes
export KB_CLUSTER_COMPONENT_POD_NAME_LIST="redis-shard-sxj-0,redis-shard-sxj-1"
export CURRENT_SHARD_POD_FQDN_LIST="redis-shard-sxj-0.redis-shard-sxj-headless.default.svc.cluster.local,redis-shard-sxj-1.redis-shard-sxj-headless.default.svc.cluster.local"
export SERVICE_PORT="6379"
}
Before "setup"

un_setup() {
unset KB_CLUSTER_COMPONENT_POD_NAME_LIST
unset CURRENT_SHARD_POD_FQDN_LIST
unset SERVICE_PORT
}
After "un_setup"

It "initializes default nodes correctly when not using advertised ports"
When call init_current_comp_default_nodes_for_scale_out
The status should be success
The variable scale_out_shard_default_primary_node['redis-shard-sxj-0'] should equal "redis-shard-sxj-0.redis-shard-sxj-headless.default.svc.cluster.local:6379"
The variable scale_out_shard_default_other_nodes['redis-shard-sxj-1'] should equal "redis-shard-sxj-1.redis-shard-sxj-headless.default.svc.cluster.local:6379"
End
End

Context "when failed to get ordinal of min lexicographical pod"
min_lexicographical_order_pod() {
echo "redis-shard-sxj-0"
}

extract_ordinal_from_object_name() {
return 1
}

setup() {
export KB_CLUSTER_COMPONENT_POD_NAME_LIST="redis-shard-sxj-0,redis-shard-sxj-1"
}
Before "setup"

un_setup() {
unset KB_CLUSTER_COMPONENT_POD_NAME_LIST
}
After "un_setup"

It "exits with error when failed to get ordinal of min lexicographical pod"
When run init_current_comp_default_nodes_for_scale_out
The status should be failure
The stderr should include "Failed to get the ordinal of the min lexicographical pod redis-shard-sxj-0 in init_current_comp_default_nodes_for_scale_out"
End
End

Context "when failed to get host ip of pod"
min_lexicographical_order_pod() {
echo "redis-shard-sxj-0"
}

extract_ordinal_from_object_name() {
case "$1" in
"redis-shard-sxj-0")
echo "0"
;;
"redis-shard-sxj-1")
echo "1"
;;
esac
}

parse_host_ip_from_built_in_envs() {
return 1
}

setup() {
export KB_CLUSTER_COMPONENT_POD_NAME_LIST="redis-shard-sxj-0,redis-shard-sxj-1"
export CURRENT_SHARD_ADVERTISED_PORT="redis-shard-sxj-0:31000,redis-shard-sxj-1:31001"
}
Before "setup"

un_setup() {
unset KB_CLUSTER_COMPONENT_POD_NAME_LIST
unset CURRENT_SHARD_ADVERTISED_PORT
}
After "un_setup"

It "exits with error when failed to get host ip of pod"
When run init_current_comp_default_nodes_for_scale_out
The status should be failure
The stderr should include "Failed to get the host ip of the pod redis-shard-sxj-0"
End
End

Context "when advertised port not found for pod"
min_lexicographical_order_pod() {
echo "redis-shard-sxj-0"
}

extract_ordinal_from_object_name() {
case "$1" in
"redis-shard-sxj-0")
echo "0"
;;
"redis-shard-sxj-1")
echo "1"
;;
esac
}

parse_host_ip_from_built_in_envs() {
case "$1" in
"redis-shard-sxj-0")
echo "10.42.0.1"
;;
"redis-shard-sxj-1")
echo "10.42.0.2"
;;
esac
}

setup() {
export KB_CLUSTER_COMPONENT_POD_NAME_LIST="redis-shard-sxj-0,redis-shard-sxj-1"
export CURRENT_SHARD_ADVERTISED_PORT="redis-shard-sxj-0:31000"
}
Before "setup"

un_setup() {
unset KB_CLUSTER_COMPONENT_POD_NAME_LIST
unset CURRENT_SHARD_ADVERTISED_PORT
}
After "un_setup"

It "exits with error when advertised port not found for pod"
When run init_current_comp_default_nodes_for_scale_out
The status should be failure
The stderr should include "Advertised port not found for pod redis-shard-sxj-1"
End
End

Context "when failed to get pod fqdn"
min_lexicographical_order_pod() {
echo "redis-shard-sxj-0"
}

extract_ordinal_from_object_name() {
case "$1" in
"redis-shard-sxj-0")
echo "0"
;;
"redis-shard-sxj-1")
echo "1"
;;
esac
}

get_target_pod_fqdn_from_pod_fqdn_vars() {
return 1
}

setup() {
export KB_CLUSTER_COMPONENT_POD_NAME_LIST="redis-shard-sxj-0,redis-shard-sxj-1"
export CURRENT_SHARD_POD_FQDN_LIST="redis-shard-sxj-0.redis-shard-sxj-headless.default.svc.cluster.local,redis-shard-sxj-1.redis-shard-sxj-headless.default.svc.cluster.local"
export SERVICE_PORT="6379"
}
Before "setup"

un_setup() {
unset KB_CLUSTER_COMPONENT_POD_NAME_LIST
unset CURRENT_SHARD_POD_FQDN_LIST
unset SERVICE_PORT
}
After "un_setup"

It "exits with error when failed to get pod fqdn"
When run init_current_comp_default_nodes_for_scale_out
The status should be failure
The stderr should include "Error: Failed to get current pod: redis-shard-sxj-0 fqdn from current shard pod fqdn list: redis-shard-sxj-0.redis-shard-sxj-headless.default.svc.cluster.local,redis-shard-sxj-1.redis-shard-sxj-headless.default.svc.cluster.local. Exiting."
End
End
End
End

0 comments on commit 48ea09f

Please sign in to comment.