opts = mock.MagicMock()
    opts.check_file_md5 = True
    opts.chunk_size_bytes = 16
    ase = azmodels.StorageEntity('cont')
    ase._size = 1024
    ase._vio = mock.MagicMock()
    ase._vio.slice_id = 0
    ase._vio.total_size = 1024
    d = models.Descriptor(lp, ase, opts, mock.MagicMock(), None)

    total_size = d._set_final_path_view()
    assert total_size == ase._size


@unittest.skipIf(util.on_python2() or util.on_windows(),
                 'fallocate does not exist')
def test_downloaddescriptor_allocate_disk_space_via_seek(tmpdir):
    fp = pathlib.Path(str(tmpdir.join('fp')))
    opts = mock.MagicMock()
    opts.check_file_md5 = True
    opts.chunk_size_bytes = 256
    ase = azmodels.StorageEntity('cont')
    ase._size = 128
    ase._name = 'blob'
    d = models.Descriptor(fp, ase, opts, mock.MagicMock(), None)

    with mock.patch('os.posix_fallocate') as patched_fallocate:
        patched_fallocate.side_effect = [AttributeError()]
        d._allocate_disk_space()
        assert d._allocated
    opts = mock.MagicMock()
    opts.check_file_md5 = True
    opts.chunk_size_bytes = 16
    ase = azmodels.StorageEntity('cont')
    ase._size = 1024
    ase._vio = mock.MagicMock()
    ase._vio.slice_id = 0
    ase._vio.total_size = 1024
    d = models.Descriptor(lp, ase, opts, mock.MagicMock(), None)

    total_size = d._set_final_path_view()
    assert total_size == ase._size


@unittest.skipIf(util.on_python2() or not util.on_linux(),
                 'fallocate does not exist')
def test_downloaddescriptor_allocate_disk_space_via_seek(tmpdir):
    fp = pathlib.Path(str(tmpdir.join('fp')))
    opts = mock.MagicMock()
    opts.check_file_md5 = True
    opts.chunk_size_bytes = 256
    ase = azmodels.StorageEntity('cont')
    ase._size = 128
    ase._name = 'blob'
    d = models.Descriptor(fp, ase, opts, mock.MagicMock(), None)

    with mock.patch('os.posix_fallocate') as patched_fallocate:
        patched_fallocate.side_effect = [AttributeError()]
        d._allocate_disk_space()
        assert d._allocated
def test_download_resume_manager(tmpdir):
    tmpdb = pathlib.Path(str(tmpdir.join('tmp.db')))
    tmpdb_dat = pathlib.Path(str(tmpdir.join('tmp.db.dat')))

    drm = ops.DownloadResumeManager(tmpdb)
    assert drm._data is not None
    drm.close()
    assert drm._data is None
    if not util.on_python2() and util.on_windows():
        assert tmpdb_dat.exists()
    else:
        assert tmpdb.exists()
    drm.delete()
    assert drm._data is None
    if not util.on_python2() and util.on_windows():
        assert not tmpdb_dat.exists()
    else:
        assert not tmpdb.exists()

    ase = mock.MagicMock()
    ase._name = 'name'
    ase._client.primary_endpoint = 'ep'
    ase._size = 16

    final_path = 'fp'
    drm = ops.DownloadResumeManager(tmpdb)
    drm.add_or_update_record(final_path, ase, 2, 0, False, None)
    d = drm.get_record(ase)

    assert d.final_path == final_path

    drm.add_or_update_record(final_path, ase, 2, 1, False, 'abc')
    d = drm.get_record(ase)

    assert d.final_path == final_path
    assert not d.completed
    assert d.next_integrity_chunk == 1
    assert d.md5hexdigest == 'abc'

    drm.add_or_update_record(final_path, ase, 2, 1, True, None)
    d = drm.get_record(ase)

    assert d.final_path == final_path
    assert d.completed
    assert d.next_integrity_chunk == 1
    assert d.md5hexdigest == 'abc'

    # idempotent check after completed
    drm.add_or_update_record(final_path, ase, 2, 1, True, None)
    d = drm.get_record(ase)

    assert d.final_path == final_path
    assert d.completed
    assert d.next_integrity_chunk == 1
    assert d.md5hexdigest == 'abc'

    drm.close()
    assert drm._data is None
    if not util.on_python2() and util.on_windows():
        assert tmpdb_dat.exists()
    else:
        assert tmpdb.exists()

    drm.delete()
    assert drm._data is None
    if not util.on_python2() and util.on_windows():
        assert not tmpdb_dat.exists()
    else:
        assert not tmpdb.exists()

    # oserror path
    with mock.patch('blobxfer.util.on_windows', return_value=False):
        drm.delete()
    assert drm._data is None

    # oserror path
    with mock.patch('blobxfer.util.on_windows', return_value=True):
        with mock.patch('blobxfer.util.on_python2', return_value=False):
            drm.delete()
    assert drm._data is None
