2 # ============LICENSE_START====================================================
3 # Copyright (C) 2022 Nordix Foundation.
4 # =============================================================================
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # SPDX-License-Identifier: Apache-2.0
18 # ============LICENSE_END======================================================
19 # shellcheck disable=SC2039
20 # shellcheck disable=SC2086
21 # shellcheck disable=SC2012
22 echo "Start of test $(date +%F-%T)"
24 export POLICY_HOME=/opt/app/policy
25 export SQL_USER=policy_user
26 export SQL_PASSWORD=policy_user
27 export SQL_HOST=mariadb
28 export MYSQL_ROOT_PASSWORD=secret
29 export SCHEMA=policyadmin
30 export SCRIPT_DIRECTORY=sql
34 H_UPGRADE_COUNT=$(ls /home/policy/sql/0800/upgrade/*.sql | wc -l)
35 H_DOWNGRADE_COUNT=$(ls /home/policy/sql/0800/downgrade/*.sql | wc -l)
36 TOTAL_SQLS_UPGRADE=$H_UPGRADE_COUNT
37 TOTAL_SQLS_DOWNGRADE=$H_DOWNGRADE_COUNT
39 I_UPGRADE_COUNT=$(ls /home/policy/sql/0900/upgrade/*.sql | wc -l)
40 I_DOWNGRADE_COUNT=$(ls /home/policy/sql/0900/downgrade/*.sql | wc -l)
41 TOTAL_SQLS_UPGRADE=$(($TOTAL_SQLS_UPGRADE+$I_UPGRADE_COUNT))
42 TOTAL_SQLS_DOWNGRADE=$(($TOTAL_SQLS_DOWNGRADE+$I_DOWNGRADE_COUNT))
44 J_UPGRADE_COUNT=$(ls /home/policy/sql/1000/upgrade/*.sql | wc -l)
45 J_DOWNGRADE_COUNT=$(ls /home/policy/sql/1000/downgrade/*.sql | wc -l)
46 TOTAL_SQLS_UPGRADE=$(($TOTAL_SQLS_UPGRADE+$J_UPGRADE_COUNT))
47 TOTAL_SQLS_DOWNGRADE=$(($TOTAL_SQLS_DOWNGRADE+$J_DOWNGRADE_COUNT))
49 K_UPGRADE_COUNT=$(ls /home/policy/sql/1100/upgrade/*.sql | wc -l)
50 K_DOWNGRADE_COUNT=$(ls /home/policy/sql/1100/downgrade/*.sql | wc -l)
51 TOTAL_SQLS_UPGRADE=$(($TOTAL_SQLS_UPGRADE+$K_UPGRADE_COUNT))
52 TOTAL_SQLS_DOWNGRADE=$(($TOTAL_SQLS_DOWNGRADE+$K_DOWNGRADE_COUNT))
54 L_UPGRADE_COUNT=$(ls /home/policy/sql/1200/upgrade/*.sql | wc -l)
55 L_DOWNGRADE_COUNT=$(ls /home/policy/sql/1200/downgrade/*.sql | wc -l)
56 TOTAL_SQLS_UPGRADE=$(($TOTAL_SQLS_UPGRADE+$L_UPGRADE_COUNT))
57 TOTAL_SQLS_DOWNGRADE=$(($TOTAL_SQLS_DOWNGRADE+$L_DOWNGRADE_COUNT))
59 M_UPGRADE_COUNT=$(ls /home/policy/sql/1300/upgrade/*.sql | wc -l)
60 M_DOWNGRADE_COUNT=$(ls /home/policy/sql/1300/downgrade/*.sql | wc -l)
61 TOTAL_SQLS_UPGRADE=$(($TOTAL_SQLS_UPGRADE+$M_UPGRADE_COUNT))
62 TOTAL_SQLS_DOWNGRADE=$(($TOTAL_SQLS_DOWNGRADE+$M_DOWNGRADE_COUNT))
66 PREVIOUS_SQL_EXECUTIONS=0
68 RECENT_SQL_EXECUTIONS=0
77 PDPSTATISTICS="CREATE TABLE IF NOT EXISTS pdpstatistics ("
78 PDPSTATISTICS=${PDPSTATISTICS}"PDPGROUPNAME VARCHAR(120) NULL, "
79 PDPSTATISTICS=${PDPSTATISTICS}"PDPSUBGROUPNAME VARCHAR(120) NULL, "
80 PDPSTATISTICS=${PDPSTATISTICS}"POLICYDEPLOYCOUNT BIGINT DEFAULT NULL NULL, "
81 PDPSTATISTICS=${PDPSTATISTICS}"POLICYDEPLOYFAILCOUNT BIGINT DEFAULT NULL NULL, "
82 PDPSTATISTICS=${PDPSTATISTICS}"POLICYDEPLOYSUCCESSCOUNT BIGINT DEFAULT NULL NULL, "
83 PDPSTATISTICS=${PDPSTATISTICS}"POLICYEXECUTEDCOUNT BIGINT DEFAULT NULL NULL, "
84 PDPSTATISTICS=${PDPSTATISTICS}"POLICYEXECUTEDFAILCOUNT BIGINT DEFAULT NULL NULL, "
85 PDPSTATISTICS=${PDPSTATISTICS}"POLICYEXECUTEDSUCCESSCOUNT BIGINT DEFAULT NULL NULL, "
86 PDPSTATISTICS2=${PDPSTATISTICS}"POLICYUNDEPLOYCOUNT BIGINT DEFAULT NULL NULL, "
87 PDPSTATISTICS2=${PDPSTATISTICS2}"POLICYUNDEPLOYFAILCOUNT BIGINT DEFAULT NULL NULL, "
88 PDPSTATISTICS2=${PDPSTATISTICS2}"POLICYUNDEPLOYSUCCESSCOUNT BIGINT DEFAULT NULL NULL, "
89 PDPSTATISTICS=${PDPSTATISTICS}"timeStamp datetime NOT NULL, "
90 PDPSTATISTICS2=${PDPSTATISTICS2}"timeStamp datetime DEFAULT NULL NULL, "
91 PDPSTATISTICS2=${PDPSTATISTICS2}"ID BIGINT NOT NULL, "
92 PDPSTATISTICS=${PDPSTATISTICS}"name VARCHAR(120) NOT NULL, "
93 PDPSTATISTICS2=${PDPSTATISTICS2}"name VARCHAR(120) NOT NULL, "
94 PDPSTATISTICS=${PDPSTATISTICS}"version VARCHAR(20) NOT NULL,"
95 PDPSTATISTICS2=${PDPSTATISTICS2}"version VARCHAR(20) NOT NULL,"
96 PDPSTATISTICS=${PDPSTATISTICS}"CONSTRAINT PK_PDPSTATISTICS PRIMARY KEY (timeStamp, name, version));"
97 PDPSTATISTICS2=${PDPSTATISTICS2}"CONSTRAINT PK_PDPSTATISTICS PRIMARY KEY (ID, name, version));"
99 IDX_TSIDX1="CREATE INDEX IDX_TSIDX1 ON pdpstatistics(timeStamp, name, version);"
100 INSERT="INSERT INTO pdpstatistics(PDPGROUPNAME,PDPSUBGROUPNAME,POLICYDEPLOYCOUNT,POLICYDEPLOYFAILCOUNT,POLICYDEPLOYSUCCESSCOUNT,"
101 INSERT1="${INSERT}""POLICYEXECUTEDCOUNT,POLICYEXECUTEDFAILCOUNT,POLICYEXECUTEDSUCCESSCOUNT,timeStamp,name,version)"
102 INSERT2="${INSERT}""POLICYEXECUTEDCOUNT,POLICYEXECUTEDFAILCOUNT,POLICYEXECUTEDSUCCESSCOUNT,POLICYUNDEPLOYCOUNT,"
103 INSERT2="${INSERT2}""POLICYUNDEPLOYFAILCOUNT,POLICYUNDEPLOYSUCCESSCOUNT,timeStamp,ID,name,version)"
104 SQL1="${INSERT1}"" values('groupname', 'subgroup',1,1,1,1,1,1,now(),'test1', '1.0')"
105 SQL2="${INSERT1}"" values('groupname', 'subgroup',1,1,1,1,1,1,now(),'test1', '1.0')"
106 SQL3="${INSERT2}"" values('groupname', 'subgroup',1,1,1,1,1,1,1,1,1,now(),3,'test1', '1.0')"
107 SQL4="${INSERT2}"" values('groupname', 'subgroup',1,1,1,1,1,1,1,1,1,now(),4,'test1', '1.0')"
108 SQL5="${INSERT1}"" values('groupname', 'subgroup',1,1,1,1,1,1,now(),'test2', '1.0')"
109 SQL6="${INSERT1}"" values('groupname', 'subgroup',1,1,1,1,1,1,now(),'test2', '1.0')"
112 local user="${1}" password="${2}" schema="${3}" sql="${4}"
113 MYSQL="mysql -u${user} -p${password} -h ${SQL_HOST} ${schema}"
114 ${MYSQL} --execute "${sql}"
120 echo "############################################################################################################"
123 reportStatus=$(/opt/app/policy/bin/db-migrator -s ${SQL_DB} -o report | tail -2)
124 echo "Starting test $TESTS"
126 START_VERSION=$(echo $reportStatus | cut -f13 -d' ')
127 PREVIOUS_SQL_EXECUTIONS=$(echo $reportStatus | cut -f1 -d' ')
129 if [ "${START_VERSION}" == "" ]; then
131 PREVIOUS_SQL_EXECUTIONS=0
136 reportStatus=$(/opt/app/policy/bin/db-migrator -s ${SQL_DB} -o report | tail -2)
137 echo "Ending test $TESTS"
139 END_VERSION=$(echo $reportStatus | cut -f13 -d' ')
140 RECENT_SQL_EXECUTIONS=$(echo $reportStatus | cut -f1 -d' ')
141 END_STATUS=$(echo $reportStatus | cut -f7 -d' ')
145 local status="${1}" operation="${2}" startVer="${3}" endVer="${4}" newRecords="${5}" filesRan="${6}"
148 echo "Test summary: status:$status, operation:$operation, from:$startVer, to:$endVer, new executions:$newRecords, sqlFiles:$filesRan"
150 startVer=$(echo ${startVer} | awk '{$0=int($0)}1')
151 endVer=$(echo ${endVer} | awk '{$0=int($0)}1')
153 if [ ${startVer} -eq ${endVer} ] && [ $newRecords -eq $filesRan ] && [ $newRecords -eq 0 ]; then
154 TEST_MSG="No ${operation} required"
157 elif [ $status -eq 1 ] && [ "${operation}" == "upgrade" ] && [ ${startVer} -le ${endVer} ] && [ $newRecords -eq $filesRan ]; then
158 TEST_MSG="Successful ${operation}"
161 elif [ $status -eq 1 ] && [ "${operation}" == "downgrade" ] && [ ${startVer} -ge ${endVer} ] && [ $newRecords -eq $filesRan ]; then
162 TEST_MSG="Successful ${operation}"
166 TEST_MSG="Errors occurred during ${operation}"
171 echo "*** Test $TESTS: $TEST_STATUS , $TEST_MSG, current version: $END_VERSION ***"
175 # Test 1 - Upgrade to Istanbul
176 /opt/app/policy/bin/prepare_upgrade.sh ${SQL_DB}
178 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o upgrade -t 0900
180 let TOTAL_COUNT=$H_UPGRADE_COUNT+$I_UPGRADE_COUNT
181 check_results $END_STATUS 'upgrade' "${START_VERSION}" "${END_VERSION}" $RECENT_SQL_EXECUTIONS $TOTAL_COUNT
185 # Test 2 - downgrade to 0800
187 /opt/app/policy/bin/prepare_downgrade.sh ${SQL_DB}
188 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o downgrade -f 0900 -t 0800
190 let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
191 check_results $END_STATUS 'downgrade' "${START_VERSION}" "${END_VERSION}" $NEW_SQL_EXECUTIONS $I_UPGRADE_COUNT
195 # Test 3 - upgrade to 0900
197 /opt/app/policy/bin/prepare_upgrade.sh ${SQL_DB}
198 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o upgrade -f 0800 -t 0900
200 let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
201 check_results $END_STATUS 'upgrade' "${START_VERSION}" "${END_VERSION}" $NEW_SQL_EXECUTIONS $I_UPGRADE_COUNT
205 # Test4 - run upgrade on db where tables already exist and migration schema is empty
207 /opt/app/policy/bin/prepare_downgrade.sh ${SQL_DB}
208 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o downgrade -f 0900 -t 0800
209 run_sql "root" "${MYSQL_ROOT_PASSWORD}" "${SCHEMA}" "DROP DATABASE IF EXISTS migration;"
210 /opt/app/policy/bin/prepare_upgrade.sh ${SQL_DB}
211 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o upgrade -t 0900
213 let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS
214 check_results $END_STATUS 'upgrade' "0800" "${END_VERSION}" $NEW_SQL_EXECUTIONS $I_UPGRADE_COUNT
218 # Test5 - upgrade after failed downgrade
220 /opt/app/policy/bin/prepare_downgrade.sh ${SQL_DB}
221 run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "DROP table pdpstatistics;"
222 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o downgrade -f 0900 -t 0800
223 run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "${PDPSTATISTICS2}"
224 run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "${IDX_TSIDX1}"
225 /opt/app/policy/bin/prepare_upgrade.sh ${SQL_DB}
226 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o upgrade -f 0800 -t 0900
228 let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
229 # (files run before error * 2) + 1 to run the file again
231 check_results $END_STATUS 'upgrade' "0800" "0900" $NEW_SQL_EXECUTIONS $TOTAL_COUNT
235 # Test6 - Downgrade after failed downgrade
237 /opt/app/policy/bin/prepare_downgrade.sh ${SQL_DB}
238 run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "DROP table pdpstatistics;"
239 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o downgrade -f 0900 -t 0800
240 run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "${PDPSTATISTICS2}"
241 run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "${IDX_TSIDX1}"
242 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o downgrade -f 0900 -t 0800
244 let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
245 # +1 to run the file again
246 let TOTAL_COUNT=$I_UPGRADE_COUNT+1
247 check_results $END_STATUS 'downgrade' "${START_VERSION}" "${END_VERSION}" $NEW_SQL_EXECUTIONS $TOTAL_COUNT
251 # Test7 - downgrade after failed upgrade
253 /opt/app/policy/bin/prepare_upgrade.sh ${SQL_DB}
254 run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "DROP table pdpstatistics;"
255 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o upgrade -f 0800 -t 0900
256 run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "${PDPSTATISTICS}"
257 /opt/app/policy/bin/prepare_downgrade.sh ${SQL_DB}
258 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o downgrade -f 0900 -t 0800
260 let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
261 # (files run before error * 2) + 1 to run the file again
263 check_results $END_STATUS 'downgrade' "${START_VERSION}" "${END_VERSION}" $NEW_SQL_EXECUTIONS $TOTAL_COUNT
267 # Test 8 - Upgrade after failed upgrade
269 /opt/app/policy/bin/prepare_upgrade.sh ${SQL_DB}
270 run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "DROP table pdpstatistics;"
271 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o upgrade -f 0800 -t 0900
272 run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "${PDPSTATISTICS}"
273 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o upgrade -t 0900
275 let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
276 # +1 to run the file again
277 let TOTAL_COUNT=$I_UPGRADE_COUNT+1
278 check_results $END_STATUS 'upgrade' "${START_VERSION}" "${END_VERSION}" $NEW_SQL_EXECUTIONS $TOTAL_COUNT
282 # Test 9 - Upgrade when pdpstatistics contains data
283 /opt/app/policy/bin/prepare_downgrade.sh ${SQL_DB}
284 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o downgrade -f 0900 -t 0800
286 run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "${SQL1}"
288 run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "${SQL2}"
290 /opt/app/policy/bin/prepare_upgrade.sh ${SQL_DB}
291 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o upgrade -f 0800 -t 0900
292 run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "${SQL3}"
294 run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "${SQL4}"
296 let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
297 # +1 to run the file again
298 let TOTAL_COUNT=$I_UPGRADE_COUNT
299 check_results $END_STATUS 'upgrade' "${START_VERSION}" "${END_VERSION}" $NEW_SQL_EXECUTIONS $TOTAL_COUNT
303 # Test 10 - downgrade to 0800 with records in pdpstatistics
305 /opt/app/policy/bin/prepare_downgrade.sh ${SQL_DB}
306 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o downgrade -f 0900 -t 0800
307 run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "${SQL5}"
309 run_sql "${SQL_USER}" "${SQL_PASSWORD}" "${SCHEMA}" "${SQL6}"
311 let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
312 check_results $END_STATUS 'downgrade' "${START_VERSION}" "${END_VERSION}" $NEW_SQL_EXECUTIONS $I_UPGRADE_COUNT
316 # Test 11 - downgrade from 0800
318 /opt/app/policy/bin/prepare_downgrade.sh ${SQL_DB}
319 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o downgrade -f 0800
321 let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
322 check_results $END_STATUS 'downgrade' "${START_VERSION}" "${END_VERSION}" $NEW_SQL_EXECUTIONS $H_DOWNGRADE_COUNT
326 # Test 12 - Full upgrade
328 /opt/app/policy/bin/prepare_upgrade.sh ${SQL_DB}
329 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o upgrade
331 let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
332 check_results $END_STATUS 'upgrade' "0" "${END_VERSION}" $NEW_SQL_EXECUTIONS $TOTAL_SQLS_UPGRADE
336 # Test 13 - Full downgrade
338 /opt/app/policy/bin/prepare_downgrade.sh ${SQL_DB}
339 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o downgrade -f 1300 -t 0
341 let NEW_SQL_EXECUTIONS=$RECENT_SQL_EXECUTIONS-$PREVIOUS_SQL_EXECUTIONS
342 check_results $END_STATUS 'downgrade' "${START_VERSION}" "0" $NEW_SQL_EXECUTIONS $TOTAL_SQLS_DOWNGRADE
345 /opt/app/policy/bin/prepare_upgrade.sh ${SQL_DB}
346 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o upgrade
347 /opt/app/policy/bin/db-migrator -s ${SQL_DB} -o report
350 echo "-----------------------------------------------------------------------"
351 echo "Number of Tests: $TESTS, Tests Passed: $PASSED, Tests Failed: $FAILED"
352 echo "-----------------------------------------------------------------------"
354 echo "End of test $(date +%F-%T)"