]> git.proxmox.com Git - mirror_zfs.git/blame - tests/zfs-tests/tests/functional/events/events_common.kshlib
Verify ZFS Test Suite scripts executability
[mirror_zfs.git] / tests / zfs-tests / tests / functional / events / events_common.kshlib
CommitLineData
3f03fc8d
BB
1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21
22#
23# Copyright (c) 2017 by Lawrence Livermore National Security, LLC.
24# Use is subject to license terms.
25#
26
27. $STF_SUITE/include/libtest.shlib
28. $STF_SUITE/tests/functional/events/events.cfg
29
d5e024cb
BB
30#
31# Wait for up to 'timeout' seconds for the 'file' to settle, i.e.
32# not be updated for a period of 'delay' seconds.
33#
34function file_wait # file delay timeout
35{
36 file=$1
37 delay=${2:-3}
38 timeout=${3:-120}
39
40 SECONDS=0
41
42 while [ $(( $(date +%s) - $(stat -c %Y $file) )) -lt $delay ]; do
43 if [[ $SECONDS -gt $timeout ]]; then
44 return 1
45 fi
46
47 sleep 1
48 done
49
50 return 0;
51}
52
3f03fc8d
BB
53function run_and_verify
54{
d5e024cb 55 typeset delay event pool zedlog
3f03fc8d
BB
56 set -A events
57
d5e024cb 58 while getopts "d:e:p:z:" opt; do
3f03fc8d 59 case $opt in
d5e024cb
BB
60 d)
61 delay=$OPTARG
62 ;;
3f03fc8d
BB
63 e)
64 events[${#events[*]}+1]=$OPTARG
65 ;;
66 p)
67 pool=$OPTARG
68 ;;
69 z)
70 zedlog=$OPTARG
71 ;;
72 esac
73 done
74 shift $(($OPTIND - 1))
75
76 pool=${pool:-$TESTPOOL}
d5e024cb
BB
77 delay=${delay:-3}
78 zedlog=${zedlog:-$ZED_DEBUG_LOG}
3f03fc8d
BB
79 fullcmd="$1"
80 cmd=$(echo $fullcmd | awk '{print $1}')
3f03fc8d
BB
81
82 # If we aren't running zpool or zfs, something is wrong
83 [[ $cmd == "zpool" || $cmd == "zfs" ]] || \
84 log_fail "run_and_verify called with \"$cmd ($fullcmd)\""
85
d5e024cb
BB
86 log_note "Checking events for command: '$fullcmd'"
87
88 # Remove any previous events from the logs.
3f03fc8d 89 log_must zpool events -c
d5e024cb 90 log_must truncate -s 0 $zedlog
3f03fc8d 91
d5e024cb 92 # Run the command as provided.
3f03fc8d
BB
93 log_must eval "$fullcmd"
94
d5e024cb
BB
95 # Collect the new events and verify there are some.
96 log_must zpool sync -f
97 log_must file_wait $zedlog $delay
98 log_must cp $zedlog $TMP_EVENTS_ZED
99 log_must eval "zpool events >$TMP_EVENTS 2>/dev/null"
100 log_must eval "zpool events -v > $TMP_EVENTS_FULL 2>/dev/null"
101
102 log_must test -s $TMP_EVENTS
103 log_must test -s $TMP_EVENTS_FULL
104 log_must test -s $TMP_EVENTS_ZED
105
106 log_note "Events generated:"
107 cat $TMP_EVENTS
3f03fc8d
BB
108
109 # Verify all the expected events appear in the log.
110 for event in ${events[*]}; do
111
112 # Verify the event is in in the short output.
113 log_must grep -q "$event" $TMP_EVENTS
114
115 # Verify the event is in the verbose output with pool name.
d5e024cb 116 awk -v event="$event" \
3f03fc8d
BB
117 'BEGIN{FS="\n"; RS=""} $0 ~ event { print $0 }' \
118 $TMP_EVENTS_FULL >$TMP_EVENT_FULL
119 log_must grep -q "pool = \"$pool\"" $TMP_EVENT_FULL
120
121 # Verify the event was received by the ZED and logged.
d5e024cb 122 awk -v event="$event" \
3f03fc8d
BB
123 'BEGIN{FS="\n"; RS=""} $0 ~ event { print $0 }' \
124 $TMP_EVENTS_ZED >$TMP_EVENT_ZED
125 log_must grep -q "^ZEVENT_POOL=$pool" $TMP_EVENT_ZED
3f03fc8d
BB
126 done
127
d5e024cb
BB
128 rm -f $TMP_EVENTS $TMP_EVENTS_FULL $TMP_EVENT_FULL \
129 $TMP_EVENTS_ZED $TMP_EVENT_ZED
3f03fc8d 130}