Exemple #1
0
def test_file_extract():
    tar_path = "tmp/Example.tar.gz"
    folder_path = "tmp/example_tar"

    # Generate package
    subprocess.call(["rm", "-rf", "./tmp"])
    subprocess.call(["mkdir", "-p", "{}/directory".format(folder_path)])
    subprocess.call(["mkdir", "-p", "{}/symlinks".format(folder_path)])
    p = Path("{}/directory/file".format(folder_path))
    p.write_text("Example test")
    os.symlink("../directory/file",
               "{}/symlinks/file_link".format(folder_path))
    os.symlink("../directory/",
               "{}/symlinks/directory_link".format(folder_path))
    if os.path.exists(tar_path):
        os.remove(tar_path)
    subprocess.call(["tar", "-czvf", tar_path, folder_path])

    try:
        tar = tarfile.open(tar_path, "r")
        fs = FsMongo()
        fs.fs = FakeFS()
        fs.file_extract(tar_object=tar, path=".")
    finally:
        os.remove(tar_path)
        subprocess.call(["rm", "-rf", "./tmp"])
Exemple #2
0
def test_fs_connect_with_valid_config(config, exp_logger, exp_path):
    fs = FsMongo()
    fs.fs_connect(config)
    assert fs.logger == logging.getLogger(exp_logger)
    assert fs.path == exp_path
    assert type(fs.client) == MongoClient
    assert type(fs.fs) == GridFSBucket
Exemple #3
0
def test_fs_connect_with_invalid_mongoclient(config, exp_exception_message,
                                             monkeypatch):
    def generate_exception(a, b, c=None):
        raise Exception(exp_exception_message)

    monkeypatch.setattr(MongoClient, '__init__', generate_exception)

    fs = FsMongo()
    with pytest.raises(FsException) as excinfo:
        fs.fs_connect(config)
    assert str(
        excinfo.value) == generic_fs_exception_message(exp_exception_message)
Exemple #4
0
def test_constructor_with_logger():
    logger_name = 'fs_mongo'
    fs = FsMongo(logger_name=logger_name)
    assert fs.logger == logging.getLogger(logger_name)
    assert fs.path is None
    assert fs.client is None
    assert fs.fs is None
Exemple #5
0
def test_fs_connect_with_invalid_gridfsbucket(config, exp_exception_message,
                                              monkeypatch):
    def mock_mongoclient_constructor(a, b, c=None):
        pass

    def mock_mongoclient_getitem(a, b):
        pass

    def generate_exception(a, b):
        raise Exception(exp_exception_message)

    monkeypatch.setattr(MongoClient, '__init__', mock_mongoclient_constructor)
    monkeypatch.setattr(MongoClient, '__getitem__', mock_mongoclient_getitem)
    monkeypatch.setattr(GridFSBucket, '__init__', generate_exception)

    fs = FsMongo()
    with pytest.raises(FsException) as excinfo:
        fs.fs_connect(config)
    assert str(
        excinfo.value) == generic_fs_exception_message(exp_exception_message)
Exemple #6
0
def fs_mongo(request, monkeypatch):
    def mock_mongoclient_constructor(a, b, c):
        pass

    def mock_mongoclient_getitem(a, b):
        pass

    def mock_gridfs_constructor(a, b):
        pass

    monkeypatch.setattr(MongoClient, '__init__', mock_mongoclient_constructor)
    monkeypatch.setattr(MongoClient, '__getitem__', mock_mongoclient_getitem)
    monkeypatch.setattr(GridFSBucket, '__init__', mock_gridfs_constructor)
    fs = FsMongo(lock=request.param)
    fs.fs_connect({
        'path': valid_path(),
        'host': 'mongo',
        'port': 27017,
        'collection': 'files'
    })
    return fs
Exemple #7
0
def test_upload_local_fs():
    path = "./tmp/"

    subprocess.call(["rm", "-rf", path])
    try:
        fs = FsMongo()
        fs.path = path
        fs.fs = FakeFS()
        fs.sync()
        assert os.path.isdir("{}example_tar".format(path))
        assert os.path.isdir("{}example_tar/directory".format(path))
        assert os.path.isdir("{}example_tar/symlinks".format(path))
        assert os.path.isfile("{}example_tar/directory/file".format(path))
        assert os.path.islink("{}example_tar/symlinks/file_link".format(path))
        assert os.path.islink(
            "{}example_tar/symlinks/directory_link".format(path))
    finally:
        subprocess.call(["rm", "-rf", path])
Exemple #8
0
def test_upload_mongo_fs():
    path = "./tmp/"

    subprocess.call(["rm", "-rf", path])
    try:
        fs = FsMongo()
        fs.path = path
        fs.fs = Mock()
        fs.fs.find.return_value = {}

        file_content = "Test file content"

        # Create local dir and upload content to fakefs
        os.mkdir(path)
        os.mkdir("{}example_local".format(path))
        os.mkdir("{}example_local/directory".format(path))
        with open("{}example_local/directory/test_file".format(path),
                  "w+") as test_file:
            test_file.write(file_content)
        fs.reverse_sync("example_local")

        assert fs.fs.upload_from_stream.call_count == 2

        # first call to upload_from_stream, dir_name
        dir_name = "example_local/directory"
        call_args_0 = fs.fs.upload_from_stream.call_args_list[0]
        assert call_args_0[0][0] == dir_name
        assert call_args_0[1].get("metadata").get("type") == "dir"

        # second call to upload_from_stream, dir_name
        file_name = "example_local/directory/test_file"
        call_args_1 = fs.fs.upload_from_stream.call_args_list[1]
        assert call_args_1[0][0] == file_name
        assert call_args_1[1].get("metadata").get("type") == "file"

    finally:
        subprocess.call(["rm", "-rf", path])
        pass
Exemple #9
0
def test_fs_connect_with_missing_parameters(config, exp_exception_message):
    fs = FsMongo()
    with pytest.raises(FsException) as excinfo:
        fs.fs_connect(config)
    assert str(
        excinfo.value) == generic_fs_exception_message(exp_exception_message)
Exemple #10
0
def test_fs_connect_with_invalid_path(config, exp_exception_message):
    fs = FsMongo()
    with pytest.raises(FsException) as excinfo:
        fs.fs_connect(config)
    assert str(excinfo.value) == exp_exception_message
Exemple #11
0
def test_constructor_without_logger():
    fs = FsMongo()
    assert fs.logger == logging.getLogger('fs')
    assert fs.path is None
    assert fs.client is None
    assert fs.fs is None