Commit fda79f594114c incorrectly assumed that all internal beakerlib variables are exported to the environment. This caused the cleanup execution to "work" on the basic level, but things using ie. $__INTERNAL_JOURNALIST (and many others) were broken.
The solution is to extract even local/global "not exported" variables, ideally with correct attributes (array, readonly, exported, ..).
Also fix documentation, which is incorrect even prior to this commit.
Signed-off-by: Jiri Jaburek jjaburek@redhat.com --- src/infrastructure.sh | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-)
diff --git a/src/infrastructure.sh b/src/infrastructure.sh index 29a5d43..3d05f57 100644 --- a/src/infrastructure.sh +++ b/src/infrastructure.sh @@ -116,6 +116,7 @@ __INTERNAL_Mount(){
__INTERNAL_rlCleanupGenFinal() { + local varname= local newfinal="$__INTERNAL_CLEANUP_FINAL".tmp if [ -e "$newfinal" ]; then rm -f "$newfinal" || return 1 @@ -128,10 +129,17 @@ __INTERNAL_rlCleanupGenFinal() EOF
# environment - # - env variables (incl. BEAKERLIB_DIR) - # NOTE: even works around possible single quotes in variables - env | sed -r -e "s/'/'\''/g" -e "s/^([^=]+)=(.*)$/export \1='\2'/" \ - >> "$newfinal" + # - variables (local, global and env) + for varname in $(compgen -v); do + varname=$(declare -p "$varname") + # declaration of a readonly variable may fail if a variable with + # the same name is already declared - silently ignore it + if expr + "$varname" : "declare -[^r]*r[^r]* " >/dev/null; then + echo "$varname" "2>/dev/null" >> "$newfinal" + else + echo "$varname" >> "$newfinal" + fi + done # - functions declare -f >> "$newfinal"
@@ -1134,12 +1142,13 @@ Limited, catastrophe-avoiding mechanism is in place even when the test is not run in test watcher, but that should be seen as a backup and such situation is to be avoided whenever possible.
-Since the cleanup script runs as a separate script, the environment -IS NOT SHARED, except for BEAKERLIB_DIR, which is exported explicitly upon -cleanup script generation - that means no other variables are shared between -the test and the cleanup script, therefore make sure to add only fully expanded -strings, not variable names. - +The cleanup script shares all environment (variables, exported or not, and +functions) with the test itself - the cleanup append/prepend functions "sample" +or "snapshot" the environment at the time of their call, IOW any changes to the +test environment are synchronized to the cleanup script only upon calling +append/prepend. +When the append/prepend functions are called within a function which has local +variables, these will appear as global in the cleanup. =cut
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Since we extract the environment incl. all non-exported variables, this includes even local ones, some of which might conflict with what the user defined in their test.
Attempt to avoid name collision while still providing decent code readability.
Signed-off-by: Jiri Jaburek jjaburek@redhat.com --- src/infrastructure.sh | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-)
diff --git a/src/infrastructure.sh b/src/infrastructure.sh index 3d05f57..0f0526d 100644 --- a/src/infrastructure.sh +++ b/src/infrastructure.sh @@ -116,51 +116,51 @@ __INTERNAL_Mount(){
__INTERNAL_rlCleanupGenFinal() { - local varname= - local newfinal="$__INTERNAL_CLEANUP_FINAL".tmp - if [ -e "$newfinal" ]; then - rm -f "$newfinal" || return 1 + local __varname= + local __newfinal="$__INTERNAL_CLEANUP_FINAL".tmp + if [ -e "$__newfinal" ]; then + rm -f "$__newfinal" || return 1 fi - touch "$newfinal" || return 1 + touch "$__newfinal" || return 1
# head - cat > "$newfinal" <<EOF + cat > "$__newfinal" <<EOF #!/bin/bash EOF
# environment # - variables (local, global and env) - for varname in $(compgen -v); do - varname=$(declare -p "$varname") + for __varname in $(compgen -v); do + __varname=$(declare -p "$__varname") # declaration of a readonly variable may fail if a variable with # the same name is already declared - silently ignore it - if expr + "$varname" : "declare -[^r]*r[^r]* " >/dev/null; then - echo "$varname" "2>/dev/null" >> "$newfinal" + if expr + "$__varname" : "declare -[^r]*r[^r]* " >/dev/null; then + echo "$__varname" "2>/dev/null" >> "$__newfinal" else - echo "$varname" >> "$newfinal" + echo "$__varname" >> "$__newfinal" fi done # - functions - declare -f >> "$newfinal" + declare -f >> "$__newfinal"
# journal/phase start - cat >> "$newfinal" <<EOF + cat >> "$__newfinal" <<EOF rlJournalStart rlPhaseStartCleanup EOF
# body - cat "$__INTERNAL_CLEANUP_BUFF" >> "$newfinal" + cat "$__INTERNAL_CLEANUP_BUFF" >> "$__newfinal"
# tail - cat >> "$newfinal" <<EOF + cat >> "$__newfinal" <<EOF rlPhaseEnd rlJournalEnd EOF
- chmod +x "$newfinal" || return 1 + chmod +x "$__newfinal" || return 1 # atomic move - mv "$newfinal" "$__INTERNAL_CLEANUP_FINAL" || return 1 + mv "$__newfinal" "$__INTERNAL_CLEANUP_FINAL" || return 1 }
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
beakerlib-devel@lists.fedorahosted.org