From a96b82fa0ff38681784dc3548a2d97973bec93ab Mon Sep 17 00:00:00 2001 From: vakarisz Date: Fri, 7 Oct 2022 16:02:45 +0300 Subject: [PATCH] Island: Don't raise errors if machine upsert did no changes It doesn't make sense to raise an error if upsert did no changes, because the purpose of "upsert" method is to ensure that data is up-to-date. If no changes were made it means it's already up-to-date. --- .../cc/repository/mongo_machine_repository.py | 6 ------ .../repository/test_mongo_machine_repository.py | 15 --------------- 2 files changed, 21 deletions(-) diff --git a/monkey/monkey_island/cc/repository/mongo_machine_repository.py b/monkey/monkey_island/cc/repository/mongo_machine_repository.py index 8e1934cc7..a3297449e 100644 --- a/monkey/monkey_island/cc/repository/mongo_machine_repository.py +++ b/monkey/monkey_island/cc/repository/mongo_machine_repository.py @@ -38,12 +38,6 @@ class MongoMachineRepository(IMachineRepository): except Exception as err: raise StorageError(f'Error updating machine with ID "{machine.id}": {err}') - if result.matched_count != 0 and result.modified_count != 1: - raise StorageError( - f'Error updating machine with ID "{machine.id}": Expected to update 1 machine, ' - f"but {result.modified_count} were updated" - ) - if result.matched_count == 0 and result.upserted_id is None: raise StorageError( f'Error inserting machine with ID "{machine.id}": Expected to insert 1 machine, ' diff --git a/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_machine_repository.py b/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_machine_repository.py index bbd35283b..7e4ef93a8 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_machine_repository.py +++ b/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_machine_repository.py @@ -146,21 +146,6 @@ def test_upsert_machine__storage_error_exception(error_raising_machine_repositor error_raising_machine_repository.upsert_machine(machine) -def test_upsert_machine__storage_error_update_failed(error_raising_mock_mongo_client): - mock_result = MagicMock() - mock_result.matched_count = 1 - mock_result.modified_count = 0 - - error_raising_mock_mongo_client.monkey_island.machines.replace_one = MagicMock( - return_value=mock_result - ) - machine_repository = MongoMachineRepository(error_raising_mock_mongo_client) - - machine = MACHINES[0] - with pytest.raises(StorageError): - machine_repository.upsert_machine(machine) - - def test_upsert_machine__storage_error_insert_failed(error_raising_mock_mongo_client): mock_result = MagicMock() mock_result.matched_count = 0