def test_list_instance_index_fail(): """Test if ObjectRecoveryValidator skips processing successfully if listing instance index fails""" index_api_mock = Mock(spec=CORTXS3IndexApi) object_api_mock = Mock(spec=CORTXS3ObjectApi) index_api_mock.list.return_value = False, {} config = CORTXS3Config() probable_delete_records = {'Key': 'TAcGAQAAAAA=-AwAAAAAAhEs=', \ 'Value':'{"motr_process_fid":"<0x7200000000000000:0>","create_timestamp":"2020-03-16T16:24:04.000Z", \ "force_delete":"false","global_instance_id":"TAifBwAAAAA=-AAAAAAAA2lk=","is_multipart":"false", \ "object_key_in_index":"object_1","object_layout_id":9, "pv_id":"AQAAAAAAAHYKAAAAAAAAAA==","object_list_index_oid":"TAifBwAAAHg=-AQAAAAAA2lk=", \ "objects_version_list_index_oid":"TAifBwAAAHg=-AwAAAAAA2lk=","old_oid":"AAAAAAAAAAA=-AAAAAAAAAAA=", \ "version_key_in_index":"object_1/18446742489333709430"}' } validator = ObjectRecoveryValidator(config, probable_delete_records, objectapi=object_api_mock, indexapi=index_api_mock) inst_id = json.loads(probable_delete_records['Value']) ret = validator.check_instance_is_nonactive(inst_id['global_instance_id']) # Assert that object oid delete is skipped if instance id listing fails. object_api_mock.assert_not_called assert ret == False, "value was True, should be False"
def test_object_metadata_exists_and_matches(): """Test if ObjectRecoveryValidator should attempt delete for old object oid """ index_api_mock = Mock(spec=CORTXS3IndexApi) kv_api_mock = Mock(spec=CORTXS3KVApi) object_api_mock = Mock(spec=CORTXS3ObjectApi) ol_res_val = { 'ACL': '', 'Bucket-Name': 'mybucket', 'Object-Name': 'test_object', 'Object-URI': 'mybucket\\test_object', 'create_timestamp': '2020-03-17T11:02:13.000Z', 'layout_id': 9, 'motr_oid': 'Tgj8AgAAAAA=-dQAAAAAABCY=' } index_content = { 'Delimiter': '', 'Index-Id': 'AAAAAAAAAHg=-BAAQAAAAAAA=', 'IsTruncated': 'false', 'Keys': [{ 'Key': 'test_object', 'Value': ol_res_val }], 'Marker': '', 'MaxKeys': '1000', 'NextMarker': '', 'Prefix': '' } index_response = CORTXS3ListIndexResponse( json.dumps(index_content).encode()) object_key = ol_res_val["Object-Name"] # oid mismatches in object metadata object_metadata = ol_res_val kv_response = CORTXS3GetKVResponse(object_key, json.dumps(object_metadata).encode()) index_api_mock.list.return_value = True, index_response kv_api_mock.get.return_value = True, kv_response object_api_mock.delete.return_value = True, {} config = CORTXS3Config() probable_delete_records = {'Key': 'Tgj8AgAAAAA=-dQAAAAAABCY=', \ 'Value':'{"motr_process_fid":"<0x7200000000000000:0>","create_timestamp":"2020-03-16T16:24:04.000Z", \ "force_delete":"false","global_instance_id":"TAifBwAAAAA=-AAAAAAAA2lk=","is_multipart":"false", \ "object_key_in_index":"object_1","object_layout_id":9,"pv_id":"AQAAAAAAAHYKAAAAAAAAAA==","object_list_index_oid":"TAifBwAAAHg=-AQAAAAAA2lk=", \ "objects_version_list_index_oid":"TAifBwAAAHg=-AwAAAAAA2lk=","old_oid":"AAAAAAAAAAA=-AAAAAAAAAAA=", \ "version_key_in_index":"object_1/18446742489333709430"}\n' } validator = ObjectRecoveryValidator(config, probable_delete_records, objectapi=object_api_mock, kvapi=kv_api_mock, indexapi=index_api_mock) #Mock 'process_probable_delete_record' validator.process_probable_delete_record = MagicMock(return_value=True) validator.check_instance_is_nonactive = MagicMock(return_value=False) validator.process_results() # Assert following: kv_api_mock.assert_called_once validator.process_probable_delete_record.assert_called == False