/
test_jobs.py
247 lines (196 loc) · 7.67 KB
/
test_jobs.py
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
245
246
247
# -*- coding: utf-8 -*-
#
# Copyright 2020-2021 - Swiss Data Science Center (SDSC)
# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
# Eidgenössische Technische Hochschule Zürich (ETHZ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Renku service job tests."""
import io
import os
import time
import uuid
import pytest
from marshmallow import EXCLUDE
from renku.service.controllers.utils.project_clone import user_project_clone
from renku.service.jobs.cleanup import cache_files_cleanup, cache_project_cleanup
from renku.service.serializers.templates import ProjectTemplateRequest
from tests.service.views.test_dataset_views import assert_rpc_response
from tests.utils import modified_environ, retry_failed
@pytest.mark.service
@pytest.mark.integration
@pytest.mark.jobs
@retry_failed
def test_cleanup_old_files(datapack_zip, svc_client_with_repo, service_job):
"""Upload archive and add its contents to a dataset."""
svc_client, headers, _, _ = svc_client_with_repo
headers.pop("Content-Type")
response = svc_client.post(
"/cache.files_upload",
data=dict(file=(io.BytesIO(datapack_zip.read_bytes()), datapack_zip.name)),
query_string={"unpack_archive": True, "override_existing": True},
headers=headers,
)
assert response
assert_rpc_response(response)
assert 200 == response.status_code
assert 4 == len(response.json["result"]["files"])
cache_files_cleanup()
response = svc_client.get("/cache.files_list", headers=headers)
assert response
assert_rpc_response(response)
assert 200 == response.status_code
assert 0 == len(response.json["result"]["files"])
@pytest.mark.service
@pytest.mark.jobs
def test_cleanup_files_old_keys(svc_client_with_user, service_job, tmp_path):
"""Cleanup old project."""
svc_client, headers, cache, user = svc_client_with_user
mydata = tmp_path / "mydata.json"
mydata.write_text("1,2,3")
file_upload = {
"file_id": uuid.uuid4().hex,
"content_type": "application/json",
"file_name": "mydata.json",
"file_size": 6,
"is_archive": False,
"is_dir": False,
"unpack_archive": False,
"relative_path": str(mydata),
}
cache.set_file(user, file_upload)
response = svc_client.get("/cache.files_list", headers=headers)
assert response
assert_rpc_response(response)
assert 200 == response.status_code
assert 1 == len(response.json["result"]["files"])
cache_files_cleanup()
response = svc_client.get("/cache.files_list", headers=headers)
assert response
assert_rpc_response(response)
assert 200 == response.status_code
assert 0 == len(response.json["result"]["files"])
@pytest.mark.service
@pytest.mark.jobs
@pytest.mark.integration
@retry_failed
def test_cleanup_old_project(datapack_zip, svc_client_with_repo, service_job):
"""Upload archive and add its contents to a dataset."""
svc_client, headers, _, _ = svc_client_with_repo
headers.pop("Content-Type")
response = svc_client.get("/cache.project_list", headers=headers)
assert response
assert_rpc_response(response)
assert 200 == response.status_code
assert 1 == len(response.json["result"]["projects"])
cache_project_cleanup()
response = svc_client.get("/cache.project_list", headers=headers)
assert response
assert_rpc_response(response)
assert 200 == response.status_code
assert 0 == len(response.json["result"]["projects"])
@pytest.mark.service
@pytest.mark.jobs
def test_cleanup_project_old_keys(svc_client_with_user, service_job):
"""Cleanup old project with old hset keys."""
svc_client, headers, cache, user = svc_client_with_user
project = {
"project_id": uuid.uuid4().hex,
"name": "my-project",
"slug": "my-project",
"fullname": "full project name",
"email": "my@email.com",
"owner": "me",
"token": "awesome token",
"git_url": "git@gitlab.com",
"initialized": True,
}
project = cache.make_project(user, project)
os.makedirs(str(project.abs_path), exist_ok=True)
response = svc_client.get("/cache.project_list", headers=headers)
assert response
assert_rpc_response(response)
assert 200 == response.status_code
assert 1 == len(response.json["result"]["projects"])
cache_project_cleanup()
response = svc_client.get("/cache.project_list", headers=headers)
assert response
assert_rpc_response(response)
assert 200 == response.status_code
assert 0 == len(response.json["result"]["projects"])
@pytest.mark.service
@pytest.mark.jobs
def test_job_constructor_lock(svc_client_with_user, service_job):
"""Test correct locking construction."""
svc_client, headers, cache, user = svc_client_with_user
project = {
"project_id": uuid.uuid4().hex,
"name": "my-project",
"slug": "my-project",
"fullname": "full project name",
"email": "my@email.com",
"owner": "me",
"token": "awesome token",
"git_url": "git@gitlab.com",
"initialized": True,
}
project = cache.make_project(user, project)
os.makedirs(str(project.abs_path), exist_ok=True)
job = cache.make_job(user, project=project)
assert job
assert job.job_id
assert project.project_id == job.project_id
assert user.user_id == job.user_id
assert project.project_id in {_id.decode("utf-8") for _id in job.locked.members()}
@pytest.mark.integration
@retry_failed
def test_project_cleanup_success(svc_client_cache):
"""Test project cleanup through the job."""
client, _, cache = svc_client_cache
user_data = {
"user_id": uuid.uuid4().hex,
"email": "contact@renkulab.io",
"fullname": "renku the frog",
"token": "None",
}
project_data = {
"project_name": "deadbeef",
"project_repository": "https://dev.renku.ch",
"project_namespace": "gitlab/renku-qa",
"identifier": "0xdeadbeef",
"depth": 1,
"url": "https://github.com/SwissDataScienceCenter/renku-project-template",
"owner": "SwissDataScienceCenter",
}
project_data = ProjectTemplateRequest().load({**user_data, **project_data}, unknown=EXCLUDE)
assert "user_id" not in project_data.keys()
project_one = user_project_clone(user_data, project_data)
assert project_one.age >= 0
assert not project_one.ttl_expired()
assert project_one.exists()
with modified_environ(RENKU_SVC_CLEANUP_TTL_PROJECTS="1"):
time.sleep(1)
assert project_one.age >= 1
assert project_one.ttl_expired()
cache_project_cleanup()
project_data = ProjectTemplateRequest().load({**user_data, **project_data}, unknown=EXCLUDE)
assert "user_id" not in project_data.keys()
user = cache.get_user(user_data["user_id"])
projects = cache.get_projects(user)
assert [] == [p.project_id for p in projects]
project_two = user_project_clone(user_data, project_data)
with modified_environ(RENKU_SVC_CLEANUP_TTL_PROJECTS="1800"):
assert project_two.age >= 0
assert not project_two.ttl_expired()
assert project_two.exists()
assert project_one.project_id != project_two.project_id