aboutsummaryrefslogtreecommitdiffstats
path: root/afm-test.native.sh
blob: e117ada008c0c5f29717aed223bfedc0cbdffe57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash

###########################################################################
# Copyright (C) 2017, 2018 IoT.bzh
#
# Author: Romain Forlot <romain.forlot@iot.bzh>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###########################################################################

trap "cleanNexit 1" SIGHUP SIGINT SIGABRT SIGTERM
cleanNexit() {
  rm -f $SOCKETSERVICE 2> /dev/null
  trap '' EXIT SIGHUP SIGINT SIGABRT SIGTERM
  [ $1 -ne 0 ] && echo "Error: Test launch failed. Code: $1" && cat ${LOGFILETEST} ${LOGFILESERVICE}
  exit $1
}

function usage() {
	cat >&2 << EOF
Usage: $0 <binding-wgt-rootdir> <test-wgt-rootdir> [mode] [timeout]
binding-wgt-rootdir: path to the test wgt file
test-wgt-rootdir: path to the test folder file
mode: SOLO (1 binder) or SERVICE (2 binders)
timeout: default 3 seconds
EOF
}

BINDER=$(command -v afb-daemon)
AFBTEST="$(pkg-config --variable libdir afb-test)/aft.so"
PORT=1234
PORTSERVICE=$((PORT+1))
TOKEN=

if [ "$1" ] && [ "$2" ]
then
	SERVICEPACKAGEDIR="$(readlink -f $1)"
	TESTPACKAGEDIR="$(readlink -f $2)"
else
	echo "Error: you doesn't specified either the binding folder location either test widget folder location."
	usage
	cleanNexit 1
fi

if [ "$3" ]
then
	MODE="$3"
else
	MODE="SOLO"
fi

if [ "$4" ]
then
	TIMEOUT=$4
else
	TIMEOUT=3
fi

TESTCFGFILE=$(find "${TESTPACKAGEDIR}" -name "aft-*.json" -print | head -n1)
TESTPROCNAME=$(grep '\"api\"' "${TESTCFGFILE}" | cut -d'"' -f4)

API=$(grep "provided-api" "${SERVICEPACKAGEDIR}/config.xml" -A1 2> /dev/null |  sed -r -e '1d' -e 's:.*"(.*)" v.*:\1:' 2> /dev/null)
if [ -z "$API" ] && [ "$MODE" = "SERVICE" ]
then
	echo "Error: you doesn't have the config.xml file. Please call 'make widget'"
	cleanNexit 2
else
	ENV_API=$(echo ${API} | sed 's:[^a-zA-Z0-9_]:_:g')
	declare AFT_${ENV_API}_CONFIG_PATH="${SERVICEPACKAGEDIR}"
	declare AFT_$(echo ${ENV_API} | sed 's:[^a-zA-Z0-9_]:_:g')_PLUGIN_PATH="${SERVICEPACKAGEDIR}"
	export AFT_${ENV_API}_CONFIG_PATH
	export AFT_${ENV_API}_PLUGIN_PATH
	PROCNAME="afbd-${API}"
	SOCKETSERVICE="/tmp/$API"
fi

export AFT_CONFIG_PATH="${TESTPACKAGEDIR}"
export AFT_PLUGIN_PATH="${TESTPACKAGEDIR}"

LOGFILESERVICE="test-service.log"
LOGFILETEST="test.log"

if [ ${MODE} = "SOLO" ]
then
	pkill "${TESTPROCNAME}"

	timeout -s 9 ${TIMEOUT} \
		${BINDER} --name="${TESTPROCNAME}" \
			--port="${PORT}" \
			--roothttp=. \
			--tracereq=common \
			--token=${TOKEN} \
			--workdir="${TESTPACKAGEDIR}" \
			--ldpaths=${SERVICEPACKAGEDIR} \
			--binding="${AFBTEST}" \
			--call="${TESTPROCNAME}/launch_all_tests:{}" \
			--call="${TESTPROCNAME}/exit:{}" \
			-vvv &> ${LOGFILETEST}
elif [ ${MODE} = "SERVICE" ]
then
	pkill "$TESTPROCNAME"
	pkill "$PROCNAME"

	timeout -s 9 ${TIMEOUT} ${BINDER} --name=${PROCNAME} \
				--workdir="${SERVICEPACKAGEDIR}" \
				--port=${PORTSERVICE} \
				--ldpaths=. \
				-vvv \
				--ws-server=unix:${SOCKETSERVICE} &> ${LOGFILESERVICE} &

	sleep 0.3

	timeout -s 9 ${TIMEOUT} ${BINDER} --name="${TESTPROCNAME}" \
				--port="${PORT}" \
				--no-httpd \
				--tracereq=common \
				--token=${TOKEN} \
				--workdir="${TESTPACKAGEDIR}" \
				--binding="${AFBTEST}" \
				--ws-client=unix:${SOCKETSERVICE} \
				--call="${TESTPROCNAME}/launch_all_tests:{}" \
				--call="${TESTPROCNAME}/exit:{}" \
				-vvv &> ${LOGFILETEST}

else
	echo "Error: No mode selected. Choose between SOLO or SERVICE"
	usage
	cleanNexit 3
fi

cleanNexit $?