github.com/swiftstack/ProxyFS@v0.0.0-20210203235616-4017c267d62f/pfs_middleware/tests/test_s3_compat.py (about)

     1  # Copyright (c) 2015-2021, NVIDIA CORPORATION.
     2  # SPDX-License-Identifier: Apache-2.0
     3  
     4  
     5  import json
     6  import unittest
     7  
     8  from swift.common import swob
     9  
    10  from . import helpers
    11  import pfs_middleware.s3_compat as s3_compat
    12  import pfs_middleware.utils as utils
    13  
    14  
    15  class TestS3Compat(unittest.TestCase):
    16      def setUp(self):
    17          super(TestS3Compat, self).setUp()
    18          self.app = helpers.FakeProxy()
    19          self.s3_compat = s3_compat.S3Compat(self.app, {})
    20  
    21          self.app.register(
    22              'PUT', '/v1/AUTH_test/con/obj',
    23              201, {}, '')
    24  
    25      def test_not_bimodal(self):
    26          req = swob.Request.blank(
    27              "/v1/AUTH_test/con/obj?multipart-manifest=put",
    28              environ={'REQUEST_METHOD': 'PUT',
    29                       'wsgi.input': swob.WsgiBytesIO(b"")},
    30              headers={'Content-Length': '0'})
    31  
    32          resp = req.get_response(self.s3_compat)
    33          self.assertEqual(resp.status_int, 201)
    34  
    35      def test_bimodal_but_not_swift3(self):
    36          req = swob.Request.blank(
    37              "/v1/AUTH_test/con/obj?multipart-manifest=put",
    38              environ={'REQUEST_METHOD': 'PUT',
    39                       'wsgi.input': swob.WsgiBytesIO(b""),
    40                       utils.ENV_IS_BIMODAL: True},
    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_conversion(self):
    47          self.app.register(
    48              'COALESCE', '/v1/AUTH_test/con/obj',
    49              201, {}, '')
    50  
    51          # Note that this is in SLO's internal format since this request has
    52          # already passed through SLO.
    53          slo_manifest = [{
    54              "name": "/con-segments/obj/1506721327.316611/1",
    55              "hash": "dontcare1",
    56              "bytes": 12345678901,
    57          }, {
    58              "name": "/con-segments/obj/1506721327.316611/2",
    59              "hash": "dontcare2",
    60              "bytes": 12345678902,
    61          }, {
    62              "name": "/con-segments/obj/1506721327.316611/3",
    63              "hash": "dontcare3",
    64              "bytes": 12345678903,
    65          }]
    66          serialized_slo_manifest = json.dumps(slo_manifest).encode('utf-8')
    67  
    68          req = swob.Request.blank(
    69              "/v1/AUTH_test/con/obj?multipart-manifest=put",
    70              environ={'REQUEST_METHOD': 'PUT',
    71                       'wsgi.input': swob.WsgiBytesIO(serialized_slo_manifest),
    72                       utils.ENV_IS_BIMODAL: True,
    73                       'swift.source': 'S3'},
    74              headers={'Content-Length': str(len(slo_manifest))})
    75  
    76          resp = req.get_response(self.s3_compat)
    77          self.assertEqual(resp.status_int, 201)
    78          self.assertEqual(self.app.calls[0][0], 'COALESCE')
    79  
    80          self.assertEqual(s3_compat.convert_slo_to_coalesce(slo_manifest), {
    81              "elements": [
    82                  "con-segments/obj/1506721327.316611/1",
    83                  "con-segments/obj/1506721327.316611/2",
    84                  "con-segments/obj/1506721327.316611/3"]})