1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
#!/bin/sh
################################################################################
#
# Copyright 2021 Northern.tech AS
#
# This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
#
# To the extent this program is licensed as part of the Enterprise
# versions of CFEngine, the applicable Commercial Open Source License
# (COSL) may apply to this file if you as a licensee so wish it. See
# included file COSL.txt.
#
################################################################################
################################################################################
# Test whether the init script correctly starts and kills processes.
################################################################################
#
# Detect and replace non-POSIX shell
#
try_exec() {
type "$1" > /dev/null 2>&1 && exec "$@"
}
broken_posix_shell()
{
unset foo
local foo=1
test "$foo" != "1"
}
if broken_posix_shell >/dev/null 2>&1
then
try_exec /usr/xpg4/bin/sh "$0" "$@"
echo "No compatible shell script interpreter found."
echo "Please find a POSIX shell for your system."
exit 42
fi
################################################################################
# Preparation
################################################################################
export CFTEST_PREFIX=/tmp/init_script_test.sh.$$
# Use alternate binary names to avoid killing actual system processes.
export CFTEST_CFEXECD=$CFTEST_PREFIX/bin/cf-test-execd
export CFTEST_CFSERVD=$CFTEST_PREFIX/bin/cf-test-serverd
export CFTEST_CFMOND=$CFTEST_PREFIX/bin/cf-test-monitord
export CFTEST_CFAGENT=$CFTEST_PREFIX/bin/cf-test-agent
export OUTFILE=$CFTEST_PREFIX/outfile
rm -rf $CFTEST_PREFIX
mkdir -p $CFTEST_PREFIX/bin
mkdir -p $CFTEST_PREFIX/inputs
# CALL OURSELVES AND EXIT. W-H-Y-?
if [ "$1" != "sub-invocation" ]
then
# Redirect to log, and only print if there's an error.
if ! sh -x "$0" sub-invocation > $CFTEST_PREFIX/test-output.log 2>&1
then
# Sub-invocation FAILED!
echo "FAIL: Output from test:"
cat $CFTEST_PREFIX/test-output.log
exit 1
else
# Sub-invocation SUCCESS
exit 0
fi
fi
# Fail on any error.
set -e
cp init_script_test_helper $CFTEST_PREFIX/bin/cf-test-execd
cp init_script_test_helper $CFTEST_PREFIX/bin/cf-test-serverd
cp init_script_test_helper $CFTEST_PREFIX/bin/cf-test-monitord
cp init_script_test_helper $CFTEST_PREFIX/bin/cf-test-agent
touch $CFTEST_PREFIX/inputs/promises.cf
if ps --help | egrep -e '--cols\b' > /dev/null
then
# There is a bug in SUSE which means that ps output will be truncated even
# when piped to grep, if the terminal size is small. However using --cols
# will override it.
PS_OPTIONS="--cols 200"
else
PS_OPTIONS=
fi
################################################################################
# Functions
################################################################################
match_pid()
{
if [ "$(ps $PS_OPTIONS -ef|grep -v grep|grep "$1"|awk -F' ' '{print $2}')" != "" ]
then
return 0 # PID found
else
return 1 # PID not found
fi
}
matching_pid_exists()
{
if ! match_pid "$1"
then
echo "FAIL: No such process: $1"
return 1
fi
}
no_matching_pid_exists()
{
if match_pid "$1"
then
echo "FAIL: Unexpected process: $1"
return 1
fi
}
# Shortcut to check that no daemons/agents are running.
verify_none_running()
{
# Save some verbosity
( set +x
no_matching_pid_exists $CFTEST_PREFIX/bin/cf-test-execd
no_matching_pid_exists $CFTEST_PREFIX/bin/cf-test-serverd
no_matching_pid_exists $CFTEST_PREFIX/bin/cf-test-monitord
no_matching_pid_exists $CFTEST_PREFIX/bin/cf-test-agent
)
}
# Simply a workaround for the fact that '!' disables error checking with "set
# -e". By using it inside this function, the caller preserves error checking.
negate()
{
! "$@"
}
################################################################################
# Test
################################################################################
echo "TEST: Normal starting"
../../misc/init.d/cfengine3 start
matching_pid_exists $CFTEST_PREFIX/bin/cf-test-execd
matching_pid_exists $CFTEST_PREFIX/bin/cf-test-serverd
matching_pid_exists $CFTEST_PREFIX/bin/cf-test-monitord
echo "TEST: Normal stopping"
../../misc/init.d/cfengine3 stop
verify_none_running
echo "TEST: Stopping when a daemon is missing"
$CFTEST_PREFIX/bin/cf-test-execd
../../misc/init.d/cfengine3 stop
verify_none_running
$CFTEST_PREFIX/bin/cf-test-monitord
../../misc/init.d/cfengine3 stop
verify_none_running
echo "TEST: Stopping daemons and agents"
$CFTEST_PREFIX/bin/cf-test-execd --spawn-process $CFTEST_PREFIX/bin cf-test-agent
matching_pid_exists $CFTEST_PREFIX/bin/cf-test-execd
../../misc/init.d/cfengine3 stop
verify_none_running
echo "TEST: Stopping daemons and an agent launched just at signal time"
$CFTEST_PREFIX/bin/cf-test-execd --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent
matching_pid_exists $CFTEST_PREFIX/bin/cf-test-execd
no_matching_pid_exists $CFTEST_PREFIX/bin/cf-test-agent
../../misc/init.d/cfengine3 stop
verify_none_running
echo "TEST: Stopping a chain of daemons and agents each spawning each other"
$CFTEST_PREFIX/bin/cf-test-execd \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent
matching_pid_exists $CFTEST_PREFIX/bin/cf-test-execd
no_matching_pid_exists $CFTEST_PREFIX/bin/cf-test-agent
../../misc/init.d/cfengine3 stop
verify_none_running
echo "TEST: KILLing a bunch of agents and daemons"
# Stopping a ludicrously long chain of daemons and agents each spawning each
# other. This will test the SIGKILL capacity of the script, since normal killing
# won't be enough before the iteration count runs out.
$CFTEST_PREFIX/bin/cf-test-execd \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent
matching_pid_exists $CFTEST_PREFIX/bin/cf-test-execd
no_matching_pid_exists $CFTEST_PREFIX/bin/cf-test-agent
../../misc/init.d/cfengine3 stop
verify_none_running
echo "TEST: Stopping a daemon that just won't die"
$CFTEST_PREFIX/bin/cf-test-execd --refuse-to-die
matching_pid_exists $CFTEST_PREFIX/bin/cf-test-execd
../../misc/init.d/cfengine3 stop
verify_none_running
echo "TEST: Most extreme case, a long chain of daemons and agents where they all refuse to die"
$CFTEST_PREFIX/bin/cf-test-execd \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-execd --pass-to-next-process \
--refuse-to-die --spawn-process-on-signal $CFTEST_PREFIX/bin cf-test-agent
matching_pid_exists $CFTEST_PREFIX/bin/cf-test-execd
no_matching_pid_exists $CFTEST_PREFIX/bin/cf-test-agent
../../misc/init.d/cfengine3 stop
verify_none_running
echo "TEST: Verify that status of one daemon works"
verify_none_running
$CFTEST_PREFIX/bin/cf-test-execd
../../misc/init.d/cfengine3 status > $OUTFILE \
&& retcode=$? || retcode=$?
for i in cf-test-serverd cf-test-monitord
do
cat $OUTFILE | grep "$i.*is not running"
done
cat $OUTFILE | egrep "cf-test-execd.*(\.|is )running"
cat $OUTFILE | negate grep -i "Warning"
# When a process is missing retcode must be 3
test $retcode = 3
echo "TEST: Verify that status of all daemons works"
$CFTEST_PREFIX/bin/cf-test-serverd
$CFTEST_PREFIX/bin/cf-test-monitord
../../misc/init.d/cfengine3 status > $OUTFILE \
&& retcode=$? || retcode=$?
for i in cf-test-execd cf-test-serverd cf-test-monitord
do
cat $OUTFILE | egrep "$i.*(\.|is )running"
done
cat $OUTFILE | negate grep -i "is not running"
cat $OUTFILE | negate grep -i "Warning"
test $retcode = 0
echo "TEST: Verify that status of no daemons works"
../../misc/init.d/cfengine3 stop
verify_none_running
../../misc/init.d/cfengine3 status > $OUTFILE \
&& retcode=$? || retcode=$?
for i in cf-test-execd cf-test-serverd cf-test-monitord
do
cat $OUTFILE | grep "$i.*is not running"
done
cat $OUTFILE | negate egrep -i "(\.|is )running"
cat $OUTFILE | negate grep -i "Warning"
# When a process is missing retcode must be 3
test $retcode = 3
echo "TEST: Verify that we get warnings about multiple daemons"
verify_none_running
$CFTEST_PREFIX/bin/cf-test-execd
$CFTEST_PREFIX/bin/cf-test-execd
../../misc/init.d/cfengine3 status > $OUTFILE \
&& retcode=$? || retcode=$?
for i in cf-test-serverd cf-test-monitord
do
cat $OUTFILE | grep "$i.*is not running"
done
cat $OUTFILE | egrep "cf-test-execd.*(\.|is )running"
cat $OUTFILE | grep -i "Warning.*multiple"
test $retcode = 3 # TODO should the retcode really be 3 ?
echo "TEST: Verify that we get warnings about wrong PID file"
../../misc/init.d/cfengine3 stop
verify_none_running
$CFTEST_PREFIX/bin/cf-test-execd
echo 9999999 > $CFTEST_PREFIX/cf-test-execd.pid
../../misc/init.d/cfengine3 status > $OUTFILE \
&& retcode=$? || retcode=$?
for i in cf-test-serverd cf-test-monitord
do
cat $OUTFILE | grep "$i.*is not running"
done
cat $OUTFILE | egrep "cf-test-execd.*(\.|is )running"
cat $OUTFILE | grep -i "Warning.*$CFTEST_PREFIX/cf-test-execd.pid"
test $retcode = 3 # TODO should the retcode really be 3 ?
################################################################################
# Cleanup
################################################################################
../../misc/init.d/cfengine3 stop
rm -rf $CFTEST_PREFIX
|