This repository was archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathrun-unit-tests.sh
executable file
·183 lines (162 loc) · 6.36 KB
/
run-unit-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
#!/bin/bash
#
# This assumes all of the OS-level configuration has been completed and git repo has already been cloned
#
# This script should be run from the repo's deployment directory
# cd deployment
# ./run-unit-tests.sh
#
# This script runs all tests for the root CDK project, as well as any microservices, Lambda functions, or dependency
# source code packages. These include unit tests, integration tests, and snapshot tests.
#
# This script is called by the ../initialize-repo.sh file and the buildspec.yml file. It is important that this script
# be tested and validated to ensure that all available test fixtures are run.
#
# The if/then blocks are for error handling. They will cause the script to stop executing if an error is thrown from the
# node process running the test case(s). Removing them or not using them for additional calls with result in the
# script continuing to execute despite an error being thrown.
[ "$DEBUG" == 'true' ] && set -x
set -e
# Get reference for all important folders
template_dir="$PWD"
source_dir="$(cd $template_dir/../source; pwd -P)"
echo "Current directory: $template_dir"
echo "Source directory: $source_dir"
if [[ -e './solution_env.sh' ]]; then
chmod +x ./solution_env.sh
source ./solution_env.sh
else
echo "solution_env.sh is missing from the solution root."
exit 1
fi
if [[ -z "$SOLUTION_ID" ]]; then
echo "SOLUTION_ID is missing from ../solution_env.sh"
exit 1
else
export SOLUTION_ID
fi
if [[ -z "$SOLUTION_NAME" ]]; then
echo "SOLUTION_NAME is missing from ../solution_env.sh"
exit 1
else
export SOLUTION_NAME
fi
if [[ -z "$SOLUTION_TRADEMARKEDNAME" ]]; then
echo "SOLUTION_TRADEMARKEDNAME is missing from ../solution_env.sh"
exit 1
else
export SOLUTION_TRADEMARKEDNAME
fi
setup_python_env() {
if [ -d "./.venv-test" ]; then
echo "Reusing already setup python venv in ./.venv-test. Delete ./.venv-test if you want a fresh one created."
return
fi
echo "Setting up python venv"
python3 -m venv .venv-test
echo "Initiating virtual environment"
source .venv-test/bin/activate
echo "Installing python packages"
pip3 install -U pip setuptools
pip3 install -r requirements.txt --target .
pip3 install -r requirements-dev.txt
echo "deactivate virtual environment"
deactivate
}
run_python_lambda_test() {
lambda_name=$1
lambda_description=$2
echo "------------------------------------------------------------------------------"
echo "[Test] Python Lambda: $lambda_name, $lambda_description"
echo "------------------------------------------------------------------------------"
cd $source_dir/lambda/$lambda_name
[ "${CLEAN:-true}" = "true" ] && rm -fr .venv-test
setup_python_env
echo "Initiating virtual environment"
source .venv-test/bin/activate
# setup coverage report path
mkdir -p $source_dir/test/coverage-reports
coverage_report_path=$source_dir/test/coverage-reports/$lambda_name.coverage.xml
echo "coverage report path set to $coverage_report_path"
# Use -vv for debugging
python3 -m pytest --cov --cov-report=term-missing --cov-report "xml:$coverage_report_path"
if [ "$?" = "1" ]; then
echo "(source/run-all-tests.sh) ERROR: there is likely output above." 1>&2
exit 1
fi
sed -i -e "s,<source>$source_dir,<source>source,g" $coverage_report_path
echo "deactivate virtual environment"
deactivate
if [ "${CLEAN:-true}" = "true" ]; then
rm -fr .venv-test
# Note: leaving $source_dir/test/coverage-reports to allow further processing of coverage reports
rm -fr coverage
rm .coverage
fi
}
run_javascript_lambda_test() {
lambda_name=$1
lambda_description=$2
echo "------------------------------------------------------------------------------"
echo "[Test] Javascript Lambda: $lambda_name, $lambda_description"
echo "------------------------------------------------------------------------------"
cd $source_dir/lambda/$lambda_name
[ "${CLEAN:-true}" = "true" ] && npm run clean
npm install
# npm ci
npm test
if [ "$?" = "1" ]; then
echo "(source/run-all-tests.sh) ERROR: there is likely output above." 1>&2
exit 1
fi
[ "${CLEAN:-true}" = "true" ] && rm -rf coverage/lcov-report
mkdir -p $source_dir/test/coverage-reports/jest
coverage_report_path=$source_dir/test/coverage-reports/jest/$lambda_name
rm -rf $coverage_report_path
mv coverage $coverage_report_path
}
run_cdk_project_test() {
component_description=$1
component_name=cdk
echo "------------------------------------------------------------------------------"
echo "[Test] $component_description"
echo "------------------------------------------------------------------------------"
cd $source_dir
[ "${CLEAN:-true}" = "true" ] && npm run clean
# npm ci
npm install
npm run build
npm run test -- -u
if [ "$?" = "1" ]; then
echo "(source/run-all-tests.sh) ERROR: there is likely output above." 1>&2
exit 1
fi
[ "${CLEAN:-true}" = "true" ] && rm -rf coverage/lcov-report
mkdir -p $source_dir/test/coverage-reports/jest
coverage_report_path=$source_dir/test/coverage-reports/jest/$component_name
rm -rf $coverage_report_path
echo "coverage_report_path: $coverage_report_path"
mv coverage $coverage_report_path
}
# Option to clean or not clean the unit test environment before and after running tests.
# The environment variable CLEAN has default of 'true' and can be overwritten by caller
# by setting it to 'false'. Particularly,
# $ CLEAN=false ./run-all-tests.sh
#
CLEAN="${CLEAN:-true}"
# CLEAN="${CLEAN:-false}"
# echo "------------------------------------------------------------------------------"
# echo "[Test] CDK Unit Tests"
# echo "------------------------------------------------------------------------------"
run_cdk_project_test "CDK - DevOps Monitoring Dashboard on AWS"
# echo "------------------------------------------------------------------------------"
# echo "[Test] Lambda Tests"
# echo "------------------------------------------------------------------------------"
run_python_lambda_test quicksight-custom-resources "Quicksight - Custom Resources"
run_python_lambda_test solution_helper "Solution Helper Lambda"
run_javascript_lambda_test event_parser "Lambda transformation of Source Data"
run_javascript_lambda_test query_runner "Build Athena Queries"
run_javascript_lambda_test multi_account_custom_resources "Multiple Account Custom Resources"
run_javascript_lambda_test tag_query "Tag Query Lambda"
# Return to the directory where we started
cd $template_dir