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