Skip to content

Commit

Permalink
fixed UT flakiness
Browse files Browse the repository at this point in the history
- using Bash::handlePipelineFailure
- removed usage of Embeded binaries in dbScriptAllDatabases and dbQueryAllDatabases
  • Loading branch information
fchastanet committed Jan 15, 2024
1 parent 18be7b5 commit 2722c2c
Show file tree
Hide file tree
Showing 33 changed files with 298 additions and 204 deletions.
13 changes: 7 additions & 6 deletions .github/workflows/lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,33 @@ jobs:
include:
- vendor: ubuntu
bashImage: ubuntu:20.04
batsOptions: -j 5
batsOptions: -j 30
bashTarVersion: 4.4
runPrecommitTests: false
- vendor: ubuntu
bashImage: ubuntu:20.04
bashTarVersion: 5.0
batsOptions: -j 5
batsOptions: -j 30
runPrecommitTests: false
- vendor: ubuntu
bashImage: ubuntu:20.04
bashTarVersion: 5.1
batsOptions: -j 5
batsOptions: -j 30
runPrecommitTests: true
- vendor: alpine
bashTarVersion: 4.4
bashImage: amd64/bash:4.4-alpine3.18
batsOptions: -j 5
batsOptions: -j 30
runPrecommitTests: false
- vendor: alpine
bashTarVersion: 5.0
bashImage: amd64/bash:5.0-alpine3.18
batsOptions: -j 5
batsOptions: -j 30
runPrecommitTests: false
- vendor: alpine
bashTarVersion: 5.1
bashImage: amd64/bash:5.1-alpine3.18
batsOptions: -j 5
batsOptions: -j 30
runPrecommitTests: false
steps:
- name: Checkout
Expand Down Expand Up @@ -116,6 +116,7 @@ jobs:
name: linter-reports
path: |
megalinter-reports/**
bin/**
- name: Create Pull Request
if: matrix.runPrecommitTests && failure()
Expand Down
2 changes: 1 addition & 1 deletion bin/cli
Original file line number Diff line number Diff line change
Expand Up @@ -1646,7 +1646,7 @@ cliCommand() {
# shellcheck disable=SC2054
helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped)
echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")"
echo " Default value: default"
echo ' Default value: default'
echo ' Possible values: default|default-force|noColor'
echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}"
local -a helpArray
Expand Down
38 changes: 36 additions & 2 deletions bin/dbImport
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,36 @@ Assert::commandExists() {
return 0
}

# @description ignore exit code 141 from simple command pipes
# @example use with:
# local resultingStatus=0
# local -a originalPipeStatus=()
# cmd1 | cmd2 || Bash::handlePipelineFailure resultingStatus originalPipeStatus || true
# [[ "${resultingStatus}" = "0" ]]
# @arg $1 resultingStatusCode:&int (passed by reference) (optional) resulting status code
# @arg $2 originalStatus:int[] (passed by reference) (optional) copy of original PIPESTATUS array
# @env PIPESTATUS assuming that this function is called like in the example provided
# @see https://unix.stackexchange.com/a/709880/582856
Bash::handlePipelineFailure() {
local -a pipeStatusBackup=("${PIPESTATUS[@]}")
local -n handlePipelineFailure_resultingStatusCode=$1
local -n handlePipelineFailure_originalStatus=$2
# shellcheck disable=SC2034
handlePipelineFailure_originalStatus=("${pipeStatusBackup[@]}")
handlePipelineFailure_resultingStatusCode=0
local statusCode
for statusCode in "${pipeStatusBackup[@]}"; do
if ((statusCode == 141)); then
return 0
elif ((statusCode > 0)); then
# shellcheck disable=SC2034
handlePipelineFailure_resultingStatusCode="${statusCode}"
break
fi
done
return "${handlePipelineFailure_resultingStatusCode}"
}

# @description get absolute conf file from specified conf folder deduced using these rules
# * from absolute file (ignores <confFolder> and <extension>)
# * relative to where script is executed (ignores <confFolder> and <extension>)
Expand Down Expand Up @@ -2202,7 +2232,7 @@ dbImportCommand() {
# shellcheck disable=SC2054
helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped)
echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")"
echo " Default value: default"
echo ' Default value: default'
echo ' Possible values: default|default-force|noColor'
echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}"
local -a helpArray
Expand Down Expand Up @@ -2488,9 +2518,13 @@ run() {
Log::displayInfo "avoid to create db structure"
else
Log::displayInfo "create db structure from ${remoteDbStructureDumpTempFile}"
# shellcheck disable=SC2034
local status=0
# shellcheck disable=SC2034
local -a pipeStatus=()
time (
pv "${remoteDbStructureDumpTempFile}" | zcat |
Database::query dbTargetDatabase "" "${targetDbName}"
Database::query dbTargetDatabase "" "${targetDbName}" || Bash::handlePipelineFailure status pipeStatus
)
fi
fi
Expand Down
2 changes: 1 addition & 1 deletion bin/dbImportProfile
Original file line number Diff line number Diff line change
Expand Up @@ -1781,7 +1781,7 @@ dbImportProfileCommand() {
# shellcheck disable=SC2054
helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped)
echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")"
echo " Default value: default"
echo ' Default value: default'
echo ' Possible values: default|default-force|noColor'
echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}"
local -a helpArray
Expand Down
56 changes: 45 additions & 11 deletions bin/dbImportStream
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,36 @@ Assert::commandExists() {
return 0
}

# @description ignore exit code 141 from simple command pipes
# @example use with:
# local resultingStatus=0
# local -a originalPipeStatus=()
# cmd1 | cmd2 || Bash::handlePipelineFailure resultingStatus originalPipeStatus || true
# [[ "${resultingStatus}" = "0" ]]
# @arg $1 resultingStatusCode:&int (passed by reference) (optional) resulting status code
# @arg $2 originalStatus:int[] (passed by reference) (optional) copy of original PIPESTATUS array
# @env PIPESTATUS assuming that this function is called like in the example provided
# @see https://unix.stackexchange.com/a/709880/582856
Bash::handlePipelineFailure() {
local -a pipeStatusBackup=("${PIPESTATUS[@]}")
local -n handlePipelineFailure_resultingStatusCode=$1
local -n handlePipelineFailure_originalStatus=$2
# shellcheck disable=SC2034
handlePipelineFailure_originalStatus=("${pipeStatusBackup[@]}")
handlePipelineFailure_resultingStatusCode=0
local statusCode
for statusCode in "${pipeStatusBackup[@]}"; do
if ((statusCode == 141)); then
return 0
elif ((statusCode > 0)); then
# shellcheck disable=SC2034
handlePipelineFailure_resultingStatusCode="${statusCode}"
break
fi
done
return "${handlePipelineFailure_resultingStatusCode}"
}

# @description get absolute conf file from specified conf folder deduced using these rules
# * from absolute file (ignores <confFolder> and <extension>)
# * relative to where script is executed (ignores <confFolder> and <extension>)
Expand Down Expand Up @@ -2014,7 +2044,7 @@ dbImportStreamCommand() {
# shellcheck disable=SC2054
helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped)
echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")"
echo " Default value: default"
echo ' Default value: default'
echo ' Possible values: default|default-force|noColor'
echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}"
local -a helpArray
Expand Down Expand Up @@ -2157,7 +2187,7 @@ run() {
Version::checkMinimal "gawk" "--version" "5.0.1"

# create db instances
declare -Agx dbTargetInstance
local -Agx dbTargetInstance

Database::newInstance dbTargetInstance "${optionTargetDsn}"
Database::setQueryOptions dbTargetInstance "${dbTargetInstance[QUERY_OPTIONS]} --connect-timeout=5"
Expand All @@ -2166,7 +2196,11 @@ run() {
initializeDefaultTargetMysqlOptions dbTargetInstance "${argTargetDbName}"

# TODO character set should be retrieved from dump files if possible
declare remoteCharacterSet="${optionCharacterSet:-${defaultRemoteCharacterSet}}"
local remoteCharacterSet="${optionCharacterSet:-${defaultRemoteCharacterSet}}"

local status=0
# shellcheck disable=SC2034
local -a pipeStatus=()

# shellcheck disable=2086
(
Expand All @@ -2175,19 +2209,19 @@ run() {
elif [[ "${argDumpFile}" =~ \.gz$ ]]; then
zcat "${argDumpFile}"
fi
# zcat will continue to write to stdout whereas awk has finished if table has been found
# we detect this case because zcat will return code 141 because pipe closed
status=$?
if [[ "${status}" -eq "141" ]]; then true; else exit "${status}"; fi
) |
awk \
-v PROFILE_COMMAND="${profileCommandFile}" \
-v CHARACTER_SET="${remoteCharacterSet}" \
--source "${awkScript}" \
- | mysql \
"--defaults-extra-file=${dbTargetInstance['AUTH_FILE']}" \
${dbTargetInstance['DB_IMPORT_OPTIONS']} \
"${argTargetDbName}" || exit $?
- |
mysql \
"--defaults-extra-file=${dbTargetInstance['AUTH_FILE']}" \
${dbTargetInstance['DB_IMPORT_OPTIONS']} \
"${argTargetDbName}" ||
# zcat will continue to write to stdout whereas awk has finished if table has been found
# we detect this case because zcat will return code 141 because pipe closed
Bash::handlePipelineFailure status pipeStatus
}

if [[ "${BASH_FRAMEWORK_QUIET_MODE:-0}" = "1" ]]; then
Expand Down
127 changes: 61 additions & 66 deletions bin/dbQueryAllDatabases

Large diffs are not rendered by default.

57 changes: 3 additions & 54 deletions bin/dbScriptAllDatabases

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions bin/doc
Original file line number Diff line number Diff line change
Expand Up @@ -1624,7 +1624,7 @@ docCommand() {
# shellcheck disable=SC2054
helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped)
echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")"
echo " Default value: default"
echo ' Default value: default'
echo ' Possible values: default|default-force|noColor'
echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}"
local -a helpArray
Expand Down Expand Up @@ -1732,7 +1732,7 @@ run() {
"${DOC_DIR}/Commands.md" \
"${BASH_TOOLS_ROOT_DIR}/bin" \
TOKEN_NOT_FOUND_COUNT \
'(bash-tpl|plantuml|definitionLint|compile|installFacadeExample)$'
'(test)$'

# inject plantuml diagram source code into command
sed -E -i \
Expand Down
2 changes: 1 addition & 1 deletion bin/gitIsAncestorOf
Original file line number Diff line number Diff line change
Expand Up @@ -1374,7 +1374,7 @@ gitIsAncestorOfCommand() {
# shellcheck disable=SC2054
helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped)
echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")"
echo " Default value: default"
echo ' Default value: default'
echo ' Possible values: default|default-force|noColor'
echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}"
local -a helpArray
Expand Down
2 changes: 1 addition & 1 deletion bin/gitIsBranch
Original file line number Diff line number Diff line change
Expand Up @@ -1352,7 +1352,7 @@ gitIsBranchCommand() {
# shellcheck disable=SC2054
helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped)
echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")"
echo " Default value: default"
echo ' Default value: default'
echo ' Possible values: default|default-force|noColor'
echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}"
local -a helpArray
Expand Down
2 changes: 1 addition & 1 deletion bin/gitRenameBranch
Original file line number Diff line number Diff line change
Expand Up @@ -1458,7 +1458,7 @@ gitRenameBranchCommand() {
# shellcheck disable=SC2054
helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped)
echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")"
echo " Default value: default"
echo ' Default value: default'
echo ' Possible values: default|default-force|noColor'
echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}"
local -a helpArray
Expand Down
2 changes: 1 addition & 1 deletion bin/installRequirements
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,7 @@ installRequirementsCommand() {
# shellcheck disable=SC2054
helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped)
echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")"
echo " Default value: default"
echo ' Default value: default'
echo ' Possible values: default|default-force|noColor'
echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}"
local -a helpArray
Expand Down
4 changes: 2 additions & 2 deletions bin/mysql2puml
Original file line number Diff line number Diff line change
Expand Up @@ -1525,7 +1525,7 @@ mysql2pumlCommand() {
# shellcheck disable=SC2054
helpArray=(header\ configuration\ of\ the\ plant\ uml\ file)
echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")"
echo " Default value: default"
echo ' Default value: default'
echo
echo -e "${__HELP_TITLE_COLOR}GLOBAL OPTIONS:${__RESET_COLOR}"
echo -e " ${__HELP_OPTION_COLOR}--bash-framework-config <String>${__HELP_NORMAL} {single}"
Expand Down Expand Up @@ -1568,7 +1568,7 @@ mysql2pumlCommand() {
# shellcheck disable=SC2054
helpArray=(choose\ color\ theme\ -\ default-force\ means\ colors\ will\ be\ produced\ even\ if\ command\ is\ piped)
echo -e " $(Array::wrap2 " " 76 4 "${helpArray[@]}")"
echo " Default value: default"
echo ' Default value: default'
echo ' Possible values: default|default-force|noColor'
echo -e " ${__HELP_OPTION_COLOR}--help${__HELP_NORMAL}, ${__HELP_OPTION_COLOR}-h${__HELP_NORMAL} {single}"
local -a helpArray
Expand Down
Loading

0 comments on commit 2722c2c

Please sign in to comment.