def test_encoding_error(self): node = LogNode('one', None, self.dummy_file) with io.open(self.dummy_file, 'wb') as f: print(b'Here\x9cs a line', file=f) node.check_data(self.now) actual = node.data[0] expected = 'Heres a line\n' self.assertEqual(actual, expected)
class LogNodeTestCase(TestCase): def setUp(self): self.tmpdir = mkdtemp() self.dummy_file = join(self.tmpdir, 'dummy') self.node = LogNode('one', None, self.dummy_file) self.now = datetime.utcnow() def tearDown(self): rmtree(self.tmpdir) def test_check_data_none(self): # file doesn't exist yet self.node.check_data(self.now) self.assertIsNone(self.node.log_file) self.assertIsNone(self.node.log_file_inode) def test_check_data_created(self): # create the file and write a line with open(self.dummy_file, 'w') as f: print('hello', file=f) # should be saved, wasn't there at init self.node.check_data(self.now) self.assertEqual(self.node.data, ['hello\n']) self.assertIsNotNone(self.node.log_file) self.assertIsNotNone(self.node.log_file_inode) with open(self.dummy_file, 'a') as f: print('foo', file=f) # will be saved, new in this run self.node.check_data(self.now) self.assertEqual(self.node.data, ['hello\n', 'foo\n']) self.assertIsNotNone(self.node.log_file) self.assertIsNotNone(self.node.log_file_inode) def test_check_data_rolled(self): # create the file and write a line with open(self.dummy_file, 'w') as f: print('hello', file=f) self.node.check_data(self.now) self.assertEqual(self.node.data, ['hello\n']) self.assertIsNotNone(self.node.log_file) self.assertIsNotNone(self.node.log_file_inode) with open(self.dummy_file, 'a') as f: print('foo', file=f) # will be saved, new in this run self.node.check_data(self.now) self.assertEqual(self.node.data, ['hello\n', 'foo\n']) self.assertIsNotNone(self.node.log_file) self.assertIsNotNone(self.node.log_file_inode) # write one last line to the old file with open(self.dummy_file, 'a') as f: print('bar', file=f) # rename the file, should persist inode rename(self.dummy_file, '{}.{}'.format(self.dummy_file, 1)) # now create a new file with open(self.dummy_file, 'w') as f: print('bye', file=f) # should be saved, new file # first call should notice that the file has changed, grab the # remainder of the last file self.node.check_data(self.now) self.assertEqual(self.node.data, ['hello\n', 'foo\n', 'bar\n']) # second call will grab the new file self.node.check_data(self.now) self.assertEqual( self.node.data, ['hello\n', 'foo\n', 'bar\n', 'bye\n'] ) self.assertIsNotNone(self.node.log_file) self.assertIsNotNone(self.node.log_file_inode) # this time, we'll rename but not create with open(self.dummy_file, 'a') as f: print('hi', file=f) # should be saved, new file # rename the file, should persist inode rename(self.dummy_file, '{}.{}'.format(self.dummy_file, 1)) self.node.check_data(self.now) self.assertEqual( self.node.data, ['hello\n', 'foo\n', 'bar\n', 'bye\n', 'hi\n'] ) # because the file is rolling, no known next file exists self.assertIsNone(self.node.log_file) self.assertIsNone(self.node.log_file_inode) def test__set_fd(self): with open(self.dummy_file, 'w') as f: f.write('hello world') self.node._set_fd() inode1 = self.node.log_file_inode with open(self.dummy_file, 'r') as f: contents = f.read() self.assertEqual(self.node.log_file.read(), contents) self.node._set_fd(seek_to_end=True) inode2 = self.node.log_file_inode self.assertEqual(self.node.log_file.read(), '') self.assertEqual(inode1, inode2)
class LogNodeTestCase(TestCase): def setUp(self): self.tmpdir = mkdtemp() self.dummy_file = join(self.tmpdir, 'dummy') self.node = LogNode('one', None, self.dummy_file) self.now = datetime.utcnow() def tearDown(self): rmtree(self.tmpdir) def test_check_data_none(self): # file doesn't exist yet self.node.check_data(self.now) self.assertIsNone(self.node.log_file) self.assertIsNone(self.node.log_file_inode) def test_check_data_created(self): # create the file and write a line with open(self.dummy_file, 'w') as f: print('hello', file=f) # should be saved, wasn't there at init self.node.check_data(self.now) self.assertEqual(self.node.data, ['hello\n']) self.assertIsNotNone(self.node.log_file) self.assertIsNotNone(self.node.log_file_inode) with open(self.dummy_file, 'a') as f: print('foo', file=f) # will be saved, new in this run self.node.check_data(self.now) self.assertEqual(self.node.data, ['hello\n', 'foo\n']) self.assertIsNotNone(self.node.log_file) self.assertIsNotNone(self.node.log_file_inode) def test_check_data_rolled(self): # create the file and write a line with open(self.dummy_file, 'w') as f: print('hello', file=f) self.node.check_data(self.now) self.assertEqual(self.node.data, ['hello\n']) self.assertIsNotNone(self.node.log_file) self.assertIsNotNone(self.node.log_file_inode) with open(self.dummy_file, 'a') as f: print('foo', file=f) # will be saved, new in this run self.node.check_data(self.now) self.assertEqual(self.node.data, ['hello\n', 'foo\n']) self.assertIsNotNone(self.node.log_file) self.assertIsNotNone(self.node.log_file_inode) # write one last line to the old file with open(self.dummy_file, 'a') as f: print('bar', file=f) # rename the file, should persist inode rename(self.dummy_file, '{}.{}'.format(self.dummy_file, 1)) # now create a new file with open(self.dummy_file, 'w') as f: print('bye', file=f) # should be saved, new file # first call should notice that the file has changed, grab the # remainder of the last file self.node.check_data(self.now) self.assertEqual(self.node.data, ['hello\n', 'foo\n', 'bar\n']) # second call will grab the new file self.node.check_data(self.now) self.assertEqual( self.node.data, ['hello\n', 'foo\n', 'bar\n', 'bye\n'] ) self.assertIsNotNone(self.node.log_file) self.assertIsNotNone(self.node.log_file_inode) # this time, we'll rename but not create with open(self.dummy_file, 'a') as f: print('hi', file=f) # should be saved, new file # rename the file, should persist inode rename(self.dummy_file, '{}.{}'.format(self.dummy_file, 1)) self.node.check_data(self.now) self.assertEqual( self.node.data, ['hello\n', 'foo\n', 'bar\n', 'bye\n', 'hi\n'] ) # because the file is rolling, no known next file exists self.assertIsNone(self.node.log_file) self.assertIsNone(self.node.log_file_inode) @patch('ona_service.log_watcher.logging', autospec=True) def test__set_fd(self, mock_logging): with open(self.dummy_file, 'w') as f: print('Here is a line', file=f) print('Here is another line', file=f) self.node._set_fd() inode1 = self.node.log_file_inode with open(self.dummy_file, 'r') as f: contents = f.read() self.assertEqual(self.node.log_file.read(), contents) self.node._set_fd(seek_to_end=True) inode2 = self.node.log_file_inode self.assertEqual(self.node.log_file.read(), '') self.assertEqual(inode1, inode2) log_args = mock_logging.info.call_args[0] log_message = log_args[0] % log_args[1:] self.assertIn('skipped 2 lines', log_message) def test_encoding(self): node = LogNode('one', None, self.dummy_file, encoding='cp1252') with io.open(self.dummy_file, 'wb') as f: print(b'Here\x9cs a line', file=f) node.check_data(self.now) actual = node.data[0].encode('cp1252') expected = b'Here\x9cs a line\n' self.assertEqual(actual, expected) def test_encoding_error(self): node = LogNode('one', None, self.dummy_file) with io.open(self.dummy_file, 'wb') as f: print(b'Here\x9cs a line', file=f) node.check_data(self.now) actual = node.data[0] expected = 'Heres a line\n' self.assertEqual(actual, expected)
class LogNodeTestCase(TestCase): def setUp(self): self.tmpdir = mkdtemp() self.dummy_file = join(self.tmpdir, 'dummy') self.node = LogNode('one', None, self.dummy_file) self.now = datetime.utcnow() def tearDown(self): rmtree(self.tmpdir) def test_check_data_none(self): # file doesn't exist yet self.node.check_data(self.now) self.assertIsNone(self.node.log_file) self.assertIsNone(self.node.log_file_inode) def test_check_data_created(self): # create the file and write a line with open(self.dummy_file, 'w') as f: print('hello', file=f) # should be saved, wasn't there at init self.node.check_data(self.now) self.assertEqual(self.node.data, ['hello\n']) self.assertIsNotNone(self.node.log_file) self.assertIsNotNone(self.node.log_file_inode) with open(self.dummy_file, 'a') as f: print('foo', file=f) # will be saved, new in this run self.node.check_data(self.now) self.assertEqual(self.node.data, ['hello\n', 'foo\n']) self.assertIsNotNone(self.node.log_file) self.assertIsNotNone(self.node.log_file_inode) def test_check_data_rolled(self): # create the file and write a line with open(self.dummy_file, 'w') as f: print('hello', file=f) self.node.check_data(self.now) self.assertEqual(self.node.data, ['hello\n']) self.assertIsNotNone(self.node.log_file) self.assertIsNotNone(self.node.log_file_inode) with open(self.dummy_file, 'a') as f: print('foo', file=f) # will be saved, new in this run self.node.check_data(self.now) self.assertEqual(self.node.data, ['hello\n', 'foo\n']) self.assertIsNotNone(self.node.log_file) self.assertIsNotNone(self.node.log_file_inode) # write one last line to the old file with open(self.dummy_file, 'a') as f: print('bar', file=f) # rename the file, should persist inode rename(self.dummy_file, '{}.{}'.format(self.dummy_file, 1)) # now create a new file with open(self.dummy_file, 'w') as f: print('bye', file=f) # should be saved, new file # first call should notice that the file has changed, grab the # remainder of the last file self.node.check_data(self.now) self.assertEqual(self.node.data, ['hello\n', 'foo\n', 'bar\n']) # second call will grab the new file self.node.check_data(self.now) self.assertEqual(self.node.data, ['hello\n', 'foo\n', 'bar\n', 'bye\n']) self.assertIsNotNone(self.node.log_file) self.assertIsNotNone(self.node.log_file_inode) # this time, we'll rename but not create with open(self.dummy_file, 'a') as f: print('hi', file=f) # should be saved, new file # rename the file, should persist inode rename(self.dummy_file, '{}.{}'.format(self.dummy_file, 1)) self.node.check_data(self.now) self.assertEqual(self.node.data, ['hello\n', 'foo\n', 'bar\n', 'bye\n', 'hi\n']) # because the file is rolling, no known next file exists self.assertIsNone(self.node.log_file) self.assertIsNone(self.node.log_file_inode) def test__set_fd(self): with open(self.dummy_file, 'w') as f: f.write('hello world') self.node._set_fd() inode1 = self.node.log_file_inode with open(self.dummy_file, 'r') as f: contents = f.read() self.assertEqual(self.node.log_file.read(), contents) self.node._set_fd(seek_to_end=True) inode2 = self.node.log_file_inode self.assertEqual(self.node.log_file.read(), '') self.assertEqual(inode1, inode2)