forked from bbednarek/mockserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.sh
80 lines (69 loc) · 1.86 KB
/
logging.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
#!/usr/bin/env bash
set -euo pipefail
function printMessageWithColourAndBorders() {
COLOUR="${2}"
echo
printf -v str "%-$((${#1}))s" ' '
if [[ "${#str}" -gt "225" ]]; then
printf "${COLOUR}%s\e[0m\n" "$(echo $1)"
else
printf "${COLOUR}%s\e[0m\n" "${str// /=}"
printf "${COLOUR}%s\e[0m\n" "$(echo $1)"
printf "${COLOUR}%s\e[0m\n" "${str// /=}"
fi
echo
}
function printMessageWithColour() {
COLOUR="${2}"
printf "${COLOUR}%s\e[0m\n" "${1}"
}
function printMessage() {
printMessageWithColourAndBorders >&2 "${1}" "\e[0;33m"
}
function printPassMessage() {
printMessageWithColourAndBorders >&2 "${1}" "\e[0;32m"
}
function printPlainPassMessage() {
printMessageWithColour >&2 "${1}" "\e[0;32m"
}
function printFailureMessage() {
printMessageWithColourAndBorders >&2 "${1}" "\e[0;31m"
}
function printPlainFailureMessage() {
printMessageWithColour >&2 "${1}" "\e[0;31m"
}
function runCommand() {
printMessageWithColourAndBorders >&2 "$1" "\e[0;33m"
if ! eval "$(echo $1)"; then
return 1
fi
}
function retryCommand() {
n=0
until [ "$n" -ge "${2:-3}" ] || runCommand "${1}"; do
n=$((n + 1))
printMessage "${n} of ${2:-3} retries for command: ${1}"
printMessage "sleeping ${3:-30}s before retry"
sleep "${3:-30}"
done
if [ "$n" -ge "${2:-3}" ]; then
printMessage "The command has failed after $n attempts."
return 1
fi
}
function logTestResult() {
TEST_EXIT_CODE="${1}"
TEST_CASE="${2}"
if [[ "${TEST_EXIT_CODE}" != "0" ]]; then
printFailureMessage "Failed: ${TEST_CASE}"
if [ -n "${3:-}" ]; then
container-logs "${3:-}"
fi
container-logs
printFailureMessage "Failed: ${TEST_CASE}"
printPlainFailureMessage " - ${TEST_CASE}" >>${FAIL_LOG_FILE} 2>&1
else
printPassMessage "Passed: ${TEST_CASE}"
printPlainPassMessage " - ${TEST_CASE}" >>${PASS_LOG_FILE} 2>&1
fi
}