Skip to content

Commit 746fe81

Browse files
committed
test(runners): add all runners unit tests
1 parent 77fa757 commit 746fe81

File tree

4 files changed

+294
-0
lines changed

4 files changed

+294
-0
lines changed

README.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ Before submitting a pull request make sure that the tests still succeed with
125125
your change. Unit tests and functional tests run using the travis service and
126126
passing tests are mandatory to get merge requests accepted.
127127

128+
We're currently in a restructing phase for the unit tests. If you're changing existing
129+
tests, feel free to keep the current format. Otherwise please write new tests with pytest and
130+
using `responses<https://github.com/getsentry/responses>`_. An example for new tests can be found in
131+
tests/objects/test_runner.py
132+
128133
You need to install ``tox`` to run unit tests and documentation builds locally:
129134

130135
.. code-block:: bash

gitlab/tests/conftest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pytest
2+
import gitlab
3+
4+
5+
@pytest.fixture
6+
def gl():
7+
return gitlab.Gitlab(
8+
"http://localhost",
9+
private_token="private_token",
10+
ssl_verify=True,
11+
api_version=4,
12+
)

gitlab/tests/objects/test_runners.py

Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
import unittest
2+
import responses
3+
import gitlab
4+
import pytest
5+
import re
6+
from .mocks import * # noqa
7+
8+
9+
runner_detail = {
10+
"active": True,
11+
"architecture": "amd64",
12+
"description": "test-1-20150125",
13+
"id": 6,
14+
"ip_address": "127.0.0.1",
15+
"is_shared": False,
16+
"contacted_at": "2016-01-25T16:39:48.066Z",
17+
"name": "test-runner",
18+
"online": True,
19+
"status": "online",
20+
"platform": "linux",
21+
"projects": [
22+
{
23+
"id": 1,
24+
"name": "GitLab Community Edition",
25+
"name_with_namespace": "GitLab.org / GitLab Community Edition",
26+
"path": "gitlab-foss",
27+
"path_with_namespace": "gitlab-org/gitlab-foss",
28+
}
29+
],
30+
"revision": "5nj35",
31+
"tag_list": ["ruby", "mysql"],
32+
"version": "v13.0.0",
33+
"access_level": "ref_protected",
34+
"maximum_timeout": 3600,
35+
}
36+
37+
runner_shortinfo = {
38+
"active": True,
39+
"description": "test-1-20150125",
40+
"id": 6,
41+
"is_shared": False,
42+
"ip_address": "127.0.0.1",
43+
"name": "test-name",
44+
"online": True,
45+
"status": "online",
46+
}
47+
48+
runner_jobs = [
49+
{
50+
"id": 6,
51+
"ip_address": "127.0.0.1",
52+
"status": "running",
53+
"stage": "test",
54+
"name": "test",
55+
"ref": "master",
56+
"tag": False,
57+
"coverage": "99%",
58+
"created_at": "2017-11-16T08:50:29.000Z",
59+
"started_at": "2017-11-16T08:51:29.000Z",
60+
"finished_at": "2017-11-16T08:53:29.000Z",
61+
"duration": 120,
62+
"user": {
63+
"id": 1,
64+
"name": "John Doe2",
65+
"username": "user2",
66+
"state": "active",
67+
"avatar_url": "http://www.gravatar.com/avatar/c922747a93b40d1ea88262bf1aebee62?s=80&d=identicon",
68+
"web_url": "http://localhost/user2",
69+
"created_at": "2017-11-16T18:38:46.000Z",
70+
"bio": None,
71+
"location": None,
72+
"public_email": "",
73+
"skype": "",
74+
"linkedin": "",
75+
"twitter": "",
76+
"website_url": "",
77+
"organization": None,
78+
},
79+
}
80+
]
81+
82+
@pytest.fixture
83+
def resp_get_runners_jobs():
84+
with responses.RequestsMock() as rsps:
85+
rsps.add(
86+
method=responses.GET,
87+
url="http://localhost/api/v4/runners/6/jobs",
88+
json=runner_jobs,
89+
content_type="application/json",
90+
status=200,
91+
)
92+
yield rsps
93+
94+
95+
@pytest.fixture
96+
def resp_get_runners_list():
97+
with responses.RequestsMock() as rsps:
98+
rsps.add(
99+
method=responses.GET,
100+
url=re.compile(r".*?(/runners(/all)?|/(groups|projects)/1/runners)"),
101+
json=[runner_shortinfo],
102+
content_type="application/json",
103+
status=200,
104+
)
105+
yield rsps
106+
107+
108+
@pytest.fixture
109+
def resp_runner_detail():
110+
with responses.RequestsMock() as rsps:
111+
pattern = re.compile(r".*?/runners/6")
112+
rsps.add(
113+
method=responses.GET,
114+
url=pattern,
115+
json=runner_detail,
116+
content_type="application/json",
117+
status=200,
118+
)
119+
rsps.add(
120+
method=responses.PUT,
121+
url=pattern,
122+
json=runner_detail,
123+
content_type="application/json",
124+
status=200,
125+
)
126+
yield rsps
127+
128+
129+
@pytest.fixture
130+
def resp_runner_register():
131+
with responses.RequestsMock() as rsps:
132+
pattern = re.compile(r".*?/runners")
133+
rsps.add(
134+
method=responses.POST,
135+
url=pattern,
136+
json={"id": "6", "token": "6337ff461c94fd3fa32ba3b1ff4125"},
137+
content_type="application/json",
138+
status=200,
139+
)
140+
yield rsps
141+
142+
143+
@pytest.fixture
144+
def resp_runner_enable():
145+
with responses.RequestsMock() as rsps:
146+
pattern = re.compile(r".*?(projects|groups)/1/runners")
147+
rsps.add(
148+
method=responses.POST,
149+
url=pattern,
150+
json=runner_shortinfo,
151+
content_type="application/json",
152+
status=200,
153+
)
154+
yield rsps
155+
156+
157+
@pytest.fixture
158+
def resp_runner_delete():
159+
with responses.RequestsMock() as rsps:
160+
pattern = re.compile(r".*?/runners/6")
161+
rsps.add(
162+
method=responses.GET,
163+
url=pattern,
164+
json=runner_detail,
165+
content_type="application/json",
166+
status=200,
167+
)
168+
rsps.add(
169+
method=responses.DELETE, url=pattern, status=204,
170+
)
171+
yield rsps
172+
173+
174+
@pytest.fixture
175+
def resp_runner_disable():
176+
with responses.RequestsMock() as rsps:
177+
pattern = re.compile(r".*?/(groups|projects)/1/runners/6")
178+
rsps.add(
179+
method=responses.DELETE, url=pattern, status=204,
180+
)
181+
yield rsps
182+
183+
184+
@pytest.fixture
185+
def resp_runner_verify():
186+
with responses.RequestsMock() as rsps:
187+
pattern = re.compile(r".*?/runners/verify")
188+
rsps.add(
189+
method=responses.POST, url=pattern, status=200,
190+
)
191+
yield rsps
192+
193+
194+
def test_owned_runners_list(gl: gitlab.Gitlab, resp_get_runners_list):
195+
runners = gl.runners.list()
196+
assert runners[0].active == True
197+
assert runners[0].id == 6
198+
assert runners[0].name == "test-name"
199+
assert len(runners) == 1
200+
201+
202+
def test_project_runners_list(gl: gitlab.Gitlab, resp_get_runners_list):
203+
runners = gl.projects.get(1, lazy=True).runners.list()
204+
assert runners[0].active == True
205+
assert runners[0].id == 6
206+
assert runners[0].name == "test-name"
207+
assert len(runners) == 1
208+
209+
210+
def test_group_runners_list(gl: gitlab.Gitlab, resp_get_runners_list):
211+
runners = gl.groups.get(1, lazy=True).runners.list()
212+
assert runners[0].active == True
213+
assert runners[0].id == 6
214+
assert runners[0].name == "test-name"
215+
assert len(runners) == 1
216+
217+
218+
def test_all_runners_list(gl: gitlab.Gitlab, resp_get_runners_list):
219+
runners = gl.runners.all()
220+
assert runners[0].active == True
221+
assert runners[0].id == 6
222+
assert runners[0].name == "test-name"
223+
assert len(runners) == 1
224+
225+
226+
def test_create_runner(gl: gitlab.Gitlab, resp_runner_register):
227+
runner = gl.runners.create({"token": "token"})
228+
assert runner.id == "6"
229+
assert runner.token == "6337ff461c94fd3fa32ba3b1ff4125"
230+
231+
232+
def test_get_update_runner(gl: gitlab.Gitlab, resp_runner_detail):
233+
runner = gl.runners.get(6)
234+
assert runner.active == True
235+
runner.tag_list.append("new")
236+
runner.save()
237+
238+
239+
def test_remove_runner(gl: gitlab.Gitlab, resp_runner_delete):
240+
runner = gl.runners.get(6)
241+
runner.delete()
242+
gl.runners.delete(6)
243+
244+
245+
def test_disable_project_runner(gl: gitlab.Gitlab, resp_runner_disable):
246+
gl.projects.get(1, lazy=True).runners.delete(6)
247+
248+
249+
def test_disable_group_runner(gl: gitlab.Gitlab, resp_runner_disable):
250+
gl.groups.get(1, lazy=True).runners.delete(6)
251+
252+
253+
def test_enable_project_runner(gl: gitlab.Gitlab, resp_runner_enable):
254+
runner = gl.projects.get(1, lazy=True).runners.create({"runner_id": 6})
255+
assert runner.active == True
256+
assert runner.id == 6
257+
assert runner.name == "test-name"
258+
259+
260+
def test_enable_group_runner(gl: gitlab.Gitlab, resp_runner_enable):
261+
runner = gl.groups.get(1, lazy=True).runners.create({"runner_id": 6})
262+
assert runner.active == True
263+
assert runner.id == 6
264+
assert runner.name == "test-name"
265+
266+
267+
def test_verify_runner(gl: gitlab.Gitlab, resp_runner_verify):
268+
gl.runners.verify("token")
269+
270+
271+
def test_runner_jobs(gl: gitlab.Gitlab, resp_get_runners_jobs):
272+
jobs = gl.runners.get(6, lazy=True).jobs.list()
273+
assert jobs[0].duration == 120
274+
assert jobs[0].name == "test"
275+
assert jobs[0].user.get("name") == "John Doe2"
276+
assert len(jobs) == 1

test-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ pytest
77
pytest-cov
88
sphinx>=1.3
99
sphinx_rtd_theme
10+
responses

0 commit comments

Comments
 (0)