Ejemplo n.º 1
0
def account_listing_response(account, req, response_content_type, broker=None,
                             limit='', marker='', end_marker='', prefix='',
                             delimiter='', reverse=False):
    """
    This is an exact copy of swift.account.utis.account_listing_response()
    except for one difference i.e this method passes response_content_type
    to broker.list_containers_iter() method.
    """
    if broker is None:
        broker = FakeAccountBroker()

    resp_headers = get_response_headers(broker)

    account_list = broker.list_containers_iter(limit, marker, end_marker,
                                               prefix, delimiter,
                                               response_content_type, reverse)
    if response_content_type == 'application/json':
        data = []
        for (name, object_count, bytes_used, put_tstamp,
             is_subdir) in account_list:
            if is_subdir:
                data.append({'subdir': name})
            else:
                data.append({'name': name, 'count': object_count,
                             'bytes': bytes_used,
                             'last_modified': Timestamp(put_tstamp).isoformat})
        account_list = json.dumps(data)
    elif response_content_type.endswith('/xml'):
        output_list = ['<?xml version="1.0" encoding="UTF-8"?>',
                       '<account name=%s>' % saxutils.quoteattr(account)]
        for (name, object_count, bytes_used, put_tstamp,
             is_subdir) in account_list:
            if is_subdir:
                output_list.append(
                    '<subdir name=%s />' % saxutils.quoteattr(name))
            else:
                item = '<container><name>%s</name><count>%s</count>' \
                       '<bytes>%s</bytes><last_modified>%s</last_modified> \
                        </container>' % \
                       (saxutils.escape(name), object_count, bytes_used,
                        Timestamp(put_tstamp).isoformat)
                output_list.append(item)
        output_list.append('</account>')
        account_list = '\n'.join(output_list)
    else:
        if not account_list:
            resp = HTTPNoContent(request=req, headers=resp_headers)
            resp.content_type = response_content_type
            resp.charset = 'utf-8'
            return resp
        account_list = '\n'.join(r[0] for r in account_list) + '\n'
    ret = HTTPOk(body=account_list, request=req, headers=resp_headers)
    ret.content_type = response_content_type
    ret.charset = 'utf-8'
    return ret
Ejemplo n.º 2
0
    def test_get_response_headers_with_legacy_data(self):
        broker = backend.AccountBroker(':memory:', account='a')
        now = time.time()
        with mock.patch('time.time', new=lambda: now):
            broker.initialize(Timestamp(now).internal)
        # add some container data
        ts = (Timestamp(t).internal for t in itertools.count(int(now)))
        total_containers = 0
        total_objects = 0
        total_bytes = 0
        for policy in POLICIES:
            delete_timestamp = next(ts)
            put_timestamp = next(ts)
            object_count = int(policy)
            bytes_used = int(policy) * 10
            broker.put_container('c-%s' % policy.name, put_timestamp,
                                 delete_timestamp, object_count, bytes_used,
                                 int(policy))
            total_containers += 1
            total_objects += object_count
            total_bytes += bytes_used
        expected = HeaderKeyDict({
            'X-Account-Container-Count': total_containers,
            'X-Account-Object-Count': total_objects,
            'X-Account-Bytes-Used': total_bytes,
            'X-Timestamp': Timestamp(now).normal,
            'X-PUT-Timestamp': Timestamp(now).normal,
        })
        for policy in POLICIES:
            prefix = 'X-Account-Storage-Policy-%s-' % policy.name
            expected[prefix + 'Object-Count'] = int(policy)
            expected[prefix + 'Bytes-Used'] = int(policy) * 10
        orig_policy_stats = broker.get_policy_stats

        def stub_policy_stats(*args, **kwargs):
            policy_stats = orig_policy_stats(*args, **kwargs)
            for stats in policy_stats.values():
                # legacy db's won't return container_count
                del stats['container_count']
            return policy_stats

        broker.get_policy_stats = stub_policy_stats
        resp_headers = utils.get_response_headers(broker)
        per_policy_container_headers = [
            h for h in resp_headers
            if h.lower().startswith('x-account-storage-policy-')
            and h.lower().endswith('-container-count')
        ]
        self.assertFalse(per_policy_container_headers)
        for key, value in resp_headers.items():
            expected_value = expected.pop(key)
            self.assertEqual(
                expected_value, str(value),
                'value for %r was %r not %r' % (key, value, expected_value))
        self.assertFalse(expected)
