-
Notifications
You must be signed in to change notification settings - Fork 3
/
run-lint-tests.sh
executable file
·548 lines (441 loc) · 14.9 KB
/
run-lint-tests.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#!/usr/bin/env bash
VERSION="2024.6.1"
#SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPT_DIR="$(dirname "$0")"
## PURPOSE RELATED VARS
#PROJECT_DIR=$( git rev-parse --show-toplevel )
PROJECT_DIR="$(cd "${SCRIPT_DIR}" && git rev-parse --show-toplevel)"
KEEP_TMP=0
DISPLAY_TEST_RESULTS=0
TEST_CASE_CONFIGS="
validate_yamllint
validate_kicslint
validate_inclusivity
validate_ansiblelint
"
LOG_ERROR=0
LOG_WARN=1
LOG_INFO=2
LOG_TRACE=3
LOG_DEBUG=4
#LOG_LEVEL=${LOG_DEBUG}
LOG_LEVEL=${LOG_INFO}
function logError() {
if [ $LOG_LEVEL -ge $LOG_ERROR ]; then
echo -e "[ERROR]: ==> ${1}"
fi
}
function logWarn() {
if [ $LOG_LEVEL -ge $LOG_WARN ]; then
echo -e "[WARN ]: ==> ${1}"
fi
}
function logInfo() {
if [ $LOG_LEVEL -ge $LOG_INFO ]; then
echo -e "[INFO ]: ==> ${1}"
fi
}
function logTrace() {
if [ $LOG_LEVEL -ge $LOG_TRACE ]; then
echo -e "[TRACE]: ==> ${1}"
fi
}
function logDebug() {
if [ $LOG_LEVEL -ge $LOG_DEBUG ]; then
echo -e "[DEBUG]: ==> ${1}"
fi
}
function setLogLevel() {
local LOGLEVEL=$1
case "${LOGLEVEL}" in
ERROR*)
LOG_LEVEL=$LOG_ERROR
;;
WARN*)
LOG_LEVEL=$LOG_WARN
;;
INFO*)
LOG_LEVEL=$LOG_INFO
;;
TRACE*)
LOG_LEVEL=$LOG_TRACE
;;
DEBUG*)
LOG_LEVEL=$LOG_DEBUG
DISPLAY_TEST_RESULTS=1
;;
*)
abort "Unknown loglevel of [${LOGLEVEL}] specified"
esac
}
function error() {
printf "%s\n" "$@" >&2
# echo "$@" 1>&2;
}
function fail() {
error "$@"
exit 1
}
function cleanup_tmpdir() {
test "${KEEP_TMP:-0}" = 1 || rm -rf "${TMP_DIR}"
}
## ref: https://stackoverflow.com/questions/229551/how-to-check-if-a-string-contains-a-substring-in-bash#229585
function stringContain() { case $2 in *$1* ) return 0;; *) return 1;; esac ;}
function validate_ansiblelint() {
local LOG_PREFIX="validate_ansiblelint():"
logInfo ""
logInfo "#######################################################"
eval "ansible-lint --version"
logInfo "${LOG_PREFIX} Validate ansible-lint"
local TEST_COMMAND="ansible-lint -p"
eval "${TEST_COMMAND} > /dev/null 2>&1"
local RETURN_STATUS=$?
if [[ $RETURN_STATUS -eq 0 ]]; then
logInfo "${LOG_PREFIX} SUCCESS => No exceptions found from [${TEST_COMMAND}]!!"
else
logInfo "${LOG_PREFIX} There are [${RETURN_STATUS}] exceptions found from [${TEST_COMMAND}]!! :("
logInfo "${LOG_PREFIX} ${TEST_COMMAND}"
eval "${TEST_COMMAND}"
fi
logInfo "#######################################################"
logInfo "${LOG_PREFIX} RETURN_STATUS=${RETURN_STATUS}"
logInfo ""
return "${RETURN_STATUS}"
}
function validate_yamllint() {
local LOG_PREFIX="validate_yamllint():"
logInfo ""
logInfo "#######################################################"
eval "yamllint --version"
logInfo "${LOG_PREFIX} Validate yamllint"
local TEST_COMMAND="yamllint --no-warnings ."
eval "${TEST_COMMAND} > /dev/null 2>&1"
local RETURN_STATUS=$?
if [[ $RETURN_STATUS -eq 0 ]]; then
logInfo "${LOG_PREFIX} SUCCESS => No exceptions found from [${TEST_COMMAND}]!!"
else
logInfo "${LOG_PREFIX} There are [${RETURN_STATUS}] exceptions found from [${TEST_COMMAND}]!! :("
logInfo "${LOG_PREFIX} ${TEST_COMMAND}"
eval "${TEST_COMMAND}"
fi
logInfo "#######################################################"
logInfo "${LOG_PREFIX} RETURN_STATUS=${RETURN_STATUS}"
logInfo ""
return "${RETURN_STATUS}"
}
function validate_kicslint() {
local LOG_PREFIX="validate_kicslint():"
local KICS_CONFIG_FILE=".kics-config.yml"
## ref: https://docs.kics.io/latest/documentation/#installation
## ref: https://formulae.brew.sh/formula/kics
#export KICS_QUERIES_PATH=/usr/local/opt/kics/share/kics/assets/queries
export KICS_QUERIES_PATH=${HOMEBREW_PREFIX:-/usr/local}/opt/kics/share/kics/assets/queries
logInfo ""
logInfo "#######################################################"
eval "kics version"
logInfo "${LOG_PREFIX} Validate kics lint"
local TEST_COMMAND="kics scan --ci --config ${KICS_CONFIG_FILE}"
eval "${TEST_COMMAND} > /dev/null 2>&1"
local RETURN_STATUS=$?
if [[ $RETURN_STATUS -eq 0 ]]; then
logInfo "${LOG_PREFIX} SUCCESS => No exceptions found from [${TEST_COMMAND}]!!"
else
logInfo "${LOG_PREFIX} There are [${RETURN_STATUS}] exceptions found from [${TEST_COMMAND}]!! :("
logInfo "${LOG_PREFIX} ${TEST_COMMAND}"
eval "${TEST_COMMAND}"
fi
logInfo "#######################################################"
logInfo "${LOG_PREFIX} RETURN_STATUS=${RETURN_STATUS}"
logInfo ""
return "${RETURN_STATUS}"
}
function validate_inclusivity() {
local LOG_PREFIX="validate_inclusivity():"
logInfo ""
logInfo "#######################################################"
eval "woke version"
logInfo "${LOG_PREFIX} Validate inclusive language lint"
local TEST_COMMAND="woke -c .inclusivity.yml --exit-1-on-failure ."
eval "${TEST_COMMAND} > /dev/null 2>&1"
local RETURN_STATUS=$?
if [[ $RETURN_STATUS -eq 0 ]]; then
logInfo "${LOG_PREFIX} SUCCESS => No exceptions found from [${TEST_COMMAND}]!!"
else
logInfo "${LOG_PREFIX} There are [${RETURN_STATUS}] exceptions found from [${TEST_COMMAND}]!! :("
logInfo "${LOG_PREFIX} ${TEST_COMMAND}"
eval "${TEST_COMMAND}"
fi
logInfo "#######################################################"
logInfo "${LOG_PREFIX} RETURN_STATUS=${RETURN_STATUS}"
logInfo ""
return "${RETURN_STATUS}"
}
function run_test_case() {
local TEST_CASE_ID=$1
local TEST_FUNCTION=$2
local TEST_CASES_STR=$3
shift 3
local RETURN_STATUS=0
local TEST_ARGS=()
if [ $# -gt 0 ]; then
TEST_ARGS=${@}
fi
local LOG_PREFIX="run_test_case(${TEST_CASE_ID}):"
if stringContain "${TEST_CASE_ID}" "${TEST_CASES_STR}" ||
stringContain "${TEST_FUNCTION}" "${TEST_CASES_STR}" ||
[ "${TEST_CASES_STR}" == "ALL" ]
then
logDebug "${LOG_PREFIX} Run test [${TEST_FUNCTION}]"
local TEST_COMMAND="${TEST_FUNCTION}"
logDebug "${LOG_PREFIX} TEST_COMMAND=${TEST_COMMAND}"
# local TEST_RESULTS=$(${TEST_FUNCTION} "${TEST_ARGS}")
# local TEST_RESULTS=$(eval "${TEST_FUNCTION} ${TEST_ARGS}")
eval "${TEST_FUNCTION} ${TEST_ARGS} >/dev/null 2>&1"
local RETURN_STATUS=$?
if [[ $RETURN_STATUS -eq 0 ]]; then
logInfo "${LOG_PREFIX} ${TEST_FUNCTION}: SUCCESS"
else
logError "${LOG_PREFIX} ${TEST_FUNCTION}: FAILED"
fi
if [[ $RETURN_STATUS -ne 0 || $DISPLAY_TEST_RESULTS -gt 0 ]]; then
logInfo "${LOG_PREFIX} TEST_RESULTS ****"
eval "${TEST_FUNCTION} ${TEST_ARGS}"
fi
logDebug "${LOG_PREFIX} RETURN_STATUS=${RETURN_STATUS}"
fi
logDebug "${LOG_PREFIX} TEST_CASE_IDX=${TEST_CASE_IDX_PADDED} TEST_CASE=${TEST_CASE} RETURN_STATUS=${RETURN_STATUS} ERROR_COUNT=${ERROR_COUNT}"
return "${RETURN_STATUS}"
}
function print_test_cases() {
local LOG_PREFIX="print_test_cases():"
TEST_CASE_IDX=0
IFS=$'\n'
for TEST_CASE_INFO in ${TEST_CASE_CONFIGS}
do
## ref: https://stackoverflow.com/questions/6723426/looping-over-arrays-printing-both-index-and-value
((TEST_CASE_IDX++))
## ref: https://stackoverflow.com/questions/1117134/padding-zeros-in-a-string
TEST_CASE_IDX_PADDED=$(printf %02d $TEST_CASE_IDX)
# ref: https://stackoverflow.com/questions/12317483/array-of-arrays-in-bash
# split server name from sub-list
# TEST_CASE_ARRAY=(${TEST_CASE_INFO//:/})
IFS="," read -a TEST_CASE_ARRAY <<< $TEST_CASE_INFO
TEST_CASE=${TEST_CASE_ARRAY[0]}
# echo "${TEST_CASE_IDX_PADDED}: ${TEST_CASE}"
echo "${TEST_CASE}"
done
}
function run_tests() {
local LOG_PREFIX="run_tests():"
local ERROR_COUNT=0
# local TEST_CASES=$@
local TEST_CASES=("$@")
local RETURN_STATUS=0
logInfo "${LOG_PREFIX} TEST_CASES[@]=${TEST_CASES[@]}"
TEST_CASES_STR="${TEST_CASES[@]}"
TEST_CASE_IDX=0
IFS=$'\n'
for TEST_CASE_INFO in ${TEST_CASE_CONFIGS}
do
## ref: https://stackoverflow.com/questions/6723426/looping-over-arrays-printing-both-index-and-value
((TEST_CASE_IDX++))
## ref: https://stackoverflow.com/questions/1117134/padding-zeros-in-a-string
TEST_CASE_IDX_PADDED=$(printf %02d $TEST_CASE_IDX)
# ref: https://stackoverflow.com/questions/12317483/array-of-arrays-in-bash
# split server name from sub-list
IFS="," read -a TEST_CASE_ARRAY <<< $TEST_CASE_INFO
TEST_CASE=${TEST_CASE_ARRAY[0]}
logDebug "${LOG_PREFIX} TEST_CASE_IDX=${TEST_CASE_IDX_PADDED} TEST_CASE=${TEST_CASE}"
local TEST_CASE_ARGS=()
if [[ ${#TEST_CASE_ARRAY[@]} -gt 1 ]]; then
## ref: https://unix.stackexchange.com/questions/413976/how-to-shift-array-value-in-bash#413990
TEST_CASE_ARGS=${TEST_CASE_ARRAY[@]:1}
logDebug "${LOG_PREFIX} TEST_CASE_ARGS=${TEST_CASE_ARGS[@]}"
fi
run_test_case "${TEST_CASE_IDX_PADDED}" "${TEST_CASE}" "${TEST_CASES_STR}" "${TEST_CASE_ARGS[@]}"
RETURN_STATUS=$?
ERROR_COUNT=$((${ERROR_COUNT} + ${RETURN_STATUS}))
logDebug "${LOG_PREFIX} TEST_CASE_IDX=${TEST_CASE_IDX_PADDED} TEST_CASE=${TEST_CASE} RETURN_STATUS=${RETURN_STATUS} ERROR_COUNT=${ERROR_COUNT}"
done
logInfo "${LOG_PREFIX} ERROR_COUNT=${ERROR_COUNT}"
return ${ERROR_COUNT}
}
function checkRequiredCommands() {
missingCommands=""
for currentCommand in "$@"
do
isInstalled "${currentCommand}" || missingCommands="${missingCommands} ${currentCommand}"
done
if [[ ! -z "${missingCommands}" ]]; then
fail "checkRequiredCommands(): Please install the following commands required by this script:${missingCommands}"
fi
}
function isInstalled() {
command -v "${1}" >/dev/null 2>&1 || return 1
}
function install_jq() {
local LOG_PREFIX="install_jq():"
local OS="${1}"
local PACKAGE_NAME="jq"
echo "==> installing jq - required for yq sort-keys (https://mikefarah.gitbook.io/yq/operators/sort-keys)"
if [[ -n "${INSTALL_ON_LINUX-}" ]]; then
logInfo "${LOG_PREFIX} Installing jq for linux"
if [[ -n "$(command -v dnf)" ]]; then
sudo dnf install -y "${PACKAGE_NAME}"
elif [[ -n "$(command -v yum)" ]]; then
sudo yum install -y "${PACKAGE_NAME}"
elif [[ -n "$(command -v apt-get)" ]]; then
sudo apt-get install -y "${PACKAGE_NAME}"
fi
fi
if [[ -n "${INSTALL_ON_MACOS-}" ]]; then
logInfo "${LOG_PREFIX} Installing jq for MacOS"
brew install "${PACKAGE_NAME}"
fi
if [[ -n "${INSTALL_ON_MSYS-}" ]]; then
PACKAGE_NAME="mingw-w64-x86_64-jq"
logInfo "${LOG_PREFIX} Installing jq for MSYS2"
## ref: https://packages.msys2.org/package/mingw-w64-x86_64-jq?repo=mingw64
pacman --noconfirm -S "${PACKAGE_NAME}"
fi
}
# ref: https://formulae.brew.sh/formula/kics
function install_kics() {
local LOG_PREFIX="install_kics():"
local OS="${1}"
logInfo "${LOG_PREFIX} Installing kics (https://formulae.brew.sh/formula/kics)"
if [[ -n "${INSTALL_ON_MACOS-}" ]]; then
## ref: https://formulae.brew.sh/formula/kics
logInfo "${LOG_PREFIX} Installing kics for MacOS using brew"
brew install kics
else
logInfo "${LOG_PREFIX} Kics install not supported on platform ${OS}"
fi
}
## ref: https://github.com/get-woke/woke/#install-woke
## ref: https://docs.getwoke.tech/installation/
function install_woke() {
local LOG_PREFIX="install_woke():"
local OS="${1}"
logInfo "${LOG_PREFIX} Installing woke (https://docs.getwoke.tech/installation/)"
if [[ -n "${INSTALL_ON_MACOS-}" ]]; then
## ref: https://docs.getwoke.tech/installation/
logInfo "${LOG_PREFIX} Installing woke for MacOS using brew"
brew install get-woke/tap/woke
else
logInfo "${LOG_PREFIX} woke install not supported on platform ${OS}"
fi
}
function install_ansiblelint() {
local LOG_PREFIX="install_ansiblelint():"
PIP_COMMAND="pip install ansible-lint"
logDebug "${LOG_PREFIX} ${PIP_COMMAND}"
eval "${PIP_COMMAND} >/dev/null 2>&1"
logDebug "${LOG_PREFIX} pip install complete"
}
function install_yamllint() {
local LOG_PREFIX="install_yamllint():"
PIP_COMMAND="pip install yamllint"
logDebug "${LOG_PREFIX} ${PIP_COMMAND}"
eval "${PIP_COMMAND} >/dev/null 2>&1"
logDebug "${LOG_PREFIX} pip install complete"
}
function ensure_tool() {
local LOG_PREFIX="ensure_tool():"
if [[ $# -ne 1 ]]
then
return 1
fi
local executable="${1}"
local install_function="install_${executable}"
if [[ -z "$(command -v "${executable}")" ]]; then
logDebug "${LOG_PREFIX} executable '${executable}' not found"
# First check OS.
PLATFORM_OS=$(uname -s | tr "[:upper:]" "[:lower:]")
case "${PLATFORM_OS}" in
linux*)
INSTALL_ON_LINUX=1
;;
darwin*)
INSTALL_ON_MACOS=1
;;
cygwin* | mingw64* | mingw32* | msys*)
INSTALL_ON_MSYS=1
;;
*)
abort "Package installation method is only supported on macOS, Linux and msys2."
;;
esac
logDebug "${LOG_PREFIX} installing executable '${executable}'"
eval "${install_function} ${PLATFORM_OS}"
fi
}
function usage() {
echo "Usage: ${0} [options] [[TESTCASE_ID] [TESTCASE_ID] ...]"
echo ""
echo " Options:"
echo " -L [ERROR|WARN|INFO|TRACE|DEBUG] : run with specified log level (default INFO)"
echo " -d : display test results details"
echo " -l : show/list test cases"
echo " -v : show script version"
echo " -k : keep temp directory/files"
echo " -h : help"
echo ""
echo " Examples:"
echo " ${0} "
echo " ${0} -l"
echo " ${0} validate_ansiblelint"
echo " ${0} validate_yamllint"
echo " ${0} -k -L DEBUG validate_ansiblelint"
echo " ${0} -L DEBUG"
echo " ${0} -v"
[ -z "$1" ] || exit "$1"
}
function main() {
while getopts "L:r:dlpvhk" opt; do
case "${opt}" in
L) setLogLevel "${OPTARG}" ;;
l) print_test_cases && exit ;;
r) PYTEST_JUNIT_REPORT="${OPTARG}" ;;
d) DISPLAY_TEST_RESULTS=1 ;;
k) KEEP_TMP=1 ;;
v) echo "${VERSION}" && exit ;;
h) usage 1 ;;
\?) usage 2 ;;
*) usage ;;
esac
done
shift $((OPTIND-1))
## ref: https://pypi.org/project/ansible-lint/
logDebug "Ensure ansible-lint present/installed"
ensure_tool ansiblelint
## ref: https://pypi.org/project/yamllint/
logDebug "Ensure yamllint present/installed"
ensure_tool yamllint
## ref: https://docs.kics.io/latest/documentation/
logDebug "Ensure kics present/installed"
ensure_tool kics
## ref: https://github.com/get-woke/woke/
logDebug "Ensure woke present/installed"
ensure_tool woke
checkRequiredCommands ansible-inventory ansible-lint yamllint kics woke
TEST_CASES=("ALL")
if [ $# -gt 0 ]; then
TEST_CASES=("$@")
fi
#logInfo "SCRIPT_DIR=${SCRIPT_DIR}"
logInfo "PROJECT_DIR=${PROJECT_DIR}"
logInfo "TEST_CASES=${TEST_CASES[@]}"
run_tests "${TEST_CASES[@]}"
TOTAL_FAILED=$?
logInfo "*********************** "
logInfo "OVERALL INVENTORY TEST RESULTS"
logInfo "TOTAL TOTAL_FAILED=${TOTAL_FAILED}"
if [[ $TOTAL_FAILED -eq 0 ]]; then
logInfo "TEST SUCCEEDED!"
else
logError "TEST FAILED!"
fi
exit $TOTAL_FAILED
}
main "$@"