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 function wait_process_start {
29 count=0
30 while [ $count -lt 10 ]; do
31 echo "--INFO: [$assertion]
32 Verify if testfoo.$$ is loaded and started"
33
34 pid=`ps -ef | grep testfoo.$$ | \
35 grep -v grep | awk '{print $2}' 2>/dev/null`
36 if [[ $? -eq 0 && ! -z $pid ]]; then
37 return 0
38 else
39 sleep 1
40 fi
41 count=`expr $count + 1`
42 done
43
44 #If test process not started by service in given time
45 #then fail and return 1
46
47 if [ $count -eq 10 ]; then
48 return 1
49 fi
50 }
51
52 #This function is to cleanup the leftovers by this test
53 function cleanup {
54 service_cleanup $test_service
55 /usr/bin/rm -f /var/tmp/$test_process
56
57 /usr/bin/rm -f $registration_file
58 /usr/bin/rm -f /var/tmp/testfoo.$$
59 /usr/bin/rm -f /var/tmp/$test_process
60
61 }
62
63 ###############################################################################
64 # start __stf_assertion__
65 #
66 # ASSERTION: svcadm_enable_006
67 #
68 # DESCRIPTION:
69 # Calling 'svcadm enable FMRI' over FMRI currently in disabled state
70 # should transit the state to enable and start any process associated
71 # with it.
72 # STRATEGY:
73 # - locate for template.xml in $STF_SUITE/tests/svcadm/
74 # - Create a simple test_process that sleeps for may be 10 secs.
75 # - Generate say 'svcadm_enable_006.$$.xml' using template.xml
76 # which contains say service='test_service',
77 # instance='test_instance'
78 # execname='test_process' for both start and stop event.
79 # - Import the xml using svccfg import <.xml>
80 # - Wait until test_service loads test_process.
81 # - Now attempt to disable the service.
82 # - Make sure svcadm disable <fmri> is successful with exit 0.
83 # - Wait for the transition period from enable to disable.
84 # - Also verify that 'test_process' is no more running it is STOPPED.
85 # - Make sure now 'fmri' state is disabled.
86 # - Now attempt to enable the FMRI
87 # - Verify that state is transited to "online" and process is started.
88 #
89 # COMMANDS: svcadm(1)
90 #
91 # end __stf_assertion__
92 ################################################################################
93
94 # First load up definitions of STF result variables like STF_PASS etc.
95 . ${STF_TOOLS}/include/stf.kshlib
96
97 # Load up definitions of shell functionality common to all smf sub-suites.
98 . ${STF_SUITE}/include/gltest.kshlib
99 . ${STF_SUITE}/include/svc.startd_config.kshlib
100
101 # Define Variables
102 readonly assertion=svcadm_enable_006
103 readonly ME=$(whence -p ${0})
104 readonly MYLOC=$(dirname ${ME})
105 readonly registration_template=${STF_SUITE}/tests/svcadm/enable/template.xml
106 readonly registration_file=/var/tmp/svcadm_enable_006.$$.xml
107 readonly test_service="enable_006$$"
108 readonly test_instance="enable_006$$"
109 readonly test_process=enable_006.$$
110 readonly fmri="svc:/$test_service:$test_instance"
111 pid=""
112
113 # Make sure we run as root
114 if ! /usr/bin/id | grep "uid=0(root)" > /dev/null 2>&1
115 then
116 RESULT=$(update_result $STF_UNRESOLVED $RESULT)
117 echo "--DIAG: [$assertion]
118 This test must be run from root."
119 print_result $RESULT
120 exit $RESULT
121 fi
122
123 # gltest.kshlib functions to extract and print assertion information
124 # from this source script.
125 extract_assertion_info $ME
126
127 # Initialize test result to pass.
128 typeset -i RESULT=${STF_UNRESOLVED}
129
130 # Set a trap to execute the cleanup function
131 trap cleanup 0 1 2 15
132
133 # Exit code for individual commands.
134 typeset -i tmp_rc=0
135
136 # Execute environmental sanity checks.
137 check_gl_env
138 tmp_rc=$?
139 if [[ $tmp_rc -ne 0 ]]
140 then
141 echo "--DIAG: [$assertion]
142 Invalid smf environment, quitting."
143 print_result $RESULT
144 exit $RESULT
145 fi
146
147 echo "--INFO: [$assertion]
148 Verify if required template is located"
149
150 if [ ! -s $registration_template ]; then
151 echo "--DIAG: [$assertion]
152 $registration_template is not located"
153 print_result $RESULT
154 exit $RESULT
155 fi
156
157 echo "--INFO: [$assertion]
158 Create a test process in /var/tmp/$test_process"
159
160 cat > /var/tmp/$test_process << EOF
161 #!/bin/ksh -p
162 print "#!/bin/ksh -p" > /var/tmp/testfoo.$$
163 print "/usr/bin/sleep 1000" >> /var/tmp/testfoo.$$
164 print "exit 0" >> /var/tmp/testfoo.$$
165 chmod 755 /var/tmp/testfoo.$$
166 /var/tmp/testfoo.$$ &
167 exit 0
168 EOF
169
170 echo "--INFO: [$assertion]
171 chmod 755 /var/tmp/$test_process"
172
173 chmod 755 /var/tmp/$test_process
174 if [ $? -ne 0 ]; then
175 echo "--DIAG: [$assertion]
176 chmod 755 /var/tmp/$test_process failed"
177 print_result $RESULT
178 exit $RESULT
179 fi
180
181 echo "--INFO: [$assertion]
182 Generate .xml required for this test using given template.xml"
183
184 manifest_generate $registration_template \
185 TEST_SERVICE=$test_service \
186 TEST_INSTANCE=$test_instance \
187 EXEC_NAME=/var/tmp/$test_process \
188 STOP_NAME=":kill" \
189 SERVICE_APP=$service_app \
190 STATEFILE=$service_state > $registration_file
191
192 echo "--INFO: [$assertion]
193 Verify if registration template is located and size > 0 bytes"
194
195 if [ ! -s $registration_file ]; then
196 echo "--DIAG: [$assertion]
197 $registration_file is not located"
198 print_result $RESULT
199 exit $RESULT
200 fi
201
202 echo "--INFO: [$assertion]
203 Import the service to repository using svccfg import"
204
205 svccfg import $registration_file > $svccfg_errfile 2>&1
206 if [ $? -ne 0 ]; then
207 print -- "--DIAG: $assertion: Unable to import the service $test_FMRI"
208 print -- " error messages from svccfg: \"$(cat $svccfg_errfile)\""
209 print_result $RESULT
210 exit $RESULT
211 fi
212
213 echo "--INFO: [$assertion]
214 Wait until the testfoo.$$ is loaded by $fmri"
215
216 #This function sets variable 'pid'
217
218 wait_process_start 2>/dev/null
219 if [ $? -ne 0 ]; then
220 echo "--DIAG: [$assertion]
221 testfoo.$$ not started by svccfg -v import $registration_file"
222 print_result $RESULT
223 exit $RESULT
224 fi
225
226 echo "--INFO: [$assertion]
227 svcs -p <FMRI> and make sure $pid is printed"
228 output=`svcs -p svc:/$test_service:$test_instance | grep -w $pid | \
229 awk '{print $2}' 2>/dev/null`
230 if [[ $? -ne 0 || "$output" != "$pid" ]]; then
231 echo "--DIAG: [$assertion]
232 svcs -p svc:/$test_service:$test_instance | grep $pid fails
233 EXPECTED: output = $pid
234 ACTUAL: output = $output"
235 print_result $RESULT
236 exit $RESULT
237 fi
238
239 echo "--INFO: [${assertion}]
240 disable <$fmri> using svcadm"
241
242 svcadm disable svc:/$test_service:$test_instance >/dev/null 2>&1
243 ret=$?
244 if [ $ret -ne 0 ]; then
245 echo "--DIAG: [$assertion]
246 svcadm disable svc:/$test_service:$test_instance fails
247 EXPECTED: output = ret 0
248 ACTUAL: output ret = $ret"
249 print_result $RESULT
250 exit $RESULT
251 fi
252
253 echo "--INFO: [${assertion}]
254 Wait until state transition is completed"
255
256 service_wait_state $test_service:$test_instance disabled
257 if [ $? -ne 0 ]; then
258 echo "--DIAG: [$assertion]
259 <$test_service> is not disabled"
260 print_result $RESULT
261 exit $RESULT
262 fi
263
264 echo "--INFO: [${assertion}]
265 Make sure svcs -p $test_service doesn't print $pid"
266
267 svcs -p svc:/$test_service:$test_instance | grep -w $pid >/dev/null 2>&1
268 if [ $? -eq 0 ]; then
269 echo "--DIAG: [$assertion]
270 svcs -p svc:/$test_service:$test_instance | grep $pid should
271 fail; It means svcs -p shouldn't print process info as the
272 process is dead"
273 print_result $RESULT
274 exit $RESULT
275 fi
276
277 echo "--INFO: [${assertion}]
278 Make sure process is really dead using ps"
279
280 ps -ef | grep -w $pid | grep -v grep >/dev/null 2>&1
281 if [ $? -eq 0 ]; then
282 echo "--DIAG: [$assertion]
283 ps -ef | grep $pid should fail"
284 print_result $RESULT
285 exit $RESULT
286 fi
287
288
289 echo "--INFO: [${assertion}]
290 enable <$fmri> using svcadm"
291
292 svcadm enable svc:/$test_service:$test_instance >/dev/null 2>&1
293 ret=$?
294 if [ $ret -ne 0 ]; then
295 RESULT=$(update_result $STF_FAIL $RESULT)
296 echo "--DIAG: [$assertion]
297 svcadm disable svc:/$test_service:$test_instance fails
298 EXPECTED: output = ret 0
299 ACTUAL: output ret = $ret"
300 print_result $RESULT
301 exit $RESULT
302 fi
303
304 echo "--INFO: [${assertion}]
305 Wait until STATE gets updated"
306
307 service_wait_state $test_service:$test_instance online
308 if [ $? -ne 0 ]; then
309 RESULT=$(update_result $STF_FAIL $RESULT)
310 echo "--DIAG: [$assertion]
311 <$test_service> is not online"
312 print_result $RESULT
313 exit $RESULT
314 fi
315
316 echo "--INFO: [$assertion]
317 Wait until the testfoo.$$ is loaded by $fmri"
318
319 #This function sets variable 'pid'
320
321 wait_process_start 2>/dev/null
322 if [ $? -ne 0 ]; then
323 RESULT=$(update_result $STF_FAIL $RESULT)
324 echo "--DIAG: [$assertion]
325 testfoo.$$ not started by svcadm enable $fmri"
326 print_result $RESULT
327 exit $RESULT
328 fi
329
330 echo "--INFO: [$assertion]
331 svcs -p <FMRI> and make sure $pid is printed"
332
333 output=`svcs -p svc:/$test_service:$test_instance | grep $pid | \
334 awk '{print $2}' 2>/dev/null`
335 if [[ $? -ne 0 || "$output" != "$pid" ]]; then
336 RESULT=$(update_result $STF_FAIL $RESULT)
337 echo "--DIAG: [$assertion]
338 svcs -p svc:/$test_service:$test_instance | grep $pid fails
339 EXPECTED: output = $pid
340 ACTUAL: output = $output"
341 print_result $RESULT
342 exit $RESULT
343 fi
344
345 RESULT=$STF_PASS
346 print_result $RESULT
347 exit $RESULT