xref: /freebsd/sbin/swapon/tests/swapon_test.sh (revision df21a004be237a1dccd03c7b47254625eea62fa9)
1# Copyright (c) 2025 Ronald Klop <ronald@FreeBSD.org>
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions
5# are met:
6# 1. Redistributions of source code must retain the above copyright
7#    notice, this list of conditions and the following disclaimer.
8# 2. Redistributions in binary form must reproduce the above copyright
9#    notice, this list of conditions and the following disclaimer in the
10#    documentation and/or other materials provided with the distribution.
11#
12# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
13# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
16# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22# SUCH DAMAGE.
23#
24#
25
26atf_test_case attach_mdX cleanup
27attach_mdX_head()
28{
29	atf_set "descr" "mdX device should attach"
30}
31attach_mdX_body()
32{
33	# if the swapfile is too small (like 1k) then mdconfig hangs looking up the md
34	# but need a swapfile bigger than one page kernel page size
35	pagesize=$(sysctl -n hw.pagesize)
36	minsize=$(( pagesize * 2 ))
37	atf_check -s exit:0 -x "truncate -s $minsize swapfile"
38	atf_check -s exit:0 -o save:fstab.out -x "echo 'md31    none    swap    sw,file=swapfile  0       0'"
39	atf_check -s exit:0 -o match:"swapon: adding /dev/md31 as swap device" -x "swapon -F fstab.out -a"
40}
41attach_mdX_cleanup()
42{
43	swapoff -F fstab.out -a
44}
45
46###
47atf_test_case attach_dev_mdX cleanup
48attach_dev_mdX_head()
49{
50	atf_set "descr" "/dev/mdX device should attach"
51}
52attach_dev_mdX_body()
53{
54	# if the swapfile is too small (like 1k) then mdconfig hangs looking up the md
55	# but need a swapfile bigger than one page kernel page size
56	pagesize=$(sysctl -n hw.pagesize)
57	minsize=$(( pagesize * 2 ))
58	atf_check -s exit:0 -x "truncate -s $minsize swapfile"
59	atf_check -s exit:0 -o save:fstab.out -x "echo '/dev/md32    none    swap    sw,file=swapfile  0       0'"
60	atf_check -s exit:0 -o match:"swapon: adding /dev/md32 as swap device" -x "swapon -F fstab.out -a"
61}
62attach_dev_mdX_cleanup()
63{
64	swapoff -F fstab.out -a
65}
66
67###
68atf_test_case attach_md cleanup
69attach_md_head()
70{
71	atf_set "descr" "md device should attach"
72}
73attach_md_body()
74{
75	# if the swapfile is too small (like 1k) then mdconfig hangs looking up the md
76	# but need a swapfile bigger than one page kernel page size
77	pagesize=$(sysctl -n hw.pagesize)
78	minsize=$(( pagesize * 2 ))
79	atf_check -s exit:0 -x "truncate -s $minsize swapfile"
80	atf_check -s exit:0 -o save:fstab.out -x "echo 'md    none    swap    sw,file=swapfile  0       0'"
81	atf_check -s exit:0 -o match:"swapon: adding /dev/md[0-9][0-9]* as swap device" -x "swapon -F fstab.out -a"
82}
83attach_md_cleanup()
84{
85	swapoff -F fstab.out -a
86}
87
88###
89atf_test_case attach_dev_md cleanup
90attach_dev_md_head()
91{
92	atf_set "descr" "/dev/md device should attach"
93}
94attach_dev_md_body()
95{
96	# if the swapfile is too small (like 1k) then mdconfig hangs looking up the md
97	# but need a swapfile bigger than one page kernel page size
98	pagesize=$(sysctl -n hw.pagesize)
99	minsize=$(( pagesize * 2 ))
100	atf_check -s exit:0 -x "truncate -s $minsize swapfile"
101	atf_check -s exit:0 -o save:fstab.out -x "echo '/dev/md    none    swap    sw,file=swapfile  0       0'"
102	atf_check -s exit:0 -o match:"swapon: adding /dev/md[0-9][0-9]* as swap device" -x "swapon -F fstab.out -a"
103}
104attach_dev_md_cleanup()
105{
106	swapoff -F fstab.out -a
107}
108
109###
110atf_test_case attach_mdX_eli cleanup
111attach_mdX_eli_head()
112{
113	atf_set "descr" "mdX.eli device should attach"
114}
115attach_mdX_eli_body()
116{
117	# if the swapfile is too small (like 1k) then mdconfig hangs looking up the md
118	# but need a swapfile bigger than one page kernel page size
119	pagesize=$(sysctl -n hw.pagesize)
120	minsize=$(( pagesize * 2 ))
121	atf_check -s exit:0 -x "truncate -s $minsize swapfile"
122	atf_check -s exit:0 -o save:fstab.out -x "echo 'md33.eli    none    swap    sw,file=swapfile  0       0'"
123	atf_check -s exit:0 -o match:"swapon: adding /dev/md33.eli as swap device" -x "swapon -F fstab.out -a"
124}
125attach_mdX_eli_cleanup()
126{
127	swapoff -F fstab.out -a
128}
129
130###
131atf_test_case attach_dev_mdX_eli cleanup
132attach_dev_mdX_eli_head()
133{
134	atf_set "descr" "/dev/mdX.eli device should attach"
135}
136attach_dev_mdX_eli_body()
137{
138	# if the swapfile is too small (like 1k) then mdconfig hangs looking up the md
139	# but need a swapfile bigger than one page kernel page size
140	pagesize=$(sysctl -n hw.pagesize)
141	minsize=$(( pagesize * 2 ))
142	atf_check -s exit:0 -x "truncate -s $minsize swapfile"
143	atf_check -s exit:0 -o save:fstab.out -x "echo '/dev/md34.eli    none    swap    sw,file=swapfile  0       0'"
144	atf_check -s exit:0 -o match:"swapon: adding /dev/md34.eli as swap device" -x "swapon -F fstab.out -a"
145}
146attach_dev_mdX_eli_cleanup()
147{
148	swapoff -F fstab.out -a
149}
150
151###
152atf_test_case attach_md_eli cleanup
153attach_md_eli_head()
154{
155	atf_set "descr" "md.eli device should attach"
156}
157attach_md_eli_body()
158{
159	# if the swapfile is too small (like 1k) then mdconfig hangs looking up the md
160	# but need a swapfile bigger than one page kernel page size
161	pagesize=$(sysctl -n hw.pagesize)
162	minsize=$(( pagesize * 2 ))
163	atf_check -s exit:0 -x "truncate -s $minsize swapfile"
164	atf_check -s exit:0 -o save:fstab.out -x "echo 'md.eli    none    swap    sw,file=swapfile  0       0'"
165	atf_check -s exit:0 -o match:"swapon: adding /dev/md[0-9][0-9]*.eli as swap device" -x "swapon -F fstab.out -a"
166}
167attach_md_eli_cleanup()
168{
169	swapoff -F fstab.out -a
170}
171
172###
173atf_test_case attach_dev_md_eli cleanup
174attach_dev_md_eli_head()
175{
176	atf_set "descr" "/dev/md.eli device should attach"
177}
178attach_dev_md_eli_body()
179{
180	# if the swapfile is too small (like 1k) then mdconfig hangs looking up the md
181	# but need a swapfile bigger than one page kernel page size
182	pagesize=$(sysctl -n hw.pagesize)
183	minsize=$(( pagesize * 2 ))
184	atf_check -s exit:0 -x "truncate -s $minsize swapfile"
185	atf_check -s exit:0 -o save:fstab.out -x "echo '/dev/md.eli    none    swap    sw,file=swapfile  0       0'"
186	atf_check -s exit:0 -o match:"swapon: adding /dev/md[0-9][0-9]*.eli as swap device" -x "swapon -F fstab.out -a"
187}
188attach_dev_md_eli_cleanup()
189{
190	swapoff -F fstab.out -a
191}
192
193###
194
195atf_test_case attach_too_small
196attach_too_small_head()
197{
198	atf_set "descr" "should refuse to attach if smaller than one kernel page size"
199}
200attach_too_small_body()
201{
202	# Need to use smaller than kernel page size
203	pagesize=$(sysctl -n hw.pagesize)
204	minsize=$(( pagesize / 2 ))
205	atf_check -s exit:0 -x "truncate -s $minsize swapfile"
206	atf_check -s exit:0 -o save:fstab.out -x "echo 'md35    none    swap    sw,file=swapfile  0       0'"
207	atf_check -s exit:1 -e match:"swapon: /dev/md35: NSWAPDEV limit reached" -x "swapon -F fstab.out -a"
208	atf_check -s exit:0 -x "mdconfig -d -u 35"
209}
210
211###
212atf_init_test_cases()
213{
214	atf_add_test_case attach_mdX
215	atf_add_test_case attach_dev_mdX
216	atf_add_test_case attach_md
217	atf_add_test_case attach_dev_md
218
219	atf_add_test_case attach_mdX_eli
220	atf_add_test_case attach_dev_mdX_eli
221	atf_add_test_case attach_md_eli
222	atf_add_test_case attach_dev_md_eli
223
224	atf_add_test_case attach_too_small
225}
226