-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproject-dev
More file actions
executable file
·165 lines (151 loc) · 4.97 KB
/
project-dev
File metadata and controls
executable file
·165 lines (151 loc) · 4.97 KB
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
#!/usr/bin/env bash
set -e
SOURCE="${BASH_SOURCE[0]}"
ROOT_DIR="$(dirname "$(realpath ${SOURCE})")"
DIST_FOLDER=${ROOT_DIR}/dist
function displayUsage() {
echo "
Usage: $0 <command>
<command>:
help
Display this help message.
build-jar
Build jar file
code-run
Runs containerized app
load-test
Stress testing for the API
test-coverage-report
Shows unit test coverage reports
prepare-report
Prepares the report to send the developer for debugging
"
}
function checkMaven() {
# Check maven if installed or not. If not install
if ! [ -x "$(command -v mvn --version)" ]; then
echo "Installing maven"
apt install maven
fi
}
function buildJar() {
checkMaven
mvn clean package -DskipTests && info_msg "Jar file built succesfully" || error_msg "Jar file could not build"
}
function error_msg () {
local COLOR_RED="$(tput setaf 9 2>/dev/null || :)"
local COLOR_RESET="$(tput sgr0 2>/dev/null || :)"
echo "${COLOR_RED}ERROR: "$@" ${SOURCE} line ${BASH_LINENO[0]} ${COLOR_RESET}" 1>&2;
exit 1
}
function info_msg () {
local COLOR_GREEN="$(tput setaf 10 2>/dev/null || :)"
local COLOR_RESET="$(tput sgr0 2>/dev/null || :)"
echo "${COLOR_GREEN}INFO: "$@"${COLOR_RESET}" 1>&2;
}
function warn_msg () {
local COLOR_YELLOW="$(tput setaf 3 2>/dev/null || :)"
local COLOR_RESET="$(tput sgr0 2>/dev/null || :)"
echo "${COLOR_YELLOW}WARNING: "$@"${COLOR_RESET}" 1>&2;
}
function codeRun() {
# check if docker installed
if ! [ -x "$(command -v docker --version)" ]; then
error_msg "Docker Desktop is not installed.
Please install Docker Desktop from https://docs.docker.com/get-docker/"
fi
# build app
docker compose up --build
}
function loadTesting() {
# check if locust installed
if ! [ -x "$(command -v locust -V)" ]; then
if ! [ -x "$(command -v pip3 -V)" ]; then
warn_msg "pip3 is not installed. Installing..."
apt install python3-pip
fi
warn_msg "Locust is not installed. Installing..."
pip3 install locust
fi
local HOST=http://127.0.0.1:8080
local CONCURRENT=500
local RATE=500
local DURATION=10
locust -f ${ROOT_DIR}/load-testing/locust.py \
--host ${HOST} \
--headless \
--users=${CONCURRENT} \
--spawn-rate=${RATE} \
--run-time=${DURATION}s \
--reset-stats \
--only-summary
}
function testCoverageReport() {
checkMaven
# mvn generates test coverage in this file by default
local REPORT_FILE=${ROOT_DIR}/target/site/jacoco/index.html
local REPORT_FOLDER=${ROOT_DIR}/target/site/jacoco
local OUT_FILE=${DIST_FOLDER}/unit-test-coverage.tar.gz
local REPORT_SUCCESS_MSG="The report visualized successfully. Generated file: ${REPORT_FILE}"
local REPORT_ERR="Report could not visualized but file generated on: ${REPORT_FILE}"
local TMP_DIR=$(mktemp -d)
cd ${ROOT_DIR}
# run test coverage
mvn clean test jacoco:report
# show reports
if [ -x "$(command -v firefox -V)" ]; then
firefox --new-window ${REPORT_FILE} && info_msg ${REPORT_SUCCESS_MSG} || warn_msg ${REPORT_ERR}
elif [ -x "$(command -v google-chrome --version)" ]; then
google-chrome --new-window ${REPORT_FILE} && info_msg ${REPORT_SUCCESS_MSG} || warn_msg ${REPORT_ERR}
else
warn_msg "The report generated but couldn't visualize. Generated file: ${REPORT_FILE}"
fi
cp -r ${REPORT_FOLDER} ${TMP_DIR}
cd ${TMP_DIR}
# rename file
mv jacoco report
mkdir -p ${DIST_FOLDER}
tar -cvzf ${OUT_FILE} report/
# check if the file exist and has a size
[ -s ${OUT_FILE} ] && info_msg "${OUT_FILE} generated successfully" || error_msg "File could not generated"
rm -rf ${TMP_DIR}
}
function prepareReport() {
local LOG_DIR=/tmp/demo
local TAR_FILENAME=demo-report.tar.gz
local TAR_FILE=${DIST_FOLDER}/${TAR_FILENAME}
mkdir -p ${LOG_DIR}
cd ${LOG_DIR}
tar -cvzf ${TAR_FILENAME} demo.log
mkdir -p ${DIST_FOLDER}
mv ${LOG_DIR}/${TAR_FILENAME} ${DIST_FOLDER}
# check if the file exist and has a size
[ -s ${TAR_FILE} ] && info_msg "The report genereted successfully: ${TAR_FILE}" || error_msg "Report could not generated!"
}
function main() {
case "${1-notset}" in
help)
displayUsage
;;
build-jar)
buildJar "${@}"
;;
code-run)
codeRun "${@}"
;;
load-test)
loadTesting "${@}"
;;
test-coverage-report)
testCoverageReport "${@}"
;;
prepare-report)
prepareReport "${@}"
;;
*)
>&2 echo "Error: Unknown command: '${1-}'"
>&2 displayUsage
exit 1
esac
}
main "$@"