def test_syncer_delete(temp_data_dirs): """Check that deletion on remote storage works""" tmp_source, tmp_target = temp_data_dirs syncer = _DefaultSyncer(sync_period=60) # Populate remote directory syncer.sync_up(local_dir=tmp_source, remote_dir="memory:///test/test_syncer_delete") syncer.wait() syncer.delete(remote_dir="memory:///test/test_syncer_delete") syncer.sync_down( remote_dir="memory:///test/test_syncer_delete", local_dir=tmp_target ) with pytest.raises(TuneError): syncer.wait() # Remote storage was deleted, so target should be empty assert_file(False, tmp_target, "level0.txt") assert_file(False, tmp_target, "level0_exclude.txt") assert_file(False, tmp_target, "subdir/level1.txt") assert_file(False, tmp_target, "subdir/level1_exclude.txt") assert_file(False, tmp_target, "subdir/nested/level2.txt") assert_file(False, tmp_target, "subdir_nested_level2_exclude.txt") assert_file(False, tmp_target, "subdir_exclude/something/somewhere.txt")
def test_sync_down_if_needed(temp_data_dirs): """Check that we only sync down again after sync period""" tmp_source, tmp_target = temp_data_dirs with freeze_time() as frozen: syncer = _DefaultSyncer(sync_period=60) # Populate remote directory syncer.sync_up( local_dir=tmp_source, remote_dir="memory:///test/test_sync_down_if_needed" ) syncer.wait() assert syncer.sync_down_if_needed( remote_dir="memory:///test/test_sync_down_if_needed", local_dir=tmp_target ) syncer.wait() frozen.tick(30) # Sync period not over, yet assert not syncer.sync_down_if_needed( remote_dir="memory:///test/test_sync_down_if_needed", local_dir=tmp_target ) frozen.tick(30) # Sync period over, sync again assert syncer.sync_down_if_needed( remote_dir="memory:///test/test_sync_down_if_needed", local_dir=tmp_target )
def test_syncer_sync_exclude(temp_data_dirs): """Check that the exclude parameter works""" tmp_source, tmp_target = temp_data_dirs syncer = _DefaultSyncer() syncer.sync_up( local_dir=tmp_source, remote_dir="memory:///test/test_syncer_sync_exclude", exclude=["*_exclude*"], ) syncer.wait() syncer.sync_down( remote_dir="memory:///test/test_syncer_sync_exclude", local_dir=tmp_target ) syncer.wait() # Excluded files should not be found in target assert_file(True, tmp_target, "level0.txt") assert_file(False, tmp_target, "level0_exclude.txt") assert_file(True, tmp_target, "subdir/level1.txt") assert_file(False, tmp_target, "subdir/level1_exclude.txt") assert_file(True, tmp_target, "subdir/nested/level2.txt") assert_file(False, tmp_target, "subdir_nested_level2_exclude.txt") assert_file(False, tmp_target, "subdir_exclude/something/somewhere.txt")
def test_syncer_wait_or_retry(temp_data_dirs): """Check that the wait or retry API works""" tmp_source, tmp_target = temp_data_dirs syncer = _DefaultSyncer(sync_period=60) # Will fail as dir does not exist syncer.sync_down( remote_dir="memory:///test/test_syncer_wait_or_retry", local_dir=tmp_target ) with pytest.raises(TuneError) as e: syncer.wait_or_retry(max_retries=3, backoff_s=0) assert "Failed sync even after 3 retries." in str(e)
def test_syncer_still_running_no_sync(temp_data_dirs): """Check that no new sync is issued if old sync is still running""" tmp_source, tmp_target = temp_data_dirs class FakeSyncProcess: @property def is_running(self): return True syncer = _DefaultSyncer(sync_period=60) syncer._sync_process = FakeSyncProcess() assert not syncer.sync_up_if_needed( local_dir=tmp_source, remote_dir="memory:///test/test_syncer_still_running_no_sync", )
def test_syncer_not_running_sync_last_failed(caplog, temp_data_dirs): """Check that new sync is issued if old sync completed""" caplog.set_level(logging.WARNING) tmp_source, tmp_target = temp_data_dirs class FakeSyncProcess: @property def is_running(self): return False def wait(self): raise RuntimeError("Sync failed") syncer = _DefaultSyncer(sync_period=60) syncer._sync_process = FakeSyncProcess() assert syncer.sync_up_if_needed( local_dir=tmp_source, remote_dir="memory:///test/test_syncer_not_running_sync", ) assert "Last sync command failed" in caplog.text
def test_syncer_sync_up_down(temp_data_dirs): """Check that syncing up and down works""" tmp_source, tmp_target = temp_data_dirs syncer = _DefaultSyncer() syncer.sync_up( local_dir=tmp_source, remote_dir="memory:///test/test_syncer_sync_up_down" ) syncer.wait() syncer.sync_down( remote_dir="memory:///test/test_syncer_sync_up_down", local_dir=tmp_target ) syncer.wait() # Target dir should have all files assert_file(True, tmp_target, "level0.txt") assert_file(True, tmp_target, "level0_exclude.txt") assert_file(True, tmp_target, "subdir/level1.txt") assert_file(True, tmp_target, "subdir/level1_exclude.txt") assert_file(True, tmp_target, "subdir/nested/level2.txt") assert_file(True, tmp_target, "subdir_nested_level2_exclude.txt") assert_file(True, tmp_target, "subdir_exclude/something/somewhere.txt")