~gholt/swift/timeout_baseexception

« back to all changes in this revision

Viewing changes to test/unit/obj/test_auditor.py

  • Committer: Tarmac
  • Author(s): David Goetz
  • Date: 2011-03-29 20:19:09 UTC
  • mfrom: (238.3.11 obj_server_0b_check)
  • Revision ID: tarmac-20110329201909-jvhx1i93pvg8gn6f
Check the md5sum against metadata ETag on object GETs, and zero byte checks on GETs, HEADs, and POSTs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
# See the License for the specific language governing permissions and
14
14
# limitations under the License.
15
15
 
16
 
# TODO: Tests
17
16
from test import unit
18
17
import unittest
19
18
import tempfile
22
21
from shutil import rmtree
23
22
from hashlib import md5
24
23
from tempfile import mkdtemp
 
24
from test.unit import FakeLogger
25
25
from swift.obj import auditor
26
26
from swift.obj import server as object_server
27
27
from swift.obj.server import DiskFile, write_metadata, DATADIR
34
34
class TestAuditor(unittest.TestCase):
35
35
 
36
36
    def setUp(self):
37
 
        self.testdir = \
38
 
            os.path.join(mkdtemp(), 'tmp_test_object_auditor')
 
37
        self.testdir = os.path.join(mkdtemp(), 'tmp_test_object_auditor')
39
38
        self.devices = os.path.join(self.testdir, 'node')
 
39
        self.logger = FakeLogger()
40
40
        rmtree(self.testdir, ignore_errors=1)
41
 
        os.mkdir(self.testdir)
42
 
        os.mkdir(self.devices)
43
 
        os.mkdir(os.path.join(self.devices, 'sda'))
 
41
        mkdirs(os.path.join(self.devices, 'sda'))
44
42
        self.objects = os.path.join(self.devices, 'sda', 'objects')
45
43
 
46
44
        os.mkdir(os.path.join(self.devices, 'sdb'))
55
53
        self.conf = dict(
56
54
            devices=self.devices,
57
55
            mount_check='false')
 
56
        self.disk_file = DiskFile(self.devices, 'sda', '0', 'a', 'c', 'o',
 
57
                                  self.logger)
58
58
 
59
59
    def tearDown(self):
60
60
        rmtree(os.path.dirname(self.testdir), ignore_errors=1)
62
62
 
63
63
    def test_object_audit_extra_data(self):
64
64
        self.auditor = auditor.AuditorWorker(self.conf)
65
 
        cur_part = '0'
66
 
        disk_file = DiskFile(self.devices, 'sda', cur_part, 'a', 'c', 'o')
67
65
        data = '0' * 1024
68
66
        etag = md5()
69
 
        with disk_file.mkstemp() as (fd, tmppath):
 
67
        with self.disk_file.mkstemp() as (fd, tmppath):
70
68
            os.write(fd, data)
71
69
            etag.update(data)
72
70
            etag = etag.hexdigest()
76
74
                'X-Timestamp': timestamp,
77
75
                'Content-Length': str(os.fstat(fd).st_size),
78
76
            }
79
 
            disk_file.put(fd, tmppath, metadata)
 
77
            self.disk_file.put(fd, tmppath, metadata)
80
78
            pre_quarantines = self.auditor.quarantines
81
79
 
82
80
            self.auditor.object_audit(
83
 
                os.path.join(disk_file.datadir, timestamp + '.data'),
84
 
                'sda', cur_part)
 
81
                os.path.join(self.disk_file.datadir, timestamp + '.data'),
 
82
                'sda', '0')
85
83
            self.assertEquals(self.auditor.quarantines, pre_quarantines)
86
84
 
87
85
            os.write(fd, 'extra_data')
88
86
            self.auditor.object_audit(
89
 
                os.path.join(disk_file.datadir, timestamp + '.data'),
90
 
                'sda', cur_part)
 
87
                os.path.join(self.disk_file.datadir, timestamp + '.data'),
 
88
                'sda', '0')
91
89
            self.assertEquals(self.auditor.quarantines, pre_quarantines + 1)
92
90
 
93
91
    def test_object_audit_diff_data(self):
94
92
        self.auditor = auditor.AuditorWorker(self.conf)
95
 
        cur_part = '0'
96
 
        disk_file = DiskFile(self.devices, 'sda', cur_part, 'a', 'c', 'o')
97
93
        data = '0' * 1024
98
94
        etag = md5()
99
95
        timestamp = str(normalize_timestamp(time.time()))
100
 
        with disk_file.mkstemp() as (fd, tmppath):
 
96
        with self.disk_file.mkstemp() as (fd, tmppath):
101
97
            os.write(fd, data)
102
98
            etag.update(data)
103
99
            etag = etag.hexdigest()
106
102
                'X-Timestamp': timestamp,
107
103
                'Content-Length': str(os.fstat(fd).st_size),
108
104
            }
109
 
            disk_file.put(fd, tmppath, metadata)
 
105
            self.disk_file.put(fd, tmppath, metadata)
110
106
            pre_quarantines = self.auditor.quarantines
 
107
            # remake so it will have metadata
 
