xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/tests/functional/vdev_zaps/vdev_zaps.kshlib (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1# SPDX-License-Identifier: CDDL-1.0
2#
3# CDDL HEADER START
4#
5# This file and its contents are supplied under the terms of the
6# Common Development and Distribution License ("CDDL"), version 1.0.
7# You may only use this file in accordance with the terms of version
8# 1.0 of the CDDL.
9#
10# A full copy of the text of the CDDL should have accompanied this
11# source.  A copy of the CDDL is also available via the Internet at
12# http://www.illumos.org/license/CDDL.
13#
14# CDDL HEADER END
15#
16
17#
18# Copyright (c) 2015, 2016 by Delphix. All rights reserved.
19#
20
21function get_conf_section # regex conf
22{
23        typeset regex="$1"
24        typeset conf="$2"
25
26        awk -v r="$1" '$0 ~ r, 0 {if($0 ~ r) next; if(/children\[/) exit; print}' "$conf"
27}
28
29function get_leaf_vd_zap # dsk conf
30{
31        get_conf_section "$1" "$2" | awk '/com.delphix:vdev_zap_leaf: [0-9]+/ {print $2}'
32}
33
34function get_top_vd_zap # dsk conf
35{
36        get_conf_section "$1" "$2" | awk '/com.delphix:vdev_zap_top: [0-9]+/ {print $2}'
37}
38function get_root_vd_zap # conf
39{
40        awk '/com.klarasystems:vdev_zap_root: [0-9]+/ {print $2}' "$1"
41}
42
43function assert_has_sentinel # conf
44{
45        log_must grep -q "com.delphix:has_per_vdev_zaps" "$1"
46}
47
48function assert_zap_common # pool vd lvl zapobj
49{
50        typeset pool=$1
51        typeset vd="$2"
52        typeset lvl=$3
53        typeset zapobj=$4
54
55        if [ -z "$zapobj" ]; then
56                log_fail "$vd on $pool has no $lvl ZAP in config"
57        elif ! zdb -d $pool $zapobj | grep -q 'zap'; then
58                log_fail "$vd on $pool has no $lvl ZAP in MOS"
59        fi
60}
61
62function assert_root_zap # pool conf
63{
64        typeset pool=$1
65        typeset conf=$2
66
67        root_zap=$(get_root_vd_zap $conf)
68        assert_zap_common $pool "root vdev" "root" $root_zap
69}
70
71function assert_top_zap # pool vd conf
72{
73        typeset pool=$1
74        typeset vd="$2"
75        typeset conf=$3
76
77        top_zap=$(get_top_vd_zap "$vd" $conf)
78        assert_zap_common $pool "$vd" "top" $top_zap
79}
80
81function assert_leaf_zap # pool vd conf
82{
83        typeset pool=$1
84        typeset vd="$2"
85        typeset conf=$3
86
87        leaf_zap=$(get_leaf_vd_zap "$vd" $conf)
88        assert_zap_common $pool "$vd" "leaf" $leaf_zap
89}
90
91#
92# Code common to setup/teardown for each test.
93#
94
95function cleanup
96{
97        datasetexists $TESTPOOL && log_must zpool destroy -f $TESTPOOL
98        [ -e $conf ] && log_must rm -f "$conf"
99        poolexists $POOL2 && log_must zpool destroy -f $POOL2
100}
101
102log_onexit cleanup
103