1# $NetBSD: t_rc_d_cli.sh,v 1.6 2024/04/28 07:27:42 rillig Exp $
2#
3# Copyright (c) 2010 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# This code is derived from software contributed to The NetBSD Foundation
7# by Julio Merino.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29#
30
31atf_test_case no_command
32no_command_head() {
33	atf_set "descr" "Tests that the lack of a command errors out"
34}
35no_command_body() {
36	export h_simple=YES
37	rc_helper=$(atf_get_srcdir)/h_simple
38
39	atf_check -s exit:1 -o empty -e ignore ${rc_helper}
40}
41
42atf_test_case default_start_no_args
43default_start_no_args_head() {
44	atf_set "descr" "Tests that running the default 'start' without" \
45	    "arguments does not error out"
46}
47default_start_no_args_body() {
48	export h_simple=YES
49	rc_helper=$(atf_get_srcdir)/h_simple
50
51	atf_expect_fail "PR bin/56506"
52	atf_check -s exit:0 -o ignore -e empty ${rc_helper} start
53	${rc_helper} forcestop
54	atf_fail "random failure did not happen this time"
55}
56
57atf_test_case default_start_with_args
58default_start_with_args_head() {
59	atf_set "descr" "Tests that running the default 'start' with" \
60	    "arguments errors out"
61}
62default_start_with_args_body() {
63	export h_simple=YES
64	rc_helper=$(atf_get_srcdir)/h_simple
65
66	atf_check -s exit:1 -o ignore -e ignore ${rc_helper} start foo
67	if ${rc_helper} status >/dev/null; then
68		${rc_helper} forcestop
69		atf_fail 'extra argument to start did not error out'
70	fi
71}
72
73atf_test_case default_stop_no_args
74default_stop_no_args_head() {
75	atf_set "descr" "Tests that running the default 'stop' without" \
76	    "arguments does not error out"
77}
78default_stop_no_args_body() {
79	export h_simple=YES
80	rc_helper=$(atf_get_srcdir)/h_simple
81
82	atf_expect_fail "PR bin/56506"
83	${rc_helper} start
84	atf_check -s exit:0 -o ignore -e empty ${rc_helper} stop
85	atf_fail "random failure did not happen this time"
86}
87
88atf_test_case default_stop_with_args
89default_stop_with_args_head() {
90	atf_set "descr" "Tests that running the default 'stop' with" \
91	    "arguments errors out"
92}
93default_stop_with_args_body() {
94	export h_simple=YES
95	rc_helper=$(atf_get_srcdir)/h_simple
96
97	${rc_helper} start
98	atf_check -s exit:1 -o ignore -e ignore ${rc_helper} stop foo
99	if ${rc_helper} status >/dev/null; then
100		${rc_helper} forcestop
101	else
102		atf_fail 'extra argument to stop did not error out'
103	fi
104}
105
106atf_test_case default_restart_no_args
107default_restart_no_args_head() {
108	atf_set "descr" "Tests that running the default 'restart' without" \
109	    "arguments does not error out"
110}
111default_restart_no_args_body() {
112	export h_simple=YES
113	rc_helper=$(atf_get_srcdir)/h_simple
114
115	atf_expect_fail "PR bin/56506"
116	${rc_helper} start
117	atf_check -s exit:0 -o ignore -e empty ${rc_helper} restart
118	${rc_helper} forcestop
119	atf_fail "random failure did not happen this time"
120}
121
122atf_test_case default_restart_with_args
123default_restart_with_args_head() {
124	atf_set "descr" "Tests that running the default 'restart' with" \
125	    "arguments errors out"
126}
127default_restart_with_args_body() {
128	export h_simple=YES
129	rc_helper=$(atf_get_srcdir)/h_simple
130
131	${rc_helper} start
132	atf_check -s exit:1 -o ignore -e ignore ${rc_helper} restart foo
133	${rc_helper} forcestop
134}
135
136do_overriden_no_args() {
137	local command="${1}"; shift
138
139	export h_args=YES
140	rc_helper=$(atf_get_srcdir)/h_args
141
142	cat >expout <<EOF
143pre${command}:.
144${command}:.
145post${command}:.
146EOF
147	atf_check -s exit:0 -o file:expout -e empty ${rc_helper} ${command}
148}
149
150do_overriden_with_args() {
151	local command="${1}"; shift
152
153	export h_args=YES
154	rc_helper=$(atf_get_srcdir)/h_args
155
156	cat >expout <<EOF
157pre${command}:.
158${command}: >arg1< > arg 2 < >arg3< >*<.
159post${command}:.
160EOF
161	atf_check -s exit:0 -o file:expout -e empty ${rc_helper} ${command} \
162	    'arg1' ' arg 2 ' 'arg3' '*'
163}
164
165atf_test_case overriden_start_no_args
166overriden_start_no_args_head() {
167	atf_set "descr" "Tests that running a custom 'start' without" \
168	    "arguments does not pass any parameters to the command"
169}
170overriden_start_no_args_body() {
171	do_overriden_no_args start
172}
173
174atf_test_case overriden_start_with_args
175overriden_start_with_args_head() {
176	atf_set "descr" "Tests that running a custom 'start' with" \
177	    "arguments passes those arguments as parameters to the command"
178}
179overriden_start_with_args_body() {
180	do_overriden_with_args start
181}
182
183atf_test_case overriden_stop_no_args
184overriden_stop_no_args_head() {
185	atf_set "descr" "Tests that running a custom 'stop' without" \
186	    "arguments does not pass any parameters to the command"
187}
188overriden_stop_no_args_body() {
189	do_overriden_no_args stop
190}
191
192atf_test_case overriden_stop_with_args
193overriden_stop_with_args_head() {
194	atf_set "descr" "Tests that running a custom 'stop' with" \
195	    "arguments passes those arguments as parameters to the command"
196}
197overriden_stop_with_args_body() {
198	do_overriden_with_args stop
199}
200
201atf_test_case overriden_restart_no_args
202overriden_restart_no_args_head() {
203	atf_set "descr" "Tests that running a custom 'restart' without" \
204	    "arguments does not pass any parameters to the command"
205}
206overriden_restart_no_args_body() {
207	do_overriden_no_args restart
208}
209
210atf_test_case overriden_restart_with_args
211overriden_restart_with_args_head() {
212	atf_set "descr" "Tests that running a custom 'restart' with" \
213	    "arguments passes those arguments as parameters to the command"
214}
215overriden_restart_with_args_body() {
216	do_overriden_with_args restart
217}
218
219atf_test_case overriden_custom_no_args
220overriden_custom_no_args_head() {
221	atf_set "descr" "Tests that running a custom command without" \
222	    "arguments does not pass any parameters to the command"
223}
224overriden_custom_no_args_body() {
225	do_overriden_no_args custom
226}
227
228atf_test_case overriden_custom_with_args
229overriden_custom_with_args_head() {
230	atf_set "descr" "Tests that running a custom command with" \
231	    "arguments passes those arguments as parameters to the command"
232}
233overriden_custom_with_args_body() {
234	do_overriden_with_args custom
235}
236
237atf_init_test_cases()
238{
239	atf_add_test_case no_command
240
241	atf_add_test_case default_start_no_args
242	atf_add_test_case default_start_with_args
243	atf_add_test_case default_stop_no_args
244	atf_add_test_case default_stop_with_args
245	atf_add_test_case default_restart_no_args
246	atf_add_test_case default_restart_with_args
247
248	atf_add_test_case overriden_start_no_args
249	atf_add_test_case overriden_start_with_args
250	atf_add_test_case overriden_stop_no_args
251	atf_add_test_case overriden_stop_with_args
252	atf_add_test_case overriden_restart_no_args
253	atf_add_test_case overriden_restart_with_args
254	atf_add_test_case overriden_custom_no_args
255	atf_add_test_case overriden_custom_with_args
256}
257