def sync_audit(map, counter): """Run resync audit.""" client = Client() # ignore fail to continue running, log later client.ignore_failures = True client.set_mappings(map) # init_logging(verbose=True) src_resource_list = client.find_resource_list() rlb = ResourceListBuilder(mapper=client.mapper) dst_resource_list = rlb.from_disk() # Compare these resource lists respecting any comparison options (same, updated, deleted, created) = dst_resource_list.compare(src_resource_list) result = dict(created=[], updated=[], deleted=[]) for item in created: record_id = item.uri.rsplit('/', 1)[1] result['created'].append(record_id) for item in updated: record_id = item.uri.rsplit('/', 1)[1] result['updated'].append(record_id) for item in deleted: record_id = item.uri.rsplit('/', 1)[1] result['deleted'].append(record_id) update_counter(counter, result) return dict(same=len(same), updated=len(updated), deleted=len(deleted), created=len(created))
def test18_update_resource(self): c = Client() resource = Resource(uri='http://example.org/dir/2') filename = os.path.join(self.tmpdir, 'dir/resource2') # dryrun with LogCapture() as lc: c.dryrun = True c.logger = logging.getLogger('resync.client') n = c.update_resource(resource, filename) self.assertEqual(n, 0) self.assertTrue(lc.records[-1].msg.startswith( 'dryrun: would GET http://example.org/dir/2 ')) c.dryrun = False # get from file uri that does not exist resource = Resource(uri='file:tests/testdata/i_do_not_exist') self.assertRaises(ClientFatalError, c.update_resource, resource, filename) # get from file uri that does not exist but with c.ignore_failures to # log resource = Resource(uri='file:tests/testdata/i_do_not_exist') with LogCapture() as lc: c.logger = logging.getLogger('resync.client') c.ignore_failures = True n = c.update_resource(resource, filename) self.assertEqual(n, 0) self.assertTrue(lc.records[-1].msg.startswith( 'Failed to GET file:tests/testdata/i_do_not_exist ')) # get from file uri resource = Resource( uri='file:tests/testdata/examples_from_spec/resourcesync_ex_1.xml', length=355, md5='abc', timestamp=10) c.last_timestamp = 0 with LogCapture() as lc: c.logger = logging.getLogger('resync.client') n = c.update_resource(resource, filename) self.assertEqual(n, 1) self.assertTrue(lc.records[-1].msg.startswith('Event: {')) # get from file uri with length and md5 warnings resource = Resource( uri='file:tests/testdata/examples_from_spec/resourcesync_ex_1.xml', length=111, md5='abc', timestamp=10) c.last_timestamp = 0 with LogCapture() as lc: c.logger = logging.getLogger('resync.client') c.hashes = set(['md5']) n = c.update_resource(resource, filename) self.assertEqual(n, 1) self.assertTrue(lc.records[-1].msg.startswith('MD5 mismatch ')) self.assertTrue( lc.records[-2].msg.startswith('Downloaded size for ')) self.assertTrue(lc.records[-3].msg.startswith('Event: {'))
def test18_update_resource(self): c = Client() resource = Resource(uri='http://example.org/dir/2') filename = os.path.join(self.tmpdir, 'dir/resource2') # dryrun with LogCapture() as lc: c.dryrun = True c.logger = logging.getLogger('resync.client') n = c.update_resource(resource, filename) self.assertEqual(n, 0) self.assertTrue( lc.records[-1].msg.startswith('dryrun: would GET http://example.org/dir/2 ')) c.dryrun = False with webserver('tests/testdata', 'localhost', 9999): # get from file uri that does not exist resource = Resource(uri='http://localhost:9999/i_do_not_exist') self.assertRaises(ClientFatalError, c.update_resource, resource, filename) # get from file uri that does not exist but with c.ignore_failures to # log resource = Resource(uri='http://localhost:9999/i_do_not_exist') with LogCapture() as lc: c.logger = logging.getLogger('resync.client') c.ignore_failures = True n = c.update_resource(resource, filename) self.assertEqual(n, 0) self.assertTrue( lc.records[-1].msg.startswith('Failed to GET http://localhost:9999/i_do_not_exist ')) # get from file uri resource = Resource(uri='http://localhost:9999/examples_from_spec/resourcesync_ex_1.xml', length=355, md5='abc', timestamp=10) c.last_timestamp = 0 with LogCapture() as lc: c.logger = logging.getLogger('resync.client') n = c.update_resource(resource, filename) self.assertEqual(n, 1) self.assertTrue(lc.records[-1].msg.startswith('Event: {')) # get from file uri with length and md5 warnings resource = Resource(uri='http://localhost:9999/examples_from_spec/resourcesync_ex_1.xml', length=111, md5='abc', timestamp=10) c.last_timestamp = 0 with LogCapture() as lc: c.logger = logging.getLogger('resync.client') c.hashes = set(['md5']) n = c.update_resource(resource, filename) self.assertEqual(n, 1) self.assertTrue(lc.records[-1].msg.startswith('MD5 mismatch ')) self.assertTrue( lc.records[-2].msg.startswith('Downloaded size for ')) self.assertTrue(lc.records[-3].msg.startswith('Event: {'))