diff options
author | Tony Hutter <[email protected]> | 2017-04-21 09:27:04 -0700 |
---|---|---|
committer | Brian Behlendorf <[email protected]> | 2017-04-21 09:27:04 -0700 |
commit | d6418de057ecb71fb4cdc1b0a89d5265d13d121a (patch) | |
tree | bb4eec33de86e05bcc12d74ac7f8ff335ee45919 /tests | |
parent | 038091fd4f8d24f308708987192065e55574bbe9 (diff) |
Prebaked scripts for zpool status/iostat -c
This patch updates the "zpool status/iostat -c" commands to only run
"pre-baked" scripts from the /etc/zfs/zpool.d directory (or wherever
you install to). The scripts can only be run from -c as an unprivileged
user (unless the ZPOOL_SCRIPTS_AS_ROOT environment var is
set by root). This was done to encourage scripts to be written is such
a way that normal users can use them, and to be cautious. If your
script needs to run a privileged command, consider adding the
appropriate line in /etc/sudoers. See zpool(8) for an example of how
to do this.
The patch also allows the scripts to output custom column names. If
the script outputs a line like:
name=value
then "name" is used for the column name, and "value" is its value.
Multiple columns can be specified by outputting multiple lines. Column
names and values can have spaces. If the value is empty, a dash (-) is
printed instead.
After all the "name=value" lines are read (if any), zpool will take the
next the next line of output (if any) and print it without a column
header. After that, no more lines will be processed. This can be
useful for printing errors.
Lastly, this patch also disables the -c option with the latency and
request size histograms, since it produced awkward output and made the
code harder to maintain.
Reviewed-by: Brian Behlendorf <[email protected]>
Reviewed-by: Giuseppe Di Natale <[email protected]>
Signed-off-by: Tony Hutter <[email protected]>
Closes #5852
Diffstat (limited to 'tests')
8 files changed, 163 insertions, 56 deletions
diff --git a/tests/runfiles/linux.run b/tests/runfiles/linux.run index 9415edbe0..b8c0efa36 100644 --- a/tests/runfiles/linux.run +++ b/tests/runfiles/linux.run @@ -335,7 +335,8 @@ pre = post = [tests/functional/cli_root/zpool_status] -tests = ['zpool_status_001_pos', 'zpool_status_002_pos'] +tests = ['zpool_status_001_pos', 'zpool_status_002_pos','zpool_status_003_pos'] +user = # DISABLED: # zpool_upgrade_002_pos - https://github.com/zfsonlinux/zfs/issues/4034 diff --git a/tests/zfs-tests/include/Makefile.am b/tests/zfs-tests/include/Makefile.am index a10d6a324..d6fb32b61 100644 --- a/tests/zfs-tests/include/Makefile.am +++ b/tests/zfs-tests/include/Makefile.am @@ -4,4 +4,5 @@ dist_pkgdata_SCRIPTS = \ default.cfg \ libtest.shlib \ math.shlib \ - properties.shlib + properties.shlib \ + zpool_script.shlib diff --git a/tests/zfs-tests/include/default.cfg b/tests/zfs-tests/include/default.cfg index 83c3ec47c..4556b2cf6 100644 --- a/tests/zfs-tests/include/default.cfg +++ b/tests/zfs-tests/include/default.cfg @@ -34,6 +34,7 @@ # ZFS Directories export ZEDLETDIR=${ZEDLETDIR:-/etc/zfs/zed.d} +export ZPOOLSCRIPTDIR=${ZPOOLSCRIPTDIR:-/etc/zfs/zpool.d} # Define run length constants export RT_LONG="3" diff --git a/tests/zfs-tests/include/zpool_script.shlib b/tests/zfs-tests/include/zpool_script.shlib new file mode 100755 index 000000000..52ae64890 --- /dev/null +++ b/tests/zfs-tests/include/zpool_script.shlib @@ -0,0 +1,50 @@ +#!/bin/ksh -p +# +# Common functions used by the zpool_status and zpool_iostat tests for running +# scripts with the -c option. +# +# Copyright (c) 2017 Lawrence Livermore National Security, LLC. +# + +. $STF_SUITE/include/libtest.shlib + +function test_zpool_script { + script="$1" + testpool="$2" + cmd="$3" + wholecmd="$cmd $script $testpool" + out="$($wholecmd)" + + # Default number of columns that get printed without -c + if echo "$cmd" | grep -q iostat ; then + # iostat + dcols=7 + else + + # status + dcols=5 + fi + + # Get the new column name that the script created + col="$(echo "$out" | \ + awk '/^pool +alloc +free +read +write +/ {print $8} \ + /NAME +STATE +READ +WRITE +CKSUM/ {print $6}')" + + if [ -z "$col" ] ; then + log_fail "'$wholecmd' created no new columns" + fi + + # Count the number of columns for each vdev. Each script should produce + # at least one new column value. Even if scripts return blank, zpool + # will convert the blank to a '-' to make things awk-able. Normal + # zpool iostat -v output is 7 columns, so if the script ran correctly + # we should see more than that. + if ! newcols=$(echo "$out" | \ + awk '/\/dev/{print NF-'$dcols'; if (NF <= '$dcols') {exit 1}}' | \ + head -n 1) ; \ + then + log_fail "'$wholecmd' didn't create a new column value" + else + log_note "'$wholecmd' passed ($newcols new columns)" + fi +} diff --git a/tests/zfs-tests/tests/functional/cli_root/zpool_status/Makefile.am b/tests/zfs-tests/tests/functional/cli_root/zpool_status/Makefile.am index beb59e3d0..747ec1dfa 100644 --- a/tests/zfs-tests/tests/functional/cli_root/zpool_status/Makefile.am +++ b/tests/zfs-tests/tests/functional/cli_root/zpool_status/Makefile.am @@ -3,4 +3,5 @@ dist_pkgdata_SCRIPTS = \ setup.ksh \ cleanup.ksh \ zpool_status_001_pos.ksh \ - zpool_status_002_pos.ksh + zpool_status_002_pos.ksh \ + zpool_status_003_pos.ksh diff --git a/tests/zfs-tests/tests/functional/cli_root/zpool_status/zpool_status_003_pos.ksh b/tests/zfs-tests/tests/functional/cli_root/zpool_status/zpool_status_003_pos.ksh new file mode 100755 index 000000000..cf7959161 --- /dev/null +++ b/tests/zfs-tests/tests/functional/cli_root/zpool_status/zpool_status_003_pos.ksh @@ -0,0 +1,76 @@ +#!/bin/ksh -p +# +# 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 http://www.opensolaris.org/os/licensing. +# 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 2007 Sun Microsystems, Inc. All rights reserved. +# Use is subject to license terms. +# + +# +# Copyright (c) 2013 by Delphix. All rights reserved. +# + +# +# Copyright (c) 2016-2017 by Lawrence Livermore National Security, LLC. +# + +# DESCRIPTION: +# Verify zpool status command mode (-c) works for all pre-baked scripts. +# +# STRATEGY: +# 1. Make sure each script creates at least one new column. +# 2. Make sure the new column values exist. +# 3. Make sure we can run multiple scripts in one -c line + +. $STF_SUITE/include/libtest.shlib +. $STF_SUITE/include/zpool_script.shlib + +verify_runnable "both" + +typeset testpool +if is_global_zone ; then + testpool="$TESTPOOL" +else + testpool="${TESTPOOL%%/*}" +fi + +files="$(ls $ZPOOLSCRIPTDIR)" +scripts="" +for i in $files ; do + if [ ! -x "$ZPOOLSCRIPTDIR/$i" ] ; then + # Skip non-executables + continue + fi + + # Collect executable script names + scripts="$scripts $i" + + # Run each one with -c + test_zpool_script "$i" "$testpool" "zpool status -P -c" +done + +# Test that we can run multiple scripts separated with a commma by running +# all the scripts in a single -c line. +allscripts="$(echo $scripts | sed -r 's/[[:blank:]]+/,/g')" +test_zpool_script "$allscripts" "$testpool" "zpool status -P -c" + +exit 0 diff --git a/tests/zfs-tests/tests/functional/cli_user/misc/zpool_status_001_neg.ksh b/tests/zfs-tests/tests/functional/cli_user/misc/zpool_status_001_neg.ksh index a3f839de4..7fe9fe55b 100755 --- a/tests/zfs-tests/tests/functional/cli_user/misc/zpool_status_001_neg.ksh +++ b/tests/zfs-tests/tests/functional/cli_user/misc/zpool_status_001_neg.ksh @@ -26,7 +26,7 @@ # # -# Copyright (c) 2013, 2016 by Delphix. All rights reserved. +# Copyright (c) 2013 by Delphix. All rights reserved. # . $STF_SUITE/include/libtest.shlib @@ -69,23 +69,4 @@ check_pool_status log_must eval "zpool status -v $TESTPOOL > /tmp/pool-status.$$" check_pool_status -# Make sure -c option works, and that VDEV_PATH and VDEV_UPATH get set. -# -# grep for '^\s+/' to just get the vdevs (not pools). All vdevs will start with -# a '/' when we specify the path (-P) flag. We check for "{}" to see if one -# of the VDEV variables isn't set. -C1=$(zpool status -P | grep -E '^\s+/' | wc -l) -C2=$(zpool status -P -c 'echo vdev_test{$VDEV_PATH}{$VDEV_UPATH}' | \ - grep -E '^\s+/' | grep -v '{}' | wc -l) - -if [ "$C1" != "$C2" ] ; then - log_fail "zpool status -c option failed. Expected $C1 vdevs, got $C2" -else - log_pass "zpool status -c option passed. Expected $C1 vdevs, got $C2" -fi - -# $TESTPOOL.virt has an offline device, so -x will show it -log_must eval "zpool status -x $TESTPOOL.virt > /tmp/pool-status.$$" -check_pool_status - log_pass "zpool status works when run as a user" diff --git a/tests/zfs-tests/tests/functional/cli_user/zpool_iostat/zpool_iostat_005_pos.ksh b/tests/zfs-tests/tests/functional/cli_user/zpool_iostat/zpool_iostat_005_pos.ksh index 6204c1461..0528e0c0a 100755 --- a/tests/zfs-tests/tests/functional/cli_user/zpool_iostat/zpool_iostat_005_pos.ksh +++ b/tests/zfs-tests/tests/functional/cli_user/zpool_iostat/zpool_iostat_005_pos.ksh @@ -30,11 +30,19 @@ # # -# Copyright (c) 2016 by Lawrence Livermore National Security, LLC. +# Copyright (c) 2016-2017 by Lawrence Livermore National Security, LLC. # +# DESCRIPTION: +# Verify zpool iostat command mode (-c) works for all pre-baked scripts. +# +# STRATEGY: +# 1. Make sure each script creates at least one new column. +# 2. Make sure the new column values exist. +# 3. Make sure we can run multiple scripts in one -c line . $STF_SUITE/include/libtest.shlib +. $STF_SUITE/include/zpool_script.shlib verify_runnable "both" @@ -45,36 +53,24 @@ else testpool=${TESTPOOL%%/*} fi -# -# DESCRIPTION: -# Verify 'zpool iostat -c CMD' works, and that VDEV_PATH and VDEV_UPATH get set. -# -# STRATEGY: -# grep for '^\s+/' to just get the vdevs (not pools). All vdevs will start with -# a '/' when we specify the path (-P) flag. We check for "{}" to see if one -# of the VDEV variables isn't set. -# -C1=$(zpool iostat -Pv $testpool | grep -E '^\s+/' | wc -l) -C2=$(zpool iostat -Pv -c 'echo vdev_test{$VDEV_PATH}{$VDEV_UPATH}' $testpool \ - | grep -E '^\s+/' | grep -v '{}' | wc -l) -if [ "$C1" != "$C2" ] ; then - log_fail "zpool iostat -c failed, expected $C1 vdevs, got $C2" -else - log_note "zpool iostat -c passed, expected $C1 vdevs, got $C2" -fi +files="$(ls $ZPOOLSCRIPTDIR)" +scripts="" +for i in $files ; do + if [ ! -x "$ZPOOLSCRIPTDIR/$i" ] ; then + # Skip non-executables + continue + fi -# Call iostat on only a specific vdev, and verify that the command only gets -# run on the vdev. We write the command results to a temp file to verify that -# the command actually gets run, rather than just verifying that the results -# are *displayed* for the specific vdev. -TMP=$(mktemp) -FIRST_VDEV=$(zpool iostat -Pv $testpool | grep -Eo '^\s+/[^ ]+' | head -n 1) -log_must zpool iostat -Pv -c "echo \$VDEV_PATH >> $TMP" $testpool \ - $FIRST_VDEV > /dev/null -C2=$(wc -w < $TMP) -rm $TMP -if [ "$C2" != "1" ] ; then - log_fail "zpool iostat -c <VDEV> failed, expected 1 vdev, got $C2" -else - log_note "zpool iostat -c <VDEV> passed, expected 1 vdev, got $C2" -fi + # Collect executable script names + scripts="$scripts $i" + + # Run each one with -c + test_zpool_script "$i" "$testpool" "zpool iostat -Pv -c" +done + +# Test that we can run multiple scripts separated with a commma by running +# all the scripts in a single -c line. +allscripts="$(echo $scripts | sed -r 's/[[:blank:]]+/,/g')" +test_zpool_script "$allscripts" "$testpool" "zpool iostat -Pv -c" + +exit 0 |