Esempio n. 1
0
def test_find_old_backups_weekly():
    cs = CauSync(config, src, dst, task='cleanup')
    cs.config.DATE_FORMAT = "%Y%m%d"
    cs.config.BACKUPS_TO_KEEP = {
        'yearly': 10,
        'monthly': 6,
        'weekly': 4,
        'daily': 7
    }
    cs.config.BACKUP_MULTIPLIERS = {
        'yearly': 365,
        'monthly': 31,
        'weekly': 7,
        'daily': 1
    }
    cs.config.BACKUPS_LINK_DEST_COUNT = 5
    cs.curdate = datetime(year=2018, month=4, day=11)

    (dirnames_keep, dirnames_delete) = ([
        datetime(2018, 4, 9, 0, 0),
        datetime(2018, 4, 2, 0, 0)
    ], [
        datetime(2018, 1, 1, 0, 0),
        datetime(2016, 4, 11, 0, 0),
        datetime(2016, 2, 1, 0, 0),
        datetime(2007, 1, 1, 0, 0)
    ])

    assert cs.find_old_backups(dirnames,
                               'weekly') == (dirnames_keep, dirnames_delete)
Esempio n. 2
0
def test_dry_run():
    create_temp()

    # start sync testing
    cs = CauSync(config, src, dst, task='sync')
    cs.config.DATE_FORMAT = date_format
    cs.dry_run = True
    cs.run_sync()

    # check for non-existing test dirs
    [
        assert_false(
            os.path.isdir(os.path.join(dst, curdate_str, 'causync_src', d)))
        for d in ['testdir1', 'testdir2']
    ]

    # collect destination testfile paths
    files = [
        os.path.join(dst, curdate_str, 'causync_src', 'testdir1', 'testfile1'),
        os.path.join(dst, curdate_str, 'causync_src', 'testdir1', 'testfile2'),
        os.path.join(dst, curdate_str, 'causync_src', 'testdir2', 'testfile3')
    ]

    # check for non-existing testfiles
    [assert_false(os.path.isfile(f)) for f in files]

    remove_temp()
Esempio n. 3
0
def test_sync():
    create_temp()

    # start sync testing
    cs = CauSync(config, src, dst, task='sync')
    cs.config.DATE_FORMAT = date_format
    cs.run_sync()

    # check for test dirs
    [
        assert_true(
            os.path.isdir(os.path.join(dst, curdate_str, 'causync_src', d)))
        for d in ['testdir1', 'testdir2']
    ]

    # collect destination testfile paths
    files = [
        os.path.join(dst, curdate_str, 'causync_src', 'testdir1', 'testfile1'),
        os.path.join(dst, curdate_str, 'causync_src', 'testdir1', 'testfile2'),
        os.path.join(dst, curdate_str, 'causync_src', 'testdir2', 'testfile3')
    ]

    # check for existing testfiles
    [assert_true(os.path.isfile(f)) for f in files]

    # compare testfile contents
    for i in range(0, 3):
        with open(files[i], 'r') as fp:
            assert_equals(fp.read(),
                          lorem[lorem_parts[i][0]:lorem_parts[i][1]])

    remove_temp()
Esempio n. 4
0
def test_multi_exclude():
    create_temp()

    # start sync testing
    cs = CauSync(config,
                 src,
                 dst,
                 task='sync',
                 excludes=['testfile2', 'testfile3'])
    cs.config.DATE_FORMAT = date_format
    cs.run_sync()

    # collect destination testfile paths
    files = [
        os.path.join(dst, curdate_str, 'causync_src', 'testdir1', 'testfile1'),
        os.path.join(dst, curdate_str, 'causync_src', 'testdir1', 'testfile2'),
        os.path.join(dst, curdate_str, 'causync_src', 'testdir2', 'testfile3')
    ]

    # check for file1 and file4
    assert_true(os.path.isfile(files[0]))
    # file2 and file3 should not exist because of exclude
    [assert_false(os.path.isfile(f)) for f in [files[1], files[2]]]

    remove_temp()
Esempio n. 5
0
def test_get_dirdate():
    cs = CauSync(config, "/tmp/causync_src", "/tmp/causync_dest", 'check')
    now = datetime.now()
    cs.config.DATE_FORMAT = "%Y%m%d"
    dirname = now.strftime(config.DATE_FORMAT)

    assert isinstance(cs.get_dirdate(dirname), datetime)
    assert cs.get_dirdate(dirname).strftime(
        cs.config.DATE_FORMAT) == now.strftime(cs.config.DATE_FORMAT)
Esempio n. 6
0
def test_find_latest_backups():
    cs = CauSync(config, src, dst, task='sync')
    cs.config.DATE_FORMAT = "%Y%m%d"

    paths = ['20180411', '20180410', '20180409', '20180408', '20180407']

    dirnames_result = [path.realpath("{}/{}".format(dst, i)) for i in paths]

    print(cs.find_latest_backups(dirnames))
    assert cs.find_latest_backups(dirnames) == dirnames_result
Esempio n. 7
0
def test_lockfile_after_sync():
    create_temp()

    # start sync testing
    cs = CauSync(config, src, dst, task='sync')
    cs.config.DATE_FORMAT = date_format
    cs.run_sync()

    # lockfile should not exist after sync
    assert_false(os.path.isfile("{}.lock".format(src)))

    remove_temp()
