def test_tail_file_elatest2(rmlogs): """ CASE 2: rotate elatest, new elatest, update and process """ import shutil EXP_LATEST = 'exp_latest.log' PRE_LATEST = 'tailtest3_2016-03-30.log' finfo = tcfg['from'][0]['file'] bdir = finfo['dir'] epath = os.path.join(bdir, EXP_LATEST) with open(epath, 'w') as f: f.write('A\n') ptrn = "tailtest3_*-*-*.log" ftail = FileTailer(bdir, ptrn, 'wdfwd.tail', pos_dir, fcfg, 0, elatest=EXP_LATEST) ftail.update_target() rotated, psent_pos, sent_line = ftail.tmain() assert ftail.target_path.endswith(EXP_LATEST) assert ftail.elatest_fid is not None assert sent_line == 1 elatest_sent_pos = ftail.get_sent_pos() with open(epath, 'a') as f: f.write('B\n') f.write('C\n') # rotate latest pre_sent_pos = ftail.get_sent_pos() pre_path = os.path.join(bdir, PRE_LATEST) shutil.move(epath, pre_path) assert os.path.isfile(pre_path) assert not os.path.isfile(epath) # new elatest with open(epath, 'w') as f: f.write('a\n') f.write('b\n') f.write('c\n') time.sleep(1) rotated, psent_pos, sent_line = ftail.tmain() assert rotated # pre-elatest file is target assert ftail.target_path == pre_path # pre-elatest sent_pos is equal to elatest's assert psent_pos == elatest_sent_pos # send remain 2 lines assert sent_line == 2 # now new elatest is target ftail.update_target() assert ftail.target_path == epath assert ftail.may_send_newlines() == 3
def test_tail_file_elatest3(rmlogs): """ CASE 3: rotate elatest, reversed pre-elatest order """ import shutil EXP_LATEST = 'z1_action.log' PRE_LATEST = 'z1_action.log.1' PRE_LATEST2 = 'z1_action.log.2' finfo = tcfg['from'][0]['file'] bdir = finfo['dir'] epath = os.path.join(bdir, EXP_LATEST) with open(epath, 'w') as f: f.write('A\n') ptrn = "z*_action.*" ftail = FileTailer(bdir, ptrn, 'wdfwd.tail', pos_dir, fcfg, 0, elatest=EXP_LATEST, reverse_order=True) ftail.update_target() rotated, psent_pos, sent_line = ftail.tmain() assert ftail.target_path.endswith(EXP_LATEST) assert ftail.elatest_fid is not None assert sent_line == 1 elatest_sent_pos = ftail.get_sent_pos() with open(epath, 'a') as f: f.write('B\n') f.write('C\n') # rotate latest pre_sent_pos = ftail.get_sent_pos() pre_path = os.path.join(bdir, PRE_LATEST) shutil.move(epath, pre_path) assert os.path.isfile(pre_path) assert not os.path.isfile(epath) # new elatest with open(epath, 'w') as f: f.write('a\n') f.write('b\n') f.write('c\n') time.sleep(1) rotated, psent_pos, sent_line = ftail.tmain() assert rotated # pre-elatest file is target assert ftail.target_path == pre_path # pre-elatest sent_pos is equal to elatest's assert psent_pos == elatest_sent_pos # send remain 2 lines assert sent_line == 2 # now new elatest is target ftail.update_target() assert ftail.target_path == epath assert ftail.may_send_newlines() == 3 # rotate again time.sleep(2) pre_path2 = os.path.join(bdir, PRE_LATEST2) shutil.move(pre_path, pre_path2) shutil.move(epath, pre_path) rotated = ftail.handle_elatest_rotation(cur=0) assert rotated ftail.update_target() assert ftail.target_path == pre_path