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: {'))
def test19_delete_resource(self): c = Client() resource = Resource(uri='http://example.org/1') filename = os.path.join(self.tmpdir, 'resource1') c.last_timestamp = 5 # no delete, no timestamp update with LogCapture() as lc: c.logger = logging.getLogger('resync.client') n = c.delete_resource(resource, filename) self.assertEqual(n, 0) self.assertEqual( lc.records[-1].msg, 'nodelete: would delete http://example.org/1 (--delete to enable)' ) self.assertEqual(c.last_timestamp, 5) # no delete but timestamp update resource.timestamp = 10 with LogCapture() as lc: c.logger = logging.getLogger('resync.client') n = c.delete_resource(resource, filename) self.assertEqual(n, 0) self.assertEqual( lc.records[-1].msg, 'nodelete: would delete http://example.org/1 (--delete to enable)' ) self.assertEqual(c.last_timestamp, 10) # allow delete but dryrun with LogCapture() as lc: c.dryrun = True c.logger = logging.getLogger('resync.client') n = c.delete_resource(resource, filename, allow_deletion=True) self.assertEqual(n, 0) self.assertTrue(lc.records[-1].msg.startswith( 'dryrun: would delete http://example.org/1')) c.dryrun = False # allow delete but no resource present with LogCapture() as lc: c.logger = logging.getLogger('resync.client') n = c.delete_resource(resource, filename, allow_deletion=True) self.assertEqual(n, 0) self.assertTrue(lc.records[-1].msg.startswith( 'Failed to DELETE http://example.org/1')) # successful deletion, first make file... with open(filename, 'w') as fh: fh.write('delete me') fh.close() with LogCapture() as lc: c.logger = logging.getLogger('resync.client') n = c.delete_resource(resource, filename, allow_deletion=True) self.assertEqual(n, 1) self.assertTrue(lc.records[-1].msg.startswith('Event: {')) self.assertTrue(lc.records[-2].msg.startswith( 'deleted: http://example.org/1 ->'))
def test19_delete_resource(self): c = Client() resource = Resource(uri='http://example.org/1') filename = os.path.join(self.tmpdir, 'resource1') c.last_timestamp = 5 # no delete, no timestamp update with LogCapture() as lc: c.logger = logging.getLogger('resync.client') n = c.delete_resource(resource, filename) self.assertEqual(n, 0) self.assertEqual(lc.records[-1].msg, 'nodelete: would delete http://example.org/1 (--delete to enable)') self.assertEqual(c.last_timestamp, 5) # no delete but timestamp update resource.timestamp = 10 with LogCapture() as lc: c.logger = logging.getLogger('resync.client') n = c.delete_resource(resource, filename) self.assertEqual(n, 0) self.assertEqual(lc.records[-1].msg, 'nodelete: would delete http://example.org/1 (--delete to enable)') self.assertEqual(c.last_timestamp, 10) # allow delete but dryrun with LogCapture() as lc: c.dryrun = True c.logger = logging.getLogger('resync.client') n = c.delete_resource(resource, filename, allow_deletion=True) self.assertEqual(n, 0) self.assertTrue( lc.records[-1].msg.startswith('dryrun: would delete http://example.org/1')) c.dryrun = False # allow delete but no resource present with LogCapture() as lc: c.logger = logging.getLogger('resync.client') n = c.delete_resource(resource, filename, allow_deletion=True) self.assertEqual(n, 0) self.assertTrue( lc.records[-1].msg.startswith('Failed to DELETE http://example.org/1')) # successful deletion, first make file... with open(filename, 'w') as fh: fh.write('delete me') fh.close() with LogCapture() as lc: c.logger = logging.getLogger('resync.client') n = c.delete_resource(resource, filename, allow_deletion=True) self.assertEqual(n, 1) self.assertTrue(lc.records[-1].msg.startswith('Event: {')) self.assertTrue( lc.records[-2].msg.startswith('deleted: http://example.org/1 ->'))