def test_synccopy_resume_manager(tmpdir):
    tmpdb = pathlib.Path(str(tmpdir.join('tmp.db')))
    tmpdb_dat = pathlib.Path(str(tmpdir.join('tmp.db.dat')))

    srm = ops.SyncCopyResumeManager(tmpdb)
    assert srm._data is not None
    srm.close()
    assert srm._data is None
    if not util.on_python2() and util.on_windows():
        assert tmpdb_dat.exists()
    else:
        assert tmpdb.exists()
    srm.delete()
    assert srm._data is None
    if not util.on_python2() and util.on_windows():
        assert not tmpdb_dat.exists()
    else:
        assert not tmpdb.exists()

    ase = mock.MagicMock()
    ase._name = 'name'
    ase._client.primary_endpoint = 'ep'
    ase._size = 16

    src_block_list = 'srcbl'

    srm = ops.SyncCopyResumeManager(tmpdb)
    srm.add_or_update_record(ase, src_block_list, 0, 2, 8, 0, False)
    s = srm.get_record(ase)

    assert s.src_block_list == src_block_list
    assert s.length == ase._size
    assert s.offset == 0
    assert s.chunk_size == 2
    assert s.total_chunks == 8
    assert s.completed_chunks == 0
    assert not s.completed

    srm.add_or_update_record(ase, src_block_list, 1, 2, 8, 1, False)
    s = srm.get_record(ase)

    assert s.src_block_list == src_block_list
    assert s.length == ase._size
    assert s.offset == 1
    assert s.chunk_size == 2
    assert s.total_chunks == 8
    assert s.completed_chunks == 1
    assert not s.completed

    srm.add_or_update_record(ase, src_block_list, 8, 2, 8, 8, True)
    s = srm.get_record(ase)

    assert s.src_block_list == src_block_list
    assert s.length == ase._size
    assert s.offset == 8
    assert s.chunk_size == 2
    assert s.total_chunks == 8
    assert s.completed_chunks == 8
    assert s.completed

    # idempotent check after completed
    srm.add_or_update_record(ase, src_block_list, 8, 2, 8, 8, True)
    s = srm.get_record(ase)

    assert s.src_block_list == src_block_list
    assert s.length == ase._size
    assert s.offset == 8
    assert s.chunk_size == 2
    assert s.total_chunks == 8
    assert s.completed_chunks == 8
    assert s.completed

    srm.close()
    assert srm._data is None
    if not util.on_python2() and util.on_windows():
        assert tmpdb_dat.exists()
    else:
        assert tmpdb.exists()

    srm.delete()
    assert srm._data is None
    if not util.on_python2() and util.on_windows():
        assert not tmpdb_dat.exists()
    else:
        assert not tmpdb.exists()
def test_upload_resume_manager(tmpdir):
    tmpdb = pathlib.Path(str(tmpdir.join('tmp.db')))
    tmpdb_dat = pathlib.Path(str(tmpdir.join('tmp.db.dat')))

    urm = ops.UploadResumeManager(tmpdb)
    assert urm._data is not None
    urm.close()
    assert urm._data is None
    if not util.on_python2() and util.on_windows():
        assert tmpdb_dat.exists()
    else:
        assert tmpdb.exists()
    urm.delete()
    assert urm._data is None
    if not util.on_python2() and util.on_windows():
        assert not tmpdb_dat.exists()
    else:
        assert not tmpdb.exists()

    ase = mock.MagicMock()
    ase._name = 'name'
    ase._client.primary_endpoint = 'ep'
    ase._size = 16

    local_path = 'fp'
    urm = ops.UploadResumeManager(tmpdb)
    urm.add_or_update_record(local_path, ase, 2, 8, 0, False, None)
    u = urm.get_record(ase)

    assert u.local_path == local_path
    assert u.length == ase._size
    assert u.chunk_size == 2
    assert u.total_chunks == 8
    assert u.completed_chunks == 0
    assert not u.completed

    urm.add_or_update_record(local_path, ase, 2, 8, 1, False, 'abc')
    u = urm.get_record(ase)

    assert u.local_path == local_path
    assert u.length == ase._size
    assert u.chunk_size == 2
    assert u.total_chunks == 8
    assert u.completed_chunks == 1
    assert not u.completed
    assert u.md5hexdigest == 'abc'

    urm.add_or_update_record(local_path, ase, 2, 8, 8, True, None)
    u = urm.get_record(ase)

    assert u.local_path == local_path
    assert u.length == ase._size
    assert u.chunk_size == 2
    assert u.total_chunks == 8
    assert u.completed_chunks == 8
    assert u.completed
    assert u.md5hexdigest == 'abc'

    # idempotent check after completed
    urm.add_or_update_record(local_path, ase, 2, 8, 8, True, None)
    u = urm.get_record(ase)

    assert u.local_path == local_path
    assert u.length == ase._size
    assert u.chunk_size == 2
    assert u.total_chunks == 8
    assert u.completed_chunks == 8
    assert u.completed
    assert u.md5hexdigest == 'abc'

    urm.close()
    assert urm._data is None
    if not util.on_python2() and util.on_windows():
        assert tmpdb_dat.exists()
    else:
        assert tmpdb.exists()

    urm.delete()
    assert urm._data is None
    if not util.on_python2() and util.on_windows():
        assert not tmpdb_dat.exists()
    else:
        assert not tmpdb.exists()
    opts = mock.MagicMock()
    opts.check_file_md5 = True
    opts.chunk_size_bytes = 16
    ase = azmodels.StorageEntity('cont')
    ase._size = 1024
    ase._vio = mock.MagicMock()
    ase._vio.slice_id = 0
    ase._vio.total_size = 1024
    d = models.Descriptor(lp, ase, opts, mock.MagicMock(), None)

    total_size = d._set_final_path_view()
    assert total_size == ase._size


@unittest.skipIf(
    util.on_python2() or util.on_windows(), 'fallocate does not exist')
def test_downloaddescriptor_allocate_disk_space_via_seek(tmpdir):
    fp = pathlib.Path(str(tmpdir.join('fp')))
    opts = mock.MagicMock()
    opts.check_file_md5 = True
    opts.chunk_size_bytes = 256
    ase = azmodels.StorageEntity('cont')
    ase._size = 128
    ase._name = 'blob'
    d = models.Descriptor(fp, ase, opts, mock.MagicMock(), None)

    with mock.patch('os.posix_fallocate') as patched_fallocate:
        patched_fallocate.side_effect = [AttributeError()]
        d._allocate_disk_space()
        assert d._allocated
        assert fp.exists()