def test_purge_error_storage_response(self, event_loop, conf, expected_return): """Test that purge_data logs error when storage purge returns an error response""" @asyncio.coroutine def mock_audit_info(): return "" mockStorageClient = MagicMock(spec=StorageClient) mockAuditLogger = AuditLogger(mockStorageClient) with patch.object(FoglampProcess, '__init__'): with patch.object(mockAuditLogger, "__init__", return_value=None): p = Purge(loop=event_loop) p._logger = logger p._logger.info = MagicMock() p._logger.error = MagicMock() p._storage = MagicMock(spec=StorageClient) p._readings_storage = MagicMock(spec=ReadingsStorageClient) audit = p._audit with patch.object(p._readings_storage, 'purge', side_effect=self.store_purge): with patch.object(audit, 'information', return_value=mock_audit_info()): assert expected_return == p.purge_data(conf)
def test_purge_data_invalid_conf(self, event_loop, conf, expected_error_key): """Test that purge_data raises exception when called with invalid configuration""" @asyncio.coroutine def mock_audit_info(): return "" mockStorageClient = MagicMock(spec=StorageClient) mockAuditLogger = AuditLogger(mockStorageClient) with patch.object(FoglampProcess, '__init__'): with patch.object(mockAuditLogger, "__init__", return_value=None): p = Purge(loop=event_loop) p._logger = logger p._logger.info = MagicMock() p._logger.error = MagicMock() p._storage = MagicMock(spec=StorageClient) p._readings_storage = MagicMock(spec=ReadingsStorageClient) audit = p._audit with patch.object(p._readings_storage, 'purge', side_effect=self.store_purge): with patch.object(audit, 'information', return_value=mock_audit_info()): # Test the code block when purge failed because of invalid configuration p.purge_data(conf) p._logger.error.assert_called_with( 'Configuration item {} bla should be integer!'. format(expected_error_key))
def test_purge_data(self, event_loop, conf, expected_return, expected_calls): """Test that purge_data calls Storage's purge with defined configuration""" @asyncio.coroutine def mock_audit_info(): return "" mockStorageClient = MagicMock(spec=StorageClient) mockAuditLogger = AuditLogger(mockStorageClient) with patch.object(FoglampProcess, '__init__'): with patch.object(mockAuditLogger, "__init__", return_value=None): p = Purge(loop=event_loop) p._logger = logger p._logger.info = MagicMock() p._logger.error = MagicMock() p._storage = MagicMock(spec=StorageClient) p._readings_storage = MagicMock(spec=ReadingsStorageClient) audit = p._audit with patch.object( p._readings_storage, 'purge', side_effect=self.store_purge) as mock_storage_purge: with patch.object( audit, 'information', return_value=mock_audit_info()) as audit_info: # Test the positive case when all if conditions in purge_data pass assert expected_return == p.purge_data(conf) assert audit_info.called args, kwargs = mock_storage_purge.call_args assert kwargs == expected_calls