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