Esempio n. 8
0
def test_logfile_default():
    create_temp()

    # start sync testing
    cs = CauSync(config, src, dst, task='sync')
    cs.config.DATE_FORMAT = date_format
    cs.run_sync()

    # lockfile should not exist after sync
    assert_true(os.path.isfile(cs.config.LOGFILE))

    remove_temp()
Esempio n. 9
0
def test_logfile_custom():
    create_temp()

    # start sync testing
    cs = CauSync(config, src, dst, task='sync', logfile='./temp/test.log')
    cs.config.DATE_FORMAT = date_format
    cs.run_sync()

    # lockfile should not exist after sync
    assert_true(os.path.isfile('./temp/test.log'))

    remove_temp()
Esempio n. 10
0
def test_get_parent_dir():
    cs = CauSync(config, "/tmp/causync_src", "/tmp/causync_dest", 'sync')

    assert_equals(cs.get_parent_dir('/tmp/causync_src/test'),
                  '/tmp/causync_src')
    assert_equals(cs.get_parent_dir('/tmp/causync_src/test/'),
                  '/tmp/causync_src')
    assert_equals(cs.get_parent_dir('/tmp/causync_src'), '/tmp')
    assert_equals(cs.get_parent_dir('/tmp/causync_src/'), '/tmp')
    assert_equals(cs.get_parent_dir('/tmp'), '/')
    assert_equals(cs.get_parent_dir('/tmp/'), '/')
    assert_equals(cs.get_parent_dir('/'), '/')
Esempio n. 11
0
def test_exclude_file():
    create_temp()

    with open('./temp/exclude.txt', 'w') as f:
        f.write('testfile1\ntestfile3\n')

    # start sync testing
    cs = CauSync(config, src, dst, task='sync')
    cs.config.DATE_FORMAT = date_format
    cs.excludes = cs.parse_exclude_file('./temp/exclude.txt')
    cs.run_sync()

    # collect destination testfile paths
    files = [
        os.path.join(dst, curdate_str, 'causync_src', 'testdir1', 'testfile1'),
        os.path.join(dst, curdate_str, 'causync_src', 'testdir1', 'testfile2'),
        os.path.join(dst, curdate_str, 'causync_src', 'testdir2', 'testfile3')
    ]

    # check for file1 and file3
    [assert_false(os.path.isfile(f)) for f in [files[0], files[2]]]
    # file2 should not exist because of exclude
    assert_true(os.path.isfile(files[1]))

    remove_temp()
Esempio n. 12
0
def test_cleanup():
    remove_temp()

    cs = CauSync(config, src, dst, task='cleanup')
    cs.config.DATE_FORMAT = "%Y%m%d"
    cs.config.BACKUPS_TO_KEEP = {
        'yearly': 10,
        'monthly': 6,
        'weekly': 4,
        'daily': 7
    }
    cs.config.BACKUP_MULTIPLIERS = {
        'yearly': 365,
        'monthly': 31,
        'weekly': 7,
        'daily': 1
    }
    cs.config.BACKUPS_LINK_DEST_COUNT = 5
    cs.curdate = curdate

    create_temp()

    os.makedirs(dst)
    [os.makedirs(os.path.join(dst, i)) for i in dirnames]

    dirnames_delete = list(set(dirnames) - set(dirnames_keep))

    cs.run_cleanup()

    for dirname in dirnames_keep:
        isdircheck = os.path.realpath(
            os.path.join("./temp/causync_dst", dirname))
        assert_true(os.path.isdir(isdircheck))

    for dirname in dirnames_delete:
        isdircheck = os.path.realpath(
            os.path.join("./temp/causync_dst", dirname))
        assert_false(os.path.isdir(isdircheck))

    remove_temp()
Esempio n. 13
0
def test_get_parent_dir_doubleslash():
    cs = CauSync(config, "/tmp/causync_src", "/tmp/causync_dest", 'sync')

    assert_equals(cs.get_parent_dir('/tmp/causync_src/test//'),
                  '/tmp/causync_src')
    assert_equals(cs.get_parent_dir('/tmp//causync_src//test//'),
                  '/tmp/causync_src')
    assert_equals(cs.get_parent_dir('/tmp///causync_src/test'),
                  '/tmp/causync_src')
    assert_equals(cs.get_parent_dir('/tmp////causync_src/test'),
                  '/tmp/causync_src')
Esempio n. 14
0
def test_get_basename_root2():
    cs = CauSync(config, "/tmp/causync_src", "/tmp/causync_dest", 'sync')
    cs.get_basename('//')
Esempio n. 15
0
def test_get_basename():
    cs = CauSync(config, "/tmp/causync_src", "/tmp/causync_dest", 'sync')
    assert_equals(cs.get_basename('/tmp/causync_src/test'), 'test')
    assert_equals(cs.get_basename('/tmp'), 'tmp')
Esempio n. 16
0
def test_get_parent_dir_toomuchroot2():
    cs = CauSync(config, "/tmp/causync_src", "/tmp/causync_dest", 'sync')
    cs.get_parent_dir('//')