]> git.proxmox.com Git - mirror_zfs.git/blobdiff - tests/zfs-tests/tests/functional/events/events_common.kshlib
Emit history events for 'zpool create'
[mirror_zfs.git] / tests / zfs-tests / tests / functional / events / events_common.kshlib
index 9ef97ca62e9586b6e9b075da249f5562221391d6..5c411936d9570688266fd120c97288799a2c206d 100755 (executable)
 . $STF_SUITE/include/libtest.shlib
 . $STF_SUITE/tests/functional/events/events.cfg
 
+#
+# Wait for up to 'timeout' seconds for the 'file' to settle, i.e.
+# not be updated for a period of 'delay' seconds.
+#
+function file_wait # file delay timeout
+{
+       file=$1
+       delay=${2:-3}
+       timeout=${3:-120}
+
+       SECONDS=0
+
+       while [ $(( $(date +%s) - $(stat -c %Y $file) )) -lt $delay ]; do
+               if [[ $SECONDS -gt $timeout ]]; then
+                       return 1
+               fi
+
+               sleep 1
+       done
+
+       return 0;
+}
+
 function run_and_verify
 {
-       typeset event pool
+       typeset delay event pool zedlog
        set -A events
 
-       while getopts "e:p:z:" opt; do
+       while getopts "d:e:p:z:" opt; do
                case $opt in
+               d)
+                       delay=$OPTARG
+                       ;;
                e)
                        events[${#events[*]}+1]=$OPTARG
                        ;;
@@ -48,7 +74,8 @@ function run_and_verify
        shift $(($OPTIND - 1))
 
        pool=${pool:-$TESTPOOL}
-       zedlog=${zedlog:-$ZEDLET_DIR/zed.debug.log}
+       delay=${delay:-3}
+       zedlog=${zedlog:-$ZED_DEBUG_LOG}
        fullcmd="$1"
        cmd=$(echo $fullcmd | awk '{print $1}')
        subcmd=$(echo $fullcmd | awk '{print $2}')
@@ -57,21 +84,28 @@ function run_and_verify
        [[ $cmd == "zpool" || $cmd == "zfs" ]] || \
            log_fail "run_and_verify called with \"$cmd ($fullcmd)\""
 
-       # Run the command as provided and collect the new events.
+       log_note "Checking events for command: '$fullcmd'"
+
+       # Remove any previous events from the logs.
        log_must zpool events -c
-       if [[ -f $zedlog ]]; then
-               cp -f $zedlog $zedlog.old
-       fi
+       log_must truncate -s 0 $zedlog
 
+       # Run the command as provided.
        log_must eval "$fullcmd"
 
-       log_must zpool events > $TMP_EVENTS 2>/dev/null
-       log_must zpool events -v > $TMP_EVENTS_FULL 2>/dev/null
-       if [[ -f $zedlog ]]; then
-               sleep 5 # Brief delay for the ZED to handle the event.
-               diff $zedlog.old $zedlog | grep "^> " | sed 's/^> //g' \
-                   >$TMP_EVENTS_ZED
-       fi
+       # Collect the new events and verify there are some.
+       log_must zpool sync -f
+       log_must file_wait $zedlog $delay
+       log_must cp $zedlog $TMP_EVENTS_ZED
+       log_must eval "zpool events >$TMP_EVENTS 2>/dev/null"
+       log_must eval "zpool events -v > $TMP_EVENTS_FULL 2>/dev/null"
+
+       log_must test -s $TMP_EVENTS
+       log_must test -s $TMP_EVENTS_FULL
+       log_must test -s $TMP_EVENTS_ZED
+
+       log_note "Events generated:"
+       cat $TMP_EVENTS
 
        # Verify all the expected events appear in the log.
        for event in ${events[*]}; do
@@ -80,19 +114,18 @@ function run_and_verify
                log_must grep -q "$event" $TMP_EVENTS
 
                # Verify the event is in the verbose output with pool name.
-               log_must awk -v event="$event" \
+               awk -v event="$event" \
                    'BEGIN{FS="\n"; RS=""} $0 ~ event { print $0 }' \
                    $TMP_EVENTS_FULL >$TMP_EVENT_FULL
                log_must grep -q "pool = \"$pool\"" $TMP_EVENT_FULL
 
                # Verify the event was received by the ZED and logged.
-               log_must awk -v event="$event" \
+               awk -v event="$event" \
                    'BEGIN{FS="\n"; RS=""} $0 ~ event { print $0 }' \
                    $TMP_EVENTS_ZED >$TMP_EVENT_ZED
                log_must grep -q "^ZEVENT_POOL=$pool" $TMP_EVENT_ZED
-
        done
 
-       log_must rm -f $TMP_EVENTS $TMP_EVENTS_FULL $TMP_EVENT_FULL \
-           $TMP_EVENTS_ZED $TMP_EVENT_ZED $zedlog.old
+       rm -f $TMP_EVENTS $TMP_EVENTS_FULL $TMP_EVENT_FULL \
+           $TMP_EVENTS_ZED $TMP_EVENT_ZED
 }