1#!/usr/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 http://www.opensolaris.org/os/licensing.
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	if datasetexists $rootfs ; then
54		log_must zfs destroy -Rf $rootfs
55	fi
56	log_must zfs create $rootfs
57
58	for file in $output $oldoutput ; do
59		if [[ -f $file ]]; then
60			log_must rm -f $file
61		fi
62	done
63}
64
65log_assert "Executing 'zfs upgrade' command succeeds."
66log_onexit cleanup
67
68rootfs=$TESTPOOL/$TESTFS
69typeset output=/tmp/zfs-versions.$$
70typeset oldoutput=/tmp/zfs-versions-old.$$
71typeset expect_str1="This system is currently running ZFS filesystem version"
72typeset expect_str2="All filesystems are formatted with the current version"
73typeset expect_str3="The following filesystems are out of date, and can be upgraded"
74typeset -i COUNT OLDCOUNT
75
76zfs upgrade | nawk '$1 ~ "^[0-9]+$" {print $2}'> $oldoutput
77OLDCOUNT=$( wc -l $oldoutput | awk '{print $1}' )
78
79old_datasets=""
80for version in $ZFS_ALL_VERSIONS ; do
81	typeset verfs
82	eval verfs=\$ZFS_VERSION_$version
83	typeset current_fs=$rootfs/$verfs
84	typeset current_snap=${current_fs}@snap
85	typeset current_clone=$rootfs/clone$verfs
86	log_must zfs create -o version=${version} ${current_fs}
87	log_must zfs snapshot ${current_snap}
88	log_must zfs clone ${current_snap} ${current_clone}
89
90	if (( version != $ZFS_VERSION )); then
91		old_datasets="$old_datasets ${current_fs} ${current_clone}"
92	fi
93done
94
95if is_global_zone; then
96	log_must zfs create -V 100m $rootfs/$TESTVOL
97fi
98
99log_must eval 'zfs upgrade > $output 2>&1'
100
101# we also check that the usage message contains at least a description
102# of the current ZFS version.
103log_must eval 'grep "${expect_str1} $ZFS_VERSION" $output > /dev/null 2>&1'
104zfs upgrade | nawk '$1 ~ "^[0-9]+$" {print $2}'> $output
105COUNT=$( wc -l $output | awk '{print $1}' )
106
107typeset -i i=0
108for fs in ${old_datasets}; do
109	log_must grep "^$fs$" $output
110	(( i = i + 1 ))
111done
112
113if (( i != COUNT - OLDCOUNT )); then
114	cat $output
115	log_fail "More old-version filesystems print out than expect."
116fi
117
118for fs in $old_datasets ; do
119	if datasetexists $fs ; then
120		log_must zfs destroy -Rf $fs
121	fi
122done
123
124log_must eval 'zfs upgrade > $output 2>&1'
125log_must eval 'grep "${expect_str1} $ZFS_VERSION" $output > /dev/null 2>&1'
126if (( OLDCOUNT == 0 )); then
127	log_must eval 'grep "${expect_str2}" $output > /dev/null 2>&1'
128else
129	log_must eval 'grep "${expect_str3}" $output > /dev/null 2>&1'
130fi
131zfs upgrade | nawk '$1 ~ "^[0-9]+$" {print $2}'> $output
132COUNT=$( wc -l $output | awk '{print $1}' )
133
134if (( COUNT != OLDCOUNT )); then
135	cat $output
136	log_fail "Unexpect old-version filesystems print out."
137fi
138
139log_pass "Executing 'zfs upgrade' command succeeds."
140