151292Sobrien# Copyright (c) 2018 Alan Somers
251292Sobrien# All rights reserved.
351292Sobrien#
451292Sobrien# Redistribution and use in source and binary forms, with or without
5174294Sobrien# modification, are permitted provided that the following conditions
651292Sobrien# are met:
751292Sobrien# 1. Redistributions of source code must retain the above copyright
851292Sobrien#    notice, this list of conditions and the following disclaimer.
951292Sobrien# 2. Redistributions in binary form must reproduce the above copyright
1051292Sobrien#    notice, this list of conditions and the following disclaimer in the
1151292Sobrien#    documentation and/or other materials provided with the distribution.
1251292Sobrien#
1351292Sobrien# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1451292Sobrien# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1551292Sobrien# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1651292Sobrien# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1751292Sobrien# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1851292Sobrien# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1951292Sobrien# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2051292Sobrien# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2151292Sobrien# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2251292Sobrien# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2351292Sobrien# SUCH DAMAGE.
2451292Sobrien#
2551292Sobrien# $FreeBSD: stable/11/tests/sys/geom/class/part/misc.sh 329275 2018-02-14 20:26:09Z asomers $
2651292Sobrien
2751292SobrienMD_DEVS="md.devs"
2851292Sobrien
29174294Sobrienatf_test_case blank_physpath cleanup
3051292Sobrienblank_physpath_head()
3151292Sobrien{
3251292Sobrien	atf_set "descr" "gpart shouldn't add physical paths to underlying providers that have none"
3351292Sobrien	atf_set "require.user" "root"
3451292Sobrien}
3551292Sobrienblank_physpath_body()
3651292Sobrien{
3751292Sobrien	load_gnop
3851292Sobrien	load_gpart
3951292Sobrien	md=$(alloc_md)
4051292Sobrien	atf_check -o empty -e ignore diskinfo -p ${md}
4151292Sobrien	atf_check -s exit:0 -o ignore gpart create -s bsd ${md}
4251292Sobrien	atf_check -s exit:0 -o ignore gpart add -t freebsd-ufs ${md}
4351292Sobrien	atf_check -o empty -e ignore diskinfo -p ${md}a
4451292Sobrien}
4551292Sobrienblank_physpath_cleanup()
4651292Sobrien{
4751292Sobrien	common_cleanup
4851292Sobrien}
4951292Sobrien
5051292Sobrien
5151292Sobrienatf_test_case bsd_physpath cleanup
5251292Sobrienbsd_physpath_head()
5351292Sobrien{
5451292Sobrien	atf_set "descr" "BSD partitions should append /X to the underlying device's physical path"
5551292Sobrien	atf_set "require.user" "root"
5651292Sobrien}
5751292Sobrienbsd_physpath_body()
5851292Sobrien{
5951292Sobrien	load_gnop
6051292Sobrien	load_gpart
6151292Sobrien	md=$(alloc_md)
6251292Sobrien	physpath="some/physical/path"
6351292Sobrien	atf_check gnop create -z $physpath /dev/${md}
6451292Sobrien	atf_check -s exit:0 -o ignore gpart create -s bsd ${md}.nop
6551292Sobrien	atf_check -s exit:0 -o ignore gpart add -t freebsd-ufs ${md}.nop
6651292Sobrien	gpart_physpath=$(diskinfo -p ${md}.nopa)
6751292Sobrien	atf_check_equal "${physpath}/a" "$gpart_physpath"
6851292Sobrien}
6951292Sobrienbsd_physpath_cleanup()
7051292Sobrien{
7151292Sobrien	common_cleanup
7251292Sobrien}
73
74atf_test_case gpt_physpath cleanup
75gpt_physpath_head()
76{
77	atf_set "descr" "GPT partitions should append /pX to the underlying device's physical path"
78	atf_set "require.user" "root"
79}
80gpt_physpath_body()
81{
82	load_gnop
83	load_gpart
84	md=$(alloc_md)
85	physpath="some/physical/path"
86	atf_check gnop create -z $physpath /dev/${md}
87	atf_check -s exit:0 -o ignore gpart create -s gpt ${md}.nop
88	atf_check -s exit:0 -o ignore gpart add -t efi ${md}.nop
89	gpart_physpath=$(diskinfo -p ${md}.nopp1)
90	atf_check_equal "${physpath}/p1" "$gpart_physpath"
91}
92gpt_physpath_cleanup()
93{
94	common_cleanup
95}
96
97atf_test_case mbr_physpath cleanup
98mbr_physpath_head()
99{
100	atf_set "descr" "MBR partitions should append /sX to the underlying device's physical path"
101	atf_set "require.user" "root"
102}
103mbr_physpath_body()
104{
105	load_gnop
106	load_gpart
107	md=$(alloc_md)
108	physpath="some/physical/path"
109	atf_check gnop create -z $physpath /dev/${md}
110	atf_check -s exit:0 -o ignore gpart create -s mbr ${md}.nop
111	atf_check -s exit:0 -o ignore gpart add -t freebsd ${md}.nop
112	gpart_physpath=$(diskinfo -p ${md}.nops1)
113	atf_check_equal "${physpath}/s1" "$gpart_physpath"
114}
115mbr_physpath_cleanup()
116{
117	common_cleanup
118}
119
120atf_test_case mbr_bsd_physpath cleanup
121mbr_bsd_physpath_head()
122{
123	atf_set "descr" "BSD partitions nested within MBR partitions should append /sX/Y to the underlying device's physical path"
124	atf_set "require.user" "root"
125}
126mbr_bsd_physpath_body()
127{
128	load_gnop
129	load_gpart
130	md=$(alloc_md)
131	physpath="some/physical/path"
132	atf_check gnop create -z $physpath /dev/${md}
133	atf_check -s exit:0 -o ignore gpart create -s mbr ${md}.nop
134	atf_check -s exit:0 -o ignore gpart add -t freebsd ${md}.nop
135	atf_check -s exit:0 -o ignore gpart create -s bsd ${md}.nops1
136	atf_check -s exit:0 -o ignore gpart add -t freebsd-ufs ${md}.nops1
137	gpart_physpath=$(diskinfo -p ${md}.nops1a)
138	atf_check_equal "${physpath}/s1/a" "$gpart_physpath"
139}
140mbr_bsd_physpath_cleanup()
141{
142	common_cleanup
143}
144
145atf_init_test_cases()
146{
147	atf_add_test_case blank_physpath
148	atf_add_test_case bsd_physpath
149	atf_add_test_case gpt_physpath
150	atf_add_test_case mbr_physpath
151	atf_add_test_case mbr_bsd_physpath
152}
153
154alloc_md()
155{
156	local md
157
158	md=$(mdconfig -a -t swap -s 1M) || atf_fail "mdconfig -a failed"
159	echo ${md} >> $MD_DEVS
160	echo ${md}
161}
162
163common_cleanup()
164{
165	if [ -f "$MD_DEVS" ]; then
166		while read test_md; do
167			gnop destroy -f ${test_md}.nop 2>/dev/null
168			mdconfig -d -u $test_md 2>/dev/null
169		done < $MD_DEVS
170		rm $MD_DEVS
171	fi
172	true
173}
174
175load_gpart()
176{
177	if ! kldstat -q -m g_part; then
178		geom part load || atf_skip "could not load module for geom part"
179	fi
180}
181
182load_gnop()
183{
184	if ! kldstat -q -m g_nop; then
185		geom nop load || atf_skip "could not load module for geom nop"
186	fi
187}
188