forked from galasa-dev/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-galasactl-local.sh
executable file
·329 lines (265 loc) · 9.07 KB
/
test-galasactl-local.sh
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/bin/bash
#
# Copyright contributors to the Galasa project
#
# SPDX-License-Identifier: EPL-2.0
#
echo "Running script test-galasactl-local.sh"
# This script can be ran locally or executed in a pipeline to test the various built binaries of galasactl
# This script tests the 'galasactl project create' and 'galasactl runs submit local' commands
# Pre-requesite: the CLI must have been built first so the binaries are present in the ./bin directory
# Where is this script executing from ?
BASEDIR=$(dirname "$0");pushd $BASEDIR 2>&1 >> /dev/null ;BASEDIR=$(pwd);popd 2>&1 >> /dev/null
export ORIGINAL_DIR=$(pwd)
cd "${BASEDIR}"
#--------------------------------------------------------------------------
#
# Set Colors
#
#--------------------------------------------------------------------------
bold=$(tput bold)
underline=$(tput sgr 0 1)
reset=$(tput sgr0)
red=$(tput setaf 1)
green=$(tput setaf 76)
white=$(tput setaf 7)
tan=$(tput setaf 202)
blue=$(tput setaf 25)
#--------------------------------------------------------------------------
#
# Headers and Logging
#
#--------------------------------------------------------------------------
underline() { printf "${underline}${bold}%s${reset}\n" "$@"
}
h1() { printf "\n${underline}${bold}${blue}%s${reset}\n" "$@"
}
h2() { printf "\n${underline}${bold}${white}%s${reset}\n" "$@"
}
debug() { printf "${white}%s${reset}\n" "$@"
}
info() { printf "${white}➜ %s${reset}\n" "$@"
}
success() { printf "${green}✔ %s${reset}\n" "$@"
}
error() { printf "${red}✖ %s${reset}\n" "$@"
}
warn() { printf "${tan}➜ %s${reset}\n" "$@"
}
bold() { printf "${bold}%s${reset}\n" "$@"
}
note() { printf "\n${underline}${bold}${blue}Note:${reset} ${blue}%s${reset}\n" "$@"
}
#-----------------------------------------------------------------------------------------
# Functions
#-----------------------------------------------------------------------------------------
function usage {
info "Syntax: test-galasactl-local.sh [flags]"
cat << EOF
Optional flags are:
--buildTool maven: Use Maven to build the generated project.
--buildTool gradle: Use Gradle to build the generated project.
If neither are specified, defaults to Maven.
EOF
}
#-----------------------------------------------------------------------------------------
# Process parameters
#-----------------------------------------------------------------------------------------
buildTool=""
while [ "$1" != "" ]; do
case $1 in
--buildTool ) shift
buildTool="$1"
;;
-h | --help ) usage
exit
;;
* ) error "Unexpected argument $1"
usage
exit 1
esac
shift
done
if [[ "${buildTool}" != "" ]]; then
case ${buildTool} in
maven ) echo "Using Maven"
;;
gradle ) echo "Using Gradle"
;;
* ) error "Unrecognised build tool ${buildTool}"
usage
exit 1
esac
else
export buildTool="maven"
info "No build tool specified so defaulting to Maven."
fi
#--------------------------------------------------------------------------
function calculate_galasactl_executable {
h2 "Calculate the name of the galasactl executable for this machine/os"
raw_os=$(uname -s) # eg: "Darwin"
os=""
case $raw_os in
Darwin*)
os="darwin"
;;
Windows*)
os="windows"
;;
Linux*)
os="linux"
;;
*)
error "Failed to recognise which operating system is in use. $raw_os"
exit 1
esac
architecture=$(uname -m)
export binary="galasactl-${os}-${architecture}"
info "galasactl binary is ${binary}"
success "OK"
}
#--------------------------------------------------------------------------
# Initialise Galasa home
function galasa_home_init {
h2 "Initialising galasa home directory"
rm -rf ${BASEDIR}/temp
mkdir -p ${BASEDIR}/temp
cd ${BASEDIR}/temp
cmd="${BASEDIR}/bin/${binary} local init --development \
--log -"
info "Command is: $cmd"
$cmd
rc=$?
if [[ "${rc}" != "0" ]]; then
error "Failed to initialise galasa home"
exit 1
fi
success "Galasa home initialised"
}
#--------------------------------------------------------------------------
# Invoke the galasactl command to create a project.
function generate_sample_code {
h2 "Invoke the tool to create a sample project."
cd ${BASEDIR}/temp
export PACKAGE_NAME="dev.galasa.example.banking"
if [[ "${buildTool}" == "maven" ]]; then
${BASEDIR}/bin/${binary} project create --package ${PACKAGE_NAME} --features payee --obr --maven --force --development --log -
elif [[ "${buildTool}" == "gradle" ]]; then
${BASEDIR}/bin/${binary} project create --package ${PACKAGE_NAME} --features payee --obr --gradle --force --development --log -
fi
rc=$?
if [[ "${rc}" != "0" ]]; then
error " Failed to create the galasa test project using galasactl command. rc=${rc}"
exit 1
fi
success "OK"
}
#--------------------------------------------------------------------------
# Now build the source it created
function build_generated_source {
h2 "Building the sample project we just generated."
cd ${BASEDIR}/temp/${PACKAGE_NAME}
if [[ "${buildTool}" == "maven" ]]; then
mvn clean test install
elif [[ "${buildTool}" == "gradle" ]]; then
gradle clean build publishToMavenLocal
fi
rc=$?
if [[ "${rc}" != "0" ]]; then
error " Failed to build the generated source code which galasactl created."
exit 1
fi
success "OK"
}
#--------------------------------------------------------------------------
# Run test using the galasactl locally in a JVM
function submit_local_test {
h2 "Submitting a local test using galasactl in a local JVM"
cd ${BASEDIR}/temp/*banking
BUNDLE=$1
JAVA_CLASS=$2
OBR_GROUP_ID=$3
OBR_ARTIFACT_ID=$4
OBR_VERSION=$5
export REMOTE_MAVEN=https://development.galasa.dev/main/maven-repo/obr/
export GALASACTL="${BASEDIR}/bin/${binary}"
${GALASACTL} runs submit local \
--obr mvn:${OBR_GROUP_ID}/${OBR_ARTIFACT_ID}/${OBR_VERSION}/obr \
--remoteMaven ${REMOTE_MAVEN} \
--class ${BUNDLE}/${JAVA_CLASS} \
--throttle 1 \
--requesttype automated-test \
--poll 10 \
--progress 1 \
--log - \
--reportjunit junitReport.xml \
--reportjson jsonReport.json
# Uncomment this if testing that a test that should fail, fails
# --noexitcodeontestfailures \
rc=$?
if [[ "${rc}" != "0" ]]; then
error "Failed to run the test"
exit 1
fi
success "Test ran OK"
}
function run_test_locally_using_galasactl {
# Run the Payee tests.
export TEST_BUNDLE=dev.galasa.example.banking.payee
export TEST_JAVA_CLASS=dev.galasa.example.banking.payee.TestPayee
export TEST_OBR_GROUP_ID=dev.galasa.example.banking
export TEST_OBR_ARTIFACT_ID=dev.galasa.example.banking.obr
export TEST_OBR_VERSION=0.0.1-SNAPSHOT
export totalTests=1
export failedTests=0
export testName="simpleSampleTest"
export testResult="Passed"
submit_local_test $TEST_BUNDLE $TEST_JAVA_CLASS $TEST_OBR_GROUP_ID $TEST_OBR_ARTIFACT_ID $TEST_OBR_VERSION
check_junit_report $totalTests $failedTests
check_json_report $testName $testResult
}
function check_junit_report {
cd ${BASEDIR}/temp/*banking
totalTests=$1
failedTests=$2
junitReportFile="junitReport.xml"
stringToFind="name=\"Galasa test run\" tests=\"${totalTests}\" failures=\"${failedTests}\""
echo "string to find: "${stringToFind}
if ! grep -q "${stringToFind}" ${junitReportFile}; then
error "Junit report not created properly"
exit 1
fi
success "Junit report was created successfully"
}
function check_json_report {
cd ${BASEDIR}/temp/*banking
testName=$1
testResult=$2
jsonReportFile="jsonReport.json"
stringToFind=" \"tests\": \\[
{
\"name\": \"${testName}\",
\"result\": \"${testResult}\"
}
]"
echo "string to find: "${stringToFind}
if ! grep -q "${stringToFind}" ${jsonReportFile}; then
error "Json report not created properly"
exit 1
fi
success "Json report was created successfully"
}
function cleanup_local_maven_repo {
rm -fr ~/.m2/repository/dev/galasa/example
}
calculate_galasactl_executable
# Initialise Galasa home ...
galasa_home_init
# Generate sample project ...
generate_sample_code
cleanup_local_maven_repo
build_generated_source
run_test_locally_using_galasactl
CALLED_BY_MAIN="true"
source ${BASEDIR}/test-scripts/gherkin-runs-tests.sh
test_gherkin_commands