-
Notifications
You must be signed in to change notification settings - Fork 36
/
__init__.py
296 lines (240 loc) · 8.64 KB
/
__init__.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
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
#!/usr/bin/env python
# coding=utf-8
"""Test base files
"""
import aiohttp
import arrow
import asyncio
from copy import deepcopy
import json
import mock
import os
import pytest
import tempfile
import taskcluster.exceptions
from scriptworker.config import get_unfrozen_copy, apply_product_config
from scriptworker.constants import DEFAULT_CONFIG
from scriptworker.context import Context
from scriptworker.utils import makedirs, noop_sync
try:
import yarl
YARL = True
except ImportError:
YARL = False
VERBOSE = os.environ.get("SCRIPTWORKER_VERBOSE_TESTS", False)
GOOD_GPG_KEYS = {
"docker.root@example.com": {
"fingerprint": "BFCEA6E98A1C2EC4918CBDEE9DA033D5FFFABCCF",
"keyid": "FFFABCCF",
},
"docker@example.com": {
"fingerprint": "F612354DFAF46BAADAE23801CD3C13EFBEAB7ED4",
"keyid": "BEAB7ED4",
},
"scriptworker@example.com": {
"fingerprint": "FB7765CD0FC616FF7AC961A1D9DC50F64C7D44CF",
"keyid": "4C7D44CF",
},
}
BAD_GPG_KEYS = {
"unknown@example.com": {
"fingerprint": "B45FE2F4035C3786120998174ACA2B25224905DA",
"keyid": "224905DA",
},
}
ARTIFACT_SHAS = {
"public/foo": "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c",
"public/baz": "bf07a7fbb825fc0aae7bf4a1177b2b31fcf8a3feeaf7092761e18c859ee52a9c",
"public/logs/bar": "7d865e959b2466918c9863afca942d0fb89d7c9ac0c99bafc3749504ded97730",
}
TIMEOUT_SCRIPT = os.path.join(os.path.dirname(__file__), "data", "long_running.py")
def read(path):
"""Return the contents of a file.
"""
with open(path, "r", encoding="utf-8") as fh:
return fh.read()
def touch(path):
""" Create an empty file. Different from the system 'touch' in that it
will overwrite an existing file.
"""
with open(path, "w") as fh:
print(path, file=fh, end="")
class SuccessfulQueue(object):
result = "yay"
info = None
status = 409
task = {}
reclaim_task = {
'credentials': {'a': 'b'},
}
async def claimTask(self, *args, **kwargs):
return self.result
async def reclaimTask(self, *args, **kwargs):
self.info = ['reclaimTask', args, kwargs]
raise taskcluster.exceptions.TaskclusterRestFailure("foo", None, status_code=self.status)
async def reportCompleted(self, *args, **kwargs):
self.info = ['reportCompleted', args, kwargs]
async def reportFailed(self, *args, **kwargs):
self.info = ['reportFailed', args, kwargs]
async def reportException(self, *args, **kwargs):
self.info = ['reportException', args, kwargs]
async def createArtifact(self, *args, **kwargs):
self.info = ['createArtifact', args, kwargs]
return {
"contentType": "text/plain",
"putUrl": "url",
}
async def claimWork(self, *args, **kwargs):
self.info = ['claimWork', args, kwargs]
return {'tasks': [self.task]}
class UnsuccessfulQueue(object):
status = 409
async def claimTask(self, *args, **kwargs):
raise taskcluster.exceptions.TaskclusterRestFailure("foo", None, status_code=self.status)
async def reportCompleted(self, *args, **kwargs):
raise taskcluster.exceptions.TaskclusterRestFailure("foo", None, status_code=self.status)
async def reportFailed(self, *args, **kwargs):
raise taskcluster.exceptions.TaskclusterRestFailure("foo", None, status_code=self.status)
class FakeResponse(aiohttp.client_reqrep.ClientResponse):
"""Integration tests allow us to test everything's hooked up to aiohttp
correctly. When we don't want to actually hit an external url, have
the aiohttp session's _request method return a FakeResponse.
"""
def __init__(self, *args, status=200, payload=None, **kwargs):
self._connection = mock.MagicMock()
self._payload = payload or {}
self.status = status
self._headers = {'content-type': 'application/json'}
self._cache = {}
self._loop = mock.MagicMock()
self.content = self
self.resp = [b"asdf", b"asdf"]
self._url = args[1]
self._history = ()
if YARL:
# fix aiohttp 1.1.0
self._url_obj = yarl.URL(args[1])
@asyncio.coroutine
def text(self, *args, **kwargs):
return json.dumps(self._payload)
@asyncio.coroutine
def json(self, *args, **kwargs):
return self._payload
@asyncio.coroutine
def release(self):
return
async def read(self, *args):
if self.resp:
return self.resp.pop(0)
@pytest.fixture(scope='function')
def successful_queue():
return SuccessfulQueue()
@pytest.fixture(scope='function')
def unsuccessful_queue():
return UnsuccessfulQueue()
@pytest.mark.asyncio
@pytest.fixture(scope='function')
async def fake_session():
@asyncio.coroutine
def _fake_request(method, url, *args, **kwargs):
resp = FakeResponse(method, url)
resp._history = (FakeResponse(method, url, status=302),)
return resp
session = aiohttp.ClientSession()
session._request = _fake_request
yield session
await session.close()
@pytest.mark.asyncio
@pytest.fixture(scope='function')
async def fake_session_500():
@asyncio.coroutine
def _fake_request(method, url, *args, **kwargs):
resp = FakeResponse(method, url, status=500)
resp._history = (FakeResponse(method, url, status=302),)
return resp
session = aiohttp.ClientSession()
session._request = _fake_request
yield session
await session.close()
def integration_create_task_payload(config, task_group_id, scopes=None,
task_payload=None, task_extra=None):
"""For various integration tests, we need to call createTask for test tasks.
This function creates a dummy payload for those createTask calls.
"""
now = arrow.utcnow()
deadline = now.replace(hours=1)
expires = now.replace(days=3)
scopes = scopes or []
task_payload = task_payload or {}
task_extra = task_extra or {}
return {
'provisionerId': config['provisioner_id'],
'schedulerId': 'test-dummy-scheduler',
'workerType': config['worker_type'],
'taskGroupId': task_group_id,
'dependencies': [],
'requires': 'all-completed',
'routes': [],
'priority': 'normal',
'retries': 5,
'created': now.isoformat(),
'deadline': deadline.isoformat(),
'expires': expires.isoformat(),
'scopes': scopes,
'payload': task_payload,
'metadata': {
'name': 'ScriptWorker Integration Test',
'description': 'ScriptWorker Integration Test',
'owner': 'release+python@mozilla.com',
'source': 'https://github.com/mozilla-releng/scriptworker/'
},
'tags': {},
'extra': task_extra,
}
@pytest.yield_fixture(scope='function')
def tmpdir():
"""Yield a tmpdir that gets cleaned up afterwards.
This is because various pytest tmpdir implementations either don't return
a string, or don't clean up properly.
"""
with tempfile.TemporaryDirectory() as tmp:
yield tmp
@pytest.yield_fixture(scope='function')
def tmpdir2():
"""Yield a tmpdir that gets cleaned up afterwards.
Sometimes I need 2 tmpdirs in a test.
a string, or don't clean up properly.
"""
with tempfile.TemporaryDirectory() as tmp:
yield tmp
async def _close_session(obj):
"""Get rid of all the unclosed session warnings.
"""
if not hasattr(obj, 'session'):
return
if isinstance(obj.session, aiohttp.ClientSession):
await obj.session.close()
@pytest.mark.asyncio
@pytest.yield_fixture(scope='function', params=['firefox'])
async def rw_context(request, event_loop):
with tempfile.TemporaryDirectory() as tmp:
config = get_unfrozen_copy(DEFAULT_CONFIG)
config['cot_product'] = request.param
context = Context()
context.config = apply_product_config(config)
context.config['gpg_lockfile'] = os.path.join(tmp, 'gpg_lockfile')
context.config['cot_job_type'] = "signing"
for key, value in context.config.items():
if key.endswith("_dir"):
context.config[key] = os.path.join(tmp, key)
makedirs(context.config[key])
if key.endswith("key_path") or key in ("gpg_home", ):
context.config[key] = os.path.join(tmp, key)
context.config['verbose'] = VERBOSE
context.event_loop = event_loop
yield context
await _close_session(context)
await _close_session(context.queue)
await _close_session(context.temp_queue)
async def noop_async(*args, **kwargs):
pass