Skip to content

Commit

Permalink
minio addon add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Rookie committed Oct 18, 2024
1 parent db0a741 commit b5f9361
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 6 deletions.
5 changes: 4 additions & 1 deletion addons/minio/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
apiVersion: v1
description: High Performance, Kubernetes Native Object Storage
name: minio

version: 1.0.0-alpha.0

appVersion: master

keywords:
- storage
- object-storage
Expand All @@ -18,6 +21,6 @@ maintainers:
url: https://github.com/apecloud/kubeblocks/

annotations:
addon.kubeblocks.io/kubeblocks-version: ">=0.8.0"
addon.kubeblocks.io/kubeblocks-version: ">=1.0.0"
addon.kubeblocks.io/model: "object-storage"
addon.kubeblocks.io/provider: "community"
86 changes: 86 additions & 0 deletions addons/minio/scripts-ut-spec/initialize_patch_configmap_spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# 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 "initialize_patch_configmap_spec.sh skip cases because dependency bash version 4 or higher is not installed."
exit 0
fi

Describe "Minio init container bash script Tests"
# load the scripts to be tested and dependencies
Include ../scripts/initialize-patch-configmap.sh

init() {
ut_mode="true"
}
BeforeAll "init"

Describe "get_current_cm_key_value()"
It "returns the value of the specified key from the ConfigMap"
kubectl() {
echo "[1,2,3]"
}

When call get_current_cm_key_value "my-configmap" "my-namespace" "replicas"
The output should eq "1,2,3"
End
End

Describe "update_cm_key_value()"
It "updates the value of the specified key in the ConfigMap"
kubectl() {
return 0
}

When call update_cm_key_value "my-configmap" "my-namespace" "replicas" "[1,2,3,4]"
The status should be success
End
End

Describe "get_cm_key_new_value()"
It "returns the replicas value when cur is empty"
When call get_cm_key_new_value "" "4"
The output should eq "[4]"
End

It "returns the cur value when last equals replicas"
When call get_cm_key_new_value "1,2,3" "3"
The output should eq "[1,2,3]"
End

It "appends the replicas value to cur when last does not equal replicas"
When call get_cm_key_new_value "1,2,3" "4"
The output should eq "[1,2,3,4]"
End
End

Describe "update_configmap()"
setup() {
export MINIO_COMPONENT_NAME="minio"
export CLUSTER_NAMESPACE="default"
export MINIO_COMP_REPLICAS="4"
}
Before "setup"

un_setup() {
unset MINIO_COMPONENT_NAME
unset CLUSTER_NAMESPACE
unset MINIO_COMP_REPLICAS
}
After "un_setup"

It "updates the ConfigMap with the new replicas value"
get_current_cm_key_value() {
echo "1,2,3"
}

update_cm_key_value() {
return 0
}

When run update_configmap
The output should eq "ConfigMap minio-minio-configuration updated successfully with MINIO_REPLICAS_HISTORY=[1,2,3,4]"
End
End
End
146 changes: 146 additions & 0 deletions addons/minio/scripts-ut-spec/startup_spec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# 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 "initialize_patch_configmap_spec.sh skip cases because dependency bash version 4 or higher is not installed."
exit 0
fi

Describe "Minio startup bash script tests"
# load the scripts to be tested and dependencies
Include ../scripts/startup.sh

init() {
replicas_history_file="./replicas_history"
ut_mode="true"
}
BeforeAll "init"

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

Describe "init_buckets()"
setup() {
bucket_dir="./data"
mkdir -p "$bucket_dir"
}
Before "setup"

un_setup() {
rm -rf "$bucket_dir"
}
After "un_setup"

It "creates directories for the specified buckets"
When call init_buckets "bucket1,bucket2,bucket3"
The output should include "Successfully init bucket: $bucket_dir/bucket1"
The output should include "Successfully init bucket: $bucket_dir/bucket2"
The output should include "Successfully init bucket: $bucket_dir/bucket3"
The directory "$bucket_dir/bucket1" should be exist
The directory "$bucket_dir/bucket2" should be exist
The directory "$bucket_dir/bucket3" should be exist
End
End

