Skip to content

Commit d2b7e53

Browse files
authored
Merge pull request #24 from jasonlyle88/develop
- Add parallelization control parameter, default to executing tests in a single test type in parallel (test types in serial) - Never run anything against DATABASECHANGELOG, always used custom changelog - Cleanup all changelog objects after all tests complete - Update `find` command used to collect test cases to be POSIX compatible
2 parents 3c139d9 + 79a59cd commit d2b7e53

12 files changed

+130
-51
lines changed

source/sqlclUnitTest.sh

Lines changed: 114 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ function main() {
4949
printf -- ' -l {directory} -- The directory containing SQLcl Liquibase tests to run.\n'
5050
printf -- ' If not provided, will default to "lb_unit_tests"\n'
5151
printf -- ' directory in the same directory as this script.\n'
52+
printf -- ' -P {type} -- The parallelization type to use for this execution.\n'
53+
printf -- ' Accepts one of the following types:\n'
54+
printf -- ' NONE - Execute all tests serially\n'
55+
printf -- ' TYPE - Execute all tests for a each test type in parallel (default)\n'
56+
printf -- ' TEST - Execute all tests in parallel\n'
5257
printf -- ' -h -- Show this help.\n'
5358
printf -- '\n'
5459
printf -- 'Example:\n'
@@ -157,14 +162,18 @@ function main() {
157162
local testType="$1"
158163
local testFile="$2"
159164

165+
local functionPid
166+
#shellcheck disable=2016
167+
functionPid="$($SHELL -c 'echo $PPID')"
168+
local functionUniqueIdentifier="${scriptUniqueIdentifier}_${functionPid}"
169+
local databaseChangelogTableName="${functionUniqueIdentifier}_changelog"
160170
local testDirectory
161171
local testFilename
162172
local testName
163173
local testResultCode
164174
local resultFile
165175
local logFile
166176
local wrapperFile
167-
local databaseChangelogTableName
168177

169178
testDirectory="$(dirname "${testFile}")"
170179
testFilename="$(basename "${testFile}")"
@@ -184,14 +193,16 @@ function main() {
184193
mkdir -p "$(dirname "${logFile}")"
185194
touch "${logFile}"
186195

187-
"${sqlclBinary}" "${sqlParamsDirectConnect[@]}" "${sqlParamsWithoutPassword[@]}" 1>"${logFile}" 2>&1 <<- EOF
196+
printf -- 'Execution unique Identifier: "%s"\n\n' "${functionUniqueIdentifier}" 1>>"${logFile}"
197+
198+
"${sqlclBinary}" "${sqlParamsDirectConnect[@]}" "${sqlParamsWithoutPassword[@]}" 1>>"${logFile}" 2>&1 <<- EOF
188199
${databasePassword}
189200
whenever sqlerror exit failure
190201
set serveroutput on size unlimited
191202
set verify on
192203
set echo on
193204
show connection
194-
@ "${testFile}" "${testDirectory}"
205+
@ "${testFile}" "${functionUniqueIdentifier}" "${testDirectory}"
195206
EOF
196207
testResultCode=$?
197208

@@ -203,33 +214,34 @@ function main() {
203214
mkdir -p "$(dirname "${logFile}")"
204215
touch "${logFile}"
205216

217+
printf -- 'Execution unique Identifier: "%s"\n\n' "${functionUniqueIdentifier}" 1>>"${logFile}"
218+
206219
{
207220
printf -- 'whenever sqlerror exit failure\n'
208221
printf -- 'connect %s\n' "${sqlParamsWithPassword[*]}"
209222
printf -- 'set serveroutput on size unlimited\n'
210223
printf -- 'set verify on\n'
211224
printf -- 'set echo on\n'
212225
printf -- 'show connection\n'
213-
printf -- '@ "%s" "%s"' "${testFile}" "${testDirectory}"
226+
printf -- '@ "%s" "%s" "%s"' "${testFile}" "${functionUniqueIdentifier}" "${testDirectory}"
214227
} > "${wrapperFile}"
215228

216-
"${sqlclBinary}" -noupdates /nolog "@${wrapperFile}" 1>"${logFile}" 2>&1
229+
"${sqlclBinary}" -noupdates /nolog "@${wrapperFile}" 1>>"${logFile}" 2>&1
217230
testResultCode=$?
218231

219232
rm "${wrapperFile}"
220233
elif [[ "${testType}" = "${testTypeSqlclLiquibaseCurrentDirectory}" ]]; then
221234
resultFile="${liquibaseCurrentDirectoryTestResultFile}"
222235
logFile="${logDirectory}/liquibase-current-directory/${testName}.log"
223236

224-
# Make sure database changelog table name is unique
225-
databaseChangelogTableName="ut${RANDOM}$(date +%s)"
226-
227237
mkdir -p "$(dirname "${logFile}")"
228238
touch "${logFile}"
229239

240+
printf -- 'Execution unique Identifier: "%s"\n\n' "${functionUniqueIdentifier}" 1>>"${logFile}"
241+
230242
cd "${testDirectory}" || return 1
231243

232-
"${sqlclBinary}" "${sqlParamsDirectConnect[@]}" "${sqlParamsWithPassword[@]}" 1>"${logFile}" 2>&1 <<- EOF
244+
"${sqlclBinary}" "${sqlParamsDirectConnect[@]}" "${sqlParamsWithPassword[@]}" 1>>"${logFile}" 2>&1 <<- EOF
233245
whenever sqlerror exit failure
234246
set serveroutput on size unlimited
235247
set verify on
@@ -238,24 +250,16 @@ function main() {
238250
liquibase update -contexts test_context -database-changelog-table-name ${databaseChangelogTableName} -changelog-file ${testFilename}
239251
EOF
240252
testResultCode=$?
241-
242-
"${sqlclBinary}" "${sqlParamsDirectConnect[@]}" "${sqlParamsWithPassword[@]}" 1>/dev/null 2>&1 <<- EOF
243-
drop view ${databaseChangelogTableName}_DETAILS;
244-
drop table ${databaseChangelogTableName}_ACTIONS;
245-
drop table ${databaseChangelogTableName};
246-
drop table ${databaseChangelogTableName}LOCK;
247-
EOF
248253
elif [[ "${testType}" = "${testTypeSqlclLiquibaseSearchPath}" ]]; then
249254
resultFile="${liquibaseSearchPathTestResultFile}"
250255
logFile="${logDirectory}/liquibase-search-path/${testName}.log"
251256

252-
# Make sure database changelog table name is unique
253-
databaseChangelogTableName="ut${RANDOM}$(date +%s)"
254-
255257
mkdir -p "$(dirname "${logFile}")"
256258
touch "${logFile}"
257259

258-
"${sqlclBinary}" "${sqlParamsDirectConnect[@]}" "${sqlParamsWithPassword[@]}" 1>"${logFile}" 2>&1 <<- EOF
260+
printf -- 'Execution unique Identifier: "%s"\n\n' "${functionUniqueIdentifier}" 1>>"${logFile}"
261+
262+
"${sqlclBinary}" "${sqlParamsDirectConnect[@]}" "${sqlParamsWithPassword[@]}" 1>>"${logFile}" 2>&1 <<- EOF
259263
whenever sqlerror exit failure
260264
set serveroutput on size unlimited
261265
set verify on
@@ -264,13 +268,6 @@ function main() {
264268
liquibase update -contexts test_context -database-changelog-table-name ${databaseChangelogTableName} -search-path ${testDirectory} -changelog-file ${testFilename}
265269
EOF
266270
testResultCode=$?
267-
268-
"${sqlclBinary}" "${sqlParamsDirectConnect[@]}" "${sqlParamsWithPassword[@]}" 1>/dev/null 2>&1 <<- EOF
269-
drop view ${databaseChangelogTableName}_DETAILS;
270-
drop table ${databaseChangelogTableName}_ACTIONS;
271-
drop table ${databaseChangelogTableName};
272-
drop table ${databaseChangelogTableName}LOCK;
273-
EOF
274271
fi
275272

276273
printf -- '%s:%s\n' "${testName}" "${testResultCode}" >> "${resultFile}"
@@ -321,6 +318,7 @@ function main() {
321318
local dFlag='false'
322319
local wFlag='false'
323320
local lFlag='false'
321+
local PFlag='false'
324322

325323
local sqlclBinary
326324
local databaseUsername
@@ -330,6 +328,7 @@ function main() {
330328
local sqlclDirectTestsDirectory
331329
local sqlclWrappedTestsDirectory
332330
local liquibaseTestsDirectory
331+
local parallelizationType
333332

334333
############################################################################
335334
## Constants
@@ -348,6 +347,10 @@ function main() {
348347
local originalPWD="${PWD}"
349348
# shellcheck disable=SC2034
350349
local originalIFS="${IFS}"
350+
# shellcheck disable=SC2034
351+
local scriptPid="${$}"
352+
# shellcheck disable=SC2034
353+
local scriptUniqueIdentifier="sqlcl_ut_${scriptPid}"
351354

352355
############################################################################
353356
## Procedural variables
@@ -408,7 +411,7 @@ function main() {
408411
# Option parsing
409412
#
410413
############################################################################
411-
while getopts ':b:u:p:c:C:d:w:l:h' opt
414+
while getopts ':b:u:p:c:C:d:w:l:P:h' opt
412415
do
413416

414417
case "${opt}" in
@@ -444,6 +447,10 @@ function main() {
444447
lFlag='true'
445448
liquibaseTestsDirectory="${OPTARG}"
446449
;;
450+
'P')
451+
PFlag='true'
452+
parallelizationType="$(toUpperCase "${OPTARG}")"
453+
;;
447454
'h')
448455
usage
449456
return $?
@@ -487,6 +494,10 @@ function main() {
487494
liquibaseTestsDirectory="${scriptPath}/lb_unit_tests"
488495
fi
489496

497+
if [[ "${PFlag}" != 'true' ]]; then
498+
parallelizationType="TYPE"
499+
fi
500+
490501
############################################################################
491502
#
492503
# Function Logic
@@ -547,13 +558,26 @@ function main() {
547558
return 17
548559
fi
549560

561+
if [[ "${parallelizationType}" != 'NONE' ]] && [[ "${parallelizationType}" != 'TYPE' ]] && [[ "${parallelizationType}" != 'TEST' ]]; then
562+
printf -- 'ERROR: Invalid parallelization type (-P): "%s"\n' "${parallelizationType}" | tee -a "${logMainFile}" >&2
563+
return 18
564+
fi
565+
550566
##
551567
## Manipulate parameters
552568
##
553569
sqlclDirectTestsDirectory="$(getCanonicalPath "${sqlclDirectTestsDirectory}")"
554570
sqlclWrappedTestsDirectory="$(getCanonicalPath "${sqlclWrappedTestsDirectory}")"
555571
liquibaseTestsDirectory="$(getCanonicalPath "${liquibaseTestsDirectory}")"
556572

573+
if [[ "${bFlag}" == 'true' ]]; then
574+
sqlclBinary="$(getCanonicalPath "${sqlclBinary}")"
575+
fi
576+
577+
if [[ "${CFlag}" == 'true' ]]; then
578+
databaseCloudWallet="$(getCanonicalPath "${databaseCloudWallet}")"
579+
fi
580+
557581
sqlParamsWithoutPassword=()
558582
if [[ "${CFlag}" == 'true' ]]; then
559583
sqlParamsWithoutPassword+=('-cloudconfig' "${databaseCloudWallet}")
@@ -632,7 +656,7 @@ function main() {
632656
{
633657
printf -- 'whenever sqlerror exit failure\n'
634658
printf -- 'show connection\n'
635-
printf -- 'liquibase update -changelog-file %s\n' "$(basename "${liquibaseWehenverErrorTest}")"
659+
printf -- 'liquibase update -database-changelog-table-name %s -changelog-file %s\n' "${scriptUniqueIdentifier}_changelog" "$(basename "${liquibaseWehenverErrorTest}")"
636660
printf -- 'exit'
637661
} > "${sqlWheneverErrorTest}"
638662

@@ -725,23 +749,77 @@ function main() {
725749

726750
executeUnitTest "${testType}" "${testFile}" &
727751

728-
sleep 1
729-
done < <( find "${testsDirectory}" -type 'f' -iname "${testExtension}" -mindepth '1' -maxdepth '1' | sort )
752+
# Wait for test to finish if no parallel execution was requested
753+
if [[ "${parallelizationType}" == 'NONE' ]]; then
754+
wait
755+
fi
756+
done < <( find "${testsDirectory}" -mindepth '1' -maxdepth '1' -type 'f' -iname "${testExtension}" | sort )
757+
758+
##
759+
## Wait for unit test execution to finish if TYPE parallelization type was requested
760+
##
761+
if [[ "${parallelizationType}" == 'TYPE' ]]; then
762+
printf -- '\n' | tee -a "${logMainFile}"
763+
printf -- '%s\n' "${h1}" | tee -a "${logMainFile}"
764+
printf -- '%s %s\n' "${hs}" 'Wait for unit test execution to finish' | tee -a "${logMainFile}"
765+
printf -- '%s\n' "${h1}" | tee -a "${logMainFile}"
766+
767+
wait
768+
769+
printf -- 'Unit tests finished running.\n' | tee -a "${logMainFile}"
770+
fi
771+
730772
done
731773

732774
##
733-
## Wait for unit test execution to finish
775+
## Wait for unit test execution to finish if TEST parallelization type was requested
776+
##
777+
if [[ "${parallelizationType}" == 'TEST' ]]; then
778+
printf -- '\n' | tee -a "${logMainFile}"
779+
printf -- '%s\n' "${h1}" | tee -a "${logMainFile}"
780+
printf -- '%s %s\n' "${hs}" 'Wait for unit test execution to finish' | tee -a "${logMainFile}"
781+
printf -- '%s\n' "${h1}" | tee -a "${logMainFile}"
782+
783+
wait
784+
785+
printf -- 'Unit tests finished running.\n' | tee -a "${logMainFile}"
786+
fi
787+
788+
##
789+
## Cleanup testing objects in database
734790
##
735791

736792
printf -- '\n' | tee -a "${logMainFile}"
737793
printf -- '%s\n' "${h1}" | tee -a "${logMainFile}"
738-
printf -- '%s %s\n' "${hs}" 'Wait for unit test execution to finish' | tee -a "${logMainFile}"
794+
printf -- '%s %s\n' "${hs}" 'Cleanup unit test database objects' | tee -a "${logMainFile}"
739795
printf -- '%s\n' "${h1}" | tee -a "${logMainFile}"
740796

741-
# Wait for all background executions of unit tests to complete
742-
wait
797+
"${sqlclBinary}" "${sqlParamsDirectConnect[@]}" "${sqlParamsWithPassword[@]}" 1>>"${logMainFile}" 2>&1 <<- EOF
798+
declare
799+
c_unique_identifier constant varchar2(255 char) := upper('${scriptUniqueIdentifier}');
800+
begin
801+
for obj in (
802+
select table_name
803+
from user_tables
804+
where table_name like c_unique_identifier || '%'
805+
)
806+
loop
807+
execute immediate 'drop table ' || obj.table_name || ' cascade constraints purge';
808+
end loop;
809+
810+
for obj in (
811+
select view_name
812+
from user_views
813+
where view_name like c_unique_identifier || '%'
814+
)
815+
loop
816+
execute immediate 'drop view ' || obj.view_name;
817+
end loop;
818+
end;
819+
/
820+
EOF
743821

744-
printf -- 'Unit tests finished running.\n' | tee -a "${logMainFile}"
822+
printf -- 'Completed.\n' | tee -a "${logMainFile}"
745823

746824
##
747825
## Gather unit test results
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
liquibase update -search-path / -defaults-file &1/sqlcl-liquibase-defaults-file-respected/liquibase.properties -changelog-file &1/sqlcl-liquibase-defaults-file-respected/changelog.xml
1+
liquibase update -database-changelog-table-name &1._changelog -search-path / -defaults-file &2/sqlcl-liquibase-defaults-file-respected/liquibase.properties -changelog-file &2/sqlcl-liquibase-defaults-file-respected/changelog.xml
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
liquibase update -search-path &1/sqlcl-liquibase-do-not-prompt-password -changelog-file changelog.xml
1+
liquibase update -database-changelog-table-name &1._changelog -search-path &2/sqlcl-liquibase-do-not-prompt-password -changelog-file changelog.xml
22

33
select 'test'
44
from sys.dual;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
cd "sqlcl-liquibase-root-changelog-relative-to-cwd"
22

3-
liquibase update -changelog-file sqlcl-liquibase-root-changelog-relative-to-cwd-changelog.xml
3+
liquibase update -database-changelog-table-name &1._changelog -changelog-file sqlcl-liquibase-root-changelog-relative-to-cwd-changelog.xml
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
@ "&1/sqlcl-liquibase-root-changelog-relative-to-script-absolute/sqlcl-liquibase-root-changelog-relative-to-script-absolute-sql.sql"
1+
@ "&2/sqlcl-liquibase-root-changelog-relative-to-script-absolute/sqlcl-liquibase-root-changelog-relative-to-script-absolute-sql.sql"
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
liquibase update -changelog-file sqlcl-liquibase-root-changelog-relative-to-script-absolute-changelog.xml
1+
liquibase update -database-changelog-table-name &1._changelog -changelog-file sqlcl-liquibase-root-changelog-relative-to-script-absolute-changelog.xml
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
liquibase update -changelog-file sqlcl-liquibase-root-changelog-relative-to-script-relative-changelog.xml
1+
liquibase update -database-changelog-table-name &1._changelog -changelog-file sqlcl-liquibase-root-changelog-relative-to-script-relative-changelog.xml
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
liquibase update -changelog-file sqlcl-liquibase-runApexScript-basic/changelog.xml
1+
liquibase update -database-changelog-table-name &1._changelog -changelog-file sqlcl-liquibase-runApexScript-basic/changelog.xml
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
liquibase update -changelog-file sqlcl-liquibase-runApexScript-heirarchical/changelog.xml
1+
liquibase update -database-changelog-table-name &1._changelog -changelog-file sqlcl-liquibase-runApexScript-heirarchical/changelog.xml
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
liquibase update -changelog-file sqlcl-liquibase-runApexScript-basic/changelog.xml
1+
liquibase update -database-changelog-table-name &1._changelog -changelog-file sqlcl-liquibase-runApexScript-basic/changelog.xml

0 commit comments

Comments
 (0)