def test_SIGTERM(self): # SIGTERM should shut down the server whether daemonized or not. try: from signal import SIGTERM except ImportError: return self.skip("skipped (no SIGTERM) ") try: from os import kill except ImportError: return self.skip("skipped (no os.kill) ") # Spawn a normal, undaemonized process. p = helper.CPProcess(ssl=(self.scheme.lower() == 'https')) p.write_conf(extra='test_case_name: "test_SIGTERM"') p.start(imports='cherrypy.test._test_states_demo') # Send a SIGTERM os.kill(p.get_pid(), SIGTERM) # This might hang if things aren't working right, but meh. p.join() if os.name in ['posix']: # Spawn a daemonized process and test again. p = helper.CPProcess(ssl=(self.scheme.lower() == 'https'), wait=True, daemonize=True) p.write_conf(extra='test_case_name: "test_SIGTERM_2"') p.start(imports='cherrypy.test._test_states_demo') # Send a SIGTERM os.kill(p.get_pid(), SIGTERM) # This might hang if things aren't working right, but meh. p.join()
def test_SIGTERM(self): "SIGTERM should shut down the server whether daemonized or not." self._require_signal_and_kill('SIGTERM') # Spawn a normal, undaemonized process. p = helper.CPProcess(ssl=(self.scheme.lower() == 'https')) p.write_conf( extra='test_case_name: "test_SIGTERM"') p.start(imports='cherrypy.test._test_states_demo') # Send a SIGTERM os.kill(p.get_pid(), signal.SIGTERM) # This might hang if things aren't working right, but meh. p.join() if os.name in ['posix']: # Spawn a daemonized process and test again. p = helper.CPProcess(ssl=(self.scheme.lower() == 'https'), wait=True, daemonize=True) p.write_conf( extra='test_case_name: "test_SIGTERM_2"') p.start(imports='cherrypy.test._test_states_demo') # Send a SIGTERM os.kill(p.get_pid(), signal.SIGTERM) # This might hang if things aren't working right, but meh. p.join()
def test_SIGHUP_daemonized(self): try: from signal import SIGHUP except ImportError: return self.skip('skipped (no SIGHUP) ') if os.name not in ('posix', ): return self.skip('skipped (not on posix) ') p = helper.CPProcess(ssl=self.scheme.lower() == 'https', wait=True, daemonize=True) p.write_conf(extra='test_case_name: "test_SIGHUP_daemonized"') p.start(imports='cherrypy.test._test_states_demo') pid = p.get_pid() try: os.kill(pid, SIGHUP) time.sleep(2) self.getPage('/pid') self.assertStatus(200) new_pid = int(self.body) self.assertNotEqual(new_pid, pid) finally: self.getPage('/exit') p.join()
def test_signal_handler_unsubscribe(self): self._require_signal_and_kill('SIGTERM') # Although Windows has `os.kill` and SIGTERM is defined, the # platform does not implement signals and sending SIGTERM # will result in a forced termination of the process. # Therefore, this test is not suitable for Windows. if os.name == 'nt': self.skip("SIGTERM not available") # Spawn a normal, undaemonized process. p = helper.CPProcess(ssl=(self.scheme.lower() == 'https')) p.write_conf( extra="""unsubsig: True test_case_name: "test_signal_handler_unsubscribe" """) p.start(imports='cherrypy.test._test_states_demo') # Ask the process to quit os.kill(p.get_pid(), signal.SIGTERM) # This might hang if things aren't working right, but meh. p.join() # Assert the old handler ran. target_line = open(p.error_log, 'rb').readlines()[-10] if not ntob("I am an old SIGTERM handler.") in target_line: self.fail("Old SIGTERM handler did not run.\n%r" % target_line)
def test_daemonize(self): if os.name not in ['posix']: return self.skip("skipped (not on posix) ") self.HOST = '127.0.0.1' self.PORT = 8081 # Spawn the process and wait, when this returns, the original process # is finished. If it daemonized properly, we should still be able # to access pages. p = helper.CPProcess(ssl=(self.scheme.lower() == 'https'), wait=True, daemonize=True, socket_host='127.0.0.1', socket_port=8081) p.write_conf( extra='test_case_name: "test_daemonize"') p.start(imports='cherrypy.test._test_states_demo') try: # Just get the pid of the daemonization process. self.getPage("/pid") self.assertStatus(200) page_pid = int(self.body) self.assertEqual(page_pid, p.get_pid()) finally: # Shut down the spawned process self.getPage("/exit") p.join() # Wait until here to test the exit code because we want to ensure # that we wait for the daemon to finish running before we fail. if p.exit_code != 0: self.fail("Daemonized parent process failed to exit cleanly.")
def test_SIGHUP_daemonized(self): # When daemonized, SIGHUP should restart the server. try: from signal import SIGHUP except ImportError: return self.skip("skipped (no SIGHUP) ") if os.name not in ['posix']: return self.skip("skipped (not on posix) ") # Spawn the process and wait, when this returns, the original process # is finished. If it daemonized properly, we should still be able # to access pages. p = helper.CPProcess(ssl=(self.scheme.lower() == 'https'), wait=True, daemonize=True) p.write_conf( extra='test_case_name: "test_SIGHUP_daemonized"') p.start(imports='cherrypy.test._test_states_demo') pid = p.get_pid() try: # Send a SIGHUP os.kill(pid, SIGHUP) # Give the server some time to restart time.sleep(2) self.getPage("/pid") self.assertStatus(200) new_pid = int(self.body) self.assertNotEqual(new_pid, pid) finally: # Shut down the spawned process self.getPage("/exit") p.join()
def test_signal_handler_unsubscribe(self): try: from signal import SIGTERM except ImportError: return self.skip("skipped (no SIGTERM) ") try: from os import kill except ImportError: return self.skip("skipped (no os.kill) ") # Spawn a normal, undaemonized process. p = helper.CPProcess(ssl=(self.scheme.lower() == 'https')) p.write_conf(extra="""unsubsig: True test_case_name: "test_signal_handler_unsubscribe" """) p.start(imports='cherrypy.test._test_states_demo') # Send a SIGTERM os.kill(p.get_pid(), SIGTERM) # This might hang if things aren't working right, but meh. p.join() # Assert the old handler ran. target_line = open(p.error_log, 'rb').readlines()[-10] if not ntob("I am an old SIGTERM handler.") in target_line: self.fail("Old SIGTERM handler did not run.\n%r" % target_line)
def test_4_Autoreload(self): # If test_3 has not been executed, the server won't be stopped, # so we'll have to do it. if engine.state != engine.states.EXITING: engine.exit() # Start the demo script in a new process p = helper.CPProcess(ssl=(self.scheme.lower() == 'https')) p.write_conf(extra='test_case_name: "test_4_Autoreload"') p.start(imports='cherrypy.test._test_states_demo') try: self.getPage("/start") start = float(self.body) # Give the autoreloader time to cache the file time. time.sleep(2) # Touch the file os.utime(os.path.join(thisdir, "_test_states_demo.py"), None) # Give the autoreloader time to re-exec the process time.sleep(2) host = cherrypy.server.socket_host port = cherrypy.server.socket_port cherrypy._cpserver.wait_for_occupied_port(host, port) self.getPage("/start") if not (float(self.body) > start): raise AssertionError("start time %s not greater than %s" % (float(self.body), start)) finally: # Shut down the spawned process self.getPage("/exit") p.join()
def test_5_Start_Error(self): p = helper.CPProcess(ssl=self.scheme.lower() == 'https', wait=True) p.write_conf( extra='starterror: True\ntest_case_name: "test_5_Start_Error"\n') p.start(imports='cherrypy.test._test_states_demo') if p.exit_code == 0: self.fail('Process failed to return nonzero exit code.')
def test_4_Autoreload(self): # Start the demo script in a new process p = helper.CPProcess(ssl=(self.scheme.lower() == 'https')) p.write_conf(extra='test_case_name: "test_4_Autoreload"') p.start(imports='cherrypy.test.test_states_demo') try: self.getPage("/start") start = float(self.body) # Give the autoreloader time to cache the file time. time.sleep(2) # Touch the file os.utime(os.path.join(thisdir, "test_states_demo.py"), None) # Give the autoreloader time to re-exec the process time.sleep(2) host = cherrypy.server.socket_host port = cherrypy.server.socket_port cherrypy._cpserver.wait_for_occupied_port(host, port) self.getPage("/start") self.assert_(float(self.body) > start) finally: # Shut down the spawned process self.getPage("/exit") p.join()
def test_5_Start_Error(self): # If a process errors during start, it should stop the engine # and exit with a non-zero exit code. p = helper.CPProcess(ssl=(self.scheme.lower() == 'https'), wait=True) p.write_conf(extra="""starterror: True test_case_name: "test_5_Start_Error" """) p.start(imports='cherrypy.test._test_states_demo') if p.exit_code == 0: self.fail("Process failed to return nonzero exit code.")
def test_SIGHUP_tty(self): try: from signal import SIGHUP except ImportError: return self.skip('skipped (no SIGHUP) ') p = helper.CPProcess(ssl=self.scheme.lower() == 'https') p.write_conf(extra='test_case_name: "test_SIGHUP_tty"') p.start(imports='cherrypy.test._test_states_demo') os.kill(p.get_pid(), SIGHUP) p.join()
def test_5_Start_Error(self): if not self.server_class: print "skipped (no server) ", return # If a process errors during start, it should stop the engine # and exit with a non-zero exit code. p = helper.CPProcess(ssl=(self.scheme.lower() == 'https'), wait=True) p.write_conf(extra="starterror: True") p.start(imports='cherrypy.test.test_states_demo') if p.exit_code == 0: self.fail("Process failed to return nonzero exit code.")
def test_SIGTERM(self): try: from signal import SIGTERM except ImportError: return self.skip('skipped (no SIGTERM) ') try: from os import kill except ImportError: return self.skip('skipped (no os.kill) ') p = helper.CPProcess(ssl=self.scheme.lower() == 'https') p.write_conf(extra='test_case_name: "test_SIGTERM"') p.start(imports='cherrypy.test._test_states_demo') os.kill(p.get_pid(), SIGTERM) p.join() if os.name in ('posix',): p = helper.CPProcess(ssl=self.scheme.lower() == 'https', wait=True, daemonize=True) p.write_conf(extra='test_case_name: "test_SIGTERM_2"') p.start(imports='cherrypy.test._test_states_demo') os.kill(p.get_pid(), SIGTERM) p.join()
def test_SIGHUP_tty(self): # When not daemonized, SIGHUP should shut down the server. try: from signal import SIGHUP except ImportError: return self.skip("skipped (no SIGHUP) ") # Spawn the process. p = helper.CPProcess(ssl=(self.scheme.lower() == 'https')) p.write_conf(extra='test_case_name: "test_SIGHUP_tty"') p.start(imports='cherrypy.test._test_states_demo') # Send a SIGHUP os.kill(p.get_pid(), SIGHUP) # This might hang if things aren't working right, but meh. p.join()
def test_5_Start_Error(self): # If test_3 has not been executed, the server won't be stopped, # so we'll have to do it. if engine.state != engine.states.EXITING: engine.exit() # If a process errors during start, it should stop the engine # and exit with a non-zero exit code. p = helper.CPProcess(ssl=(self.scheme.lower() == 'https'), wait=True) p.write_conf(extra="""starterror: True test_case_name: "test_5_Start_Error" """) p.start(imports='cherrypy.test._test_states_demo') if p.exit_code == 0: self.fail('Process failed to return nonzero exit code.')
def test_engine(self): if os.name not in ['posix']: return self.skip("skipped (not on posix) ") p = helper.CPProcess(ssl=(self.scheme.lower() == 'https')) p.write_conf(extra='test_case_name: "test_engine"') p.start(imports='blueberrypy.tests._test_plugins_engine') try: self.getPage("/engine") self.assertStatus(200) self.assertEqual(u"Engine(sqlite://)", self.body.decode()) finally: self.getPage("/exit") p.join()
def test_4_Autoreload(self): p = helper.CPProcess(ssl=self.scheme.lower() == 'https') p.write_conf(extra='test_case_name: "test_4_Autoreload"') p.start(imports='cherrypy.test._test_states_demo') try: self.getPage('/start') start = float(self.body) time.sleep(2) os.utime(os.path.join(thisdir, '_test_states_demo.py'), None) time.sleep(2) host = cherrypy.server.socket_host port = cherrypy.server.socket_port cherrypy._cpserver.wait_for_occupied_port(host, port) self.getPage('/start') self.assert_(float(self.body) > start) finally: self.getPage('/exit') p.join()
def test_signal_handler_unsubscribe(self): try: from signal import SIGTERM except ImportError: return self.skip('skipped (no SIGTERM) ') try: from os import kill except ImportError: return self.skip('skipped (no os.kill) ') p = helper.CPProcess(ssl=self.scheme.lower() == 'https') p.write_conf(extra='unsubsig: True\ntest_case_name: "test_signal_handler_unsubscribe"\n') p.start(imports='cherrypy.test._test_states_demo') os.kill(p.get_pid(), SIGTERM) p.join() target_line = open(p.error_log, 'rb').readlines()[-10] if ntob('I am an old SIGTERM handler.') not in target_line: self.fail('Old SIGTERM handler did not run.\n%r' % target_line)
def test_daemonize(self): if os.name not in ('posix',): return self.skip('skipped (not on posix) ') self.HOST = '127.0.0.1' self.PORT = 8081 p = helper.CPProcess(ssl=self.scheme.lower() == 'https', wait=True, daemonize=True, socket_host='127.0.0.1', socket_port=8081) p.write_conf(extra='test_case_name: "test_daemonize"') p.start(imports='cherrypy.test._test_states_demo') try: self.getPage('/pid') self.assertStatus(200) page_pid = int(self.body) self.assertEqual(page_pid, p.get_pid()) finally: self.getPage('/exit') p.join() if p.exit_code != 0: self.fail('Daemonized parent process failed to exit cleanly.')
def test_SIGHUP_tty(self): # When not daemonized, SIGHUP should shut down the server. if not self.server_class: print "skipped (no server) ", return try: from signal import SIGHUP except ImportError: print "skipped (no SIGHUP) ", return # Spawn the process. p = helper.CPProcess(ssl=(self.scheme.lower() == 'https')) p.write_conf() p.start(imports='cherrypy.test.test_states_demo') # Send a SIGHUP os.kill(p.get_pid(), SIGHUP) # This might hang if things aren't working right, but meh. p.join()