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):
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')
46
44
os.mkdir(os.path.join(self.devices, 'sdb'))
56
54
devices=self.devices,
57
55
mount_check='false')
56
self.disk_file = DiskFile(self.devices, 'sda', '0', 'a', 'c', 'o',
59
59
def tearDown(self):
60
60
rmtree(os.path.dirname(self.testdir), ignore_errors=1)
63
63
def test_object_audit_extra_data(self):
64
64
self.auditor = auditor.AuditorWorker(self.conf)
66
disk_file = DiskFile(self.devices, 'sda', cur_part, 'a', 'c', 'o')
69
with disk_file.mkstemp() as (fd, tmppath):
67
with self.disk_file.mkstemp() as (fd, tmppath):
72
70
etag = etag.hexdigest()
76
74
'X-Timestamp': timestamp,
77
75
'Content-Length': str(os.fstat(fd).st_size),
79
disk_file.put(fd, tmppath, metadata)
77
self.disk_file.put(fd, tmppath, metadata)
80
78
pre_quarantines = self.auditor.quarantines
82
80
self.auditor.object_audit(
83
os.path.join(disk_file.datadir, timestamp + '.data'),
81
os.path.join(self.disk_file.datadir, timestamp + '.data'),
85
83
self.assertEquals(self.auditor.quarantines, pre_quarantines)
87
85
os.write(fd, 'extra_data')
88
86
self.auditor.object_audit(
89
os.path.join(disk_file.datadir, timestamp + '.data'),
87
os.path.join(self.disk_file.datadir, timestamp + '.data'),
91
89
self.assertEquals(self.auditor.quarantines, pre_quarantines + 1)
93
91
def test_object_audit_diff_data(self):
94
92
self.auditor = auditor.AuditorWorker(self.conf)
96
disk_file = DiskFile(self.devices, 'sda', cur_part, 'a', 'c', 'o')
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)
103
99
etag = etag.hexdigest()
106
102
'X-Timestamp': timestamp,
107
103
'Content-Length': str(os.fstat(fd).st_size),
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',
112
111
self.auditor.object_audit(
113
os.path.join(disk_file.datadir, timestamp + '.data'),
112
os.path.join(self.disk_file.datadir, timestamp + '.data'),
115
114
self.assertEquals(self.auditor.quarantines, pre_quarantines)
117
116
etag.update('1' + '0' * 1023)
120
119
write_metadata(fd, metadata)
122
121
self.auditor.object_audit(
123
os.path.join(disk_file.datadir, timestamp + '.data'),
122
os.path.join(self.disk_file.datadir, timestamp + '.data'),
125
124
self.assertEquals(self.auditor.quarantines, pre_quarantines + 1)
127
126
def test_object_audit_no_meta(self):
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)
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'),
137
os.path.join(self.disk_file.datadir, timestamp + '.data'),
142
139
self.assertEquals(self.auditor.quarantines, pre_quarantines + 1)
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
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
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),
171
disk_file.put(fd, tmppath, metadata)
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)
176
171
def test_object_run_once_no_sda(self):
177
172
self.auditor = auditor.AuditorWorker(self.conf)
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
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),
193
disk_file.put(fd, tmppath, metadata)
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)
199
192
def test_object_run_once_multi_devices(self):
200
193
self.auditor = auditor.AuditorWorker(self.conf)
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')
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),
216
disk_file.put(fd, tmppath, metadata)
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',
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),
231
disk_file.put(fd, tmppath, metadata)
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
241
disk_file = DiskFile(self.devices, 'sda', cur_part, 'a', 'c', 'o')
242
232
data = '0' * 1024
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),
253
disk_file.put(fd, tmppath, metadata)
243
self.disk_file.put(fd, tmppath, metadata)
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
271
260
ts_file_path = ''
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):
280
269
fp = open(ts_file_path, 'w')
284
disk_file = DiskFile(self.devices, 'sda', cur_part, 'a', 'c', 'o')
286
with disk_file.mkstemp() as (fd, tmppath):
273
with self.disk_file.mkstemp() as (fd, tmppath):
287
274
etag = etag.hexdigest()
290
277
'X-Timestamp': str(normalize_timestamp(time.time())),
291
278
'Content-Length': 10,
293
disk_file.put(fd, tmppath, metadata)
280
self.disk_file.put(fd, tmppath, metadata)
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
302
289
def test_object_run_fast_track_all(self):