summaryrefslogtreecommitdiff
path: root/tests/functional/api/test_import_export.py
blob: 8f9db9c60b6c74078580031bdbb0621ee5130d10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import time

import pytest

import gitlab


def test_group_import_export(gl, group, temp_dir):
    export = group.exports.create()
    assert export.message == "202 Accepted"

    # We cannot check for export_status with group export API
    time.sleep(10)

    import_archive = temp_dir / "gitlab-group-export.tgz"
    import_path = "imported_group"
    import_name = "Imported Group"

    with open(import_archive, "wb") as f:
        export.download(streamed=True, action=f.write)

    with open(import_archive, "rb") as f:
        output = gl.groups.import_group(f, import_path, import_name)
    assert output["message"] == "202 Accepted"

    # We cannot check for returned ID with group import API
    time.sleep(10)
    group_import = gl.groups.get(import_path)

    assert group_import.path == import_path
    assert group_import.name == import_name


def test_project_import_export(gl, project, temp_dir):
    export = project.exports.create()
    assert export.message == "202 Accepted"

    export = project.exports.get()
    assert isinstance(export, gitlab.v4.objects.ProjectExport)

    count = 0
    while export.export_status != "finished":
        time.sleep(1)
        export.refresh()
        count += 1
        if count == 15:
            raise Exception("Project export taking too much time")

    with open(temp_dir / "gitlab-export.tgz", "wb") as f:
        export.download(streamed=True, action=f.write)  # type: ignore[arg-type]

    output = gl.projects.import_project(
        open(temp_dir / "gitlab-export.tgz", "rb"),
        "imported_project",
        name="Imported Project",
    )
    project_import = gl.projects.get(output["id"], lazy=True).imports.get()

    assert project_import.path == "imported_project"
    assert project_import.name == "Imported Project"

    count = 0
    while project_import.import_status != "finished":
        time.sleep(1)
        project_import.refresh()
        count += 1
        if count == 15:
            raise Exception("Project import taking too much time")


def test_project_remote_import(gl):
    with pytest.raises(gitlab.exceptions.GitlabImportError) as err_info:
        gl.projects.remote_import(
            "ftp://whatever.com/url", "remote-project", "remote-project", "root"
        )
    assert err_info.value.response_code == 400
    assert (
        "File url is blocked: Only allowed schemes are https"
        in err_info.value.error_message
    )


def test_project_remote_import_s3(gl):
    gl.features.set("import_project_from_remote_file_s3", True)
    with pytest.raises(gitlab.exceptions.GitlabImportError) as err_info:
        gl.projects.remote_import_s3(
            "remote-project",
            "aws-region",
            "aws-bucket-name",
            "aws-file-key",
            "aws-access-key-id",
            "secret-access-key",
            "remote-project",
            "root",
        )
    assert err_info.value.response_code == 400
    assert (
        "Failed to open 'aws-file-key' in 'aws-bucket-name'"
        in err_info.value.error_message
    )