blob: bdf1395d0780988f42a5c458d84b17a8c3f5cb88 [file] [log] [blame]
Dean Troyer3159a822014-08-27 14:13:58 -05001#!/bin/bash
Chris Dent2f27a0e2014-09-09 13:46:02 +01002# tests/exec.sh - Test DevStack run_process() and stop_process()
Dean Troyer3159a822014-08-27 14:13:58 -05003#
4# exec.sh start|stop|status
5#
Chris Dent2f27a0e2014-09-09 13:46:02 +01006# Set USE_SCREEN True|False to change use of screen.
Dean Troyer3159a822014-08-27 14:13:58 -05007#
8# This script emulates the basic exec envirnment in ``stack.sh`` to test
9# the process spawn and kill operations.
10
11if [[ -z $1 ]]; then
12 echo "$0 start|stop"
13 exit 1
14fi
15
16TOP_DIR=$(cd $(dirname "$0")/.. && pwd)
17source $TOP_DIR/functions
18
19USE_SCREEN=${USE_SCREEN:-False}
20
21ENABLED_SERVICES=fake-service
22
23SERVICE_DIR=/tmp
24SCREEN_NAME=test
25SCREEN_LOGDIR=${SERVICE_DIR}/${SCREEN_NAME}
26
27
28# Kill background processes on exit
29trap clean EXIT
30clean() {
31 local r=$?
32 jobs -p
33 kill >/dev/null 2>&1 $(jobs -p)
34 exit $r
35}
36
37
38# Exit on any errors so that errors don't compound
39trap failed ERR
40failed() {
41 local r=$?
42 jobs -p
43 kill >/dev/null 2>&1 $(jobs -p)
44 set +o xtrace
45 [ -n "$LOGFILE" ] && echo "${0##*/} failed: full log in $LOGFILE"
46 exit $r
47}
48
49function status {
50 if [[ -r $SERVICE_DIR/$SCREEN_NAME/fake-service.pid ]]; then
51 pstree -pg $(cat $SERVICE_DIR/$SCREEN_NAME/fake-service.pid)
52 fi
53 ps -ef | grep fake
54}
55
56function setup_screen {
57if [[ ! -d $SERVICE_DIR/$SCREEN_NAME ]]; then
58 rm -rf $SERVICE_DIR/$SCREEN_NAME
59 mkdir -p $SERVICE_DIR/$SCREEN_NAME
60fi
61
62if [[ "$USE_SCREEN" == "True" ]]; then
63 # Create a new named screen to run processes in
64 screen -d -m -S $SCREEN_NAME -t shell -s /bin/bash
65 sleep 1
66
67 # Set a reasonable status bar
68 if [ -z "$SCREEN_HARDSTATUS" ]; then
69 SCREEN_HARDSTATUS='%{= .} %-Lw%{= .}%> %n%f %t*%{= .}%+Lw%< %-=%{g}(%{d}%H/%l%{g})'
70 fi
71 screen -r $SCREEN_NAME -X hardstatus alwayslastline "$SCREEN_HARDSTATUS"
72fi
73
74# Clear screen rc file
75SCREENRC=$TOP_DIR/tests/$SCREEN_NAME-screenrc
76if [[ -e $SCREENRC ]]; then
77 echo -n > $SCREENRC
78fi
79}
80
81# Mimic logging
82 # Set up output redirection without log files
83 # Copy stdout to fd 3
84 exec 3>&1
85 if [[ "$VERBOSE" != "True" ]]; then
86 # Throw away stdout and stderr
87 #exec 1>/dev/null 2>&1
88 :
89 fi
90 # Always send summary fd to original stdout
91 exec 6>&3
92
93
94if [[ "$1" == "start" ]]; then
95 echo "Start service"
96 setup_screen
Chris Dent2f27a0e2014-09-09 13:46:02 +010097 run_process fake-service "$TOP_DIR/tests/fake-service.sh"
Dean Troyer3159a822014-08-27 14:13:58 -050098 sleep 1
99 status
100elif [[ "$1" == "stop" ]]; then
101 echo "Stop service"
Chris Dent2f27a0e2014-09-09 13:46:02 +0100102 stop_process fake-service
Dean Troyer3159a822014-08-27 14:13:58 -0500103 status
104elif [[ "$1" == "status" ]]; then
105 status
106else
107 echo "Unknown command"
108 exit 1
109fi