Debian Codegraph @

tests/CLINodesetTest.py

raw symbols SCIP
1 # ClusterShell.CLI.Nodeset test suite
2 # Written by S. Thiell
3
4 """Unit test for CLI.Nodeset"""
5
6 import os
7 import random
8 from textwrap import dedent
9 import unittest
10
11 from .TLib import *
12 from ClusterShell.CLI.Nodeset import main
13
14 from ClusterShell.NodeUtils import GroupResolverConfig
15 from ClusterShell.NodeSet import set_std_group_resolver, \
16 set_std_group_resolver_config
17
18
19 class CLINodesetTestBase(unittest.TestCase):
20 """Base unit test class for testing CLI/Nodeset.py"""
21
22 def _nodeset_t(self, args, stdin, expected_stdout, expected_rc=0,
23 expected_stderr=None):
24 CLI_main(self, main, ['nodeset'] + args, stdin, expected_stdout,
25 expected_rc, expected_stderr)
26
27
28 class CLINodesetTest(CLINodesetTestBase):
29 """Unit test class for testing CLI/Nodeset.py"""
30
31 def _battery_count(self, args):
32 self._nodeset_t(args + ["--count", ""], None, b"0\n")
33 self._nodeset_t(args + ["--count", "foo"], None, b"1\n")
34 self._nodeset_t(args + ["--count", "foo", "bar"], None, b"2\n")
35 self._nodeset_t(args + ["--count", "foo", "foo"], None, b"1\n")
36 self._nodeset_t(args + ["--count", "foo", "foo", "bar"], None, b"2\n")
37 self._nodeset_t(args + ["--count", "foo[0]"], None, b"1\n")
38 self._nodeset_t(args + ["--count", "foo[2]"], None, b"1\n")
39 self._nodeset_t(args + ["--count", "foo[1,2]"], None, b"2\n")
40 self._nodeset_t(args + ["--count", "foo[1-2]"], None, b"2\n")
41 self._nodeset_t(args + ["--count", "foo[1,2]", "foo[1-2]"], None, b"2\n")
42 self._nodeset_t(args + ["--count", "foo[1-200,245-394]"], None, b"350\n")
43 self._nodeset_t(args + ["--count", "foo[395-442]", "foo[1-200,245-394]"], None, b"398\n")
44 self._nodeset_t(args + ["--count", "foo[395-442]", "foo", "foo[1-200,245-394]"], None, b"399\n")
45 self._nodeset_t(args + ["--count", "foo[395-442]", "foo", "foo[0-200,245-394]"], None, b"400\n")
46 self._nodeset_t(args + ["--count", "foo[395-442]", "bar3,bar24", "foo[1-200,245-394]"], None, b"400\n")
47 # from stdin: use string not bytes as input because CLI/Nodeset.py works in text mode
48 self._nodeset_t(args + ["--count"], "\n", b"0\n")
49 self._nodeset_t(args + ["--count"], "foo\n", b"1\n")
50 self._nodeset_t(args + ["--count"], "foo\nbar\n", b"2\n")
51 self._nodeset_t(args + ["--count"], "foo\nfoo\n", b"1\n")
52 self._nodeset_t(args + ["--count"], "foo\nfoo\nbar\n", b"2\n")
53 self._nodeset_t(args + ["--count"], "foo[0]\n", b"1\n")
54 self._nodeset_t(args + ["--count"], "foo[2]\n", b"1\n")
55 self._nodeset_t(args + ["--count"], "foo[1,2]\n", b"2\n")
56 self._nodeset_t(args + ["--count"], "foo[1-2]\n", b"2\n")
57 self._nodeset_t(args + ["--count"], "foo[1,2]\nfoo[1-2]\n", b"2\n")
58 self._nodeset_t(args + ["--count"], "foo[1-200,245-394]\n", b"350\n")
59 self._nodeset_t(args + ["--count"], "foo[395-442]\nfoo[1-200,245-394]\n", b"398\n")
60 self._nodeset_t(args + ["--count"], "foo[395-442]\nfoo\nfoo[1-200,245-394]\n", b"399\n")
61 self._nodeset_t(args + ["--count"], "foo[395-442]\nfoo\nfoo[0-200,245-394]\n", b"400\n")
62 self._nodeset_t(args + ["--count"], "foo[395-442]\nbar3,bar24\nfoo[1-200,245-394]\n", b"400\n")
63
64 def test_001_count(self):
65 """test nodeset --count"""
66 self._battery_count([])
67 self._battery_count(["--autostep=1"])
68 self._battery_count(["--autostep=2"])
69 self._battery_count(["--autostep=5"])
70 self._battery_count(["--autostep=auto"])
71 self._battery_count(["--autostep=0%"])
72 self._battery_count(["--autostep=50%"])
73 self._battery_count(["--autostep=100%"])
74
75 def test_002_count_intersection(self):
76 """test nodeset --count --intersection"""
77 self._nodeset_t(["--count", "foo", "--intersection", "bar"], None, b"0\n")
78 self._nodeset_t(["--count", "foo", "--intersection", "foo"], None, b"1\n")
79 self._nodeset_t(["--count", "foo", "--intersection", "foo", "-i", "bar"], None, b"0\n")
80 self._nodeset_t(["--count", "foo[0]", "--intersection", "foo0"], None, b"1\n")
81 self._nodeset_t(["--count", "foo[2]", "--intersection", "foo"], None, b"0\n")
82 self._nodeset_t(["--count", "foo[1,2]", "--intersection", "foo[1-2]"], None, b"2\n")
83 self._nodeset_t(["--count", "foo[395-442]", "--intersection", "foo[1-200,245-394]"], None, b"0\n")
84 self._nodeset_t(["--count", "foo[395-442]", "--intersection", "foo", "-i", "foo[1-200,245-394]"], None, b"0\n")
85 self._nodeset_t(["--count", "foo[395-442]", "-i", "foo", "-i", "foo[0-200,245-394]"], None, b"0\n")
86 self._nodeset_t(["--count", "foo[395-442]", "--intersection", "bar3,bar24", "-i", "foo[1-200,245-394]"], None, b"0\n")
87 # multiline args (#394)
88 self._nodeset_t(["--count", "foo[1,2]", "-i", "foo1\nfoo2"], None, b"2\n")
89 self._nodeset_t(["--count", "foo[1,2]", "-i", "foo1\nfoo2", "foo3\nfoo4"], None, b"4\n")
90
91 def test_003_count_intersection_stdin(self):
92 """test nodeset --count --intersection (stdin)"""
93 self._nodeset_t(["--count", "--intersection", "bar"], "foo\n", b"0\n")
94 self._nodeset_t(["--count", "--intersection", "foo"], "foo\n", b"1\n")
95 self._nodeset_t(["--count", "--intersection", "foo", "-i", "bar"], "foo\n", b"0\n")
96 self._nodeset_t(["--count", "--intersection", "foo0"], "foo[0]\n", b"1\n")
97 self._nodeset_t(["--count", "--intersection", "foo"], "foo[2]\n", b"0\n")
98 self._nodeset_t(["--count", "--intersection", "foo[1-2]"], "foo[1,2]\n", b"2\n")
99 self._nodeset_t(["--count", "--intersection", "foo[1-200,245-394]"], "foo[395-442]\n", b"0\n")
100 self._nodeset_t(["--count", "--intersection", "foo", "-i", "foo[1-200,245-394]"], "foo[395-442]\n", b"0\n")
101 self._nodeset_t(["--count", "-i", "foo", "-i", "foo[0-200,245-394]"], "foo[395-442]\n", b"0\n")
102 self._nodeset_t(["--count", "--intersection", "bar3,bar24", "-i", "foo[1-200,245-394]"], "foo[395-442]\n", b"0\n")
103
104 def _battery_fold(self, args):
105 self._nodeset_t(args + ["--fold", ""], None, b"\n")
106 self._nodeset_t(args + ["--fold", "foo"], None, b"foo\n")
107 self._nodeset_t(args + ["--fold", "foo", "bar"], None, b"bar,foo\n")
108 self._nodeset_t(args + ["--fold", "foo", "foo"], None, b"foo\n")
109 self._nodeset_t(args + ["--fold", "foo", "foo", "bar"], None, b"bar,foo\n")
110 self._nodeset_t(args + ["--fold", "foo[0]"], None, b"foo0\n")
111 self._nodeset_t(args + ["--fold", "foo[2]"], None, b"foo2\n")
112 self._nodeset_t(args + ["--fold", "foo[1,2]"], None, b"foo[1-2]\n")
113 self._nodeset_t(args + ["--fold", "foo[1-2]"], None, b"foo[1-2]\n")
114 self._nodeset_t(args + ["--fold", "foo[1,2]", "foo[1-2]"], None, b"foo[1-2]\n")
115 self._nodeset_t(args + ["--fold", "foo[1-200,245-394]"], None, b"foo[1-200,245-394]\n")
116 self._nodeset_t(args + ["--fold", "foo[395-442]", "foo[1-200,245-394]"], None, b"foo[1-200,245-442]\n")
117 self._nodeset_t(args + ["--fold", "foo[395-442]", "foo", "foo[1-200,245-394]"], None, b"foo,foo[1-200,245-442]\n")
118 self._nodeset_t(args + ["--fold", "foo[395-442]", "foo", "foo[0-200,245-394]"], None, b"foo,foo[0-200,245-442]\n")
119 self._nodeset_t(args + ["--fold", "foo[395-442]", "bar3,bar24", "foo[1-200,245-394]"], None, b"bar[3,24],foo[1-200,245-442]\n")
120 # multiline arg (#394)
121 self._nodeset_t(args + ["--fold", "foo3\nfoo1\nfoo2\nbar"], None, b"bar,foo[1-3]\n")
122 self._nodeset_t(args + ["--fold", "foo3\n\n\nfoo1\n\nfoo2\n\n"], None, b"foo[1-3]\n")
123 # stdin
124 self._nodeset_t(args + ["--fold"], "\n", b"\n")
125 self._nodeset_t(args + ["--fold"], "foo\n", b"foo\n")
126 self._nodeset_t(args + ["--fold"], "foo\nbar\n", b"bar,foo\n")
127 self._nodeset_t(args + ["--fold"], "foo\nfoo\n", b"foo\n")
128 self._nodeset_t(args + ["--fold"], "foo\nfoo\nbar\n", b"bar,foo\n")
129 self._nodeset_t(args + ["--fold"], "foo[0]\n", b"foo0\n")
130 self._nodeset_t(args + ["--fold"], "foo[2]\n", b"foo2\n")
131 self._nodeset_t(args + ["--fold"], "foo[1,2]\n", b"foo[1-2]\n")
132 self._nodeset_t(args + ["--fold"], "foo[1-2]\n", b"foo[1-2]\n")
133 self._nodeset_t(args + ["--fold"], "foo[1,2]\nfoo[1-2]\n", b"foo[1-2]\n")
134 self._nodeset_t(args + ["--fold"], "foo[1-200,245-394]\n", b"foo[1-200,245-394]\n")
135 self._nodeset_t(args + ["--fold"], "foo[395-442]\nfoo[1-200,245-394]\n", b"foo[1-200,245-442]\n")
136 self._nodeset_t(args + ["--fold"], "foo[395-442]\nfoo\nfoo[1-200,245-394]\n", b"foo,foo[1-200,245-442]\n")
137 self._nodeset_t(args + ["--fold"], "foo[395-442]\nfoo\nfoo[0-200,245-394]\n", b"foo,foo[0-200,245-442]\n")
138 self._nodeset_t(args + ["--fold"], "foo[395-442]\nbar3,bar24\nfoo[1-200,245-394]\n", b"bar[3,24],foo[1-200,245-442]\n")
139
140 def test_004_fold(self):
141 """test nodeset --fold"""
142 self._battery_fold([])
143 self._battery_fold(["--autostep=3"])
144 # --autostep=auto (1.7)
145 self._battery_fold(["--autostep=auto"])
146 self._battery_count(["--autostep=0%"])
147 self._battery_count(["--autostep=50%"])
148 self._battery_count(["--autostep=100%"])
149
150 def test_005_fold_autostep(self):
151 """test nodeset --fold --autostep=X"""
152 self._nodeset_t(["--autostep=2", "-f", "foo0", "foo2", "foo4", "foo6"], None, b"foo[0-6/2]\n")
153 self._nodeset_t(["--autostep=2", "-f", "foo4", "foo2", "foo0", "foo6"], None, b"foo[0-6/2]\n")
154 self._nodeset_t(["--autostep=3", "-f", "foo0", "foo2", "foo4", "foo6"], None, b"foo[0-6/2]\n")
155 self._nodeset_t(["--autostep=4", "-f", "foo0", "foo2", "foo4", "foo6"], None, b"foo[0-6/2]\n")
156 self._nodeset_t(["--autostep=5", "-f", "foo0", "foo2", "foo4", "foo6"], None, b"foo[0,2,4,6]\n")
157 self._nodeset_t(["--autostep=auto", "-f", "foo0", "foo2", "foo4", "foo6"], None, b"foo[0-6/2]\n")
158 self._nodeset_t(["--autostep=auto", "-f", "foo4", "foo2", "foo0", "foo6"], None, b"foo[0-6/2]\n")
159 self._nodeset_t(["--autostep=auto", "-f", "foo4", "foo2", "foo0", "foo2", "foo6"], None, b"foo[0-6/2]\n")
160 self._nodeset_t(["--autostep=auto", "-f", "foo4", "foo2", "foo0", "foo5", "foo6"], None, b"foo[0,2,4-6]\n")
161 self._nodeset_t(["--autostep=auto", "-f", "foo4", "foo2", "foo0", "foo9", "foo6"], None, b"foo[0,2,4,6,9]\n")
162 self._nodeset_t(["--autostep=75%", "-f", "foo0", "foo2", "foo4", "foo6"], None, b"foo[0-6/2]\n")
163 self._nodeset_t(["--autostep=75%", "-f", "foo4", "foo2", "foo0", "foo6"], None, b"foo[0-6/2]\n")
164 self._nodeset_t(["--autostep=80%", "-f", "foo4", "foo2", "foo0", "foo2", "foo6"], None, b"foo[0-6/2]\n")
165 self._nodeset_t(["--autostep=80%", "-f", "foo4", "foo2", "foo0", "foo5", "foo6"], None, b"foo[0,2,4-6]\n")
166 self._nodeset_t(["--autostep=80%", "-f", "foo4", "foo2", "foo0", "foo9", "foo6"], None, b"foo[0-6/2,9]\n")
167 self._nodeset_t(["--autostep=81%", "-f", "foo4", "foo2", "foo0", "foo9", "foo6"], None, b"foo[0,2,4,6,9]\n")
168 self._nodeset_t(["--autostep=100%", "-f", "foo4", "foo2", "foo0", "foo9", "foo6"], None, b"foo[0,2,4,6,9]\n")
169
170 def test_006_expand(self):
171 """test nodeset --expand"""
172 self._nodeset_t(["--expand", ""], None, b"\n")
173 self._nodeset_t(["--expand", "foo"], None, b"foo\n")
174 self._nodeset_t(["--expand", "foo", "bar"], None, b"bar foo\n")
175 self._nodeset_t(["--expand", "foo", "foo"], None, b"foo\n")
176 self._nodeset_t(["--expand", "foo[0]"], None, b"foo0\n")
177 self._nodeset_t(["--expand", "foo[2]"], None, b"foo2\n")
178 self._nodeset_t(["--expand", "foo[1,2]"], None, b"foo1 foo2\n")
179 self._nodeset_t(["--expand", "foo[1-2]"], None, b"foo1 foo2\n")
180 self._nodeset_t(["--expand", "foo[1-2],bar"], None, b"bar foo1 foo2\n")
181
182 def test_007_expand_stdin(self):
183 """test nodeset --expand (stdin)"""
184 self._nodeset_t(["--expand"], "\n", b"\n")
185 self._nodeset_t(["--expand"], "foo\n", b"foo\n")
186 self._nodeset_t(["--expand"], "foo\nbar\n", b"bar foo\n")
187 self._nodeset_t(["--expand"], "foo\nfoo\n", b"foo\n")
188 self._nodeset_t(["--expand"], "foo[0]\n", b"foo0\n")
189 self._nodeset_t(["--expand"], "foo[2]\n", b"foo2\n")
190 self._nodeset_t(["--expand"], "foo[1,2]\n", b"foo1 foo2\n")
191 self._nodeset_t(["--expand"], "foo[1-2]\n", b"foo1 foo2\n")
192 self._nodeset_t(["--expand"], "foo[1-2],bar\n", b"bar foo1 foo2\n")
193
194 def test_008_expand_separator(self):
195 """test nodeset --expand -S"""
196 self._nodeset_t(["--expand", "-S", ":", "foo"], None, b"foo\n")
197 self._nodeset_t(["--expand", "-S", ":", "foo", "bar"], None, b"bar:foo\n")
198 self._nodeset_t(["--expand", "--separator", ":", "foo", "bar"], None, b"bar:foo\n")
199 self._nodeset_t(["--expand", "--separator=:", "foo", "bar"], None, b"bar:foo\n")
200 self._nodeset_t(["--expand", "-S", ":", "foo", "foo"], None, b"foo\n")
201 self._nodeset_t(["--expand", "-S", ":", "foo[0]"], None, b"foo0\n")
202 self._nodeset_t(["--expand", "-S", ":", "foo[2]"], None, b"foo2\n")
203 self._nodeset_t(["--expand", "-S", ":", "foo[1,2]"], None, b"foo1:foo2\n")
204 self._nodeset_t(["--expand", "-S", ":", "foo[1-2]"], None, b"foo1:foo2\n")
205 self._nodeset_t(["--expand", "-S", " ", "foo[1-2]"], None, b"foo1 foo2\n")
206 self._nodeset_t(["--expand", "-S", ",", "foo[1-2],bar"], None, b"bar,foo1,foo2\n")
207 self._nodeset_t(["--expand", "-S", "uuu", "foo[1-2],bar"], None, b"baruuufoo1uuufoo2\n")
208 self._nodeset_t(["--expand", "-S", "\\n", "foo[1-2]"], None, b"foo1\nfoo2\n")
209 self._nodeset_t(["--expand", "-S", "\n", "foo[1-2]"], None, b"foo1\nfoo2\n")
210
211 def test_009_fold_xor(self):
212 """test nodeset --fold --xor"""
213 self._nodeset_t(["--fold", "foo", "-X", "bar"], None, b"bar,foo\n")
214 self._nodeset_t(["--fold", "foo", "-X", "foo"], None, b"\n")
215 self._nodeset_t(["--fold", "foo[1,2]", "-X", "foo[1-2]"], None, b"\n")
216 self._nodeset_t(["--fold", "foo[1-10]", "-X", "foo[5-15]"], None, b"foo[1-4,11-15]\n")
217 self._nodeset_t(["--fold", "foo[395-442]", "-X", "foo[1-200,245-394]"], None, b"foo[1-200,245-442]\n")
218 self._nodeset_t(["--fold", "foo[395-442]", "-X", "foo", "-X", "foo[1-200,245-394]"], None, b"foo,foo[1-200,245-442]\n")
219 self._nodeset_t(["--fold", "foo[395-442]", "-X", "foo", "-X", "foo[0-200,245-394]"], None, b"foo,foo[0-200,245-442]\n")
220 self._nodeset_t(["--fold", "foo[395-442]", "-X", "bar3,bar24", "-X", "foo[1-200,245-394]"], None, b"bar[3,24],foo[1-200,245-442]\n")
221 # multiline args (#394)
222 self._nodeset_t(["--fold", "foo[1-10]", "-X", "foo5\nfoo6\nfoo7"], None, b"foo[1-4,8-10]\n")
223 self._nodeset_t(["--fold", "foo[1-10]", "-X", "foo5\nfoo6\nfoo7", "foo5\nfoo6"], None, b"foo[1-6,8-10]\n")
224
225 def test_010_fold_xor_stdin(self):
226 """test nodeset --fold --xor (stdin)"""
227 self._nodeset_t(["--fold", "-X", "bar"], "foo\n", b"bar,foo\n")
228 self._nodeset_t(["--fold", "-X", "foo"], "foo\n", b"\n")
229 self._nodeset_t(["--fold", "-X", "foo[1-2]"], "foo[1,2]\n", b"\n")
230 self._nodeset_t(["--fold", "-X", "foo[5-15]"], "foo[1-10]\n", b"foo[1-4,11-15]\n")
231 self._nodeset_t(["--fold", "-X", "foo[1-200,245-394]"], "foo[395-442]\n", b"foo[1-200,245-442]\n")
232 self._nodeset_t(["--fold", "-X", "foo", "-X", "foo[1-200,245-394]"], "foo[395-442]\n", b"foo,foo[1-200,245-442]\n")
233 self._nodeset_t(["--fold", "-X", "foo", "-X", "foo[0-200,245-394]"], "foo[395-442]\n", b"foo,foo[0-200,245-442]\n")
234 self._nodeset_t(["--fold", "-X", "bar3,bar24", "-X", "foo[1-200,245-394]"], "foo[395-442]\n", b"bar[3,24],foo[1-200,245-442]\n")
235 # using stdin for -X
236 self._nodeset_t(["-f", "foo[2-4]", "-X", "-"], "foo4 foo5 foo6\n", b"foo[2-3,5-6]\n")
237 self._nodeset_t(["-f", "-X", "-", "foo[1-6]"], "foo4 foo5 foo6\n",
238 b"foo[1-6]\n", 0,
239 b"WARNING: empty left operand for set operation\n")
240
241 def test_011_fold_exclude(self):
242 """test nodeset --fold --exclude"""
243 # Empty result
244 self._nodeset_t(["--fold", "foo", "-x", "foo"], None, b"\n")
245 # With no range
246 self._nodeset_t(["--fold", "foo,bar", "-x", "foo"], None, b"bar\n")
247 # Normal with range
248 self._nodeset_t(["--fold", "foo[0-5]", "-x", "foo[0-10]"], None, b"\n")
249 self._nodeset_t(["--fold", "foo[0-10]", "-x", "foo[0-5]"], None, b"foo[6-10]\n")
250 # Do no change
251 self._nodeset_t(["--fold", "foo[6-10]", "-x", "bar[0-5]"], None, b"foo[6-10]\n")
252 self._nodeset_t(["--fold", "foo[0-10]", "foo[13-18]", "--exclude", "foo[5-10,15]"], None, b"foo[0-4,13-14,16-18]\n")
253 # multiline args (#394)
254 self._nodeset_t(["--fold", "foo[0-5]", "-x", "foo0\nfoo9\nfoo3\nfoo2\nfoo1"], None, b"foo[4-5]\n")
255 self._nodeset_t(["--fold", "foo[0-5]", "-x", "foo0\nfoo9\nfoo3\nfoo2\nfoo1", "foo5\nfoo6"], None, b"foo[4-6]\n")
256
257 def test_012_fold_exclude_stdin(self):
258 """test nodeset --fold --exclude (stdin)"""
259 # Empty result
260 self._nodeset_t(["--fold", "-x", "foo"], "", b"\n", 0,
261 b"WARNING: empty left operand for set operation\n")
262 self._nodeset_t(["--fold", "-x", "foo"], "\n", b"\n", 0,
263 b"WARNING: empty left operand for set operation\n")
264 self._nodeset_t(["--fold", "-x", "foo"], "foo\n", b"\n")
265 # With no range
266 self._nodeset_t(["--fold", "-x", "foo"], "foo,bar\n", b"bar\n")
267 # Normal with range
268 self._nodeset_t(["--fold", "-x", "foo[0-10]"], "foo[0-5]\n", b"\n")
269 self._nodeset_t(["--fold", "-x", "foo[0-5]"], "foo[0-10]\n", b"foo[6-10]\n")
270 # Do no change
271 self._nodeset_t(["--fold", "-x", "bar[0-5]"], "foo[6-10]\n", b"foo[6-10]\n")
272 self._nodeset_t(["--fold", "--exclude", "foo[5-10,15]"], "foo[0-10]\nfoo[13-18]\n", b"foo[0-4,13-14,16-18]\n")
273 # using stdin for -x
274 self._nodeset_t(["-f", "foo[1-6]", "-x", "-"], "foo4 foo5 foo6\n", b"foo[1-3]\n")
275 self._nodeset_t(["-f", "-x", "-", "foo[1-6]"], "foo4 foo5 foo6\n",
276 b"foo[1-6]\n", 0,
277 b"WARNING: empty left operand for set operation\n")
278
279 def test_013_fold_intersection(self):
280 """test nodeset --fold --intersection"""
281 # Empty result
282 self._nodeset_t(["--fold", "foo", "-i", "foo"], None, b"foo\n")
283 # With no range
284 self._nodeset_t(["--fold", "foo,bar", "--intersection", "foo"], None, b"foo\n")
285 # Normal with range
286 self._nodeset_t(["--fold", "foo[0-5]", "-i", "foo[0-10]"], None, b"foo[0-5]\n")
287 self._nodeset_t(["--fold", "foo[0-10]", "-i", "foo[0-5]"], None, b"foo[0-5]\n")
288 self._nodeset_t(["--fold", "foo[6-10]", "-i", "bar[0-5]"], None, b"\n")
289 self._nodeset_t(["--fold", "foo[0-10]", "foo[13-18]", "-i", "foo[5-10,15]"], None, b"foo[5-10,15]\n")
290 # numerical bracket folding (#228)
291 self._nodeset_t(["--fold", "node123[1-2]", "-i", "node1232"], None, b"node1232\n")
292 self._nodeset_t(["--fold", "node023[1-2]0", "-i", "node02320"], None, b"node02320\n")
293 self._nodeset_t(["--fold", "node023[1-2]0-ipmi2", "-i", "node02320-ipmi2"], None, b"node02320-ipmi2\n")
294 self._nodeset_t(["--fold", "-i", "foo", "foo"], None, b"foo\n", 0,
295 b"WARNING: empty left operand for set operation\n")
296
297 def test_014_fold_intersection_stdin(self):
298 """test nodeset --fold --intersection (stdin)"""
299 # Empty result
300 self._nodeset_t(["--fold", "--intersection", "foo"], "", b"\n", 0,
301 b"WARNING: empty left operand for set operation\n")
302 self._nodeset_t(["--fold", "--intersection", "foo"], "\n", b"\n", 0,
303 b"WARNING: empty left operand for set operation\n")
304 self._nodeset_t(["--fold", "-i", "foo"], "foo\n", b"foo\n")
305 # With no range
306 self._nodeset_t(["--fold", "-i", "foo"], "foo,bar\n", b"foo\n")
307 # Normal with range
308 self._nodeset_t(["--fold", "-i", "foo[0-10]"], "foo[0-5]\n", b"foo[0-5]\n")
309 self._nodeset_t(["--fold", "-i", "foo[0-5]"], "foo[0-10]\n", b"foo[0-5]\n")
310 # Do no change
311 self._nodeset_t(["--fold", "-i", "bar[0-5]"], "foo[6-10]\n", b"\n")
312 self._nodeset_t(["--fold", "-i", "foo[5-10,15]"], "foo[0-10]\nfoo[13-18]\n", b"foo[5-10,15]\n")
313 # using stdin for -i
314 self._nodeset_t(["-f", "foo[1-6]", "-i", "-"], "foo4 foo5 foo6\n", b"foo[4-6]\n")
315 self._nodeset_t(["-f", "-i", "-", "foo[1-6]"], "foo4 foo5 foo6\n",
316 b"foo[1-6]\n", 0,
317 b"WARNING: empty left operand for set operation\n")
318 # numerical bracket folding (#228)
319 self._nodeset_t(["--fold", "-i", "node123[1-2]"], "node1232\n", b"node1232\n")
320 self._nodeset_t(["--fold", "-i", "node023[1-2]0"], "node02320\n", b"node02320\n")
321 self._nodeset_t(["--fold", "-i", "node023[1-2]0-ipmi2"], "node02320-ipmi2\n", b"node02320-ipmi2\n")
322
323 def test_015_rangeset(self):
324 """test nodeset --rangeset"""
325 self._nodeset_t(["--fold", "--rangeset", "1,2"], None, b"1-2\n")
326 self._nodeset_t(["--expand", "-R", "1-2"], None, b"1 2\n")
327 self._nodeset_t(["--fold", "-R", "1-2", "-X", "2-3"], None, b"1,3\n")
328
329 def test_016_rangeset_stdin(self):
330 """test nodeset --rangeset (stdin)"""
331 self._nodeset_t(["--fold", "--rangeset"], "1,2\n", b"1-2\n")
332 self._nodeset_t(["--expand", "-R"], "1-2\n", b"1 2\n")
333 self._nodeset_t(["--fold", "-R", "-X", "2-3"], "1-2\n", b"1,3\n")
334
335 def test_017_stdin(self):
336 """test nodeset - (stdin)"""
337 self._nodeset_t(["-f", "-"], "foo\n", b"foo\n")
338 self._nodeset_t(["-f", "-"], "foo1 foo2 foo3\n", b"foo[1-3]\n")
339 self._nodeset_t(["--autostep=2", "-f"], "foo0 foo2 foo4 foo6\n", b"foo[0-6/2]\n")
340 self._nodeset_t(["--autostep=auto", "-f"], "foo0 foo2 foo4 foo6\n", b"foo[0-6/2]\n")
341 self._nodeset_t(["--autostep=100%", "-f"], "foo0 foo2 foo4 foo6\n", b"foo[0-6/2]\n")
342 self._nodeset_t(["--autostep=0%", "-f"], "foo0 foo2 foo4 foo6\n", b"foo[0-6/2]\n")
343
344 def test_018_split(self):
345 """test nodeset --split"""
346 self._nodeset_t(["--split=2", "-f", "bar"], None, b"bar\n")
347 self._nodeset_t(["--split", "2", "-f", "foo,bar"], None, b"bar\nfoo\n")
348 self._nodeset_t(["--split", "2", "-e", "foo", "bar", "bur", "oof", "gcc"], None, b"bar bur foo\ngcc oof\n")
349 self._nodeset_t(["--split=2", "-f", "foo[2-9]"], None, b"foo[2-5]\nfoo[6-9]\n")
350 self._nodeset_t(["--split=2", "-f", "foo[2-3,7]", "bar9"], None, b"bar9,foo2\nfoo[3,7]\n")
351 self._nodeset_t(["--split=3", "-f", "foo[2-9]"], None, b"foo[2-4]\nfoo[5-7]\nfoo[8-9]\n")
352 self._nodeset_t(["--split=1", "-f", "foo2", "foo3"], None, b"foo[2-3]\n")
353 self._nodeset_t(["--split=4", "-f", "foo[2-3]"], None, b"foo2\nfoo3\n")
354 self._nodeset_t(["--split=4", "-f", "foo3", "foo2"], None, b"foo2\nfoo3\n")
355 self._nodeset_t(["--split=2", "-e", "foo[2-9]"], None, b"foo2 foo3 foo4 foo5\nfoo6 foo7 foo8 foo9\n")
356 self._nodeset_t(["--split=3", "-e", "foo[2-9]"], None, b"foo2 foo3 foo4\nfoo5 foo6 foo7\nfoo8 foo9\n")
357 self._nodeset_t(["--split=1", "-e", "foo3", "foo2"], None, b"foo2 foo3\n")
358 self._nodeset_t(["--split=4", "-e", "foo[2-3]"], None, b"foo2\nfoo3\n")
359 self._nodeset_t(["--split=4", "-e", "foo2", "foo3"], None, b"foo2\nfoo3\n")
360 self._nodeset_t(["--split=2", "-c", "foo2", "foo3"], None, b"1\n1\n")
361
362 def test_019_contiguous(self):
363 """test nodeset --contiguous"""
364 self._nodeset_t(["--contiguous", "-f", "bar"], None, b"bar\n")
365 self._nodeset_t(["--contiguous", "-f", "foo,bar"], None, b"bar\nfoo\n")
366 self._nodeset_t(["--contiguous", "-f", "foo", "bar", "bur", "oof", "gcc"], None, b"bar\nbur\nfoo\ngcc\noof\n")
367 self._nodeset_t(["--contiguous", "-e", "foo", "bar", "bur", "oof", "gcc"], None, b"bar\nbur\nfoo\ngcc\noof\n")
368 self._nodeset_t(["--contiguous", "-f", "foo2"], None, b"foo2\n")
369 self._nodeset_t(["--contiguous", "-R", "-f", "2"], None, b"2\n")
370 self._nodeset_t(["--contiguous", "-f", "foo[2-9]"], None, b"foo[2-9]\n")
371 self._nodeset_t(["--contiguous", "-f", "foo[2-3,7]", "bar9"], None, b"bar9\nfoo[2-3]\nfoo7\n")
372 self._nodeset_t(["--contiguous", "-R", "-f", "2-3,7", "9"], None, b"2-3\n7\n9\n")
373 self._nodeset_t(["--contiguous", "-f", "foo2", "foo3"], None, b"foo[2-3]\n")
374 self._nodeset_t(["--contiguous", "-f", "foo3", "foo2"], None, b"foo[2-3]\n")
375 self._nodeset_t(["--contiguous", "-f", "foo3", "foo1"], None, b"foo1\nfoo3\n")
376 self._nodeset_t(["--contiguous", "-f", "foo[1-5/2]", "foo7"], None, b"foo1\nfoo3\nfoo5\nfoo7\n")
377
378 def test_020_slice(self):
379 """test nodeset -I/--slice"""
380 self._nodeset_t(["--slice=0", "-f", "bar"], None, b"bar\n")
381 self._nodeset_t(["--slice=0", "-e", "bar"], None, b"bar\n")
382 self._nodeset_t(["--slice=1", "-f", "bar"], None, b"\n")
383 self._nodeset_t(["--slice=0-1", "-f", "bar"], None, b"bar\n")
384 self._nodeset_t(["-I0", "-f", "bar[34-68,89-90]"], None, b"bar34\n")
385 self._nodeset_t(["-R", "-I0", "-f", "34-68,89-90"], None, b"34\n")
386 self._nodeset_t(["-I 0", "-f", "bar[34-68,89-90]"], None, b"bar34\n")
387 self._nodeset_t(["-I 0", "-e", "bar[34-68,89-90]"], None, b"bar34\n")
388 self._nodeset_t(["-I 0-3", "-f", "bar[34-68,89-90]"], None, b"bar[34-37]\n")
389 self._nodeset_t(["-I 0-3", "-f", "bar[34-68,89-90]", "-x", "bar34"], None, b"bar[35-38]\n")
390 self._nodeset_t(["-I 0-3", "-f", "bar[34-68,89-90]", "-x", "bar35"], None, b"bar[34,36-38]\n")
391 self._nodeset_t(["-I 0-3", "-e", "bar[34-68,89-90]"], None, b"bar34 bar35 bar36 bar37\n")
392 self._nodeset_t(["-I 3,1,0,2", "-f", "bar[34-68,89-90]"], None, b"bar[34-37]\n")
393 self._nodeset_t(["-I 1,3,7,10,16,20,30,34-35,37", "-f", "bar[34-68,89-90]"], None, b"bar[35,37,41,44,50,54,64,68,89]\n")
394 self._nodeset_t(["-I 8", "-f", "bar[34-68,89-90]"], None, b"bar42\n")
395 self._nodeset_t(["-I 8-100", "-f", "bar[34-68,89-90]"], None, b"bar[42-68,89-90]\n")
396 self._nodeset_t(["-I 0-100", "-f", "bar[34-68,89-90]"], None, b"bar[34-68,89-90]\n")
397 self._nodeset_t(["-I 8-100/2", "-f", "bar[34-68,89-90]"], None, b"bar[42,44,46,48,50,52,54,56,58,60,62,64,66,68,90]\n")
398 self._nodeset_t(["--autostep=2", "-I 8-100/2", "-f", "bar[34-68,89-90]"], None, b"bar[42-68/2,90]\n")
399 self._nodeset_t(["--autostep=93%", "-I 8-100/2", "-f", "bar[34-68,89-90]"], None, b"bar[42-68/2,90]\n")
400 self._nodeset_t(["--autostep=94%", "-I 8-100/2", "-f", "bar[34-68,89-90]"], None, b"bar[42,44,46,48,50,52,54,56,58,60,62,64,66,68,90]\n")
401 self._nodeset_t(["--autostep=auto", "-I 8-100/2", "-f", "bar[34-68,89-90]"], None, b"bar[42,44,46,48,50,52,54,56,58,60,62,64,66,68,90]\n")
402 self._nodeset_t(["--autostep=auto", "-I 8-100/2", "-f", "bar[34-68]"], None, b"bar[42-68/2]\n")
403 self._nodeset_t(["--autostep=100%", "-I 8-100/2", "-f", "bar[34-68]"], None, b"bar[42-68/2]\n")
404
405 def test_021_slice_stdin(self):
406 """test nodeset -I/--slice (stdin)"""
407 self._nodeset_t(["--slice=0", "-f"], "bar\n", b"bar\n")
408 self._nodeset_t(["--slice=0", "-e"], "bar\n", b"bar\n")
409 self._nodeset_t(["--slice=1", "-f"], "bar\n", b"\n")
410 self._nodeset_t(["--slice=0-1", "-f"], "bar\n", b"bar\n")
411 self._nodeset_t(["-I0", "-f"], "bar[34-68,89-90]\n", b"bar34\n")
412 self._nodeset_t(["-R", "-I0", "-f"], "34-68,89-90\n", b"34\n")
413 self._nodeset_t(["-I 0", "-f"], "bar[34-68,89-90]\n", b"bar34\n")
414 self._nodeset_t(["-I 0", "-e"], "bar[34-68,89-90]\n", b"bar34\n")
415 self._nodeset_t(["-I 0-3", "-f"], "bar[34-68,89-90]\n", b"bar[34-37]\n")
416 self._nodeset_t(["-I 0-3", "-f", "-x", "bar34"], "bar[34-68,89-90]\n", b"bar[35-38]\n")
417 self._nodeset_t(["-I 0-3", "-f", "-x", "bar35"], "bar[34-68,89-90]\n", b"bar[34,36-38]\n")
418 self._nodeset_t(["-I 0-3", "-e"], "bar[34-68,89-90]\n", b"bar34 bar35 bar36 bar37\n")
419 self._nodeset_t(["-I 3,1,0,2", "-f"], "bar[34-68,89-90]\n", b"bar[34-37]\n")
420 self._nodeset_t(["-I 1,3,7,10,16,20,30,34-35,37", "-f"], "bar[34-68,89-90]\n", b"bar[35,37,41,44,50,54,64,68,89]\n")
421 self._nodeset_t(["-I 8", "-f"], "bar[34-68,89-90]\n", b"bar42\n")
422 self._nodeset_t(["-I 8-100", "-f"], "bar[34-68,89-90]\n", b"bar[42-68,89-90]\n")
423 self._nodeset_t(["-I 0-100", "-f"], "bar[34-68,89-90]\n", b"bar[34-68,89-90]\n")
424 self._nodeset_t(["-I 8-100/2", "-f"], "bar[34-68,89-90]\n", b"bar[42,44,46,48,50,52,54,56,58,60,62,64,66,68,90]\n")
425 self._nodeset_t(["--autostep=2", "-I 8-100/2", "-f"], "bar[34-68,89-90]\n", b"bar[42-68/2,90]\n")
426 self._nodeset_t(["--autostep=93%", "-I 8-100/2", "-f"], "bar[34-68,89-90]\n", b"bar[42-68/2,90]\n")
427 self._nodeset_t(["--autostep=93.33%", "-I 8-100/2", "-f"], "bar[34-68,89-90]\n", b"bar[42-68/2,90]\n")
428 self._nodeset_t(["--autostep=94%", "-I 8-100/2", "-f"], "bar[34-68,89-90]\n", b"bar[42,44,46,48,50,52,54,56,58,60,62,64,66,68,90]\n")
429 self._nodeset_t(["--autostep=auto", "-I 8-100/2", "-f"], "bar[34-68,89-90]\n", b"bar[42,44,46,48,50,52,54,56,58,60,62,64,66,68,90]\n")
430 self._nodeset_t(["--autostep=2", "-I 8-100/2", "-f"], "bar[34-68]\n", b"bar[42-68/2]\n")
431
432 def test_022_output_format(self):
433 """test nodeset -O"""
434 self._nodeset_t(["--expand", "--output-format", "/path/%s/", "foo"], None, b"/path/foo/\n")
435 self._nodeset_t(["--expand", "-O", "/path/%s/", "-S", ":", "foo"], None, b"/path/foo/\n")
436 self._nodeset_t(["--expand", "-O", "/path/%s/", "foo[2]"], None, b"/path/foo2/\n")
437 self._nodeset_t(["--expand", "-O", "%s-ib0", "foo[1-4]"], None, b"foo1-ib0 foo2-ib0 foo3-ib0 foo4-ib0\n")
438 self._nodeset_t(["--expand", "-O", "%s-ib0", "-S", ":", "foo[1-4]"], None, b"foo1-ib0:foo2-ib0:foo3-ib0:foo4-ib0\n")
439 self._nodeset_t(["--fold", "-O", "%s-ipmi", "foo", "bar"], None, b"bar-ipmi,foo-ipmi\n")
440 self._nodeset_t(["--fold", "-O", "%s-ib0", "foo1", "foo2"], None, b"foo[1-2]-ib0\n")
441 self._nodeset_t(["--fold", "-O", "%s-ib0", "foo1", "foo2", "bar1", "bar2"], None, b"bar[1-2]-ib0,foo[1-2]-ib0\n")
442 self._nodeset_t(["--fold", "-O", "%s-ib0", "--autostep=auto", "foo[1-9/2]"], None, b"foo[1-9/2]-ib0\n")
443 self._nodeset_t(["--fold", "-O", "%s-ib0", "--autostep=6", "foo[1-9/2]"], None, b"foo[1,3,5,7,9]-ib0\n")
444 self._nodeset_t(["--fold", "-O", "%s-ib0", "--autostep=5", "foo[1-9/2]"], None, b"foo[1-9/2]-ib0\n")
445 self._nodeset_t(["--count", "-O", "result-%s", "foo1", "foo2"], None, b"result-2\n")
446 self._nodeset_t(["--contiguous", "-O", "%s-ipmi", "-f", "foo[2-3,7]", "bar9"], None, b"bar9-ipmi\nfoo[2-3]-ipmi\nfoo7-ipmi\n")
447 self._nodeset_t(["--split=2", "-O", "%s-ib", "-e", "foo[2-9]"], None, b"foo2-ib foo3-ib foo4-ib foo5-ib\nfoo6-ib foo7-ib foo8-ib foo9-ib\n")
448 self._nodeset_t(["--split=3", "-O", "hwm-%s", "-f", "foo[2-9]"], None, b"hwm-foo[2-4]\nhwm-foo[5-7]\nhwm-foo[8-9]\n")
449 self._nodeset_t(["-I0", "-O", "{%s}", "-f", "bar[34-68,89-90]"], None, b"{bar34}\n")
450 # RangeSet mode (-R)
451 self._nodeset_t(["--fold", "-O", "{%s}", "--rangeset", "1,2"], None, b"{1-2}\n")
452 self._nodeset_t(["--expand", "-O", "{%s}", "-R", "1-2"], None, b"{1} {2}\n")
453 self._nodeset_t(["--fold", "-O", "{%s}", "-R", "1-2", "-X", "2-3"], None, b"{1,3}\n")
454 self._nodeset_t(["--fold", "-O", "{%s}", "-S", ":", "--rangeset", "1,2"], None, b"{1-2}\n")
455 self._nodeset_t(["--expand", "-O", "{%s}", "-S", ":", "-R", "1-2"], None, b"{1}:{2}\n")
456 self._nodeset_t(["--fold", "-O", "{%s}", "-S", ":", "-R", "1-2", "-X", "2-3"], None, b"{1,3}\n")
457 self._nodeset_t(["-R", "-I0", "-O", "{%s}", "-f", "34-68,89-90"], None, b"{34}\n")
458
459 def test_023_axis(self):
460 """test nodeset folding with --axis"""
461 self._nodeset_t(["--axis=0", "-f", "bar"], None, b"bar\n")
462 self._nodeset_t(["--axis=1", "-f", "bar"], None, b"bar\n")
463 self._nodeset_t(["--axis=1", "-R", "-f", "1,2,3"], None, None, 2,
464 b"--axis option is only supported when folding nodeset\n")
465 self._nodeset_t(["--axis=1", "-e", "bar"], None, None, 2,
466 b"--axis option is only supported when folding nodeset\n")
467
468 # 1D and 2D nodeset: fold along axis 0 only
469 self._nodeset_t(["--axis=1", "-f", "comp-[1-2]-[1-3],login-[1-2]"], None,
470 b'comp-[1-2]-1,comp-[1-2]-2,comp-[1-2]-3,login-[1-2]\n')
471 # 1D and 2D nodeset: fold along axis 1 only
472 self._nodeset_t(["--axis=2", "-f", "comp-[1-2]-[1-3],login-[1-2]"], None,
473 b'comp-1-[1-3],comp-2-[1-3],login-1,login-2\n')
474 # 1D and 2D nodeset: fold along last axis only
475 self._nodeset_t(["--axis=-1", "-f", "comp-[1-2]-[1-3],login-[1-2]"], None,
476 b'comp-1-[1-3],comp-2-[1-3],login-[1-2]\n')
477
478 # test for a common case
479 ndnodes = []
480 for ib in range(2):
481 for idx in range(500):
482 ndnodes.append("node%d-ib%d" % (idx, ib))
483 random.shuffle(ndnodes)
484
485 self._nodeset_t(["--axis=1", "-f"] + ndnodes, None,
486 b"node[0-499]-ib0,node[0-499]-ib1\n")
487
488 exp_result = []
489 for idx in range(500):
490 exp_result.append("node%d-ib[0-1]" % idx)
491
492 exp_result_str = '%s\n' % ','.join(exp_result)
493
494 self._nodeset_t(["--axis=2", "-f"] + ndnodes, None,
495 exp_result_str.encode())
496
497 # 4D test
498 ndnodes = ["c-1-2-3-4", "c-2-2-3-4", "c-3-2-3-4", "c-5-5-5-5",
499 "c-5-7-5-5", "c-5-9-5-5", "c-5-11-5-5", "c-9-8-8-08",
500 "c-9-8-8-09"]
501 self._nodeset_t(["--axis=1", "-f"] + ndnodes, None,
502 b"c-5-5-5-5,c-5-7-5-5,c-5-9-5-5,c-5-11-5-5,c-[1-3]-2-3-4,c-9-8-8-08,c-9-8-8-09\n")
503 self._nodeset_t(["--axis=2", "-f"] + ndnodes, None,
504 b"c-5-[5,7,9,11]-5-5,c-1-2-3-4,c-2-2-3-4,c-3-2-3-4,c-9-8-8-08,c-9-8-8-09\n")
505 self._nodeset_t(["--axis=3", "-f"] + ndnodes, None,
506 b"c-5-5-5-5,c-5-7-5-5,c-5-9-5-5,c-5-11-5-5,c-1-2-3-4,c-2-2-3-4,c-3-2-3-4,c-9-8-8-08,c-9-8-8-09\n")
507 self._nodeset_t(["--axis=4", "-f"] + ndnodes, None,
508 b"c-5-5-5-5,c-5-7-5-5,c-5-9-5-5,c-5-11-5-5,c-1-2-3-4,c-2-2-3-4,c-3-2-3-4,c-9-8-8-[08-09]\n")
509
510 self._nodeset_t(["--axis=1-2", "-f"] + ndnodes, None,
511 b"c-5-[5,7,9,11]-5-5,c-[1-3]-2-3-4,c-9-8-8-08,c-9-8-8-09\n")
512 self._nodeset_t(["--axis=2-3", "-f"] + ndnodes, None,
513 b"c-5-[5,7,9,11]-5-5,c-1-2-3-4,c-2-2-3-4,c-3-2-3-4,c-9-8-8-08,c-9-8-8-09\n")
514 self._nodeset_t(["--axis=3-4", "-f"] + ndnodes, None,
515 b"c-5-5-5-5,c-5-7-5-5,c-5-9-5-5,c-5-11-5-5,c-1-2-3-4,c-2-2-3-4,c-3-2-3-4,c-9-8-8-[08-09]\n")
516 self._nodeset_t(["--axis=1-3", "-f"] + ndnodes, None,
517 b"c-5-[5,7,9,11]-5-5,c-[1-3]-2-3-4,c-9-8-8-08,c-9-8-8-09\n")
518 self._nodeset_t(["--axis=2-4", "-f"] + ndnodes, None,
519 b"c-5-[5,7,9,11]-5-5,c-1-2-3-4,c-2-2-3-4,c-3-2-3-4,c-9-8-8-[08-09]\n")
520
521 self._nodeset_t(["--axis=1-4", "-f"] + ndnodes, None,
522 b"c-5-[5,7,9,11]-5-5,c-[1-3]-2-3-4,c-9-8-8-[08-09]\n")
523 self._nodeset_t(["-f"] + ndnodes, None,
524 b"c-5-[5,7,9,11]-5-5,c-[1-3]-2-3-4,c-9-8-8-[08-09]\n")
525
526 # a case where axis and autostep are working
527 self._nodeset_t(["--autostep=4", "--axis=1-2", "-f"] + ndnodes, None,
528 b"c-5-[5-11/2]-5-5,c-[1-3]-2-3-4,c-9-8-8-08,c-9-8-8-09\n")
529
530 def test_024_axis_stdin(self):
531 """test nodeset folding with --axis (stdin)"""
532 self._nodeset_t(["--axis=0", "-f"], "bar\n", b"bar\n")
533 self._nodeset_t(["--axis=1", "-f"], "bar\n", b"bar\n")
534 self._nodeset_t(["--axis=1", "-R", "-f"], "1,2,3", None, 2,
535 b"--axis option is only supported when folding nodeset\n")
536 self._nodeset_t(["--axis=1", "-e"], "bar\n", None, 2,
537 b"--axis option is only supported when folding nodeset\n")
538
539 # 1D and 2D nodeset: fold along axis 0 only
540 self._nodeset_t(["--axis=1", "-f"], "comp-[1-2]-[1-3],login-[1-2]\n",
541 b'comp-[1-2]-1,comp-[1-2]-2,comp-[1-2]-3,login-[1-2]\n')
542 # 1D and 2D nodeset: fold along axis 1 only
543 self._nodeset_t(["--axis=2", "-f"], "comp-[1-2]-[1-3],login-[1-2]\n",
544 b'comp-1-[1-3],comp-2-[1-3],login-1,login-2\n')
545 # 1D and 2D nodeset: fold along last axis only
546 self._nodeset_t(["--axis=-1", "-f"], "comp-[1-2]-[1-3],login-[1-2]\n",
547 b'comp-1-[1-3],comp-2-[1-3],login-[1-2]\n')
548
549 # test for a common case
550 ndnodes = []
551 for ib in range(2):
552 for idx in range(500):
553 ndnodes.append("node%d-ib%d" % (idx, ib))
554 random.shuffle(ndnodes)
555
556 self._nodeset_t(["--axis=1", "-f"], '\n'.join(ndnodes) + '\n',
557 b"node[0-499]-ib0,node[0-499]-ib1\n")
558
559 exp_result = []
560 for idx in range(500):
561 exp_result.append("node%d-ib[0-1]" % idx)
562
563 exp_result_str = '%s\n' % ','.join(exp_result)
564
565 self._nodeset_t(["--axis=2", "-f"], '\n'.join(ndnodes) + '\n',
566 exp_result_str.encode())
567
568 def test_025_pick(self):
569 """test nodeset --pick"""
570 for num in range(1, 100):
571 self._nodeset_t(["--count", "--pick", str(num), "foo[1-100]"],
572 None, str(num).encode() + b'\n')
573 self._nodeset_t(["--count", "--pick", str(num), "-R", "1-100"],
574 None, str(num).encode() + b'\n')
575
576 def test_026_index(self):
577 """test nodeset --index"""
578 self._nodeset_t(["--index", "node0", "node[0-9]"], None, b"0\n")
579 self._nodeset_t(["--index", "node5", "node[0-9]"], None, b"5\n")
580 self._nodeset_t(["--index", "node9", "node[0-9]"], None, b"9\n")
581 # --index is the reverse of -I/--slice
582 self._nodeset_t(["--index", "bar34", "bar[34-68,89-90]"], None, b"0\n")
583 self._nodeset_t(["--index", "bar42", "bar[34-68,89-90]"], None, b"8\n")
584 self._nodeset_t(["--index", "bar89", "bar[34-68,89-90]"], None, b"35\n")
585 self._nodeset_t(["--index", "bar90", "bar[34-68,89-90]"], None, b"36\n")
586 # several patterns: sorted alphabetically by name, not input order
587 self._nodeset_t(["--index", "bmc10", "node[1-4],bmc[10-20]"], None,
588 b"0\n")
589 self._nodeset_t(["--index", "node1", "node[1-4],bmc[10-20]"], None,
590 b"11\n")
591 # nD nodeset
592 self._nodeset_t(["--index", "da34p1", "da[30,34-51]p[1-2]"], None,
593 b"2\n")
594 # zero-padding is significant
595 self._nodeset_t(["--index", "prune003", "prune[003-034]"], None, b"0\n")
596 self._nodeset_t(["--index", "prune034", "prune[003-034]"], None,
597 b"31\n")
598 self._nodeset_t(["--index", "prune3", "prune[003-034]"], None, b"", 1,
599 b"ERROR: 'prune3' is not in nodeset\n")
600 # read the set from stdin
601 self._nodeset_t(["--index", "node5"], "node[0-9]\n", b"5\n")
602 # honor -O output format
603 self._nodeset_t(["--index", "node5", "-O", "idx=%s", "node[0-9]"],
604 None, b"idx=5\n")
605 # set operations are applied before the lookup
606 self._nodeset_t(["--index", "node5", "node[0-9]", "-x", "node0"], None,
607 b"4\n")
608 # -I/--slice is a transform like any other: --index sees the sliced set
609 self._nodeset_t(["--index", "node3", "-I", "0-4", "node[0-9]"], None,
610 b"3\n")
611 self._nodeset_t(["--index", "node7", "-I", "0-4", "node[0-9]"], None,
612 b"", 1, b"ERROR: 'node7' is not in nodeset\n")
613 # missing node: ValueError -> exit 1 (like list.index())
614 self._nodeset_t(["--index", "node42", "node[0-9]"], None, b"", 1,
615 b"ERROR: 'node42' is not in nodeset\n")
616 # a single node is required
617 self._nodeset_t(["--index", "node[1-2]", "node[0-9]"], None, b"", 1,
618 b"ERROR: index() argument must be a single node\n")
619 # --index is a command: incompatible with other commands and --pick
620 self._nodeset_t(["--index", "node5", "-c", "node[0-9]"], None, None, 2,
621 b"Multiple commands not allowed.\n")
622 self._nodeset_t(["--index", "node5", "--pick", "2", "node[0-9]"], None,
623 None, 2, b"--index cannot be combined with --pick\n")
624
625 def test_027_index_rangeset(self):
626 """test nodeset --index (rangeset mode)"""
627 self._nodeset_t(["-R", "--index", "5", "0-9"], None, b"5\n")
628 self._nodeset_t(["-R", "--index", "200", "0-100,200"], None, b"101\n")
629 self._nodeset_t(["-R", "--index", "18", "1,5,18-31"], None, b"2\n")
630 # zero-padding is significant
631 self._nodeset_t(["-R", "--index", "09", "08-10"], None, b"1\n")
632 # read the set from stdin
633 self._nodeset_t(["-R", "--index", "5"], "0-9\n", b"5\n")
634 # missing element: ValueError -> exit 1
635 self._nodeset_t(["-R", "--index", "50", "0-9"], None, b"", 1,
636 b"ERROR: 50 is not in RangeSet\n")
637
638 def test_028_index_nd(self):
639 """test nodeset --index (multidimensional ordering)"""
640 # nD flattening is a cartesian product where the LAST dimension
641 # varies fastest; --index round-trips with -e/expand for every node.
642 # 2-D: da[1-2]p[1-3] expands da1p1 da1p2 da1p3 da2p1 da2p2 da2p3
643 nd2 = "da[1-2]p[1-3]"
644 for idx, node in enumerate(["da1p1", "da1p2", "da1p3",
645 "da2p1", "da2p2", "da2p3"]):
646 self._nodeset_t(["--index", node, nd2], None,
647 ("%d\n" % idx).encode())
648 # 3-D: a[1-2]b[1-2]c[1-2] expands a1b1c1 a1b1c2 a1b2c1 a1b2c2
649 # a2b1c1 a2b1c2 a2b2c1 a2b2c2 (c least significant, a most)
650 nd3 = "a[1-2]b[1-2]c[1-2]"
651 for idx, node in enumerate(["a1b1c1", "a1b1c2", "a1b2c1", "a1b2c2",
652 "a2b1c1", "a2b1c2", "a2b2c1", "a2b2c2"]):
653 self._nodeset_t(["--index", node, nd3], None,
654 ("%d\n" % idx).encode())
655 # rightmost axis varies fastest: p cycles fully before da increments
656 self._nodeset_t(["--index", "da1p1", nd2], None, b"0\n")
657 self._nodeset_t(["--index", "da1p3", nd2], None, b"2\n")
658 self._nodeset_t(["--index", "da2p1", nd2], None, b"3\n")
659 self._nodeset_t(["--index", "da2p3", nd2], None, b"5\n")
660 # last axis is least significant (a1b1c1=0 .. a2b2c2=7)
661 self._nodeset_t(["--index", "a1b1c2", nd3], None, b"1\n")
662 self._nodeset_t(["--index", "a1b2c1", nd3], None, b"2\n")
663 self._nodeset_t(["--index", "a2b1c1", nd3], None, b"4\n")
664 self._nodeset_t(["--index", "a2b2c2", nd3], None, b"7\n")
665 # several vectors in one pattern: larger vector emitted first.
666 # da[1-2]p[1-3] (6 elts) precedes da[5-6]p[1-2] (4 elts)
667 nd2v = "da[1-2]p[1-3],da[5-6]p[1-2]"
668 for idx, node in enumerate(["da1p1", "da1p2", "da1p3",
669 "da2p1", "da2p2", "da2p3",
670 "da5p1", "da5p2", "da6p1", "da6p2"]):
671 self._nodeset_t(["--index", node, nd2v], None,
672 ("%d\n" % idx).encode())
673 self._nodeset_t(["--index", "da5p1", nd2v], None, b"6\n")
674 self._nodeset_t(["--index", "da6p2", nd2v], None, b"9\n")
675
676
677 class CLINodesetGroupResolverTest1(CLINodesetTestBase):
678 """Unit test class for testing CLI/Nodeset.py with custom Group Resolver"""
679
680 def setUp(self):
681 # Special tests that require a default group source set
682 #
683 # The temporary file needs to be persistent during the tests
684 # because GroupResolverConfig does lazy init, this is why we
685 # use an instance variable self.f
686 #
687 self.f = make_temp_file(dedent("""
688 [Main]
689 default: local
690
691 [local]
692 map: echo example[1-100]
693 all: echo example[1-1000]
694 list: echo foo bar moo
695 """).encode())
696 set_std_group_resolver(GroupResolverConfig(self.f.name))
697
698 def tearDown(self):
699 set_std_group_resolver(None)
700 self.f = None # used to release temp file
701
702 def test_022_list(self):
703 """test nodeset --list"""
704 self._nodeset_t(["--list"], None, b"@bar\n@foo\n@moo\n")
705 self._nodeset_t(["-ll"], None, b"@bar example[1-100]\n@foo example[1-100]\n@moo example[1-100]\n")
706 self._nodeset_t(["-lll"], None, b"@bar example[1-100] 100\n@foo example[1-100] 100\n@moo example[1-100] 100\n")
707 self._nodeset_t(["-l", "example[4,95]", "example5"], None, b"@bar\n@foo\n@moo\n")
708 self._nodeset_t(["-ll", "example[4,95]", "example5"], None, b"@bar example[4-5,95]\n@foo example[4-5,95]\n@moo example[4-5,95]\n")
709 self._nodeset_t(["-lll", "example[4,95]", "example5"], None, b"@bar example[4-5,95] 3/100\n@foo example[4-5,95] 3/100\n@moo example[4-5,95] 3/100\n")
710 # test empty result
711 self._nodeset_t(["-l", "foo[3-70]", "bar6"], None, b"")
712 # more arg-mixed tests
713 self._nodeset_t(["-a", "-l"], None, b"@bar\n@foo\n@moo\n")
714 self._nodeset_t(["-a", "-l", "-x example[1-100]"], None, b"")
715 self._nodeset_t(["-a", "-l", "-x example[1-40]"], None, b"@bar\n@foo\n@moo\n")
716 self._nodeset_t(["-l", "-x example3"], None, b"") # no -a, remove from nothing
717 self._nodeset_t(["-l", "-i example3"], None, b"") # no -a, intersect from nothing
718 self._nodeset_t(["-l", "-X example3"], None, b"@bar\n@foo\n@moo\n") # no -a, xor from nothing
719 self._nodeset_t(["-l", "-", "-i example3"], "example[3,500]\n", b"@bar\n@foo\n@moo\n")
720
721 def test_023_list_all(self):
722 """test nodeset --list-all"""
723 self._nodeset_t(["--list-all"], None, b"@bar\n@foo\n@moo\n")
724 self._nodeset_t(["-L"], None, b"@bar\n@foo\n@moo\n")
725 self._nodeset_t(["-LL"], None, b"@bar example[1-100]\n@foo example[1-100]\n@moo example[1-100]\n")
726 self._nodeset_t(["-LLL"], None, b"@bar example[1-100] 100\n@foo example[1-100] 100\n@moo example[1-100] 100\n")
727
728
729 class CLINodesetGroupResolverTest2(CLINodesetTestBase):
730 """Unit test class for testing CLI/Nodeset.py with custom Group Resolver"""
731
732 def setUp(self):
733 # Special tests that require a default group source set
734 self.f = make_temp_file(dedent("""
735 [Main]
736 default: test
737
738 [test]
739 map: echo example[1-100]
740 all: echo @foo,@bar,@moo
741 list: echo foo bar moo
742
743 [other]
744 map: echo nova[030-489]
745 all: echo @baz,@qux,@norf
746 list: echo baz qux norf
747 """).encode())
748 set_std_group_resolver(GroupResolverConfig(self.f.name))
749
750 def tearDown(self):
751 set_std_group_resolver(None)
752 self.f = None # used to release temp file
753
754 def test_024_groups(self):
755 self._nodeset_t(["--split=2", "-r", "unknown2", "unknown3"], None, b"unknown2\nunknown3\n")
756 self._nodeset_t(["-f", "-a"], None, b"example[1-100]\n")
757 self._nodeset_t(["-f", "@moo"], None, b"example[1-100]\n")
758 self._nodeset_t(["-f", "@moo", "@bar"], None, b"example[1-100]\n")
759 self._nodeset_t(["-e", "-a"], None, ' '.join(["example%d" % i for i in range(1, 101)]).encode() + b'\n')
760 self._nodeset_t(["-c", "-a"], None, b"100\n")
761 self._nodeset_t(["-r", "-a"], None, b"@bar\n")
762 self._nodeset_t(["--split=2", "-r", "unknown2", "unknown3"], None, b"unknown2\nunknown3\n")
763
764 # We need to split following unit tests in order to reset group
765 # source in setUp/tearDown...
766
767 def test_025_groups(self):
768 self._nodeset_t(["-s", "test", "-c", "-a", "-d"], None, b"100\n")
769
770 def test_026_groups(self):
771 self._nodeset_t(["-s", "test", "-r", "-a"], None, b"@test:bar\n")
772
773 def test_027_groups(self):
774 self._nodeset_t(["-s", "test", "-G", "-r", "-a"], None, b"@bar\n")
775
776 def test_028_groups(self):
777 self._nodeset_t(["-s", "test", "--groupsources"], None, b"test (default)\nother\n")
778
779 def test_029_groups(self):
780 self._nodeset_t(["-s", "test", "-q", "--groupsources"], None, b"test\nother\n")
781
782 def test_030_groups(self):
783 self._nodeset_t(["-f", "-a", "-"], "example101\n", b"example[1-101]\n")
784 self._nodeset_t(["-f", "-a", "-"], "example102 example101\n", b"example[1-102]\n")
785
786 # Check default group source switching...
787
788 def test_031_groups(self):
789 self._nodeset_t(["-s", "other", "-c", "-a", "-d"], None, b"460\n")
790 self._nodeset_t(["-s", "test", "-c", "-a", "-d"], None, b"100\n")
791
792 def test_032_groups(self):
793 self._nodeset_t(["-s", "other", "-r", "-a"], None, b"@other:baz\n")
794 self._nodeset_t(["-s", "test", "-r", "-a"], None, b"@test:bar\n")
795
796 def test_033_groups(self):
797 self._nodeset_t(["-s", "other", "-G", "-r", "-a"], None, b"@baz\n")
798 self._nodeset_t(["-s", "test", "-G", "-r", "-a"], None, b"@bar\n")
799
800 def test_034_groups(self):
801 self._nodeset_t(["--groupsources"], None, b"test (default)\nother\n")
802
803 def test_035_groups(self):
804 self._nodeset_t(["-s", "other", "--groupsources"], None, b"other (default)\ntest\n")
805
806 def test_036_groups(self):
807 self._nodeset_t(["--groupsources"], None, b"test (default)\nother\n")
808
809 def test_037_groups_output_format(self):
810 self._nodeset_t(["-r", "-O", "{%s}", "-a"], None, b"{@bar}\n")
811
812 def test_038_groups_output_format(self):
813 self._nodeset_t(["-O", "{%s}", "-s", "other", "-r", "-a"], None, b"{@other:baz}\n")
814
815 def test_039_list_all(self):
816 """test nodeset --list-all (multi sources)"""
817 self._nodeset_t(["--list-all"], None,
818 b"@bar\n@foo\n@moo\n@other:baz\n@other:norf\n@other:qux\n")
819 self._nodeset_t(["--list-all", "-G"], None,
820 b"@bar\n@foo\n@moo\n@baz\n@norf\n@qux\n")
821 self._nodeset_t(["-GL"], None,
822 b"@bar\n@foo\n@moo\n@baz\n@norf\n@qux\n")
823 self._nodeset_t(["--list-all", "-s", "other"], None,
824 b"@baz\n@norf\n@qux\n@test:bar\n@test:foo\n@test:moo\n")
825 self._nodeset_t(["--list-all", "-G", "-s", "other"], None,
826 b"@baz\n@norf\n@qux\n@bar\n@foo\n@moo\n") # 'other' source first
827
828 def test_040_wildcards(self):
829 """test nodeset with wildcards"""
830 self._nodeset_t(["-f", "*"], None, b"example[1-100]\n")
831 self._nodeset_t(["-f", "x*"], None, b"\n") # no match
832 self._nodeset_t(["-s", "other", "-f", "*"], None, b"nova[030-489]\n")
833 self._nodeset_t(["-G", "-s", "other", "-f", "*"], None,
834 b"nova[030-489]\n")
835 self._nodeset_t(["-s", "other", "-f", "nova0??"], None,
836 b"nova[030-099]\n")
837 self._nodeset_t(["-s", "other", "-f", "nova?[12-42]"], None,
838 b"nova[030-042,112-142,212-242,312-342,412-442]\n")
839 self._nodeset_t(["-s", "other", "-f", "*!*[033]"], None,
840 b"nova[030-032,034-489]\n")
841 self._nodeset_t(["-s", "other", "--autostep=3", "-f", "*!*[033-099/2]"],
842 None, b"nova[030-032,034-100/2,101-489]\n")
843
844 def test_041_completion(self):
845 """test nodeset --completion"""
846 self._nodeset_t(["--completion"], None,
847 b"@test:\n@other:\n@bar\n@foo\n@moo\n")
848 self._nodeset_t(["--completion", "node1", "node2"], None,
849 b"@test:\n@other:\n@bar\n@foo\n@moo\nnode1 node2\n")
850 self._nodeset_t(["-s", "other", "--completion"], None,
851 b"@other:baz\n@other:norf\n@other:qux\n")
852 self._nodeset_t(["-s", "other", "--completion", "node1", "node2"], None,
853 b"@other:baz\n@other:norf\n@other:qux\nnode1 node2\n")
854
855
856 class CLINodesetGroupResolverTest3(CLINodesetTestBase):
857 """Unit test class for testing CLI/Nodeset.py with custom Group Resolver
858
859 A case we support: one of the source misses the list upcall.
860 """
861
862 def setUp(self):
863 # Special tests that require a default group source set
864 self.f = make_temp_file(dedent("""
865 [Main]
866 default: test
867
868 [test]
869 map: echo example[1-100]
870 all: echo @foo,@bar,@moo
871 list: echo foo bar moo
872
873 [other]
874 map: echo nova[030-489]
875 all: echo @baz,@qux,@norf
876 list: echo baz qux norf
877
878 [pdu]
879 map: echo pdu-[0-3]-[1-2]
880 """).encode())
881 set_std_group_resolver(GroupResolverConfig(self.f.name))
882
883 def tearDown(self):
884 set_std_group_resolver(None)
885 self.f = None # used to release temp file
886
887 def test_list_all(self):
888 """test nodeset --list-all (w/ missing list upcall)"""
889 self._nodeset_t(["--list-all"], None,
890 b"@bar\n@foo\n@moo\n@other:baz\n@other:norf\n@other:qux\n", 0,
891 b"Warning: No list upcall defined for group source pdu\n")
892 self._nodeset_t(["-LL"], None,
893 b"@bar example[1-100]\n@foo example[1-100]\n@moo example[1-100]\n"
894 b"@other:baz nova[030-489]\n@other:norf nova[030-489]\n@other:qux nova[030-489]\n", 0,
895 b"Warning: No list upcall defined for group source pdu\n")
896 self._nodeset_t(["-LLL"], None,
897 b"@bar example[1-100] 100\n@foo example[1-100] 100\n@moo example[1-100] 100\n"
898 b"@other:baz nova[030-489] 460\n@other:norf nova[030-489] 460\n@other:qux nova[030-489] 460\n", 0,
899 b"Warning: No list upcall defined for group source pdu\n")
900
901 def test_list_failure(self):
902 """test nodeset --list -s source w/ missing list upcall"""
903 self._nodeset_t(["--list", "-s", "pdu"], None, b"", 1,
904 b'No list upcall defined for group source "pdu"\n')
905
906
907 class CLINodesetGroupResolverConfigErrorTest(CLINodesetTestBase):
908 """Unit test class for testing GroupResolverConfigError"""
909
910 def setUp(self):
911 self.tdir = make_temp_dir()
912 self.gconff = make_temp_file(dedent("""
913 [Main]
914 default: default
915 autodir: %s
916 """ % self.tdir.name).encode('ascii'))
917 self.yamlf = make_temp_file(dedent("""
918 default:
919 compute: 'foo'
920 broken: i am not a dict
921 """).encode('ascii'), suffix=".yaml", dir=self.tdir.name)
922
923 set_std_group_resolver(GroupResolverConfig(self.gconff.name))
924
925 def tearDown(self):
926 set_std_group_resolver(None)
927 self.yamlf.close()
928 self.gconff.close()
929 self.tdir.cleanup()
930
931 def test_bad_yaml_config(self):
932 """test nodeset with bad yaml config"""
933 self._nodeset_t(["--list-all"], None, b"", 1,
934 b"invalid content (group source 'broken' is not a dict)\n")
935
936
937 class CLINodesetEmptyGroupsConf(CLINodesetTestBase):
938 """Unit test class for testing empty groups.conf"""
939
940 def setUp(self):
941 self.gconff = make_temp_file(b"")
942 set_std_group_resolver(GroupResolverConfig(self.gconff.name))
943
944 def tearDown(self):
945 set_std_group_resolver(None)
946 self.gconff = None
947
948 def test_empty_groups_conf(self):
949 """test nodeset with empty groups.conf"""
950 self._nodeset_t(["--list-all"], None, b"")
951
952
953 class CLINodesetMalformedGroupsConf(CLINodesetTestBase):
954 """Unit test class for testing malformed groups.conf"""
955
956 def setUp(self):
957 self.gconff = make_temp_file(b"[Main")
958 set_std_group_resolver(GroupResolverConfig(self.gconff.name))
959
960 def tearDown(self):
961 set_std_group_resolver(None)
962 self.gconff = None
963
964 def test_malformed_groups_conf(self):
965 """test nodeset with malformed groups.conf"""
966 self._nodeset_t(["--list-all"], None, b"", 1, b"'[Main'\n")
967
968
969 class CLINodesetGroupsConfOption(CLINodesetTestBase):
970 """Unit test class for testing --groupsconf option"""
971
972 def setUp(self):
973 self.gconff = make_temp_file(dedent("""
974 [Main]
975 default: global_default
976
977 [global_default]
978 map: echo example[1-100]
979 all: echo @foo,@bar,@moo
980 list: echo foo bar moo
981 """).encode())
982 set_std_group_resolver_config(self.gconff.name)
983
984 # passed to --groupsconf
985 self.custf = make_temp_file(dedent("""
986 [Main]
987 default: custom
988
989 [custom]
990 map: echo custom[7-42]
991 all: echo @selene,@artemis
992 list: echo selene artemis
993 """).encode())
994
995 def tearDown(self):
996 set_std_group_resolver(None)
997 self.gconff = None
998 self.custf = None
999
1000 def test_groupsconf_option(self):
1001 """test nodeset with --groupsconf"""
1002 self._nodeset_t(["--list-all"], None, b"@bar\n@foo\n@moo\n")
1003 self._nodeset_t(["-f", "@foo"], None, b"example[1-100]\n")
1004 self._nodeset_t(["--groupsconf", self.custf.name, "--list-all"], None, b"@artemis\n@selene\n")
1005 self._nodeset_t(["--groupsconf", self.custf.name, "-f", "@artemis"], None, b"custom[7-42]\n")