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