2
2
GitLab API: https://docs.gitlab.com/ee/api/jobs.html
3
3
"""
4
4
5
+ from functools import partial
6
+
5
7
import pytest
6
8
import responses
7
9
8
10
from gitlab .v4 .objects import ProjectJob
9
11
10
- job_content = {
12
+ failed_job_content = {
11
13
"commit" : {
12
14
"author_email" : "admin@example.com" ,
13
15
"author_name" : "Administrator" ,
37
39
"user" : {"id" : 1 },
38
40
}
39
41
42
+ success_job_content = {
43
+ ** failed_job_content ,
44
+ "status" : "success" ,
45
+ "id" : failed_job_content ["id" ] + 1 ,
46
+ }
47
+
40
48
41
49
@pytest .fixture
42
50
def resp_get_job ():
43
51
with responses .RequestsMock () as rsps :
44
52
rsps .add (
45
53
method = responses .GET ,
46
54
url = "http://localhost/api/v4/projects/1/jobs/1" ,
47
- json = job_content ,
55
+ json = failed_job_content ,
48
56
content_type = "application/json" ,
49
57
status = 200 ,
50
58
)
@@ -57,7 +65,7 @@ def resp_cancel_job():
57
65
rsps .add (
58
66
method = responses .POST ,
59
67
url = "http://localhost/api/v4/projects/1/jobs/1/cancel" ,
60
- json = job_content ,
68
+ json = failed_job_content ,
61
69
content_type = "application/json" ,
62
70
status = 201 ,
63
71
)
@@ -70,13 +78,53 @@ def resp_retry_job():
70
78
rsps .add (
71
79
method = responses .POST ,
72
80
url = "http://localhost/api/v4/projects/1/jobs/1/retry" ,
73
- json = job_content ,
81
+ json = failed_job_content ,
74
82
content_type = "application/json" ,
75
83
status = 201 ,
76
84
)
77
85
yield rsps
78
86
79
87
88
+ @pytest .fixture
89
+ def resp_list_job ():
90
+ urls = [
91
+ "http://localhost/api/v4/projects/1/jobs" ,
92
+ "http://localhost/api/v4/projects/1/pipelines/1/jobs" ,
93
+ ]
94
+ with responses .RequestsMock (assert_all_requests_are_fired = False ) as rsps :
95
+ register_endpoint = partial (
96
+ rsps .add ,
97
+ method = responses .GET ,
98
+ content_type = "application/json" ,
99
+ status = 200 ,
100
+ )
101
+ for url in urls :
102
+ register_endpoint (
103
+ url = url ,
104
+ json = [failed_job_content ],
105
+ match = [responses .matchers .query_param_matcher ({"scope[]" : "failed" })],
106
+ )
107
+ register_endpoint (
108
+ url = url ,
109
+ json = [success_job_content ],
110
+ match = [responses .matchers .query_param_matcher ({"scope[]" : "success" })],
111
+ )
112
+ register_endpoint (
113
+ url = url ,
114
+ json = [success_job_content , failed_job_content ],
115
+ match = [
116
+ responses .matchers .query_string_matcher (
117
+ "scope[]=success&scope[]failed"
118
+ )
119
+ ],
120
+ )
121
+ register_endpoint (
122
+ url = url ,
123
+ json = [success_job_content , failed_job_content ],
124
+ )
125
+ yield rsps
126
+
127
+
80
128
def test_get_project_job (project , resp_get_job ):
81
129
job = project .jobs .get (1 )
82
130
assert isinstance (job , ProjectJob )
@@ -95,3 +143,28 @@ def test_retry_project_job(project, resp_retry_job):
95
143
96
144
output = job .retry ()
97
145
assert output ["ref" ] == "main"
146
+
147
+
148
+ def test_list_project_job (project , resp_list_job ):
149
+ failed_jobs = project .jobs .list (scope = "failed" )
150
+ success_jobs = project .jobs .list (scope = "success" )
151
+ failed_and_success_jobs = project .jobs .list (scope = ["failed" , "success" ])
152
+ pipeline_lazy = project .pipelines .get (1 , lazy = True )
153
+ pjobs_failed = pipeline_lazy .jobs .list (scope = "failed" )
154
+ pjobs_success = pipeline_lazy .jobs .list (scope = "success" )
155
+ pjobs_failed_and_success = pipeline_lazy .jobs .list (scope = ["failed" , "success" ])
156
+
157
+ prepared_urls = [c .request .url for c in resp_list_job .calls ]
158
+
159
+ # Both pipelines and pipelines/jobs should behave the same way
160
+ # When `scope` is scalar, one can use scope=value or scope[]=value
161
+ assert set (failed_and_success_jobs ) == set (failed_jobs + success_jobs )
162
+ assert set (pjobs_failed_and_success ) == set (pjobs_failed + pjobs_success )
163
+ assert prepared_urls == [
164
+ "http://localhost/api/v4/projects/1/jobs?scope%5B%5D=failed" ,
165
+ "http://localhost/api/v4/projects/1/jobs?scope%5B%5D=success" ,
166
+ "http://localhost/api/v4/projects/1/jobs?scope%5B%5D=failed&scope%5B%5D=success" ,
167
+ "http://localhost/api/v4/projects/1/pipelines/1/jobs?scope%5B%5D=failed" ,
168
+ "http://localhost/api/v4/projects/1/pipelines/1/jobs?scope%5B%5D=success" ,
169
+ "http://localhost/api/v4/projects/1/pipelines/1/jobs?scope%5B%5D=failed&scope%5B%5D=success" ,
170
+ ]
0 commit comments