def test_direct_error_return_invalid_source(self, monkeypatch, caplog, tmp_gpg_dir, config_override): config_override({'sources': { 'TEST': {}, }}) mock_dh = Mock() test_data = [ SAMPLE_UNKNOWN_ATTRIBUTE, # valid, because mirror imports are non-strict SAMPLE_ROUTE.replace('TEST', 'BADSOURCE'), ] test_input = '\n\n'.join(test_data) with tempfile.NamedTemporaryFile() as fp: fp.write(test_input.encode('utf-8')) fp.seek(0) parser = MirrorFileImportParser( source='TEST', filename=fp.name, serial=424242, database_handler=mock_dh, direct_error_return=True, ) error = parser.run_import() assert error == 'Invalid source BADSOURCE for object 192.0.2.0/24AS65537, expected TEST' assert len(mock_dh.mock_calls) == 1 assert mock_dh.mock_calls[0][0] == 'upsert_rpsl_object' assert mock_dh.mock_calls[0][1][0].pk() == '192.0.2.0/24AS65537' assert 'Invalid source BADSOURCE for object' not in caplog.text assert 'File import for TEST' not in caplog.text
def test_parse(self, monkeypatch, caplog): monkeypatch.setenv('IRRD_SOURCES_TEST_OBJECT_CLASS_FILTER', 'route') mock_dh = Mock() test_data = [ SAMPLE_UNKNOWN_ATTRIBUTE, # valid, because mirror imports are non-strict SAMPLE_ROUTE6, # Valid, excluded by object class filter SAMPLE_ROUTE.replace('TEST', 'BADSOURCE'), SAMPLE_UNKNOWN_CLASS, SAMPLE_MALFORMED_PK, ] test_input = '\n\n'.join(test_data) with tempfile.NamedTemporaryFile() as fp: fp.write(test_input.encode('utf-8')) fp.seek(0) MirrorFileImportParser( source='TEST', filename=fp.name, serial=424242, database_handler=mock_dh, ) assert len(mock_dh.mock_calls) == 3 assert mock_dh.mock_calls[0][0] == 'upsert_rpsl_object' assert mock_dh.mock_calls[0][1][0].pk() == '192.0.2.0/24AS65537' assert mock_dh.mock_calls[1][0] == 'record_mirror_error' assert mock_dh.mock_calls[2][0] == 'record_mirror_error' assert 'Invalid source BADSOURCE for object' in caplog.text assert 'Invalid address prefix' in caplog.text assert 'File import for TEST: 5 objects read, 1 objects inserted, ignored 2 due to errors' in caplog.text assert 'ignored 1 due to object_class_filter' in caplog.text assert 'Ignored 1 objects found in file import for TEST due to unknown object classes' in caplog.text
def test_parse(self, mock_scopefilter, caplog, tmp_gpg_dir, config_override): config_override({ 'sources': { 'TEST': { 'object_class_filter': ['route', 'key-cert'], 'strict_import_keycert_objects': True, } } }) mock_dh = Mock() mock_roa_validator = Mock(spec=BulkRouteROAValidator) mock_roa_validator.validate_route = lambda ip, length, asn, source: RPKIStatus.invalid test_data = [ SAMPLE_UNKNOWN_ATTRIBUTE, # valid, because mirror imports are non-strict SAMPLE_ROUTE6, # Valid, excluded by object class filter SAMPLE_KEY_CERT, SAMPLE_ROUTE.replace('TEST', 'BADSOURCE'), SAMPLE_UNKNOWN_CLASS, SAMPLE_MALFORMED_PK, SAMPLE_LEGACY_IRRD_ARTIFACT, ] test_input = '\n\n'.join(test_data) with tempfile.NamedTemporaryFile() as fp: fp.write(test_input.encode('utf-8')) fp.seek(0) parser = MirrorFileImportParser( source='TEST', filename=fp.name, serial=424242, database_handler=mock_dh, roa_validator=mock_roa_validator, ) parser.run_import() assert len(mock_dh.mock_calls) == 5 assert mock_dh.mock_calls[0][0] == 'upsert_rpsl_object' assert mock_dh.mock_calls[0][1][0].pk() == '192.0.2.0/24AS65537' assert mock_dh.mock_calls[0][1][0].rpki_status == RPKIStatus.invalid assert mock_dh.mock_calls[0][1][ 0].scopefilter_status == ScopeFilterStatus.in_scope assert mock_dh.mock_calls[1][0] == 'upsert_rpsl_object' assert mock_dh.mock_calls[1][1][0].pk() == 'PGPKEY-80F238C6' assert mock_dh.mock_calls[2][0] == 'record_mirror_error' assert mock_dh.mock_calls[3][0] == 'record_mirror_error' assert mock_dh.mock_calls[4][0] == 'record_serial_seen' assert mock_dh.mock_calls[4][1][0] == 'TEST' assert mock_dh.mock_calls[4][1][1] == 424242 assert 'Invalid source BADSOURCE for object' in caplog.text assert 'Invalid address prefix' in caplog.text assert 'File import for TEST: 6 objects read, 2 objects inserted, ignored 2 due to errors' in caplog.text assert 'ignored 1 due to object_class_filter' in caplog.text assert 'Ignored 1 objects found in file import for TEST due to unknown object classes' in caplog.text key_cert_obj = rpsl_object_from_text(SAMPLE_KEY_CERT, strict_validation=False) assert key_cert_obj.verify(KEY_CERT_SIGNED_MESSAGE_VALID)
def test_parse(self, mock_scopefilter, caplog, config_override): config_override({ 'sources': { 'TEST': { 'object_class_filter': ['route', 'route6', 'key-cert', 'role'], } } }) mock_dh = Mock() test_data = [ SAMPLE_ROUTE, # Valid retained SAMPLE_ROUTE6, # Valid modified SAMPLE_ROLE, # Valid new object SAMPLE_ROUTE.replace('TEST', 'BADSOURCE'), SAMPLE_UNKNOWN_CLASS, SAMPLE_MALFORMED_PK, ] test_input = '\n\n'.join(test_data) route_with_last_modified = SAMPLE_ROUTE + 'last-modified: 2020-01-01T00:00:00Z\n' mock_query_result = [ { # Retained object (with format cleaning) # includes a last-modified which should be ignored in the comparison 'rpsl_pk': '192.0.2.0/24AS65537', 'object_class': 'route', 'object_text': rpsl_object_from_text( route_with_last_modified).render_rpsl_text(), }, { # Modified object 'rpsl_pk': '2001:DB8::/48AS65537', 'object_class': 'route6', 'object_text': SAMPLE_ROUTE6.replace('test-MNT', 'existing-mnt'), }, { # Deleted object 'rpsl_pk': 'rtrs-settest', 'object_class': 'route-set', 'object_text': SAMPLE_RTR_SET, }, ] mock_dh.execute_query = lambda query: mock_query_result with tempfile.NamedTemporaryFile() as fp: fp.write(test_input.encode('utf-8')) fp.seek(0) parser = MirrorUpdateFileImportParser( source='TEST', filename=fp.name, database_handler=mock_dh, ) parser.run_import() assert len(mock_dh.mock_calls) == 5 assert mock_dh.mock_calls[0][0] == 'record_mirror_error' assert mock_dh.mock_calls[1][0] == 'record_mirror_error' assert mock_dh.mock_calls[2][0] == 'upsert_rpsl_object' assert mock_dh.mock_calls[2][1][0].pk() == 'ROLE-TEST' assert mock_dh.mock_calls[3][0] == 'delete_rpsl_object' assert mock_dh.mock_calls[3][2]['source'] == 'TEST' assert mock_dh.mock_calls[3][2]['rpsl_pk'] == 'rtrs-settest' assert mock_dh.mock_calls[3][2]['object_class'] == 'route-set' assert mock_dh.mock_calls[3][2][ 'origin'] == JournalEntryOrigin.synthetic_nrtm assert mock_dh.mock_calls[4][0] == 'upsert_rpsl_object' assert mock_dh.mock_calls[4][1][0].pk() == '2001:DB8::/48AS65537' assert 'Invalid source BADSOURCE for object' in caplog.text assert 'Invalid address prefix' in caplog.text assert 'File update for TEST: 6 objects read, 3 objects processed, 1 objects newly inserted, 1 objects newly deleted, 2 objects retained, of which 1 modified' in caplog.text assert 'ignored 0 due to object_class_filter' in caplog.text assert 'Ignored 1 objects found in file import for TEST due to unknown object classes' in caplog.text