forked from WebODM/WebODM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
291 lines (227 loc) · 11.5 KB
/
Copy pathtests.py
File metadata and controls
291 lines (227 loc) · 11.5 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
from datetime import timedelta
import requests
from django.test import TestCase
from django.utils import six
import subprocess, time
from django.utils import timezone
from os import path
from .models import ProcessingNode, OFFLINE_MINUTES
from .api_client import ApiClient
from requests.exceptions import ConnectionError
from .exceptions import ProcessingError
from . import status_codes
current_dir = path.dirname(path.realpath(__file__))
class TestClientApi(TestCase):
fixtures = ['test_processingnodes', ]
@classmethod
def setUpClass(cls):
super(TestClientApi, cls).setUpClass()
cls.node_odm = subprocess.Popen(['node', 'index.js', '--port', '11223', '--test'], shell=False, cwd=path.join(current_dir, "external", "node-OpenDroneMap"))
time.sleep(2) # Wait for the server to launch
@classmethod
def tearDownClass(cls):
super(TestClientApi, cls).tearDownClass()
cls.node_odm.terminate()
def setUp(self):
self.api_client = ApiClient("localhost", 11223)
def tearDown(self):
pass
def test_offline_api(self):
api = ApiClient("offline-host", 3000)
self.assertRaises(ConnectionError, api.info)
self.assertRaises(ConnectionError, api.options)
def test_info(self):
info = self.api_client.info()
self.assertTrue(isinstance(info['version'], six.string_types), "Found version string")
self.assertTrue(isinstance(info['taskQueueCount'], int), "Found task queue count")
def test_options(self):
options = self.api_client.options()
self.assertTrue(len(options) > 0, "Found options")
def test_online_processing_node(self):
online_node = ProcessingNode.objects.get(pk=1)
self.assertTrue(str(online_node) == "localhost:11223", "Formatting string works")
self.assertTrue(online_node.last_refreshed == None, "Last refreshed not yet set")
self.assertTrue(len(online_node.available_options) == 0, "Available options not yet set")
self.assertTrue(online_node.api_version == "", "API version is not set")
self.assertTrue(online_node.update_node_info(), "Could update info")
self.assertTrue(online_node.last_refreshed != None, "Last refreshed is set")
self.assertTrue(len(online_node.available_options) > 0, "Available options are set")
self.assertTrue(online_node.api_version != "", "API version is set")
self.assertTrue(isinstance(online_node.get_available_options_json(), six.string_types), "Available options json works")
self.assertTrue(isinstance(online_node.get_available_options_json(pretty=True), six.string_types), "Available options json works with pretty")
def test_offline_processing_node(self):
offline_node = ProcessingNode.objects.get(pk=2)
self.assertFalse(offline_node.update_node_info(), "Could not update info (offline)")
self.assertTrue(offline_node.api_version == "", "API version is not set")
def test_auto_update_node_info(self):
online_node = ProcessingNode.objects.create(hostname="localhost", port=11223)
self.assertTrue(online_node.last_refreshed != None, "Last refreshed info is here (update_node_info() was called)")
def test_client_api_and_task_methods(self):
def wait_for_status(api, uuid, status, num_retries = 10, error_description = "Failed to wait for status"):
retries = 0
while True:
try:
task_info = api.task_info(uuid)
if task_info['status']['code'] == status:
return True
except ProcessingError:
pass
time.sleep(0.5)
retries += 1
if retries >= num_retries:
self.assertTrue(False, error_description)
return False
api = ApiClient("localhost", 11223)
online_node = ProcessingNode.objects.get(pk=1)
# Can call info(), options()
self.assertTrue(type(api.info()['version']) == str)
self.assertTrue(len(api.options()) > 0)
# Can call new_task()
import glob
res = api.new_task(
glob.glob("nodeodm/fixtures/test_images/*.JPG"),
"test",
[{'name': 'force-ccd', 'value': 6.16}])
uuid = res['uuid']
self.assertTrue(uuid != None)
# Can call task_info()
task_info = api.task_info(uuid)
self.assertTrue(isinstance(task_info['dateCreated'], int))
self.assertTrue(isinstance(task_info['uuid'], str))
# Can download assets?
# Here we are waiting for the task to be completed
wait_for_status(api, uuid, status_codes.COMPLETED, 10, "Could not download assets")
asset = api.task_download(uuid, "all.zip")
self.assertTrue(isinstance(asset, requests.Response))
# task_output
self.assertTrue(isinstance(api.task_output(uuid, 0), list))
self.assertTrue(isinstance(online_node.get_task_console_output(uuid, 0), str))
self.assertRaises(ProcessingError, online_node.get_task_console_output, "wrong-uuid", 0)
# Can restart task
self.assertTrue(online_node.restart_task(uuid))
self.assertRaises(ProcessingError, online_node.restart_task, "wrong-uuid")
wait_for_status(api, uuid, status_codes.COMPLETED, 10, "Could not restart task")
# Can restart task by passing options
self.assertTrue(online_node.restart_task(uuid, [{'name': 'mesh-size', 'value': 12345},
{'name': 'invalid', 'value': True}]))
wait_for_status(api, uuid, status_codes.COMPLETED, 10, "Could not restart task with options")
# Verify that options have been updated after restarting the task
task_info = api.task_info(uuid)
self.assertTrue(len(task_info['options']) == 1)
self.assertTrue(task_info['options'][0]['name'] == 'mesh-size')
self.assertTrue(task_info['options'][0]['value'] == 12345)
# Can cancel task (should work even if we completed the task)
self.assertTrue(online_node.cancel_task(uuid))
self.assertRaises(ProcessingError, online_node.cancel_task, "wrong-uuid")
# Wait for task to be canceled
wait_for_status(api, uuid, status_codes.CANCELED, 5, "Could not remove task")
self.assertTrue(online_node.remove_task(uuid))
self.assertRaises(ProcessingError, online_node.remove_task, "wrong-uuid")
# Cannot delete task again
self.assertRaises(ProcessingError, online_node.remove_task, uuid)
# Task has been deleted
self.assertRaises(ProcessingError, online_node.get_task_info, uuid)
def test_find_best_available_node_and_is_online(self):
# Fixtures are all offline
self.assertTrue(ProcessingNode.find_best_available_node() is None)
# Bring one online
pnode = ProcessingNode.objects.get(pk=1)
self.assertFalse(pnode.is_online())
pnode.last_refreshed = timezone.now()
pnode.queue_count = 2
pnode.save()
self.assertTrue(pnode.is_online())
self.assertTrue(ProcessingNode.find_best_available_node().id == pnode.id)
# Bring another online with lower queue count
another_pnode = ProcessingNode.objects.get(pk=2)
another_pnode.last_refreshed = pnode.last_refreshed
another_pnode.queue_count = 1
another_pnode.save()
self.assertTrue(ProcessingNode.find_best_available_node().id == another_pnode.id)
# Bring it offline
another_pnode.last_refreshed -= timedelta(minutes=OFFLINE_MINUTES)
another_pnode.save()
self.assertFalse(another_pnode.is_online())
# Best choice now is original processing node
self.assertTrue(ProcessingNode.find_best_available_node().id == pnode.id)
def test_token_auth(self):
node_odm = subprocess.Popen(
['node', 'index.js', '--port', '11224', '--token', 'test_token', '--test'], shell=False,
cwd=path.join(current_dir, "external", "node-OpenDroneMap"))
time.sleep(2)
def wait_for_status(api, uuid, status, num_retries=10, error_description="Failed to wait for status"):
retries = 0
while True:
try:
task_info = api.task_info(uuid)
if task_info['status']['code'] == status:
return True
except ProcessingError:
pass
time.sleep(0.5)
retries += 1
if retries >= num_retries:
self.assertTrue(False, error_description)
return False
api = ApiClient("localhost", 11224, "test_token")
online_node = ProcessingNode.objects.get(pk=3)
self.assertTrue(online_node.update_node_info(), "Could update info")
# Can always call info(), options() (even without valid tokens)
api.token = "invalid"
self.assertTrue(type(api.info()['version']) == str)
self.assertTrue(len(api.options()) > 0)
# Cannot call new_task() without token
import glob
res = api.new_task(
glob.glob("nodeodm/fixtures/test_images/*.JPG"))
self.assertTrue('error' in res)
# Can call new_task() with token
api.token = "test_token"
res = api.new_task(
glob.glob("nodeodm/fixtures/test_images/*.JPG"))
self.assertTrue('uuid' in res)
self.assertFalse('error' in res)
uuid = res['uuid']
# Can call task_info() with token
task_info = api.task_info(uuid)
self.assertTrue(isinstance(task_info['dateCreated'], int))
# Cannot call task_info() without token
api.token = "invalid"
res = api.task_info(uuid)
self.assertTrue('error' in res)
self.assertTrue('token does not match' in res['error'])
# Here we are waiting for the task to be completed
api.token = "test_token"
wait_for_status(api, uuid, status_codes.COMPLETED, 10, "Could not download assets")
# Cannot download assets without token
api.token = "invalid"
res = api.task_download(uuid, "all.zip")
self.assertTrue('error' in res)
api.token = "test_token"
asset = api.task_download(uuid, "all.zip")
self.assertTrue(isinstance(asset, requests.Response))
# Cannot get task output without token
api.token = "invalid"
res = api.task_output(uuid, 0)
self.assertTrue('error' in res)
api.token = "test_token"
res = api.task_output(uuid, 0)
self.assertTrue(isinstance(res, list))
# Cannot restart task without token
online_node.token = "invalid"
self.assertRaises(ProcessingError, online_node.restart_task, uuid)
online_node.token = "test_token"
self.assertTrue(online_node.restart_task(uuid))
# Cannot cancel task without token
online_node.token = "invalid"
self.assertRaises(ProcessingError, online_node.cancel_task, uuid)
online_node.token = "test_token"
self.assertTrue(online_node.cancel_task(uuid))
# Wait for task to be canceled
wait_for_status(api, uuid, status_codes.CANCELED, 5, "Could not cancel task")
# Cannot delete task without token
online_node.token = "invalid"
self.assertRaises(ProcessingError, online_node.remove_task, "invalid token")
online_node.token = "test_token"
self.assertTrue(online_node.remove_task(uuid))
node_odm.terminate();