108
            self.disk_file = DiskFile(self.devices, 'sda', '0', 'a', 'c', 'o',
 
109
                                      self.logger)
111
110
 
112
111
            self.auditor.object_audit(
113
 
                os.path.join(disk_file.datadir, timestamp + '.data'),
114
 
                'sda', cur_part)
 
112
                os.path.join(self.disk_file.datadir, timestamp + '.data'),
 
113
                'sda', '0')
115
114
            self.assertEquals(self.auditor.quarantines, pre_quarantines)
116
115
            etag = md5()
117
116
            etag.update('1' + '0' * 1023)
120
119
            write_metadata(fd, metadata)
121
120
 
122
121
            self.auditor.object_audit(
123
 
                os.path.join(disk_file.datadir, timestamp + '.data'),
124
 
                'sda', cur_part)
 
122
                os.path.join(self.disk_file.datadir, timestamp + '.data'),
 
123
                'sda', '0')
125
124
            self.assertEquals(self.auditor.quarantines, pre_quarantines + 1)
126
125
 
127
126
    def test_object_audit_no_meta(self):
128
 
        cur_part = '0'
129
 
        disk_file = DiskFile(self.devices, 'sda', cur_part, 'a', 'c', 'o')
130
127
        timestamp = str(normalize_timestamp(time.time()))
131
 
        path = os.path.join(disk_file.datadir, timestamp + '.data')
132
 
        mkdirs(disk_file.datadir)
 
128
        path = os.path.join(self.disk_file.datadir, timestamp + '.data')
 
129
        mkdirs(self.disk_file.datadir)
133
130
        fp = open(path, 'w')
134
131
        fp.write('0' * 1024)
135
132
        fp.close()
136
 
        invalidate_hash(os.path.dirname(disk_file.datadir))
 
133
        invalidate_hash(os.path.dirname(self.disk_file.datadir))
137
134
        self.auditor = auditor.AuditorWorker(self.conf)
138
135
        pre_quarantines = self.auditor.quarantines
139
136
        self.auditor.object_audit(
140
 
            os.path.join(disk_file.datadir, timestamp + '.data'),
141
 
            'sda', cur_part)
 
137
            os.path.join(self.disk_file.datadir, timestamp + '.data'),
 
138
            'sda', '0')
142
139
        self.assertEquals(self.auditor.quarantines, pre_quarantines + 1)
143
140
 
144
141
    def test_object_audit_bad_args(self):
153
150
    def test_object_run_once_pass(self):
154
151
        self.auditor = auditor.AuditorWorker(self.conf)
155
152
        self.auditor.log_time = 0
156
 
        cur_part = '0'
157
153
        timestamp = str(normalize_timestamp(time.time()))
158
154
        pre_quarantines = self.auditor.quarantines
159
 
        disk_file = DiskFile(self.devices, 'sda', cur_part, 'a', 'c', 'o')
160
155
        data = '0' * 1024
161
156
        etag = md5()
162
 
        with disk_file.mkstemp() as (fd, tmppath):
 
157
        with self.disk_file.mkstemp() as (fd, tmppath):
163
158
            os.write(fd, data)
164
159
            etag.update(data)
165
160
            etag = etag.hexdigest()
168
163
                'X-Timestamp': timestamp,
169
164
                'Content-Length': str(os.fstat(fd).st_size),
170
165
            }
171
 
            disk_file.put(fd, tmppath, metadata)
172
 
            disk_file.close()
 
166
            self.disk_file.put(fd, tmppath, metadata)
 
167
            self.disk_file.close()
173
168
        self.auditor.audit_all_objects()
174
169
        self.assertEquals(self.auditor.quarantines, pre_quarantines)
175
170
 
176
171
    def test_object_run_once_no_sda(self):
177
172
        self.auditor = auditor.AuditorWorker(self.conf)
178
 
        cur_part = '0'
179
173
        timestamp = str(normalize_timestamp(time.time()))
180
174
        pre_quarantines = self.auditor.quarantines
181
 
        disk_file = DiskFile(self.devices, 'sdb', cur_part, 'a', 'c', 'o')
182
175
        data = '0' * 1024
183
176
        etag = md5()
184
 
        with disk_file.mkstemp() as (fd, tmppath):
 
177
        with self.disk_file.mkstemp() as (fd, tmppath):
185
178
            os.write(fd, data)
186
179
            etag.update(data)
187
180
            etag = etag.hexdigest()
190
183
                'X-Timestamp': timestamp,
191
184
                'Content-Length': str(os.fstat(fd).st_size),
192
185
            }
193
 
            disk_file.put(fd, tmppath, metadata)
194
 
            disk_file.close()
 
186
            self.disk_file.put(fd, tmppath, metadata)
 
187
            self.disk_file.close()
195
188
            os.write(fd, 'extra_data')
196
189
        self.auditor.audit_all_objects()
197
190
        self.assertEquals(self.auditor.quarantines, pre_quarantines + 1)
198
191
 
199
192
    def test_object_run_once_multi_devices(self):
200
193
        self.auditor = auditor.AuditorWorker(self.conf)
201
 
        cur_part = '0'
