-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathctail_test.py
executable file
·490 lines (399 loc) · 15.3 KB
/
ctail_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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import time
import unittest
import ctail as ctail
class CtailTests(unittest.TestCase):
def setUp(self):
self.dir = "unittest/test"
self.file_name = "test.txt"
self.file_path = self.dir + "/" + self.file_name
self.another_file_name = "another.test.txt"
self.another_file_path = self.dir + "/" + self.another_file_name
self.soft_link_file_name = "softlink.txt"
self.soft_link_file_path = self.dir + "/" + self.soft_link_file_name
try:
exist = os.path.exists(self.dir)
if exist == False:
os.makedirs(self.dir)
except OSError as e:
print ("error : {}, {}".format(self.dir, e))
else:
print ("made dir : {}".format(self.dir))
def tearDown(self):
if os.path.isfile(self.file_path):
os.remove(self.file_path)
if os.path.isfile(self.another_file_path):
os.remove(self.another_file_path)
if os.path.islink(self.soft_link_file_path):
os.remove(self.soft_link_file_path)
os.removedirs(self.dir)
print ("removed dir : {}".format(self.dir))
pass
def newFile(self, end):
try:
f = open(self.file_path, "w")
for i in range(1, end+1):
print >> f, "{:07}".format(i)
finally:
if f is not None:
f.close()
def appendFile(self, begin, end):
try:
f = open(self.file_path, "aw")
for i in range(begin, end+1):
print >> f, "{:07}".format(i)
finally:
if f is not None:
f.close()
def removeFile(self):
try:
if os.path.isfile(self.file_path):
os.remove(self.file_path)
except OSError as e:
pass
def newAnotherFile(self, end):
try:
f = open(self.another_file_path, "w")
for i in range(1, end+1):
print >> f, "{:07}".format(i)
finally:
if f is not None:
f.close()
def appendAnotherFile(self, begin, end):
try:
f = open(self.another_file_path, "aw")
for i in range(begin, end+1):
print >> f, "{:07}".format(i)
finally:
if f is not None:
f.close()
def removeAnotherFile(self):
try:
if os.path.isfile(self.another_file_path):
os.remove(self.another_file_path)
except OSError as e:
pass
def linkFile(self, original):
try:
if os.path.islink(self.soft_link_file_path):
os.remove(self.soft_link_file_path)
os.symlink(original, self.soft_link_file_path)
except OSError as e:
pass
# 파일 크기가 2048보다 작거나 같은 경우
def test_the_filesize_eqless_than_2048(self):
self.newFile(256)
f = None
try:
ctail._verbose = False
f, error = ctail.open_tail(self.file_path)
self.assertEqual(False, error)
offset = f.tell()
size = os.path.getsize(self.file_path)
self.assertEqual(2048, size)
self.assertEqual(0, offset)
except Exception as e:
self.assertTrue(False, "error, {}".format(e))
finally:
if f is not None:
f.close()
# 파일 크기가 2048보다 큰경우
def test_the_filesize_greater_than_2048(self):
self.newFile(257)
f = None
try:
ctail._verbose = False
f, error = ctail.open_tail(self.file_path)
self.assertEqual(False, error)
offset = f.tell()
size = os.path.getsize(self.file_path)
self.assertEqual(2056, size)
self.assertEqual(size - 2048, offset)
except Exception as e:
self.assertTrue(False, "error, {}".format(e))
finally:
if f is not None:
f.close()
# tail 중인 파일에 변화가 없는 경우
def test_keep_tail_not_touched_file(self):
self.newFile(1)
f = None
try:
ctail._verbose = False
f, error = ctail.open_tail(self.file_path)
self.assertEqual(False, error)
offset = f.tell()
size = os.path.getsize(self.file_path)
self.assertEqual(8, size)
self.assertEqual(0, offset)
# 첫 번째 tail
offset, error = ctail.keep_tail(f)
self.assertEqual(False, error)
self.assertEqual(8, size)
self.assertEqual(8, offset)
# 두 번째 tail
offset, error = ctail.keep_tail(f)
self.assertEqual(False, error)
self.assertEqual(8, size)
self.assertEqual(8, offset)
except Exception as e:
self.assertTrue(False, "error, {}".format(e))
finally:
if f is not None:
f.close()
# tail 중인 파일에 변화가 있는 경우
def test_keep_tail_touched_file(self):
self.newFile(1)
f = None
try:
ctail._verbose = False
f, error = ctail.open_tail(self.file_path)
self.assertEqual(False, error)
offset = f.tell()
size = os.path.getsize(self.file_path)
self.assertEqual(8, size)
self.assertEqual(0, offset)
# 첫 번째 tail
offset, error = ctail.keep_tail(f)
size = os.path.getsize(self.file_path)
self.assertEqual(False, error)
self.assertEqual(8, size)
self.assertEqual(8, offset)
# 두 번째 tail
offset, error = ctail.keep_tail(f)
size = os.path.getsize(self.file_path)
self.assertEqual(False, error)
self.assertEqual(8, size)
self.assertEqual(8, offset)
# file 변경 후 tail
self.appendFile(2, 2)
offset, error = ctail.keep_tail(f)
size = os.path.getsize(self.file_path)
self.assertEqual(False, error)
self.assertEqual(16, size)
self.assertEqual(16, offset)
except Exception as e:
self.assertTrue(False, "error, {}".format(e))
finally:
if f is not None:
f.close()
# file이 있는 folder 내의 최신 파일 찾기
def test_find_the_newest_file(self):
self.newFile(1)
time.sleep(0.1)
self.newAnotherFile(1)
f = None
try:
ctail._verbose = False
abs_another_file_path = os.path.join(ctail.get_path_of(
self.another_file_path), self.another_file_name)
abs_file_path = os.path.join(
ctail.get_path_of(self.file_path), self.file_name)
target, exist, inode = ctail.get_tail_filename(
self.file_path, False)
self.assertEqual(True, exist)
self.assertEqual(abs_another_file_path, target)
time.sleep(0.1)
self.appendFile(2, 2)
target, exist, inode = ctail.get_tail_filename(
self.file_path, False)
self.assertEqual(True, exist)
self.assertEqual(abs_file_path, target)
time.sleep(0.1)
self.appendAnotherFile(2, 2)
target, exist, inode = ctail.get_tail_filename(
self.file_path, False)
self.assertEqual(True, exist)
self.assertEqual(abs_another_file_path, target)
except Exception as e:
self.assertTrue(False, "error, {}".format(e))
finally:
if f is not None:
f.close()
# -f option 을 사용해서 최신 파일 찾기
def test_find_the_file_with_option_f(self):
self.newFile(1)
time.sleep(0.1)
self.newAnotherFile(1)
f = None
try:
ctail._verbose = False
ctail._follow_file = True
target, exist, inode = ctail.get_tail_filename(
self.file_path, True)
self.assertEqual(True, exist)
self.assertEqual(os.path.abspath(self.file_path), target)
time.sleep(0.1)
self.appendFile(2, 2)
target, exist, inode = ctail.get_tail_filename(
self.file_path, True)
self.assertEqual(True, exist)
self.assertEqual(os.path.abspath(self.file_path), target)
time.sleep(0.1)
self.appendAnotherFile(2, 2)
target, exist, inode = ctail.get_tail_filename(
self.file_path, True)
self.assertEqual(True, exist)
self.assertEqual(os.path.abspath(self.file_path), target)
time.sleep(0.1)
self.appendFile(3, 3)
target, exist, inode = ctail.get_tail_filename(
self.another_file_path, True)
self.assertEqual(True, exist)
self.assertEqual(os.path.abspath(self.another_file_path), target)
time.sleep(0.1)
self.appendAnotherFile(3, 3)
target, exist, inode = ctail.get_tail_filename(
self.another_file_path, True)
self.assertEqual(True, exist)
self.assertEqual(os.path.abspath(self.another_file_path), target)
except Exception as e:
self.assertTrue(False, "error, {}".format(e))
finally:
if f is not None:
f.close()
# -f option 을 tail 하기
def test_keep_tail_the_file_with_option_f(self):
self.newFile(1)
time.sleep(0.1)
self.newAnotherFile(1)
f = None
try:
ctail._verbose = False
ctail._follow_file = True
target, exist, inode = ctail.get_tail_filename(
self.file_path, True)
self.assertEqual(True, exist)
self.assertEqual(os.path.abspath(self.file_path), target)
f, error = ctail.open_tail(target)
self.assertEqual(False, error)
offset = f.tell()
size = os.path.getsize(target)
self.assertEqual(8, size)
self.assertEqual(0, offset)
# 첫 번째 tail
offset, error = ctail.keep_tail(f)
self.assertEqual(False, error)
self.assertEqual(8, size)
self.assertEqual(8, offset)
# 두 번째 tail
offset, error = ctail.keep_tail(f)
self.assertEqual(False, error)
self.assertEqual(8, size)
self.assertEqual(8, offset)
time.sleep(0.1)
self.appendFile(2, 2)
time.sleep(0.1)
self.appendAnotherFile(2, 2)
# 세 번째 tail
offset, error = ctail.keep_tail(f)
size = os.path.getsize(target)
self.assertEqual(False, error)
self.assertEqual(16, size)
self.assertEqual(16, offset)
except Exception as e:
self.assertTrue(False, "error, {}".format(e))
finally:
if f is not None:
f.close()
# open 할 때 error
def test_first_error_in_opening_tail_the_file_with_option_f(self):
self.newFile(1)
f = None
try:
ctail._verbose = False
ctail._follow_file = True
target, exist, inode = ctail.get_tail_filename(
self.file_path, True)
self.assertEqual(True, exist)
self.assertEqual(os.path.abspath(self.file_path), target)
# file 이 지워지는 경우
self.removeFile()
f, error = ctail.open_tail(target)
self.assertEqual(True, error)
except Exception as e:
self.assertTrue(False, "error, {}".format(e))
finally:
if f is not None:
f.close()
# test 해보니 open 후에 파일이 지워지면 에러가 안남
def test_error_in_keeping_tail_the_file_with_option_f(self):
self.newFile(1)
f = None
try:
ctail._verbose = False
ctail._follow_file = True
target, exist, inode = ctail.get_tail_filename(
self.file_path, True)
print("get tail file:{}, {} ".format(
os.path.abspath(self.file_path), target))
self.assertEqual(True, exist)
self.assertEqual(os.path.abspath(self.file_path), target)
f, error = ctail.open_tail(target)
self.assertEqual(False, error)
offset = f.tell()
size = os.path.getsize(target)
self.assertEqual(8, size)
self.assertEqual(0, offset)
# 첫 번째 tail
offset, error = ctail.keep_tail(f)
self.assertEqual(False, error)
self.assertEqual(8, size)
self.assertEqual(8, offset)
# file 이 지워지는 경우
self.removeFile()
# file 이 지워져도 f 가 유효한 건지, error가 나지 않음
offset, error = ctail.keep_tail(f)
self.assertEqual(False, error)
self.assertEqual(8, offset)
offset, error = ctail.keep_tail(f)
self.assertEqual(False, error)
self.assertEqual(8, offset)
except Exception as e:
self.assertTrue(False, "error, {}".format(e))
finally:
if f is not None:
f.close()
# -f option, soft link file 의 inode 값 변경
def test_open_soft_link_change_point_with_option_f(self):
self.newFile(1)
self.newAnotherFile(1)
f = None
try:
self.linkFile(self.file_name)
original_path = os.readlink(self.soft_link_file_path)
# print("link:{} to original:{} ".format(
# self.soft_link_file_path, original_path))
ctail._verbose = False
ctail._follow_file = True
target, exist, inode = ctail.get_tail_filename(
self.soft_link_file_path, True)
#print("target:{}, exist:{}, inode:{} ".format(target, exist, inode))
self.assertEqual(True, exist)
self.assertEqual(os.path.abspath(self.soft_link_file_path), target)
self.linkFile(self.another_file_name)
original_path = os.readlink(self.soft_link_file_path)
# print("link:{} to original:{} ".format(
# self.soft_link_file_path, original_path))
target, exist, new_inode = ctail.get_tail_filename(
self.soft_link_file_path, True)
#print("target:{}, exist:{}, inode:{} ".format(target, exist, new_inode))
self.assertEqual(True, exist)
self.assertNotEqual(inode, new_inode)
except Exception as e:
self.assertTrue(False, "error, {}".format(e))
finally:
if f is not None:
f.close()
def test_exists_isfile_isdir_islink_to_soft_link(self):
self.newFile(1)
self.linkFile(self.file_name)
file = self.soft_link_file_path
self.assertEqual(True, os.path.exists(file))
self.assertEqual(True, os.path.isfile(file))
self.assertEqual(False, os.path.isdir(file))
self.assertEqual(True, os.path.islink(file))
if __name__ == '__main__':
unittest.main()