1#!/bin/ksh -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or https://opensource.org/licenses/CDDL-1.0.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26
27#
28# Copyright (c) 2016 by Delphix. All rights reserved.
29#
30
31. $STF_SUITE/include/libtest.shlib
32. $STF_SUITE/tests/functional/cli_root/zfs_upgrade/zfs_upgrade.kshlib
33
34#
35# DESCRIPTION:
36#	Executing 'zfs upgrade' command succeeds, it should report
37#	the current system version and list all old-version filesystems.
38#	If no old-version filesystems be founded, it prints out
39#	"All filesystems are formatted with the current version."
40#
41# STRATEGY:
42# 1. Prepare a set of datasets which contain old-version and current version.
43# 2. Execute 'zfs upgrade', verify return 0, and it prints out
44#	the current system version and list all old-version filesystems.
45# 3. Remove all old-version filesystems, then execute 'zfs upgrade' again,
46#	verify return 0, and get the expected message.
47#
48
49verify_runnable "both"
50
51function cleanup
52{
53	datasetexists $rootfs && destroy_dataset $rootfs -Rf
54	log_must zfs create $rootfs
55
56	for file in $output $oldoutput ; do
57		if [[ -f $file ]]; then
58			log_must rm -f $file
59		fi
60	done
61}
62
63log_assert "Executing 'zfs upgrade' command succeeds."
64log_onexit cleanup
65
66rootfs=$TESTPOOL/$TESTFS
67typeset output=$TEST_BASE_DIR/zfs-versions.$$
68typeset oldoutput=$TEST_BASE_DIR/zfs-versions-old.$$
69typeset expect_str1="This system is currently running ZFS filesystem version"
70typeset expect_str2="All filesystems are formatted with the current version"
71typeset expect_str3="The following filesystems are out of date, and can be upgraded"
72typeset -i COUNT OLDCOUNT
73
74zfs upgrade | awk '$1 ~ "^[0-9]+$" {print $2}'> $oldoutput
75OLDCOUNT=$(wc -l < $oldoutput)
76
77old_datasets=""
78for version in $ZFS_ALL_VERSIONS ; do
79	typeset verfs
80	eval verfs=\$ZFS_VERSION_$version
81	typeset current_fs=$rootfs/$verfs
82	typeset current_snap=${current_fs}@snap
83	typeset current_clone=$rootfs/clone$verfs
84	log_must zfs create -o version=${version} ${current_fs}
85	log_must zfs snapshot ${current_snap}
86	log_must zfs clone ${current_snap} ${current_clone}
87
88	if (( version != $ZFS_VERSION )); then
89		old_datasets="$old_datasets ${current_fs} ${current_clone}"
90	fi
91done
92
93if is_global_zone; then
94	log_must zfs create -V 100m $rootfs/$TESTVOL
95fi
96
97log_must eval 'zfs upgrade > $output 2>&1'
98
99# we also check that the usage message contains at least a description
100# of the current ZFS version.
101log_must grep -q "${expect_str1} $ZFS_VERSION" $output
102zfs upgrade | awk '$1 ~ "^[0-9]+$" {print $2}'> $output
103COUNT=$(wc -l < $output)
104
105typeset -i i=0
106for fs in ${old_datasets}; do
107	log_must grep "^$fs$" $output
108	(( i = i + 1 ))
109done
110
111if (( i != COUNT - OLDCOUNT )); then
112	cat $output
113	log_fail "More old-version filesystems print out than expect."
114fi
115
116for fs in $old_datasets ; do
117	datasetexists $fs && destroy_dataset $fs -Rf
118done
119
120log_must eval 'zfs upgrade > $output 2>&1'
121log_must grep -q "${expect_str1} $ZFS_VERSION" $output
122if (( OLDCOUNT == 0 )); then
123	log_must grep -q "${expect_str2}" $output
124else
125	log_must grep -q "${expect_str3}" $output
126fi
127zfs upgrade | awk '$1 ~ "^[0-9]+$" {print $2}'> $output
128COUNT=$(wc -l < $output)
129
130if (( COUNT != OLDCOUNT )); then
131	cat $output
132	log_fail "Unexpected old-version filesystems print out."
133fi
134
135log_pass "Executing 'zfs upgrade' command succeeds."
136