-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
195 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# This file is part of Buildbot. Buildbot 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 2. | ||
# | ||
# 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., 51 | ||
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
# | ||
# Copyright Buildbot Team Members | ||
|
||
import time | ||
import os | ||
import errno | ||
import signal | ||
from buildbot.scripts import base | ||
|
||
def stop(config, signame="TERM", wait=False): | ||
basedir = config['basedir'] | ||
quiet = config['quiet'] | ||
|
||
if not base.isBuildmasterDir(config['basedir']): | ||
print "not a buildmaster directory" | ||
return 1 | ||
|
||
pidfile = os.path.join(basedir, 'twistd.pid') | ||
try: | ||
with open(pidfile, "rt") as f: | ||
pid = int(f.read().strip()) | ||
except: | ||
if not config['quiet']: | ||
print "buildmaster not running" | ||
return 0 | ||
|
||
signum = getattr(signal, "SIG"+signame) | ||
try: | ||
os.kill(pid, signum) | ||
except OSError, e: | ||
if e.errno != errno.ESRCH: | ||
raise | ||
else: | ||
if not config['quiet']: | ||
print "buildmaster not running" | ||
try: | ||
os.unlink(pidfile) | ||
except: | ||
pass | ||
return 0 | ||
|
||
if not wait: | ||
if not quiet: | ||
print "sent SIG%s to process" % signame | ||
return 0 | ||
|
||
time.sleep(0.1) | ||
for _ in range(10): | ||
# poll once per second until twistd.pid goes away, up to 10 seconds | ||
try: | ||
os.kill(pid, 0) | ||
except OSError: | ||
if not quiet: | ||
print "buildbot process %d is dead" % pid | ||
return 0 | ||
time.sleep(1) | ||
if not quiet: | ||
print "never saw process go away" | ||
return 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# This file is part of Buildbot. Buildbot 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 2. | ||
# | ||
# 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., 51 | ||
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
# | ||
# Copyright Buildbot Team Members | ||
|
||
from __future__ import with_statement | ||
|
||
import os | ||
import time | ||
import signal | ||
from twisted.trial import unittest | ||
from buildbot.scripts import stop | ||
from buildbot.test.util import dirs, misc, compat | ||
|
||
def mkconfig(**kwargs): | ||
config = dict(quiet=False, basedir=os.path.abspath('basedir')) | ||
config.update(kwargs) | ||
return config | ||
|
||
class TestStop(misc.StdoutAssertionsMixin, dirs.DirsMixin, unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.setUpDirs('basedir') | ||
self.setUpStdoutAssertions() | ||
|
||
def tearDown(self): | ||
self.tearDownDirs() | ||
|
||
# tests | ||
|
||
def do_test_stop(self, config, kill_sequence, is_running=True, **kwargs): | ||
with open(os.path.join('basedir', 'buildbot.tac'), 'wt') as f: | ||
f.write("Application('buildmaster')") | ||
if is_running: | ||
with open("basedir/twistd.pid", 'wt') as f: | ||
f.write('1234') | ||
def sleep(t): | ||
what, exp_t = kill_sequence.pop(0) | ||
self.assertEqual((what, exp_t), ('sleep', t)) | ||
self.patch(time, 'sleep', sleep) | ||
def kill(pid, signal): | ||
exp_sig, result = kill_sequence.pop(0) | ||
self.assertEqual((pid,signal), (1234,exp_sig)) | ||
if isinstance(result, Exception): | ||
raise result | ||
else: | ||
return result | ||
self.patch(os, 'kill', kill) | ||
rv = stop.stop(config, **kwargs) | ||
self.assertEqual(kill_sequence, []) | ||
return rv | ||
|
||
@compat.skipUnlessPlatformIs('posix') | ||
def test_stop_not_running(self): | ||
rv = self.do_test_stop(mkconfig(), [], is_running=False) | ||
self.assertStdout('not running') | ||
self.assertEqual(rv, 0) | ||
|
||
@compat.skipUnlessPlatformIs('posix') | ||
def test_stop_dead_but_pidfile_remains(self): | ||
rv = self.do_test_stop(mkconfig(), | ||
[ (signal.SIGTERM, OSError(3, 'No such process')) ]) | ||
self.assertEqual(rv, 0) | ||
self.assertFalse(os.path.exists(os.path.join('basedir', 'twistd.pid'))) | ||
self.assertStdout('not running') | ||
|
||
@compat.skipUnlessPlatformIs('posix') | ||
def test_stop_dead_but_pidfile_remains_quiet(self): | ||
rv = self.do_test_stop(mkconfig(quiet=True), | ||
[ (signal.SIGTERM, OSError(3, 'No such process')) ],) | ||
self.assertEqual(rv, 0) | ||
self.assertFalse(os.path.exists(os.path.join('basedir', 'twistd.pid'))) | ||
self.assertWasQuiet() | ||
|
||
@compat.skipUnlessPlatformIs('posix') | ||
def test_stop_dead_but_pidfile_remains_wait(self): | ||
rv = self.do_test_stop(mkconfig(), | ||
[ (signal.SIGTERM, OSError(3, 'No such process')) ], | ||
wait=True) | ||
self.assertEqual(rv, 0) | ||
self.assertFalse(os.path.exists(os.path.join('basedir', 'twistd.pid'))) | ||
|
||
@compat.skipUnlessPlatformIs('posix') | ||
def test_stop_slow_death_wait(self): | ||
rv = self.do_test_stop(mkconfig(), [ | ||
(signal.SIGTERM, None), | ||
('sleep', 0.1), | ||
(0, None), # polling.. | ||
('sleep', 1), | ||
(0, None), | ||
('sleep', 1), | ||
(0, None), | ||
('sleep', 1), | ||
(0, OSError(3, 'No such process')), | ||
], | ||
wait=True) | ||
self.assertStdout('is dead') | ||
self.assertEqual(rv, 0) | ||
|
||
@compat.skipUnlessPlatformIs('posix') | ||
def test_stop_slow_death_wait_timeout(self): | ||
rv = self.do_test_stop(mkconfig(), [ | ||
(signal.SIGTERM, None), | ||
('sleep', 0.1), ] + | ||
[ (0, None), | ||
('sleep', 1), ] * 10, | ||
wait=True) | ||
self.assertStdout('never saw process') | ||
self.assertEqual(rv, 1) |