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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
import tempfile
import time
import uuid
from pathlib import Path
from random import randint
from subprocess import check_output
import pytest
import gitlab
def reset_gitlab(gl):
# previously tools/reset_gitlab.py
for project in gl.projects.list():
project.delete()
for group in gl.groups.list():
group.delete()
for variable in gl.variables.list():
variable.delete()
for user in gl.users.list():
if user.username != "root":
user.delete()
def set_token(container, rootdir):
set_token_rb = rootdir / "fixtures" / "set_token.rb"
with open(set_token_rb, "r") as f:
set_token_command = f.read().strip()
rails_command = [
"docker",
"exec",
container,
"gitlab-rails",
"runner",
set_token_command,
]
output = check_output(rails_command).decode().strip()
return output
@pytest.fixture(scope="session")
def temp_dir():
return Path(tempfile.gettempdir())
@pytest.fixture(scope="session")
def test_dir(pytestconfig):
return pytestconfig.rootdir / "tools" / "functional"
@pytest.fixture(scope="session")
def docker_compose_file(test_dir):
return test_dir / "fixtures" / "docker-compose.yml"
@pytest.fixture(scope="session")
def check_is_alive(request):
"""
Return a healthcheck function fixture for the GitLab container spinup.
"""
start = time.time()
# Temporary manager to disable capsys in a session-scoped fixture
# so people know it takes a while for GitLab to spin up
# https://github.com/pytest-dev/pytest/issues/2704
capmanager = request.config.pluginmanager.getplugin("capturemanager")
def _check(container):
delay = int(time.time() - start)
with capmanager.global_and_fixture_disabled():
print(f"Waiting for GitLab to reconfigure.. (~{delay}s)")
logs = ["docker", "logs", container]
output = check_output(logs).decode()
return "gitlab Reconfigured!" in output
return _check
@pytest.fixture(scope="session")
def gitlab_config(check_is_alive, docker_ip, docker_services, temp_dir, test_dir):
config_file = temp_dir / "python-gitlab.cfg"
port = docker_services.port_for("gitlab", 80)
docker_services.wait_until_responsive(
timeout=180, pause=5, check=lambda: check_is_alive("gitlab-test")
)
token = set_token("gitlab-test", rootdir=test_dir)
config = f"""[global]
default = local
timeout = 60
[local]
url = http://{docker_ip}:{port}
private_token = {token}
api_version = 4"""
with open(config_file, "w") as f:
f.write(config)
return config_file
@pytest.fixture(scope="session")
def gl(gitlab_config):
"""Helper instance to make fixtures and asserts directly via the API."""
instance = gitlab.Gitlab.from_config("local", [gitlab_config])
reset_gitlab(instance)
return instance
@pytest.fixture(scope="module")
def group(gl):
"""Group fixture for group API resource tests."""
_id = uuid.uuid4().hex
data = {
"name": f"test-group-{_id}",
"path": f"group-{_id}",
}
group = gl.groups.create(data)
yield group
try:
group.delete()
except gitlab.exceptions.GitlabDeleteError as e:
print(f"Group already deleted: {e}")
@pytest.fixture(scope="module")
def project(gl):
"""Project fixture for project API resource tests."""
_id = uuid.uuid4().hex
name = f"test-project-{_id}"
project = gl.projects.create(name=name)
yield project
try:
project.delete()
except gitlab.exceptions.GitlabDeleteError as e:
print(f"Project already deleted: {e}")
@pytest.fixture(scope="module")
def user(gl):
"""User fixture for user API resource tests."""
_id = uuid.uuid4().hex
email = f"user{_id}@email.com"
username = f"user{_id}"
name = f"User {_id}"
password = "fakepassword"
user = gl.users.create(email=email, username=username, name=name, password=password)
yield user
try:
user.delete()
except gitlab.exceptions.GitlabDeleteError as e:
print(f"User already deleted: {e}")
@pytest.fixture(scope="module")
def issue(project):
"""Issue fixture for issue API resource tests."""
_id = uuid.uuid4().hex
data = {"title": f"Issue {_id}", "description": f"Issue {_id} description"}
return project.issues.create(data)
@pytest.fixture(scope="module")
def label(project):
"""Label fixture for project label API resource tests."""
_id = uuid.uuid4().hex
data = {
"name": f"prjlabel{_id}",
"description": f"prjlabel1 {_id} description",
"color": "#112233",
}
return project.labels.create(data)
@pytest.fixture(scope="module")
def group_label(group):
"""Label fixture for group label API resource tests."""
_id = uuid.uuid4().hex
data = {
"name": f"grplabel{_id}",
"description": f"grplabel1 {_id} description",
"color": "#112233",
}
return group.labels.create(data)
@pytest.fixture(scope="module")
def variable(project):
"""Variable fixture for project variable API resource tests."""
_id = uuid.uuid4().hex
data = {"key": f"var{_id}", "value": f"Variable {_id}"}
return project.variables.create(data)
@pytest.fixture(scope="module")
def deploy_token(project):
"""Deploy token fixture for project deploy token API resource tests."""
_id = uuid.uuid4().hex
data = {
"name": f"token-{_id}",
"username": "root",
"expires_at": "2021-09-09",
"scopes": "read_registry",
}
return project.deploytokens.create(data)
@pytest.fixture(scope="module")
def group_deploy_token(group):
"""Deploy token fixture for group deploy token API resource tests."""
_id = uuid.uuid4().hex
data = {
"name": f"group-token-{_id}",
"username": "root",
"expires_at": "2021-09-09",
"scopes": "read_registry",
}
return group.deploytokens.create(data)
|