1# Copyright (C) 2012 Hewlett-Packard Development Company, L.P. 2# Copyright (C) 2014 TrilioData, Inc 3# All Rights Reserved. 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); you may 6# not use this file except in compliance with the License. You may obtain 7# a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14# License for the specific language governing permissions and limitations 15# under the License. 16 17import hashlib 18import os 19import socket 20import tempfile 21 22from six.moves import http_client 23 24from swiftclient import client as swift 25 26 27class FakeSwiftClient2(object): 28 def __init__(self, *args, **kwargs): 29 pass 30 31 @classmethod 32 def Connection(self, *args, **kargs): 33 return FakeSwiftConnection2() 34 35 36class FakeSwiftConnection2(object): 37 def __init__(self, *args, **kwargs): 38 self.tempdir = tempfile.mkdtemp() 39 40 def head_container(self, container): 41 if container == 'missing_container': 42 raise swift.ClientException('fake exception', 43 http_status=http_client.NOT_FOUND) 44 elif container == 'unauthorized_container': 45 raise swift.ClientException('fake exception', 46 http_status=http_client.UNAUTHORIZED) 47 elif container == 'socket_error_on_head': 48 raise socket.error(111, 'ECONNREFUSED') 49 50 def put_container(self, container): 51 pass 52 53 def get_container(self, container, **kwargs): 54 fake_header = None 55 container_dir = tempfile.gettempdir() + '/' + container 56 fake_body = [] 57 for f in os.listdir(container_dir): 58 try: 59 f.index(kwargs['prefix']) 60 fake_body.append({'name': f}) 61 except Exception: 62 pass 63 64 return fake_header, fake_body 65 66 def head_object(self, container, name): 67 return {'etag': 'fake-md5-sum'} 68 69 def get_object(self, container, name): 70 if container == 'socket_error_on_get': 71 raise socket.error(111, 'ECONNREFUSED') 72 object_path = tempfile.gettempdir() + '/' + container + '/' + name 73 with open(object_path, 'rb') as object_file: 74 return (None, object_file.read()) 75 76 def put_object(self, container, name, reader, content_length=None, 77 etag=None, chunk_size=None, content_type=None, 78 headers=None, query_string=None): 79 object_path = tempfile.gettempdir() + '/' + container + '/' + name 80 with open(object_path, 'wb') as object_file: 81 object_file.write(reader.read()) 82 return hashlib.md5(reader.read()).hexdigest() 83 84 def delete_object(self, container, name): 85 pass 86