-
Notifications
You must be signed in to change notification settings - Fork 12
/
test_integration.py
833 lines (733 loc) · 34 KB
/
test_integration.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
import os
import ssl
import sys
import tempfile
from doctest import testfile
from io import StringIO, TextIOWrapper
from unittest import SkipTest, TestCase
from unittest.mock import Mock, patch
from urllib3.exceptions import LocationParseError
from crate.client.exceptions import ProgrammingError
from crate.crash.command import (
CrateShell,
_create_shell,
get_lines_from_stdin,
get_parser,
host_and_port,
main,
noargs_command,
)
from crate.crash.commands import Command
from crate.crash.outputs import _val_len as val_len
from crate.crash.printer import ColorPrinter
from crate.testing.layer import CrateLayer
from tests import ftouch
if sys.platform != "linux":
raise SkipTest("Integration tests only supported on Linux")
crate_version = os.getenv("CRATEDB_VERSION", "5.5.0")
crate_http_port = 44209
crate_transport_port = 44309
crate_settings = {
'cluster.name': 'Testing44209',
'node.name': 'crate',
'psql.port': 45441,
'lang.js.enabled': True,
'http.port': crate_http_port,
'transport.tcp.port': crate_transport_port
}
node = CrateLayer.from_uri(
f'https://cdn.crate.io/downloads/releases/cratedb/x64_linux/crate-{crate_version}.tar.gz',
'crate',
settings=crate_settings
)
def setUpModule():
node.start()
def tearDownModule():
node.stop()
def fake_stdin(data):
stdin = TextIOWrapper(tempfile.TemporaryFile())
stdin.write(data)
stdin.flush()
stdin.seek(0)
return stdin
class RaiseOnceSideEffect:
"""
A callable class used for mock side_effect.
The side effect raises an exception once, every subsequent call invokes the
original method.
"""
def __init__(self, exception, original):
self.exception = exception
self.original = original
self.raised = False
def __call__(self, *args, **kwargs):
if not self.raised:
self.raised = True
raise self.exception
return self.original(*args, **kwargs)
class DocumentationTest(TestCase):
def test_output(self):
testfile('../crate/crash/output.txt')
def test_connect(self):
testfile('../crate/crash/connect.txt')
class CommandTest(TestCase):
def _output_format(self, format, func, query="select name from sys.cluster"):
orig_argv = sys.argv[:]
try:
sys.argv = ["testcrash",
"-c", query,
"--hosts", node.http_url,
'--format', format
]
with patch('sys.stdout', new_callable=StringIO) as output:
with patch('sys.stderr', new_callable=StringIO) as err:
try:
main()
except SystemExit as e:
func(self, e, output, err)
finally:
sys.argv = orig_argv
def test_val_len(self):
self.assertEqual(val_len(1), 1)
self.assertEqual(val_len(12), 2)
self.assertEqual(val_len('123'), 3)
self.assertEqual(val_len(True), 4)
self.assertEqual(val_len(None), 4)
self.assertEqual(val_len([1, 2, 3]), 9)
self.assertEqual(val_len({'key': 'val'}), 14)
def test_tabulate_output(self):
def assert_func(self, e, output, err):
exception_code = e.code
self.assertEqual(exception_code, 0)
output = output.getvalue()
self.assertIn('| name |', output)
self.assertIn('| Testing44209 |', output)
self._output_format('tabular', assert_func)
def test_json_output(self):
def assert_func(self, e, output, err):
exception_code = e.code
self.assertEqual(exception_code, 0)
output = output.getvalue()
self.assertIn('"name": "Testing44209"', output)
self._output_format('json', assert_func)
def test_json_row_output(self):
query = "select table_name from information_schema.tables where table_name = 'cluster'"
def assert_func(self, e, output, err):
exception_code = e.code
self.assertEqual(exception_code, 0)
output = output.getvalue()
self.assertIn('{"table_name": "cluster"}', output)
self._output_format('json_row', assert_func, query)
def test_csv_obj_output(self):
query = "select name, port from sys.nodes limit 1"
def assert_func(self, e, output, err):
exception_code = e.code
self.assertEqual(exception_code, 0)
output = output.getvalue()
self.assertIn("""crate,'{"http": 44209, "psql": 45441, "transport": 44309}'""", output)
self._output_format('csv', assert_func, query)
def test_csv_array_output(self):
query = "select ['/dev/', 'foo'] as arr"
def assert_func(self, e, output, err):
exception_code = e.code
self.assertEqual(exception_code, 0)
output = output.getvalue()
self.assertIn('["/dev/', output)
self._output_format('csv', assert_func, query)
def test_raw_output(self):
def assert_func(self, e, output, err):
exception_code = e.code
self.assertEqual(exception_code, 0)
output = output.getvalue()
self.assertIn('"duration":', output)
self.assertIn('"rowcount":', output)
self.assertIn('"rows":', output)
self.assertIn('"cols":', output)
self._output_format('raw', assert_func)
def test_mixed_output(self):
def assert_func(self, e, output, err):
exception_code = e.code
self.assertEqual(exception_code, 0)
output = output.getvalue()
self.assertIn("name | Testing44209", output)
self._output_format('mixed', assert_func)
def test_pprint_duplicate_keys(self):
"Output: table with duplicate keys"
expected = "\n".join(["+------+------+",
"| name | name |",
"+------+------+",
"+------+------+\n"])
with CrateShell() as cmd:
with patch('sys.stdout', new_callable=StringIO) as output:
cmd.pprint([], ['name', 'name'])
self.assertEqual(expected, output.getvalue())
def test_pprint_dont_guess_type(self):
"Output: Numeric looking strings should still be processed as strings"
expected = "\n".join(["+---------+",
"| version |",
"+---------+",
"| 0.50 |",
"+---------+\n"])
with CrateShell() as cmd:
with patch('sys.stdout', new_callable=StringIO) as output:
cmd.pprint([["0.50"]], ['version'])
self.assertEqual(expected, output.getvalue())
@patch('sys.stdin', fake_stdin(u"select 'via-stdin' from sys.cluster"))
def test_stdin_cmd(self):
"Test passing in SQL via stdin"
try:
orig_argv = sys.argv[:]
tmphistory = tempfile.mkstemp()[1]
sys.argv = ['testcrash',
'--hosts', node.http_url,
'--history', tmphistory]
with patch('sys.stdout', new_callable=StringIO) as output:
try:
main()
except SystemExit as e:
exception_code = e.code
self.assertEqual(exception_code, 0)
output = output.getvalue()
self.assertIn('via-stdin', output)
finally:
try:
os.remove(tmphistory)
except IOError:
pass
sys.argv = orig_argv
sys.stdin.close()
@patch('sys.stdin', fake_stdin(u"select 'via-stdin' from sys.cluster"))
def test_cmd_precedence(self):
"""Test precedence of SQL passed in via -c vs. stdin
SQL passed in via --command should take precedence
over stdin
"""
try:
stmt = u"select 'via-command' from information_schema.tables limit 1"
orig_argv = sys.argv[:]
tmphistory = tempfile.mkstemp()[1]
sys.argv = ['testcrash',
"--command", stmt,
'--hosts', node.http_url,
'--history', tmphistory]
with patch('sys.stdout', new_callable=StringIO) as output:
try:
main()
except SystemExit as e:
exception_code = e.code
self.assertEqual(exception_code, 0)
output = output.getvalue()
self.assertIn('via-command', output)
self.assertNotIn('via-stdin', output)
finally:
try:
os.remove(tmphistory)
except IOError:
pass
sys.argv = orig_argv
sys.stdin.close()
def test_multiple_hosts(self):
orig_argv = sys.argv[:]
try:
tmphistory = tempfile.mkstemp()[1]
sys.argv = ["testcrash",
"-c", "select * from sys.cluster",
"--hosts", node.http_url, "127.0.0.1:1",
'--history', tmphistory,
'--format', 'tabular',
'-v',
]
with patch('sys.stdout', new_callable=StringIO) as output:
try:
main()
except SystemExit as e:
exception_code = e.code
self.assertEqual(exception_code, 0)
output = output.getvalue()
lines = output.split('\n')
self.assertRegex(lines[3], r'^\| http://[\d\.:]+ .*\| NULL .*\| FALSE .*\| Server not available')
self.assertRegex(lines[4], r'^\| http://[\d\.:]+. *\| crate .*\| TRUE .*\| OK')
finally:
try:
os.remove(tmphistory)
except IOError:
pass
sys.argv = orig_argv
def test_cmd_line_sys_info(self):
sys.argv = ["testcrash",
"--hosts", node.http_url,
"--sysinfo"
]
with patch('sys.stdout', new_callable=StringIO):
try:
main()
except SystemExit as e:
self.assertEqual(e.code, 0)
@patch('sys.stdin', fake_stdin('\n'.join(["create table test(",
"d string",
")",
"clustered into 2 shards",
"with (number_of_replicas=0)"])))
def test_multiline_stdin(self):
"""Test pass multiline statement via stdin
Newlines must be replaced with whitespaces
"""
stmt = ''.join(list(get_lines_from_stdin())).replace('\n', ' ')
expected = ("create table test( d string ) "
"clustered into 2 shards "
"with (number_of_replicas=0)")
try:
self.assertEqual(stmt, expected)
finally:
sys.stdin.close()
@patch('sys.stdin', fake_stdin('\n'.join(["create table test(",
"d string",
")",
"clustered into 2 shards",
"with (number_of_replicas=0);"])))
def test_multiline_stdin_delimiter(self):
"""Test pass multiline statement with delimiter via stdin
Newlines must be replaced with whitespaces
"""
stmt = ''.join(list(get_lines_from_stdin())).replace('\n', ' ')
expected = ("create table test( d string ) "
"clustered into 2 shards "
"with (number_of_replicas=0);")
try:
self.assertEqual(stmt, expected)
finally:
sys.stdin.close()
def test_tabulate_null_int_column(self):
"""
Create a column with a non-string value and NULL.
"""
rows = [[1], [None]]
expected = "\n".join(['+------+',
'| x |',
'+------+',
'| 1 |',
'| NULL |',
'+------+\n'])
with CrateShell() as cmd:
with patch('sys.stdout', new_callable=StringIO) as output:
cmd.pprint(rows, cols=['x'])
self.assertEqual(expected, output.getvalue())
def test_tabulate_boolean_int_column(self):
"""
Create another column with a non-string value and FALSE.
"""
rows = [['FALSE'], [1]]
expected = "\n".join(['+-------+',
'| x |',
'+-------+',
'| FALSE |',
'| 1 |',
'+-------+\n'])
with CrateShell() as cmd:
with patch('sys.stdout', new_callable=StringIO) as output:
cmd.pprint(rows, cols=['x'])
self.assertEqual(expected, output.getvalue())
def test_multiline_header(self):
"""
Create a column with newline characters.
"""
rows = [['FALSE'], [1]]
expected = "\n".join(['+-------+',
'| x |',
'| y |',
'+-------+',
'| FALSE |',
'| 1 |',
'+-------+\n'])
with CrateShell() as cmd:
with patch('sys.stdout', new_callable=StringIO) as output:
cmd.pprint(rows, cols=['x\ny'])
self.assertEqual(expected, output.getvalue())
def test_multiline_row(self):
"""
Create a column that holds rows with multiline text.
"""
self.maxDiff = None
rows = [['create table foo (\n id integer,\n name string\n)', 'foo\nbar', 1]]
expected = "\n".join(['+-----------------------+-----+---+',
'| show create table foo | a | b |',
'+-----------------------+-----+---+',
'| create table foo ( | foo | 1 |',
'| id integer, | bar | |',
'| name string | | |',
'| ) | | |',
'+-----------------------+-----+---+\n'])
with CrateShell() as cmd:
with patch('sys.stdout', new_callable=StringIO) as output:
cmd.pprint(rows, cols=['show create table foo', 'a', 'b'])
self.assertEqual(expected, output.getvalue())
def test_tabulate_empty_line(self):
self.maxDiff = None
rows = (
['Aldebaran', 'Star System'],
['Berlin', 'City'],
['Galactic Sector QQ7 Active J Gamma', 'Galaxy'],
['', 'Planet']
)
expected = "\n".join(['+------------------------------------+-------------+',
'| min(name) | kind |',
'+------------------------------------+-------------+',
'| Aldebaran | Star System |',
'| Berlin | City |',
'| Galactic Sector QQ7 Active J Gamma | Galaxy |',
'| | Planet |',
'+------------------------------------+-------------+\n'])
with CrateShell() as cmd:
with patch('sys.stdout', new_callable=StringIO) as output:
cmd.pprint(rows, cols=['min(name)', 'kind'])
# assert 0
self.assertEqual(expected, output.getvalue())
def test_empty_line_first_row_first_column(self):
self.maxDiff = None
rows = (
['', 'Planet'],
['Aldebaran', 'Star System'],
['Berlin', 'City'],
['Galactic Sector QQ7 Active J Gamma', 'Galaxy']
)
expected = "\n".join(['+------------------------------------+-------------+',
'| min(name) | kind |',
'+------------------------------------+-------------+',
'| | Planet |',
'| Aldebaran | Star System |',
'| Berlin | City |',
'| Galactic Sector QQ7 Active J Gamma | Galaxy |',
'+------------------------------------+-------------+\n'])
with CrateShell() as cmd:
with patch('sys.stdout', new_callable=StringIO) as output:
cmd.pprint(rows, cols=['min(name)', 'kind'])
self.assertEqual(expected, output.getvalue())
def test_empty_first_row(self):
self.maxDiff = None
rows = (
['', ''],
['Aldebaran', 'Aldebaran'],
['Algol', 'Algol'],
['Allosimanius Syneca', 'Allosimanius - Syneca'],
['Alpha Centauri', 'Alpha - Centauri']
)
expected = "\n".join(['+---------------------+-----------------------+',
'| name | replaced |',
'+---------------------+-----------------------+',
'| | |',
'| Aldebaran | Aldebaran |',
'| Algol | Algol |',
'| Allosimanius Syneca | Allosimanius - Syneca |',
'| Alpha Centauri | Alpha - Centauri |',
'+---------------------+-----------------------+\n'])
with CrateShell() as cmd:
with patch('sys.stdout', new_callable=StringIO) as output:
cmd.pprint(rows, cols=['name', 'replaced'])
self.assertEqual(expected, output.getvalue())
def test_any_empty(self):
self.maxDiff = None
rows = (
['Features and conformance views', 'FALSE', '', ''],
['Features and conformance views', 'TRUE', 1, 'SQL_FEATURES view'],
['Features and conformance views', 'FALSE', 2, 'SQL_SIZING view'],
['Features and conformance views', 'FALSE', 3, 'SQL_LANGUAGES view']
)
expected = "\n".join(['+--------------------------------+--------------+----------------+--------------------+',
'| feature_name | is_supported | sub_feature_id | sub_feature_name |',
'+--------------------------------+--------------+----------------+--------------------+',
'| Features and conformance views | FALSE | | |',
'| Features and conformance views | TRUE | 1 | SQL_FEATURES view |',
'| Features and conformance views | FALSE | 2 | SQL_SIZING view |',
'| Features and conformance views | FALSE | 3 | SQL_LANGUAGES view |',
'+--------------------------------+--------------+----------------+--------------------+\n'])
with CrateShell() as cmd:
with patch('sys.stdout', new_callable=StringIO) as output:
cmd.pprint(rows, cols=['feature_name', 'is_supported', 'sub_feature_id', 'sub_feature_name'])
self.assertEqual(expected, output.getvalue())
def test_first_column_first_row_empty(self):
self.maxDiff = None
rows = (
['', 1.0],
['Aldebaran', 1.0],
['Algol', 1.0],
['Allosimanius Syneca', 1.0],
['Alpha Centauri', 1.0],
['Argabuthon', 1.0],
['Arkintoofle Minor', 1.0],
['Galactic Sector QQ7 Active J Gamma', 1.0],
['North West Ripple', 1.0],
['Outer Eastern Rim', 1.0],
['NULL', 1.0]
)
expected = "\n".join(['+------------------------------------+--------+',
'| name | _score |',
'+------------------------------------+--------+',
'| | 1.0 |',
'| Aldebaran | 1.0 |',
'| Algol | 1.0 |',
'| Allosimanius Syneca | 1.0 |',
'| Alpha Centauri | 1.0 |',
'| Argabuthon | 1.0 |',
'| Arkintoofle Minor | 1.0 |',
'| Galactic Sector QQ7 Active J Gamma | 1.0 |',
'| North West Ripple | 1.0 |',
'| Outer Eastern Rim | 1.0 |',
'| NULL | 1.0 |',
'+------------------------------------+--------+\n'])
with CrateShell() as cmd:
with patch('sys.stdout', new_callable=StringIO) as output:
cmd.pprint(rows, cols=['name', '_score'])
self.assertEqual(expected, output.getvalue())
def test_error_exit_code(self):
"""Test returns an error exit code"""
stmt = u"select * from invalid sql statement"
sys.argv = [
"testcrash",
"--command", stmt,
'--hosts', node.http_url
]
try:
main()
except SystemExit as e:
self.assertEqual(e.code, 1)
def test_verbose_with_error_trace(self):
with CrateShell(error_trace=True) as cmd:
cmd.logger = Mock()
cmd.cursor.execute = Mock(side_effect=ProgrammingError(msg="the error message",
error_trace="error trace"))
cmd._exec_and_print("select invalid statement")
cmd.logger.critical.assert_any_call("the error message")
cmd.logger.critical.assert_called_with("\nerror trace")
def test_verbose_no_error_trace(self):
with CrateShell(error_trace=True) as cmd:
cmd.logger = Mock()
cmd.cursor.execute = Mock(side_effect=ProgrammingError(msg="the error message",
error_trace=None))
cmd._exec_and_print("select invalid statement")
# only the message is logged
cmd.logger.critical.assert_called_once_with("the error message")
def test_rendering_object(self):
"""Test rendering an object"""
user = {'name': 'Arthur', 'age': 42}
expected = "\n".join(['+-------------------------------+',
'| user |',
'+-------------------------------+',
'| {"age": 42, "name": "Arthur"} |',
'+-------------------------------+\n'])
with CrateShell() as cmd:
with patch('sys.stdout', new_callable=StringIO) as output:
cmd.pprint([[user]], ['user'])
self.assertEqual(expected, output.getvalue())
def test_rendering_array(self):
"""Test rendering an array"""
names = ['Arthur', 'Ford']
expected = "\n".join(['+--------------------+',
'| names |',
'+--------------------+',
'| ["Arthur", "Ford"] |',
'+--------------------+\n'])
with CrateShell() as cmd:
with patch('sys.stdout', new_callable=StringIO) as output:
cmd.pprint([[names]], ['names'])
self.assertEqual(expected, output.getvalue())
def test_rendering_float(self):
"""Test rendering an array"""
expected = "\n".join(['+---------------+',
'| number |',
'+---------------+',
'| 3.1415926535 |',
'| 42.0 |',
'+---------------+\n'])
with CrateShell() as cmd:
with patch('sys.stdout', new_callable=StringIO) as output:
cmd.pprint([[3.1415926535], [42.0]], ['number'])
self.assertEqual(expected, output.getvalue())
def test_help_command(self):
"""Test output of help command"""
command = CrateShell(is_tty=False)
expected = "\n".join([
'\\? print this help',
'\\autocapitalize toggle automatic capitalization of SQL keywords',
'\\autocomplete toggle autocomplete',
'\\c connect to the given server, e.g.: \\connect localhost:4200',
'\\check print failed cluster and/or node checks, e.g. \\check nodes',
'\\connect connect to the given server, e.g.: \\connect localhost:4200',
'\\dt print the existing tables within the \'doc\' schema',
'\\format switch output format',
'\\pager set an external pager. Use without argument to reset to internal paging',
'\\q quit crash',
'\\r read and execute statements from a file',
'\\sysinfo print system and cluster info',
'\\verbose toggle verbose mode',
])
help_ = command.commands['?']
self.assertTrue(isinstance(help_, Command))
self.assertEqual(expected, help_(command))
with CrateShell(is_tty=False) as cmd:
output = StringIO()
cmd.logger = ColorPrinter(False, stream=output)
text = help_(cmd, 'arg1', 'arg2')
self.assertEqual(None, text)
self.assertEqual('Command does not take any arguments.\n', output.getvalue())
def test_noargs_decorator(self):
"""Test noargs decorator"""
output = StringIO()
class MyCmd:
logger = ColorPrinter(False, stream=output)
@noargs_command
def my_cmd(self, *args):
return 'awesome'
command = MyCmd()
command.my_cmd()
text = command.my_cmd()
self.assertEqual('awesome', text)
text = command.my_cmd('arg1')
self.assertEqual(None, text)
self.assertEqual('Command does not take any arguments.\n', output.getvalue())
def test_wrong_host_format(self):
parser = get_parser()
args = parser.parse_args([
"--hosts", "localhost:12AB"
])
crate_hosts = [host_and_port(h) for h in args.hosts]
with self.assertRaises(LocationParseError):
_create_shell(crate_hosts, False, None, False, args)
def test_command_timeout(self):
with CrateShell(node.http_url) as crash:
crash.process("""
CREATE FUNCTION fib(long)
RETURNS LONG
LANGUAGE javascript AS '
function fib(n) {
if (n < 2) return 1;
return fib(n - 1) + fib(n - 2);
}'
""")
timeout = 0.1
slow_query = "SELECT fib(35)"
# without verbose
with CrateShell(node.http_url,
error_trace=False,
timeout=timeout) as crash:
crash.logger = Mock()
crash.process(slow_query)
crash.logger.warn.assert_any_call("Use \\connect <server> to connect to one or more servers first.")
# with verbose
with CrateShell(node.http_url,
error_trace=True,
timeout=timeout) as crash:
crash.logger = Mock()
crash.process(slow_query)
crash.logger.warn.assert_any_call("No more Servers available, exception from last server: HTTPConnectionPool(host='127.0.0.1', port=44209): Read timed out. (read timeout=0.1)")
crash.logger.warn.assert_any_call("Use \\connect <server> to connect to one or more servers first.")
def test_username_param(self):
with CrateShell(node.http_url,
username='crate') as crash:
self.assertEqual(crash.username, "crate")
self.assertEqual(crash.connection.client.username, "crate")
def test_ssl_params(self):
tmpdirname = tempfile.mkdtemp()
cert_filename = os.path.join(tmpdirname, "cert_file")
key_filename = os.path.join(tmpdirname, "key_file")
ca_cert_filename = os.path.join(tmpdirname, "ca_cert_file")
ftouch(cert_filename)
ftouch(key_filename)
ftouch(ca_cert_filename)
with CrateShell(node.http_url,
verify_ssl=False,
cert_file=cert_filename,
key_file=key_filename,
ca_cert_file=ca_cert_filename) as crash:
self.assertEqual(crash.verify_ssl, False)
self.assertEqual(crash.connection.client._pool_kw['cert_reqs'], ssl.CERT_NONE)
self.assertEqual(crash.cert_file, cert_filename)
self.assertEqual(crash.connection.client._pool_kw['cert_file'], cert_filename)
self.assertEqual(crash.key_file, key_filename)
self.assertEqual(crash.connection.client._pool_kw['key_file'], key_filename)
self.assertEqual(crash.ca_cert_file, ca_cert_filename)
self.assertEqual(crash.connection.client._pool_kw['ca_certs'], ca_cert_filename)
def test_ssl_params_missing_file(self):
argv = [
"--hosts", node.http_url,
"--verify-ssl", "false",
"--key-file", "wrong_file",
"--ca-cert-file", "ca_cert_file"
]
parser = get_parser()
with self.assertRaises(FileNotFoundError):
parser.parse_args(argv)
def test_ssl_params_wrong_permision_file(self):
tmpdirname = tempfile.mkdtemp()
ca_cert_filename = os.path.join(tmpdirname, "ca_cert_file")
ftouch(ca_cert_filename)
os.chmod(ca_cert_filename, 0000)
argv = [
"--hosts", node.http_url,
"--verify-ssl", "false",
"--ca-cert-file", ca_cert_filename
]
parser = get_parser()
with self.assertRaises(PermissionError):
parser.parse_args(argv)
def test_close_shell(self):
crash = CrateShell(node.http_url)
self.assertFalse(crash.is_closed())
self.assertTrue(crash.is_conn_available())
crash.close()
self.assertTrue(crash.is_closed())
self.assertFalse(crash.is_conn_available())
with self.assertRaises(ProgrammingError) as ctx:
crash.close()
self.assertEqual('CrateShell is already closed',
ctx.exception.message)
def test_connect_info(self):
with CrateShell(node.http_url,
username='crate',
schema='test') as crash:
self.assertEqual(crash.connect_info.user, "crate")
self.assertEqual(crash.connect_info.schema, "test")
self.assertEqual(crash.connect_info.cluster, "Testing44209")
with patch.object(
crash.cursor,
"execute",
side_effect=RaiseOnceSideEffect(
ProgrammingError("SQLActionException[UnsupportedFeatureException]"),
crash.cursor.execute,
)
):
crash._fetch_session_info()
self.assertEqual(crash.connect_info.user, None)
self.assertEqual(crash.connect_info.schema, "test")
self.assertEqual(crash.connect_info.cluster, "Testing44209")
with patch.object(
crash.cursor,
"execute",
side_effect=RaiseOnceSideEffect(
ProgrammingError("SQLActionException[SchemaUnknownException]"),
crash.cursor.execute,
)
):
crash._fetch_session_info()
self.assertEqual(crash.connect_info.user, "crate")
self.assertEqual(crash.connect_info.schema, "test")
self.assertEqual(crash.connect_info.cluster, None)
with patch.object(
crash.cursor,
"execute",
side_effect=RaiseOnceSideEffect(
ProgrammingError("SQLActionException"),
crash.cursor.execute,
)
):
crash._fetch_session_info()
self.assertEqual(crash.connect_info.user, None)
self.assertEqual(crash.connect_info.schema, None)
self.assertEqual(crash.connect_info.cluster, None)
@patch.object(CrateShell, "is_conn_available")
def test_connect_info_not_available(self, is_conn_available):
is_conn_available.return_value = False
with CrateShell(node.http_url,
username='crate',
schema='test') as crash:
self.assertEqual(crash.connect_info.user, None)
self.assertEqual(crash.connect_info.schema, None)