Ejemplo n.º 3
0
 def HEAD(self, req):
     """Handle HTTP HEAD request."""
     drive, part, account = split_and_validate_path(req, 3)
     out_content_type = get_listing_content_type(req)
     if self.mount_check and not check_mount(self.root, drive):
         return HTTPInsufficientStorage(drive=drive, request=req)
     broker = self._get_account_broker(drive, part, account, pending_timeout=0.1, stale_reads_ok=True)
     if broker.is_deleted():
         return self._deleted_response(broker, req, HTTPNotFound)
     headers = get_response_headers(broker)
     headers["Content-Type"] = out_content_type
     return HTTPNoContent(request=req, headers=headers, charset="utf-8")
Ejemplo n.º 4
0
    def test_get_response_headers_with_legacy_data(self):
        broker = backend.AccountBroker(':memory:', account='a')
        now = time.time()
        with mock.patch('time.time', new=lambda: now):
            broker.initialize(Timestamp(now).internal)
        # add some container data
        ts = (Timestamp(t).internal for t in itertools.count(int(now)))
        total_containers = 0
        total_objects = 0
        total_bytes = 0
        for policy in POLICIES:
            delete_timestamp = ts.next()
            put_timestamp = ts.next()
            object_count = int(policy)
            bytes_used = int(policy) * 10
            broker.put_container('c-%s' % policy.name, put_timestamp,
                                 delete_timestamp, object_count, bytes_used,
                                 int(policy))
            total_containers += 1
            total_objects += object_count
            total_bytes += bytes_used
        expected = HeaderKeyDict({
            'X-Account-Container-Count': total_containers,
            'X-Account-Object-Count': total_objects,
            'X-Account-Bytes-Used': total_bytes,
            'X-Timestamp': Timestamp(now).normal,
            'X-PUT-Timestamp': Timestamp(now).normal,
        })
        for policy in POLICIES:
            prefix = 'X-Account-Storage-Policy-%s-' % policy.name
            expected[prefix + 'Object-Count'] = int(policy)
            expected[prefix + 'Bytes-Used'] = int(policy) * 10
        orig_policy_stats = broker.get_policy_stats

        def stub_policy_stats(*args, **kwargs):
            policy_stats = orig_policy_stats(*args, **kwargs)
            for stats in policy_stats.values():
                # legacy db's won't return container_count
                del stats['container_count']
            return policy_stats
        broker.get_policy_stats = stub_policy_stats
        resp_headers = utils.get_response_headers(broker)
        per_policy_container_headers = [
            h for h in resp_headers if
            h.lower().startswith('x-account-storage-policy-') and
            h.lower().endswith('-container-count')]
        self.assertFalse(per_policy_container_headers)
        for key, value in resp_headers.items():
            expected_value = expected.pop(key)
            self.assertEqual(expected_value, str(value),
                             'value for %r was %r not %r' % (
                                 key, value, expected_value))
        self.assertFalse(expected)
 def test_get_response_headers_fake_broker(self):
     broker = utils.FakeAccountBroker()
     now = time.time()
     expected = {
         'X-Account-Container-Count': 0,
         'X-Account-Object-Count': 0,
         'X-Account-Bytes-Used': 0,
         'X-Timestamp': Timestamp(now).normal,
         'X-PUT-Timestamp': Timestamp(now).normal,
     }
     with mock.patch('time.time', new=lambda: now):
         resp_headers = utils.get_response_headers(broker)
     self.assertEqual(resp_headers, expected)
Ejemplo n.º 6
0
 def test_get_response_headers_fake_broker(self):
     broker = utils.FakeAccountBroker()
     now = time.time()
     expected = {
         'X-Account-Container-Count': 0,
         'X-Account-Object-Count': 0,
         'X-Account-Bytes-Used': 0,
         'X-Timestamp': Timestamp(now).normal,
         'X-PUT-Timestamp': Timestamp(now).normal,
     }
     with mock.patch('time.time', new=lambda: now):
         resp_headers = utils.get_response_headers(broker)
     self.assertEqual(resp_headers, expected)
 def test_get_response_headers_empty_memory_broker(self):
     broker = backend.AccountBroker(':memory:', account='a')
     now = time.time()
     with mock.patch('time.time', new=lambda: now):
         broker.initialize(Timestamp(now).internal)
     expected = {
         'X-Account-Container-Count': 0,
         'X-Account-Object-Count': 0,
         'X-Account-Bytes-Used': 0,
         'X-Timestamp': Timestamp(now).normal,
         'X-PUT-Timestamp': Timestamp(now).normal,
     }
     resp_headers = utils.get_response_headers(broker)
     self.assertEqual(resp_headers, expected)