Describe "read_replicas_history()"
setup() {
replicas_history_file="./replicas_history"
echo "[2,4,6]" > "$replicas_history_file"
}
Before "setup"

un_setup() {
rm -f "$replicas_history_file"
}
After "un_setup"

It "reads the replicas history from the specified file"
When call read_replicas_history "$replicas_history_file"
The output should eq "2,4,6"
End
End

Describe "generate_server_pool()"
setup() {
export HTTP_PROTOCOL="http"
export MINIO_COMPONENT_NAME="minio-minio"
export CLUSTER_NAMESPACE="default"
export CLUSTER_DOMAIN="cluster.local"
}
Before "setup"

un_setup() {
unset HTTP_PROTOCOL
unset MINIO_COMPONENT_NAME
unset CLUSTER_NAMESPACE
unset CLUSTER_DOMAIN
}
After "un_setup"

It "generates the server pool based on the replicas"
When call generate_server_pool "2,4,6"
The output should eq " http://minio-minio-{0...1}.minio-minio-headless.default.svc.cluster.local/data http://minio-minio-{2...3}.minio-minio-headless.default.svc.cluster.local/data http://minio-minio-{4...5}.minio-minio-headless.default.svc.cluster.local/data"
End
End

Describe "build_startup_cmd()"
setup() {
export HTTP_PROTOCOL="http"
export MINIO_COMPONENT_NAME="minio"
export MINIO_BUCKETS="bucket1,bucket2"
export CERTS_PATH="/certs"
export MINIO_API_PORT="9000"
export MINIO_CONSOLE_PORT="9001"
export CLUSTER_DOMAIN="cluster.local"
replicas_history_file="./replicas_history"
echo "[1,3,5]" > "$replicas_history_file"
}
Before "setup"

un_setup() {
unset HTTP_PROTOCOL
unset MINIO_COMPONENT_NAME
unset MINIO_BUCKETS
unset CERTS_PATH
unset MINIO_API_PORT
unset MINIO_CONSOLE_PORT
unset CLUSTER_DOMAIN
rm -f "$replicas_history_file"
}
After "un_setup"

It "builds the startup command with the generated server pool"
init_buckets() {
return 0
}

When call build_startup_cmd
The stderr should include "the minio replicas history is 1,3,5"
The output should eq "/usr/bin/docker-entrypoint.sh minio server http://minio-{0...0}.minio-headless..svc.cluster.local/data http://minio-{1...2}.minio-headless..svc.cluster.local/data http://minio-{3...4}.minio-headless..svc.cluster.local/data -S /certs --address :9000 --console-address :9001"
The status should be success
End

It "returns status 1 when replicas history file does not exist"
replicas_history_file="/nonexistent"

When run build_startup_cmd
The stderr should include "minio config don't existed"
The status should be failure
End
End

Describe "startup()"
It "exits with status 1 when failed to build startup command"
build_startup_cmd() {
return 1
}

When run startup
The stderr should include "Failed to build startup command"
The status should be failure
End
End
End
21 changes: 16 additions & 5 deletions addons/minio/scripts/startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ generate_server_pool() {
echo "$server"
}

startup() {
build_startup_cmd() {
if [ ! -f "$replicas_history_file" ]; then
echo "minio config don't existed"
exit 1
echo "minio config don't existed" >&2
return 1
fi

buckets="$MINIO_BUCKETS"
Expand All @@ -51,12 +51,23 @@ startup() {
fi

replicas=$(read_replicas_history "$replicas_history_file")
echo "the minio replicas history is $replicas"
echo "the minio replicas history is $replicas" >&2

server=$(generate_server_pool $replicas)
echo "the minio server pool is $server"
echo "the minio server pool is $server" >&2

cmd="/usr/bin/docker-entrypoint.sh minio server $server -S $CERTS_PATH --address :$MINIO_API_PORT --console-address :$MINIO_CONSOLE_PORT"
echo "$cmd"
return 0
}

startup() {
cmd=$(build_startup_cmd)
status=$?
if [ $status -ne 0 ]; then
echo "Failed to build startup command" >&2
exit 1
fi
echo "Starting minio server with command: $cmd"
eval "$cmd"
}
Expand Down

0 comments on commit b5f9361

Please sign in to comment.