1cddbc3b4SKyle Evans# 2*4d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause 3cddbc3b4SKyle Evans# 4cddbc3b4SKyle Evans# Copyright (c) 2019 Rob Wing 5cddbc3b4SKyle Evans# 6cddbc3b4SKyle Evans# Redistribution and use in source and binary forms, with or without 7cddbc3b4SKyle Evans# modification, are permitted provided that the following conditions 8cddbc3b4SKyle Evans# are met: 9cddbc3b4SKyle Evans# 1. Redistributions of source code must retain the above copyright 10cddbc3b4SKyle Evans# notice, this list of conditions and the following disclaimer. 11cddbc3b4SKyle Evans# 2. Redistributions in binary form must reproduce the above copyright 12cddbc3b4SKyle Evans# notice, this list of conditions and the following disclaimer in the 13cddbc3b4SKyle Evans# documentation and/or other materials provided with the distribution. 14cddbc3b4SKyle Evans# 15cddbc3b4SKyle Evans# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16cddbc3b4SKyle Evans# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17cddbc3b4SKyle Evans# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18cddbc3b4SKyle Evans# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19cddbc3b4SKyle Evans# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20cddbc3b4SKyle Evans# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21cddbc3b4SKyle Evans# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22cddbc3b4SKyle Evans# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23cddbc3b4SKyle Evans# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24cddbc3b4SKyle Evans# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25cddbc3b4SKyle Evans# SUCH DAMAGE. 26cddbc3b4SKyle Evans# 27cddbc3b4SKyle Evans 28cddbc3b4SKyle Evans# The code for the following tests was copied from the 29cddbc3b4SKyle Evans# bectl tests found in src/sbin/bectl/tests, modified as needed. 30cddbc3b4SKyle Evans 31cddbc3b4SKyle EvansZPOOL_NAME_FILE=zpool_name 32cddbc3b4SKyle Evansget_zpool_name() 33cddbc3b4SKyle Evans{ 34cddbc3b4SKyle Evans cat $ZPOOL_NAME_FILE 35cddbc3b4SKyle Evans} 36cddbc3b4SKyle Evansmake_zpool_name() 37cddbc3b4SKyle Evans{ 38cddbc3b4SKyle Evans mktemp -u libbe_test_XXXXXX > $ZPOOL_NAME_FILE 39cddbc3b4SKyle Evans get_zpool_name 40cddbc3b4SKyle Evans} 41cddbc3b4SKyle Evans 42cddbc3b4SKyle Evans# Establishes a libbe zpool that can be used for some light testing; contains 43cddbc3b4SKyle Evans# a 'default' BE and not much else. 44cddbc3b4SKyle Evanslibbe_create_setup() 45cddbc3b4SKyle Evans{ 46cddbc3b4SKyle Evans zpool=$1 47cddbc3b4SKyle Evans disk=$2 48cddbc3b4SKyle Evans mnt=$3 49cddbc3b4SKyle Evans 50cddbc3b4SKyle Evans # Sanity check to make sure `make_zpool_name` succeeded 51cddbc3b4SKyle Evans atf_check test -n "$zpool" 52cddbc3b4SKyle Evans 53cddbc3b4SKyle Evans kldload -n -q zfs || atf_skip "ZFS module not loaded on the current system" 54cddbc3b4SKyle Evans atf_check mkdir -p ${mnt} 55cddbc3b4SKyle Evans atf_check truncate -s 1G ${disk} 56cddbc3b4SKyle Evans atf_check zpool create -o altroot=${mnt} ${zpool} ${disk} 57cddbc3b4SKyle Evans atf_check zfs create -o mountpoint=none ${zpool}/ROOT 58cddbc3b4SKyle Evans atf_check zfs create -o mountpoint=/ -o canmount=noauto \ 59cddbc3b4SKyle Evans ${zpool}/ROOT/default 60cddbc3b4SKyle Evans atf_check zfs create -o mountpoint=/usr -o canmount=noauto \ 61cddbc3b4SKyle Evans ${zpool}/ROOT/default/usr 62cddbc3b4SKyle Evans atf_check zfs create -o mountpoint=/usr/obj -o canmount=noauto \ 63cddbc3b4SKyle Evans ${zpool}/ROOT/default/usr/obj 64cddbc3b4SKyle Evans} 65cddbc3b4SKyle Evans 66cddbc3b4SKyle Evanslibbe_cleanup() 67cddbc3b4SKyle Evans{ 68cddbc3b4SKyle Evans zpool=$1 69cddbc3b4SKyle Evans cwd=$(atf_get_srcdir) 70cddbc3b4SKyle Evans 71cddbc3b4SKyle Evans if [ -z "$zpool" ]; then 72cddbc3b4SKyle Evans echo "Skipping cleanup; zpool not set up" 73cddbc3b4SKyle Evans elif zpool get health ${zpool} >/dev/null 2>&1; then 74cddbc3b4SKyle Evans zpool destroy -f ${zpool} 75cddbc3b4SKyle Evans fi 76cddbc3b4SKyle Evans 77cddbc3b4SKyle Evans if [ -f "${cwd}/disk.img" ]; then 78cddbc3b4SKyle Evans rm ${cwd}/disk.img 79cddbc3b4SKyle Evans fi 80cddbc3b4SKyle Evans} 81cddbc3b4SKyle Evans 82cddbc3b4SKyle Evansatf_test_case libbe_create cleanup 83cddbc3b4SKyle Evanslibbe_create_head() 84cddbc3b4SKyle Evans{ 85cddbc3b4SKyle Evans atf_set "descr" "check _be_create from libbe" 86cddbc3b4SKyle Evans atf_set "require.user" root 87cddbc3b4SKyle Evans} 88cddbc3b4SKyle Evanslibbe_create_body() 89cddbc3b4SKyle Evans{ 900932ee9fSLi-Wen Hsu if [ "$(atf_config_get ci false)" = "true" ] && \ 910932ee9fSLi-Wen Hsu [ "$(uname -p)" = "i386" ]; then 920932ee9fSLi-Wen Hsu atf_skip "https://bugs.freebsd.org/249055" 930932ee9fSLi-Wen Hsu fi 940932ee9fSLi-Wen Hsu 952ccf8a82SEdward Tomasz Napierala if [ "$(atf_config_get ci false)" = "true" ] && \ 962ccf8a82SEdward Tomasz Napierala [ "$(uname -p)" = "armv7" ]; then 972ccf8a82SEdward Tomasz Napierala atf_skip "https://bugs.freebsd.org/249229" 982ccf8a82SEdward Tomasz Napierala fi 992ccf8a82SEdward Tomasz Napierala 100cddbc3b4SKyle Evans cwd=$(atf_get_srcdir) 101cddbc3b4SKyle Evans zpool=$(make_zpool_name) 102cddbc3b4SKyle Evans disk=${cwd}/disk.img 103cddbc3b4SKyle Evans mount=${cwd}/mnt 104cddbc3b4SKyle Evans prog=${cwd}/./target_prog 105cddbc3b4SKyle Evans 106cddbc3b4SKyle Evans # preliminary setup/checks 107cddbc3b4SKyle Evans atf_require_prog $prog 108cddbc3b4SKyle Evans libbe_create_setup ${zpool} ${disk} ${mount} 109cddbc3b4SKyle Evans 110cddbc3b4SKyle Evans # a recursive and non-recursive snapshot to test against 111cddbc3b4SKyle Evans atf_check zfs snapshot ${zpool}/ROOT/default@non-recursive 112cddbc3b4SKyle Evans atf_check zfs snapshot -r ${zpool}/ROOT/default@recursive 113cddbc3b4SKyle Evans 114cddbc3b4SKyle Evans # create a dataset after snapshots were taken 115cddbc3b4SKyle Evans atf_check zfs create -o mountpoint=/usr/src -o canmount=noauto \ 116cddbc3b4SKyle Evans ${zpool}/ROOT/default/usr/src 117cddbc3b4SKyle Evans 118cddbc3b4SKyle Evans # test boot environment creation with depth of 0 (i.e. a non-recursive boot environment). 119cddbc3b4SKyle Evans atf_check $prog "${zpool}/ROOT" \ 120cddbc3b4SKyle Evans nonrecursive \ 121cddbc3b4SKyle Evans "${zpool}/ROOT/default@non-recursive" \ 122cddbc3b4SKyle Evans 0 123cddbc3b4SKyle Evans # the dataset should exist 124cddbc3b4SKyle Evans atf_check -o not-empty \ 125cddbc3b4SKyle Evans zfs list "${zpool}/ROOT/nonrecursive" 126cddbc3b4SKyle Evans # the child dataset should not exist. 127cddbc3b4SKyle Evans atf_check -e not-empty -s not-exit:0 \ 128cddbc3b4SKyle Evans zfs list "${zpool}/ROOT/nonrecursive/usr" 129cddbc3b4SKyle Evans 130cddbc3b4SKyle Evans # test boot environment creation with unlimited depth (i.e. a recursive boot environment). 131cddbc3b4SKyle Evans atf_check $prog "${zpool}/ROOT" \ 132cddbc3b4SKyle Evans recursive \ 133cddbc3b4SKyle Evans "${zpool}/ROOT/default@recursive" \ 134cddbc3b4SKyle Evans -1 135cddbc3b4SKyle Evans # the dataset should exist 136cddbc3b4SKyle Evans atf_check -o not-empty \ 137cddbc3b4SKyle Evans zfs list "${zpool}/ROOT/recursive" 138cddbc3b4SKyle Evans # the child dataset should exist 139cddbc3b4SKyle Evans atf_check -o not-empty \ 140cddbc3b4SKyle Evans zfs list "${zpool}/ROOT/recursive/usr" 141cddbc3b4SKyle Evans # the child dataset should exist 142cddbc3b4SKyle Evans atf_check -o not-empty \ 143cddbc3b4SKyle Evans zfs list "${zpool}/ROOT/recursive/usr/obj" 144cddbc3b4SKyle Evans # the child dataset should not exist. 145cddbc3b4SKyle Evans atf_check -e not-empty -s not-exit:0 \ 146cddbc3b4SKyle Evans zfs list "${zpool}/ROOT/recursive/usr/src" 147cddbc3b4SKyle Evans 148cddbc3b4SKyle Evans # test boot environment creation with a depth of 1 149cddbc3b4SKyle Evans atf_check $prog "${zpool}/ROOT" \ 150cddbc3b4SKyle Evans depth \ 151cddbc3b4SKyle Evans "${zpool}/ROOT/default@recursive" \ 152cddbc3b4SKyle Evans 1 153cddbc3b4SKyle Evans # the child dataset should exist 154cddbc3b4SKyle Evans atf_check -o not-empty \ 155cddbc3b4SKyle Evans zfs list "${zpool}/ROOT/depth/usr" 156cddbc3b4SKyle Evans # the child dataset should not exist. 157cddbc3b4SKyle Evans atf_check -e not-empty -s not-exit:0 \ 158cddbc3b4SKyle Evans zfs list "${zpool}/ROOT/depth/usr/obj" 159cddbc3b4SKyle Evans # the child dataset should not exist. 160cddbc3b4SKyle Evans atf_check -e not-empty -s not-exit:0 \ 161cddbc3b4SKyle Evans zfs list "${zpool}/ROOT/depth/usr/src" 162cddbc3b4SKyle Evans 163cddbc3b4SKyle Evans 164cddbc3b4SKyle Evans # create a recursive boot environment named 'relative-snap'. 165cddbc3b4SKyle Evans # This test is to ensure that a relative snapshot label can be used, 166cddbc3b4SKyle Evans # (i.e. the format: 'bootenvironment@snapshot') 167cddbc3b4SKyle Evans atf_check $prog "${zpool}/ROOT" \ 168cddbc3b4SKyle Evans relative-snap \ 169cddbc3b4SKyle Evans default@recursive \ 170cddbc3b4SKyle Evans -1 171cddbc3b4SKyle Evans # the dataset should exist 172cddbc3b4SKyle Evans atf_check -o not-empty \ 173cddbc3b4SKyle Evans zfs list "${zpool}/ROOT/relative-snap" 174cddbc3b4SKyle Evans # the child dataset should exist 175cddbc3b4SKyle Evans atf_check -o not-empty \ 176cddbc3b4SKyle Evans zfs list "${zpool}/ROOT/relative-snap/usr" 177cddbc3b4SKyle Evans} 178cddbc3b4SKyle Evans 179cddbc3b4SKyle Evanslibbe_create_cleanup() 180cddbc3b4SKyle Evans{ 181cddbc3b4SKyle Evans libbe_cleanup $(get_zpool_name) 182cddbc3b4SKyle Evans} 183cddbc3b4SKyle Evans 184cddbc3b4SKyle Evansatf_init_test_cases() 185cddbc3b4SKyle Evans{ 186cddbc3b4SKyle Evans atf_add_test_case libbe_create 187cddbc3b4SKyle Evans} 188