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
31verify_runnable "both"
32
33function cleanup
34{
35	zinject -c all
36}
37
38log_onexit cleanup
39
40log_assert "Test colorized zpool status output"
41
42DISK2="$(echo $DISKS | cut -d' ' -f2)"
43DISK3="$(echo $DISKS | cut -d' ' -f3)"
44
45log_must dd if=/dev/urandom of=/$TESTDIR/testfile bs=10M count=1
46
47log_must zpool sync
48
49log_must zpool offline -f $TESTPOOL $DISK3
50log_must wait_for_degraded $TESTPOOL
51log_must zinject -d $DISK2 -e io -T read -f 20 $TESTPOOL
52log_must zinject -d $DISK2 -e io -T write -f 20 $TESTPOOL
53
54
55log_must zpool scrub -w $TESTPOOL
56log_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.
61log_note "$(faketty TERM=xterm-256color ZFS_COLOR=1 zpool status)"
62
63# Replace the escape codes with "ESC" so they're easier to grep
64out="$(faketty TERM=xterm-256color ZFS_COLOR=1 zpool status | \
65    grep -E 'pool:|DEGRADED' | \
66    sed -r 's/[[:space:]]+//g;'$(echo -e 's/\033/ESC/g'))"
67
68log_note "$(echo $out)"
69
70log_note "Look for 'pool:' in bold"
71log_must eval "echo \"$out\" | grep -q 'ESC\[1mpool:ESC\[0m' "
72
73log_note "Look for 'DEGRADED' in yellow"
74log_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#
85out="$(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
88log_note "Look for 'FAULTED' in red"
89log_must eval "echo \"$out\" | grep -q 'ESC\[0;31mFAULTEDESC\[0m'"
90
91log_pass "zpool status displayed colors"
92