File: test_jobs.py

package info (click to toggle)
python-gitlab 1%3A4.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,048 kB
  • sloc: python: 24,168; makefile: 171; ruby: 27; javascript: 3
file content (170 lines) | stat: -rw-r--r-- 5,248 bytes parent folder | download
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""
GitLab API: https://docs.gitlab.com/ee/api/jobs.html
"""

from functools import partial

import pytest
import responses

from gitlab.v4.objects import ProjectJob

failed_job_content = {
    "commit": {
        "author_email": "admin@example.com",
        "author_name": "Administrator",
    },
    "coverage": None,
    "allow_failure": False,
    "created_at": "2015-12-24T15:51:21.880Z",
    "started_at": "2015-12-24T17:54:30.733Z",
    "finished_at": "2015-12-24T17:54:31.198Z",
    "duration": 0.465,
    "queued_duration": 0.010,
    "artifacts_expire_at": "2016-01-23T17:54:31.198Z",
    "tag_list": ["docker runner", "macos-10.15"],
    "id": 1,
    "name": "rubocop",
    "pipeline": {
        "id": 1,
        "project_id": 1,
    },
    "ref": "main",
    "artifacts": [],
    "runner": None,
    "stage": "test",
    "status": "failed",
    "tag": False,
    "web_url": "https://example.com/foo/bar/-/jobs/1",
    "user": {"id": 1},
}

success_job_content = {
    **failed_job_content,
    "status": "success",
    "id": failed_job_content["id"] + 1,
}


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


@pytest.fixture
def resp_cancel_job():
    with responses.RequestsMock() as rsps:
        rsps.add(
            method=responses.POST,
            url="http://localhost/api/v4/projects/1/jobs/1/cancel",
            json=failed_job_content,
            content_type="application/json",
            status=201,
        )
        yield rsps


@pytest.fixture
def resp_retry_job():
    with responses.RequestsMock() as rsps:
        rsps.add(
            method=responses.POST,
            url="http://localhost/api/v4/projects/1/jobs/1/retry",
            json=failed_job_content,
            content_type="application/json",
            status=201,
        )
        yield rsps


@pytest.fixture
def resp_list_job():
    urls = [
        "http://localhost/api/v4/projects/1/jobs",
        "http://localhost/api/v4/projects/1/pipelines/1/jobs",
    ]
    with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
        register_endpoint = partial(
            rsps.add,
            method=responses.GET,
            content_type="application/json",
            status=200,
        )
        for url in urls:
            register_endpoint(
                url=url,
                json=[failed_job_content],
                match=[responses.matchers.query_param_matcher({"scope[]": "failed"})],
            )
            register_endpoint(
                url=url,
                json=[success_job_content],
                match=[responses.matchers.query_param_matcher({"scope[]": "success"})],
            )
            register_endpoint(
                url=url,
                json=[success_job_content, failed_job_content],
                match=[
                    responses.matchers.query_string_matcher(
                        "scope[]=success&scope[]failed"
                    )
                ],
            )
            register_endpoint(
                url=url,
                json=[success_job_content, failed_job_content],
            )
        yield rsps


def test_get_project_job(project, resp_get_job):
    job = project.jobs.get(1)
    assert isinstance(job, ProjectJob)
    assert job.ref == "main"


def test_cancel_project_job(project, resp_cancel_job):
    job = project.jobs.get(1, lazy=True)

    output = job.cancel()
    assert output["ref"] == "main"


def test_retry_project_job(project, resp_retry_job):
    job = project.jobs.get(1, lazy=True)

    output = job.retry()
    assert output["ref"] == "main"


def test_list_project_job(project, resp_list_job):
    failed_jobs = project.jobs.list(scope="failed")
    success_jobs = project.jobs.list(scope="success")
    failed_and_success_jobs = project.jobs.list(scope=["failed", "success"])
    pipeline_lazy = project.pipelines.get(1, lazy=True)
    pjobs_failed = pipeline_lazy.jobs.list(scope="failed")
    pjobs_success = pipeline_lazy.jobs.list(scope="success")
    pjobs_failed_and_success = pipeline_lazy.jobs.list(scope=["failed", "success"])

    prepared_urls = [c.request.url for c in resp_list_job.calls]

    # Both pipelines and pipelines/jobs should behave the same way
    # When `scope` is scalar, one can use scope=value or scope[]=value
    assert set(failed_and_success_jobs) == set(failed_jobs + success_jobs)
    assert set(pjobs_failed_and_success) == set(pjobs_failed + pjobs_success)
    assert prepared_urls == [
        "http://localhost/api/v4/projects/1/jobs?scope%5B%5D=failed",
        "http://localhost/api/v4/projects/1/jobs?scope%5B%5D=success",
        "http://localhost/api/v4/projects/1/jobs?scope%5B%5D=failed&scope%5B%5D=success",
        "http://localhost/api/v4/projects/1/pipelines/1/jobs?scope%5B%5D=failed",
        "http://localhost/api/v4/projects/1/pipelines/1/jobs?scope%5B%5D=success",
        "http://localhost/api/v4/projects/1/pipelines/1/jobs?scope%5B%5D=failed&scope%5B%5D=success",
    ]