def check_status_file( backed_up_file, threshold, access_file_method = None, source_file = None, config_dict = None): """ check the backed up and determine whether it is within limits backed_up_file - location of the status file post backup threshold - number of days allowed before a problem access_file_method - optional method to access the backup """ logging.debug('run_test_part2 - checking %s. Threshold %d', backed_up_file, threshold) try : if access_file_method : assert source_file | config_dict # Go retrieve the file access_file_method(backed_up_file, source_file, config_dict) backed_up_sync = SyncData(backed_up_file) status = backed_up_sync.is_sync_late(threshold) except UtilError as e : logging.error('run_test_part1 status update failed -> %s', str(e)) status = -1 return status
def test_valid_write_SyncData(self) : """ Write out a valid file - should be no exceptions """ sync_data = SyncData(HOST_NAME, GOOD_TEST_PATH, TEST_NAME) sync_data.set_last_stored() self.assertIsNone(None)
def run_originator_tests(hostname, testdir) : """ hostname - us testlist - contains details of the tests to be run pick out those tests that apply and generate/update the file """ logging.debug('sync_tests.py::run_originator_tests -> %s', hostname) # Run through the list and look for ones applicable to this host for testname, sync_test in testdir.items(): try: logging.debug('checking %s -> %s', testname, sync_test) if hostname == sync_test.origination_host : # We've found one - update the status logging.info('Updating source for %s', sync_test.testname) sync_data = SyncData( hostname, sync_test.origination_path, sync_test.testname ) # @todo determin whether to add a reverse check option for # syncs rather than backups sync_data.set_last_stored() except UtilError as util_error : logging.error('run_originator_test - failed -> %s\n%s', str(util_error), str(sync_test))
def main() : """ Mess with dates for testing purposes """ # Get the command line arguments p = optparse.OptionParser() p.add_option("--source_dir", action="store", dest="source_dir") p.add_option("--test_name", action="store", dest="test_name") opts, source_file_args = p.parse_args() #pylint: disable-msg=W0612 hostname = socket.gethostname().split('.')[0] while True : print "Enter Data or Alert D|A -> " mode = sys.stdin.readline() print "Enter New Date -> " date_string = sys.stdin.readline() date_object = datetime.strptime(date_string.rstrip(), '%Y %m %d %H:%M') if mode.rstrip() == 'D' : sync_data = SyncData(hostname, opts.source_dir, opts.test_name) sync_data.variable = date_object sync_data.write() else : sync_alert = SyncDataAlert(hostname, opts.source_dir, opts.test_name) sync_alert.variable = date_object sync_alert.write()
def test_corrupted_salt_SyncData(self): """ Corrupt the salt before writing """ sync_data = SyncData(HOST_NAME, GOOD_TEST_PATH, TEST_NAME) sync_data.salt = CORRUPT_SALT sync_data.set_last_stored() self.assertRaises(UtilError, SyncData, host = HOST_NAME, dir_path = GOOD_TEST_PATH, testname = TEST_NAME)
def test_good_write1_SyncData(self) : """ Write a good file based on last_stored and check its restored ok """ sync_data = SyncData(HOST_NAME, GOOD_TEST_PATH, TEST_NAME) sync_data.set_last_stored() last_stored = sync_data.last_stored sync_data = SyncData(HOST_NAME, GOOD_TEST_PATH, TEST_NAME) self.assertEquals(last_stored, sync_data.last_stored)
def test_clockdrift_SyncData(self) : """ Check that delay checks are properly implemented """ sync_data = SyncData(HOST_NAME, GOOD_TEST_PATH, TEST_NAME) sync_data.set_last_stored() sync_data.last_stored = sync_data.last_stored + GOOD_DIFF_TIME sync_data.variable = sync_data.last_stored sync_data.write() sync_data = SyncData( HOST_NAME, GOOD_TEST_PATH, TEST_NAME) self.assertEquals(sync_data.is_sync_late(2), POTENTIAL_CLOCK_DRIFT)
def generate_status_file(testname, host, source_file) : """ update the status file """ logging.debug('Set status for %s@%s -> %s', testname, host, source_file) ret_code = 0 try: sync = SyncData(source_file) sync.set_last_stored() except UtilError as e: logging.error('run_test_part1 (%s@%s) - status update failed -> %s', testname, host, str(e)) ret_code = -1 return ret_code
def test_delay_SyncData(self) : """ Check that delay value is set correctly """ sync_data = SyncData(HOST_NAME, GOOD_TEST_PATH, TEST_NAME) self.assertEquals(sync_data.delay(), 0) sync_data = SyncData(HOST_NAME, GOOD_TEST_PATH, TEST_NAME) sync_data.set_last_stored() sync_data = SyncData(HOST_NAME, GOOD_TEST_PATH, TEST_NAME) self.assertNotEquals(sync_data.delay(), 0)
def test_islate_SyncData(self) : """ Check that delay checks are properly implemented """ sync_data = SyncData(HOST_NAME, GOOD_TEST_PATH, TEST_NAME) self.assertEquals(sync_data.is_sync_late(2), SYNC_LATE) sync_data.set_last_stored() self.assertEquals(sync_data.is_sync_late(2), SYNC_OK) sync_data.last_stored = sync_data.last_stored - GOOD_DIFF_TIME sync_data.variable = sync_data.last_stored sync_data.write() sync_data = SyncData( HOST_NAME, GOOD_TEST_PATH, TEST_NAME) self.assertEquals(sync_data.is_sync_late(1.9), SYNC_LATE)
def run_destination_tests(hostname, data_dir, testlist, alert_function): """ hostname - us data_dir - where the alert tracking files are stored testlist - contains details of the files we are looking for Methodology is simple * if the file is late and an alert is outstanding - generate one * if the file is on time and an alert has been sent - send out an all clear """ logging.debug('sync_tests.py::run_destination_tests -> %s', hostname) # Run through the list and look for ones applicable to this host for testname, sync_test in testlist.iteritems(): logging.debug('checking %s -> %s', hostname, testname) if hostname == sync_test.destination_host : # We've found one - get the status and check it logging.info('running test %s', testname) sync_data = SyncData( sync_test.origination_host, sync_test.destination_path, sync_test.testname ) sync_alert = SyncDataAlert( hostname, data_dir, sync_test.testname) is_sync_late = sync_data.is_sync_late(sync_test.max_delay) if is_sync_late == SyncData.SYNC_LATE : # We're late - see if we have to do anthing if sync_alert.alert_due(sync_test.alert_frequency) : logging.info('Late -> Alerting') # An Alert is due so send one out sync_alert.set_last_alerted() delay = sync_data.delay() if int(delay) == 0 : delay = "No Sender Detected" alert_function(SYNC_ALERT, sync_test, delay) else : logging.info("Late but alerted -> %s", sync_alert.last_alerted) elif is_sync_late == SyncData.POTENTIAL_CLOCK_DRIFT : logging.error('Clock Drift') # We're late - see if we have to do anthing if sync_alert.alert_due(sync_test.alert_frequency) : logging.info('Alerting') # An Alert is due so send one out sync_alert.set_last_alerted() delay = sync_data.delay() alert_function(CLOCK_DRIFT, sync_test, delay) else : # We're on time - see if something had previously been sent if sync_alert.alerted() : logging.info('Synchronisation has caught up') # It had - issue the all clear and reset alert_function(SYNC_ALL_CLEAR, sync_test) sync_alert.reset() logging.debug('sync_tests.py::run_destination_tests - completed')
def test_sync(): """unit test""" sync = SyncData() conf = sync.get_config() assert isinstance(conf['hostname'], str)