]> git.proxmox.com Git - mirror_zfs.git/blob - tests/zfs-tests/tests/functional/cli_root/zpool/zpool_colors.ksh
Colorize zpool status output
[mirror_zfs.git] / tests / zfs-tests / tests / functional / cli_root / zpool / zpool_colors.ksh
1 #!/bin/ksh -p
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 # Copyright (c) 2019 Lawrence Livermore National Security, LLC.
17
18 . $STF_SUITE/include/libtest.shlib
19
20 #
21 # DESCRIPTION:
22 # Test that zpool status colored output works.
23 #
24 # STRATEGY:
25 # 1. Create a pool with a bunch of errors and force fault one of the vdevs.
26 # 2. Look for 'pool:' in bold.
27 # 3. Look for 'DEGRADED' in yellow
28 # 3. Look for 'FAULTED' in red
29 #
30
31 verify_runnable "both"
32
33 function cleanup
34 {
35 zinject -c all
36 }
37
38 log_onexit cleanup
39
40 log_assert "Test colorized zpool status output"
41
42 DISK2="$(echo $DISKS | cut -d' ' -f2)"
43 DISK3="$(echo $DISKS | cut -d' ' -f3)"
44
45 log_must dd if=/dev/urandom of=/$TESTDIR/testfile bs=10M count=1
46
47 log_must zpool sync
48
49 log_must zpool offline -f $TESTPOOL $DISK3
50 log_must wait_for_degraded $TESTPOOL
51 log_must zinject -d $DISK2 -e io -T read -f 20 $TESTPOOL
52 log_must zinject -d $DISK2 -e io -T write -f 20 $TESTPOOL
53
54
55 log_must zpool scrub -w $TESTPOOL
56 log_must zinject -c all
57
58
59 # Use 'script' to fake zpool status into thinking it's running in a tty.
60 # Log the output here in case it's needed for postmortem.
61 log_note "$(faketty TERM=xterm-256color ZFS_COLOR=1 zpool status)"
62
63 # Replace the escape codes with "ESC" so they're easier to grep
64 out="$(faketty TERM=xterm-256color ZFS_COLOR=1 zpool status | \
65 grep -E 'pool:|DEGRADED' | \
66 sed -r 's/\s+//g;'$(echo -e 's/\033/ESC/g'))"
67
68 log_note "$(echo $out)"
69
70 log_note "Look for 'pool:' in bold"
71 log_must eval "echo \"$out\" | grep -q 'ESC\[1mpool:ESC\[0m' "
72
73 log_note "Look for 'DEGRADED' in yellow"
74 log_must eval "echo \"$out\" | grep -q 'ESC\[0;33mDEGRADEDESC\[0m'"
75
76 #
77 # The escape code for 'FAULTED' is a little more tricky. The line starts like
78 # this:
79 #
80 # <start red escape code> loop2 FAULTED <end escape code>
81 #
82 # Luckily, awk counts the start and end escape codes as separate fields, so
83 # we can easily remove the vdev field to get what we want.
84 #
85 out="$(faketty TERM=xterm-256color ZFS_COLOR=1 zpool status \
86 | awk '/FAULTED/{print $1$3$4}' | sed -r $(echo -e 's/\033/ESC/g'))"
87
88 log_note "Look for 'FAULTED' in red"
89 log_must eval "echo \"$out\" | grep -q 'ESC\[0;31mFAULTEDESC\[0m'"
90
91 log_pass "zpool status displayed colors"