forked from cea-hpc/clustershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCLIConfigTest.py
602 lines (544 loc) · 22.8 KB
/
CLIConfigTest.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
# ClusterShell.CLI.Config test suite
# Written by S. Thiell
"""Unit test for CLI.Config"""
import resource
import os.path
import shutil
import tempfile
from textwrap import dedent
import unittest
from TLib import *
from ClusterShell.CLI.Clush import set_fdlimit
from ClusterShell.CLI.Config import ClushConfig, ClushConfigError
from ClusterShell.CLI.Display import *
from ClusterShell.CLI.OptionParser import OptionParser
class CLIClushConfigTest(unittest.TestCase):
"""This test case performs a complete CLI.Config.ClushConfig
verification. Also CLI.OptionParser is used and some parts are
verified btw.
"""
def testClushConfigEmpty(self):
"""test CLI.Config.ClushConfig (empty)"""
f = tempfile.NamedTemporaryFile(prefix='testclushconfig')
f.write(b"\n")
parser = OptionParser("dummy")
parser.install_clush_config_options()
parser.install_display_options(verbose_options=True)
parser.install_connector_options()
options, _ = parser.parse_args([])
config = ClushConfig(options, filename=f.name)
self.assertEqual(config.color, THREE_CHOICES[0])
self.assertEqual(config.verbosity, VERB_STD)
self.assertEqual(config.fanout, 64)
self.assertEqual(config.maxrc, False)
self.assertEqual(config.node_count, True)
self.assertEqual(config.connect_timeout, 10)
self.assertEqual(config.command_timeout, 0)
self.assertEqual(config.ssh_user, None)
self.assertEqual(config.ssh_path, None)
self.assertEqual(config.ssh_options, None)
f.close()
def testClushConfigAlmostEmpty(self):
"""test CLI.Config.ClushConfig (almost empty)"""
f = tempfile.NamedTemporaryFile(prefix='testclushconfig')
f.write("[Main]\n".encode())
parser = OptionParser("dummy")
parser.install_clush_config_options()
parser.install_display_options(verbose_options=True)
parser.install_connector_options()
options, _ = parser.parse_args([])
config = ClushConfig(options, filename=f.name)
self.assertEqual(config.color, THREE_CHOICES[0])
self.assertEqual(config.verbosity, VERB_STD)
self.assertEqual(config.maxrc, False)
self.assertEqual(config.node_count, True)
self.assertEqual(config.fanout, 64)
self.assertEqual(config.connect_timeout, 10)
self.assertEqual(config.command_timeout, 0)
self.assertEqual(config.ssh_user, None)
self.assertEqual(config.ssh_path, None)
self.assertEqual(config.ssh_options, None)
f.close()
def testClushConfigDefault(self):
"""test CLI.Config.ClushConfig (default)"""
f = tempfile.NamedTemporaryFile(prefix='testclushconfig')
f.write(dedent("""
[Main]
fanout: 42
connect_timeout: 14
command_timeout: 0
history_size: 100
color: auto
verbosity: 1
#ssh_user: root
#ssh_path: /usr/bin/ssh
#ssh_options: -oStrictHostKeyChecking=no""").encode())
f.flush()
parser = OptionParser("dummy")
parser.install_clush_config_options()
parser.install_display_options(verbose_options=True)
parser.install_connector_options()
options, _ = parser.parse_args([])
config = ClushConfig(options, filename=f.name)
display = Display(options, config)
display.vprint(VERB_STD, "test")
display.vprint(VERB_DEBUG, "shouldn't see this")
self.assertEqual(config.color, THREE_CHOICES[-1])
self.assertEqual(config.verbosity, VERB_STD)
self.assertEqual(config.maxrc, False)
self.assertEqual(config.node_count, True)
self.assertEqual(config.fanout, 42)
self.assertEqual(config.connect_timeout, 14)
self.assertEqual(config.command_timeout, 0)
self.assertEqual(config.ssh_user, None)
self.assertEqual(config.ssh_path, None)
self.assertEqual(config.ssh_options, None)
f.close()
def testClushConfigFull(self):
"""test CLI.Config.ClushConfig (full)"""
f = tempfile.NamedTemporaryFile(prefix='testclushconfig')
f.write(dedent("""
[Main]
fanout: 42
connect_timeout: 14
command_timeout: 0
history_size: 100
color: auto
maxrc: yes
node_count: yes
verbosity: 1
ssh_user: root
ssh_path: /usr/bin/ssh
ssh_options: -oStrictHostKeyChecking=no
""").encode())
f.flush()
parser = OptionParser("dummy")
parser.install_clush_config_options()
parser.install_display_options(verbose_options=True)
parser.install_connector_options()
options, _ = parser.parse_args([])
config = ClushConfig(options, filename=f.name)
self.assertEqual(config.color, THREE_CHOICES[-1])
self.assertEqual(config.verbosity, VERB_STD)
self.assertEqual(config.maxrc, True)
self.assertEqual(config.node_count, True)
self.assertEqual(config.fanout, 42)
self.assertEqual(config.connect_timeout, 14)
self.assertEqual(config.command_timeout, 0)
self.assertEqual(config.ssh_user, "root")
self.assertEqual(config.ssh_path, "/usr/bin/ssh")
self.assertEqual(config.ssh_options, "-oStrictHostKeyChecking=no")
f.close()
def testClushConfigError(self):
"""test CLI.Config.ClushConfig (error)"""
f = tempfile.NamedTemporaryFile(prefix='testclushconfig')
f.write(dedent("""
[Main]
fanout: 3.2
connect_timeout: foo
command_timeout: bar
history_size: 100
color: maybe
node_count: 3
verbosity: bar
ssh_user: root
ssh_path: /usr/bin/ssh
ssh_options: -oStrictHostKeyChecking=no
""").encode())
f.flush()
parser = OptionParser("dummy")
parser.install_clush_config_options()
parser.install_display_options(verbose_options=True)
parser.install_connector_options()
options, _ = parser.parse_args([])
config = ClushConfig(options, filename=f.name)
try:
c = config.color
self.fail("Exception ClushConfigError not raised (color)")
except ClushConfigError:
pass
self.assertEqual(config.verbosity, 0) # probably for compatibility
try:
f = config.fanout
self.fail("Exception ClushConfigError not raised (fanout)")
except ClushConfigError:
pass
try:
f = config.node_count
self.fail("Exception ClushConfigError not raised (node_count)")
except ClushConfigError:
pass
try:
f = config.fanout
except ClushConfigError as e:
self.assertEqual(str(e)[0:20], "(Config Main.fanout)")
try:
t = config.connect_timeout
self.fail("Exception ClushConfigError not raised (connect_timeout)")
except ClushConfigError:
pass
try:
m = config.command_timeout
self.fail("Exception ClushConfigError not raised (command_timeout)")
except ClushConfigError:
pass
f.close()
def testClushConfigSetRlimit(self):
"""test CLI.Config.ClushConfig (setrlimit)"""
soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
hard2 = min(32768, hard)
f = tempfile.NamedTemporaryFile(prefix='testclushconfig')
f.write(dedent("""
[Main]
fanout: 42
connect_timeout: 14
command_timeout: 0
history_size: 100
color: auto
fd_max: %d
verbosity: 1
""" % hard2).encode())
f.flush()
parser = OptionParser("dummy")
parser.install_clush_config_options()
parser.install_display_options(verbose_options=True)
parser.install_connector_options()
options, _ = parser.parse_args([])
config = ClushConfig(options, filename=f.name)
display = Display(options, config)
# force a lower soft limit
resource.setrlimit(resource.RLIMIT_NOFILE, (hard2//2, hard))
# max_fdlimit should increase soft limit again
set_fdlimit(config.fd_max, display)
# verify
soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
self.assertEqual(soft, hard2)
f.close()
def testClushConfigSetRlimitValueError(self):
"""test CLI.Config.ClushConfig (setrlimit ValueError)"""
soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
f = tempfile.NamedTemporaryFile(prefix='testclushconfig')
f.write(dedent("""
[Main]
fanout: 42
connect_timeout: 14
command_timeout: 0
history_size: 100
color: auto
# Use wrong fd_max value to generate ValueError
fd_max: -1
verbosity: 1""").encode())
f.flush()
parser = OptionParser("dummy")
parser.install_clush_config_options()
parser.install_display_options(verbose_options=True)
parser.install_connector_options()
options, _ = parser.parse_args([])
config = ClushConfig(options, filename=f.name)
f.close()
display = Display(options, config)
class TestException(Exception): pass
def mock_vprint_err(level, message):
if message.startswith('Warning: Failed to set max open files'):
raise TestException()
display.vprint_err = mock_vprint_err
self.assertRaises(TestException, set_fdlimit, config.fd_max, display)
soft2, _ = resource.getrlimit(resource.RLIMIT_NOFILE)
self.assertEqual(soft, soft2)
def testClushConfigDefaultWithOptions(self):
"""test CLI.Config.ClushConfig (default with options)"""
f = tempfile.NamedTemporaryFile(prefix='testclushconfig')
f.write(dedent("""
[Main]
fanout: 42
connect_timeout: 14
command_timeout: 0
history_size: 100
color: auto
verbosity: 1""").encode())
f.flush()
parser = OptionParser("dummy")
parser.install_clush_config_options()
parser.install_display_options(verbose_options=True)
parser.install_connector_options()
options, _ = parser.parse_args(["-f", "36", "-u", "3", "-t", "7",
"--user", "foobar", "--color",
"always", "-d", "-v", "-q", "-o",
"-oSomething"])
config = ClushConfig(options, filename=f.name)
display = Display(options, config)
display.vprint(VERB_STD, "test")
display.vprint(VERB_DEBUG, "test")
self.assertEqual(config.color, THREE_CHOICES[2])
self.assertEqual(config.verbosity, VERB_DEBUG) # takes biggest
self.assertEqual(config.fanout, 36)
self.assertEqual(config.connect_timeout, 7)
self.assertEqual(config.command_timeout, 3)
self.assertEqual(config.ssh_user, "foobar")
self.assertEqual(config.ssh_path, None)
self.assertEqual(config.ssh_options, "-oSomething")
f.close()
def testClushConfigWithInstalledConfig(self):
"""test CLI.Config.ClushConfig (installed config required)"""
# This test needs installed configuration files (needed for
# maximum coverage).
parser = OptionParser("dummy")
parser.install_clush_config_options()
parser.install_display_options(verbose_options=True)
parser.install_connector_options()
options, _ = parser.parse_args([])
config = ClushConfig(options)
def testClushConfigCustomGlobal(self):
"""test CLI.Config.ClushConfig (CLUSTERSHELL_CFGDIR global custom config)
"""
# Save existing environment variable, if it's defined
custom_config_save = os.environ.get('CLUSTERSHELL_CFGDIR')
# Create fake CLUSTERSHELL_CFGDIR
custom_cfg_dir = make_temp_dir()
try:
os.environ['CLUSTERSHELL_CFGDIR'] = custom_cfg_dir.name
cfgfile = open(os.path.join(custom_cfg_dir.name, 'clush.conf'), 'w')
cfgfile.write(dedent("""
[Main]
fanout: 42
connect_timeout: 14
command_timeout: 0
history_size: 100
color: never
verbosity: 2
ssh_user: joebar
ssh_path: ~/bin/ssh
ssh_options: -oSomeDummyUserOption=yes
"""))
cfgfile.flush()
parser = OptionParser("dummy")
parser.install_clush_config_options()
parser.install_display_options(verbose_options=True)
parser.install_connector_options()
options, _ = parser.parse_args([])
config = ClushConfig(options) # filename=None to use defaults!
self.assertEqual(config.color, THREE_CHOICES[1])
self.assertEqual(config.verbosity, VERB_VERB) # takes biggest
self.assertEqual(config.fanout, 42)
self.assertEqual(config.connect_timeout, 14)
self.assertEqual(config.command_timeout, 0)
self.assertEqual(config.ssh_user, 'joebar')
self.assertEqual(config.ssh_path, '~/bin/ssh')
self.assertEqual(config.ssh_options, '-oSomeDummyUserOption=yes')
cfgfile.close()
finally:
if custom_config_save:
os.environ['CLUSTERSHELL_CFGDIR'] = custom_config_save
else:
del os.environ['CLUSTERSHELL_CFGDIR']
custom_cfg_dir.cleanup()
def testClushConfigUserOverride(self):
"""test CLI.Config.ClushConfig (XDG_CONFIG_HOME user config)"""
xdg_config_home_save = os.environ.get('XDG_CONFIG_HOME')
# Create fake XDG_CONFIG_HOME
tdir = make_temp_dir()
try:
os.environ['XDG_CONFIG_HOME'] = tdir.name
# create $XDG_CONFIG_HOME/clustershell/clush.conf
usercfgdir = os.path.join(tdir.name, 'clustershell')
os.mkdir(usercfgdir)
cfgfile = open(os.path.join(usercfgdir, 'clush.conf'), 'w')
cfgfile.write(dedent("""
[Main]
fanout: 42
connect_timeout: 14
command_timeout: 0
history_size: 100
color: never
verbosity: 2
ssh_user: trump
ssh_path: ~/bin/ssh
ssh_options: -oSomeDummyUserOption=yes
"""))
cfgfile.flush()
parser = OptionParser("dummy")
parser.install_clush_config_options()
parser.install_display_options(verbose_options=True)
parser.install_connector_options()
options, _ = parser.parse_args([])
config = ClushConfig(options) # filename=None to use defaults!
self.assertEqual(config.color, THREE_CHOICES[1])
self.assertEqual(config.verbosity, VERB_VERB) # takes biggest
self.assertEqual(config.fanout, 42)
self.assertEqual(config.connect_timeout, 14)
self.assertEqual(config.command_timeout, 0)
self.assertEqual(config.ssh_user, 'trump')
self.assertEqual(config.ssh_path, '~/bin/ssh')
self.assertEqual(config.ssh_options, '-oSomeDummyUserOption=yes')
cfgfile.close()
finally:
if xdg_config_home_save:
os.environ['XDG_CONFIG_HOME'] = xdg_config_home_save
else:
del os.environ['XDG_CONFIG_HOME']
tdir.cleanup()
def testClushConfigConfDirModesEmpty(self):
"""test CLI.Config.ClushConfig (confdir with no modes)"""
tdir1 = make_temp_dir()
dname1 = tdir1.name
tdir2 = make_temp_dir()
dname2 = tdir2.name
f = make_temp_file(dedent("""
[Main]
fanout: 42
connect_timeout: 14
command_timeout: 0
history_size: 100
color: auto
maxrc: yes
node_count: yes
verbosity: 1
confdir: %s "%s" %s
""" % (dname1, dname2, dname1)).encode())
try:
parser = OptionParser("dummy")
parser.install_clush_config_options()
parser.install_display_options(verbose_options=True)
parser.install_connector_options()
options, _ = parser.parse_args([])
config = ClushConfig(options, filename=f.name)
self.assertEqual(config.color, THREE_CHOICES[-1])
self.assertEqual(config.verbosity, VERB_STD)
self.assertTrue(config.maxrc)
self.assertTrue(config.node_count)
self.assertEqual(config.fanout, 42)
self.assertEqual(config.connect_timeout, 14)
self.assertEqual(config.command_timeout, 0)
self.assertEqual(config.ssh_user, None)
self.assertEqual(config.ssh_path, None)
self.assertEqual(config.ssh_options, None)
self.assertEqual(config.command_prefix, "")
self.assertFalse(config.command_prefix)
self.assertFalse(config.password_prompt)
self.assertEqual(len(set(config.modes())), 0)
self.assertRaises(ClushConfigError, config.set_mode, "sshpass")
finally:
f.close()
tdir2.cleanup()
tdir1.cleanup()
def testClushConfigConfDirModes(self):
"""test CLI.Config.ClushConfig (confdir and modes)"""
tdir1 = make_temp_dir()
dname1 = tdir1.name
tdir2 = make_temp_dir()
dname2 = tdir2.name
# Notes:
# - use dname1 two times to check dup checking code
# - use quotes on one of the directory path
# - enable each run modes and test config options
f = make_temp_file(dedent("""
[Main]
fanout: 42
connect_timeout: 14
command_timeout: 0
history_size: 100
color: auto
maxrc: yes
node_count: yes
verbosity: 1
ssh_user: root
ssh_path: /usr/bin/ssh
ssh_options: -oStrictHostKeyChecking=no
confdir: %s "%s" %s
""" % (dname1, dname2, dname1)).encode())
f1 = make_temp_file(dedent("""
[mode:sshpass]
password_prompt: yes
ssh_path: /usr/bin/sshpass /usr/bin/ssh
scp_path: /usr/bin/sshpass /usr/bin/scp
ssh_options: -oBatchMode=no
""").encode(), suffix=".conf", dir=dname1)
f2 = make_temp_file(dedent("""
[mode:sudo]
password_prompt: yes
command_prefix: /usr/bin/sudo -S -p "''"
""").encode(), suffix=".conf", dir=dname2)
f3 = make_temp_file(dedent("""
[mode:test]
fanout: 100
connect_timeout: 6
command_timeout: 5
history_size: 200
color: always
maxrc: no
node_count: no
verbosity: 0
ssh_user: nobody
ssh_path: /some/other/ssh
ssh_options:
""").encode(), suffix=".conf", dir=dname2)
try:
parser = OptionParser("dummy")
parser.install_clush_config_options()
parser.install_display_options(verbose_options=True)
parser.install_connector_options()
options, _ = parser.parse_args([])
config = ClushConfig(options, filename=f.name)
self.assertEqual(config.color, THREE_CHOICES[-1])
self.assertEqual(config.verbosity, VERB_STD)
self.assertTrue(config.maxrc)
self.assertTrue(config.node_count)
self.assertEqual(config.fanout, 42)
self.assertEqual(config.connect_timeout, 14)
self.assertEqual(config.command_timeout, 0)
self.assertEqual(config.ssh_user, "root")
self.assertEqual(config.ssh_path, "/usr/bin/ssh")
self.assertEqual(config.ssh_options, "-oStrictHostKeyChecking=no")
self.assertEqual(config.command_prefix, "")
self.assertFalse(config.command_prefix)
self.assertFalse(config.password_prompt)
self.assertEqual(set(config.modes()), {'sshpass', 'sudo', 'test'})
config.set_mode("sshpass")
self.assertEqual(config.color, THREE_CHOICES[-1])
self.assertEqual(config.verbosity, VERB_STD)
self.assertTrue(config.maxrc)
self.assertTrue(config.node_count)
self.assertEqual(config.fanout, 42)
self.assertEqual(config.connect_timeout, 14)
self.assertEqual(config.command_timeout, 0)
self.assertEqual(config.ssh_user, "root")
self.assertEqual(config.ssh_path, "/usr/bin/sshpass /usr/bin/ssh")
self.assertEqual(config.ssh_options, "-oBatchMode=no")
self.assertEqual(config.command_prefix, "")
self.assertFalse(config.command_prefix)
self.assertTrue(config.password_prompt)
config.set_mode("sudo")
self.assertEqual(config.color, THREE_CHOICES[-1])
self.assertEqual(config.verbosity, VERB_STD)
self.assertTrue(config.maxrc)
self.assertTrue(config.node_count)
self.assertEqual(config.fanout, 42)
self.assertEqual(config.connect_timeout, 14)
self.assertEqual(config.command_timeout, 0)
self.assertEqual(config.ssh_user, "root")
self.assertEqual(config.ssh_path, "/usr/bin/ssh")
self.assertEqual(config.ssh_options, "-oStrictHostKeyChecking=no")
self.assertEqual(config.command_prefix, '/usr/bin/sudo -S -p "\'\'"')
self.assertTrue(config.command_prefix)
self.assertTrue(config.password_prompt)
config.set_mode("test")
self.assertEqual(config.color, THREE_CHOICES[2])
self.assertEqual(config.verbosity, VERB_STD)
self.assertFalse(config.maxrc)
self.assertFalse(config.node_count)
self.assertEqual(config.fanout, 100)
self.assertEqual(config.connect_timeout, 6)
self.assertEqual(config.command_timeout, 5)
self.assertEqual(config.ssh_user, "nobody")
self.assertEqual(config.ssh_path, "/some/other/ssh")
self.assertEqual(config.ssh_options, "")
self.assertEqual(config.command_prefix, "")
self.assertFalse(config.command_prefix)
self.assertFalse(config.password_prompt)
finally:
f3.close()
f2.close()
f1.close()
f.close()
tdir2.cleanup()
tdir1.cleanup()