class WatchNodeTestCase(TestCase): def setUp(self): self.mock_api = MagicMock() self.now = utcnow() self.later = self.now + timedelta(seconds=1) self.test_data = [b'line_1\n', b'line_2\n'] self.inst = WatchNode('test_type', self.mock_api, timedelta(seconds=1)) self.inst.last_send = self.now # Creates a temporary file in a known location self.tmpdir = mkdtemp() def fixed_temp_file(*args, **kwargs): return NamedTemporaryFile(delete=False, dir=self.tmpdir) self.fixed_temp_file = fixed_temp_file def tearDown(self): rmtree(self.tmpdir) def test_flush_data_compressed(self): patch_src = 'ona_service.log_watcher.NamedTemporaryFile' with patch(patch_src, self.fixed_temp_file): self.inst.flush_data(self.test_data, self.later, compress=True) # Gzip-read of the file should give back the input data for file_path in iglob(join(self.tmpdir, '*')): with gzip.open(file_path, 'rb') as infile: self.assertEqual(infile.read(), ''.join(self.test_data)) def test_flush_data_uncompressed(self): patch_src = 'ona_service.log_watcher.NamedTemporaryFile' with patch(patch_src, self.fixed_temp_file): self.inst.flush_data(self.test_data, self.later) # Direct read of the file should give back the input data for file_path in iglob(join(self.tmpdir, '*')): with open(file_path, 'r') as infile: self.assertEqual(infile.read(), ''.join(self.test_data)) def test_flush_data_calls(self): # No data -> no calls self.inst.flush_data([], self.later) self.assertEqual(self.mock_api.send_signal.call_count, 0) # Not enough time has passed -> no calls self.inst.flush_data(self.test_data, self.now) self.assertEqual(self.mock_api.send_signal.call_count, 0) # Data is present, enough time has passed -> one call self.inst.flush_data(self.test_data, self.later) self.assertEqual(self.mock_api.send_signal.call_count, 1)
def setUp(self): self.mock_api = MagicMock() self.now = utcnow() self.later = self.now + timedelta(seconds=1) self.test_data = [b'line_1\n', b'line_2\n'] self.inst = WatchNode('test_type', self.mock_api, timedelta(seconds=1)) self.inst.last_send = self.now # Creates a temporary file in a known location self.tmpdir = mkdtemp() def fixed_temp_file(*args, **kwargs): return NamedTemporaryFile(delete=False, dir=self.tmpdir) self.fixed_temp_file = fixed_temp_file