1#!/bin/bash
2# Copyright (C) 2015-2017 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
3# This Source Code Form is subject to the terms of the Mozilla Public
4# License, v. 2.0. If a copy of the MPL was not distributed with this
5# file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7# Purpose: This script is needed to start the services with
8# one command. This is necessary as ctest - which is used to run the
9# tests - isn't able to start multiple binaries for one testcase. Therefore
10# the testcase simply executes this script. This script then runs the services
11# and checks that all exit successfully.
12
13if [ $# -lt 1 ]
14then
15    echo "Please pass a json file to this script."
16    echo "For example: $0 initial_event_test_diff_client_ids_diff_ports_master.json"
17    echo "To use the same service id but different instances on the node pass SAME_SERVICE_ID as third parameter"
18    echo "To ensure the first client only subscribes to one event pass SUBSCRIBE_ONLY_ONE as third/fourth parameter"
19    exit 1
20fi
21
22PASSED_JSON_FILE=$1
23# Remove processed options from $@
24shift 1
25REMAINING_OPTIONS="$@"
26
27print_starter_message () {
28
29if [ ! -z "$USE_LXC_TEST" ]; then
30    echo "starting initial event test on slave LXC with params $CLIENT_JSON_FILE $REMAINING_OPTIONS"
31    ssh -tt -i $SANDBOX_ROOT_DIR/commonapi_main/lxc-config/.ssh/mgc_lxc/rsa_key_file.pub -o StrictHostKeyChecking=no root@$LXC_TEST_SLAVE_IP "bash -ci \"set -m; cd \\\$SANDBOX_TARGET_DIR/vsomeip_lib/test; ./initial_event_test_slave_starter.sh $CLIENT_JSON_FILE $REMAINING_OPTIONS\"" &
32elif [ ! -z "$USE_DOCKER" ]; then
33    docker exec $DOCKER_IMAGE sh -c "cd $DOCKER_TESTS && ./initial_event_test_slave_starter.sh $CLIENT_JSON_FILE $REMAINING_OPTIONS" &
34elif [ ! -z "$JENKINS" ]; then
35    ssh -tt -i $PRV_KEY -o StrictHostKeyChecking=no jenkins@$IP_SLAVE "bash -ci \"set -m; cd $WS_ROOT/build/test; ./initial_event_test_slave_starter.sh $CLIENT_JSON_FILE $REMAINING_OPTIONS\" >> $WS_ROOT/slave_test_output 2>&1" &
36else
37cat <<End-of-message
38*******************************************************************************
39*******************************************************************************
40** Please now run:
41** initial_event_test_slave_starter.sh $CLIENT_JSON_FILE $REMAINING_OPTIONS
42** from an external host to successfully complete this test.
43**
44** You probably will need to adapt the 'unicast' settings in
45** initial_event_test_diff_client_ids_diff_ports_master.json and
46** initial_event_test_diff_client_ids_diff_ports_slave.json to your personal setup.
47*******************************************************************************
48*******************************************************************************
49End-of-message
50fi
51}
52
53# replace master with slave to be able display the correct json file to be used
54# with the slave script
55MASTER_JSON_FILE=$PASSED_JSON_FILE
56CLIENT_JSON_FILE=${MASTER_JSON_FILE/master/slave}
57
58FAIL=0
59
60# Start the services
61export VSOMEIP_CONFIGURATION=$PASSED_JSON_FILE
62
63export VSOMEIP_APPLICATION_NAME=initial_event_test_service_one
64./initial_event_test_service 1 $REMAINING_OPTIONS &
65PID_SERVICE_ONE=$!
66
67export VSOMEIP_APPLICATION_NAME=initial_event_test_service_two
68./initial_event_test_service 2 $REMAINING_OPTIONS &
69PID_SERVICE_TWO=$!
70
71export VSOMEIP_APPLICATION_NAME=initial_event_test_service_three
72./initial_event_test_service 3 $REMAINING_OPTIONS &
73PID_SERVICE_THREE=$!
74
75sleep 3
76
77unset VSOMEIP_APPLICATION_NAME
78
79# Array for client pids
80CLIENT_PIDS=()
81
82# Start first client which subscribes remotely
83./initial_event_test_client 9000 DONT_EXIT $REMAINING_OPTIONS &
84FIRST_PID=$!
85
86# Start availability checker in order to wait until the services on the remote
87# were started as well
88./initial_event_test_availability_checker 1234 $REMAINING_OPTIONS &
89PID_AVAILABILITY_CHECKER=$!
90
91sleep 1
92
93print_starter_message
94
95# remove SUBSCRIBE_ONLY_ONCE parameter from $REMAINING_OPTIONS to ensure the
96# following clients subscribe normaly
97REMAINING_OPTIONS=${REMAINING_OPTIONS%SUBSCRIBE_ONLY_ONE}
98REMAINING_OPTIONS=${REMAINING_OPTIONS#SUBSCRIBE_ONLY_ONE}
99
100
101# wait until the services on the remote node were started as well
102echo "WAITING FOR SERVICE AVAILABILITY"
103wait $PID_AVAILABILITY_CHECKER
104echo "ALL SERVICES ARE AVAILABLE NOW"
105
106sleep 2
107
108for client_number in $(seq 9001 9011)
109do
110   ./initial_event_test_client $client_number STRICT_CHECKING $REMAINING_OPTIONS &
111   CLIENT_PIDS+=($!)
112done
113
114
115# Wait until all clients are finished
116for job in ${CLIENT_PIDS[*]}
117do
118    # Fail gets incremented if a client exits with a non-zero exit code
119    wait $job || FAIL=$(($FAIL+1))
120done
121
122# wait until all clients exited on slave side
123./initial_event_test_stop_service MASTER &
124PID_STOP_SERVICE=$!
125wait $PID_STOP_SERVICE
126
127# shutdown the first client
128kill $FIRST_PID
129wait $FIRST_PID || FAIL=$(($FAIL+1))
130
131# shutdown the services
132kill $PID_SERVICE_THREE $PID_SERVICE_TWO $PID_SERVICE_ONE
133wait $PID_SERVICE_THREE $PID_SERVICE_TWO $PID_SERVICE_ONE
134
135
136
137sleep 1
138echo ""
139
140# Check if both exited successfully 
141if [ $FAIL -eq 0 ]
142then
143    exit 0
144else
145    exit 1
146fi
147