202
194
        timestamp = str(normalize_timestamp(time.time()))
203
195
        pre_quarantines = self.auditor.quarantines
204
 
        disk_file = DiskFile(self.devices, 'sda', cur_part, 'a', 'c', 'o')
205
196
        data = '0' * 10
206
197
        etag = md5()
207
 
        with disk_file.mkstemp() as (fd, tmppath):
 
198
        with self.disk_file.mkstemp() as (fd, tmppath):
208
199
            os.write(fd, data)
209
200
            etag.update(data)
210
201
            etag = etag.hexdigest()
213
204
                'X-Timestamp': timestamp,
214
205
                'Content-Length': str(os.fstat(fd).st_size),
215
206
            }
216
 
            disk_file.put(fd, tmppath, metadata)
217
 
            disk_file.close()
 
207
            self.disk_file.put(fd, tmppath, metadata)
 
208
            self.disk_file.close()
218
209
        self.auditor.audit_all_objects()
219
 
        disk_file = DiskFile(self.devices, 'sdb', cur_part, 'a', 'c', 'ob')
 
210
        self.disk_file = DiskFile(self.devices, 'sdb', '0', 'a', 'c',
 
211
                                  'ob', self.logger)
220
212
        data = '1' * 10
221
213
        etag = md5()
222
 
        with disk_file.mkstemp() as (fd, tmppath):
 
214
        with self.disk_file.mkstemp() as (fd, tmppath):
223
215
            os.write(fd, data)
224
216
            etag.update(data)
225
217
            etag = etag.hexdigest()
228
220
                'X-Timestamp': timestamp,
229
221
                'Content-Length': str(os.fstat(fd).st_size),
230
222
            }
231
 
            disk_file.put(fd, tmppath, metadata)
232
 
            disk_file.close()
 
223
            self.disk_file.put(fd, tmppath, metadata)
 
224
            self.disk_file.close()
233
225
            os.write(fd, 'extra_data')
234
226
        self.auditor.audit_all_objects()
235
227
        self.assertEquals(self.auditor.quarantines, pre_quarantines + 1)
237
229
    def test_object_run_fast_track_non_zero(self):
238
230
        self.auditor = auditor.ObjectAuditor(self.conf)
239
231
        self.auditor.log_time = 0
240
 
        cur_part = '0'
241
 
        disk_file = DiskFile(self.devices, 'sda', cur_part, 'a', 'c', 'o')
242
232
        data = '0' * 1024
243
233
        etag = md5()
244
 
        with disk_file.mkstemp() as (fd, tmppath):
 
234
        with self.disk_file.mkstemp() as (fd, tmppath):
245
235
            os.write(fd, data)
246
236
            etag.update(data)
247
237
            etag = etag.hexdigest()
250
240
                'X-Timestamp': str(normalize_timestamp(time.time())),
251
241
                'Content-Length': str(os.fstat(fd).st_size),
252
242
            }
253
 
            disk_file.put(fd, tmppath, metadata)
 
243
            self.disk_file.put(fd, tmppath, metadata)
254
244
            etag = md5()
255
245
            etag.update('1' + '0' * 1023)
256
246
            etag = etag.hexdigest()
267
257
    def setup_bad_zero_byte(self, with_ts=False):
268
258
        self.auditor = auditor.ObjectAuditor(self.conf)
269
259
        self.auditor.log_time = 0
270
 
        cur_part = '0'
271
260
        ts_file_path = ''
272
261
        if with_ts:
273
262
 
274
263
            name_hash = hash_path('a', 'c', 'o')
275
264
            dir_path = os.path.join(self.devices, 'sda',
276
 
                               storage_directory(DATADIR, cur_part, name_hash))
 
265
                               storage_directory(DATADIR, '0', name_hash))
277
266
            ts_file_path = os.path.join(dir_path, '99999.ts')
278
267
            if not os.path.exists(dir_path):
279
268
                mkdirs(dir_path)
280
269
            fp = open(ts_file_path, 'w')
281
270
            fp.close()
282
271
 
283
 
 
284
 
        disk_file = DiskFile(self.devices, 'sda', cur_part, 'a', 'c', 'o')
285
272
        etag = md5()
286
 
        with disk_file.mkstemp() as (fd, tmppath):
 
273
        with self.disk_file.mkstemp() as (fd, tmppath):
287
274
            etag = etag.hexdigest()
288
275
            metadata = {
289
276
                'ETag': etag,
290
277
                'X-Timestamp': str(normalize_timestamp(time.time())),
291
278
                'Content-Length': 10,
292
279
            }
293
 
            disk_file.put(fd, tmppath, metadata)
 
280
            self.disk_file.put(fd, tmppath, metadata)
294
281
            etag = md5()
295
282
            etag = etag.hexdigest()
296
283
            metadata['ETag'] = etag
297
284
            write_metadata(fd, metadata)
298
 
        if disk_file.data_file:
299
 
            return disk_file.data_file
 
285
        if self.disk_file.data_file:
 
286
            return self.disk_file.data_file
300
287
        return ts_file_path
301
288
 
302
289
    def test_object_run_fast_track_all(self):