Debian Codegraph @

tests/CLIClubakTest.py

raw symbols SCIP
1 # ClusterShell.CLI.Clubak test suite
2 # Written by S. Thiell
3
4 """Unit test for CLI.Clubak"""
5
6 import re
7 from textwrap import dedent
8 import unittest
9
10 from .TLib import *
11 from ClusterShell.CLI.Clubak import main
12 from ClusterShell.Defaults import DEFAULTS
13
14 from ClusterShell.NodeSet import set_std_group_resolver, \
15 set_std_group_resolver_config
16
17
18 def _outfmt(*args):
19 outfmt = "---------------\n%s\n---------------\n bar\n"
20 res = outfmt % args
21 return res.encode()
22
23 def _outfmt_verb(*args):
24 outfmt = "INPUT foo: bar\n---------------\n%s\n---------------\n bar\n"
25 res = outfmt % args
26 return res.encode()
27
28
29 class CLIClubakTest(unittest.TestCase):
30 """Unit test class for testing CLI/Clubak.py"""
31
32 def _clubak_t(self, args, stdin, expected_stdout, expected_rc=0,
33 expected_stderr=None):
34 CLI_main(self, main, ['clubak'] + args, stdin, expected_stdout,
35 expected_rc, expected_stderr)
36
37 def test_000_noargs(self):
38 """test clubak (no argument)"""
39 self._clubak_t([], b"foo: bar\n", _outfmt('foo'))
40 self._clubak_t([], b"foo space: bar\n", _outfmt('foo space'))
41 self._clubak_t([], b"foo space1: bar\n", _outfmt('foo space1'))
42 self._clubak_t([], b"foo space1: bar\nfoo space2: bar",
43 _outfmt('foo space1') + _outfmt('foo space2'))
44 self._clubak_t([], b": bar\n", b'', 1, b'clubak: no node found: ": bar"\n')
45 self._clubak_t([], b"foo[: bar\n", _outfmt('foo['))
46 self._clubak_t([], b"]o[o]: bar\n", _outfmt(']o[o]'))
47 self._clubak_t([], b"foo:\n", b'---------------\nfoo\n---------------\n\n')
48 self._clubak_t([], b"foo: \n", b'---------------\nfoo\n---------------\n \n')
49 # nD
50 self._clubak_t([], b"n1c1: bar\n", _outfmt('n1c1'))
51 # Ticket #286
52 self._clubak_t([], b"n1c01: bar\n", _outfmt('n1c01'))
53 self._clubak_t([], b"n01c01: bar\n", _outfmt('n01c01'))
54 self._clubak_t([], b"n001c01: bar\nn001c02: bar\n",
55 _outfmt('n001c01') + _outfmt('n001c02'))
56
57 def test_001_verbosity(self):
58 """test clubak (-q/-v/-d)"""
59 self._clubak_t(["-d"], b"foo: bar\n", _outfmt_verb('foo'), 0,
60 b'line_mode=False gather=False tree_depth=1\n')
61 self._clubak_t(["-d", "-b"], b"foo: bar\n", _outfmt_verb('foo'), 0,
62 b'line_mode=False gather=True tree_depth=1\n')
63 self._clubak_t(["-d", "-L"], b"foo: bar\n", b'INPUT foo: bar\nfoo: bar\n', 0,
64 b'line_mode=True gather=False tree_depth=1\n')
65 self._clubak_t(["-v"], b"foo: bar\n", _outfmt_verb('foo'), 0)
66 self._clubak_t(["-v", "-b"], b"foo: bar\n", _outfmt_verb('foo'), 0)
67 # no node count with -q
68 self._clubak_t(["-q", "-b"], b"foo[1-5]: bar\n", _outfmt('foo[1-5]'), 0)
69 # non-printable characters replaced by the replacement character
70 self._clubak_t(["-L"], b"foo:\xffbar\n", "foo: \ufffdbar\n".encode(), 0)
71 self._clubak_t(["-d", "-L"], b"foo:\xf8bar\n",
72 'INPUT foo:\ufffdbar\nfoo: \ufffdbar\n'.encode(), 0,
73 b'line_mode=True gather=False tree_depth=1\n')
74
75 def test_002_b(self):
76 """test clubak (gather -b)"""
77 self._clubak_t(["-b"], b"foo: bar\n", _outfmt('foo'))
78 self._clubak_t(["-b"], b"foo space: bar\n", _outfmt("foo space"))
79 self._clubak_t(["-b"], b"foo space1: bar\n", _outfmt("foo space1"))
80 self._clubak_t(["-b"], b"foo space1: bar\nfoo space2: bar",
81 _outfmt("foo space[1-2] (2)"))
82 self._clubak_t(["-b"], b"foo space1: bar\nfoo space2: foo",
83 b"---------------\nfoo space1\n---------------\n bar\n---------------\nfoo space2\n---------------\n foo\n")
84 self._clubak_t(["-b"], b": bar\n", b"", 1, b'clubak: no node found: ": bar"\n')
85 self._clubak_t(["-b"], b"foo[: bar\n", _outfmt("foo["))
86 self._clubak_t(["-b"], b"]o[o]: bar\n", _outfmt("]o[o]"))
87 self._clubak_t(["-b"], b"foo:\n", b"---------------\nfoo\n---------------\n\n")
88 self._clubak_t(["-b"], b"foo: \n", b"---------------\nfoo\n---------------\n \n")
89 # nD
90 self._clubak_t(["-b"], b"n1c1: bar\n", _outfmt("n1c1"))
91 # Ticket #286
92 self._clubak_t(["-b"], b"n1c01: bar\n", _outfmt("n1c01"))
93 self._clubak_t(["-b"], b"n001c01: bar\n", _outfmt("n001c01"))
94 self._clubak_t(["-b"], b"n001c01: bar\nn001c02: bar\n", _outfmt("n001c[01-02] (2)"))
95
96 def test_003_L(self):
97 """test clubak (line mode -L)"""
98 self._clubak_t(["-L"], b"foo: bar\n", b"foo: bar\n")
99 self._clubak_t(["-L", "-S", ": "], b"foo: bar\n", b"foo: bar\n")
100 self._clubak_t(["-bL"], b"foo: bar\n", b"foo: bar\n")
101 self._clubak_t(["-bL", "-S", ": "], b"foo: bar\n", b"foo: bar\n")
102 # nD
103 self._clubak_t(["-bL", "-S", ": "], b"n1c01: bar\n", b"n1c01: bar\n")
104
105 def test_004_N(self):
106 """test clubak (no header -N)"""
107 self._clubak_t(["-N"], b"foo: bar\n", b" bar\n")
108 self._clubak_t(["-NL"], b"foo: bar\n", b" bar\n")
109 self._clubak_t(["-N", "-S", ": "], b"foo: bar\n", b"bar\n")
110 self._clubak_t(["-bN"], b"foo: bar\n", b" bar\n")
111 self._clubak_t(["-bN", "-S", ": "], b"foo: bar\n", b"bar\n")
112
113 def test_005_fast(self):
114 """test clubak (fast mode --fast)"""
115 self._clubak_t(["--fast"], b"foo: bar\n", _outfmt("foo"))
116 self._clubak_t(["-b", "--fast"], b"foo: bar\n", _outfmt("foo"))
117 self._clubak_t(["-b", "--fast"], b"foo2: bar\nfoo1: bar\nfoo4: bar",
118 _outfmt("foo[1-2,4] (3)"))
119 # check conflicting options
120 self._clubak_t(["-L", "--fast"], b"foo2: bar\nfoo1: bar\nfoo4: bar",
121 b'', 2, b"error: incompatible tree options\n")
122
123 def test_006_tree(self):
124 """test clubak (tree mode --tree)"""
125 self._clubak_t(["--tree"], b"foo: bar\n", _outfmt("foo"))
126 self._clubak_t(["--tree", "-L"], b"foo: bar\n", b"foo:\n bar\n")
127 self._clubak_t(["--tree", "-L"], b"foo: \xf8bar\n",
128 "foo:\n \ufffdbar\n".encode())
129 stdin_buf = dedent("""foo1:bar
130 foo2:bar
131 foo1:moo
132 foo1:bla
133 foo2:m00
134 foo2:bla
135 foo1:abc
136 """).encode()
137 self._clubak_t(["--tree", "-L"], stdin_buf,
138 re.compile(br"(foo\[1-2\]:\nbar\nfoo2:\n m00\n bla\nfoo1:\n moo\n bla\n abc\n"
139 br"|foo\[1-2\]:\nbar\nfoo1:\n moo\n bla\n abc\nfoo2:\n m00\n)"))
140 # check conflicting options
141 self._clubak_t(["--tree", "--fast"], stdin_buf, b'', 2,
142 b"error: incompatible tree options\n")
143
144 def test_007_interpret_keys(self):
145 """test clubak (--interpret-keys)"""
146 self._clubak_t(["--interpret-keys=auto"], b"foo: bar\n", _outfmt("foo"))
147 self._clubak_t(["-b", "--interpret-keys=auto"], b"foo: bar\n", _outfmt("foo"))
148 self._clubak_t(["-b", "--interpret-keys=never"], b"foo: bar\n", _outfmt("foo"))
149 self._clubak_t(["-b", "--interpret-keys=always"], b"foo: bar\n", _outfmt("foo"))
150 self._clubak_t(["-b", "--interpret-keys=always"], b"foo[1-3]: bar\n",
151 _outfmt("foo[1-3] (3)"))
152 self._clubak_t(["-b", "--interpret-keys=auto"], b"[]: bar\n", _outfmt("[]"))
153 self._clubak_t(["-b", "--interpret-keys=never"], b"[]: bar\n", _outfmt("[]"))
154 self._clubak_t(["-b", "--interpret-keys=always"], b"[]: bar\n",
155 b'', 1, b"Parse error: bad range: \"empty range\"\n")
156
157 def test_008_color(self):
158 """test clubak (--color)"""
159 self._clubak_t(["-b"], b"foo: bar\n", _outfmt("foo"))
160 self._clubak_t(["-b", "--color=never"], b"foo: bar\n", _outfmt("foo"))
161 self._clubak_t(["-b", "--color=auto"], b"foo: bar\n", _outfmt("foo"))
162 self._clubak_t(["-L", "--color=always"], b"foo: bar\n",
163 b"\x1b[94mfoo: \x1b[0m bar\n")
164 self._clubak_t(["-b", "--color=always"], b"foo: bar\n",
165 b"\x1b[94m---------------\nfoo\n---------------\x1b[0m\n bar\n")
166
167 def test_009_diff(self):
168 """test clubak (--diff)"""
169 self._clubak_t(["--diff"], b"foo1: bar\nfoo2: bar", b'')
170 self._clubak_t(["--diff"], b"foo1: bar\nfoo2: BAR\nfoo2: end\nfoo1: end",
171 b"--- foo1\n+++ foo2\n@@ -1,2 +1,2 @@\n- bar\n+ BAR\n end\n")
172 self._clubak_t(["--diff"], b"foo1: bar\nfoo2: BAR\nfoo3: bar\nfoo2: end\nfoo1: end\nfoo3: end",
173 b"--- foo[1,3] (2)\n+++ foo2\n@@ -1,2 +1,2 @@\n- bar\n+ BAR\n end\n")
174 self._clubak_t(["--diff", "--color=always"], b"foo1: bar\nfoo2: BAR\nfoo3: bar\nfoo2: end\nfoo1: end\nfoo3: end",
175 b"\x1b[1m--- foo[1,3] (2)\x1b[0m\n\x1b[1m+++ foo2\x1b[0m\n\x1b[96m@@ -1,2 +1,2 @@\x1b[0m\n\x1b[91m- bar\x1b[0m\n\x1b[92m+ BAR\x1b[0m\n end\n")
176 self._clubak_t(["--diff", "-d"], b"foo: bar\n", b"INPUT foo: bar\n", 0,
177 b"line_mode=False gather=True tree_depth=1\n")
178 self._clubak_t(["--diff", "-L"], b"foo1: bar\nfoo2: bar", b'', 2,
179 b"error: option mismatch (diff not supported in line_mode)\n")
180 # GH #400
181 self._clubak_t(["--diff"], b"host1: \xc3\xa5\nhost2: a\nhost2: b\nhost1: b\n",
182 b"--- host1\n+++ host2\n@@ -1,2 +1,2 @@\n- \xc3\xa5\n+ a\n b\n")
183
184 def test_010_axis(self):
185 """test clubak --axis (fold gathered output along selected axis)"""
186 # 2D node keys with identical content gather into a single block
187 # whose header folds along the requested axis.
188 nd = b"foo1-1: bar\nfoo1-2: bar\nfoo2-1: bar\nfoo2-2: bar\n"
189 fold_axis_save = DEFAULTS.fold_axis
190 try:
191 DEFAULTS.fold_axis = ()
192 # no --axis: fold along all axis (default)
193 self._clubak_t(["-b"], nd,
194 b"---------------\nfoo[1-2]-[1-2] (4)\n"
195 b"---------------\n bar\n")
196 # --axis=1: fold first dimension only
197 self._clubak_t(["--axis=1", "-b"], nd,
198 b"---------------\nfoo[1-2]-1,foo[1-2]-2 (4)\n"
199 b"---------------\n bar\n")
200 # --axis=2: fold second dimension only
201 self._clubak_t(["--axis=2", "-b"], nd,
202 b"---------------\nfoo1-[1-2],foo2-[1-2] (4)\n"
203 b"---------------\n bar\n")
204 # --axis=-1: fold last dimension only
205 self._clubak_t(["--axis=-1", "-b"], nd,
206 b"---------------\nfoo1-[1-2],foo2-[1-2] (4)\n"
207 b"---------------\n bar\n")
208 # interpret-keys=never: raw key, --axis is a no-op
209 self._clubak_t(["--axis=1", "--interpret-keys=never", "-b"],
210 b"foo1-1: bar\n",
211 b"---------------\nfoo1-1\n---------------\n bar\n")
212 finally:
213 DEFAULTS.fold_axis = fold_axis_save
214
215
216 class CLIClubakTestGroupsConf(CLIClubakTest):
217 """Unit test class for testing --groupsconf option"""
218
219 def setUp(self):
220 self.gconff = make_temp_file(dedent("""
221 [Main]
222 default: global_default
223
224 [global_default]
225 map: echo foo[1-2]
226 all: echo @foo
227 list: echo foo
228 """).encode())
229 set_std_group_resolver_config(self.gconff.name)
230
231 # passed to --groupsconf
232 self.custf = make_temp_file(dedent("""
233 [Main]
234 default: custom
235
236 [custom]
237 map: echo foo[1-2]
238 all: echo @bar
239 list: echo bar
240 """).encode())
241
242 def tearDown(self):
243 set_std_group_resolver(None)
244 self.gconff = None
245 self.custf = None
246
247 def test_groupsconf_option(self):
248 """test nodeset with --groupsconf"""
249 # use -r (--regroup) to test group resolution
250 self._clubak_t(["-r"], b"foo1: bar\nfoo2: bar", _outfmt("@foo (2)"))
251 self._clubak_t(["--groupsconf", self.custf.name, "-r"],
252 b"foo1: bar\nfoo2: bar", _outfmt("@bar (2)"))