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
42read -r _ DISK2 DISK3 _ <<<"$DISKS"
43
44log_must dd if=/dev/urandom of=/$TESTDIR/testfile bs=10M count=1
45
46sync_all_pools
47
48log_must zpool offline -f $TESTPOOL $DISK3
49log_must wait_for_degraded $TESTPOOL
50log_must zinject -d $DISK2 -e io -T read -f 20 $TESTPOOL
51log_must zinject -d $DISK2 -e io -T write -f 20 $TESTPOOL
52
53
54log_must zpool scrub -w $TESTPOOL
55log_must zinject -c all
56
57
58# Use 'script' to fake zpool status into thinking it's running in a tty.
59# Log the output here in case it's needed for postmortem.
60log_note "$(faketty TERM=xterm-256color ZFS_COLOR=1 zpool status)"
61
62# Replace the escape codes with "ESC" so they're easier to grep
63out="$(faketty TERM=xterm-256color ZFS_COLOR=1 zpool status | \
64    sed -E '/pool:|DEGRADED/!d;s/[[:space:]]+//g;'$(printf 's/\033/ESC/g'))"
65
66log_note "$(echo $out)"
67
68log_note "Look for 'pool:' in bold"
69log_must grep -q 'ESC\[1mpool:ESC\[0m' <<<"$out"
70
71log_note "Look for 'DEGRADED' in yellow"
72log_must grep -q 'ESC\[0;33mDEGRADEDESC\[0m' <<<"$out"
73
74#
75# The escape code for 'FAULTED' is a little more tricky.  The line starts like
76# this:
77#
78# <start red escape code> loop2  FAULTED <end escape code>
79#
80# Luckily, awk counts the start and end escape codes as separate fields, so
81# we can easily remove the vdev field to get what we want.
82#
83out="$(faketty TERM=xterm-256color ZFS_COLOR=1 zpool status \
84    | awk '/FAULTED/ {print $1$3$4}' | sed -E $(printf 's/\033/ESC/g'))"
85
86log_note "$(echo $out)"
87
88log_note "Look for 'FAULTED' in red"
89log_must grep -q 'ESC\[0;31mFAULTEDESC\[0m' <<<"$out"
90
91log_pass "zpool status displayed colors"
92