-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest_resolwe_runtime_utils.py
964 lines (853 loc) · 36.6 KB
/
test_resolwe_runtime_utils.py
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
# Copyright 2019 Genialis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import os
import shutil
import socket
import sys
import tempfile
import time
# pylint: disable=missing-docstring
from io import StringIO
from pathlib import Path
from threading import Thread
from unittest import TestCase
from unittest.mock import patch
import requests
import responses
from resolwe_runtime_utils import (
ImportedFormat,
_re_annotate_entity_main,
_re_checkrc_main,
_re_error_main,
_re_export_main,
_re_info_main,
_re_progress_main,
_re_save_dir_list_main,
_re_save_dir_main,
_re_save_file_list_main,
_re_save_file_main,
_re_save_list_main,
_re_save_main,
_re_warning_main,
annotate_entity,
checkrc,
error,
export_file,
import_file,
info,
progress,
save,
save_dir,
save_dir_list,
save_file,
save_file_list,
save_list,
send_message,
warning,
)
TEST_ROOT = os.path.abspath(os.path.dirname(__file__))
class ResolweRuntimeUtilsTestCase(TestCase):
def assertJSONEqual(self, json_, expected_json): # pylint: disable=invalid-name
self.assertEqual(json.loads(json_), json.loads(expected_json))
class TestAnnotate(ResolweRuntimeUtilsTestCase):
def test_annotation(self):
expected = {'type': 'COMMAND', 'type_data': 'annotate', 'data': {'foo': 0}}
self.assertEqual(annotate_entity('foo', '0'), expected)
class TestSave(ResolweRuntimeUtilsTestCase):
def test_number(self):
expected = {'type': 'COMMAND', 'type_data': 'update_output', 'data': {'foo': 0}}
self.assertEqual(save('foo', '0'), expected)
def test_quote(self):
expected = {
'type': 'COMMAND',
'type_data': 'update_output',
'data': {'foo': '"'},
}
self.assertEqual(save('foo', '"'), expected)
def test_string(self):
expected = {
'type': 'COMMAND',
'type_data': 'update_output',
'data': {'bar': 'baz'},
}
self.assertEqual(save('bar', 'baz'), expected)
expected["data"] = {'proc.warning': 'Warning foo'}
self.assertEqual(save('proc.warning', 'Warning foo'), expected)
expected["data"] = {'number': "0"}
self.assertEqual(save('number', '"0"'), expected)
@patch('resolwe_runtime_utils.collect_entry', return_value=(1, 1))
def test_hash(self, collect_mock):
expected = {
'type': 'COMMAND',
'type_data': 'update_output',
'data': {'etc': {'file': 'foo.py', "size": 1, "total_size": 2}},
}
self.assertEqual(save('etc', '{"file": "foo.py"}'), expected)
def test_improper_input(self):
self.assertRaises(TypeError, save, 'proc.rc')
self.assertRaises(TypeError, save, 'proc.rc', '0', 'Foo')
# NOTE: If a user doesn't put a JSON hash in single-quotes (''), then
# Bash will split it into multiple arguments as shown with the test
# case below.
self.assertRaises(TypeError, save, 'etc', '{file:', 'foo.py}')
class TestExport(ResolweRuntimeUtilsTestCase):
@patch('os.path.isfile', return_value=True)
def test_filename(self, isfile_mock):
expected = {'type': 'COMMAND', 'type_data': 'export_files', 'data': ['foo.txt']}
self.assertEqual(export_file('foo.txt'), expected)
@patch('os.path.isfile', return_value=False)
def test_missing_file(self, isfile_mock):
expected = {
'type': 'COMMAND',
'type_data': 'process_log',
'data': {'error': "Referenced file does not exist: 'foo.txt'."},
}
self.assertEqual(
export_file('foo.txt'),
expected,
)
def test_many_filenames(self):
self.assertRaises(TypeError, export_file, 'etc', 'foo.txt', 'bar.txt')
class TestSaveList(ResolweRuntimeUtilsTestCase):
def test_paths(self):
expected = {
'type': 'COMMAND',
'type_data': 'update_output',
'data': {'src': ['file1.txt', 'file 2.txt']},
}
self.assertEqual(save_list('src', 'file1.txt', 'file 2.txt'), expected)
def test_urls(self):
expected = {
'type': 'COMMAND',
'type_data': 'update_output',
'data': {
'urls': [
{'name': 'View', 'url': 'https://www.google.com'},
{'name': 'View', 'url': 'https://www.genialis.com'},
]
},
}
self.assertEqual(
save_list(
'urls',
'{"name": "View", "url": "https://www.google.com"}',
'{"name": "View", "url": "https://www.genialis.com"}',
),
expected,
)
class TestSaveFile(ResolweRuntimeUtilsTestCase):
@patch('resolwe_runtime_utils.Path')
@patch('resolwe_runtime_utils.collect_entry', return_value=(42, 0))
@patch('os.path.isfile', return_value=True)
def test_file(self, isfile_mock, collect_mock, path_mock):
path_mock.is_file.return_value = True
expected = {
'type': 'COMMAND',
'type_data': 'update_output',
'data': {'etc': {'file': 'foo.py', 'size': 42, 'total_size': 42}},
}
self.assertEqual(save_file('etc', 'foo.py'), expected)
expected["data"]["etc"]["file"] = "foo bar.py"
self.assertEqual(save_file('etc', 'foo bar.py'), expected)
@patch('resolwe_runtime_utils.Path')
@patch('resolwe_runtime_utils.collect_entry', return_value=(42, 0))
@patch('os.path.isfile', return_value=True)
def test_file_with_refs(self, isfile_mock, collect_mock, path_mock):
expected = {
'type': 'COMMAND',
'type_data': 'update_output',
'data': {
'etc': {
'file': 'foo.py',
'size': 42,
'total_size': 42,
'refs': ('ref1.txt', 'ref2.txt'),
}
},
}
self.assertEqual(
save_file('etc', 'foo.py', 'ref1.txt', 'ref2.txt'),
expected,
)
def test_missing_file(self):
expected = {
'type': 'COMMAND',
'type_data': 'process_log',
'data': {'error': "Output 'etc' set to a missing file: 'foo.py'."},
}
self.assertEqual(
save_file('etc', 'foo.py'),
expected,
)
expected = {
'type': 'COMMAND',
'type_data': 'process_log',
'data': {'error': "Output 'etc' set to a missing file: 'foo bar.py'."},
}
self.assertEqual(save_file('etc', 'foo bar.py'), expected)
@patch('resolwe_runtime_utils.Path')
@patch('os.path.isfile', side_effect=[False, False])
def test_file_with_missing_refs(self, isfile_mock, path_mock):
path_mock.is_file.return_value = True
expected = {
'type': 'COMMAND',
'type_data': 'process_log',
'data': {
'error': "Output 'src' set to missing references: 'ref1.gz, ref2.gz'."
},
}
self.assertEqual(save_file('src', 'foo.py', 'ref1.gz', 'ref2.gz'), expected)
def test_improper_input(self):
self.assertRaises(TypeError, save_file, 'etc')
class TestSaveFileList(ResolweRuntimeUtilsTestCase):
@patch('resolwe_runtime_utils.collect_entry', side_effect=[(1, 0), (2, 0), (3, 0)])
@patch('resolwe_runtime_utils.Path')
def test_files(self, path_mock, collect_mock):
path_mock.is_file.return_value = True
expected = {
'type': 'COMMAND',
'type_data': 'update_output',
'data': {
'src': [
{'file': 'foo.py', 'size': 1, 'total_size': 1},
{'file': 'bar 2.py', 'size': 2, 'total_size': 2},
{'file': 'baz/3.py', 'size': 3, 'total_size': 3},
]
},
}
self.assertEqual(
save_file_list('src', 'foo.py', 'bar 2.py', 'baz/3.py'), expected
)
@patch('resolwe_runtime_utils.collect_entry', side_effect=[(1, 1), (2, 0)])
@patch('resolwe_runtime_utils.Path')
@patch('os.path.isfile', return_value=True)
def test_file_with_refs(self, isfile_mock, path_mock, collect_mock):
path_mock.is_file.return_value = True
expected = {
'type': 'COMMAND',
'type_data': 'update_output',
'data': {
'src': [
{
'file': 'foo.py',
'size': 1,
'total_size': 2,
'refs': ['ref1.gz', 'ref2.gz'],
},
{'file': 'bar.py', 'size': 2, 'total_size': 2},
]
},
}
self.assertEqual(
save_file_list('src', 'foo.py:ref1.gz,ref2.gz', 'bar.py'), expected
)
def test_missing_file(self):
expected = {
'type': 'COMMAND',
'type_data': 'process_log',
'data': {'error': "Output 'src' set to a missing file: 'foo.py'."},
}
self.assertEqual(
save_file_list('src', 'foo.py', 'bar 2.py', 'baz/3.py'), expected
)
def test_missing_file_with_refs(self):
expected = {
'type': 'COMMAND',
'type_data': 'process_log',
'data': {'error': "Output 'src' set to a missing file: 'foo.py'."},
}
self.assertEqual(
save_file_list('src', 'foo.py:ref1.gz,ref2.gz', 'bar.py'), expected
)
@patch('resolwe_runtime_utils.collect_entry', side_effect=[(0, 0)])
@patch('resolwe_runtime_utils.Path')
@patch('os.path.isfile', side_effect=[False, False])
def test_file_with_missing_refs(self, isfile_mock, path_mock, collect_mock):
path_mock.is_file.return_value = True
expected = {
'type': 'COMMAND',
'type_data': 'process_log',
'data': {
'error': "Output 'src' set to missing references: 'ref1.gz, ref2.gz'."
},
}
self.assertEqual(save_file_list('src', 'foo.py:ref1.gz,ref2.gz'), expected)
def test_files_invalid_format(self):
expected = {
'type': 'COMMAND',
'type_data': 'process_log',
'data': {'error': "Only one colon ':' allowed in file-refs specification."},
}
self.assertEqual(
save_file_list('src', 'foo.py:ref1.gz:ref2.gz', 'bar.py'),
expected,
)
class TestSaveDir(ResolweRuntimeUtilsTestCase):
@patch('resolwe_runtime_utils.collect_entry', side_effect=[(42, 0), (42, 0)])
@patch('resolwe_runtime_utils.Path')
def test_dir(self, path_mock, collect_mock):
expected = {
'type': 'COMMAND',
'type_data': 'update_output',
'data': {'etc': {'dir': 'foo', 'size': 42, 'total_size': 42}},
}
self.assertEqual(save_dir('etc', 'foo'), expected)
expected = {
'type': 'COMMAND',
'type_data': 'update_output',
'data': {'etc': {'dir': 'foo bar', 'size': 42, 'total_size': 42}},
}
self.assertEqual(save_dir('etc', 'foo bar'), expected)
@patch('resolwe_runtime_utils.collect_entry', return_value=(42, 0))
@patch('resolwe_runtime_utils.Path')
@patch('os.path.isdir', return_value=True)
def test_dir_with_refs(self, isdir_mock, path_mock, collect_mock):
expected = {
'type': 'COMMAND',
'type_data': 'update_output',
'data': {
'etc': {
'dir': 'foo',
'size': 42,
'total_size': 42,
'refs': ('ref1.txt', 'ref2.txt'),
}
},
}
self.assertEqual(save_dir('etc', 'foo', 'ref1.txt', 'ref2.txt'), expected)
def test_missing_dir(self):
expected = {
'type': 'COMMAND',
'type_data': 'process_log',
'data': {'error': "Output 'etc' set to a missing directory: 'foo'."},
}
self.assertEqual(save_dir('etc', 'foo'), expected)
expected = {
'type': 'COMMAND',
'type_data': 'process_log',
'data': {'error': "Output 'etc' set to a missing directory: 'foo bar'."},
}
self.assertEqual(save_dir('etc', 'foo bar'), expected)
@patch('resolwe_runtime_utils.collect_entry', return_value=(42, 0))
@patch('resolwe_runtime_utils.Path')
@patch('os.path.isdir', side_effect=[False, False])
def test_dir_with_missing_refs(self, isdir_mock, path_mock, collect_mock):
expected = {
'type': 'COMMAND',
'type_data': 'process_log',
'data': {
'error': "Output 'etc' set to missing references: 'ref1.gz, ref2.gz'."
},
}
self.assertEqual(save_dir('etc', 'foo', 'ref1.gz', 'ref2.gz'), expected)
def test_improper_input(self):
self.assertRaises(TypeError, save_dir, 'etc')
class TestSaveDirList(ResolweRuntimeUtilsTestCase):
@patch('resolwe_runtime_utils.collect_entry', side_effect=[(1, 0), (2, 0), (3, 0)])
@patch('resolwe_runtime_utils.Path')
@patch('os.path.isdir', return_value=True)
def test_dirs(self, isdir_mock, path_mock, collect_mock):
expected = {
'type': 'COMMAND',
'type_data': 'update_output',
'data': {
'src': [
{'dir': 'dir1', 'size': 1, 'total_size': 1},
{'dir': 'dir 2', 'size': 2, 'total_size': 2},
{'dir': 'dir/3', 'size': 3, 'total_size': 3},
]
},
}
self.assertEqual(save_dir_list('src', 'dir1', 'dir 2', 'dir/3'), expected)
@patch('resolwe_runtime_utils.collect_entry', side_effect=[(1, 0), (2, 0)])
@patch('resolwe_runtime_utils.Path')
@patch('os.path.isdir', return_value=True)
def test_dir_with_refs(self, isdir_mock, path_mock, collect_mock):
expected = {
'type': 'COMMAND',
'type_data': 'update_output',
'data': {
'src': [
{
'dir': 'dir1',
'size': 1,
'total_size': 1,
'refs': ['ref1.gz', 'ref2.gz'],
},
{'dir': 'dir2', 'size': 2, 'total_size': 2},
]
},
}
self.assertEqual(save_dir_list('src', 'dir1:ref1.gz,ref2.gz', 'dir2'), expected)
def test_missing_dir(self):
expected = {
'type': 'COMMAND',
'type_data': 'process_log',
'data': {'error': "Output 'src' set to a missing directory: 'dir1'."},
}
self.assertEqual(save_dir_list('src', 'dir1', 'dir 2', 'dir/3'), expected)
@patch('resolwe_runtime_utils.Path')
@patch('os.path.isdir', side_effect=[False, False])
def test_dir_with_missing_refs(self, isdir_mock, path_mock):
expected = {
'type': 'COMMAND',
'type_data': 'process_log',
'data': {
'error': "Output 'src' set to missing references: 'ref1.gz, ref2.gz'."
},
}
self.assertEqual(save_dir_list('src', 'dir:ref1.gz,ref2.gz'), expected)
def test_files_invalid_format(self):
expected = {
'type': 'COMMAND',
'type_data': 'process_log',
'data': {'error': "Only one colon ':' allowed in dir-refs specification."},
}
self.assertEqual(
save_dir_list('src', 'dir1:ref1.bar:ref2.bar', 'dir2'), expected
)
class TestInfo(ResolweRuntimeUtilsTestCase):
def test_string(self):
expected = {
'type': 'COMMAND',
'type_data': 'process_log',
'data': {'info': 'Some info'},
}
self.assertEqual(info('Some info'), expected)
def test_improper_input(self):
self.assertRaises(TypeError, info, 'First', 'Second')
class TestWarning(ResolweRuntimeUtilsTestCase):
def test_string(self):
expected = {
'type': 'COMMAND',
'type_data': 'process_log',
'data': {'warning': 'Some warning'},
}
self.assertEqual(warning('Some warning'), expected)
def test_improper_input(self):
self.assertRaises(TypeError, warning, 'First', 'Second')
class TestError(ResolweRuntimeUtilsTestCase):
def test_string(self):
expected = {
'type': 'COMMAND',
'type_data': 'process_log',
'data': {'error': 'Some error'},
}
self.assertEqual(error('Some error'), expected)
def test_improper_input(self):
self.assertRaises(TypeError, error, 'First', 'Second')
class TestProgress(ResolweRuntimeUtilsTestCase):
def test_number(self):
expected = {'type': 'COMMAND', 'type_data': 'progress', 'data': 10}
self.assertEqual(progress(0.1), expected)
expected = {'type': 'COMMAND', 'type_data': 'progress', 'data': 0}
self.assertEqual(progress(0), expected)
expected = {'type': 'COMMAND', 'type_data': 'progress', 'data': 100}
self.assertEqual(progress(1), expected)
def test_string(self):
expected = {'type': 'COMMAND', 'type_data': 'progress', 'data': 10}
self.assertEqual(progress('0.1'), expected)
expected = {'type': 'COMMAND', 'type_data': 'progress', 'data': 0}
self.assertEqual(progress('0'), expected)
expected = {'type': 'COMMAND', 'type_data': 'progress', 'data': 100}
self.assertEqual(progress('1'), expected)
def test_bool(self):
expected = {'type': 'COMMAND', 'type_data': 'progress', 'data': 100}
self.assertEqual(progress(True), expected)
def test_improper_input(self):
expected = {
'type': 'COMMAND',
'type_data': 'process_log',
'data': {'warning': 'Progress must be a float.'},
}
self.assertEqual(progress(None), expected)
self.assertEqual(progress('one'), expected)
self.assertEqual(progress('[0.1]'), expected)
expected = {
'type': 'COMMAND',
'type_data': 'process_log',
'data': {'warning': 'Progress must be a float between 0 and 1.'},
}
self.assertEqual(progress(-1), expected)
self.assertEqual(progress(1.1), expected)
self.assertEqual(progress('1.1'), expected)
class TestCheckRC(ResolweRuntimeUtilsTestCase):
def test_valid_integers(self):
expected = {'type': 'COMMAND', 'type_data': 'update_rc', 'data': {'rc': 0}}
self.assertEqual(checkrc(0), expected)
self.assertEqual(checkrc(2, 2, 'Error'), expected)
expected = {
'type': 'COMMAND',
'type_data': 'update_rc',
'data': {'rc': 1, 'error': 'Error'},
}
self.assertEqual(checkrc(1, 2, 'Error'), expected)
expected = {'type': 'COMMAND', 'type_data': 'update_rc', 'data': {'rc': 0}}
self.assertEqual(checkrc(2, 2), expected)
expected = {'type': 'COMMAND', 'type_data': 'update_rc', 'data': {'rc': 1}}
self.assertEqual(checkrc(1, 2), expected)
def test_valid_strings(self):
expected = {'type': 'COMMAND', 'type_data': 'update_rc', 'data': {'rc': 0}}
self.assertEqual(checkrc('0'), expected)
self.assertEqual(checkrc('2', '2', 'Error'), expected)
expected = {
'type': 'COMMAND',
'type_data': 'update_rc',
'data': {'rc': 1, 'error': 'Error'},
}
self.assertEqual(checkrc('1', '2', 'Error'), expected)
def test_error_message_not_string(self):
expected = {
'type': 'COMMAND',
'type_data': 'update_rc',
'data': {'rc': 1, 'error': ['Error']},
}
self.assertEqual(checkrc(1, ['Error']), expected)
def test_improper_input(self):
expected = {
'type': 'COMMAND',
'type_data': 'process_log',
'data': {'error': "Invalid return code: 'None'."},
}
self.assertEqual(checkrc(None), expected)
self.assertEqual(
checkrc(1, None, 'Error'),
expected,
)
expected = {
'type': 'COMMAND',
'type_data': 'process_log',
'data': {'error': "Invalid return code: 'foo'."},
}
self.assertEqual(checkrc('foo'), expected)
self.assertEqual(
checkrc(1, 'foo', 'Error'),
expected,
)
class SendMessageTest(TestCase):
def test_send_message(self):
def _receive(server_socket, result):
header_size = 8
response = {'type_data': 'OK'}
message_body = json.dumps(response).encode()
message_header = len(message_body).to_bytes(header_size, byteorder="big")
message = message_header + message_body
connection = sock.accept()[0]
received = b""
header_length = int.from_bytes(
connection.recv(header_size), byteorder="big"
)
received = connection.recv(header_length)
connection.send(message)
result.append(received)
result = []
test_message = "Test data"
temp_dir = tempfile.mkdtemp()
try:
socket_path = os.path.join(temp_dir, "socket.s")
with patch("resolwe_runtime_utils.COMMUNICATOR_SOCKET", socket_path):
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.bind(socket_path)
sock.listen(1)
server_thread = Thread(target=_receive, args=(sock, result))
server_thread.start()
send_message(test_message)
server_thread.join()
finally:
shutil.rmtree(temp_dir)
self.assertEqual(test_message, json.loads(result[0].decode()))
class ImportFileTestCase(TestCase):
_test_data_dir = os.path.join(TEST_ROOT, '.test_data')
def setUp(self):
# Clean after terminated tests
shutil.rmtree(self._test_data_dir, ignore_errors=True)
os.mkdir(self._test_data_dir)
os.chdir(self._test_data_dir)
def tearDown(self):
os.chdir(TEST_ROOT)
shutil.rmtree(self._test_data_dir)
def _file(self, path):
"""Add path prefix to test file."""
return os.path.join(TEST_ROOT, 'test_files', path)
def assertImportFile(self, src, dst, returned_name):
# Test both
src = self._file(src)
file_name = import_file(src, dst)
assert file_name == returned_name
assert os.path.exists(returned_name), "file not found"
assert os.path.exists(returned_name + '.gz'), "file not found"
os.remove(returned_name)
os.remove(returned_name + '.gz')
# Test extracted
file_name = import_file(src, dst, ImportedFormat.EXTRACTED)
assert file_name == returned_name
assert os.path.exists(returned_name), "file not found"
assert not os.path.exists(returned_name + '.gz'), "file should not exist"
os.remove(returned_name)
# Test compressed
file_name = import_file(src, dst, ImportedFormat.COMPRESSED)
assert file_name == returned_name + '.gz'
assert os.path.exists(returned_name + '.gz'), "file not found"
assert not os.path.exists(returned_name), "file should not exist"
os.remove(returned_name + '.gz')
@patch('resolwe_runtime_utils.send_message', side_effect=lambda x: x)
def test_uncompressed(self, send_mock):
self.assertImportFile(
'some file.1.txt', 'test uncompressed.txt', 'test uncompressed.txt'
)
@patch('resolwe_runtime_utils.send_message', side_effect=lambda x: x)
def test_gz(self, send_mock):
self.assertImportFile('some file.1.txt.gz', 'test gz.txt.gz', 'test gz.txt')
@patch('resolwe_runtime_utils.send_message', side_effect=lambda x: x)
def test_7z(self, send_mock):
self.assertImportFile('some file.1.txt.zip', 'test 7z.txt.zip', 'test 7z.txt')
file_name = import_file(self._file('some folder.tar.gz'), 'some folder.tar.gz')
assert file_name == 'some folder'
assert os.path.isdir('some folder'), "directory not found"
assert os.path.exists('some folder.tar.gz'), "file not found"
shutil.rmtree('some folder')
os.remove('some folder.tar.gz')
file_name = import_file(
self._file('some folder.tar.gz'),
'some folder.tar.gz',
ImportedFormat.COMPRESSED,
)
assert file_name == 'some folder.tar.gz'
assert not os.path.isdir('some folder'), "directory should not exist"
assert os.path.exists('some folder.tar.gz'), "file not found"
file_name = import_file(self._file('some folder 1.zip'), 'some folder 1.zip')
assert file_name == 'some folder 1'
assert os.path.isdir('some folder 1'), "directory not found"
assert os.path.exists('some folder 1.tar.gz'), "file not found"
shutil.rmtree('some folder 1')
os.remove('some folder 1.tar.gz')
file_name = import_file(
self._file('some folder 1.zip'),
'some folder 1.zip',
ImportedFormat.COMPRESSED,
)
assert file_name == 'some folder 1.tar.gz'
assert not os.path.isdir('some folder 1'), "directory should not exist"
assert os.path.exists('some folder 1.tar.gz'), "file not found"
@patch('resolwe_runtime_utils.send_message', side_effect=lambda x: x)
def test_7z_corrupted(self, send_mock):
with self.assertRaises(ValueError, msg='failed to extract file: corrupted.zip'):
import_file(self._file('corrupted.zip'), 'corrupted.zip')
@patch('resolwe_runtime_utils.send_message', side_effect=lambda x: x)
def test_gz_corrupted(self, send_mock):
with self.assertRaises(
ValueError, msg='invalid gzip file format: corrupted.gz'
):
import_file(self._file('corrupted.gz'), 'corrupted.gz')
with self.assertRaises(
ValueError, msg='invalid gzip file format: corrupted.gz'
):
import_file(
self._file('corrupted.gz'), 'corrupted.gz', ImportedFormat.COMPRESSED
)
@responses.activate
@patch('resolwe_runtime_utils.send_message', side_effect=lambda x: x)
def test_uncompressed_url(self, send_mock):
responses.add(
responses.GET, 'https://testurl/someslug', status=200, body='some text'
)
import_file('https://testurl/someslug', 'test uncompressed.txt')
assert os.path.exists('test uncompressed.txt'), "file not found"
assert os.path.exists('test uncompressed.txt.gz'), "file not found"
@responses.activate
@patch('resolwe_runtime_utils.send_message', side_effect=lambda x: x)
def test_gz_url(self, send_mock):
# Return gzipped file
responses.add(
responses.GET,
'https://testurl/someslug',
status=200,
body=bytes.fromhex(
'1f8b0808a6cea15c0003736f6d652066696c652e312e747874002bcecf4d'
'552849ad28e10200bce62c190a000000'
),
)
import_file('https://testurl/someslug', 'test uncompressed.txt.gz')
assert os.path.exists('test uncompressed.txt'), "file not found"
assert os.path.exists('test uncompressed.txt.gz'), "file not found"
@responses.activate
@patch('resolwe_runtime_utils.send_message', side_effect=lambda x: x)
def test_7z_url(self, send_mock):
# Return zipped file
responses.add(
responses.GET,
'http://testurl/someslug',
status=200,
body=bytes.fromhex(
'504b03041400080008000c5b814e0000000000000000000000000f001000'
'736f6d652066696c652e312e74787455580c009fdaa15cc7d8a15cf50114'
'002bcecf4d552849ad28e10200504b0708bce62c190c0000000a00000050'
'4b010215031400080008000c5b814ebce62c190c0000000a0000000f000c'
'000000000000000040a48100000000736f6d652066696c652e312e747874'
'555808009fdaa15cc7d8a15c504b05060000000001000100490000005900'
'00000000'
),
)
import_file('http://testurl/someslug', 'test uncompressed.txt.zip')
assert os.path.exists('test uncompressed.txt'), "file not found"
assert os.path.exists('test uncompressed.txt.gz'), "file not found"
@patch('resolwe_runtime_utils.send_message', side_effect=lambda x: x)
def test_invalid_url(self, send_mock):
with self.assertRaises(requests.exceptions.ConnectionError):
import_file('http://testurl/someslug', 'test uncompressed.txt.zip')
class TestConsoleCommands(ResolweRuntimeUtilsTestCase):
@patch('resolwe_runtime_utils.send_message', side_effect=lambda x: print(x))
@patch('sys.stdout', new_callable=StringIO)
def test_re_annotate(self, stdout_mock, send_mock):
with patch.object(sys, 'argv', ['_', 'foo.bar', '2']):
_re_annotate_entity_main()
self.assertEqual(
stdout_mock.getvalue(),
"{'type': 'COMMAND', 'type_data': 'annotate', 'data': {'foo.bar': 2}}\n",
)
@patch('resolwe_runtime_utils.send_message', side_effect=lambda x: print(x))
@patch('sys.stdout', new_callable=StringIO)
def test_error_handling(self, stdout_mock, send_mock):
with patch.object(sys, 'argv', ['re-save', 'test', '123', 'test', '345']):
_re_save_main()
self.assertEqual(
stdout_mock.getvalue(),
"{\'type\': \'COMMAND\', \'type_data\': \'process_log\', \'data\': {\'error\': \"Unexpected error in \'re-save\': save() takes 2 positional arguments but 4 were given\"}}\n",
)
@patch('resolwe_runtime_utils.send_message', side_effect=lambda x: print(x))
@patch('sys.stdout', new_callable=StringIO)
def test_re_save(self, stdout_mock, send_mock):
send_mock.side_effect = lambda x: print(x)
with patch.object(sys, 'argv', ['_', 'foo.bar', '2']):
_re_save_main()
self.assertEqual(
stdout_mock.getvalue(),
"{'type': 'COMMAND', 'type_data': 'update_output', 'data': {'foo.bar': 2}}\n",
)
@patch('resolwe_runtime_utils.send_message', side_effect=lambda x: print(x))
@patch('os.path.isfile', return_value=True)
@patch('sys.stdout', new_callable=StringIO)
def test_re_export(self, stdout_mock, isfile_mock, send_mock):
with patch.object(sys, 'argv', ['_', 'foo.bar']):
_re_export_main()
self.assertEqual(
stdout_mock.getvalue(),
"{'type': 'COMMAND', 'type_data': 'export_files', 'data': ['foo.bar']}\n",
)
@patch('resolwe_runtime_utils.send_message', side_effect=lambda x: print(x))
@patch('sys.stdout', new_callable=StringIO)
def test_re_save_list(self, stdout_mock, send_mock):
with patch.object(sys, 'argv', ['_', 'foo.bar', '2', 'baz']):
_re_save_list_main()
self.assertEqual(
stdout_mock.getvalue(),
"{'type': 'COMMAND', 'type_data': 'update_output', 'data': {'foo.bar': [2, 'baz']}}\n",
)
@patch('resolwe_runtime_utils.collect_entry', return_value=(42, 0))
@patch('resolwe_runtime_utils.send_message', side_effect=lambda x: print(x))
@patch('resolwe_runtime_utils.Path')
@patch('sys.stdout', new_callable=StringIO)
def test_re_save_file(self, stdout_mock, path_mock, send_mock, collect_mock):
with patch.object(sys, 'argv', ['_', 'foo.bar', 'baz.py']):
_re_save_file_main()
self.assertEqual(
stdout_mock.getvalue(),
"{'type': 'COMMAND', 'type_data': 'update_output', 'data': {'foo.bar': {'file': 'baz.py', 'size': 42, 'total_size': 42}}}\n",
)
@patch('resolwe_runtime_utils.collect_entry', return_value=(42, 0))
@patch('resolwe_runtime_utils.send_message', side_effect=lambda x: print(x))
@patch('resolwe_runtime_utils.Path')
@patch('sys.stdout', new_callable=StringIO)
def test_re_save_file_list(self, stdout_mock, path_mock, send_mock, collect_mock):
path_mock.is_file.return_value = True
with patch.object(sys, 'argv', ['_', 'foo.bar', 'baz.py', 'baz 2.py']):
_re_save_file_list_main()
self.assertEqual(
stdout_mock.getvalue(),
"{'type': 'COMMAND', 'type_data': 'update_output', 'data': {'foo.bar': [{'file': 'baz.py', 'size': 42, 'total_size': 42}, {'file': 'baz 2.py', 'size': 42, 'total_size': 42}]}}\n",
)
@patch('resolwe_runtime_utils.collect_entry', return_value=(42, 0))
@patch('resolwe_runtime_utils.send_message', side_effect=lambda x: print(x))
@patch('resolwe_runtime_utils.Path')
@patch('os.path.isdir', return_value=True)
@patch('sys.stdout', new_callable=StringIO)
def test_re_save_dir(
self, stdout_mock, isdir_mock, path_mock, send_mock, collect_mock
):
with patch.object(sys, 'argv', ['_', 'foo.bar', 'baz']):
_re_save_dir_main()
self.assertEqual(
stdout_mock.getvalue(),
"{'type': 'COMMAND', 'type_data': 'update_output', 'data': {'foo.bar': {'dir': 'baz', 'size': 42, 'total_size': 42}}}\n",
)
@patch('resolwe_runtime_utils.collect_entry', return_value=(42, 0))
@patch('resolwe_runtime_utils.send_message', side_effect=lambda x: print(x))
@patch('resolwe_runtime_utils.Path')
@patch('os.path.isdir', return_value=True)
@patch('sys.stdout', new_callable=StringIO)
def test_re_save_dir_list(
self, stdout_mock, isfile_mock, path_mock, send_mock, collect_mock
):
with patch.object(sys, 'argv', ['_', 'foo.bar', 'baz', 'baz 2']):
_re_save_dir_list_main()
self.assertEqual(
stdout_mock.getvalue(),
"{'type': 'COMMAND', 'type_data': 'update_output', 'data': {'foo.bar': [{'dir': 'baz', 'size': 42, 'total_size': 42}, {'dir': 'baz 2', 'size': 42, 'total_size': 42}]}}\n",
)
@patch('resolwe_runtime_utils.send_message', side_effect=lambda x: print(x))
@patch('sys.stdout', new_callable=StringIO)
def test_re_info(self, stdout_mock, send_mock):
with patch.object(sys, 'argv', ['_', 'some info']):
_re_info_main()
self.assertEqual(
stdout_mock.getvalue(),
"{'type': 'COMMAND', 'type_data': 'process_log', 'data': {'info': 'some info'}}\n",
)
@patch('resolwe_runtime_utils.send_message', side_effect=lambda x: print(x))
@patch('sys.stdout', new_callable=StringIO)
def test_re_warning(self, stdout_mock, send_mock):
with patch.object(sys, 'argv', ['_', 'some warning']):
_re_warning_main()
self.assertEqual(
stdout_mock.getvalue(),
"{'type': 'COMMAND', 'type_data': 'process_log', 'data': {'warning': 'some warning'}}\n",
)
@patch('resolwe_runtime_utils.send_message', side_effect=lambda x: print(x))
@patch('sys.stdout', new_callable=StringIO)
def test_re_error(self, stdout_mock, send_mock):
with patch.object(sys, 'argv', ['_', 'some error']):
_re_error_main()
self.assertEqual(
stdout_mock.getvalue(),
"{'type': 'COMMAND', 'type_data': 'process_log', 'data': {'error': 'some error'}}\n",
)
@patch('resolwe_runtime_utils.send_message', side_effect=lambda x: print(x))
@patch('sys.stdout', new_callable=StringIO)
def test_re_progress(self, stdout_mock, send_mock):
with patch.object(sys, 'argv', ['_', '0.7']):
_re_progress_main()
self.assertEqual(
stdout_mock.getvalue(),
"{'type': 'COMMAND', 'type_data': 'progress', 'data': 70}\n",
)
@patch('resolwe_runtime_utils.send_message', side_effect=lambda x: print(x))
@patch('sys.stdout', new_callable=StringIO)
def test_re_checkrc(self, stdout_mock, send_mock):
with patch.object(sys, 'argv', ['_', '1', '2', 'error']):
_re_checkrc_main()
self.assertEqual(
stdout_mock.getvalue(),
"{'type': 'COMMAND', 'type_data': 'update_rc', 'data': {'rc': 1, 'error': 'error'}}\n",
)