File: test_container_deleter.py

package info (click to toggle)
swift 2.35.1-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 22,760 kB
  • sloc: python: 281,901; javascript: 1,059; sh: 619; pascal: 295; makefile: 81; xml: 32
file content (282 lines) | stat: -rw-r--r-- 10,669 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import collections
import itertools
import json
from unittest import mock
import unittest

from swift.cli import container_deleter
from swift.common import internal_client
from swift.common import swob
from swift.common import utils

AppCall = collections.namedtuple('AppCall', [
    'method', 'path', 'query', 'headers', 'body'])


class FakeInternalClient(internal_client.InternalClient):
    def __init__(self, responses):
        self.resp_iter = iter(responses)
        self.calls = []

    def make_request(self, method, path, headers, acceptable_statuses,
                     body_file=None, params=None):
        if body_file is None:
            body = None
        else:
            body = body_file.read()
        path, _, query = path.partition('?')
        self.calls.append(AppCall(method, path, query, headers, body))
        resp = next(self.resp_iter)
        if isinstance(resp, Exception):
            raise resp
        return resp

    def __enter__(self):
        return self

    def __exit__(self, *args):
        unused_responses = [r for r in self.resp_iter]
        if unused_responses:
            raise Exception('Unused responses: %r' % unused_responses)


