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