Ejemplo n.º 8
0
 def test_get_response_headers_empty_memory_broker(self):
     broker = backend.AccountBroker(':memory:', account='a')
     now = time.time()
     with mock.patch('time.time', new=lambda: now):
         broker.initialize(Timestamp(now).internal)
     expected = {
         'X-Account-Container-Count': 0,
         'X-Account-Object-Count': 0,
         'X-Account-Bytes-Used': 0,
         'X-Timestamp': Timestamp(now).normal,
         'X-PUT-Timestamp': Timestamp(now).normal,
     }
     resp_headers = utils.get_response_headers(broker)
     self.assertEqual(resp_headers, expected)
Ejemplo n.º 9
0
 def HEAD(self, req):
     """Handle HTTP HEAD request."""
     drive, part, account = split_and_validate_path(req, 3)
     out_content_type = get_listing_content_type(req)
     if self.mount_check and not check_mount(self.root, drive):
         return HTTPInsufficientStorage(drive=drive, request=req)
     broker = self._get_account_broker(drive, part, account,
                                       pending_timeout=0.1,
                                       stale_reads_ok=True)
     if broker.is_deleted():
         return self._deleted_response(broker, req, HTTPNotFound)
     headers = get_response_headers(broker)
     headers['Content-Type'] = out_content_type
     return HTTPNoContent(request=req, headers=headers, charset='utf-8')
Ejemplo n.º 10
0
 def HEAD(self, req):
     """Handle HTTP HEAD request."""
     drive, part, account = split_and_validate_path(req, 3)
     out_content_type = listing_formats.get_listing_content_type(req)
     try:
         check_drive(self.root, drive, self.mount_check)
     except ValueError:
         return HTTPInsufficientStorage(drive=drive, request=req)
     broker = self._get_account_broker(drive, part, account,
                                       pending_timeout=0.1,
                                       stale_reads_ok=True)
     if broker.is_deleted():
         return self._deleted_response(broker, req, HTTPNotFound)
     headers = get_response_headers(broker)
     headers['Content-Type'] = out_content_type
     return HTTPNoContent(request=req, headers=headers, charset='utf-8')
Ejemplo n.º 11
0
 def HEAD(self, req):
     """Handle HTTP HEAD request."""
     drive, part, account = get_account_name_and_placement(req)
     out_content_type = listing_formats.get_listing_content_type(req)
     try:
         check_drive(self.root, drive, self.mount_check)
     except ValueError:
         return HTTPInsufficientStorage(drive=drive, request=req)
     broker = self._get_account_broker(drive, part, account,
                                       pending_timeout=0.1,
                                       stale_reads_ok=True)
     if broker.is_deleted():
         return self._deleted_response(broker, req, HTTPNotFound)
     headers = get_response_headers(broker)
     headers['Content-Type'] = out_content_type
     return HTTPNoContent(request=req, headers=headers, charset='utf-8')
 def test_get_response_headers_with_data(self):
     broker = backend.AccountBroker(':memory:', account='a')
     now = time.time()
     with mock.patch('time.time', new=lambda: now):
         broker.initialize(Timestamp(now).internal)
     # add some container data
     ts = (Timestamp(t).internal for t in itertools.count(int(now)))
     total_containers = 0
     total_objects = 0
     total_bytes = 0
     for policy in POLICIES:
         delete_timestamp = ts.next()
         put_timestamp = ts.next()
         object_count = int(policy)
         bytes_used = int(policy) * 10
         broker.put_container('c-%s' % policy.name, put_timestamp,
                              delete_timestamp, object_count, bytes_used,
                              int(policy))
         total_containers += 1
         total_objects += object_count
         total_bytes += bytes_used
     expected = HeaderKeyDict({
         'X-Account-Container-Count': total_containers,
         'X-Account-Object-Count': total_objects,
         'X-Account-Bytes-Used': total_bytes,
         'X-Timestamp': Timestamp(now).normal,
         'X-PUT-Timestamp': Timestamp(now).normal,
     })
     for policy in POLICIES:
         prefix = 'X-Account-Storage-Policy-%s-' % policy.name
         expected[prefix + 'Object-Count'] = int(policy)
         expected[prefix + 'Bytes-Used'] = int(policy) * 10
     resp_headers = utils.get_response_headers(broker)
     for key, value in resp_headers.items():
         expected_value = expected.pop(key)
         self.assertEqual(
             expected_value, str(value),
             'value for %r was %r not %r' % (key, value, expected_value))
     self.assertFalse(expected)
