Adding kind cluster registration test
[integration/csit.git] / plans / so / integration-cnfm-testing / config / wait-for-kind-cluster-container.sh
1 #!/bin/bash
2 #
3 # ============LICENSE_START=======================================================
4 #  Copyright (C) 2023 Nordix Foundation.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # SPDX-License-Identifier: Apache-2.0
19 # ============LICENSE_END=========================================================
20 #
21
22 # @author Waqas Ikram (waqas.ikram@est.tech)
23
24 SLEEP_TIME=5
25 SUCCESSFUL_TEXT="Successfully created kind cluster"
26 FAILURE_TEXT="Failed creating kind cluster"
27 SCRIPT_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
28 SCRIPT_NAME=$(basename $0)
29
30 current_timestamp()
31 {
32  date +"%Y-%m-%d %H:%M:%S"
33 }
34
35 # main body
36 if [ -z "$TIME_OUT_DEFAULT_VALUE_SEC" ]; then
37     echo "$SCRIPT_NAME $(current_timestamp): ERROR: Undefined value for TIME_OUT_DEFAULT_VALUE_SEC attribute"
38     exit 1
39 fi
40
41 CONTAINER_NAME=$(docker ps -aqf "name=kind-cluster" --format "{{.Names}}")
42 if [ -z "$CONTAINER_NAME" ]; then
43    echo "$SCRIPT_NAME $(current_timestamp): Unable to find kind-cluster docker container id "
44    exit 1
45 fi
46
47 START_TIME_IN_SECONDS=`date +%s`
48 TIME_OUT_END_TIME_IN_SECONDS=$(($START_TIME_IN_SECONDS+$TIME_OUT_DEFAULT_VALUE_SEC));
49
50
51 echo echo "$SCRIPT_NAME $(current_timestamp): $SCRIPT_NAME script Start Time `date -d @$START_TIME_IN_SECONDS`"
52 echo echo "$SCRIPT_NAME $(current_timestamp): $SCRIPT_NAME will time out at `date -d @$TIME_OUT_END_TIME_IN_SECONDS`"
53
54 while [ `date +%s` -lt "$TIME_OUT_END_TIME_IN_SECONDS" ]; do
55     echo "$(current_timestamp): Waiting for $CONTAINER_NAME to finish ..."
56
57     result=$(docker logs "$CONTAINER_NAME" 2>&1 | grep -E "$SUCCESSFUL_TEXT|$FAILURE_TEXT")
58     if [ ! -z "$result" ]; then
59         echo "$SCRIPT_NAME $(current_timestamp): Found result: $result"
60         break;
61     fi
62     echo "$(current_timestamp): Sleeping for ${SLEEP_TIME} seconds"
63     sleep ${SLEEP_TIME}
64 done
65
66 if [ -z "$result" ] || echo "$result" | grep -E "$FAILURE_TEXT"; then
67    echo "$SCRIPT_NAME $(current_timestamp): ERROR: failed to create kind-cluster... "
68    echo "-------------- $CONTAINER_NAME logs -------------"
69    docker logs "$CONTAINER_NAME"
70    echo "------------------------------------------------------------"
71    exit 1
72 fi
73
74 echo "$SCRIPT_NAME $(current_timestamp): Finished successfully '$result'..."
75 exit 0