def read_header(self, prefix, connection, data): """Test WriteFile() header format.""" # Delete log if it already exists (it shouldn't). log = prefix + '.log' self.delete_if_exists(log) # Create logging object. wf = WriteFile(prefix, connection) self.assertFalse(os.path.exists(log)) wf.write(data) wf.close() # Read log file header. self.assertTrue(os.path.exists(log)) with open(log, 'r') as f: lines = f.readlines() lines = [line for line in lines if line.startswith('#')] # Use hard-coded example for raw connections. if isinstance(connection, mcl.network.abstract.Connection): template = RAW_HEADER # Use hard-coded example for messages. else: template = MESSAGE_TEMPLATE # Make sure header is correctly formatted. for i, (line, template_line) in enumerate(zip(lines, template)): if i == 3: tokens = line.split() self.assertEqual('revision', tokens[2]) elif i == 4: tokens = line.split() self.assertEqual('created', tokens[2]) else: self.assertEqual(line, template_line + '\n') # Clean up after test. self.delete_if_exists(log)
def file_write(self, prefix, writes_per_split=2, split_delay=None, max_splits=2, max_entries=None, max_time=None): """Method for testing (split) logging.""" # Create file names. prefix = os.path.join(TMP_PATH, 'unittest') # Create logging object. wf = WriteFile(prefix, UnitTestMessageA, max_entries=max_entries, max_time=max_time) # Iterate through log file splits. for split in range(max_splits): # No splitting enabled if max_entries is None and max_time is None: tmp = prefix + '.tmp' log = prefix + '.log' # Split encountered. else: tmp = prefix + '_%03i.tmp' % split log = prefix + '_%03i.log' % split # Ensure files do not exist. self.delete_if_exists(tmp) self.delete_if_exists(log) # Write data to log file. for i in range(writes_per_split): j = (split * writes_per_split) + i message = { 'time_received': None, 'topic': 'test', 'payload': UnitTestMessageA(data=j) } wf.write(message) # File has not been closed. Ensure the file still has a temporary # extension. self.assertTrue(os.path.exists(tmp)) self.assertFalse(os.path.exists(log)) # Re-read split/log file (skip header). with open(tmp, 'r') as f: lines = f.readlines() lines = [line for line in lines if not line.startswith('#')] # Ensure data has been written to the file correctly. for i in range(writes_per_split): j = (split * writes_per_split) + i recorded_time, topic, payload = lines[i].split() self.assertEqual(topic, "'" + message['topic'] + "'") self.assertEqual( msgpack.loads(payload.decode('hex'))['data'], UnitTestMessageA(data=j)['data']) # No splits enabled. if max_entries is None and max_time is None: break # Pause before creating next split. if split_delay is not None: time.sleep(split_delay) # Ensure log file gets 'closed' - rotated from '.tmp' to '.log' # extension. wf.close() self.assertTrue(os.path.exists(log)) # Clean up after testing. for ext in ['tmp', 'log']: self.delete_if_exists(prefix + '.%s' % ext) for s_ext in ['_%03i.%s' % (i, ext) for i in range(max_splits)]: self.delete_if_exists(prefix + '%s' % s_ext)
def file_write(self, prefix, writes_per_split=2, split_delay=None, max_splits=2, max_entries=None, max_time=None): """Method for testing (split) logging.""" # Create file names. prefix = os.path.join(TMP_PATH, 'unittest') # Create logging object. wf = WriteFile(prefix, UnitTestMessageA, max_entries=max_entries, max_time=max_time) # Iterate through log file splits. for split in range(max_splits): # No splitting enabled if max_entries is None and max_time is None: tmp = prefix + '.tmp' log = prefix + '.log' # Split encountered. else: tmp = prefix + '_%03i.tmp' % split log = prefix + '_%03i.log' % split # Ensure files do not exist. self.delete_if_exists(tmp) self.delete_if_exists(log) # Write data to log file. for i in range(writes_per_split): j = (split * writes_per_split) + i message = {'time_received': None, 'topic': 'test', 'payload': UnitTestMessageA(data=j)} wf.write(message) # File has not been closed. Ensure the file still has a temporary # extension. self.assertTrue(os.path.exists(tmp)) self.assertFalse(os.path.exists(log)) # Re-read split/log file (skip header). with open(tmp, 'r') as f: lines = f.readlines() lines = [line for line in lines if not line.startswith('#')] # Ensure data has been written to the file correctly. for i in range(writes_per_split): j = (split * writes_per_split) + i recorded_time, topic, payload = lines[i].split() self.assertEqual(topic, "'" + message['topic'] + "'") self.assertEqual(msgpack.loads(payload.decode('hex'))['data'], UnitTestMessageA(data=j)['data']) # No splits enabled. if max_entries is None and max_time is None: break # Pause before creating next split. if split_delay is not None: time.sleep(split_delay) # Ensure log file gets 'closed' - rotated from '.tmp' to '.log' # extension. wf.close() self.assertTrue(os.path.exists(log)) # Clean up after testing. for ext in ['tmp', 'log']: self.delete_if_exists(prefix + '.%s' % ext) for s_ext in ['_%03i.%s' % (i, ext) for i in range(max_splits)]: self.delete_if_exists(prefix + '%s' % s_ext)