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
def test_write_statistics(self, event_loop): """Test that write_statistics calls update statistics with defined keys and value increments""" @asyncio.coroutine def mock_s_update(): return "" mockStorageClient = MagicMock(spec=StorageClient) mockAuditLogger = AuditLogger(mockStorageClient) with patch.object(FoglampProcess, '__init__'): with patch.object( Statistics, 'update', return_value=mock_s_update()) as mock_stats_update: with patch.object(mockAuditLogger, "__init__", return_value=None): p = Purge(loop=event_loop) p._storage = mockStorageClient p.write_statistics(1, 2) mock_stats_update.assert_has_calls( [call('PURGED', 1), call('UNSNPURGED', 2)])
async def test_set_configuration(self): """Test that purge's set_configuration returns configuration item with key 'PURGE_READ' """ @asyncio.coroutine def mock_cm_return(): return "" mockStorageClientAsync = MagicMock(spec=StorageClientAsync) mockAuditLogger = AuditLogger(mockStorageClientAsync) with patch.object(FoglampProcess, '__init__'): with patch.object(mockAuditLogger, "__init__", return_value=None): p = Purge() p._storage = MagicMock(spec=StorageClientAsync) mock_cm = ConfigurationManager(p._storage) with patch.object(mock_cm, 'create_category', return_value=mock_cm_return()) as mock_create_cat: with patch.object(mock_cm, 'get_category_all_items', return_value=mock_cm_return()) \ as mock_get_cat: await p.set_configuration() mock_get_cat.assert_called_once_with('PURGE_READ') args, kwargs = mock_create_cat.call_args assert len(args) == 3 assert args[0] == 'PURGE_READ'