xref: /illumos-gate/usr/src/test/zfs-tests/tests/functional/channel_program/channel_common.kshlib (revision 5f82aa32fbc5dc2c59bca6ff315f44a4c4c9ea86)
1#!/bin/ksh
2#
3# This file and its contents are supplied under the terms of the
4# Common Development and Distribution License ("CDDL"), version 1.0.
5# You may only use this file in accordance with the terms of version
6# 1.0 of the CDDL.
7#
8# A full copy of the text of the CDDL should have accompanied this
9# source.  A copy of the CDDL is also available via the Internet at
10# http://www.illumos.org/license/CDDL.
11#
12
13#
14# Copyright (c) 2016 by Delphix. All rights reserved.
15#
16
17. $STF_SUITE/include/libtest.shlib
18
19ZCP_ROOT=$STF_SUITE/tests/functional/channel_program
20
21# <exitcode> <expected error string> <zfs program args>
22# e.g. log_program 0 $POOL foo.zcp arg1 arg2
23function log_program
24{
25	typeset expectexit=$1
26	shift
27	typeset expecterror=$1
28	shift
29	typeset cmdargs=$@ tmpout=$(mktemp) tmperr=$(mktemp) tmpin=$(mktemp)
30
31	# Expected output/error filename is the same as the .zcp name
32	typeset basename
33	if [[ $2 != "-" ]]; then
34		basename=${2%.*}
35	fi
36
37	log_note "running: zfs program $cmdargs:"
38
39	tee $tmpin | zfs program $cmdargs >$tmpout 2>$tmperr
40	typeset ret=$?
41
42	log_note "input:\n$(cat $tmpin)"
43	log_note "output:\n$(cat $tmpout)"
44	log_note "error:\n$(cat $tmperr)"
45	# verify correct return value
46	if [[ $ret -ne $expectexit ]]; then
47		log_fail "return mismatch: expected $expectexit, got $ret"
48	fi
49
50	#
51	# Check the output or reported error for successful or error returns,
52	# respectively.
53	if [[ -f "$basename.out" ]] && [[ $expectexit -eq 0 ]]; then
54
55		outdiff=$(diff "$basename.out" "$tmpout")
56		[[ $? -ne 0 ]] && log_fail "Output mismatch. Expected:\n" \
57		    "$(cat $basename.out)\nBut got:$(cat $tmpout)\n" \
58		    "Diff:\n$outdiff"
59
60	elif [[ -f "$basename.err" ]] && [[ $expectexit -ne 0 ]]; then
61
62		outdiff=$(diff "$basename.err" "$tmperr")
63		[[ $? -ne 0 ]] && log_fail "Error mismatch. Expected:\n" \
64		    "$(cat $basename.err)\nBut got:$(cat $tmpout)\n" \
65		    "Diff:\n$outdiff"
66
67	elif [[ -n $expecterror ]] && [[ $expectexit -ne 0 ]]; then
68
69		grep -q "$expecterror" $tmperr || \
70		    log_fail "Error mismatch. Expected to contain:\n" \
71		    "$expecterror\nBut got:$(cat $tmpout)\n"
72
73	elif [[ $expectexit -ne 0 ]]; then
74		#
75		# If there's no expected output, error reporting is allowed to
76		# vary, but ensure that we didn't fail silently.
77		#
78		[[ -z "$(cat $tmperr)" ]] && \
79		    log_fail "error with no stderr output"
80	fi
81
82	rm $tmpout $tmperr
83}
84
85function log_must_program
86{
87	log_program 0 "" "$@"
88}
89
90function log_mustnot_program
91{
92	log_program 1 "" "$@"
93}
94
95function log_mustnot_checkerror_program
96{
97	typeset expecterror=$1
98	shift
99	log_program 1 "$expecterror" "$@"
100}
101