1#!/usr/local/bin/ksh93 -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23# $FreeBSD$
24
25#
26# Copyright 2012 Spectra Logic.  All rights reserved.
27# Use is subject to license terms.
28#
29# ident	"@(#)hotspare_replace_007_pos.ksh	1.0	12/08/10 SL"
30#
31. $STF_SUITE/tests/hotspare/hotspare.kshlib
32
33################################################################################
34#
35# __stc_assertion_start
36#
37# ID: zfsd_hotspare_005_pos
38#
39# DESCRIPTION:
40#	If a spare gets added to an already damaged pool, the spare will be
41#	activated
42#
43# STRATEGY:
44#	1. Create 1 storage pool without hot spares
45#	2. Fail one vdev by using zinject to degrade or fault it
46#	3. Verify that it gets degraded or faulted, respectively
47#	4. Add a hotspare
48#	5. Verify that the spare is in use.
49#
50# TESTABILITY: explicit
51#
52# TEST_AUTOMATION_LEVEL: automated
53#
54# CODING STATUS: COMPLETED (2012-08-10)
55#
56# __stc_assertion_end
57#
58###############################################################################
59
60verify_runnable "global"
61
62function cleanup
63{
64	if poolexists $TESTPOOL ; then
65		destroy_pool $TESTPOOL
66	fi
67
68	partition_cleanup
69}
70
71function verify_assertion # damage_type
72{
73	typeset damage=$1
74	typeset err_dev=${devarray[3]}
75	typeset mntp=$(get_prop mountpoint $TESTPOOL)
76
77	log_must $ZINJECT -d $err_dev -A $damage $TESTPOOL
78	log_must check_state $TESTPOOL $err_dev ${damage_status[$damage]}
79
80	# Add the spare, and check that it is in use
81	log_must $ZPOOL add $TESTPOOL spare $sdev
82	for ((timeout=0; $timeout<10; timeout=$timeout+1)); do
83		if check_state $TESTPOOL "$sdev" "INUSE"; then
84			break
85		fi
86		$SLEEP 6
87	done
88	log_must $ZPOOL status $TESTPOOL
89	log_must check_state $TESTPOOL "$sdev" "INUSE"
90}
91
92log_onexit cleanup
93
94log_assert "A spare that is added to a degraded pool will be activated"
95
96ensure_zfsd_running
97set_devs
98
99typeset  sdev="${sparedevs[0]}"
100typeset -A damage_status
101damage_status["degrade"]="DEGRADED"
102damage_status["fault"]="FAULTED"
103
104set -A my_keywords "mirror" "raidz1" "raidz2"
105
106for keyword in "${my_keywords[@]}"; do
107	for damage in "degrade" "fault"; do
108		log_must create_pool $TESTPOOL $keyword ${pooldevs[@]}
109		verify_assertion $damage
110		destroy_pool $TESTPOOL
111	done
112done
113