summaryrefslogtreecommitdiff
path: root/tests/unit/objects/test_ci_lint.py
blob: 76281f1e29332dcd9a92063b13ae0053f39e3fb8 (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
import pytest
import responses

from gitlab import exceptions

ci_lint_create_content = {"status": "valid", "errors": [], "warnings": []}
ci_lint_create_invalid_content = {
    "status": "invalid",
    "errors": ["invalid format"],
    "warnings": [],
}


project_ci_lint_content = {
    "valid": True,
    "merged_yaml": "---\n:test_job:\n  :script: echo 1\n",
    "errors": [],
    "warnings": [],
}


@pytest.fixture
def resp_create_ci_lint():
    with responses.RequestsMock() as rsps:
        rsps.add(
            method=responses.POST,
            url="http://localhost/api/v4/ci/lint",
            json=ci_lint_create_content,
            content_type="application/json",
            status=200,
        )
        yield rsps


@pytest.fixture
def resp_create_ci_lint_invalid():
    with responses.RequestsMock() as rsps:
        rsps.add(
            method=responses.POST,
            url="http://localhost/api/v4/ci/lint",
            json=ci_lint_create_invalid_content,
            content_type="application/json",
            status=200,
        )
        yield rsps


@pytest.fixture
def resp_get_project_ci_lint():
    with responses.RequestsMock() as rsps:
        rsps.add(
            method=responses.GET,
            url="http://localhost/api/v4/projects/1/ci/lint",
            json=project_ci_lint_content,
            content_type="application/json",
            status=200,
        )
        yield rsps


@pytest.fixture
def resp_create_project_ci_lint():
    with responses.RequestsMock() as rsps:
        rsps.add(
            method=responses.POST,
            url="http://localhost/api/v4/projects/1/ci/lint",
            json=project_ci_lint_content,
            content_type="application/json",
            status=200,
        )
        yield rsps


def test_ci_lint_create(gl, resp_create_ci_lint, valid_gitlab_ci_yml):
    lint_result = gl.ci_lint.create({"content": valid_gitlab_ci_yml})
    assert lint_result.status == "valid"


def test_ci_lint_validate(gl, resp_create_ci_lint, valid_gitlab_ci_yml):
    gl.ci_lint.validate({"content": valid_gitlab_ci_yml})


def test_ci_lint_validate_invalid_raises(
    gl, resp_create_ci_lint_invalid, invalid_gitlab_ci_yml
):
    with pytest.raises(exceptions.GitlabCiLintError, match="invalid format"):
        gl.ci_lint.validate({"content": invalid_gitlab_ci_yml})


def test_project_ci_lint_get(project, resp_get_project_ci_lint):
    lint_result = project.ci_lint.get()
    assert lint_result.valid is True


def test_project_ci_lint_create(
    project, resp_create_project_ci_lint, valid_gitlab_ci_yml
):
    lint_result = project.ci_lint.create({"content": valid_gitlab_ci_yml})
    assert lint_result.valid is True