Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: upgrade zk to v1.0 api, refactor script and add ut #1097

Merged
merged 9 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions addons-cluster/zookeeper/templates/cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ spec:
{{- include "kblib.affinity" . | indent 2 }}
componentSpecs:
- name: zookeeper
## TODO: use regex pattern when cluster validation supported
componentDef: zookeeper
replicas: {{ .Values.replicas }}
serviceAccountName: {{ include "kblib.serviceAccountName" . }}
Expand All @@ -17,9 +18,9 @@ spec:
{{- include "kblib.componentResources" . | indent 6 }}
env:
- name: ZOOKEEPER_IMAGE_VERSION
value: "{{ .Values.serviceVersion }}"
value: "{{ .Values.version }}"
volumeClaimTemplates:
- name: data # ref clusterDefinition components.containers.volumeMounts.name
- name: data
spec:
accessModes:
- ReadWriteOnce
Expand Down
8 changes: 6 additions & 2 deletions addons-cluster/zookeeper/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ memory: 2
# cpu: 1
# memory: 1


## @param storage, the unit is Gi
storage: 10

logStorage: 2
## @param log storage, the unit is Gi
logStorage: 2

## sc for log and data
storageClassName:
106 changes: 106 additions & 0 deletions addons/zookeeper/scripts-ut-spec/roleprobe_spec.sh
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
149 changes: 149 additions & 0 deletions addons/zookeeper/scripts-ut-spec/startup_spec.sh
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
35 changes: 35 additions & 0 deletions addons/zookeeper/scripts/roleprobe.sh
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
Loading
Loading