Esempio n. 1
0
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
Esempio n. 2
0
def test_tail_file_elatest1(rmlogs):
    """
    CASE 1: rotate elatest, process and new elatest
    """

    EXP_LATEST = 'exp_latest.log'
    PRE_LATEST = 'tailtest3_2016-03-30.9.log'
    PRE_LATEST2 = 'tailtest3_2016-03-30.10.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,
                       order_ptrn=r'(?P<date>[^\.]+)\.(?P<order>\d+)\.log')
    ftail.update_target()
    rotated = ftail.handle_elatest_rotation()
    assert not rotated
    epath, efid = ftail.get_elatest_info()
    assert epath and efid
    assert ftail.target_path.endswith(EXP_LATEST)
    assert ftail.elatest_fid is not None
    # update target again
    ftail.update_target()
    assert ftail.target_path.endswith(EXP_LATEST)
    assert ftail.may_send_newlines() == 1
    elatest_sent_pos = ftail.get_sent_pos()

    with open(epath, 'a') as f:
        f.write('B\n')
        f.write('C\n')

    ### CASE 1: rotate elatest, process and new elatest
    ## 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)

    ## check elatest rotation
    rotated = ftail.handle_elatest_rotation(cur=0)
    assert rotated
    # now elatest fid is None
    assert ftail.elatest_fid is None
    ftail.update_target()
    # pre-elatest file is target
    assert ftail.target_path == pre_path
    # pre-elatest sent_pos is equal to elatest's
    assert ftail.get_sent_pos() == elatest_sent_pos
    # elatest sent_pos is cleared
    assert ftail.get_sent_pos(epath) == 0
    # send remain 2 lines
    assert ftail.may_send_newlines() == 2

    # new elatest
    with open(epath, 'w') as f:
        f.write('a\n')
        f.write('b\n')
        f.write('c\n')

    rotated = ftail.handle_elatest_rotation(cur=0)
    assert rotated == False
    ftail.update_target()
    assert ftail.target_path.endswith(EXP_LATEST)
    epath, efid = ftail.get_elatest_info()
    assert epath and efid
    assert ftail.get_sent_pos() == 0
    assert ftail.may_send_newlines() == 3

    # rotate again
    time.sleep(2)
    pre_path2 = os.path.join(bdir, PRE_LATEST2)
    shutil.move(epath, pre_path2)
    rotated = ftail.handle_elatest_rotation(cur=0)
    assert rotated
    ftail.update_target()
    assert ftail.target_path == pre_path2