-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_uid.py
More file actions
206 lines (161 loc) · 7.17 KB
/
Copy pathtest_uid.py
File metadata and controls
206 lines (161 loc) · 7.17 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
"""
Tests for App.uid and Workspace.uid:
- uid is generated by get_next_uid() and is unique under concurrency.
- uid is read-only via the REST API (cannot be modified via PATCH/PUT).
"""
from concurrent.futures import ThreadPoolExecutor, as_completed
import requests_mock
from django.contrib.auth import get_user_model
from django.core.cache import cache
from django.db import connection, connections
from api.models.app import App
from api.models.workspace import Workspace, WorkspaceMember
from api.tests import adapter, DryccTestCase, DryccTransactionTestCase
User = get_user_model()
class WorkspaceUidTest(DryccTestCase):
"""uid immutability via REST API for Workspace."""
fixtures = ['tests.json']
def setUp(self):
self.user = User.objects.get(username='autotest')
self.token = self.get_or_create_token(self.user)
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
def tearDown(self):
cache.clear()
def test_workspace_uid_assigned_on_create(self):
response = self.client.post(
'/v2/workspaces', {'id': 'uidws01', 'email': 'uidws01@example.com'},
)
self.assertEqual(response.status_code, 201, response.data)
self.assertIn('uid', response.data)
self.assertIsNotNone(response.data['uid'])
self.assertGreater(response.data['uid'], 0)
def test_workspace_uid_immutable_via_patch(self):
response = self.client.post(
'/v2/workspaces', {'id': 'uidws02', 'email': 'uidws02@example.com'},
)
self.assertEqual(response.status_code, 201, response.data)
original_uid = response.data['uid']
# Attempt to mutate uid via PATCH
response = self.client.patch(
'/v2/workspaces/uidws02',
{'uid': 999999, 'email': 'new@example.com'},
)
self.assertEqual(response.status_code, 200, response.data)
self.assertEqual(response.data['uid'], original_uid)
self.assertEqual(response.data['email'], 'new@example.com')
# Re-fetch and confirm
ws = Workspace.objects.get(id='uidws02')
self.assertEqual(ws.uid, original_uid)
def test_workspace_uid_immutable_via_put(self):
response = self.client.post(
'/v2/workspaces', {'id': 'uidws03', 'email': 'uidws03@example.com'},
)
self.assertEqual(response.status_code, 201, response.data)
original_uid = response.data['uid']
response = self.client.put(
'/v2/workspaces/uidws03',
{'id': 'uidws03', 'uid': 888888, 'email': 'uidws03@example.com'},
)
# PUT may return 200 or 405 depending on viewset config; uid must not change.
ws = Workspace.objects.get(id='uidws03')
self.assertEqual(ws.uid, original_uid)
class WorkspaceUidConcurrencyTest(DryccTransactionTestCase):
"""Concurrency: parallel Workspace.save() must produce unique uids."""
fixtures = ['tests.json']
def tearDown(self):
cache.clear()
# Close any connections opened by worker threads.
connections.close_all()
def test_workspace_uid_unique_under_concurrency(self):
cache.clear()
n = 16
def _create(i):
try:
ws = Workspace.objects.create(
id=f'wsuid{i:03d}', email=f'wsuid{i:03d}@example.com',
)
return ws.uid
finally:
connection.close()
with ThreadPoolExecutor(max_workers=n) as ex:
uids = [f.result() for f in as_completed([ex.submit(_create, i) for i in range(n)])]
self.assertEqual(len(uids), n)
self.assertEqual(len(set(uids)), n, f'duplicate uids detected: {uids}')
self.assertTrue(all(u > 0 for u in uids))
@requests_mock.Mocker(real_http=True, adapter=adapter)
class AppUidTest(DryccTestCase):
"""uid immutability for App.
App.save() requires K8s. We exercise the REST API where possible, and fall
back to ORM + serializer validation for environments without a K8s
serviceaccount token. The serializer marking uid as read-only is the source
of truth for API-level immutability.
"""
fixtures = ['tests.json']
def setUp(self):
self.user = User.objects.get(username='autotest')
self.token = self.get_or_create_token(self.user)
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
self.workspace = Workspace.objects.create(
id='uidappws', email='uidappws@example.com',
)
WorkspaceMember.objects.create(
workspace=self.workspace, user=self.user, role='admin',
)
def tearDown(self):
cache.clear()
def test_app_uid_field_is_read_only_in_serializer(self, mock_requests):
from api.serializers import AppSerializer
self.assertIn('uid', AppSerializer.Meta.read_only_fields)
def test_app_uid_ignored_on_serializer_update(self, mock_requests):
"""Even if a client sends uid in a PATCH body, the serializer drops it."""
from api.utils import get_next_uid
from api.serializers import AppSerializer
# Bypass App.save() (K8s dependency) by inserting via the queryset.
App.objects.bulk_create([
App(id='uidapp01', workspace=self.workspace, uid=get_next_uid(App)),
])
app = App.objects.get(id='uidapp01')
original_uid = app.uid
serializer = AppSerializer(
instance=app,
data={'uid': 999999, 'workspace': self.workspace.id},
partial=True,
)
self.assertTrue(serializer.is_valid(), serializer.errors)
# uid is read-only -> dropped from validated_data, so no update path
# can ever persist a client-supplied uid.
self.assertNotIn('uid', serializer.validated_data)
app.refresh_from_db()
self.assertEqual(app.uid, original_uid)
class AppUidConcurrencyTest(DryccTransactionTestCase):
"""Concurrency: parallel App.uid allocation via get_next_uid must be unique.
App.save() requires K8s; we test the uid allocation pathway directly by
creating App rows with the same code path used by save(): get_next_uid(App)
plus bulk_create to bypass the K8s-dependent App.save() body.
"""
fixtures = ['tests.json']
def setUp(self):
self.user = User.objects.get(username='autotest')
self.workspace = Workspace.objects.create(
id='uidappcon', email='uidappcon@example.com',
)
WorkspaceMember.objects.create(
workspace=self.workspace, user=self.user, role='admin',
)
def tearDown(self):
cache.clear()
connections.close_all()
def test_app_uid_unique_under_concurrency(self):
from api.utils import get_next_uid
cache.clear()
n = 32
def _alloc():
try:
return get_next_uid(App)
finally:
connection.close()
with ThreadPoolExecutor(max_workers=n) as ex:
uids = [f.result() for f in as_completed([ex.submit(_alloc) for _ in range(n)])]
self.assertEqual(len(uids), n)
self.assertEqual(len(set(uids)), n, f'duplicate uids detected: {uids}')
self.assertTrue(all(u > 0 for u in uids))