-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_hooks.py
More file actions
345 lines (298 loc) · 15 KB
/
test_hooks.py
File metadata and controls
345 lines (298 loc) · 15 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
"""
Unit tests for the Drycc api app.
Run the tests with "./manage.py test api"
"""
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.cache import cache
from rest_framework.authtoken.models import Token
from api.tests import adapter, DryccTransactionTestCase
import requests_mock
User = get_user_model()
RSA_PUBKEY = (
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfQkkUUoxpvcNMkvv7jqnfodgs37M2eBO"
"APgLK+KNBMaZaaKB4GF1QhTCMfFhoiTW3rqa0J75bHJcdkoobtTHlK8XUrFqsquWyg3XhsT"
"Yr/3RQQXvO86e2sF7SVDJqVtpnbQGc5SgNrHCeHJmf5HTbXSIjCO/AJSvIjnituT/SIAMGe"
"Bw0Nq/iSltwYAek1hiKO7wSmLcIQ8U4A00KEUtalaumf2aHOcfjgPfzlbZGP0S0cuBwSqLr"
"8b5XGPmkASNdUiuJY4MJOce7bFU14B7oMAy2xacODUs1momUeYtGI9T7X2WMowJaO7tP3Gl"
"sgBMP81VfYTfYChAyJpKp2yoP autotest@autotesting comment"
)
RSA_PUBKEY2 = (
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC4xELdubosJ2/bQuiSUyWclVVa71pXpmq"
"aXTwfau/XFLgD5yE+TOFbVT22xvEr4AwZqS9w0TBMp4RLfi4pTdjoIK+lau2lDMuEpbF4xg"
"PWAveAqKuLcKJbJrZQdo5VWn5//7+M1RHQCPqjeN2iS9I3C8yiPg3mMPT2mKuyZYB9VD3hK"
"mhT4xRAsS6vfKZr7CmFHgAmRBqdaU1RetR5nfTj0R5yyAv7Z2BkE8UhUAseFZ0djBs6kzjs"
"5ddgM4Gv2Zajs7qVvpVPzZpq3vFB16Q5TMj2YtoYF6UZFFf4u/4KAW8xfYJAFdpNsvh279s"
"dJS08nTeElUg6pn83A3hqWX+J testing"
)
ECDSA_PUBKEY = (
"ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAAB"
"BBCGB0x9lmubbLJTF5NekCI0Cgjyip6jJh/t/qQQi1LAZisbREBJ8Wy+hwSn3tnbf/Imh9X"
"+MQnrrza0jaQ3QUAQ= autotest@autotesting comment"
)
ED25519_PUBKEY = (
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAPYa7ztrkGyl/LSpBxv0DjPej74GCSVItX"
"9Y2+/zxc+ testing"
)
BAD_KEY = (
"ssh-rsa foooooooooooooooooooooooooooooooooooooooooooooooooooobaaaaaaarrr"
"rrrrr testing"
)
@requests_mock.Mocker(real_http=True, adapter=adapter)
class HookTest(DryccTransactionTestCase):
"""Tests API hooks used to trigger actions from external components"""
fixtures = ['tests.json']
def setUp(self):
self.user = User.objects.get(username='autotest')
self.token = Token.objects.get(user=self.user).key
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
def tearDown(self):
# make sure every test has a clean slate for k8s mocking
cache.clear()
def test_key_hook(self, mock_requests):
"""Test fetching keys for an app and a user"""
# Create app to use
app_id = self.create_app()
# give user permission to app
url = "/v2/apps/{}/perms".format(app_id)
body = {'username': str(self.user)}
response = self.client.post(url, body)
self.assertEqual(response.status_code, 201, response.data)
# Create rsa key
body = {'id': str(self.user), 'public': RSA_PUBKEY}
response = self.client.post('/v2/keys', body)
self.assertEqual(response.status_code, 201, response.data)
rsa_pub = response.data['public']
# Create another rsa key
body = {'id': str(self.user) + '-2', 'public': RSA_PUBKEY2}
response = self.client.post('/v2/keys', body)
self.assertEqual(response.status_code, 201, response.data)
rsa_pub2 = response.data['public']
# Create dsa key
body = {'id': str(self.user) + '-3', 'public': ECDSA_PUBKEY}
response = self.client.post('/v2/keys', body)
self.assertEqual(response.status_code, 201, response.data)
dsa_pub = response.data['public']
# Create ed25519 key
body = {'id': str(self.user) + '-4', 'public': ED25519_PUBKEY}
response = self.client.post('/v2/keys', body)
self.assertEqual(response.status_code, 201, response.data)
ed25519_pub = response.data['public']
# Attempt adding a bad SSH pubkey
body = {'id': str(self.user) + '-5', 'public': BAD_KEY}
response = self.client.post('/v2/keys', body)
self.assertEqual(response.status_code, 400, response.data)
# Make sure 404 is returned for a random app
url = '/v2/hooks/keys/doesnotexist'
response = self.client.get(url, HTTP_X_DRYCC_BUILDER_AUTH=settings.BUILDER_KEY)
self.assertEqual(response.status_code, 404)
# Test app that exists
url = '/v2/hooks/keys/{}'.format(app_id)
response = self.client.get(url, HTTP_X_DRYCC_BUILDER_AUTH=settings.BUILDER_KEY)
self.assertEqual(response.status_code, 200, response.data)
self.assertEqual(response.data, {"autotest": [
{'key': rsa_pub, 'fingerprint': '54:6d:da:1f:91:b5:2b:6f:a2:83:90:c4:f9:73:76:f5'},
{'key': rsa_pub2, 'fingerprint': '43:fd:22:bc:dc:ca:6a:28:ba:71:4c:18:41:1d:d1:e2'},
{'key': dsa_pub, 'fingerprint': '28:dd:ef:f9:12:ab:f9:80:6f:4c:0a:e7:e7:a4:59:95'},
{'key': ed25519_pub, 'fingerprint': '75:9a:b3:81:13:40:c2:78:32:aa:e3:b4:93:2a:12:c9'}
]})
# Test against an app that exist but user does not
url = '/v2/hooks/keys/{}/foooooo'.format(app_id)
response = self.client.get(url, HTTP_X_DRYCC_BUILDER_AUTH=settings.BUILDER_KEY)
self.assertEqual(response.status_code, 404)
# Test against an app that exists and user that does
url = '/v2/hooks/keys/{}/{}'.format(app_id, str(self.user))
response = self.client.get(url, HTTP_X_DRYCC_BUILDER_AUTH=settings.BUILDER_KEY)
self.assertEqual(response.status_code, 200, response.data)
self.assertEqual(response.data, {"autotest": [
{'key': rsa_pub, 'fingerprint': '54:6d:da:1f:91:b5:2b:6f:a2:83:90:c4:f9:73:76:f5'},
{'key': rsa_pub2, 'fingerprint': '43:fd:22:bc:dc:ca:6a:28:ba:71:4c:18:41:1d:d1:e2'},
{'key': dsa_pub, 'fingerprint': '28:dd:ef:f9:12:ab:f9:80:6f:4c:0a:e7:e7:a4:59:95'},
{'key': ed25519_pub, 'fingerprint': '75:9a:b3:81:13:40:c2:78:32:aa:e3:b4:93:2a:12:c9'}
]})
# Fetch a valid ssh key
url = '/v2/hooks/key/54:6d:da:1f:91:b5:2b:6f:a2:83:90:c4:f9:73:76:f5'
response = self.client.get(url, HTTP_X_DRYCC_BUILDER_AUTH=settings.BUILDER_KEY)
self.assertEqual(response.status_code, 200, response.data)
self.assertEqual(response.data, {
"username": str(self.user),
"apps": [
app_id
]
})
# Fetch an non-existent base64 encoded ssh key
url = '/v2/hooks/key/54:6d:da:1f:91:b5:2b:6f:a2:83:90:c4:f9:73:76:wooooo'
response = self.client.get(url, HTTP_X_DRYCC_BUILDER_AUTH=settings.BUILDER_KEY)
self.assertEqual(response.status_code, 404)
# Fetch an invalid (not encoded) ssh key
url = '/v2/hooks/key/nope'
response = self.client.get(url, HTTP_X_DRYCC_BUILDER_AUTH=settings.BUILDER_KEY)
self.assertEqual(response.status_code, 404)
def test_build_hook(self, mock_requests):
"""Test creating a Build via an API Hook"""
app_id = self.create_app()
build = {'username': 'autotest', 'app': app_id}
url = '/v2/hooks/build'.format(**locals())
body = {'receive_user': 'autotest',
'receive_repo': app_id,
'image': '{app_id}:v2'.format(**locals()),
'stack': 'container'}
# post the build without an auth token
self.client.credentials()
response = self.client.post(url, body)
self.assertEqual(response.status_code, 401, response.data)
# post the build with the builder auth key
response = self.client.post(url, body,
HTTP_X_DRYCC_BUILDER_AUTH=settings.BUILDER_KEY)
self.assertEqual(response.status_code, 200, response.data)
self.assertIn('release', response.data)
self.assertIn('version', response.data['release'])
def test_build_hook_slug_url(self, mock_requests):
"""Test creating a slug_url build via an API Hook"""
app_id = self.create_app()
build = {'username': 'autotest', 'app': app_id}
url = '/v2/hooks/build'.format(**locals())
body = {'receive_user': 'autotest',
'receive_repo': app_id,
'image': 'http://example.com/slugs/foo-12345354.tar.gz',
'stack': 'container'}
# post the build without an auth token
self.client.credentials()
response = self.client.post(url, body)
self.assertEqual(response.status_code, 401, response.data)
# post the build with the builder auth key
response = self.client.post(url, body,
HTTP_X_DRYCC_BUILDER_AUTH=settings.BUILDER_KEY)
self.assertEqual(response.status_code, 200, response.data)
self.assertIn('release', response.data)
self.assertIn('version', response.data['release'])
def test_build_hook_procfile(self, mock_requests):
"""Test creating a Procfile build via an API Hook"""
app_id = self.create_app()
build = {'username': 'autotest', 'app': app_id}
url = '/v2/hooks/build'.format(**locals())
PROCFILE = {'web': 'node server.js', 'worker': 'node worker.js'}
SHA = 'ecdff91c57a0b9ab82e89634df87e293d259a3aa'
body = {'receive_user': 'autotest',
'receive_repo': app_id,
'image': '{app_id}:v2'.format(**locals()),
'stack': 'heroku-18',
'sha': SHA,
'procfile': PROCFILE}
# post the build with the builder auth key
response = self.client.post(url, body,
HTTP_X_DRYCC_BUILDER_AUTH=settings.BUILDER_KEY)
self.assertEqual(response.status_code, 200, response.data)
self.assertIn('release', response.data)
self.assertIn('version', response.data['release'])
# make sure build fields were populated
url = '/v2/apps/{app_id}/builds'.format(**locals())
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertIn('results', response.data)
build = response.data['results'][0]
self.assertEqual(build['sha'], SHA)
self.assertEqual(build['procfile'], PROCFILE)
# test listing/retrieving container info
url = "/v2/apps/{app_id}/pods/web".format(**locals())
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertEqual(len(response.data['results']), 1)
container = response.data['results'][0]
self.assertEqual(container['type'], 'web')
self.assertEqual(container['release'], 'v2')
# pod name is auto generated so use regex
self.assertRegex(container['name'], app_id + '-web-[0-9]{1,10}-[a-z0-9]{5}')
# post the build without an auth token
self.client.credentials()
response = self.client.post(url, body)
self.assertEqual(response.status_code, 401, response.data)
def test_build_hook_dockerfile(self, mock_requests):
"""Test creating a Dockerfile build via an API Hook"""
app_id = self.create_app()
build = {'username': 'autotest', 'app': app_id}
url = '/v2/hooks/build'.format(**locals())
SHA = 'ecdff91c57a0b9ab82e89634df87e293d259a3aa'
DOCKERFILE = """FROM busybox
CMD /bin/true"""
body = {'receive_user': 'autotest',
'receive_repo': app_id,
'image': '{app_id}:v2'.format(**locals()),
'stack': 'container',
'sha': SHA,
'dockerfile': DOCKERFILE}
# post the build with the builder auth key
response = self.client.post(url, body,
HTTP_X_DRYCC_BUILDER_AUTH=settings.BUILDER_KEY)
self.assertEqual(response.status_code, 200, response.data)
self.assertIn('release', response.data)
self.assertIn('version', response.data['release'])
# make sure build fields were populated
url = '/v2/apps/{app_id}/builds'.format(**locals())
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertIn('results', response.data)
build = response.data['results'][0]
self.assertEqual(build['sha'], SHA)
self.assertEqual(build['dockerfile'], DOCKERFILE)
# test default container
url = "/v2/apps/{app_id}/pods/cmd".format(**locals())
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertEqual(len(response.data['results']), 1)
container = response.data['results'][0]
self.assertEqual(container['type'], 'cmd')
self.assertEqual(container['release'], 'v2')
# pod name is auto generated so use regex
self.assertRegex(container['name'], app_id + '-cmd-[0-9]{1,10}-[a-z0-9]{5}')
# post the build without an auth token
self.client.credentials()
response = self.client.post(url, body)
self.assertEqual(response.status_code, 401, response.data)
def test_config_hook(self, mock_requests):
"""Test reading Config via an API Hook"""
app_id = self.create_app()
url = '/v2/apps/{app_id}/config'.format(**locals())
response = self.client.get(url)
self.assertEqual(response.status_code, 200, response.data)
self.assertIn('values', response.data)
values = response.data['values']
# prepare the config hook
config = {'username': 'autotest', 'app': app_id}
url = '/v2/hooks/config'.format(**locals())
body = {'receive_user': 'autotest',
'receive_repo': app_id}
# post without an auth token
self.client.credentials()
response = self.client.post(url, body)
self.assertEqual(response.status_code, 401, response.data)
# post with the builder auth key
response = self.client.post(url, body,
HTTP_X_DRYCC_BUILDER_AUTH=settings.BUILDER_KEY)
self.assertEqual(response.status_code, 200, response.data)
self.assertIn('values', response.data)
self.assertEqual(values, response.data['values'])
def test_admin_can_hook(self, mock_requests):
"""Administrator should be able to create build hooks on non-admin apps.
"""
"""Test creating a Push via the API"""
user = User.objects.get(username='autotest2')
token = Token.objects.get(user=user).key
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
app_id = self.create_app()
# prepare a push body
DOCKERFILE = """
FROM busybox
CMD /bin/true
"""
body = {'receive_user': 'autotest',
'receive_repo': app_id,
'image': '{app_id}:v2'.format(**locals()),
'stack': 'container',
'sha': 'ecdff91c57a0b9ab82e89634df87e293d259a3aa',
'dockerfile': DOCKERFILE}
url = '/v2/hooks/build'
response = self.client.post(url, body,
HTTP_X_DRYCC_BUILDER_AUTH=settings.BUILDER_KEY)
self.assertEqual(response.status_code, 200, response.data)
self.assertEqual(response.data['release']['version'], 2)