"Fossies" - the Fresh Open Source Software Archive

Member "buildbot-2.5.1/buildbot/test/unit/test_scripts_stop.py" (24 Nov 2019, 5319 Bytes) of package /linux/misc/buildbot-2.5.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # This file is part of Buildbot.  Buildbot is free software: you can
    2 # redistribute it and/or modify it under the terms of the GNU General Public
    3 # License as published by the Free Software Foundation, version 2.
    4 #
    5 # This program is distributed in the hope that it will be useful, but WITHOUT
    6 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    7 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    8 # details.
    9 #
   10 # You should have received a copy of the GNU General Public License along with
   11 # this program; if not, write to the Free Software Foundation, Inc., 51
   12 # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   13 #
   14 # Copyright Buildbot Team Members
   15 
   16 import os
   17 import signal
   18 import time
   19 
   20 from twisted.trial import unittest
   21 
   22 from buildbot.scripts import stop
   23 from buildbot.test.util import dirs
   24 from buildbot.test.util import misc
   25 from buildbot.test.util.decorators import skipUnlessPlatformIs
   26 
   27 
   28 def mkconfig(**kwargs):
   29     config = dict(quiet=False, clean=False, basedir=os.path.abspath('basedir'))
   30     config['no-wait'] = kwargs.pop('no_wait', False)
   31     config.update(kwargs)
   32     return config
   33 
   34 
   35 class TestStop(misc.StdoutAssertionsMixin, dirs.DirsMixin, unittest.TestCase):
   36 
   37     def setUp(self):
   38         self.setUpDirs('basedir')
   39         self.setUpStdoutAssertions()
   40 
   41     def tearDown(self):
   42         self.tearDownDirs()
   43 
   44     # tests
   45 
   46     def do_test_stop(self, config, kill_sequence, is_running=True, **kwargs):
   47         with open(os.path.join('basedir', 'buildbot.tac'), 'wt') as f:
   48             f.write("Application('buildmaster')")
   49         if is_running:
   50             with open("basedir/twistd.pid", 'wt') as f:
   51                 f.write('1234')
   52 
   53         def sleep(t):
   54             self.assertTrue(kill_sequence, "unexpected sleep: %d" % t)
   55             what, exp_t = kill_sequence.pop(0)
   56             self.assertEqual((what, exp_t), ('sleep', t))
   57         self.patch(time, 'sleep', sleep)
   58 
   59         def kill(pid, signal):
   60             self.assertTrue(kill_sequence, "unexpected signal: %d" % signal)
   61             exp_sig, result = kill_sequence.pop(0)
   62             self.assertEqual((pid, signal), (1234, exp_sig))
   63             if isinstance(result, Exception):
   64                 raise result
   65             return result
   66         self.patch(os, 'kill', kill)
   67         rv = stop.stop(config, **kwargs)
   68         self.assertEqual(kill_sequence, [])
   69         return rv
   70 
   71     @skipUnlessPlatformIs('posix')
   72     def test_stop_not_running(self):
   73         rv = self.do_test_stop(mkconfig(no_wait=True), [], is_running=False)
   74         self.assertInStdout('not running')
   75         self.assertEqual(rv, 0)
   76 
   77     @skipUnlessPlatformIs('posix')
   78     def test_stop_dead_but_pidfile_remains(self):
   79         rv = self.do_test_stop(mkconfig(no_wait=True),
   80                                [(signal.SIGTERM, OSError(3, 'No such process'))])
   81         self.assertEqual(rv, 0)
   82         self.assertFalse(os.path.exists(os.path.join('basedir', 'twistd.pid')))
   83         self.assertInStdout('not running')
   84 
   85     @skipUnlessPlatformIs('posix')
   86     def test_stop_dead_but_pidfile_remains_quiet(self):
   87         rv = self.do_test_stop(mkconfig(quiet=True, no_wait=True),
   88                                [(signal.SIGTERM, OSError(3, 'No such process'))],)
   89         self.assertEqual(rv, 0)
   90         self.assertFalse(os.path.exists(os.path.join('basedir', 'twistd.pid')))
   91         self.assertWasQuiet()
   92 
   93     @skipUnlessPlatformIs('posix')
   94     def test_stop_dead_but_pidfile_remains_wait(self):
   95         rv = self.do_test_stop(mkconfig(no_wait=True),
   96                                [(signal.SIGTERM, OSError(3, 'No such process'))
   97                                 ],
   98                                wait=True)
   99         self.assertEqual(rv, 0)
  100         self.assertFalse(os.path.exists(os.path.join('basedir', 'twistd.pid')))
  101 
  102     @skipUnlessPlatformIs('posix')
  103     def test_stop_slow_death_wait(self):
  104         rv = self.do_test_stop(mkconfig(no_wait=True), [
  105             (signal.SIGTERM, None),
  106             ('sleep', 0.1),
  107             (0, None),  # polling..
  108             ('sleep', 1),
  109             (0, None),
  110             ('sleep', 1),
  111             (0, None),
  112             ('sleep', 1),
  113             (0, OSError(3, 'No such process')),
  114         ],
  115             wait=True)
  116         self.assertInStdout('is dead')
  117         self.assertEqual(rv, 0)
  118 
  119     @skipUnlessPlatformIs('posix')
  120     def test_stop_slow_death_wait_timeout(self):
  121         rv = self.do_test_stop(mkconfig(no_wait=True), [
  122             (signal.SIGTERM, None),
  123             ('sleep', 0.1), ] +
  124             [(0, None),
  125              ('sleep', 1), ] * 10,
  126             wait=True)
  127         self.assertInStdout('never saw process')
  128         self.assertEqual(rv, 1)
  129 
  130     @skipUnlessPlatformIs('posix')
  131     def test_stop_slow_death_config_wait_timeout(self):
  132         rv = self.do_test_stop(mkconfig(), [
  133             (signal.SIGTERM, None),
  134             ('sleep', 0.1), ] +
  135             [(0, None),
  136              ('sleep', 1), ] * 10,
  137         )
  138         self.assertInStdout('never saw process')
  139         self.assertEqual(rv, 1)
  140 
  141     @skipUnlessPlatformIs('posix')
  142     def test_stop_clean(self):
  143         rv = self.do_test_stop(mkconfig(clean=True, no_wait=True), [
  144             (signal.SIGUSR1, None), ], wait=False)
  145         self.assertInStdout('sent SIGUSR1 to process')
  146         self.assertEqual(rv, 0)