xref: /freebsd/usr.sbin/ctladm/tests/port.sh (revision edbd489d)
1# Things that aren't tested due to lack of kernel support:
2# * Creating camsim ports
3# * Creating tpc ports
4# * Creating camtgt ports
5# * Creating umass ports
6
7# TODO
8# * Creating nvmf ports
9# * Creating ha ports
10# * Creating fc ports
11
12# The PGTAG can be any 16-bit number.  The only constraint is that each
13# PGTAG,TARGET pair must be globally unique.
14PGTAG=30257
15
16load_cfiscsi() {
17	if ! kldstat -q -m cfiscsi; then
18		kldload cfiscsi || atf_skip "could not load cfscsi kernel mod"
19	fi
20}
21
22skip_if_ctld() {
23	if service ctld onestatus > /dev/null; then
24		# If ctld is running on this server, let's not interfere.
25		atf_skip "Cannot run this test while ctld is running"
26	fi
27}
28
29cleanup() {
30	driver=$1
31
32	if [ -e port-create.txt ]; then
33		case "$driver" in
34		"ioctl")
35			PORTNUM=`awk '/port:/ {print $2}' port-create.txt`
36			ctladm port -r -d $driver -p $PORTNUM
37			;;
38		"iscsi")
39			TARGET=`awk '/target:/ {print $2}' port-create.txt`
40			ctladm port -r -d $driver -p "$PORTNUM" -O cfiscsi_portal_group_tag=$PGTAG -O cfiscsi_target=$TARGET
41			;;
42		esac
43	fi
44}
45
46atf_test_case create_ioctl cleanup
47create_ioctl_head()
48{
49	atf_set "descr" "ctladm can create a new ioctl port"
50	atf_set "require.user" "root"
51}
52create_ioctl_body()
53{
54	skip_if_ctld
55
56	atf_check -o save:port-create.txt ctladm port -c -d "ioctl"
57	atf_check egrep -q "Port created successfully" port-create.txt
58	atf_check egrep -q "frontend: *ioctl" port-create.txt
59	atf_check egrep -q "port: *[0-9]+" port-create.txt
60	portnum=`awk '/port:/ {print $2}' port-create.txt`
61	atf_check -o save:portlist.txt ctladm portlist -qf ioctl
62	atf_check egrep -q "$portnum *YES *ioctl *ioctl" portlist.txt
63}
64create_ioctl_cleanup()
65{
66	cleanup ioctl
67}
68
69atf_test_case remove_ioctl_without_required_args cleanup
70remove_ioctl_without_required_args_head()
71{
72	atf_set "descr" "ctladm will gracefully fail to remove an ioctl target if required arguments are missing"
73	atf_set "require.user" "root"
74}
75remove_ioctl_without_required_args_body()
76{
77	skip_if_ctld
78
79	atf_check -o save:port-create.txt ctladm port -c -d "ioctl"
80	atf_check egrep -q "Port created successfully" port-create.txt
81	atf_check -s exit:1 -e match:"Missing required argument: port_id" ctladm port -r -d "ioctl"
82}
83remove_ioctl_without_required_args_cleanup()
84{
85	cleanup ioctl
86}
87
88atf_test_case create_iscsi cleanup
89create_iscsi_head()
90{
91	atf_set "descr" "ctladm can create a new iscsi port"
92	atf_set "require.user" "root"
93}
94create_iscsi_body()
95{
96	skip_if_ctld
97	load_cfiscsi
98
99	TARGET=iqn.2018-10.myhost.create_iscsi
100	atf_check -o save:port-create.txt ctladm port -c -d "iscsi" -O cfiscsi_portal_group_tag=$PGTAG -O cfiscsi_target="$TARGET"
101	echo "target: $TARGET" >> port-create.txt
102	atf_check egrep -q "Port created successfully" port-create.txt
103	atf_check egrep -q "frontend: *iscsi" port-create.txt
104	atf_check egrep -q "port: *[0-9]+" port-create.txt
105	atf_check -o save:portlist.txt ctladm portlist -qf iscsi
106	# Unlike the ioctl driver, the iscsi driver creates ports in a disabled
107	# state, so the port's lunmap may be set before enabling it.
108	atf_check egrep -q "$portnum *NO *iscsi *iscsi.*$TARGET" portlist.txt
109}
110create_iscsi_cleanup()
111{
112	cleanup iscsi
113}
114
115atf_test_case create_iscsi_alias cleanup
116create_iscsi_alias_head()
117{
118	atf_set "descr" "ctladm can create a new iscsi port with a target alias"
119	atf_set "require.user" "root"
120}
121create_iscsi_alias_body()
122{
123	skip_if_ctld
124	load_cfiscsi
125
126	TARGET=iqn.2018-10.myhost.create_iscsi_alias
127	ALIAS="foobar"
128	atf_check -o save:port-create.txt ctladm port -c -d "iscsi" -O cfiscsi_portal_group_tag=$PGTAG -O cfiscsi_target="$TARGET" -O cfiscsi_target_alias="$ALIAS"
129	echo "target: $TARGET" >> port-create.txt
130	atf_check egrep -q "Port created successfully" port-create.txt
131	atf_check egrep -q "frontend: *iscsi" port-create.txt
132	atf_check egrep -q "port: *[0-9]+" port-create.txt
133	atf_check -o save:portlist.txt ctladm portlist -qvf iscsi
134	atf_check egrep -q "cfiscsi_target_alias=$ALIAS" portlist.txt
135}
136create_iscsi_alias_cleanup()
137{
138	cleanup iscsi
139}
140
141atf_test_case create_iscsi_without_required_args
142create_iscsi_without_required_args_head()
143{
144	atf_set "descr" "ctladm will gracefully fail to create an iSCSI target if required arguments are missing"
145	atf_set "require.user" "root"
146}
147create_iscsi_without_required_args_body()
148{
149	skip_if_ctld
150	load_cfiscsi
151
152	TARGET=iqn.2018-10.myhost.create_iscsi
153	atf_check -s exit:1 -e match:"Missing required argument: cfiscsi_target" ctladm port -c -d "iscsi" -O cfiscsi_portal_group_tag=$PGTAG
154	atf_check -s exit:1 -e match:"Missing required argument: cfiscsi_portal_group_tag" ctladm port -c -d "iscsi" -O cfiscsi_target=$TARGET
155}
156
157atf_test_case create_ioctl_options cleanup
158create_ioctl_options_head()
159{
160	atf_set "descr" "ctladm can set options when creating a new ioctl port"
161	atf_set "require.user" "root"
162}
163create_ioctl_options_body()
164{
165	skip_if_ctld
166
167	atf_check -o save:port-create.txt ctladm port -c -d "ioctl" -O pp=101 -O vp=102
168	atf_check egrep -q "Port created successfully" port-create.txt
169	atf_check egrep -q "frontend: *ioctl" port-create.txt
170	atf_check egrep -q "port: *[0-9]+" port-create.txt
171	portnum=`awk '/port:/ {print $2}' port-create.txt`
172	atf_check -o save:portlist.txt ctladm portlist -qf ioctl
173	if ! egrep -q '101[[:space:]]+102' portlist.txt; then
174		ctladm portlist
175		atf_fail "Did not create the port with the specified options"
176	fi
177}
178create_ioctl_options_cleanup()
179{
180	cleanup ioctl
181}
182
183
184atf_test_case disable_ioctl cleanup
185disable_ioctl_head()
186{
187	atf_set "descr" "ctladm can disable an ioctl port"
188	atf_set "require.user" "root"
189}
190disable_ioctl_body()
191{
192	skip_if_ctld
193
194	atf_check -o save:port-create.txt ctladm port -c -d "ioctl"
195	portnum=`awk '/port:/ {print $2}' port-create.txt`
196	atf_check -o save:portlist.txt ctladm portlist -qf ioctl
197	atf_check -o ignore ctladm port -o off -p $portnum
198	atf_check -o match:"^$portnum *NO" ctladm portlist -qf ioctl
199}
200disable_ioctl_cleanup()
201{
202	cleanup ioctl
203}
204
205atf_test_case enable_ioctl cleanup
206enable_ioctl_head()
207{
208	atf_set "descr" "ctladm can enable an ioctl port"
209	atf_set "require.user" "root"
210}
211enable_ioctl_body()
212{
213	skip_if_ctld
214
215	atf_check -o save:port-create.txt ctladm port -c -d "ioctl"
216	portnum=`awk '/port:/ {print $2}' port-create.txt`
217	atf_check -o save:portlist.txt ctladm portlist -qf ioctl
218	atf_check -o ignore ctladm port -o off -p $portnum
219	atf_check -o ignore ctladm port -o on -p $portnum
220	atf_check -o match:"^$portnum *YES" ctladm portlist -qf ioctl
221}
222enable_ioctl_cleanup()
223{
224	cleanup ioctl
225}
226
227atf_test_case remove_ioctl
228remove_ioctl_head()
229{
230	atf_set "descr" "ctladm can remove an ioctl port"
231	atf_set "require.user" "root"
232}
233remove_ioctl_body()
234{
235	skip_if_ctld
236
237	# Specify exact pp and vp to make the post-removal portlist check
238	# unambiguous
239	atf_check -o save:port-create.txt ctladm port -c -d "ioctl" -O pp=10001 -O vp=10002
240	portnum=`awk '/port:/ {print $2}' port-create.txt`
241	atf_check -o save:portlist.txt ctladm portlist -qf ioctl
242	atf_check -o inline:"Port destroyed successfully\n" ctladm port -r -d ioctl -p $portnum
243	# Check that the port was removed.  A new port may have been added with
244	# the same ID, so match against the pp and vp numbers, too.
245	if ctladm portlist -qf ioctl | egrep -q "^${portnum} .*10001 *10002"; then
246		ctladm portlist -qf ioctl
247		atf_fail "port was not removed"
248	fi
249}
250
251atf_test_case remove_iscsi
252remove_iscsi_head()
253{
254	atf_set "descr" "ctladm can remove an iscsi port"
255	atf_set "require.user" "root"
256}
257remove_iscsi_body()
258{
259	skip_if_ctld
260	load_cfiscsi
261
262	TARGET=iqn.2018-10.myhost.remove_iscsi
263	atf_check -o save:port-create.txt ctladm port -c -d "iscsi" -O cfiscsi_portal_group_tag=$PGTAG -O cfiscsi_target="$TARGET"
264	portnum=`awk '/port:/ {print $2}' port-create.txt`
265	atf_check -o save:portlist.txt ctladm portlist -qf iscsi
266	atf_check -o inline:"Port destroyed successfully\n" ctladm port -r -d iscsi -O cfiscsi_portal_group_tag=$PGTAG -O cfiscsi_target="$TARGET"
267	# Check that the port was removed.  A new port may have been added with
268	# the same ID, so match against the target and tag, too.
269	PGTAGHEX=0x7631	# PGTAG in hex
270	if ctladm portlist -qf iscsi | egrep -q "^${portnum} .*$PGTAG +[0-9]+ +$TARGET,t,$PGTAGHEX"; then
271		ctladm portlist -qf iscsi
272		atf_fail "port was not removed"
273	fi
274}
275
276atf_test_case remove_iscsi_without_required_args cleanup
277remove_iscsi_without_required_args_head()
278{
279	atf_set "descr" "ctladm will gracefully fail to remove an iSCSI target if required arguments are missing"
280	atf_set "require.user" "root"
281}
282remove_iscsi_without_required_args_body()
283{
284	skip_if_ctld
285	load_cfiscsi
286
287	TARGET=iqn.2018-10.myhost.remove_iscsi_without_required_args
288	atf_check -o save:port-create.txt ctladm port -c -d "iscsi" -O cfiscsi_portal_group_tag=$PGTAG -O cfiscsi_target="$TARGET"
289	echo "target: $TARGET" >> port-create.txt
290	atf_check -s exit:1 -e match:"Missing required argument: cfiscsi_portal_group_tag" ctladm port -r -d iscsi -O cfiscsi_target="$TARGET"
291	atf_check -s exit:1 -e match:"Missing required argument: cfiscsi_target" ctladm port -r -d iscsi -O cfiscsi_portal_group_tag=$PGTAG
292}
293remove_iscsi_without_required_args_cleanup()
294{
295	cleanup iscsi
296}
297
298atf_init_test_cases()
299{
300	atf_add_test_case create_ioctl
301	atf_add_test_case create_iscsi
302	atf_add_test_case create_iscsi_without_required_args
303	atf_add_test_case create_iscsi_alias
304	atf_add_test_case create_ioctl_options
305	atf_add_test_case disable_ioctl
306	atf_add_test_case enable_ioctl
307	atf_add_test_case remove_ioctl
308	atf_add_test_case remove_ioctl_without_required_args
309	atf_add_test_case remove_iscsi
310	atf_add_test_case remove_iscsi_without_required_args
311}
312