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