Skip to content

Commit

Permalink
Reusing testing code across repos (#1522)
Browse files Browse the repository at this point in the history
Reusing code across repos
  • Loading branch information
cniackz authored Aug 7, 2023
1 parent df9cbf0 commit 64f6d81
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 74 deletions.
66 changes: 66 additions & 0 deletions shared-functions/shared-code.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env bash
# Copyright (C) 2023, MinIO, Inc.
#
# This code is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3,
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License, version 3,
# along with this program. If not, see <http://www.gnu.org/licenses/>

function wait_for_resource() {
waitdone=0
totalwait=0
echo "command to wait on:"
command_to_wait="kubectl -n $1 get pods -l $3=$2 --no-headers"
echo $command_to_wait

while true; do
waitdone=$($command_to_wait | wc -l)
if [ "$waitdone" -ne 0 ]; then
echo "Found $waitdone pods"
break
fi
sleep 5
totalwait=$((totalwait + 5))
if [ "$totalwait" -gt 305 ]; then
echo "Unable to get resource after 5 minutes, exiting."
try false
fi
done
}

function wait_for_resource_field_selector() {
# example 1 job:
# namespace="minio-tenant-1"
# codition="condition=Complete"
# selector="metadata.name=setup-bucket"
# wait_for_resource_field_selector $namespace job $condition $selector
#
# example 2 tenant:
# wait_for_resource_field_selector $namespace job $condition $selector
# condition=jsonpath='{.status.currentState}'=Initialized
# selector="metadata.name=storage-policy-binding"
# wait_for_resource_field_selector $namespace tenant $condition $selector 900s

namespace=$1
resourcetype=$2
condition=$3
fieldselector=$4
if [ $# -ge 5 ]; then
timeout="$5"
else
timeout="600s"
fi

echo "Waiting for $resourcetype \"$fieldselector\" for \"$condition\" ($timeout timeout)"
kubectl wait -n "$namespace" "$resourcetype" \
--for=$condition \
--field-selector $fieldselector \
--timeout="$timeout"
}
54 changes: 2 additions & 52 deletions testing/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# You should have received a copy of the GNU Affero General Public License, version 3,
# along with this program. If not, see <http://www.gnu.org/licenses/>

source "${GITHUB_WORKSPACE}/shared-functions/shared-code.sh" # This is common.sh for k8s tests across multiple repos.

## this enables :dev tag for minio/operator container image.
CI="true"
export CI
Expand Down Expand Up @@ -181,58 +183,6 @@ function destroy_kind() {
fi
}

function wait_for_resource() {
waitdone=0
totalwait=0
echo "command to wait on:"
command_to_wait="kubectl -n $1 get pods -l $3=$2 --no-headers"
echo $command_to_wait

while true; do
waitdone=$($command_to_wait | wc -l)
if [ "$waitdone" -ne 0 ]; then
echo "Found $waitdone pods"
break
fi
sleep 5
totalwait=$((totalwait + 5))
if [ "$totalwait" -gt 305 ]; then
echo "Unable to get resource after 5 minutes, exiting."
try false
fi
done
}

function wait_for_resource_field_selector() {
# example 1 job:
# namespace="minio-tenant-1"
# codition="condition=Complete"
# selector="metadata.name=setup-bucket"
# wait_for_resource_field_selector $namespace job $condition $selector
#
# example 2 tenant:
# wait_for_resource_field_selector $namespace job $condition $selector
# condition=jsonpath='{.status.currentState}'=Initialized
# selector="metadata.name=storage-policy-binding"
# wait_for_resource_field_selector $namespace tenant $condition $selector 900s

namespace=$1
resourcetype=$2
condition=$3
fieldselector=$4
if [ $# -ge 5 ]; then
timeout="$5"
else
timeout="600s"
fi

echo "Waiting for $resourcetype \"$fieldselector\" for \"$condition\" ($timeout timeout)"
kubectl wait -n "$namespace" "$resourcetype" \
--for=$condition \
--field-selector $fieldselector \
--timeout="$timeout"
}

function check_tenant_status() {
# Check MinIO is accessible
# $1 namespace
Expand Down
24 changes: 2 additions & 22 deletions tests/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# You should have received a copy of the GNU Affero General Public License, version 3,
# along with this program. If not, see <http://www.gnu.org/licenses/>

source "${GITHUB_WORKSPACE}/shared-functions/shared-code.sh" # This is common.sh for k8s tests across multiple repos.

yell() { echo "$0: $*" >&2; }

die() {
Expand Down Expand Up @@ -103,28 +105,6 @@ function check_tenant_status() {
echo "Done."
}

function wait_for_resource() {
waitdone=0
totalwait=0
echo "command to wait on:"
command_to_wait="kubectl -n $1 get pods -l $3=$2 --no-headers"
echo $command_to_wait

while true; do
waitdone=$($command_to_wait | wc -l)
if [ "$waitdone" -ne 0 ]; then
echo "Found $waitdone pods"
break
fi
sleep 5
totalwait=$((totalwait + 5))
if [ "$totalwait" -gt 305 ]; then
echo "Unable to get resource after 5 minutes, exiting."
try false
fi
done
}

# Install tenant function is being used by deploy-tenant and check-prometheus
function install_tenant() {

Expand Down

0 comments on commit 64f6d81

Please sign in to comment.