1#!/bin/ksh -p
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) 2017 by Delphix. All rights reserved.
15#
16
17. $STF_SUITE/tests/functional/channel_program/channel_common.kshlib
18
19#
20# DESCRIPTION:
21#       Listing zfs holds should work correctly.
22#
23
24verify_runnable "global"
25
26TESTHOLD=testhold-tag
27TESTHOLD1=$TESTHOLD-1
28TESTHOLD2=$TESTHOLD-2
29TESTHOLD3=$TESTHOLD-3
30SNAP=$TESTPOOL/$TESTFS@$TESTSNAP
31
32function cleanup
33{
34	holdexists $TESTHOLD $SNAP && log_must zfs release $TESTHOLD $SNAP
35	holdexists $TESTHOLD1 $SNAP && log_must zfs release $TESTHOLD1 $SNAP
36	holdexists $TESTHOLD2 $SNAP && log_must zfs release $TESTHOLD2 $SNAP
37	holdexists $TESTHOLD3 $SNAP && log_must zfs release $TESTHOLD3 $SNAP
38	destroy_snapshot
39}
40
41log_onexit cleanup
42
43create_snapshot
44
45# 0 holds handled correctly
46log_must_program $TESTPOOL - <<-EOF
47	n = 0
48	for s in zfs.list.holds("$SNAP") do
49		n = n + 1
50	end
51	assert(n == 0)
52	return 0
53EOF
54
55# Create a hold
56log_must zfs hold $TESTHOLD $SNAP
57
58log_must_program $TESTPOOL - <<-EOF
59	n = 0
60	for s in zfs.list.holds("$SNAP") do
61		assert(s == "$TESTHOLD")
62		n = n + 1
63	end
64	assert(n == 1)
65	return 0
66EOF
67
68log_must zfs hold $TESTHOLD1 $SNAP
69log_must zfs hold $TESTHOLD2 $SNAP
70log_must zfs hold $TESTHOLD3 $SNAP
71
72# All holds appear exactly once
73log_must_program $TESTPOOL - <<-EOF
74	a = {}
75	a["$TESTHOLD"] = false
76	a["$TESTHOLD1"] = false
77	a["$TESTHOLD2"] = false
78	a["$TESTHOLD3"] = false
79	n = 0
80	for s in zfs.list.holds("$SNAP") do
81		assert(not a[s])
82		a[s] = true
83		n = n + 1
84	end
85	assert(n == 4)
86	assert(a["$TESTHOLD"] and
87	    a["$TESTHOLD1"] and
88	    a["$TESTHOLD2"] and
89	    a["$TESTHOLD3"])
90	return 0
91EOF
92
93# Nonexistent input
94log_mustnot_program $TESTPOOL - <<-EOF
95	zfs.list.holds("$TESTPOOL/nonexistent-fs@nonexistent-snap")
96	return 0
97EOF
98log_mustnot_program $TESTPOOL - <<-EOF
99	zfs.list.holds("nonexistent-pool/$TESTFS")
100	return 0
101EOF
102
103# Can't look in a different pool than the one specified on command line
104log_mustnot_program $TESTPOOL - <<-EOF
105	zfs.list.holds("testpool2")
106	return 0
107EOF
108
109# Can't have holds on filesystems
110log_mustnot_program $TESTPOOL - <<-EOF
111	zfs.list.holds("$TESTPOOL/$TESTFS")
112	return 0
113EOF
114
115# Can't have holds on bookmarks
116log_mustnot_program $TESTPOOL - <<-EOF
117	zfs.list.holds("$TESTPOOL/$TESTFS#bookmark")
118	return 0
119EOF
120
121log_pass "Listing zfs holds should work correctly."
122