github.com/swiftstack/proxyfs@v0.0.0-20201223034610-5434d919416e/pfs_middleware/tests/test_s3_compat.py (about) 1 # Copyright (c) 2017 SwiftStack, Inc. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 12 # implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 16 import json 17 import unittest 18 19 from swift.common import swob 20 21 from . import helpers 22 import pfs_middleware.s3_compat as s3_compat 23 import pfs_middleware.utils as utils 24 25 26 class TestS3Compat(unittest.TestCase): 27 def setUp(self): 28 super(TestS3Compat, self).setUp() 29 self.app = helpers.FakeProxy() 30 self.s3_compat = s3_compat.S3Compat(self.app, {}) 31 32 self.app.register( 33 'PUT', '/v1/AUTH_test/con/obj', 34 201, {}, '') 35 36 def test_not_bimodal(self): 37 req = swob.Request.blank( 38 "/v1/AUTH_test/con/obj?multipart-manifest=put", 39 environ={'REQUEST_METHOD': 'PUT', 40 'wsgi.input': swob.WsgiBytesIO(b"")}, 41 headers={'Content-Length': '0'}) 42 43 resp = req.get_response(self.s3_compat) 44 self.assertEqual(resp.status_int, 201) 45 46 def test_bimodal_but_not_swift3(self): 47 req = swob.Request.blank( 48 "/v1/AUTH_test/con/obj?multipart-manifest=put", 49 environ={'REQUEST_METHOD': 'PUT', 50 'wsgi.input': swob.WsgiBytesIO(b""), 51 utils.ENV_IS_BIMODAL: True}, 52 headers={'Content-Length': '0'}) 53 54 resp = req.get_response(self.s3_compat) 55 self.assertEqual(resp.status_int, 201) 56 57 def test_conversion(self): 58 self.app.register( 59 'COALESCE', '/v1/AUTH_test/con/obj', 60 201, {}, '') 61 62 # Note that this is in SLO's internal format since this request has 63 # already passed through SLO. 64 slo_manifest = [{ 65 "name": "/con-segments/obj/1506721327.316611/1", 66 "hash": "dontcare1", 67 "bytes": 12345678901, 68 }, { 69 "name": "/con-segments/obj/1506721327.316611/2", 70 "hash": "dontcare2", 71 "bytes": 12345678902, 72 }, { 73 "name": "/con-segments/obj/1506721327.316611/3", 74 "hash": "dontcare3", 75 "bytes": 12345678903, 76 }] 77 serialized_slo_manifest = json.dumps(slo_manifest).encode('utf-8') 78 79 req = swob.Request.blank( 80 "/v1/AUTH_test/con/obj?multipart-manifest=put", 81 environ={'REQUEST_METHOD': 'PUT', 82 'wsgi.input': swob.WsgiBytesIO(serialized_slo_manifest), 83 utils.ENV_IS_BIMODAL: True, 84 'swift.source': 'S3'}, 85 headers={'Content-Length': str(len(slo_manifest))}) 86 87 resp = req.get_response(self.s3_compat) 88 self.assertEqual(resp.status_int, 201) 89 self.assertEqual(self.app.calls[0][0], 'COALESCE') 90 91 self.assertEqual(s3_compat.convert_slo_to_coalesce(slo_manifest), { 92 "elements": [ 93 "con-segments/obj/1506721327.316611/1", 94 "con-segments/obj/1506721327.316611/2", 95 "con-segments/obj/1506721327.316611/3"]})