1#!/usr/local/bin/ksh93 -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# $FreeBSD$
24
25#
26# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
27# Use is subject to license terms.
28#
29# ident	"@(#)zfs_upgrade_001_pos.ksh	1.2	08/08/15 SMI"
30#
31#
32. $STF_SUITE/include/libtest.kshlib
33. $STF_SUITE/tests/cli_root/zfs_upgrade/zfs_upgrade.kshlib
34
35################################################################################
36#
37# __stc_assertion_start
38#
39# ID: zfs_upgrade_001_pos
40#
41# DESCRIPTION:
42# 	Executing 'zfs upgrade' command succeeds, it should report
43#	the current system version and list all old-version filesystems.
44#	If no old-version filesystems be founded, it prints out
45#	"All filesystems are formatted with the current version."
46#
47# STRATEGY:
48# 1. Prepare a set of datasets which contain old-version and current version.
49# 2. Execute 'zfs upgrade', verify return 0, and it prints out
50#	the current system version and list all old-version filesystems.
51# 3. Remove all old-version filesystems, then execute 'zfs upgrade' again,
52#	verify return 0, and get the expected message.
53#
54# TESTABILITY: explicit
55#
56# TEST_AUTOMATION_LEVEL: automated
57#
58# CODING_STATUS: COMPLETED (2007-06-25)
59#
60# __stc_assertion_end
61#
62################################################################################
63
64verify_runnable "both"
65
66function cleanup
67{
68	if datasetexists $rootfs ; then
69		log_must $ZFS destroy -Rf $rootfs
70	fi
71	log_must $ZFS create $rootfs
72
73	for file in $output $oldoutput ; do
74		if [[ -f $file ]]; then
75			log_must $RM -f $file
76		fi
77	done
78}
79
80log_assert "Executing 'zfs upgrade' command succeeds."
81log_onexit cleanup
82
83rootfs=$TESTPOOL/$TESTFS
84typeset output=$TMPDIR/zfs-versions.${TESTCASE_ID}
85typeset oldoutput=$TMPDIR/zfs-versions-old.${TESTCASE_ID}
86typeset expect_str1="This system is currently running ZFS filesystem version"
87typeset expect_str2="All filesystems are formatted with the current version"
88typeset expect_str3="The following filesystems are out of date, and can be upgraded"
89typeset -i COUNT OLDCOUNT
90
91$ZFS upgrade | $NAWK '$1 ~ "^[0-9]+$" {print $2}'> $oldoutput
92OLDCOUNT=$( $WC -l $oldoutput | $AWK '{print $1}' )
93
94old_datasets=""
95for version in $ZFS_ALL_VERSIONS ; do
96	typeset verfs
97	eval verfs=\$ZFS_VERSION_$version
98	typeset current_fs=$rootfs/$verfs
99	typeset current_snap=${current_fs}@snap
100	typeset current_clone=$rootfs/clone$verfs
101	log_must $ZFS create -o version=${version} ${current_fs}
102	log_must $ZFS snapshot ${current_snap}
103	log_must $ZFS clone ${current_snap} ${current_clone}
104
105	if (( version != $ZFS_VERSION )); then
106		old_datasets="$old_datasets ${current_fs} ${current_clone}"
107	fi
108done
109
110if is_global_zone; then
111	log_must $ZFS create -V 100m $rootfs/$TESTVOL
112fi
113
114log_must eval '$ZFS upgrade > $output 2>&1'
115
116# we also check that the usage message contains at least a description
117# of the current ZFS version.
118log_must eval '$GREP "${expect_str1} $ZFS_VERSION" $output > /dev/null 2>&1'
119$ZFS upgrade | $NAWK '$1 ~ "^[0-9]+$" {print $2}'> $output
120COUNT=$( $WC -l $output | $AWK '{print $1}' )
121
122typeset -i i=0
123for fs in ${old_datasets}; do
124	log_must $GREP "^$fs$" $output
125	(( i = i + 1 ))
126done
127
128if (( i != COUNT - OLDCOUNT )); then
129	$CAT $output
130	log_fail "More old-version filesystems print out than expect."
131fi
132
133for fs in $old_datasets ; do
134	if datasetexists $fs ; then
135		log_must $ZFS destroy -Rf $fs
136	fi
137done
138
139log_must eval '$ZFS upgrade > $output 2>&1'
140log_must eval '$GREP "${expect_str1} $ZFS_VERSION" $output > /dev/null 2>&1'
141if (( OLDCOUNT == 0 )); then
142	log_must eval '$GREP "${expect_str2}" $output > /dev/null 2>&1'
143else
144	log_must eval '$GREP "${expect_str3}" $output > /dev/null 2>&1'
145fi
146$ZFS upgrade | $NAWK '$1 ~ "^[0-9]+$" {print $2}'> $output
147COUNT=$( $WC -l $output | $AWK '{print $1}' )
148
149if (( COUNT != OLDCOUNT )); then
150	$CAT $output
151	log_fail "Unexpect old-version filesystems print out."
152fi
153
154log_pass "Executing 'zfs upgrade' command succeeds."
155