# SPDX-License-Identifier: CDDL-1.0
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or https://opensource.org/licenses/CDDL-1.0.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

#
# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# Copyright (c) 2012, 2017 by Delphix. All rights reserved.
# Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
#

. $STF_SUITE/include/libtest.shlib
. $STF_SUITE/tests/functional/cli_root/zpool_upgrade/zpool_upgrade.cfg

# This part of the test suite relies on variables being setup in the
# zpool_upgrade.cfg script. Those variables give us details about which
# files make up the pool, and what the pool name is.


# A function to import a pool from files we have stored in the test suite
# We import the pool, and create some random data in the pool.
# $1 a version number we can use to get information about the pool
function create_old_pool
{
	typeset vers=$1
	typeset -n pool_files=ZPOOL_VERSION_${vers}_FILES
	typeset -n pool_name=ZPOOL_VERSION_${vers}_NAME

	log_note "Creating $pool_name from $pool_files"
	for pool_file in $pool_files; do
		log_must bzcat \
		    $STF_SUITE/tests/functional/cli_root/zpool_upgrade/blockfiles/$pool_file.bz2 \
		    >$TEST_BASE_DIR/$pool_file
	done
	log_must zpool import -d $TEST_BASE_DIR $pool_name

	# Put some random contents into the pool
	for i in {1..1024} ; do
		dd if=/dev/urandom of=/$pool_name/random.$i \
		    count=1 bs=1024 > /dev/null 2>&1
	done
}


# A function to check the contents of a pool, upgrade it to the current version
# and then verify that the data is consistent after upgrading. Note that we're
# not using "zpool status -x" to see if the pool is healthy, as it's possible
# to also upgrade faulted, or degraded pools.
# $1 a version number we can use to get information about the pool
function check_upgrade
{
	typeset vers=$1
	typeset -n pool_files=ZPOOL_VERSION_${vers}_FILES
	typeset -n pool_name=ZPOOL_VERSION_${vers}_NAME
	typeset pre_upgrade_checksum
	typeset post_upgrade_checksum

	log_note "Checking if we can upgrade from ZFS version $vers"
	pre_upgrade_checksum=$(check_pool $pool_name pre)
	log_must zpool upgrade $pool_name
	post_upgrade_checksum=$(check_pool $pool_name post)

	log_note "Checking that there are no differences between checksum output"
	log_must diff $pre_upgrade_checksum $post_upgrade_checksum
	rm $pre_upgrade_checksum $post_upgrade_checksum
}

# A function to destroy an upgraded pool, plus the files it was based on.
# $1 a version number we can use to get information about the pool
function destroy_upgraded_pool
{
	typeset vers=$1
	typeset -n pool_files=ZPOOL_VERSION_${vers}_FILES
	typeset -n pool_name=ZPOOL_VERSION_${vers}_NAME

	destroy_pool $pool_name

	for file in $pool_files; do
		rm -f $TEST_BASE_DIR/$file
	done
}

# This function does a basic sanity check on the pool by computing the
# checksums of all files in the pool, echoing the name of the file containing
# the checksum results.
# $1 the name of the pool
# $2 a flag we can use to determine when this check is being performed
#    (ie. pre or post pool-upgrade)
function check_pool
{
	typeset pool=$1
	typeset flag=$2
	find /$pool -type f -exec cksum {} + > \
		$TEST_BASE_DIR/pool-checksums.$pool.$flag
	echo $TEST_BASE_DIR/pool-checksums.$pool.$flag
}

# This function simply checks that a pool has a particular version number
# as reported by zdb and zpool upgrade -v
# $1 the name of the pool
# $2 the version of the pool we expect to see
function check_poolversion
{
	typeset pool=$1
	typeset vers=$2
	typeset actual

	# check version using zdb
	actual=$(zdb -C $pool | sed -n 's/^.*version: \(.*\)$/\1/p')
	if [[ $actual != $vers ]] ; then
		log_fail "$pool: zdb reported version $actual, expected $vers"
	fi

	# check version using zpool upgrade
	actual=$(zpool upgrade | awk -v p="$pool$" '$0 ~ p {gsub(/ /, "", $1); print $1}')
	if [[ $actual != $vers ]] ; then
		log_fail "$pool: zpool reported version $actual, expected $vers"
	fi
}
