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 2007 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28#
29# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
30#
31
32. $STF_SUITE/include/libtest.shlib
33. $STF_SUITE/tests/functional/history/history_common.kshlib
34
35#
36# DESCRIPTION:
37#	Verify command history moves with pool while pool being migrated
38#
39# STRATEGY:
40#	1. Import uniform platform and cross platform pools
41#	2. Contract the command history of the imported pool
42#	3. Compare imported history log with the previous log.
43#
44
45verify_runnable "global"
46
47function cleanup
48{
49	poolexists $migratedpoolname &&  \
50		log_must zpool destroy -f $migratedpoolname
51
52	rm -rf $import_dir
53}
54
55log_assert "Verify command history moves with migrated pool."
56log_onexit cleanup
57
58tst_dir=$STF_SUITE/tests/functional/history
59import_dir=$TESTDIR/importdir.$$
60migrated_cmds_f=$import_dir/migrated_history.$$
61migratedpoolname=$MIGRATEDPOOLNAME
62typeset -i RET=1
63typeset -i linenum=0
64
65[[ ! -d $import_dir ]] && log_must mkdir -p $import_dir
66
67# We test the migrations on both uniform platform and cross platform
68for arch in "i386" "sparc"; do
69	log_must cp $tst_dir/${arch}.orig_history.txt $import_dir
70	orig_cmds_f=$import_dir/${arch}.orig_history.txt
71	# remove blank line
72	orig_cmds_f1=$import_dir/${arch}.orig_history_1.txt
73	grep -v "^$" $orig_cmds_f > $orig_cmds_f1
74
75	log_must cp $tst_dir/${arch}.migratedpool.DAT.Z $import_dir
76	log_must uncompress -f $import_dir/${arch}.migratedpool.DAT.Z
77
78	# destroy the pool with same name, so that import operation succeeds.
79	poolexists $migratedpoolname && \
80	    log_must zpool destroy -f $migratedpoolname
81
82	log_must zpool import -d $import_dir $migratedpoolname
83	log_must eval "TZ=$TIMEZONE zpool history $migratedpoolname | grep -v \"^\$\" >$migrated_cmds_f"
84
85	# The migrated history file should differ with original history file on
86	# two commands -- 'export' and 'import', which are included in migrated
87	# history file but not in original history file. so, check the two
88	# commands firstly in migrated history file and then delete them, and
89	# then compare this filtered file with the original history file. They
90	# should be identical at this time.
91	for subcmd in "export" "import"; do
92		grep -q "$subcmd" $migrated_cmds_f ||
93			log_fail "zpool $subcmd is not logged for" \
94			    "the imported pool $migratedpoolname."
95	done
96
97	tmpfile=$import_dir/cmds_tmp.$$
98	linenum=$(wc -l < $migrated_cmds_f)
99	(( linenum = linenum - 2 ))
100	head -n $linenum $migrated_cmds_f > $tmpfile
101	log_must diff $tmpfile $orig_cmds_f1
102
103	# cleanup for next loop testing
104	log_must zpool destroy -f $migratedpoolname
105	log_must rm -f $(ls $import_dir)
106done
107
108log_pass "Verify command history moves with migrated pool."
109