def test_SyncListenServer_start_close(self): sync_ls = syncdata.SyncListenServer() os.kill(sync_ls.server_pid, 0) time.sleep(2) sync_ls.close() self.assertEqual(utils.pid_is_alive(sync_ls.server_pid), False, "Server should be dead.")
def cleanup(container, options): """Cleanup orphaned container. @param container: A Container object to be cleaned up. @param options: Options to do cleanup. @return: True if cleanup is successful. False otherwise. """ if not options.execute: logging.info('dryrun: Cleanup container %s', container.name) return False try: _, pid = get_info(container.name) # Kill autoserv process if pid and utils.pid_is_alive(pid): logging.info('Stopping process %s...', pid) utils.nuke_pid(int(pid), (signal.SIGKILL, )) # Destroy container logging.info('Destroying container %s...', container.name) container.destroy() return True except Exception as e: logging.error('Failed to cleanup container %s. Error: %s', container.name, e) return False
def test_SyncData_with_listenServer_auto_close(self): sync = syncdata.SyncData("127.0.0.1", "127.0.0.1", ["127.0.0.1"], "127.0.0.1#1") os.kill(sync.listen_server.server_pid, 0) data = sync.single_sync("test3") self.assertEqual(utils.pid_is_alive(sync.listen_server.server_pid), False, "Server should be dead.") self.assertEqual(data, {'127.0.0.1': 'test3'})
def test_SyncListenServer_start_with_tmp(self): tmpdir = "/tmp" sync_ls = syncdata.SyncListenServer(tmpdir=tmpdir) os.kill(sync_ls.server_pid, 0) time.sleep(2) sync_ls.close() self.assertEqual(utils.pid_is_alive(sync_ls.server_pid), False, "Server should be dead.") self.assertEqual(os.path.exists(tmpdir), True, "Tmpdir should exists after SyncListenServer" " finish with predefined tmpdir.")
def test_SyncListenServer_start_with_tmp(self): tmpdir = "/tmp" sync_ls = syncdata.SyncListenServer(tmpdir=tmpdir) os.kill(sync_ls.server_pid, 0) time.sleep(2) sync_ls.close() self.assertEqual(utils.pid_is_alive(sync_ls.server_pid), False, "Server should be dead.") self.assertEqual( os.path.exists(tmpdir), True, "Tmpdir should exists after SyncListenServer" " finish with predefined tmpdir.")
def close(self): """ Close SyncListenServer thread. Close all open connection with clients and listen server. """ utils.signal_pid(self.server_pid, signal.SIGTERM) if utils.pid_is_alive(self.server_pid): parallel.fork_waitfor_timed(self.tmpdir.name, self.server_pid, 2 * _DEFAULT_TIMEOUT) self.tmpdir.clean() logging.debug("SyncListenServer was killed.")
def is_container_orphaned(container): """Check if a container is orphaned. A container is orphaned if any of these condition is True: 1. The autoserv process created the container is no longer running. 2. The test job is finished at least 1 hour ago. @param container: A Container object. @return: True if the container is orphaned. """ logging.debug('Checking if container is orphaned: %s', container.name) if container.id is None: logging.debug('Container %s is not created for test.', container.name) return False job_id = container.id.job_id pid = container.id.pid if pid and not utils.pid_is_alive(pid): logging.debug( 'Process with PID %s is not alive, container %s is ' 'orphaned.', pid, container.name) return True try: hqes = AFE.get_host_queue_entries(job_id=job_id) except Exception as e: logging.error('Failed to get hqe for job %s. Error: %s.', job_id, e) return False if not hqes: # The job has not run yet. return False for hqe in hqes: if hqe.active or not hqe.complete: logging.debug( 'Test job %s is not completed yet, container %s is ' 'not orphaned.', job_id, container.name) return False if (hqe.finished_on and (time_utils.time_string_to_datetime( hqe.finished_on) > FINISHED_JOB_CUTOFF_TIME)): logging.debug('Test job %s was completed less than an hour ago.', job_id) return False logging.debug('Test job %s was completed, container %s is orphaned.', job_id, container.name) return True