-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstore_test.py
360 lines (270 loc) · 11.9 KB
/
store_test.py
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
from testify import *
import io
import datetime
import shutil
import tempfile
import os
import blueox
from blueox import store
class ParseDateArgumentTestCase(TestCase):
def test_simple_date(self):
dt = store.parse_date_range_argument("20150521")
assert_equal(dt, datetime.datetime(2015, 5, 21, 0, 0, 0))
def test_simple_time(self):
dt = store.parse_date_range_argument("20150521 12:45")
assert_equal(dt, datetime.datetime(2015, 5, 21, 12, 45, 0))
def test_bad(self):
with assert_raises(store.InvalidDateError):
store.parse_date_range_argument("foo")
class LogFileFilePathTest(TestCase):
def test_simple_date(self):
date = datetime.date(2015, 5, 21)
lf = store.LogFile("foo", date=date)
assert_equal(lf.file_path, "20150521/foo-20150521.log")
def test_simple_dt(self):
dt = datetime.datetime(2015, 5, 21, 20)
lf = store.LogFile("foo", dt=dt)
assert_equal(lf.file_path, "20150521/foo-2015052120.log")
def test_dt_host(self):
dt = datetime.datetime(2015, 5, 21, 20)
lf = store.LogFile("foo", dt=dt, host='localhost')
assert_equal(lf.file_path, "20150521/foo-2015052120-localhost.log")
def test_dt_zipped(self):
dt = datetime.datetime(2015, 5, 21, 20)
lf = store.LogFile("foo", dt=dt, bzip=True)
assert_equal(lf.file_path, "20150521/foo-2015052120.log.bz2")
class LogFileFromFilenameTest(TestCase):
def test_simple_date(self):
file_name = "/var/log/20150521/foo-20150521.log"
lf = store.LogFile.from_filename(file_name)
assert_equal(lf.type_name, "foo")
assert_equal(lf.date, datetime.date(2015, 5, 21))
assert_equal(lf.host, None)
assert_equal(lf.bzip, False)
def test_simple_dt(self):
file_name = "/var/log/20150521/foo-2015052120.log"
lf = store.LogFile.from_filename(file_name)
assert_equal(lf.dt, datetime.datetime(2015, 5, 21, 20))
def test_host(self):
file_name = "/var/log/20150521/foo-2015052120-localhost.log"
lf = store.LogFile.from_filename(file_name)
assert_equal(lf.host, "localhost")
def test_fqdnhost(self):
file_name = "/var/log/20150521/foo-2015052120-ip-192-168-1-1.compute.internal.log"
lf = store.LogFile.from_filename(file_name)
assert_equal(lf.host, "ip-192-168-1-1.compute.internal")
def test_bzip(self):
file_name = "/var/log/20150521/foo-2015052120-localhost.log.bz2"
lf = store.LogFile.from_filename(file_name)
assert lf.bzip
class S3LogFileFromS3KeyTest(TestCase):
def test(self):
key = turtle.Turtle()
key.name = "20150521/foo-20150521-localhost.log.bz2"
lf = store.S3LogFile.from_s3_key(key)
assert_equal(lf.type_name, "foo")
assert_equal(lf.date, datetime.date(2015, 5, 21))
assert_equal(lf.host, "localhost")
assert_equal(lf.bzip, True)
class LocalLogFileBuildRemoteTest(TestCase):
def test(self):
lf = store.LocalLogFile('foo', dt=datetime.datetime(2015, 5, 21, 20), bzip=True)
r_lf = lf.build_remote('log-host')
assert_equal(r_lf.host, 'log-host')
assert_equal(r_lf.type_name, 'foo')
assert_equal(r_lf.date, datetime.date(2015, 5, 21))
assert r_lf.bzip
class ListLogFilesTest(TestCase):
@setup
def build_log_directory(self):
self.log_path = tempfile.mkdtemp(suffix="oxtest")
@teardown
def remove_log_directory(self):
shutil.rmtree(self.log_path)
def test_empty(self):
files = store.list_log_files(self.log_path)
assert_equal(len(files), 0)
def test_garbage(self):
file_name = os.path.join(self.log_path, "foo.dat")
with open(file_name, "w") as f:
f.write("hi")
files = store.list_log_files(self.log_path)
assert_equal(len(files), 0)
def test_simple(self):
file_name = os.path.join(self.log_path, "foo-20150521.log")
with open(file_name, "w") as f:
f.write("hi")
files = store.list_log_files(self.log_path)
assert_equal(len(files), 1)
assert_equal(files[0].type_name, "foo")
class FilterActiveTest(TestCase):
def test_leave_active(self):
files = [store.LogFile('foo', date=datetime.date.today())]
out_files = store.filter_log_files_for_active(files)
assert_equal(len(out_files), 0)
def test_only_yesterday(self):
files = [
store.LogFile('foo', date=datetime.date.today()),
store.LogFile('bar', date=datetime.date.today()),
store.LogFile('bar', date=datetime.date.today() - datetime.timedelta(days=1))
]
out_files = store.filter_log_files_for_active(files)
assert_equal(len(out_files), 1)
assert_equal(out_files[0], files[-1])
def test_hourly(self):
now_hourly = datetime.datetime.utcnow().replace(minute=0, second=0)
files = [
store.LogFile('bar', dt=now_hourly - datetime.timedelta(hours=1)),
store.LogFile('bar', dt=now_hourly)
]
out_files = store.filter_log_files_for_active(files)
assert_equal(len(out_files), 1)
assert_equal(out_files[0], files[0])
def test_hourly_and_daily(self):
files = [
store.LogFile('bar', date=datetime.date(2015, 5, 20)),
store.LogFile('bar', dt=datetime.datetime(2015, 5, 21, 20)),
store.LogFile('bar', dt=datetime.datetime.utcnow().replace(minute=0, second=0))
]
out_files = store.filter_log_files_for_active(files)
assert_equal(len(out_files), 2)
class FilterUnzippedTest(TestCase):
def test_no_zipped(self):
date = (datetime.datetime.utcnow() - datetime.timedelta(days=2)).date()
files = [store.LogFile('foo', date=date, bzip=True)]
out_files = store.filter_log_files_for_zipping(files)
assert_equal(len(out_files), 0)
class FilterUploadTest(TestCase):
def test_only_zipped(self):
date = (datetime.datetime.utcnow() - datetime.timedelta(days=2)).date()
files = [store.LogFile('foo', date=date, bzip=False)]
out_files = store.filter_log_files_for_uploading(files, True)
assert_equal(len(out_files), 0)
def test_any(self):
date = (datetime.datetime.utcnow() - datetime.timedelta(days=2)).date()
files = [store.LogFile('foo', date=date, bzip=False)]
out_files = store.filter_log_files_for_uploading(files, False)
assert_equal(len(out_files), 1)
class ZipLogFileTest(TestCase):
@setup
def build_log_directory(self):
self.log_path = tempfile.mkdtemp(suffix="oxtest")
@teardown
def remove_log_directory(self):
shutil.rmtree(self.log_path)
def test(self):
log_file = store.LocalLogFile("foo", date=datetime.date(2015, 5, 21))
full_file_path = os.path.join(self.log_path, log_file.file_path)
os.makedirs(os.path.dirname(full_file_path))
with open(full_file_path, "w") as f:
f.write("hi")
store.zip_log_file(log_file, self.log_path)
assert log_file.bzip
assert os.path.exists(os.path.join(self.log_path, log_file.file_path))
class S3PrefixTest(TestCase):
def test(self):
dt = datetime.datetime(2015, 5, 21)
prefix = store.s3_prefix_for_date_and_type(dt, "foo")
assert_equal(prefix, "20150521/foo-")
class InclusiveDateRangeTest(TestCase):
def test_dates(self):
start_dt = datetime.datetime(2015, 5, 19)
end_dt = datetime.datetime(2015, 5, 21)
dates = list(store.inclusive_date_range(start_dt, end_dt))
assert_equal(len(dates), 3)
assert_equal(dates[0], start_dt.date())
assert_equal(dates[-1], end_dt.date())
def test_boundry(self):
start_dt = datetime.datetime(2015, 5, 19, 20)
end_dt = datetime.datetime(2015, 5, 21, 3)
dates = list(store.inclusive_date_range(start_dt, end_dt))
assert_equal(len(dates), 3)
assert_equal(dates[0], start_dt.date())
assert_equal(dates[-1], end_dt.date())
class FindLogFilesInS3Test(TestCase):
def test_empty(self):
start_dt = datetime.datetime(2015, 5, 19)
end_dt = datetime.datetime(2015, 5, 21)
bucket = turtle.Turtle()
def do_list(prefix):
return []
bucket.list = do_list
log_files = store.find_log_files_in_s3(bucket, "foo", start_dt, end_dt)
assert_equal(len(log_files), 0)
def test_date(self):
start_dt = datetime.datetime(2015, 5, 19)
end_dt = datetime.datetime(2015, 5, 21)
bucket = turtle.Turtle()
def do_list(prefix):
if prefix.startswith("20150519"):
key = turtle.Turtle()
key.name = "20150519/foo-20150519-localhost.log.bz2"
return [key]
else:
return []
bucket.list = do_list
log_files = store.find_log_files_in_s3(bucket, "foo", start_dt, end_dt)
assert_equal(len(log_files), 1)
assert_equal(log_files[0].type_name, "foo")
def test_range(self):
start_dt = datetime.datetime(2015, 5, 19, 1)
end_dt = datetime.datetime(2015, 5, 19, 3)
bucket = turtle.Turtle()
def do_list(prefix):
assert prefix.startswith("20150519")
return [
turtle.Turtle(name="20150519/foo-2015051900-localhost.log.bz2"),
turtle.Turtle(name="20150519/foo-2015051901-localhost.log.bz2"),
turtle.Turtle(name="20150519/foo-2015051902-localhost.log.bz2"),
turtle.Turtle(name="20150519/foo-2015051903-localhost.log.bz2"),
turtle.Turtle(name="20150519/foo-2015051904-localhost.log.bz2"),
]
bucket.list = do_list
log_files = store.find_log_files_in_s3(bucket, "foo", start_dt, end_dt)
assert_equal(len(log_files), 3)
assert_equal(log_files[0].dt, start_dt)
assert_equal(log_files[-1].dt, end_dt)
def test_range_with_date_key(self):
start_dt = datetime.datetime(2015, 5, 19, 1)
end_dt = datetime.datetime(2015, 5, 19, 3)
bucket = turtle.Turtle()
def do_list(prefix):
assert prefix.startswith("20150519")
return [
turtle.Turtle(name="20150519/foo-20150519-localhost.log.bz2"),
]
bucket.list = do_list
log_files = store.find_log_files_in_s3(bucket, "foo", start_dt, end_dt)
assert_equal(len(log_files), 1)
class FindLogFilesInLocalTest(TestCase):
@setup
def build_log_directory(self):
self.log_path = tempfile.mkdtemp(suffix="oxtest")
@teardown
def remove_log_directory(self):
shutil.rmtree(self.log_path)
def test_empty(self):
start_dt = datetime.datetime(2015, 5, 19)
end_dt = datetime.datetime(2015, 5, 21)
log_files = store.find_log_files_in_path(self.log_path, "foo", start_dt, end_dt)
assert_equal(len(log_files), 0)
def test_range(self):
dts = [
datetime.datetime(2015, 5, 19, 0),
datetime.datetime(2015, 5, 19, 1),
datetime.datetime(2015, 5, 19, 2),
datetime.datetime(2015, 5, 19, 3),
datetime.datetime(2015, 5, 19, 4)]
os.makedirs(os.path.join(self.log_path, dts[0].strftime("%Y%m%d")))
for dt in dts:
date_str = dt.strftime('%Y%m%d')
dt_str = dt.strftime('%Y%m%d%H')
full_path = os.path.join(self.log_path, date_str, "foo-{}.log".format(dt_str))
with io.open(full_path, "w") as f:
f.write(u"hi")
start_dt = datetime.datetime(2015, 5, 19, 1)
end_dt = datetime.datetime(2015, 5, 19, 3)
log_files = store.find_log_files_in_path(self.log_path, "foo", start_dt, end_dt)
assert_equal(len(log_files), 3)
assert_equal(log_files[0].dt, start_dt)
assert_equal(log_files[-1].dt, end_dt)