class TestContainerDeleter(unittest.TestCase):
    def setUp(self):
        patcher = mock.patch.object(container_deleter.time, 'time',
                                    side_effect=itertools.count())
        patcher.__enter__()
        self.addCleanup(patcher.__exit__, None, None, None)

        patcher = mock.patch.object(container_deleter, 'OBJECTS_PER_UPDATE', 5)
        patcher.__enter__()
        self.addCleanup(patcher.__exit__, None, None, None)

    def test_make_delete_jobs(self):
        ts = '1558463777.42739'
        self.assertEqual(
            container_deleter.make_delete_jobs(
                'acct', 'cont', ['obj1', 'obj2'],
                utils.Timestamp(ts)),
            [{'name': ts + '-acct/cont/obj1',
              'deleted': 0,
              'created_at': ts,
              'etag': utils.MD5_OF_EMPTY_STRING,
              'size': 0,
              'storage_policy_index': 0,
              'content_type': 'application/async-deleted'},
             {'name': ts + '-acct/cont/obj2',
              'deleted': 0,
              'created_at': ts,
              'etag': utils.MD5_OF_EMPTY_STRING,
              'size': 0,
              'storage_policy_index': 0,
              'content_type': 'application/async-deleted'}])

    def test_make_delete_jobs_native_utf8(self):
        ts = '1558463777.42739'
        uacct = acct = u'acct-\U0001f334'
        ucont = cont = u'cont-\N{SNOWMAN}'
        uobj1 = obj1 = u'obj-\N{GREEK CAPITAL LETTER ALPHA}'
        uobj2 = obj2 = u'/obj-\N{GREEK CAPITAL LETTER OMEGA}'
        self.assertEqual(
            container_deleter.make_delete_jobs(
                acct, cont, [obj1, obj2], utils.Timestamp(ts)),
            [{'name': u'%s-%s/%s/%s' % (ts, uacct, ucont, uobj1),
              'deleted': 0,
              'created_at': ts,
              'etag': utils.MD5_OF_EMPTY_STRING,
              'size': 0,
              'storage_policy_index': 0,
              'content_type': 'application/async-deleted'},
             {'name': u'%s-%s/%s/%s' % (ts, uacct, ucont, uobj2),
              'deleted': 0,
              'created_at': ts,
              'etag': utils.MD5_OF_EMPTY_STRING,
              'size': 0,
              'storage_policy_index': 0,
              'content_type': 'application/async-deleted'}])

    def test_make_delete_jobs_unicode_utf8(self):
        ts = '1558463777.42739'
        acct = u'acct-\U0001f334'
        cont = u'cont-\N{SNOWMAN}'
        obj1 = u'obj-\N{GREEK CAPITAL LETTER ALPHA}'
        obj2 = u'obj-\N{GREEK CAPITAL LETTER OMEGA}'
        self.assertEqual(
            container_deleter.make_delete_jobs(
                acct, cont, [obj1, obj2], utils.Timestamp(ts)),
            [{'name': u'%s-%s/%s/%s' % (ts, acct, cont, obj1),
              'deleted': 0,
              'created_at': ts,
              'etag': utils.MD5_OF_EMPTY_STRING,
              'size': 0,
              'storage_policy_index': 0,
              'content_type': 'application/async-deleted'},
             {'name': u'%s-%s/%s/%s' % (ts, acct, cont, obj2),
              'deleted': 0,
              'created_at': ts,
              'etag': utils.MD5_OF_EMPTY_STRING,
              'size': 0,
              'storage_policy_index': 0,
              'content_type': 'application/async-deleted'}])

    def test_mark_for_deletion_empty_no_yield(self):
        with FakeInternalClient([
            swob.Response(json.dumps([
            ])),
        ]) as swift:
            self.assertEqual(container_deleter.mark_for_deletion(
                swift,
                'account',
                'container',
                'marker',
                'end',
                'prefix',
                timestamp=None,
                yield_time=None,
            ), 0)
            self.assertEqual(swift.calls, [
                ('GET', '/v1/account/container',
                 'format=json&marker=marker&end_marker=end&prefix=prefix',
                 {}, None),
            ])

    def test_mark_for_deletion_empty_with_yield(self):
        with FakeInternalClient([
            swob.Response(json.dumps([
            ])),
        ]) as swift:
            self.assertEqual(list(container_deleter.mark_for_deletion(
                swift,
                'account',
                'container',
                'marker',
                'end',
                'prefix',
                timestamp=None,
                yield_time=0.5,
            )), [(0, None)])
            self.assertEqual(swift.calls, [
                ('GET', '/v1/account/container',
                 'format=json&marker=marker&end_marker=end&prefix=prefix',
                 {}, None),
            ])

    def test_mark_for_deletion_one_update_no_yield(self):
        ts = '1558463777.42739'
        with FakeInternalClient([
            swob.Response(json.dumps([
                {'name': '/obj1'},
                {'name': 'obj2'},
                {'name': 'obj3'},
            ])),
            swob.Response(json.dumps([
            ])),
            swob.Response(status=202),
        ]) as swift:
            self.assertEqual(container_deleter.mark_for_deletion(
                swift,
                'account',
                'container',
                '',
                '',
                '',
                timestamp=utils.Timestamp(ts),
                yield_time=None,
            ), 3)
            self.assertEqual(swift.calls, [
                ('GET', '/v1/account/container',
                 'format=json&marker=&end_marker=&prefix=', {}, None),
                ('GET', '/v1/account/container',
                 'format=json&marker=obj3&end_marker=&prefix=', {}, None),
                ('UPDATE', '/v1/.expiring_objects/' + ts.split('.')[0], '', {
                    'X-Backend-Allow-Private-Methods': 'True',
                    'X-Backend-Storage-Policy-Index': '0',
                    'X-Timestamp': ts}, mock.ANY),
            ])
            self.assertEqual(
                json.loads(swift.calls[-1].body),
                container_deleter.make_delete_jobs(
                    'account', 'container', ['/obj1', 'obj2', 'obj3'],
                    utils.Timestamp(ts)
                )
            )

    def test_mark_for_deletion_two_updates_with_yield(self):
        ts = '1558463777.42739'
        with FakeInternalClient([
            swob.Response(json.dumps([
                {'name': 'obj1'},
                {'name': 'obj2'},
                {'name': 'obj3'},
                {'name': u'obj4-\N{SNOWMAN}'},
                {'name': 'obj5'},
                {'name': 'obj6'},
            ])),
            swob.Response(status=202),
            swob.Response(json.dumps([
            ])),
            swob.Response(status=202),
        ]) as swift:
            self.assertEqual(list(container_deleter.mark_for_deletion(
                swift,
                'account',
                'container',
                '',
                'end',
                'pre',
                timestamp=utils.Timestamp(ts),
                yield_time=0,
            )), [(5, 'obj5'), (6, 'obj6'), (6, None)])
            self.assertEqual(swift.calls, [
                ('GET', '/v1/account/container',
                 'format=json&marker=&end_marker=end&prefix=pre', {}, None),
                ('UPDATE', '/v1/.expiring_objects/' + ts.split('.')[0], '', {
                    'X-Backend-Allow-Private-Methods': 'True',
                    'X-Backend-Storage-Policy-Index': '0',
                    'X-Timestamp': ts}, mock.ANY),
                ('GET', '/v1/account/container',
                 'format=json&marker=obj6&end_marker=end&prefix=pre',
                 {}, None),
                ('UPDATE', '/v1/.expiring_objects/' + ts.split('.')[0], '', {
                    'X-Backend-Allow-Private-Methods': 'True',
                    'X-Backend-Storage-Policy-Index': '0',
                    'X-Timestamp': ts}, mock.ANY),
            ])
            self.assertEqual(
                json.loads(swift.calls[-3].body),
                container_deleter.make_delete_jobs(
                    'account', 'container',
                    ['obj1', 'obj2', 'obj3', u'obj4-\N{SNOWMAN}', 'obj5'],
                    utils.Timestamp(ts)
                )
            )
            self.assertEqual(
                json.loads(swift.calls[-1].body),
                container_deleter.make_delete_jobs(
                    'account', 'container', ['obj6'],
                    utils.Timestamp(ts)
                )
            )

    def test_init_internal_client_log_name(self):
        with mock.patch(
                'swift.cli.container_deleter.InternalClient') \
                as mock_ic:
            container_deleter.main(['a', 'c', '--request-tries', '2'])
        mock_ic.assert_called_once_with(
            '/etc/swift/internal-client.conf',
            'Swift Container Deleter', 2,
            global_conf={'log_name': 'container-deleter-ic'})