-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: upgrade zk to v1.0 api, refactor script and add ut (#1097)
- Loading branch information
Showing
16 changed files
with
485 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
# 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 "roleprobe_spec.sh skip cases because dependency bash version 4 or higher is not installed." | ||
exit 0 | ||
fi | ||
|
||
Describe "ZooKeeper Startup Bash Script Tests" | ||
# load the scripts to be tested and dependencies | ||
Include ../scripts/roleprobe.sh | ||
|
||
init() { | ||
zk_env_file="./zkEnv.sh" | ||
ut_mode="true" | ||
} | ||
BeforeAll "init" | ||
|
||
cleanup() { | ||
rm -f $zk_env_file; | ||
} | ||
AfterAll 'cleanup' | ||
|
||
Describe "get_zookeeper_mode()" | ||
It "returns standalone mode" | ||
java() { | ||
echo "Mode: standalone" | ||
} | ||
|
||
When call get_zookeeper_mode | ||
The output should eq "standalone" | ||
End | ||
|
||
It "returns leader mode" | ||
java() { | ||
echo "Mode: leader" | ||
} | ||
|
||
When call get_zookeeper_mode | ||
The output should eq "leader" | ||
End | ||
|
||
It "returns follower mode" | ||
java() { | ||
echo "Mode: follower" | ||
} | ||
|
||
When call get_zookeeper_mode | ||
The output should eq "follower" | ||
End | ||
End | ||
|
||
Describe "load_zk_env()" | ||
setup() { | ||
touch $zk_env_file | ||
echo "#!/bin/bash" > $zk_env_file | ||
echo "export ZOO_LOG_DIR=/var/log/zookeeper" >> $zk_env_file | ||
echo "export ZOO_LOG4J_PROP=INFO,ROLLINGFILE" >> $zk_env_file | ||
chmod +x $zk_env_file | ||
} | ||
Before "setup" | ||
|
||
un_setup() { | ||
rm -rf $zk_env_file | ||
unset ZOO_LOG_DIR | ||
unset ZOO_LOG4J_PROP | ||
} | ||
After "un_setup" | ||
|
||
It "loads zkEnv.sh and sets environment variables" | ||
When call load_zk_env | ||
The variable ZOO_LOG_DIR should eq "/var/log/zookeeper" | ||
The variable ZOO_LOG4J_PROP should eq "INFO,ROLLINGFILE" | ||
End | ||
End | ||
|
||
Describe "get_zk_role()" | ||
It "returns leader when mode is standalone" | ||
get_zookeeper_mode() { | ||
echo "standalone" | ||
} | ||
|
||
When call get_zk_role | ||
The output should eq "leader" | ||
End | ||
|
||
It "returns leader when mode is leader" | ||
get_zookeeper_mode() { | ||
echo "leader" | ||
} | ||
|
||
When call get_zk_role | ||
The output should eq "leader" | ||
End | ||
|
||
It "returns follower when mode is follower" | ||
get_zookeeper_mode() { | ||
echo "follower" | ||
} | ||
|
||
When call get_zk_role | ||
The output should eq "follower" | ||
End | ||
End | ||
End |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
# 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 "startup_spec.sh skip cases because dependency bash version 4 or higher is not installed." | ||
exit 0 | ||
fi | ||
|
||
Describe "ZooKeeper Startup Bash Script Tests" | ||
# load the scripts to be tested and dependencies | ||
Include ../scripts/startup.sh | ||
|
||
init() { | ||
myid_file="./myid" | ||
ut_mode="true" | ||
} | ||
BeforeAll "init" | ||
|
||
cleanup() { | ||
rm -f $myid_file; | ||
} | ||
AfterAll 'cleanup' | ||
|
||
Describe "set_zookeeper_server_id()" | ||
Context "when myid_file exists" | ||
setup() { | ||
echo "1" > $myid_file | ||
} | ||
Before "setup" | ||
|
||
un_setup() { | ||
rm -rf $myid_file | ||
} | ||
After "un_setup" | ||
|
||
It "sets ZOO_SERVER_ID from $myid_file" | ||
When call set_zookeeper_server_id | ||
The variable ZOO_SERVER_ID should eq "1" | ||
End | ||
End | ||
|
||
Context "when $myid_file does not exist" | ||
setup() { | ||
rm -rf $myid_file | ||
export CURRENT_POD_NAME="zookeeper-2" | ||
} | ||
Before "setup" | ||
|
||
un_setup() { | ||
unset CURRENT_POD_NAME | ||
} | ||
After "un_setup" | ||
|
||
It "sets ZOO_SERVER_ID from CURRENT_POD_NAME and creates $myid_file" | ||
When call set_zookeeper_server_id | ||
The variable ZOO_SERVER_ID should eq "2" | ||
The contents of file "$myid_file" should eq "2" | ||
End | ||
End | ||
End | ||
|
||
Describe "compare_version()" | ||
It "returns true when v1 > v2" | ||
When call compare_version "gt" "3.5.0" "3.4.0" | ||
The status should be success | ||
End | ||
|
||
It "returns false when v1 <= v2" | ||
When call compare_version "gt" "3.4.0" "3.5.0" | ||
The status should be failure | ||
End | ||
|
||
It "returns true when v1 <= v2" | ||
When call compare_version "le" "3.4.0" "3.5.0" | ||
The status should be success | ||
End | ||
|
||
It "returns false when v1 > v2" | ||
When call compare_version "le" "3.5.0" "3.4.0" | ||
The status should be failure | ||
End | ||
|
||
It "returns true when v1 < v2" | ||
When call compare_version "lt" "3.4.0" "3.5.0" | ||
The status should be success | ||
End | ||
|
||
It "returns false when v1 >= v2" | ||
When call compare_version "lt" "3.5.0" "3.4.0" | ||
The status should be failure | ||
End | ||
|
||
It "returns true when v1 >= v2" | ||
When call compare_version "ge" "3.5.0" "3.4.0" | ||
The status should be success | ||
End | ||
|
||
It "returns false when v1 < v2" | ||
When call compare_version "ge" "3.4.0" "3.5.0" | ||
The status should be failure | ||
End | ||
End | ||
|
||
Describe "set_scripts_path()" | ||
Context "when ZOOKEEPER_IMAGE_VERSION is not set" | ||
It "sets scripts_path to /opt/bitnami/scripts/zookeeper" | ||
When call set_scripts_path | ||
The variable scripts_path should eq "/opt/bitnami/scripts/zookeeper" | ||
End | ||
End | ||
|
||
Context "when ZOOKEEPER_IMAGE_VERSION is set" | ||
Context "when ZOOKEEPER_IMAGE_VERSION < 3.6.0" | ||
setup() { | ||
export ZOOKEEPER_IMAGE_VERSION="3.5.0" | ||
} | ||
Before "setup" | ||
|
||
un_setup() { | ||
unset ZOOKEEPER_IMAGE_VERSION | ||
} | ||
After "un_setup" | ||
|
||
It "sets scripts_path to /opt/bitnami/scripts/zookeeper" | ||
When call set_scripts_path | ||
The variable scripts_path should eq "/opt/bitnami/scripts/zookeeper" | ||
End | ||
End | ||
|
||
Context "when ZOOKEEPER_IMAGE_VERSION >= 3.6.0" | ||
setup() { | ||
export ZOOKEEPER_IMAGE_VERSION="3.6.0" | ||
} | ||
Before "setup" | ||
|
||
un_setup() { | ||
unset ZOOKEEPER_IMAGE_VERSION | ||
} | ||
After "un_setup" | ||
|
||
It "sets scripts_path to empty string" | ||
When call set_scripts_path | ||
The variable scripts_path should eq "" | ||
End | ||
End | ||
End | ||
End | ||
End |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/bin/bash | ||
|
||
zk_env_file="$ZOOBINDIR"/zkEnv.sh | ||
|
||
load_zk_env() { | ||
# shellcheck source=$ZOOBINDIR"/zkEnv.sh | ||
source "$zk_env_file" > /dev/null | ||
} | ||
|
||
get_zookeeper_mode() { | ||
local stat | ||
stat=$(java -cp "$CLASSPATH" $CLIENT_JVMFLAGS $JVMFLAGS org.apache.zookeeper.client.FourLetterWordMain localhost 2181 srvr 2> /dev/null | grep Mode) | ||
echo "$stat" | awk -F': ' '{print $2}' | tr -d '[:space:]\n' | ||
} | ||
|
||
get_zk_role() { | ||
local mode | ||
mode=$(get_zookeeper_mode) | ||
if [[ "$mode" == "standalone" ]]; then | ||
printf "leader" | ||
else | ||
printf "%s" "$mode" | ||
fi | ||
} | ||
|
||
# This is magic for shellspec ut framework. | ||
# Sometime, functions are defined in a single shell script. | ||
# You will want to test it. but you do not want to run the script. | ||
# When included from shellspec, __SOURCED__ variable defined and script | ||
# end here. The script path is assigned to the __SOURCED__ variable. | ||
${__SOURCED__:+false} : || return 0 | ||
|
||
# main | ||
load_zk_env | ||
get_zk_role |
Oops, something went wrong.