-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathintegration_test_custom_socket.py
More file actions
249 lines (216 loc) · 8.57 KB
/
integration_test_custom_socket.py
File metadata and controls
249 lines (216 loc) · 8.57 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
# -*- coding: utf-8 -*-
import time
from syncano.models import (
Class,
ClassDependency,
CustomSocket,
Endpoint,
RuntimeChoices,
Script,
ScriptCall,
ScriptDependency,
ScriptEndpoint,
SocketEndpoint
)
from tests.integration_test import InstanceMixin, IntegrationTest
class CustomSocketTest(InstanceMixin, IntegrationTest):
def test_install_custom_socket(self):
# this tests new ScriptEndpoint dependency create;
self.assert_custom_socket('installing', self._define_dependencies_new_script_endpoint)
def test_dependencies_new_script(self):
self.assert_custom_socket('new_script_installing', self._define_dependencies_new_script)
def test_dependencies_existing_script(self):
self.assert_custom_socket('existing_script_installing', self._define_dependencies_existing_script)
def test_dependencies_existing_script_endpoint(self):
self.assert_custom_socket('existing_script_e_installing',
self._define_dependencies_existing_script_endpoint)
def test_creating_raw_data(self):
custom_socket = CustomSocket.please.create(
name='my_custom_socket_123',
endpoints={
"my_custom_endpoint_123": {
"calls": [{"type": "script", "name": "script_123", "methods": ["GET", "POST"]}]
}
},
dependencies=[
{
"type": "script",
"runtime_name": "python_library_v5.0",
"name": "script_123",
"source": "print(123)"
},
{
"type": "class",
"name": "klass",
"schema": [
{"name": "fieldA", "type": "string"},
{"name": "fieldB", "type": "integer"},
]
}
]
)
self.assertTrue(custom_socket.name)
def test_custom_socket_run(self):
suffix = 'default'
custom_socket = self._create_custom_socket(suffix, self._define_dependencies_new_script_endpoint)
self._assert_custom_socket(custom_socket)
results = custom_socket.run('my_endpoint_{}'.format(suffix))
self.assertEqual(results['result']['stdout'], suffix)
def test_custom_socket_recheck(self):
suffix = 'recheck'
custom_socket = self._create_custom_socket(suffix, self._define_dependencies_new_script_endpoint)
self._assert_custom_socket(custom_socket)
custom_socket = custom_socket.recheck()
self._assert_custom_socket(custom_socket)
def test_fetching_all_endpoints(self):
all_endpoints = SocketEndpoint.get_all_endpoints()
self.assertTrue(isinstance(all_endpoints, list))
self.assertTrue(len(all_endpoints) >= 1)
self.assertTrue(all_endpoints[0].name)
def test_endpoint_run(self):
script_endpoint = SocketEndpoint.get_all_endpoints()[0]
result = script_endpoint.run()
self.assertIsInstance(result, dict)
self.assertTrue(result['result']['stdout'])
def test_custom_socket_update(self):
socket_to_update = self._create_custom_socket('to_update', self._define_dependencies_new_script_endpoint)
socket_to_update.remove_endpoint(endpoint_name='my_endpoint_to_update')
new_endpoint = Endpoint(name='my_endpoint_new_to_update')
new_endpoint.add_call(
ScriptCall(name='script_default', methods=['GET'])
)
socket_to_update.add_endpoint(new_endpoint)
socket_to_update.update()
time.sleep(2) # wait for custom socket setup;
socket_to_update.reload()
self.assertIn('my_endpoint_new_to_update', socket_to_update.endpoints)
def test_class_dependency_new(self):
suffix = 'new_class'
custom_socket = self._create_custom_socket(suffix, self._define_dependencies_new_class)
self._assert_custom_socket(custom_socket)
def test_class_dependency_existing(self):
suffix = 'existing_class'
custom_socket = self._create_custom_socket(suffix, self._define_dependencies_new_class)
self._assert_custom_socket(custom_socket)
def assert_custom_socket(self, suffix, dependency_method):
custom_socket = self._create_custom_socket(suffix, dependency_method=dependency_method)
self._assert_custom_socket(custom_socket)
def _assert_custom_socket(self, custom_socket):
self._wait_till_socket_process(custom_socket)
self.assertTrue(custom_socket.name)
self.assertTrue(custom_socket.created_at)
self.assertTrue(custom_socket.updated_at)
@classmethod
def _create_custom_socket(cls, suffix, dependency_method):
custom_socket = CustomSocket(name='my_custom_socket_{}'.format(suffix))
cls._define_endpoints(suffix, custom_socket)
dependency_method(suffix, custom_socket)
custom_socket.install()
return custom_socket
@classmethod
def _define_endpoints(cls, suffix, custom_socket):
endpoint = Endpoint(name='my_endpoint_{}'.format(suffix))
endpoint.add_call(
ScriptCall(
name='script_endpoint_{}'.format(suffix),
methods=['GET', 'POST']
)
)
custom_socket.add_endpoint(endpoint)
@classmethod
def _define_dependencies_new_class(cls, suffix, custom_socket):
cls._add_base_script(suffix, custom_socket)
custom_socket.add_dependency(
ClassDependency(
Class(
name="test_class_{}".format(suffix),
schema=[
{"name": "testA", "type": "string"},
{"name": "testB", "type": "integer"},
]
)
)
)
@classmethod
def _define_dependencies_existing_class(cls, suffix, custom_socket):
cls._add_base_script(suffix, custom_socket)
klass = Class(
name="test_class_{}".format(suffix),
schema=[
{"name": "testA", "type": "string"},
{"name": "testB", "type": "integer"},
]
)
klass.save()
custom_socket.add_dependency(
ClassDependency(
klass
)
)
@classmethod
def _define_dependencies_new_script_endpoint(cls, suffix, custom_socket):
script = cls._create_script(suffix)
script_endpoint = ScriptEndpoint(
name='script_endpoint_{}'.format(suffix),
script=script.id
)
custom_socket.add_dependency(
ScriptDependency(
script_endpoint
)
)
@classmethod
def _define_dependencies_new_script(cls, suffix, custom_socket):
custom_socket.add_dependency(
ScriptDependency(
Script(
source='print("{}")'.format(suffix),
runtime_name=RuntimeChoices.PYTHON_V5_0
),
name='script_endpoint_{}'.format(suffix),
)
)
@classmethod
def _define_dependencies_existing_script(cls, suffix, custom_socket):
# create Script first:
cls._create_script(suffix)
custom_socket.add_dependency(
ScriptDependency(
Script.please.first(),
name='script_endpoint_{}'.format(suffix),
)
)
@classmethod
def _define_dependencies_existing_script_endpoint(cls, suffix, custom_socket):
script = cls._create_script(suffix)
ScriptEndpoint.please.create(
name='script_endpoint_{}'.format(suffix),
script=script.id
)
custom_socket.add_dependency(
ScriptDependency(
ScriptEndpoint.please.first()
)
)
@classmethod
def _add_base_script(cls, suffix, custom_socket):
custom_socket.add_dependency(
ScriptDependency(
Script(
source='print("{}")'.format(suffix),
runtime_name=RuntimeChoices.PYTHON_V5_0
),
name='script_endpoint_{}'.format(suffix),
)
)
@classmethod
def _create_script(cls, suffix):
return Script.please.create(
label='script_{}'.format(suffix),
runtime_name=RuntimeChoices.PYTHON_V5_0,
source='print("{}")'.format(suffix)
)
@classmethod
def _wait_till_socket_process(cls, custom_socket):
while custom_socket.status == 'checking':
custom_socket.reload()