def fake_get_auth(url, user, key, snet, auth_version, **kwargs): if url is None: return None, None if 'http' in url and '://' not in url: raise ValueError('Invalid url %s' % url) # Check the auth version against the configured value if swift_store_auth_version != auth_version: msg = 'AUTHENTICATION failed (version mismatch)' raise swiftclient.ClientException(msg) return None, None
def fake_delete_object(url, token, container, name, **kwargs): # DELETE returns nothing fixture_key = "%s/%s" % (container, name) if fixture_key not in fixture_headers: msg = "Object DELETE failed - Object does not exist" raise swiftclient.ClientException(msg, http_status=httplib.NOT_FOUND) else: del fixture_headers[fixture_key] del fixture_objects[fixture_key]
def fake_put_object(url, token, container, name, contents, **kwargs): # PUT returns the ETag header for the newly-added object # Large object manifest... global SWIFT_PUT_OBJECT_CALLS SWIFT_PUT_OBJECT_CALLS += 1 CHUNKSIZE = 64 * units.Ki fixture_key = "%s/%s" % (container, name) if fixture_key not in fixture_headers: if kwargs.get('headers'): etag = kwargs['headers']['ETag'] fixture_headers[fixture_key] = {'manifest': True, 'etag': etag} return etag if hasattr(contents, 'read'): fixture_object = StringIO.StringIO() chunk = contents.read(CHUNKSIZE) checksum = hashlib.md5() while chunk: fixture_object.write(chunk) checksum.update(chunk) chunk = contents.read(CHUNKSIZE) etag = checksum.hexdigest() else: fixture_object = StringIO.StringIO(contents) etag = hashlib.md5(fixture_object.getvalue()).hexdigest() read_len = fixture_object.len if read_len > MAX_SWIFT_OBJECT_SIZE: msg = ('Image size:%d exceeds Swift max:%d' % (read_len, MAX_SWIFT_OBJECT_SIZE)) raise swiftclient.ClientException( msg, http_status=httplib.REQUEST_ENTITY_TOO_LARGE) fixture_objects[fixture_key] = fixture_object fixture_headers[fixture_key] = { 'content-length': read_len, 'etag': etag } return etag else: msg = ("Object PUT failed - Object with key %s already exists" % fixture_key) raise swiftclient.ClientException(msg, http_status=httplib.CONFLICT)
def side_effect_func(*args, **kwargs): if (args[0] in self._containers and args[1] in map(lambda x: x['name'], self._objects[args[0]])): return ( {'content-length': len(contents), 'accept-ranges': 'bytes', 'last-modified': 'Mon, 10 Mar 2013 01:06:34 GMT', 'etag': 'eb15a6874ce265e2c3eb1b4891567bab', 'x-timestamp': '1363568794.67584', 'x-trans-id': 'txef3aaf26c897420c8e77c9750ce6a501', 'date': 'Mon, 10 Mar 2013 05:35:14 GMT', 'content-type': 'application/octet-stream'}, [obj for obj in self._objects[args[0]] if obj['name'] == args[1]][0]['contents']) else: raise swiftclient.ClientException('Resource Not Found', http_status=404)
def fake_get_object(url, token, container, name, **kwargs): # GET returns the tuple (list of headers, file object) fixture_key = "%s/%s" % (container, name) if fixture_key not in fixture_headers: msg = "Object GET failed" raise swiftclient.ClientException(msg, http_status=httplib.NOT_FOUND) fixture = fixture_headers[fixture_key] if 'manifest' in fixture: # Large object manifest... we return a file containing # all objects with prefix of this fixture key chunk_keys = sorted([ k for k in fixture_headers.keys() if k.startswith(fixture_key) and k != fixture_key ]) result = StringIO.StringIO() for key in chunk_keys: result.write(fixture_objects[key].getvalue()) return fixture_headers[fixture_key], result else: return fixture_headers[fixture_key], fixture_objects[fixture_key]
def fake_get_object(url, token, container, name, **kwargs): # GET returns the tuple (list of headers, file object) fixture_key = "%s/%s" % (container, name) if fixture_key not in fixture_headers: msg = "Object GET failed" raise swiftclient.ClientException(msg, http_status=httplib.NOT_FOUND) byte_range = None headers = kwargs.get('headers', dict()) if headers is not None: headers = dict((k.lower(), v) for k, v in headers.iteritems()) if 'range' in headers: byte_range = headers.get('range') fixture = fixture_headers[fixture_key] if 'manifest' in fixture: # Large object manifest... we return a file containing # all objects with prefix of this fixture key chunk_keys = sorted([ k for k in fixture_headers.keys() if k.startswith(fixture_key) and k != fixture_key ]) result = six.StringIO() for key in chunk_keys: result.write(fixture_objects[key].getvalue()) else: result = fixture_objects[fixture_key] if byte_range is not None: start = int(byte_range.split('=')[1].strip('-')) result = six.StringIO(result.getvalue()[start:]) fixture_headers[fixture_key]['content-length'] = len( result.getvalue()) return fixture_headers[fixture_key], result
def side_effect_func(*args, **kwargs): if args[0] in self._containers: return container_resp(args[0]) else: raise swiftclient.ClientException('Resource Not Found', http_status=404)
def fake_head_container(url, token, container, **kwargs): if container not in fixture_containers: msg = "No container %s found" % container raise swiftclient.ClientException(msg, http_status=httplib.NOT_FOUND) return fixture_container_headers
def raise_500(): """Raise 500 error""" raise swiftclient.ClientException('error', http_status=500)
def raise_416(): """Raise 416 error""" raise swiftclient.ClientException('error', http_status=416)
def raise_404(): """Raise 404 error""" raise swiftclient.ClientException('error', http_status=404)
def test_post_object_bad_auth(self, connection, error): argv = ["", "post", "container", "object"] connection.return_value.post_object.side_effect = \ swiftclient.ClientException("bad auth") swiftclient.shell.main(argv) error.assert_called_with('bad auth')
def test_post_account_not_found(self, connection, error): argv = ["", "post"] connection.return_value.post_account.side_effect = \ swiftclient.ClientException('test', http_status=404) swiftclient.shell.main(argv) error.assert_called_with('Account not found')