-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathtest_integration.py
More file actions
348 lines (298 loc) · 13.7 KB
/
test_integration.py
File metadata and controls
348 lines (298 loc) · 13.7 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env python
# coding=utf-8
"""Scriptworker integration tests.
"""
import asyncio
import json
import logging
import os
import tempfile
import aiohttp
import arrow
import pytest
import slugid
from asyncio_extras.contextmanager import async_contextmanager
import scriptworker.artifacts as artifacts
import scriptworker.log as swlog
import scriptworker.utils as utils
import scriptworker.worker as worker
from scriptworker.config import CREDS_FILES, apply_product_config, get_unfrozen_copy, read_worker_creds
from scriptworker.constants import DEFAULT_CONFIG
from scriptworker.context import Context
from scriptworker.exceptions import Download404
from . import integration_create_task_payload
log = logging.getLogger(__name__)
# constants helpers and fixtures {{{1
pytestmark = [
pytest.mark.skipif(os.environ.get("NO_TESTS_OVER_WIRE"), reason="NO_TESTS_OVER_WIRE: skipping integration test"),
pytest.mark.skipif(os.environ.get("NO_CREDENTIALS_TESTS"), reason="NO_CREDENTIALS_TESTS: skipping integration test"),
]
def read_integration_creds():
creds = read_worker_creds(key="integration_credentials")
if creds:
return creds
raise Exception(
"""To run integration tests, put your worker-test clientId creds, in json format,
in one of these files:
{files}
with the format
{{
"integration_credentials": {{
"accessToken": "...", "clientId": "...", "certificate": "..."
}}
}}
(only specify "certificate" if using temporary credentials)
This clientId will need the scope assume:project:taskcluster:worker-test-scopes
To skip integration tests, set the environment variable NO_CREDENTIALS_TESTS""".format(
files=CREDS_FILES
)
)
def build_config(override, basedir):
randstring = slugid.nice().lower().replace("_", "").replace("-", "")[:6]
config = get_unfrozen_copy(DEFAULT_CONFIG)
ED25519_DIR = os.path.join(os.path.dirname(__file__), "data", "ed25519")
config.update(
{
"log_dir": os.path.join(basedir, "log"),
"artifact_dir": os.path.join(basedir, "artifact"),
"task_log_dir": os.path.join(basedir, "artifact", "public", "logs"),
"work_dir": os.path.join(basedir, "work"),
"worker_type": "dummy-worker-{}".format(randstring),
"worker_id": "dummy-worker-{}".format(randstring),
"artifact_upload_timeout": 60 * 2,
"poll_interval": 5,
"reclaim_interval": 5,
"task_script": ("bash", "-c", ">&2 echo bar && echo foo && sleep 9 && exit 1"),
"task_max_timeout": 60,
"cot_product": "firefox",
"ed25519_private_key_path": os.path.join(ED25519_DIR, "scriptworker_private_key"),
"ed25519_public_keys": {
"docker-worker": ["8dBv4bbnZ3RsDzQiPKTJ18uo3hq5Rjm94JG6HXzAcBM="],
"generic-worker": ["PkI5NslA78wSsYaKNzKq7iD7MLQy7W6wYO/0WFd4tWM="],
"scriptworker": ["KxYrV3XAJ3uOyAUX0Wcl1Oeu6GSMrI/5hOn39q8Lf0I="],
},
}
)
creds = read_integration_creds()
del config["credentials"]
if isinstance(override, dict):
config.update(override)
with open(os.path.join(basedir, "config.json"), "w") as fh:
json.dump(config, fh, indent=2, sort_keys=True)
config = apply_product_config(config)
return config, creds
@async_contextmanager
async def get_context(config_override=None):
context = Context()
with tempfile.TemporaryDirectory() as tmp:
context.config, credentials = build_config(config_override, basedir=tmp)
swlog.update_logging_config(context)
utils.cleanup(context)
async with aiohttp.ClientSession() as session:
context.session = session
context.credentials = credentials
yield context
def get_temp_creds(context):
creds = read_integration_creds()
if "certificate" in creds:
return
temp_creds = utils.create_temp_creds(
creds["clientId"],
creds["accessToken"],
expires=arrow.utcnow().shift(minutes=10).datetime,
scopes=[
"queue:cancel-task:test-dummy-scheduler/*",
"queue:claim-work:test-dummy-provisioner/dummy-worker-*",
"queue:create-task:lowest:test-dummy-provisioner/dummy-worker-*",
"queue:scheduler-id:test-dummy-scheduler",
"queue:define-task:test-dummy-provisioner/dummy-worker-*",
"queue:get-artifact:SampleArtifacts/_/X.txt",
"queue:schedule-task:test-dummy-scheduler/*",
"queue:task-group-id:test-dummy-scheduler/*",
"queue:worker-id:test-dummy-workers/dummy-worker-*",
],
)
if temp_creds:
context.credentials = temp_creds
print("Using temp creds!")
else:
raise Exception("Can't get temp_creds!")
@async_contextmanager
async def get_temp_creds_context(config_override=None):
async with get_context(config_override) as context:
get_temp_creds(context)
yield context
async def create_task(context, task_id, task_group_id, **kwargs):
payload = integration_create_task_payload(context.config, task_group_id, **kwargs)
return await context.queue.createTask(task_id, payload)
async def task_status(context, task_id):
return await context.queue.status(task_id)
@async_contextmanager
async def remember_cwd():
"""http://stackoverflow.com/a/170174"""
curdir = os.getcwd()
try:
yield
finally:
os.chdir(curdir)
# run_successful_task {{{1
@pytest.mark.parametrize("context_function", [get_context, get_temp_creds_context])
@pytest.mark.asyncio
async def test_run_successful_task(context_function):
task_id = slugid.nice()
task_group_id = slugid.nice()
async with context_function(None) as context:
result = await create_task(context, task_id, task_group_id)
assert result["status"]["state"] == "pending"
async with remember_cwd():
os.chdir(os.path.dirname(context.config["work_dir"]))
status = await worker.run_tasks(context)
assert status == 1
result = await task_status(context, task_id)
assert result["status"]["state"] == "failed"
# run_maxtimeout {{{1
@pytest.mark.parametrize("context_function", [get_context, get_temp_creds_context])
@pytest.mark.asyncio
async def test_run_maxtimeout(context_function):
task_id = slugid.nice()
partial_config = {"task_max_timeout": 2, "task_script": ("bash", "-c", ">&2 echo bar && echo foo && sleep 30 && exit 1")}
async with context_function(partial_config) as context:
result = await create_task(context, task_id, task_id)
assert result["status"]["state"] == "pending"
async with remember_cwd():
os.chdir(os.path.dirname(context.config["work_dir"]))
status = await worker.run_tasks(context)
assert status == context.config["task_max_timeout_status"]
# cancel task {{{1
async def do_cancel(context, task_id):
count = 0
while True:
await asyncio.sleep(1)
count += 1
assert count < 30, "do_cancel Timeout!"
if not context.task or not context.proc:
continue
await context.queue.cancelTask(task_id)
break
async def run_task_until_stopped(context):
async with remember_cwd():
os.chdir(os.path.dirname(context.config["work_dir"]))
status = await worker.run_tasks(context)
return status
@pytest.mark.asyncio
async def test_cancel_task():
task_id = slugid.nice()
partial_config = {"invalid_reclaim_status": 19, "task_script": ("bash", "-c", ">&2 echo bar && echo foo && sleep 30 && exit 1")}
# Don't use temporary credentials from claimTask, since they don't allow us
# to cancel the created task.
async with get_context(partial_config) as context:
result = await create_task(context, task_id, task_id)
assert result["status"]["state"] == "pending"
cancel_fut = asyncio.ensure_future(do_cancel(context, task_id))
task_fut = asyncio.ensure_future(run_task_until_stopped(context))
await utils.raise_future_exceptions([cancel_fut, task_fut])
status = await context.queue.status(task_id)
assert len(status["status"]["runs"]) == 1
assert status["status"]["state"] == "exception"
assert status["status"]["runs"][0]["reasonResolved"] == "canceled"
log_url = context.queue.buildUrl("getLatestArtifact", task_id, "public/logs/live_backing.log")
log_path = os.path.join(context.config["work_dir"], "log")
await utils.download_file(context, log_url, log_path)
with open(log_path) as fh:
contents = fh.read()
assert contents.rstrip() == "bar\nfoo\nAutomation Error: python exited with signal -15"
# cancel task {{{1
async def do_shutdown(context):
count = 0
while True:
await asyncio.sleep(1)
count += 1
assert count < 30, "do_shutdown Timeout!"
if not context.running_tasks or not context.task or not context.proc:
continue
await context.running_tasks.cancel()
break
@pytest.mark.asyncio
async def test_shutdown():
task_id = slugid.nice()
partial_config = {"task_script": ("bash", "-c", ">&2 echo running task script && sleep 30 && exit 1")}
# Don't use temporary credentials from claimTask, since they don't allow us
# to cancel the created task.
async with get_context(partial_config) as context:
result = await create_task(context, task_id, task_id)
assert result["status"]["state"] == "pending"
fake_cot_log = os.path.join(context.config["artifact_dir"], "public", "logs", "chain_of_trust.log")
fake_other_artifact = os.path.join(context.config["artifact_dir"], "public", "artifact.apk")
with open(fake_cot_log, "w") as file:
file.write("CoT logs")
with open(fake_other_artifact, "w") as file:
file.write("unrelated artifact")
cancel_fut = asyncio.ensure_future(do_shutdown(context))
task_fut = asyncio.ensure_future(run_task_until_stopped(context))
await utils.raise_future_exceptions([cancel_fut, task_fut])
status = await context.queue.status(task_id)
assert len(status["status"]["runs"]) == 2 # Taskcluster should create a replacement task
assert status["status"]["runs"][0]["state"] == "exception"
assert status["status"]["runs"][0]["reasonResolved"] == "worker-shutdown"
log_url = context.queue.buildUrl("getArtifact", task_id, 0, "public/logs/live_backing.log")
cot_log_url = context.queue.buildUrl("getArtifact", task_id, 0, "public/logs/chain_of_trust.log")
other_artifact_url = context.queue.buildUrl("getArtifact", task_id, 0, "public/artifact.apk")
log_path = os.path.join(context.config["work_dir"], "log")
cot_log_path = os.path.join(context.config["work_dir"], "cot_log")
other_artifact_path = os.path.join(context.config["work_dir"], "artifact.apk")
await utils.download_file(context, log_url, log_path)
await utils.download_file(context, cot_log_url, cot_log_path)
with pytest.raises(Download404):
await utils.download_file(context, other_artifact_url, other_artifact_path)
with open(log_path) as fh:
contents = fh.read()
assert contents.rstrip() == "running task script\nAutomation Error: python exited with signal -15"
with open(cot_log_path) as fh:
contents = fh.read()
assert contents.rstrip() == "CoT logs"
# empty_queue {{{1
@pytest.mark.parametrize("context_function", [get_context, get_temp_creds_context])
@pytest.mark.asyncio
async def test_empty_queue(context_function):
async with context_function(None) as context:
async with remember_cwd():
os.chdir(os.path.dirname(context.config["work_dir"]))
status = await worker.run_tasks(context)
assert status is None
# temp_creds {{{1
@pytest.mark.parametrize("context_function", [get_context, get_temp_creds_context])
@pytest.mark.asyncio
async def test_temp_creds(context_function):
async with context_function(None) as context:
async with remember_cwd():
os.chdir(os.path.dirname(context.config["work_dir"]))
context.temp_credentials = utils.create_temp_creds(
context.credentials["clientId"], context.credentials["accessToken"], expires=arrow.utcnow().shift(minutes=10).datetime
)
result = await context.temp_queue.ping()
assert result["alive"]
# private artifacts {{{1
@pytest.mark.parametrize("context_function", [get_context, get_temp_creds_context])
@pytest.mark.asyncio
async def test_private_artifacts(context_function):
task_group_id = task_id = slugid.nice()
override = {"task_script": ("bash", "-c", ">&2 echo")}
async with context_function(override) as context:
result = await create_task(context, task_id, task_group_id, scopes=["queue:get-artifact:SampleArtifacts/_/X.txt"])
assert result["status"]["state"] == "pending"
path = os.path.join(context.config["artifact_dir"], "SampleArtifacts/_/X.txt")
utils.makedirs(os.path.dirname(path))
with open(path, "w") as fh:
fh.write("bar")
async with remember_cwd():
os.chdir(os.path.dirname(context.config["work_dir"]))
status = await worker.run_tasks(context)
assert status == 0
result = await task_status(context, task_id)
assert result["status"]["state"] == "completed"
url = artifacts.get_artifact_url(context, task_id, "SampleArtifacts/_/X.txt")
path2 = os.path.join(context.config["work_dir"], "downloaded_file")
await utils.download_file(context, url, path2)
with open(path2, "r") as fh:
contents = fh.read().strip()
assert contents == "bar"