#1# SPDX-License-Identifier: BSD-2-Clause2#3# Copyright (c) 2019 Rob Wing4#5# Redistribution and use in source and binary forms, with or without6# modification, are permitted provided that the following conditions7# are met:8# 1. Redistributions of source code must retain the above copyright9# notice, this list of conditions and the following disclaimer.10# 2. Redistributions in binary form must reproduce the above copyright11# notice, this list of conditions and the following disclaimer in the12# documentation and/or other materials provided with the distribution.13#14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24# SUCH DAMAGE.25#2627# The code for the following tests was copied from the28# bectl tests found in src/sbin/bectl/tests, modified as needed.2930ZPOOL_NAME_FILE=zpool_name31get_zpool_name()32{33cat $ZPOOL_NAME_FILE34}35make_zpool_name()36{37mktemp -u libbe_test_XXXXXX > $ZPOOL_NAME_FILE38get_zpool_name39}4041# Establishes a libbe zpool that can be used for some light testing; contains42# a 'default' BE and not much else.43libbe_create_setup()44{45zpool=$146disk=$247mnt=$34849# Sanity check to make sure `make_zpool_name` succeeded50atf_check test -n "$zpool"5152kldload -n -q zfs || atf_skip "ZFS module not loaded on the current system"53atf_check mkdir -p ${mnt}54atf_check truncate -s 1G ${disk}55atf_check zpool create -o altroot=${mnt} ${zpool} ${disk}56atf_check zfs create -o mountpoint=none ${zpool}/ROOT57atf_check zfs create -o mountpoint=/ -o canmount=noauto \58${zpool}/ROOT/default59atf_check zfs create -o mountpoint=/usr -o canmount=noauto \60${zpool}/ROOT/default/usr61atf_check zfs create -o mountpoint=/usr/obj -o canmount=noauto \62${zpool}/ROOT/default/usr/obj63}6465libbe_cleanup()66{67zpool=$168cwd=$(atf_get_srcdir)6970if [ -z "$zpool" ]; then71echo "Skipping cleanup; zpool not set up"72elif zpool get health ${zpool} >/dev/null 2>&1; then73zpool destroy -f ${zpool}74fi7576if [ -f "${cwd}/disk.img" ]; then77rm ${cwd}/disk.img78fi79}8081atf_test_case libbe_create cleanup82libbe_create_head()83{84atf_set "descr" "check _be_create from libbe"85atf_set "require.user" root86}87libbe_create_body()88{89if [ "$(atf_config_get ci false)" = "true" ] && \90[ "$(uname -p)" = "i386" ]; then91atf_skip "https://bugs.freebsd.org/249055"92fi9394if [ "$(atf_config_get ci false)" = "true" ] && \95[ "$(uname -p)" = "armv7" ]; then96atf_skip "https://bugs.freebsd.org/249229"97fi9899cwd=$(atf_get_srcdir)100zpool=$(make_zpool_name)101disk=${cwd}/disk.img102mount=${cwd}/mnt103prog=${cwd}/./target_prog104105# preliminary setup/checks106atf_require_prog $prog107libbe_create_setup ${zpool} ${disk} ${mount}108109# a recursive and non-recursive snapshot to test against110atf_check zfs snapshot ${zpool}/ROOT/default@non-recursive111atf_check zfs snapshot -r ${zpool}/ROOT/default@recursive112113# create a dataset after snapshots were taken114atf_check zfs create -o mountpoint=/usr/src -o canmount=noauto \115${zpool}/ROOT/default/usr/src116117# test boot environment creation with depth of 0 (i.e. a non-recursive boot environment).118atf_check $prog "${zpool}/ROOT" \119nonrecursive \120"${zpool}/ROOT/default@non-recursive" \1210122# the dataset should exist123atf_check -o not-empty \124zfs list "${zpool}/ROOT/nonrecursive"125# the child dataset should not exist.126atf_check -e not-empty -s not-exit:0 \127zfs list "${zpool}/ROOT/nonrecursive/usr"128129# test boot environment creation with unlimited depth (i.e. a recursive boot environment).130atf_check $prog "${zpool}/ROOT" \131recursive \132"${zpool}/ROOT/default@recursive" \133-1134# the dataset should exist135atf_check -o not-empty \136zfs list "${zpool}/ROOT/recursive"137# the child dataset should exist138atf_check -o not-empty \139zfs list "${zpool}/ROOT/recursive/usr"140# the child dataset should exist141atf_check -o not-empty \142zfs list "${zpool}/ROOT/recursive/usr/obj"143# the child dataset should not exist.144atf_check -e not-empty -s not-exit:0 \145zfs list "${zpool}/ROOT/recursive/usr/src"146147# test boot environment creation with a depth of 1148atf_check $prog "${zpool}/ROOT" \149depth \150"${zpool}/ROOT/default@recursive" \1511152# the child dataset should exist153atf_check -o not-empty \154zfs list "${zpool}/ROOT/depth/usr"155# the child dataset should not exist.156atf_check -e not-empty -s not-exit:0 \157zfs list "${zpool}/ROOT/depth/usr/obj"158# the child dataset should not exist.159atf_check -e not-empty -s not-exit:0 \160zfs list "${zpool}/ROOT/depth/usr/src"161162163# create a recursive boot environment named 'relative-snap'.164# This test is to ensure that a relative snapshot label can be used,165# (i.e. the format: 'bootenvironment@snapshot')166atf_check $prog "${zpool}/ROOT" \167relative-snap \168default@recursive \169-1170# the dataset should exist171atf_check -o not-empty \172zfs list "${zpool}/ROOT/relative-snap"173# the child dataset should exist174atf_check -o not-empty \175zfs list "${zpool}/ROOT/relative-snap/usr"176}177178libbe_create_cleanup()179{180libbe_cleanup $(get_zpool_name)181}182183atf_init_test_cases()184{185atf_add_test_case libbe_create186}187188189