summaryrefslogtreecommitdiff
path: root/usr/src/test/zfs-tests/tests/functional/channel_program/channel_common.kshlib
blob: 8398c24d178da32fae00a0749fe335f826f2e37c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/bin/ksh
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright (c) 2016, 2017 by Delphix. All rights reserved.
#

. $STF_SUITE/include/libtest.shlib

ZCP_ROOT=$STF_SUITE/tests/functional/channel_program

#
# Note: In case of failure (log_fail) in this function
# we delete the file passed as <input file> so the
# test suite doesn't leak temp files on failures. So it
# is expected that <input file> is a temp file and not
# an installed file.
#
# <exitcode> <expected error string> <input file> <zfs program args>
# e.g. log_program 0 "" tmp.7a12V $POOL foo.zcp arg1 arg2
function log_program
{
	typeset expectexit=$1
	shift
	typeset expecterror=$1
	shift
	typeset tmpin=$1
	shift
	typeset cmdargs=$@ tmpout=$(mktemp) tmperr=$(mktemp)

	# Expected output/error filename is the same as the .zcp name
	typeset basename
	if [[ $2 != "-" ]]; then
		basename=${2%.*}
	fi

	log_note "running: zfs program $cmdargs:"

	zfs program $cmdargs >$tmpout 2>$tmperr
	typeset ret=$?

	log_note "input:\n$(cat $tmpin)"
	log_note "output:\n$(cat $tmpout)"
	log_note "error:\n$(cat $tmperr)"

	#
	# Verify correct return value
	#
	if [[ $ret -ne $expectexit ]]; then
		rm $tmpout $tmperr $tmpin
		log_fail "return mismatch: expected $expectexit, got $ret"
	fi

	#
	# Check the output or reported error for successful or error returns,
	# respectively.
	#
	if [[ -f "$basename.out" ]] && [[ $expectexit -eq 0 ]]; then

		outdiff=$(diff "$basename.out" "$tmpout")
		if [[ $? -ne 0 ]]; then
			output=$(cat $tmpout)
			rm $tmpout $tmperr $tmpin
			log_fail "Output mismatch. Expected:\n" \
				"$(cat $basename.out)\nBut got:\n$output\n" \
				"Diff:\n$outdiff"
		fi

	elif [[ -f "$basename.err" ]] && [[ $expectexit -ne 0 ]]; then

		outdiff=$(diff "$basename.err" "$tmperr")
		if [[ $? -ne 0 ]]; then
			outputerror=$(cat $tmperr)
			rm $tmpout $tmperr $tmpin
			log_fail "Error mismatch. Expected:\n" \
				"$(cat $basename.err)\nBut got:\n$outputerror\n" \
				"Diff:\n$outdiff"
		fi

	elif [[ -n $expecterror ]] && [[ $expectexit -ne 0 ]]; then

		grep -q "$expecterror" $tmperr
		if [[ $? -ne 0 ]]; then
			outputerror=$(cat $tmperr)
			rm $tmpout $tmperr $tmpin
			log_fail "Error mismatch. Expected to contain:\n" \
				"$expecterror\nBut got:\n$outputerror\n"
		fi

	elif [[ $expectexit -ne 0 ]]; then
		#
		# If there's no expected output, error reporting is allowed to
		# vary, but ensure that we didn't fail silently.
		#
		if [[ -z "$(cat $tmperr)" ]]; then
			rm $tmpout $tmperr $tmpin
			log_fail "error with no stderr output"
		fi
	fi

	#
	# Clean up all temp files except $tmpin which is
	# reused for the second invocation of log_program.
	#
	rm $tmpout $tmperr
}

#
# Even though the command's arguments are passed correctly
# to the log_must_program family of wrappers the majority
# of the time, zcp scripts passed as HERE documents can
# make things trickier (see comment within the fucntion
# below) in the ordering of the commands arguments and how
# they are passed. Thus, with this function we reconstruct
# them to ensure that they are passed properly.
#
function log_program_construct_args
{
	typeset tmpin=$1
	shift

	args=""
	i=0
	while getopts "nt:m:" opt; do
		case $opt in
			t) args=$args" -t $OPTARG"; i=$(($i + 2)) ;;
			m) args=$args" -m $OPTARG"; i=$(($i + 2)) ;;
			n) args=$args" -n"; i=$(($i + 1)) ;;
		esac
	done
	shift $i

	pool=$1
	shift

	#
	# Catch HERE document if it exists and save it within our
	# temp file. The reason we do this is that since the
	# log_must_program wrapper calls zfs-program twice (once
	# for open context and once for syncing) the HERE doc
	# is consumed in the first invocation and the second one
	# does not have a program to run.
	#
	cat > $tmpin

	#
	# If $tmpin has contents it means that we consumed a HERE
	# doc and $1 currently holds "-" (a dash). If there is no
	# HERE doc and $tmpin is empty, then we copy the contents
	# of the original channel program to $tmpin.
	#
	[[ -s $tmpin ]] || cp $1 $tmpin
	shift

	lua_args=$@

	echo "$args $pool $tmpin $lua_args"
}

#
# Program should complete successfully
# when run in either context.
#
function log_must_program
{
	typeset tmpin=$(mktemp)

	program_args=$(log_program_construct_args $tmpin $@)

	log_program 0 "" $tmpin "-n $program_args"
	log_program 0 "" $tmpin "$program_args"

	rm $tmpin
}
#
# Program should error as expected in
# the same way in both contexts.
#
function log_mustnot_checkerror_program
{
	typeset expecterror=$1
	shift
	typeset tmpin=$(mktemp)

	program_args=$(log_program_construct_args $tmpin $@)

	log_program 1 "$expecterror" $tmpin "-n $program_args"
	log_program 1 "$expecterror" $tmpin "$program_args"

	rm $tmpin
}

#
# Program should fail when run in either
# context.
#
function log_mustnot_program
{
	log_mustnot_checkerror_program "" $@
}


#
# Program should error as expected in
# open context but complete successfully
# in syncing context.
#
function log_mustnot_checkerror_program_open
{
	typeset expecterror=$1
	shift
	typeset tmpin=$(mktemp)

	program_args=$(log_program_construct_args $tmpin $@)

	log_program 1 "$expecterror" $tmpin "-n $program_args"
	log_program 0 "" $tmpin "$program_args"

	rm $tmpin
}

#
# Program should complete successfully
# when run in syncing context but fail
# when attempted to run in open context.
#
function log_must_program_sync
{
	log_mustnot_checkerror_program_open "requires passing sync=TRUE" $@
}