Updating image version and fixing tests
[integration/csit.git] / plans / so / integration-etsi-testing / config / wait-for-container.sh
1 #!/bin/bash
2 #
3 # ============LICENSE_START=======================================================
4 #  Copyright (C) 2019 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 SCRIPT_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
25 SCRIPT_NAME=$(basename $0)
26 WAIT_FOR_SCRIPT=$SCRIPT_HOME/wait-for.sh
27
28 # Process the arguments passed to the script
29 usage()
30 {
31  _msg_="$@"
32  cat<<-EOF
33  Command Arguments:
34
35  -c, --container-name
36  Mandatory argument. container name
37
38  -n, --network-name
39  Mandatory argument. network name
40
41  -t, --timeout
42  Mandatory argument. time out value in seconds (must be number)
43
44  --help
45  Optional argument.  Display this usage.
46
47 EOF
48  exit 1
49 }
50
51 current_timestamp()
52 {
53  date +"%Y-%m-%d %H:%M:%S"
54 }
55
56 # Called when script is executed with invalid arguments
57 invalid_arguments()
58 {
59  echo "Missing or invalid option(s):"
60  echo "$@"
61  echo "Try --help for more information"
62  exit 1
63 }
64
65 process_arguments()
66 {
67  SHORT_ARGS="c:n:t:"
68  LONG_ARGS="help,container-name:,network-name:,timeout:"
69
70  args=$(getopt -o $SHORT_ARGS -l $LONG_ARGS -n "$0"  -- "$@"  2>&1 )
71  [[ $? -ne 0 ]] && invalid_arguments $( echo " $args"| head -1 )
72  [[ $# -eq 0 ]] && invalid_arguments "No options provided"
73
74  eval set -- "$args"
75  cmd_arg="$0"
76
77  while true; do
78     case "$1" in
79          -c|--container-name)
80           NAME=$2
81           shift 2 ;;
82          -n|--network-name)
83           NETWORK_NAME=$2
84           shift 2 ;;
85           -t|--timeout)
86           TIME_OUT=$2
87           shift 2 ;;
88          --help)
89           usage
90           exit 0
91           ;;
92          --)
93           shift
94           break ;;
95          *)
96           echo BAD ARGUMENTS # perhaps error
97           break ;;
98       esac
99  done
100
101  if [ -z "$NAME" ]; then
102    echo "$SCRIPT_NAME $(current_timestamp): error: Container name  must not be empty! $NAME" >&2; exit 1
103  fi
104
105  if [ -z "$NETWORK_NAME" ]; then
106    echo "$SCRIPT_NAME $(current_timestamp): error: network name  must not be empty! $NETWORK_NAME" >&2; exit 1
107  fi
108
109  regex='^[0-9]+$'
110  if ! [[ $TIME_OUT =~ $regex ]] ; then
111    echo "$SCRIPT_NAME $(current_timestamp): error: TIME_OUT must be number $TIME_OUT" >&2; exit 1
112  fi
113
114  CONTAINER_NAME=$(docker ps -aqf "name=$NAME" --format "{{.Names}}")
115
116  if [ $? -ne 0 ]; then
117    echo "$SCRIPT_NAME $(current_timestamp) ERROR: Unable to find container using $NAME"
118    exit 1
119  fi
120
121  result=$(docker inspect --format '{{.State.Running}}' $CONTAINER_NAME)
122
123  if [ $result != "true" ] ; then
124   docker logs $CONTAINER_NAME
125   echo "$SCRIPT_NAME $(current_timestamp) ERROR: $CONTAINER_NAME container is not running"
126   exit 1
127  fi
128
129  HOST_IP=$(docker inspect --format '{{ index .NetworkSettings.Networks "'$NETWORK_NAME'" "IPAddress"}}' $CONTAINER_NAME)
130
131  if [ $? -ne 0 ] || [ -z $HOST_IP ] ; then
132    echo "$SCRIPT_NAME $(current_timestamp) ERROR: Unable to find HOST IP using network name: $NETWORK_NAME and container name: $CONTAINER_NAME"
133    exit 1
134  fi
135
136  PORT=$(docker port $CONTAINER_NAME | cut -c1-$(docker port $CONTAINER_NAME | grep -aob '/' | grep -oE '[0-9]+'))
137
138  if [ $? -ne 0 ] || [ -z $PORT ] ; then
139    echo "$SCRIPT_NAME $(current_timestamp) ERROR: Unable to find PORT using project name: $PROJECT_NAME and container name: $CONTAINER_NAME"
140    exit 1
141  fi
142
143  $WAIT_FOR_SCRIPT -t "$TIME_OUT" -h "$HOST_IP" -p "$PORT"
144
145  if [ $? -ne 0 ]; then
146    docker logs $CONTAINER_NAME
147    echo "$SCRIPT_NAME $(current_timestamp) ERROR: wait-for.sh failed ..."
148    exit 1
149  fi
150
151  echo "$SCRIPT_NAME $(current_timestamp): finished successfully"
152 }
153
154 # main body
155 process_arguments $@