1 #! /usr/bin/ksh -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 #
24 # Copyright 2008 Sun Microsystems, Inc. All rights reserved.
25 # Use is subject to license terms.
26 #
27
28 ###############################################################################
29 # start __stf_assertion__
30 #
31 # ASSERTION: svcadm_restart_003
32 #
33 # DESCRIPTION:
34 # Calling 'svcadm restart FMRI' where FMRI is a service (not instance)
35 # that is in the online state will result in the service being stopped
36 # and restarted. The exit status shall be 0.
37 #
38 # STRATEGY:
39 # - Generate a manifest for a test service
40 # - Enable the service using svccfg import <manifest>
41 # - Verify that the start method was triggered (start file should have
42 # been touched).
43 # - remove the start file.
44 # - Call svcadm restart (service) and make sure it exits 0.
45 # - Verify that the stop method and start method were triggered,
46 # by confirming that the stop file and start file were touched,
47 # and in that order.
48 #
49 # COMMANDS: svcadm(1M)
50 #
51 # end __stf_assertion__
52 ################################################################################
53
54 # First load up definitions of STF result variables like STF_PASS etc.
55 . ${STF_TOOLS}/include/stf.kshlib
56
57 # Load up definitions of shell functionality common to all smf sub-suites.
58 . ${STF_SUITE}/include/gltest.kshlib
59 . ${STF_SUITE}/include/svc.startd_config.kshlib
60
61 # Load up common functions for tests in this directory
62 . ${STF_SUITE}/${STF_EXEC}/functions.kshlib
63
64 # Define this test's cleanup function
65 function cleanup {
66 cleanup_leftovers $test_service $start_file $stop_file \
67 $start_process $stop_process $registration_file
68 print_result $RESULT
69 }
70
71 # Define Variables
72 readonly assertion=svcadm_restart_003
73 readonly ME=$(whence -p ${0})
74 readonly MYLOC=$(dirname ${ME})
75 readonly registration_template=${STF_SUITE}/tests/svcadm/restart/template.xml
76 readonly registration_file=/var/tmp/svcadm_restart_003.$$.xml
77 readonly test_service="smftest_svcadm"
78 readonly test_instance="$assertion"
79 readonly start_process="/var/tmp/$assertion.$$.start"
80 readonly stop_process="/var/tmp/$assertion.$$.stop"
81 readonly start_file="/var/tmp/$assertion.$$.startfile"
82 readonly stop_file="/var/tmp/$assertion.$$.stopfile"
83 readonly fmri="svc:/$test_service:$test_instance"
84
85 # Make sure we run as root
86 if ! /usr/bin/id | grep "uid=0(root)" > /dev/null 2>&1
87 then
88 RESULT=$(update_result $STF_UNRESOLVED $RESULT)
89 echo "--DIAG: [$assertion]
90 This test must be run from root."
91 exit $RESULT
92 fi
93
94 # Extract and print assertion information from this source script.
95 extract_assertion_info $ME
96
97 # Initialize test result to pass.
98 typeset -i RESULT=${STF_UNRESOLVED}
99
100 # Set a trap to execute the cleanup function
101 trap cleanup 0 1 2 15
102
103 # Exit code for individual commands.
104 typeset -i tmp_rc=0
105
106 # Execute environmental sanity checks.
107 check_gl_env
108 tmp_rc=$?
109 if [[ $tmp_rc -ne 0 ]]
110 then
111 echo "--DIAG: [$assertion]
112 Invalid smf environment, quitting."
113 exit $RESULT
114 fi
115
116 echo "--INFO: [$assertion]
117 Verify if required template exists"
118
119 if [ ! -s $registration_template ]; then
120 echo "--DIAG: [$assertion]
121 $registration_template does not exist"
122 exit $RESULT
123 fi
124
125 # Create the start and stop method scripts
126 echo "--INFO: [$assertion]
127 Create test processes in /var/tmp: $start_process, $stop_process"
128
129 cat > $start_process << EOF
130 #!/bin/ksh -p
131 /bin/rm -f $start_file >/dev/null 2>&1
132 sleep 1
133 /bin/date +\%Y\%m\%d\%H\%M\%S > $start_file
134 exit 0
135 EOF
136
137 cat > $stop_process << EOF
138 #!/bin/ksh -p
139 /bin/rm -f $stop_file >/dev/null 2>&1
140 /bin/date +\%Y\%m\%d\%H\%M\%S > $stop_file
141 exit 0
142 EOF
143
144 echo "--INFO: [$assertion]
145 chmod 755 $start_process"
146
147 chmod 755 $start_process
148 if [ $? -ne 0 ]; then
149 echo "--DIAG: [$assertion]
150 chmod 755 $start_process failed"
151 exit $RESULT
152 fi
153
154 echo "--INFO: [$assertion]
155 chmod 755 $stop_process"
156
157 chmod 755 $stop_process
158 if [ $? -ne 0 ]; then
159 echo "--DIAG: [$assertion]
160 chmod 755 $stop_process failed"
161 exit $RESULT
162 fi
163
164 #
165 # Generate the test service manifest.
166 echo "--INFO: [$assertion]
167 Generate .xml required for this test using given template.xml"
168
169 echo "--INFO: [$assertion]
170 The name of service is $test_service"
171
172 manifest_generate $registration_template \
173 TEST_SERVICE=$test_service \
174 TEST_INSTANCE=$test_instance \
175 START_NAME=$start_process \
176 STOP_NAME="$stop_process" \
177 SERVICE_APP=$service_app \
178 STATEFILE=$service_state > $registration_file
179
180 echo "--INFO: [$assertion]
181 Verify that registration template exists and size > 0 bytes"
182
183 if [ ! -s $registration_file ]; then
184 echo "--DIAG: [$assertion]
185 $registration_file not found or has size <= 0 bytes"
186 exit $RESULT
187 fi
188
189 # Import the service
190 echo "--INFO: [$assertion]
191 Import the service to repository using svccfg import"
192
193 svccfg import $registration_file > $svccfg_errfile 2>&1
194 if [ $? -ne 0 ]; then
195 echo "--DIAG: [$assertion]
196 Unable to import the service $test_service
197 Error messages from svccfg: \"$(cat $svccfg_errfile)\""
198 exit $RESULT
199 fi
200
201 echo "--INFO: [$assertion]
202 Wait until $start_process is triggered"
203
204 wait_process_start 2>/dev/null
205 if [ $? -ne 0 ]; then
206 echo "--DIAG: [$assertion]
207 $start_process not started"
208 exit $RESULT
209 fi
210
211 # Wait until service has transitioned to online
212 service_wait_state $fmri online
213 if [ $? -ne 0 ]; then
214 echo "--DIAG: [$assertion]
215 Restart failed for fmri <$fmri>:
216 stop and start methods executed, but
217 instance did not transition to online state
218 EXPECTED: state = online
219 OBSERVED: state = `svcprop -p restarter/state $fmri`"
220 exit $RESULT
221 fi
222
223 # Remove the $start_file and call svcadm restart. Expect that the stop
224 # and start methods are triggered, IN THAT ORDER.
225
226 /usr/bin/rm -f $start_file >/dev/null 2>&1
227 if [ -f $start_file ]; then
228 echo "--DIAG: [$assertion]
229 $start_file should have been removed, but still exists"
230 exit $RESULT
231 fi
232
233 echo "--INFO: [${assertion}]
234 restart <$test_service> using svcadm"
235
236 svcadm restart $test_service >/dev/null 2>&1
237 ret=$?
238 if [ $ret -ne 0 ]; then
239 RESULT=$(update_result $STF_FAIL $RESULT)
240 echo "--DIAG: [$assertion]
241 svcadm restart $test_service fails
242 EXPECTED: output = ret 0
243 ACTUAL: output ret = $ret"
244 exit $RESULT
245 fi
246
247 # Verify stop event is trigerred.
248
249 echo "--INFO: [$assertion]
250 Wait until $stop_process is triggered"
251
252 wait_process_stop $stop_file 2>/dev/null
253 if [ $? -ne 0 ]; then
254 RESULT=$(update_result $STF_FAIL $RESULT)
255 echo "--DIAG: [$assertion]
256 $stop_process not started"
257 exit $RESULT
258 fi
259
260 echo "--INFO: [$assertion]
261 Wait until $start_process is triggered"
262
263 # Verify that the start event is triggered.
264 wait_process_start 2>/dev/null
265 if [ $? -ne 0 ]; then
266 RESULT=$(update_result $STF_FAIL $RESULT)
267 echo "--DIAG: [$assertion]
268 $start_process not started"
269 exit $RESULT
270 fi
271
272 # Wait until the test instance is online
273 service_wait_state $fmri online
274 if [ $? -ne 0 ]; then
275 RESULT=$(update_result $STF_FAIL $RESULT)
276 echo "--DIAG: [$assertion]
277 Restart failed for fmri <$fmri>:
278 stop and start methods executed, but
279 instance did not transition to online state
280 EXPECTED: state = online
281 OBSERVED: state = `svcprop -p restarter/state $fmri`"
282 exit $RESULT
283 fi
284
285 # Verify that the start method was not triggered BEFORE the stop method.
286 echo "--INFO: [$assertion]
287 Verify that on restart stop method is executed before start method"
288
289 typeset -i stop_runtime=`cat $stop_file`
290 typeset -i start_runtime=`cat $start_file`
291
292 if [ $start_runtime -le $stop_runtime ]; then
293 RESULT=$(update_result $STF_FAIL $RESULT)
294 echo "--DIAG: [$assertion]
295 Restart error for service '$test_service':
296 start method was not rerun or was run before stop method
297 stop method run time: $stop_runtime
298 start method run time: $start_runtime"
299 exit $RESULT
300 fi
301
302 # Disable the test instance
303 svcadm disable $fmri >/dev/null 2>&1
304 ret=$?
305 if [ $ret -ne 0 ]; then
306 RESULT=$(update_result $STF_UNRESOLVED $RESULT)
307 echo "--DIAG: [$assertion]
308 svcadm disable $fmri failed
309 EXPECTED: ret = 0
310 OBSERVED: ret = $ret"
311 exit $RESULT
312 fi
313
314 # exit, trap calls cleanup
315 RESULT=$STF_PASS
316 print_result $RESULT
317 exit $RESULT
318
319 #
320 ### END
321 #