forked from cea-hpc/clustershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodeSetGroupTest.py
1907 lines (1646 loc) · 70.6 KB
/
NodeSetGroupTest.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
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Unit test for NodeSet with Group support
"""
import os
import posixpath
import sys
from textwrap import dedent
import unittest
from TLib import *
# Wildcard import for testing purpose
from ClusterShell.NodeSet import *
from ClusterShell.NodeUtils import *
def makeTestG1():
"""Create a temporary group file 1"""
f1 = make_temp_file(dedent("""
#
oss: montana5,montana4
mds: montana6
io: montana[4-6]
#42: montana3
compute: montana[32-163]
chassis1: montana[32-33]
chassis2: montana[34-35]
chassis3: montana[36-37]
chassis4: montana[38-39]
chassis5: montana[40-41]
chassis6: montana[42-43]
chassis7: montana[44-45]
chassis8: montana[46-47]
chassis9: montana[48-49]
chassis10: montana[50-51]
chassis11: montana[52-53]
chassis12: montana[54-55]
Uppercase: montana[1-2]
gpuchassis: @chassis[4-5]
gpu: montana[38-41]
all: montana[1-6,32-163]
""").encode('ascii'))
# /!\ Need to return file object and not f1.name, otherwise the temporary
# file might be immediately unlinked.
return f1
def makeTestG2():
"""Create a temporary group file 2"""
f2 = make_temp_file(dedent("""
#
#
para: montana[32-37,42-55]
gpu: montana[38-41]
escape%test: montana[87-90]
esc%test2: @escape%test
""").encode('ascii'))
return f2
def makeTestG3():
"""Create a temporary group file 3"""
f3 = make_temp_file(dedent("""
#
#
all: montana[32-55]
para: montana[32-37,42-55]
gpu: montana[38-41]
login: montana[32-33]
overclock: montana[41-42]
chassis1: montana[32-33]
chassis2: montana[34-35]
chassis3: montana[36-37]
single: idaho
""").encode('ascii'))
return f3
def makeTestR3():
"""Create a temporary reverse group file 3"""
r3 = make_temp_file(dedent("""
#
#
montana32: all,para,login,chassis1
montana33: all,para,login,chassis1
montana34: all,para,chassis2
montana35: all,para,chassis2
montana36: all,para,chassis3
montana37: all,para,chassis3
montana38: all,gpu
montana39: all,gpu
montana40: all,gpu
montana41: all,gpu,overclock
montana42: all,para,overclock
montana43: all,para
montana44: all,para
montana45: all,para
montana46: all,para
montana47: all,para
montana48: all,para
montana49: all,para
montana50: all,para
montana51: all,para
montana52: all,para
montana53: all,para
montana54: all,para
montana55: all,para
idaho: single
""").encode('ascii'))
return r3
def makeTestG4():
"""Create a temporary group file 4 (nD)"""
f4 = make_temp_file(dedent("""
#
rack-x1y1: idaho1z1,idaho2z1
rack-x1y2: idaho2z1,idaho3z1
rack-x2y1: idaho4z1,idaho5z1
rack-x2y2: idaho6z1,idaho7z1
rack-x1: @rack-x1y[1-2]
rack-x2: @rack-x2y[1-2]
rack-y1: @rack-x[1-2]y1
rack-y2: @rack-x[1-2]y2
rack-all: @rack-x[1-2]y[1-2]
""").encode('ascii'))
return f4
class NodeSetGroupTest(unittest.TestCase):
def setUp(self):
"""setUp test reproducibility: change standard group resolver
to ensure that no local group source is used during tests"""
set_std_group_resolver(GroupResolver()) # dummy resolver
def tearDown(self):
"""tearDown: restore standard group resolver"""
set_std_group_resolver(None) # restore std resolver
def testGroupResolverSimple(self):
"""test NodeSet with simple custom GroupResolver"""
test_groups1 = makeTestG1()
source = UpcallGroupSource(
"simple",
"sed -n 's/^$GROUP:\(.*\)/\\1/p' %s" % test_groups1.name,
"sed -n 's/^all:\(.*\)/\\1/p' %s" % test_groups1.name,
"sed -n 's/^\([0-9A-Za-z_-]*\):.*/\\1/p' %s" % test_groups1.name,
None)
# create custom resolver with default source
res = GroupResolver(source)
self.assertFalse(res.has_node_groups())
self.assertFalse(res.has_node_groups("dummy_namespace"))
nodeset = NodeSet("@gpu", resolver=res)
self.assertEqual(nodeset, NodeSet("montana[38-41]"))
self.assertEqual(str(nodeset), "montana[38-41]")
nodeset = NodeSet("@chassis3", resolver=res)
self.assertEqual(str(nodeset), "montana[36-37]")
nodeset = NodeSet("@chassis[3-4]", resolver=res)
self.assertEqual(str(nodeset), "montana[36-39]")
nodeset = NodeSet("@chassis[1,3,5]", resolver=res)
self.assertEqual(str(nodeset), "montana[32-33,36-37,40-41]")
nodeset = NodeSet("@chassis[2-12/2]", resolver=res)
self.assertEqual(str(nodeset), "montana[34-35,38-39,42-43,46-47,50-51,54-55]")
nodeset = NodeSet("@chassis[1,3-4,5-11/3]", resolver=res)
self.assertEqual(str(nodeset), "montana[32-33,36-41,46-47,52-53]")
# test recursive group gpuchassis
nodeset1 = NodeSet("@chassis[4-5]", resolver=res)
nodeset2 = NodeSet("@gpu", resolver=res)
nodeset3 = NodeSet("@gpuchassis", resolver=res)
self.assertEqual(nodeset1, nodeset2)
self.assertEqual(nodeset2, nodeset3)
# test also with some inline operations
nodeset = NodeSet("montana3,@gpuchassis!montana39,montana77^montana38",
resolver=res)
self.assertEqual(str(nodeset), "montana[3,40-41,77]")
def testAllNoResolver(self):
"""test NodeSet.fromall() with no resolver"""
self.assertRaises(NodeSetExternalError, NodeSet.fromall,
resolver=RESOLVER_NOGROUP)
# Also test with a nonfunctional resolver (#263)
res = GroupResolver()
self.assertRaises(NodeSetExternalError, NodeSet.fromall, resolver=res)
def testGroupsNoResolver(self):
"""test NodeSet.groups() with no resolver"""
nodeset = NodeSet("foo", resolver=RESOLVER_NOGROUP)
self.assertRaises(NodeSetExternalError, nodeset.groups)
def testGroupResolverAddSourceError(self):
"""test GroupResolver.add_source() error"""
test_groups1 = makeTestG1()
source = UpcallGroupSource("simple",
"sed -n 's/^$GROUP:\(.*\)/\\1/p' %s" % test_groups1.name,
"sed -n 's/^all:\(.*\)/\\1/p' %s" % test_groups1.name,
"sed -n 's/^\([0-9A-Za-z_-]*\):.*/\\1/p' %s" % test_groups1.name,
None)
res = GroupResolver(source)
# adding the same source again should raise ValueError
self.assertRaises(ValueError, res.add_source, source)
def testGroupResolverMinimal(self):
"""test NodeSet with minimal GroupResolver"""
test_groups1 = makeTestG1()
source = UpcallGroupSource("minimal",
"sed -n 's/^$GROUP:\(.*\)/\\1/p' %s" % test_groups1.name,
None, None, None)
# create custom resolver with default source
res = GroupResolver(source)
nodeset = NodeSet("@gpu", resolver=res)
self.assertEqual(nodeset, NodeSet("montana[38-41]"))
self.assertEqual(str(nodeset), "montana[38-41]")
self.assertRaises(NodeSetExternalError, NodeSet.fromall, resolver=res)
def testConfigEmpty(self):
"""test groups with an empty configuration file"""
f = make_temp_file(b"")
res = GroupResolverConfig(f.name)
# NodeSet should work
nodeset = NodeSet("example[1-100]", resolver=res)
self.assertEqual(str(nodeset), "example[1-100]")
# without group support
self.assertRaises(GroupResolverSourceError, nodeset.regroup)
self.assertRaises(GroupResolverSourceError, NodeSet, "@bar", resolver=res)
def testConfigResolverEmpty(self):
"""test groups resolver with an empty file list"""
# empty file list OR as if no config file is parsable
res = GroupResolverConfig([])
# NodeSet should work
nodeset = NodeSet("example[1-100]", resolver=res)
self.assertEqual(str(nodeset), "example[1-100]")
# without group support
self.assertRaises(GroupResolverSourceError, nodeset.regroup)
self.assertRaises(GroupResolverSourceError, NodeSet, "@bar", resolver=res)
def testConfigBasicLocal(self):
"""test groups with a basic local config file"""
f = make_temp_file(dedent("""
# A comment
[Main]
default: local
[local]
map: echo example[1-100]
#all:
list: echo foo
#reverse:
""").encode('ascii'))
res = GroupResolverConfig(f.name)
nodeset = NodeSet("example[1-100]", resolver=res)
self.assertEqual(str(nodeset), "example[1-100]")
self.assertEqual(nodeset.regroup(), "@foo")
self.assertEqual(list(nodeset.groups().keys()), ["@foo"])
self.assertEqual(str(NodeSet("@foo", resolver=res)), "example[1-100]")
# No 'all' defined: all_nodes() should raise an error
self.assertRaises(GroupSourceNoUpcall, res.all_nodes)
# No 'reverse' defined: node_groups() should raise an error
self.assertRaises(GroupSourceNoUpcall, res.node_groups, "example1")
# regroup with rest
nodeset = NodeSet("example[1-101]", resolver=res)
self.assertEqual(nodeset.regroup(), "@foo,example101")
# regroup incomplete
nodeset = NodeSet("example[50-200]", resolver=res)
self.assertEqual(nodeset.regroup(), "example[50-200]")
# regroup no matching
nodeset = NodeSet("example[102-200]", resolver=res)
self.assertEqual(nodeset.regroup(), "example[102-200]")
# test default_source_name property
self.assertEqual(res.default_source_name, "local")
# check added with lazy init
res = GroupResolverConfig(f.name)
self.assertEqual(res.default_source_name, "local")
def testConfigWrongSyntax(self):
"""test wrong groups config syntax"""
f = make_temp_file(dedent("""
# A comment
[Main]
default: local
[local]
something: echo example[1-100]
""").encode('ascii'))
resolver = GroupResolverConfig(f.name)
self.assertRaises(GroupResolverConfigError, resolver.grouplist)
def testConfigBasicLocalVerbose(self):
"""test groups with a basic local config file (verbose)"""
f = make_temp_file(dedent("""
# A comment
[Main]
default: local
[local]
map: echo example[1-100]
#all:
list: echo foo
#reverse:
""").encode('ascii'))
res = GroupResolverConfig(f.name)
nodeset = NodeSet("example[1-100]", resolver=res)
self.assertEqual(str(nodeset), "example[1-100]")
self.assertEqual(nodeset.regroup(), "@foo")
self.assertEqual(str(NodeSet("@foo", resolver=res)), "example[1-100]")
def testConfigBasicLocalAlternative(self):
"""test groups with a basic local config file (= alternative)"""
f = make_temp_file(dedent("""
# A comment
[Main]
default=local
[local]
map=echo example[1-100]
#all=
list=echo foo
#reverse=
""").encode('ascii'))
res = GroupResolverConfig(f.name)
nodeset = NodeSet("example[1-100]", resolver=res)
self.assertEqual(str(nodeset), "example[1-100]")
self.assertEqual(nodeset.regroup(), "@foo")
self.assertEqual(str(NodeSet("@foo", resolver=res)), "example[1-100]")
# @truc?
def testConfigBasicEmptyDefault(self):
"""test groups with a empty default namespace"""
f = make_temp_file(dedent("""
# A comment
[Main]
default:
[local]
map: echo example[1-100]
#all:
list: echo foo
#reverse:
""").encode('ascii'))
res = GroupResolverConfig(f.name)
nodeset = NodeSet("example[1-100]", resolver=res)
self.assertEqual(str(nodeset), "example[1-100]")
self.assertEqual(nodeset.regroup(), "@foo")
self.assertEqual(str(NodeSet("@foo", resolver=res)), "example[1-100]")
def testConfigBasicNoMain(self):
"""test groups with a local config without main section"""
f = make_temp_file(dedent("""
# A comment
[local]
map: echo example[1-100]
#all:
list: echo foo
#reverse:
""").encode('ascii'))
res = GroupResolverConfig(f.name)
nodeset = NodeSet("example[1-100]", resolver=res)
self.assertEqual(str(nodeset), "example[1-100]")
self.assertEqual(nodeset.regroup(), "@foo")
self.assertEqual(str(NodeSet("@foo", resolver=res)), "example[1-100]")
def testConfigBasicWrongDefault(self):
"""test groups with a wrong default namespace"""
f = make_temp_file(dedent("""
# A comment
[Main]
default: pointless
[local]
map: echo example[1-100]
#all:
list: echo foo
#reverse:
""").encode('ascii'))
resolver = GroupResolverConfig(f.name)
self.assertRaises(GroupResolverConfigError, resolver.grouplist)
def testConfigQueryFailed(self):
"""test groups with config and failed query"""
f = make_temp_file(dedent("""
# A comment
[Main]
default: local
[local]
map: false
all: false
list: echo foo
#reverse:
""").encode('ascii'))
res = GroupResolverConfig(f.name)
nodeset = NodeSet("example[1-100]", resolver=res)
self.assertEqual(str(nodeset), "example[1-100]")
self.assertRaises(NodeSetExternalError, nodeset.regroup)
# all_nodes()
self.assertRaises(NodeSetExternalError, NodeSet.fromall, resolver=res)
def testConfigQueryFailedReverse(self):
"""test groups with config and failed query (reverse)"""
f = make_temp_file(dedent("""
# A comment
[Main]
default: local
[local]
map: echo example1
list: echo foo
reverse: false
""").encode('ascii'))
res = GroupResolverConfig(f.name)
nodeset = NodeSet("@foo", resolver=res)
self.assertEqual(str(nodeset), "example1")
self.assertRaises(NodeSetExternalError, nodeset.regroup)
def testConfigRegroupWrongNamespace(self):
"""test groups by calling regroup(wrong_namespace)"""
f = make_temp_file(dedent("""
# A comment
[Main]
default: local
[local]
map: echo example[1-100]
#all:
list: echo foo
#reverse:
""").encode('ascii'))
res = GroupResolverConfig(f.name)
nodeset = NodeSet("example[1-100]", resolver=res)
self.assertRaises(GroupResolverSourceError, nodeset.regroup, "unknown")
def testConfigNoListNoReverse(self):
"""test groups with no list and not reverse upcall"""
f = make_temp_file(dedent("""
# A comment
[Main]
default: local
[local]
map: echo example[1-100]
#all:
#list:
#reverse:
""").encode('ascii'))
res = GroupResolverConfig(f.name)
nodeset = NodeSet("example[1-100]", resolver=res)
self.assertEqual(str(nodeset), "example[1-100]")
# not able to regroup, should still return valid nodeset
self.assertEqual(nodeset.regroup(), "example[1-100]")
def testConfigNoListButReverseQuery(self):
"""test groups with no list but reverse upcall"""
f = make_temp_file(dedent("""
# A comment
[Main]
default: local
[local]
map: echo example[1-100]
#all:
#list: echo foo
reverse: echo foo
""").encode('ascii'))
res = GroupResolverConfig(f.name)
nodeset = NodeSet("example[1-100]", resolver=res)
self.assertEqual(str(nodeset), "example[1-100]")
self.assertEqual(nodeset.regroup(), "@foo")
def testConfigNoMap(self):
"""test groups with no map upcall"""
f = make_temp_file(dedent("""
# A comment
[Main]
default: local
[local]
#map: echo example[1-100]
all:
list: echo foo
#reverse: echo foo
""").encode('ascii'))
# map is a mandatory upcall, an exception should be raised early
resolver = GroupResolverConfig(f.name)
self.assertRaises(GroupResolverConfigError, resolver.grouplist)
def testConfigWithEmptyList(self):
"""test groups with list upcall returning nothing"""
f = make_temp_file(dedent("""
# A comment
[Main]
default: local
[local]
map: echo example[1-100]
#all:
list: :
reverse: echo foo
""").encode('ascii'))
res = GroupResolverConfig(f.name)
nodeset = NodeSet("example[1-100]", resolver=res)
self.assertEqual(str(nodeset), "example[1-100]")
self.assertEqual(nodeset.regroup(), "@foo")
def testConfigListAllWithAll(self):
"""test all groups listing with all upcall"""
f = make_temp_file(dedent("""
# A comment
[Main]
default: local
[local]
map: echo example[1-100]
all: echo foo bar
list: echo foo
#reverse:
""").encode('ascii'))
res = GroupResolverConfig(f.name)
nodeset = NodeSet("example[1-50]", resolver=res)
self.assertEqual(str(nodeset), "example[1-50]")
self.assertEqual(str(NodeSet.fromall(resolver=res)), "bar,foo")
# test "@*" magic group listing
nodeset = NodeSet("@*", resolver=res)
self.assertEqual(str(nodeset), "bar,foo")
nodeset = NodeSet("rab,@*,oof", resolver=res)
self.assertEqual(str(nodeset), "bar,foo,oof,rab")
# with group source
nodeset = NodeSet("@local:*", resolver=res)
self.assertEqual(str(nodeset), "bar,foo")
nodeset = NodeSet("rab,@local:*,oof", resolver=res)
self.assertEqual(str(nodeset), "bar,foo,oof,rab")
def testConfigListAllWithoutAll(self):
"""test all groups listing without all upcall"""
f = make_temp_file(dedent("""
# A comment
[Main]
default: local
[local]
map: echo example[1-100]
#all:
list: echo foo bar
#reverse:
""").encode('ascii'))
res = GroupResolverConfig(f.name)
nodeset = NodeSet("example[1-50]", resolver=res)
self.assertEqual(str(nodeset), "example[1-50]")
self.assertEqual(str(NodeSet.fromall(resolver=res)), "example[1-100]")
# test "@*" magic group listing
nodeset = NodeSet("@*", resolver=res)
self.assertEqual(str(nodeset), "example[1-100]")
nodeset = NodeSet("@*,example[101-104]", resolver=res)
self.assertEqual(str(nodeset), "example[1-104]")
nodeset = NodeSet("example[105-149],@*,example[101-104]", resolver=res)
self.assertEqual(str(nodeset), "example[1-149]")
# with group source
nodeset = NodeSet("@local:*", resolver=res)
self.assertEqual(str(nodeset), "example[1-100]")
nodeset = NodeSet("example0,@local:*,example[101-110]", resolver=res)
self.assertEqual(str(nodeset), "example[0-110]")
def testConfigListAllNDWithoutAll(self):
"""test all groups listing without all upcall (nD)"""
# Even in nD, ensure that $GROUP is a simple group that has been previously expanded
f = make_temp_file(dedent("""
# A comment
[Main]
default: local
[local]
map: if [ "$GROUP" = "x1y[3-4]" ]; then exit 1; elif [ "$GROUP" = "x1y1" ]; then echo rack[1-5]z[1-42]; else echo rack[6-10]z[1-42]; fi
#all:
list: echo x1y1 x1y2 x1y[3-4]
#reverse:
""").encode('ascii'))
res = GroupResolverConfig(f.name, illegal_chars=ILLEGAL_GROUP_CHARS)
nodeset = NodeSet("rack3z40", resolver=res)
self.assertEqual(str(NodeSet.fromall(resolver=res)), "rack[1-10]z[1-42]")
self.assertEqual(res.grouplist(), ['x1y1', 'x1y2', 'x1y[3-4]']) # raw
self.assertEqual(grouplist(resolver=res), ['x1y1', 'x1y2', 'x1y3', 'x1y4']) # cleaned
# test "@@" group name set
nodeset = NodeSet("@@", resolver=res)
self.assertEqual(str(nodeset), "x1y[1-4]")
nodeset = NodeSet("@@local", resolver=res)
self.assertEqual(str(nodeset), "x1y[1-4]")
# test "@*" magic group listing
nodeset = NodeSet("@*", resolver=res)
self.assertEqual(str(nodeset), "rack[1-10]z[1-42]")
# with group source
nodeset = NodeSet("@local:*", resolver=res)
self.assertEqual(str(nodeset), "rack[1-10]z[1-42]")
nodeset = NodeSet("rack11z1,@local:*,rack11z[2-42]", resolver=res)
self.assertEqual(str(nodeset), "rack[1-11]z[1-42]")
def testConfigIllegalCharsND(self):
"""test group list containing illegal characters"""
f = make_temp_file(dedent("""
# A comment
[Main]
default: local
[local]
map: echo rack[6-10]z[1-42]
#all:
list: echo x1y1 x1y2 @illegal x1y[3-4]
#reverse:
""").encode('ascii'))
res = GroupResolverConfig(f.name, illegal_chars=ILLEGAL_GROUP_CHARS)
nodeset = NodeSet("rack3z40", resolver=res)
self.assertRaises(GroupResolverIllegalCharError, res.grouplist)
def testConfigResolverSources(self):
"""test sources() with groups config of 2 sources"""
f = make_temp_file(dedent("""
# A comment
[Main]
default: local
[local]
map: echo example[1-100]
[other]
map: echo example[1-10]
""").encode('ascii'))
res = GroupResolverConfig(f.name)
self.assertEqual(len(res.sources()), 2)
self.assertTrue('local' in res.sources())
self.assertTrue('other' in res.sources())
def testConfigCrossRefs(self):
"""test groups config with cross references"""
f = make_temp_file(dedent("""
# A comment
[Main]
default: other
[local]
map: echo example[1-100]
[other]
map: echo "foo: @local:foo" | sed -n 's/^$GROUP:\(.*\)/\\1/p'
[third]
map: printf "bar: @ref-rel\\nref-rel: @other:foo\\nref-all: @*\\n" | sed -n 's/^$GROUP:\(.*\)/\\1/p'
list: echo bar
""").encode('ascii'))
res = GroupResolverConfig(f.name)
nodeset = NodeSet("@other:foo", resolver=res)
self.assertEqual(str(nodeset), "example[1-100]")
# @third:bar -> @ref-rel (third) -> @other:foo -> @local:foo -> nodes
nodeset = NodeSet("@third:bar", resolver=res)
self.assertEqual(str(nodeset), "example[1-100]")
nodeset = NodeSet("@third:ref-all", resolver=res)
self.assertEqual(str(nodeset), "example[1-100]")
def testConfigGroupsDirDummy(self):
"""test groups with groupsdir defined (dummy)"""
f = make_temp_file(dedent("""
[Main]
default: local
groupsdir: /path/to/nowhere
[local]
map: echo example[1-100]
#all:
list: echo foo
#reverse:
""").encode('ascii'))
res = GroupResolverConfig(f.name)
nodeset = NodeSet("example[1-100]", resolver=res)
self.assertEqual(str(nodeset), "example[1-100]")
self.assertEqual(nodeset.regroup(), "@foo")
self.assertEqual(str(NodeSet("@foo", resolver=res)), "example[1-100]")
def testConfigGroupsDirExists(self):
"""test groups with groupsdir defined (real, other)"""
tdir = make_temp_dir()
f = make_temp_file(dedent("""
[Main]
default: new_local
groupsdir: %s
[local]
map: echo example[1-100]
#all:
list: echo foo
#reverse:
""" % tdir.name).encode('ascii'))
f2 = make_temp_file(dedent("""
[new_local]
map: echo example[1-100]
#all:
list: echo bar
#reverse:
""").encode('ascii'), suffix=".conf", dir=tdir.name)
try:
res = GroupResolverConfig(f.name)
nodeset = NodeSet("example[1-100]", resolver=res)
self.assertEqual(str(nodeset), "example[1-100]")
self.assertEqual(nodeset.regroup(), "@bar")
self.assertEqual(str(NodeSet("@bar", resolver=res)), "example[1-100]")
finally:
f2.close()
f.close()
tdir.cleanup()
def testConfigGroupsMultipleDirs(self):
"""test groups with multiple confdir defined"""
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
f = make_temp_file(dedent("""
[Main]
default: local2
confdir: "%s" %s %s
[local]
map: echo example[1-100]
list: echo foo
""" % (dname1, dname2, dname1)).encode('ascii'))
fs1 = make_temp_file(dedent("""
[local1]
map: echo loc1node[1-100]
list: echo bar
""").encode('ascii'), suffix=".conf", dir=dname1)
fs2 = make_temp_file(dedent("""
[local2]
map: echo loc2node[02-50]
list: echo toto
""").encode('ascii'), suffix=".conf", dir=dname2)
try:
res = GroupResolverConfig(f.name)
nodeset = NodeSet("example[1-100]", resolver=res)
self.assertEqual(str(nodeset), "example[1-100]")
# local
self.assertEqual(nodeset.regroup("local"), "@local:foo")
self.assertEqual(str(NodeSet("@local:foo", resolver=res)), "example[1-100]")
# local1
nodeset = NodeSet("loc1node[1-100]", resolver=res)
self.assertEqual(nodeset.regroup("local1"), "@local1:bar")
self.assertEqual(str(NodeSet("@local1:bar", resolver=res)), "loc1node[1-100]")
# local2
nodeset = NodeSet("loc2node[02-50]", resolver=res)
self.assertEqual(nodeset.regroup(), "@toto") # default group source
self.assertEqual(str(NodeSet("@toto", resolver=res)), "loc2node[02-50]")
finally:
fs2.close()
fs1.close()
f.close()
tdir2.cleanup()
tdir1.cleanup()
def testConfigGroupsDirDupConfig(self):
"""test groups with duplicate in groupsdir"""
tdir = make_temp_dir()
f = make_temp_file(dedent("""
[Main]
default: iamdup
groupsdir: %s
[local]
map: echo example[1-100]
#all:
list: echo foo
#reverse:
""" % tdir.name).encode('ascii'))
f2 = make_temp_file(dedent("""
[iamdup]
map: echo example[1-100]
#all:
list: echo bar
#reverse:
""").encode('ascii'), suffix=".conf", dir=tdir.name)
f3 = make_temp_file(dedent("""
[iamdup]
map: echo example[10-200]
#all:
list: echo patato
#reverse:
""").encode('ascii'), suffix=".conf", dir=tdir.name)
try:
resolver = GroupResolverConfig(f.name)
self.assertRaises(GroupResolverConfigError, resolver.grouplist)
finally:
f3.close()
f2.close()
f.close()
tdir.cleanup()
def testConfigGroupsDirExistsNoOther(self):
"""test groups with groupsdir defined (real, no other)"""
tdir1 = make_temp_dir()
dname1 = tdir1.name
tdir2 = make_temp_dir()
dname2 = tdir2.name
f = make_temp_file(dedent("""
[Main]
default: new_local
groupsdir: %s %s
""" % (dname1, dname2)).encode('ascii'))
f2 = make_temp_file(dedent("""
[new_local]
map: echo example[1-100]
#all:
list: echo bar
#reverse:
""").encode('ascii'), suffix=".conf", dir=dname2)
try:
res = GroupResolverConfig(f.name)
nodeset = NodeSet("example[1-100]", resolver=res)
self.assertEqual(str(nodeset), "example[1-100]")
self.assertEqual(nodeset.regroup(), "@bar")
self.assertEqual(str(NodeSet("@bar", resolver=res)), "example[1-100]")
finally:
f2.close()
f.close()
tdir2.cleanup()
tdir1.cleanup()
def testConfigGroupsDirNotADirectory(self):
"""test groups with groupsdir defined (not a directory)"""
tdir = make_temp_dir()
fdummy = make_temp_file(b"wrong")
f = make_temp_file(dedent("""
[Main]
default: new_local
groupsdir: %s
""" % fdummy.name).encode('ascii'), dir=tdir.name)
try:
resolver = GroupResolverConfig(f.name)
self.assertRaises(GroupResolverConfigError, resolver.grouplist)
finally:
fdummy.close()
f.close()
tdir.cleanup()
def testConfigIllegalChars(self):
"""test groups with illegal characters"""
f = make_temp_file(dedent("""
# A comment
[Main]
default: local
[local]
map: echo example[1-100]
#all:
list: echo 'foo *'
reverse: echo f^oo
""").encode('ascii'))
res = GroupResolverConfig(f.name, illegal_chars=set("@,&!&^*"))
nodeset = NodeSet("example[1-100]", resolver=res)
self.assertRaises(GroupResolverIllegalCharError, nodeset.groups)
self.assertRaises(GroupResolverIllegalCharError, nodeset.regroup)
def testConfigMaxRecursionError(self):
"""test groups maximum recursion depth exceeded error"""
f = make_temp_file(dedent("""
# A comment
[Main]
default: local
[local]
map: echo @deep
list: echo deep
""").encode('ascii'))
res = GroupResolverConfig(f.name)
self.assertRaises(NodeSetParseError, NodeSet, "@deep", resolver=res)
def testGroupResolverND(self):
"""test NodeSet with simple custom GroupResolver (nD)"""
test_groups4 = makeTestG4()
source = UpcallGroupSource("simple",
"sed -n 's/^$GROUP:\(.*\)/\\1/p' %s" % test_groups4.name,
"sed -n 's/^all:\(.*\)/\\1/p' %s" % test_groups4.name,
"sed -n 's/^\([0-9A-Za-z_-]*\):.*/\\1/p' %s" % test_groups4.name,
None)
# create custom resolver with default source
res = GroupResolver(source)
self.assertFalse(res.has_node_groups())
self.assertFalse(res.has_node_groups("dummy_namespace"))
nodeset = NodeSet("@rack-x1y2", resolver=res)
self.assertEqual(nodeset, NodeSet("idaho[2-3]z1"))
self.assertEqual(str(nodeset), "idaho[2-3]z1")
nodeset = NodeSet("@rack-y1", resolver=res)
self.assertEqual(str(nodeset), "idaho[1-2,4-5]z1")
nodeset = NodeSet("@rack-all", resolver=res)
self.assertEqual(str(nodeset), "idaho[1-7]z1")
# test NESTED nD groups()
self.assertEqual(sorted(nodeset.groups().keys()),
['@rack-all', '@rack-x1', '@rack-x1y1', '@rack-x1y2',
'@rack-x2', '@rack-x2y1', '@rack-x2y2', '@rack-y1',
'@rack-y2'])
self.assertEqual(sorted(nodeset.groups(groupsource="simple").keys()),
['@simple:rack-all', '@simple:rack-x1',
'@simple:rack-x1y1', '@simple:rack-x1y2',
'@simple:rack-x2', '@simple:rack-x2y1',
'@simple:rack-x2y2', '@simple:rack-y1',
'@simple:rack-y2'])
self.assertEqual(sorted(nodeset.groups(groupsource="simple",
noprefix=True).keys()),
['@rack-all', '@rack-x1', '@rack-x1y1', '@rack-x1y2',
'@rack-x2', '@rack-x2y1', '@rack-x2y2', '@rack-y1',
'@rack-y2'])
testns = NodeSet()
for gnodes, inodes in nodeset.groups().values():
testns.update(inodes)
self.assertEqual(testns, nodeset)
# more tests with nested groups
nodeset = NodeSet("idaho5z1", resolver=res)
self.assertEqual(sorted(nodeset.groups().keys()),
['@rack-all', '@rack-x2', '@rack-x2y1', '@rack-y1'])
nodeset = NodeSet("idaho5z1,idaho4z1", resolver=res)
self.assertEqual(sorted(nodeset.groups().keys()),
['@rack-all', '@rack-x2', '@rack-x2y1', '@rack-y1'])
nodeset = NodeSet("idaho5z1,idaho7z1", resolver=res)
self.assertEqual(sorted(nodeset.groups().keys()),
['@rack-all', '@rack-x2', '@rack-x2y1', '@rack-x2y2',
'@rack-y1', '@rack-y2'])
def testConfigCFGDIR(self):
"""test groups with $CFGDIR use in upcalls"""
f = make_temp_file(dedent("""
[Main]
default: local
[local]
map: echo example[1-100]
list: basename $CFGDIR
""").encode('ascii'))
res = GroupResolverConfig(f.name)