Ejemplo n.º 13
0
 def test_get_response_headers_with_data(self):
     broker = backend.AccountBroker(':memory:', account='a')
     now = time.time()
     with mock.patch('time.time', new=lambda: now):
         broker.initialize(Timestamp(now).internal)
     # add some container data
     ts = (Timestamp(t).internal for t in itertools.count(int(now)))
     total_containers = 0
     total_objects = 0
     total_bytes = 0
     for policy in POLICIES:
         delete_timestamp = ts.next()
         put_timestamp = ts.next()
         object_count = int(policy)
         bytes_used = int(policy) * 10
         broker.put_container('c-%s' % policy.name, put_timestamp,
                              delete_timestamp, object_count, bytes_used,
                              int(policy))
         total_containers += 1
         total_objects += object_count
         total_bytes += bytes_used
     expected = HeaderKeyDict({
         'X-Account-Container-Count': total_containers,
         'X-Account-Object-Count': total_objects,
         'X-Account-Bytes-Used': total_bytes,
         'X-Timestamp': Timestamp(now).normal,
         'X-PUT-Timestamp': Timestamp(now).normal,
     })
     for policy in POLICIES:
         prefix = 'X-Account-Storage-Policy-%s-' % policy.name
         expected[prefix + 'Object-Count'] = int(policy)
         expected[prefix + 'Bytes-Used'] = int(policy) * 10
     resp_headers = utils.get_response_headers(broker)
     for key, value in resp_headers.items():
         expected_value = expected.pop(key)
         self.assertEqual(expected_value, str(value),
                          'value for %r was %r not %r' % (
                              key, value, expected_value))
     self.assertFalse(expected)
Ejemplo n.º 14
0
def account_listing_response(account,
                             req,
                             response_content_type,
                             broker=None,
                             limit='',
                             marker='',
                             end_marker='',
                             prefix='',
                             delimiter='',
                             reverse=False):
    """
    This is an exact copy of swift.account.utis.account_listing_response()
    except for one difference i.e this method passes response_content_type
    to broker.list_containers_iter() method.
    """
    if broker is None:
        broker = FakeAccountBroker()

    resp_headers = get_response_headers(broker)

    account_list = broker.list_containers_iter(limit, marker, end_marker,
                                               prefix, delimiter,
                                               response_content_type, reverse)
    if response_content_type == 'application/json':
        data = []
        for (name, object_count, bytes_used, put_tstamp,
             is_subdir) in account_list:
            if is_subdir:
                data.append({'subdir': name})
            else:
                data.append({
                    'name': name,
                    'count': object_count,
                    'bytes': bytes_used,
                    'last_modified': Timestamp(put_tstamp).isoformat
                })
        account_list = json.dumps(data)
    elif response_content_type.endswith('/xml'):
        output_list = [
            '<?xml version="1.0" encoding="UTF-8"?>',
            '<account name=%s>' % saxutils.quoteattr(account)
        ]
        for (name, object_count, bytes_used, put_tstamp,
             is_subdir) in account_list:
            if is_subdir:
                output_list.append('<subdir name=%s />' %
                                   saxutils.quoteattr(name))
            else:
                item = '<container><name>%s</name><count>%s</count>' \
                       '<bytes>%s</bytes><last_modified>%s</last_modified> \
                        </container>'                                      % \
                       (saxutils.escape(name), object_count, bytes_used,
                        Timestamp(put_tstamp).isoformat)
                output_list.append(item)
        output_list.append('</account>')
        account_list = '\n'.join(output_list)
    else:
        if not account_list:
            resp = HTTPNoContent(request=req, headers=resp_headers)
            resp.content_type = response_content_type
            resp.charset = 'utf-8'
            return resp
        account_list = '\n'.join(r[0] for r in account_list) + '\n'
    ret = HTTPOk(body=account_list, request=req, headers=resp_headers)
    ret.content_type = response_content_type
    ret.charset = 'utf-8'
    return ret