class ObjectTest(unittest.TestCase): """ Freerange Object class tests. """ @printdoc def test_send_retry(self): """Simulates an inactivity timeout between two send requests.""" gener = (part for part in ('the ', 'rain ', 'in ', 'spain', ' ...')) self.storage_object.size = 21 self.storage_object.content_type = "text/plain" self.container.conn.connection.let_fail() self.storage_object.send(gener) def setUp(self): self.auth = Auth('jsmith', 'qwerty') self.conn = Connection(auth=self.auth) self.conn.conn_class = FailOnceHTTPConnection self.conn.http_connect() self.container = self.conn.get_container('container1') self.storage_object = self.container.get_object('object1') def tearDown(self): del self.storage_object del self.container del self.conn del self.auth
class ContainerTest(unittest.TestCase): """ Freerange Container class tests. """ @printdoc def test_create_object(self): """ Verify that Container.create_object() returns an Object instance. """ storage_object = self.container.create_object("object1") self.assert_(isinstance(storage_object, Object)) @printdoc def test_delete_object(self): """ Simple sanity check of Container.delete_object() """ self.container.delete_object("object1") @printdoc def test_get_object(self): """ Verify that Container.get_object() returns an Object instance. """ storage_object = self.container.get_object("object1") self.assert_(isinstance(storage_object, Object)) @printdoc def test_get_objects(self): """ Iterate an ObjectResults and verify that it returns Object instances. Validate that the count() and index() methods work as expected. """ objects = self.container.get_objects() for storage_object in objects: self.assert_(isinstance(storage_object, Object)) self.assert_(objects.count("object1") == 1) self.assert_(objects.index("object3") == 2) @printdoc def test_get_objects_parametrized(self): """ Iterate an ObjectResults and verify that it returns Object instances. Validate that the count() and index() methods work as expected. """ objects = self.container.get_objects(prefix="object", limit=3, offset=3, path="/") for storage_object in objects: self.assert_(isinstance(storage_object, Object)) self.assert_(objects.count("object4") == 1) self.assert_(objects.index("object6") == 2) @printdoc def test_list_objects_info(self): """ Verify that Container.list_objects_info() returns a list object. """ self.assert_(isinstance(self.container.list_objects(), list)) @printdoc def test_list_objects(self): """ Verify that Container.list_objects() returns a list object. """ self.assert_(isinstance(self.container.list_objects(), list)) @printdoc def test_list_objects_limited(self): """ Verify that limit & order query parameters work. """ self.assert_(len(self.container.list_objects(limit=3)) == 3) self.assert_(len(self.container.list_objects(limit=3, offset=3)) == 3) @printdoc def test_list_objects_prefixed(self): """ Verify that the prefix query parameter works. """ self.assert_(isinstance(self.container.list_objects(prefix="object"), list)) @printdoc def test_list_objects_path(self): """ Verify that the prefix query parameter works. """ self.assert_(isinstance(self.container.list_objects(path="/"), list)) @printdoc def test_bad_name_assignment(self): """ Ensure you can't assign an invalid name. """ basket = Container(self.conn) try: basket.name = "yougivelove/abadname" self.fail("InvalidContainerName exception not raised!") except InvalidContainerName: pass try: basket.name = "a" * (container_name_limit + 1) self.fail("InvalidContainerName exception not raised!") except InvalidContainerName: pass @printdoc def test_bad_object_name(self): """ Verify that methods do not accept invalid container names. """ self.assertRaises(InvalidObjectName, self.container.delete_object, "") def setUp(self): self.auth = Auth("jsmith", "qwerty") self.conn = Connection(auth=self.auth) self.conn.conn_class = CustomHTTPConnection self.conn.http_connect() self.container = self.conn.get_container("container1") def tearDown(self): del self.container del self.conn del self.auth
class ObjectTest(unittest.TestCase): """ Freerange Object class tests. """ @printdoc def test_read(self): """ Test an Object's ability to read. """ self.assert_("teapot" in self.storage_object.read()) @printdoc def test_read_pass_headers(self): """ Test an Object's ability to read when it has extra headers passed in. """ hdrs = {} hdrs['x-bogus-header-1'] = 'bogus value' hdrs['x-bogus-header-2'] = 'boguser value' self.assert_("teapot" in self.storage_object.read(hdrs=hdrs)) @printdoc def test_response_error(self): """ Verify that reading a non-existent Object raises a ResponseError exception. """ storage_object = self.container.create_object('bogus') self.assertRaises(ResponseError, storage_object.read) @printdoc def test_write(self): """ Simple sanity test of Object.write() """ self.storage_object.write('the rain in spain ...') self.assertEqual('the rain in spain ...', self.conn.connection._wbuffer) @printdoc def test_write_with_stringio(self): """ Ensure write() can deal with a StringIO instance """ self.storage_object.write(StringIO('the rain in spain ...')) self.assertEqual('the rain in spain ...', self.conn.connection._wbuffer) @printdoc def test_write_with_file(self): """ Ensure write() can deal with a file instance """ tmpnam = mktemp() try: fp = open(tmpnam, 'w') fp.write('the rain in spain ...') fp.close() fp = open(tmpnam, 'r') self.storage_object.write(fp) fp.close() self.assertEqual('the rain in spain ...', self.conn.connection._wbuffer) finally: os.unlink(tmpnam) @printdoc def test_send(self): """Sanity test of Object.send().""" gener = (part for part in ('the ', 'rain ', 'in ', 'spain ...')) self.storage_object.size = 21 self.storage_object.content_type = "text/plain" self.storage_object.send(gener) @printdoc def test_sync_metadata(self): """ Sanity check of Object.sync_metadata() """ self.storage_object.metadata['unit'] = 'test' self.storage_object.sync_metadata() @printdoc def test_load_from_file(self): """ Simple sanity test of Object.load_from_file(). """ path = os.path.join(os.path.dirname(__file__), 'samplefile.txt') self.storage_object.load_from_filename(path) @printdoc def test_save_to_filename(self): """Sanity test of Object.save_to_filename().""" tmpnam = mktemp() self.storage_object.save_to_filename(tmpnam) rdr = open(tmpnam, 'r') try: self.assert_(rdr.read() == self.storage_object.read(), "save_to_filename() stored invalid content!") finally: rdr.close() os.unlink(tmpnam) @printdoc def test_compute_md5sum(self): """ Verify that the Object.compute_md5sum() class method returns an accurate md5 sum value. """ f = open('/bin/ls', 'r') m = md5() m.update(f.read()) sum1 = m.hexdigest() f.seek(0) try: sum2 = Object.compute_md5sum(f) self.assert_(sum1 == sum2, "%s != %s" % (sum1, sum2)) finally: f.close() @printdoc def test_bad_name(self): """ Ensure you can't assign an invalid object name. """ obj = Object(self.container) # name is None self.assertRaises(InvalidObjectName, obj.read) self.assertRaises(InvalidObjectName, obj.stream) self.assertRaises(InvalidObjectName, obj.sync_metadata) self.assertRaises(InvalidObjectName, obj.write, '') obj.name = '' # name is zero-length string self.assertRaises(InvalidObjectName, obj.read) self.assertRaises(InvalidObjectName, obj.stream) self.assertRaises(InvalidObjectName, obj.sync_metadata) self.assertRaises(InvalidObjectName, obj.write, '') obj.name = 'a'*(object_name_limit+1) # too-long string self.assertRaises(InvalidObjectName, obj.read) self.assertRaises(InvalidObjectName, obj.stream().next) self.assertRaises(InvalidObjectName, obj.sync_metadata) self.assertRaises(InvalidObjectName, obj.write, '') obj.name = 'a'*(object_name_limit) # ok name obj.read() obj.stream() obj.sync_metadata() obj.write('') @printdoc def test_bad_meta_data(self): """ Ensure you can't sync bad metadata. """ # too-long name self.storage_object.metadata['a'*(meta_name_limit+1)] = 'test' self.assertRaises(InvalidMetaName, self.storage_object.sync_metadata) del(self.storage_object.metadata['a'*(meta_name_limit+1)]) # too-long value self.storage_object.metadata['a'*(meta_name_limit)] = \ 'a'*(meta_value_limit+1) self.assertRaises(InvalidMetaValue, self.storage_object.sync_metadata) @printdoc def test_account_size(self): """ Test to see that the total bytes on the account is size of the samplefile """ self.assert_(self.conn.get_info()[1] == 234) def setUp(self): self.auth = Auth('jsmith', 'qwerty') self.conn = Connection(auth=self.auth) self.conn.conn_class = CustomHTTPConnection self.conn.http_connect() self.container = self.conn.get_container('container1') self.storage_object = self.container.get_object('object1') def tearDown(self): del self.storage_object del self.container del self.conn del self.auth
class ObjectTest(unittest.TestCase): """ Freerange Object class tests. """ @printdoc def test_read(self): """ Test an Object's ability to read. """ self.assert_("teapot" in self.storage_object.read()) @printdoc def test_read_pass_headers(self): """ Test an Object's ability to read when it has extra headers passed in. """ hdrs = {} hdrs['x-bogus-header-1'] = 'bogus value' hdrs['x-bogus-header-2'] = 'boguser value' self.assert_("teapot" in self.storage_object.read(hdrs=hdrs)) @printdoc def test_response_error(self): """ Verify that reading a non-existent Object raises a ResponseError exception. """ storage_object = self.container.create_object('bogus') self.assertRaises(ResponseError, storage_object.read) @printdoc def test_write(self): """ Simple sanity test of Object.write() """ self.storage_object.write('the rain in spain ...') @printdoc def test_send(self): """Sanity test of Object.send().""" gener = (part for part in ('the ', 'rain ', 'in ', 'spain ...')) self.storage_object.size = 21 self.storage_object.content_type = "text/plain" self.storage_object.send(gener) @printdoc def test_sync_metadata(self): """ Sanity check of Object.sync_metadata() """ self.storage_object.metadata['unit'] = 'test' self.storage_object.sync_metadata() @printdoc def test_load_from_file(self): """ Simple sanity test of Object.load_from_file(). """ path = os.path.join(os.path.dirname(__file__), 'samplefile.txt') self.storage_object.load_from_filename(path) @printdoc def test_save_to_filename(self): """Sanity test of Object.save_to_filename().""" tmpnam = mktemp() self.storage_object.save_to_filename(tmpnam) rdr = open(tmpnam, 'r') try: self.assert_(rdr.read() == self.storage_object.read(), "save_to_filename() stored invalid content!") finally: rdr.close() os.unlink(tmpnam) @printdoc def test_compute_md5sum(self): """ Verify that the Object.compute_md5sum() class method returns an accurate md5 sum value. """ f = open('/bin/ls', 'r') m = md5() m.update(f.read()) sum1 = m.hexdigest() f.seek(0) try: sum2 = Object.compute_md5sum(f) self.assert_(sum1 == sum2, "%s != %s" % (sum1, sum2)) finally: f.close() @printdoc def test_bad_name(self): """ Ensure you can't assign an invalid object name. """ obj = Object(self.container) # name is None self.assertRaises(InvalidObjectName, obj.read) self.assertRaises(InvalidObjectName, obj.stream) self.assertRaises(InvalidObjectName, obj.sync_metadata) self.assertRaises(InvalidObjectName, obj.write, '') obj.name = '' # name is zero-length string self.assertRaises(InvalidObjectName, obj.read) self.assertRaises(InvalidObjectName, obj.stream) self.assertRaises(InvalidObjectName, obj.sync_metadata) self.assertRaises(InvalidObjectName, obj.write, '') obj.name = 'a'*(object_name_limit+1) # too-long string self.assertRaises(InvalidObjectName, obj.read) self.assertRaises(InvalidObjectName, obj.stream().next) self.assertRaises(InvalidObjectName, obj.sync_metadata) self.assertRaises(InvalidObjectName, obj.write, '') obj.name = 'a'*(object_name_limit) # ok name obj.read() obj.stream() obj.sync_metadata() obj.write('') @printdoc def test_bad_meta_data(self): """ Ensure you can't sync bad metadata. """ # too-long name self.storage_object.metadata['a'*(meta_name_limit+1)] = 'test' self.assertRaises(InvalidMetaName, self.storage_object.sync_metadata) del(self.storage_object.metadata['a'*(meta_name_limit+1)]) # too-long value self.storage_object.metadata['a'*(meta_name_limit)] = \ 'a'*(meta_value_limit+1) self.assertRaises(InvalidMetaValue, self.storage_object.sync_metadata) @printdoc def test_account_size(self): """ Test to see that the total bytes on the account is size of the samplefile """ self.assert_(self.conn.get_info()[1] == 234) def setUp(self): self.auth = Auth('jsmith', 'qwerty') self.conn = Connection(auth=self.auth) self.conn.conn_class = CustomHTTPConnection self.conn.http_connect() self.container = self.conn.get_container('container1') self.storage_object = self.container.get_object('object1') def tearDown(self): del self.storage_object del self.container del self.conn del self.auth
class ConnectionTest(unittest.TestCase): """ Freerange Connection class tests. """ @printdoc def test_create_container(self): """ Verify that Connection.create_container() returns a Container instance. """ container = self.conn.create_container('container1') self.assert_(isinstance(container, Container)) @printdoc def test_delete_container(self): """ Simple sanity check of Connection.delete_container() """ self.conn.delete_container('container1') @printdoc def test_get_all_containers(self): """ Iterate a ContainerResults and verify that it returns Container instances. Validate that the count() and index() methods work as expected. """ containers = self.conn.get_all_containers() for instance in containers: self.assert_(isinstance(instance, Container)) self.assert_(containers.count('container1') == 1) self.assert_(containers.index('container3') == 2) @printdoc def test_get_container(self): """ Verify that Connection.get_container() returns a Container instance. """ container = self.conn.get_container('container1') self.assert_(isinstance(container, Container)) @printdoc def test_list_containers(self): """ Verify that Connection.list_containers() returns a list object. """ self.assert_(isinstance(self.conn.list_containers(), list)) @printdoc def test_list_containers_info(self): """ Verify that Connection.list_containers_info() returns a list object. """ self.assert_(isinstance(self.conn.list_containers_info(), list)) @printdoc def test_bad_names(self): """ Verify that methods do not accept invalid container names. """ exccls = InvalidContainerName for badname in ('', 'yougivelove/abadname', 'a'*(container_name_limit+1)): self.assertRaises(exccls, self.conn.create_container, badname) self.assertRaises(exccls, self.conn.get_container, badname) self.assertRaises(exccls, self.conn.delete_container, badname) @printdoc def test_account_info(self): """ Test to see if the account has only one container """ self.assert_(self.conn.get_info()[0] == 3) @printdoc def test_servicenet_cnx(self): """ Test connection to servicenet. """ auth = Auth('jsmith', 'qwerty') conn = Connection(auth=auth, servicenet=True) self.assert_(conn.connection_args[0].startswith("snet-")) def setUp(self): self.auth = Auth('jsmith', 'qwerty') self.conn = Connection(auth=self.auth) self.conn.conn_class = CustomHTTPConnection self.conn.http_connect() def tearDown(self): del self.conn del self.auth
class ContainerTest(unittest.TestCase): """ Freerange Container class tests. """ @printdoc def test_create_object(self): """ Verify that Container.create_object() returns an Object instance. """ storage_object = self.container.create_object('object1') self.assert_(isinstance(storage_object, Object)) @printdoc def test_delete_object(self): """ Simple sanity check of Container.delete_object() """ self.container.delete_object('object1') @printdoc def test_get_object(self): """ Verify that Container.get_object() returns an Object instance. """ storage_object = self.container.get_object('object1') self.assert_(isinstance(storage_object, Object)) @printdoc def test_get_objects(self): """ Iterate an ObjectResults and verify that it returns Object instances. Validate that the count() and index() methods work as expected. """ objects = self.container.get_objects() for storage_object in objects: self.assert_(isinstance(storage_object, Object)) self.assert_(objects.count('object1') == 1) self.assert_(objects.index('object3') == 2) @printdoc def test_get_objects_parametrized(self): """ Iterate an ObjectResults and verify that it returns Object instances. Validate that the count() and index() methods work as expected. """ objects = self.container.get_objects(prefix='object', limit=3, offset=3, path='/') for storage_object in objects: self.assert_(isinstance(storage_object, Object)) self.assert_(objects.count('object4') == 1) self.assert_(objects.index('object6') == 2) @printdoc def test_list_objects_info(self): """ Verify that Container.list_objects_info() returns a list object. """ self.assert_(isinstance(self.container.list_objects(), list)) @printdoc def test_list_objects(self): """ Verify that Container.list_objects() returns a list object. """ self.assert_(isinstance(self.container.list_objects(), list)) @printdoc def test_list_objects_limited(self): """ Verify that limit & order query parameters work. """ self.assert_(len(self.container.list_objects(limit=3)) == 3) self.assert_(len(self.container.list_objects(limit=3, offset=3)) == 3) @printdoc def test_list_objects_prefixed(self): """ Verify that the prefix query parameter works. """ self.assert_( isinstance(self.container.list_objects(prefix='object'), list)) @printdoc def test_list_objects_path(self): """ Verify that the path query parameter works. """ self.assert_(isinstance(self.container.list_objects(path='/'), list)) @printdoc def test_list_objects_delimiter(self): """ Verify that the delimiter query parameter works. """ self.assert_( isinstance(self.container.list_objects(delimiter='/'), list)) @printdoc def test_bad_name_assignment(self): """ Ensure you can't assign an invalid name. """ basket = Container(self.conn) try: basket.name = 'yougivelove/abadname' self.fail("InvalidContainerName exception not raised!") except InvalidContainerName: pass try: basket.name = 'a' * (container_name_limit + 1) self.fail("InvalidContainerName exception not raised!") except InvalidContainerName: pass @printdoc def test_bad_object_name(self): """ Verify that methods do not accept invalid container names. """ self.assertRaises(InvalidObjectName, self.container.delete_object, '') def setUp(self): self.auth = Auth('jsmith', 'qwerty') self.conn = Connection(auth=self.auth) self.conn.conn_class = CustomHTTPConnection self.conn.http_connect() self.container = self.conn.get_container('container1') def tearDown(self): del self.container del self.conn del self.auth
class ConnectionTest(unittest.TestCase): """ Freerange Connection class tests. """ @printdoc def test_create_container(self): """ Verify that Connection.create_container() returns a Container instance. """ container = self.conn.create_container('container1') self.assert_(isinstance(container, Container)) @printdoc def test_delete_container(self): """ Simple sanity check of Connection.delete_container() """ self.conn.delete_container('container1') @printdoc def test_get_all_containers(self): """ Iterate a ContainerResults and verify that it returns Container instances. Validate that the count() and index() methods work as expected. """ containers = self.conn.get_all_containers() for instance in containers: self.assert_(isinstance(instance, Container)) self.assert_(containers.count('container1') == 1) self.assert_(containers.index('container3') == 2) @printdoc def test_get_container(self): """ Verify that Connection.get_container() returns a Container instance. """ container = self.conn.get_container('container1') self.assert_(isinstance(container, Container)) @printdoc def test_list_containers(self): """ Verify that Connection.list_containers() returns a list object. """ self.assert_(isinstance(self.conn.list_containers(), list)) @printdoc def test_list_containers_info(self): """ Verify that Connection.list_containers_info() returns a list object. """ self.assert_(isinstance(self.conn.list_containers_info(), list)) @printdoc def test_bad_names(self): """ Verify that methods do not accept invalid container names. """ exccls = InvalidContainerName for badname in ('', 'yougivelove/abadname', 'a' * (container_name_limit + 1)): self.assertRaises(exccls, self.conn.create_container, badname) self.assertRaises(exccls, self.conn.get_container, badname) self.assertRaises(exccls, self.conn.delete_container, badname) @printdoc def test_account_info(self): """ Test to see if the account has only one container """ self.assert_(self.conn.get_info()[0] == 3) @printdoc def test_servicenet_cnx(self): """ Test connection to servicenet. """ auth = Auth('jsmith', 'qwerty') conn = Connection(auth=auth, servicenet=True) self.assert_(conn.connection_args[0].startswith("snet-")) @printdoc def test_socket_timeout(self): socket.setdefaulttimeout(21) self.conn.list_containers() self.assert_(socket.getdefaulttimeout() == 21.0) def setUp(self): self.auth = Auth('jsmith', 'qwerty') self.conn = Connection(auth=self.auth) self.conn.conn_class = CustomHTTPConnection self.conn.http_connect() def tearDown(self): del self.conn del self.auth
class ConnectionTest(unittest.TestCase): """ Freerange Connection class tests. """ @printdoc def test_create_container(self): """ Verify that Connection.create_container() returns a Container instance. """ container = self.conn.create_container('container1') self.assert_(isinstance(container, Container)) @printdoc def test_delete_container(self): """ Simple sanity check of Connection.delete_container() """ self.conn.delete_container('container1') @printdoc def test_get_all_containers(self): """ Iterate a ContainerResults and verify that it returns Container instances. Validate that the count() and index() methods work as expected. """ containers = self.conn.get_all_containers() for instance in containers: self.assert_(isinstance(instance, Container)) self.assert_(containers.count('container1') == 1) self.assert_(containers.index('container3') == 2) @printdoc def test_get_container(self): """ Verify that Connection.get_container() returns a Container instance. """ container = self.conn.get_container('container1') self.assert_(isinstance(container, Container)) @printdoc def test_list_containers(self): """ Verify that Connection.list_containers() returns a list object. """ self.assert_(isinstance(self.conn.list_containers(), list)) @printdoc def test_list_containers_info(self): """ Verify that Connection.list_containers_info() returns a list object. """ self.assert_(isinstance(self.conn.list_containers_info(), list)) @printdoc def test_bad_names(self): """ Verify that methods do not accept invalid container names. """ exccls = InvalidContainerName for badname in ('', 'yougivelove/abadname', 'a' * (container_name_limit + 1)): self.assertRaises(exccls, self.conn.create_container, badname) self.assertRaises(exccls, self.conn.get_container, badname) self.assertRaises(exccls, self.conn.delete_container, badname) @printdoc def test_account_info(self): """ Test to see if the account has only one container """ self.assert_(self.conn.get_info()[0] == 3) @printdoc def test_construct_path(self): """ Test _construct_path. """ uri = '/' + self.conn.uri.rstrip('/') + '/' self.assertEquals(self.conn._construct_path(''), uri) self.assertEquals(self.conn._construct_path(['bucket']), uri + 'bucket') self.assertEquals(self.conn._construct_path(['bucket', 'object']), uri + 'bucket/object') self.assertEquals(self.conn._construct_path('', {'param': 'value'}), uri + '?param=value') self.assertEquals( self.conn._construct_path(['yougivelove'], { 'abad': 'name', 'also': 'fun' }), uri + 'yougivelove?also=fun&abad=name') @printdoc def test_construct_header(self): """ Test _construct_header. """ self._test_construct_header() self._test_construct_header(hdrs={'hello': 'world'}) self._test_construct_header(data='yougivelove abadname') self._test_construct_header(hdrs={ 'X-Awesome': 'gnarly', 'Retry': 'now' }, data='yougivelove abadname') def _test_construct_header(self, hdrs={}, data=''): """ Helper function for test_construct_header. """ headers = self.conn._construct_headers(hdrs, data) self.assertEquals(len(headers), 3 + len(hdrs)) self.assertEquals(headers['Content-Length'], str(len(data))) self.assertEquals(headers['User-Agent'], self.conn.user_agent) self.assertEquals(headers['X-Auth-Token'], self.conn.token) for k, v in hdrs.iteritems(): self.assertEquals(headers[k], v) @printdoc def test_do_request(self): """ Test _do_request. """ response = self.conn._do_request( self.conn.http_connect, lambda: self.conn.connection, 'GET', self.conn._construct_path(''), '', self.conn._construct_headers(None, '')) self.assert_(isinstance(response, HTTPResponse)) @printdoc def test_http_connect(self): # test default timeout old_conn = self.conn.connection self.conn.http_connect() self.assertEquals(self.conn.connection.timeout, self.conn.timeout) new_conn = self.conn.connection self.assert_(isinstance(new_conn, HTTPConnection)) # new connection should be made self.assert_(new_conn is not old_conn) # test setting a timeout self.conn.http_connect(60) self.assertEquals(self.conn.connection.timeout, 60) @printdoc def test_servicenet_cnx(self): """ Test connection to servicenet. """ auth = Auth('jsmith', 'qwerty') conn = Connection(auth=auth, servicenet=True) self.assert_(conn.connection_args[0].startswith("snet-")) @printdoc def test_socket_timeout(self): socket.setdefaulttimeout(21) self.conn.list_containers() self.assert_(socket.getdefaulttimeout() == 21.0) def setUp(self): self.auth = Auth('jsmith', 'qwerty') self.conn = Connection(auth=self.auth) self.conn.conn_class = CustomHTTPConnection self.conn.http_connect() def tearDown(self): del self.conn del self.auth