def test___init__(self): """Testing method or function named __init__.""" # Initialize key variables parent = 'parent' child = 'child' # Test - No Child tester = agent.Agent(parent, config=self.config) self.assertEqual(tester.parent, parent) expected = files.pid_file(parent, self.config) self.assertEqual(tester.pidfile_parent, expected) expected = files.lock_file(parent, self.config) self.assertEqual(tester.lockfile_parent, expected) expected = files.pid_file(None, self.config) self.assertEqual(tester._pidfile_child, expected) # Test - With Child tester = agent.Agent(parent, child=child, config=self.config) self.assertEqual(tester.parent, parent) expected = files.pid_file(parent, self.config) self.assertEqual(tester.pidfile_parent, expected) expected = files.lock_file(parent, self.config) self.assertEqual(tester.lockfile_parent, expected) expected = files.pid_file(child, self.config) self.assertEqual(tester._pidfile_child, expected)
def check_lockfile(): """Delete lockfile if found and ingester is not running. Args: None Returns: running: True if ingester script is running """ # Initialize key variables agent_name = 'pattoo_ingester' config = Config() lockfile = shared_files.lock_file(agent_name, config) # Script running running = sysinfo.process_running(PATTOO_INGESTER_SCRIPT) # Delete lockfile if found and ingester is not running. # Caused by possible crash. if os.path.exists(lockfile) is True and running is False: os.remove(lockfile) log_message = ('''\ Lock file {} found, but the {} script is not running\ '''.format(lockfile, PATTOO_INGESTER_SCRIPT)) log.log2warning(20030, log_message) return running
def __init__(self, parent, child=None, config=None): """Initialize the class. Args: parent: Name of parent daemon child: Name of child daemon config: Config object Returns: None """ # Initialize key variables (Parent) if config is None: self.config = Config() else: self.config = config self.parent = parent self.pidfile_parent = files.pid_file(parent, self.config) self.lockfile_parent = files.lock_file(parent, self.config) # Initialize key variables (Child) if bool(child) is None: self._pidfile_child = None else: self._pidfile_child = files.pid_file(child, self.config)
def test_lock_file(self): """Testing function lock_file.""" # Test filename = files._File(self.config) expected = filename.lock(self.prefix) result = files.lock_file(self.prefix, self.config) self.assertEqual(result, expected)
def test__lock(self): """Testing method / function _lock.""" # Initialize key variables config = ServerConfig() lockfile = files.lock_file(PATTOO_INGESTER_NAME, config) # Test self.assertFalse(os.path.isfile(lockfile)) result = files_test._lock() self.assertTrue(os.path.isfile(lockfile)) self.assertTrue(result) # Should fail result = files_test._lock() self.assertFalse(result) # Remove and test again result = files_test._lock(delete=True) self.assertTrue(result) self.assertFalse(os.path.isfile(lockfile)) result = files_test._lock() self.assertTrue(result) self.assertTrue(os.path.isfile(lockfile)) # Delete again to revert to known working state result = files_test._lock(delete=True) self.assertTrue(result)
def test___init__(self): """Testing function __init__.""" # Check daemon name matches agent name self.assertEqual(self._daemon.name, self._agent.name()) # Checking daemon pid_file expected = files.pid_file(self._agent.name(), self._config) self.assertEqual(self._daemon.pidfile, expected) # Checking daemon lock_file expected = files.lock_file(self._agent.name(), self._config) self.assertEqual(self._daemon.lockfile, expected)
def _lock(delete=False): """Create a lock file. Args: delete: Delete the file if true Returns: None """ # Initialize key variables config = Config() lockfile = files.lock_file(PATTOO_INGESTER_NAME, config) success = False # Lock if bool(delete) is False: if os.path.exists(lockfile) is True: log_message = ('''\ Lockfile {} exists. Will not start ingester script. Is another Ingester \ instance running? If not, delete the lockfile and rerun this script.\ '''.format(lockfile)) log.log2warning(20023, log_message) else: open(lockfile, 'a').close() success = True else: if os.path.exists(lockfile) is True: try: os.remove(lockfile) success = True except: log_message = ('Error deleting lockfile {}.'.format(lockfile)) log.log2warning(20107, log_message) else: log_message = ('Lockfile {} not found.'.format(lockfile)) log.log2warning(20108, log_message) return success