-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathtest_config.py
More file actions
281 lines (264 loc) · 12.8 KB
/
test_config.py
File metadata and controls
281 lines (264 loc) · 12.8 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
"""
Unit tests for the Deis api app.
Run the tests with "./manage.py test api"
"""
from __future__ import unicode_literals
import json
import mock
import requests
from django.test import TransactionTestCase
from django.test.utils import override_settings
from api.models import Config
def mock_import_repository_task(*args, **kwargs):
resp = requests.Response()
resp.status_code = 200
resp._content_consumed = True
return resp
@override_settings(CELERY_ALWAYS_EAGER=True)
class ConfigTest(TransactionTestCase):
"""Tests setting and updating config values"""
fixtures = ['tests.json']
def setUp(self):
self.assertTrue(
self.client.login(username='autotest', password='password'))
body = {'id': 'autotest', 'domain': 'autotest.local', 'type': 'mock',
'hosts': 'host1,host2', 'auth': 'base64string', 'options': {}}
response = self.client.post('/api/clusters', json.dumps(body),
content_type='application/json')
self.assertEqual(response.status_code, 201)
@mock.patch('requests.post', mock_import_repository_task)
def test_config(self):
"""
Test that config is auto-created for a new app and that
config can be updated using a PATCH
"""
url = '/api/apps'
body = {'cluster': 'autotest'}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
app_id = response.data['id']
# check to see that an initial/empty config was created
url = "/api/apps/{app_id}/config".format(**locals())
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertIn('values', response.data)
self.assertEqual(response.data['values'], json.dumps({}))
config1 = response.data
# set an initial config value
body = {'values': json.dumps({'NEW_URL1': 'http://localhost:8080/'})}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
self.assertIn('x-deis-release', response._headers)
config2 = response.data
self.assertNotEqual(config1['uuid'], config2['uuid'])
self.assertIn('NEW_URL1', json.loads(response.data['values']))
# read the config
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
config3 = response.data
self.assertEqual(config2, config3)
self.assertIn('NEW_URL1', json.loads(response.data['values']))
# set an additional config value
body = {'values': json.dumps({'NEW_URL2': 'http://localhost:8080/'})}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
config3 = response.data
self.assertNotEqual(config2['uuid'], config3['uuid'])
self.assertIn('NEW_URL1', json.loads(response.data['values']))
self.assertIn('NEW_URL2', json.loads(response.data['values']))
# read the config again
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
config4 = response.data
self.assertEqual(config3, config4)
self.assertIn('NEW_URL1', json.loads(response.data['values']))
self.assertIn('NEW_URL2', json.loads(response.data['values']))
# unset a config value
body = {'values': json.dumps({'NEW_URL2': None})}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
config5 = response.data
self.assertNotEqual(config4['uuid'], config5['uuid'])
self.assertNotIn('NEW_URL2', json.dumps(response.data['values']))
# unset all config values
body = {'values': json.dumps({'NEW_URL1': None})}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
self.assertNotIn('NEW_URL1', json.dumps(response.data['values']))
# disallow put/patch/delete
self.assertEqual(self.client.put(url).status_code, 405)
self.assertEqual(self.client.patch(url).status_code, 405)
self.assertEqual(self.client.delete(url).status_code, 405)
return config5
@mock.patch('requests.post', mock_import_repository_task)
def test_config_set_same_key(self):
"""
Test that config sets on the same key function properly
"""
url = '/api/apps'
body = {'cluster': 'autotest'}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
app_id = response.data['id']
url = "/api/apps/{app_id}/config".format(**locals())
# set an initial config value
body = {'values': json.dumps({'PORT': '5000'})}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
self.assertIn('PORT', json.loads(response.data['values']))
# reset same config value
body = {'values': json.dumps({'PORT': '5001'})}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
self.assertIn('PORT', json.loads(response.data['values']))
self.assertEqual(json.loads(response.data['values'])['PORT'], '5001')
@mock.patch('requests.post', mock_import_repository_task)
def test_config_str(self):
"""Test the text representation of a node."""
config5 = self.test_config()
config = Config.objects.get(uuid=config5['uuid'])
self.assertEqual(str(config), "{}-{}".format(config5['app'], config5['uuid'][:7]))
@mock.patch('requests.post', mock_import_repository_task)
def test_limit_memory(self):
"""
Test that limit is auto-created for a new app and that
limits can be updated using a PATCH
"""
url = '/api/apps'
body = {'cluster': 'autotest'}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
app_id = response.data['id']
url = '/api/apps/{app_id}/config'.format(**locals())
# check default limit
response = self.client.get(url, content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertIn('memory', response.data)
self.assertEqual(json.loads(response.data['memory']), {})
# regression test for https://github.com/deis/deis/issues/1563
self.assertNotIn('"', response.data['memory'])
# set an initial limit
mem = {'web': '1G'}
body = {'memory': json.dumps(mem)}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
self.assertIn('x-deis-release', response._headers)
limit1 = response.data
# check memory limits
response = self.client.get(url, content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertIn('memory', response.data)
memory = json.loads(response.data['memory'])
self.assertIn('web', memory)
self.assertEqual(memory['web'], '1G')
# set an additional value
body = {'memory': json.dumps({'worker': '512M'})}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
limit2 = response.data
self.assertNotEqual(limit1['uuid'], limit2['uuid'])
memory = json.loads(response.data['memory'])
self.assertIn('worker', memory)
self.assertEqual(memory['worker'], '512M')
self.assertIn('web', memory)
self.assertEqual(memory['web'], '1G')
# read the limit again
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
limit3 = response.data
self.assertEqual(limit2, limit3)
memory = json.loads(response.data['memory'])
self.assertIn('worker', memory)
self.assertEqual(memory['worker'], '512M')
self.assertIn('web', memory)
self.assertEqual(memory['web'], '1G')
# regression test for https://github.com/deis/deis/issues/1613
# ensure that config:set doesn't wipe out previous limits
body = {'values': json.dumps({'NEW_URL2': 'http://localhost:8080/'})}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
self.assertIn('NEW_URL2', json.loads(response.data['values']))
# read the limit again
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
memory = json.loads(response.data['memory'])
self.assertIn('worker', memory)
self.assertEqual(memory['worker'], '512M')
self.assertIn('web', memory)
self.assertEqual(memory['web'], '1G')
# unset a value
body = {'memory': json.dumps({'worker': None})}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
limit4 = response.data
self.assertNotEqual(limit3['uuid'], limit4['uuid'])
self.assertNotIn('worker', json.dumps(response.data['memory']))
# disallow put/patch/delete
self.assertEqual(self.client.put(url).status_code, 405)
self.assertEqual(self.client.patch(url).status_code, 405)
self.assertEqual(self.client.delete(url).status_code, 405)
return limit4
@mock.patch('requests.post', mock_import_repository_task)
def test_limit_cpu(self):
"""
Test that CPU limits can be set
"""
url = '/api/apps'
body = {'cluster': 'autotest'}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
app_id = response.data['id']
url = '/api/apps/{app_id}/config'.format(**locals())
# check default limit
response = self.client.get(url, content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertIn('cpu', response.data)
self.assertEqual(json.loads(response.data['cpu']), {})
# regression test for https://github.com/deis/deis/issues/1563
self.assertNotIn('"', response.data['cpu'])
# set an initial limit
body = {'cpu': json.dumps({'web': '1024'})}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
self.assertIn('x-deis-release', response._headers)
limit1 = response.data
# check memory limits
response = self.client.get(url, content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertIn('cpu', response.data)
cpu = json.loads(response.data['cpu'])
self.assertIn('web', cpu)
self.assertEqual(cpu['web'], '1024')
# set an additional value
body = {'cpu': json.dumps({'worker': '512'})}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
limit2 = response.data
self.assertNotEqual(limit1['uuid'], limit2['uuid'])
cpu = json.loads(response.data['cpu'])
self.assertIn('worker', cpu)
self.assertEqual(cpu['worker'], '512')
self.assertIn('web', cpu)
self.assertEqual(cpu['web'], '1024')
# read the limit again
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
limit3 = response.data
self.assertEqual(limit2, limit3)
cpu = json.loads(response.data['cpu'])
self.assertIn('worker', cpu)
self.assertEqual(cpu['worker'], '512')
self.assertIn('web', cpu)
self.assertEqual(cpu['web'], '1024')
# unset a value
body = {'memory': json.dumps({'worker': None})}
response = self.client.post(url, json.dumps(body), content_type='application/json')
self.assertEqual(response.status_code, 201)
limit4 = response.data
self.assertNotEqual(limit3['uuid'], limit4['uuid'])
self.assertNotIn('worker', json.dumps(response.data['memory']))
# disallow put/patch/delete
self.assertEqual(self.client.put(url).status_code, 405)
self.assertEqual(self.client.patch(url).status_code, 405)
self.assertEqual(self.client.delete(url).status_code, 405)
return limit4