]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/pybind/test_ceph_argparse.py
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / test / pybind / test_ceph_argparse.py
1 #!/usr/bin/env python3
2 # -*- mode:python; tab-width:4; indent-tabs-mode:t; coding:utf-8 -*-
3 # vim: ts=4 sw=4 smarttab expandtab fileencoding=utf-8
4 #
5 # Ceph - scalable distributed file system
6 #
7 # Copyright (C) 2013,2014 Cloudwatt <libre.licensing@cloudwatt.com>
8 # Copyright (C) 2014 Red Hat <contact@redhat.com>
9 #
10 # Author: Loic Dachary <loic@dachary.org>
11 #
12 # This library is free software; you can redistribute it and/or
13 # modify it under the terms of the GNU Lesser General Public
14 # License as published by the Free Software Foundation; either
15 # version 2.1 of the License, or (at your option) any later version.
16 #
17
18 from nose.tools import eq_ as eq
19 from nose.tools import *
20 from unittest import TestCase
21
22 from ceph_argparse import validate_command, parse_json_funcsigs, validate, \
23 parse_funcsig, ArgumentError, ArgumentTooFew, ArgumentMissing, \
24 ArgumentNumber, ArgumentValid
25
26 import os
27 import random
28 import re
29 import string
30 import sys
31 import json
32 try:
33 from StringIO import StringIO
34 except ImportError:
35 from io import StringIO
36
37 def get_command_descriptions(what):
38 CEPH_BIN = os.environ['CEPH_BIN']
39 if CEPH_BIN == "":
40 CEPH_BIN = "."
41 return os.popen(CEPH_BIN + "/get_command_descriptions " + "--" + what).read()
42
43 def test_parse_json_funcsigs():
44 commands = get_command_descriptions("all")
45 cmd_json = parse_json_funcsigs(commands, 'cli')
46
47 # syntax error https://github.com/ceph/ceph/pull/585
48 commands = get_command_descriptions("pull585")
49 assert_raises(TypeError, parse_json_funcsigs, commands, 'cli')
50
51 sigdict = parse_json_funcsigs(get_command_descriptions("all"), 'cli')
52
53
54 class TestArgparse:
55
56 def assert_valid_command(self, args):
57 result = validate_command(sigdict, args)
58 assert_not_in(result, [{}, None])
59
60 def check_1_natural_arg(self, prefix, command):
61 self.assert_valid_command([prefix, command, '1'])
62 assert_equal({}, validate_command(sigdict, [prefix, command]))
63 assert_equal({}, validate_command(sigdict, [prefix, command, '-1']))
64 assert_equal({}, validate_command(sigdict, [prefix, command, '1',
65 '1']))
66
67 def check_0_or_1_natural_arg(self, prefix, command):
68 self.assert_valid_command([prefix, command, '1'])
69 self.assert_valid_command([prefix, command])
70 assert_equal({}, validate_command(sigdict, [prefix, command, '-1']))
71 assert_equal({}, validate_command(sigdict, [prefix, command, '1',
72 '1']))
73
74 def check_1_string_arg(self, prefix, command):
75 assert_equal({}, validate_command(sigdict, [prefix, command]))
76 self.assert_valid_command([prefix, command, 'string'])
77 assert_equal({}, validate_command(sigdict, [prefix,
78 command,
79 'string',
80 'toomany']))
81
82 def check_0_or_1_string_arg(self, prefix, command):
83 self.assert_valid_command([prefix, command, 'string'])
84 self.assert_valid_command([prefix, command])
85 assert_equal({}, validate_command(sigdict, [prefix, command, 'string',
86 'toomany']))
87
88 def check_1_or_more_string_args(self, prefix, command):
89 assert_equal({}, validate_command(sigdict, [prefix,
90 command]))
91 self.assert_valid_command([prefix,
92 command,
93 'string'])
94 self.assert_valid_command([prefix,
95 command,
96 'string',
97 'more string'])
98
99 def check_no_arg(self, prefix, command):
100 self.assert_valid_command([prefix,
101 command])
102 assert_equal({}, validate_command(sigdict, [prefix,
103 command,
104 'toomany']))
105
106 def capture_output(self, args, stdout=None, stderr=None):
107 if stdout:
108 stdout = StringIO()
109 sys.stdout = stdout
110 if stderr:
111 stderr = StringIO()
112 sys.stderr = stderr
113 ret = validate_command(sigdict, args)
114 if stdout:
115 stdout = stdout.getvalue().strip()
116 if stderr:
117 stderr = stderr.getvalue().strip()
118 return ret, stdout, stderr
119
120
121 class TestBasic:
122
123 def test_non_ascii_in_non_options(self):
124 # ArgumentPrefix("no match for {0}".format(s)) is not able to convert
125 # unicode str parameter into str. and validate_command() should not
126 # choke on it.
127 assert_equal({}, validate_command(sigdict, [u'章鱼和鱿鱼']))
128 assert_equal({}, validate_command(sigdict, [u'–w']))
129 # actually we always pass unicode strings to validate_command() in "ceph"
130 # CLI, but we also use bytestrings in our tests, so make sure it does not
131 # break.
132 assert_equal({}, validate_command(sigdict, ['章鱼和鱿鱼']))
133 assert_equal({}, validate_command(sigdict, ['–w']))
134
135
136 class TestPG(TestArgparse):
137
138 def test_stat(self):
139 self.assert_valid_command(['pg', 'stat'])
140
141 def test_getmap(self):
142 self.assert_valid_command(['pg', 'getmap'])
143
144 def test_dump(self):
145 self.assert_valid_command(['pg', 'dump'])
146 self.assert_valid_command(['pg', 'dump',
147 'all',
148 'summary',
149 'sum',
150 'delta',
151 'pools',
152 'osds',
153 'pgs',
154 'pgs_brief'])
155 assert_equal({}, validate_command(sigdict, ['pg', 'dump', 'invalid']))
156
157 def test_dump_json(self):
158 self.assert_valid_command(['pg', 'dump_json'])
159 self.assert_valid_command(['pg', 'dump_json',
160 'all',
161 'summary',
162 'sum',
163 'pools',
164 'osds',
165 'pgs'])
166 assert_equal({}, validate_command(sigdict, ['pg', 'dump_json',
167 'invalid']))
168
169 def test_dump_pools_json(self):
170 self.assert_valid_command(['pg', 'dump_pools_json'])
171
172 def test_dump_pools_stuck(self):
173 self.assert_valid_command(['pg', 'dump_stuck'])
174 self.assert_valid_command(['pg', 'dump_stuck',
175 'inactive',
176 'unclean',
177 'stale'])
178 assert_equal({}, validate_command(sigdict, ['pg', 'dump_stuck',
179 'invalid']))
180 self.assert_valid_command(['pg', 'dump_stuck',
181 'inactive',
182 '1234'])
183
184 def one_pgid(self, command):
185 self.assert_valid_command(['pg', command, '1.1'])
186 assert_equal({}, validate_command(sigdict, ['pg', command]))
187 assert_equal({}, validate_command(sigdict, ['pg', command, '1']))
188
189 def test_map(self):
190 self.one_pgid('map')
191
192 def test_scrub(self):
193 self.one_pgid('scrub')
194
195 def test_deep_scrub(self):
196 self.one_pgid('deep-scrub')
197
198 def test_repair(self):
199 self.one_pgid('repair')
200
201 def test_debug(self):
202 self.assert_valid_command(['pg',
203 'debug',
204 'unfound_objects_exist'])
205 self.assert_valid_command(['pg',
206 'debug',
207 'degraded_pgs_exist'])
208 assert_equal({}, validate_command(sigdict, ['pg', 'debug']))
209 assert_equal({}, validate_command(sigdict, ['pg', 'debug',
210 'invalid']))
211
212 def test_pg_missing_args_output(self):
213 ret, _, stderr = self.capture_output(['pg'], stderr=True)
214 assert_equal({}, ret)
215 assert_regexp_matches(stderr, re.compile('no valid command found.* closest matches'))
216
217 def test_pg_wrong_arg_output(self):
218 ret, _, stderr = self.capture_output(['pg', 'map', 'bad-pgid'],
219 stderr=True)
220 assert_equal({}, ret)
221 assert_in("Invalid command", stderr)
222
223
224 class TestAuth(TestArgparse):
225
226 def test_export(self):
227 self.assert_valid_command(['auth', 'export'])
228 self.assert_valid_command(['auth',
229 'export',
230 'string'])
231 assert_equal({}, validate_command(sigdict, ['auth',
232 'export',
233 'string',
234 'toomany']))
235
236 def test_get(self):
237 self.check_1_string_arg('auth', 'get')
238
239 def test_get_key(self):
240 self.check_1_string_arg('auth', 'get-key')
241
242 def test_print_key(self):
243 self.check_1_string_arg('auth', 'print-key')
244 self.check_1_string_arg('auth', 'print_key')
245
246 def test_list(self):
247 self.check_no_arg('auth', 'list')
248
249 def test_import(self):
250 self.check_no_arg('auth', 'import')
251
252 def test_add(self):
253 self.check_1_or_more_string_args('auth', 'add')
254
255 def test_get_or_create_key(self):
256 self.check_1_or_more_string_args('auth', 'get-or-create-key')
257
258 def test_get_or_create(self):
259 self.check_1_or_more_string_args('auth', 'get-or-create')
260
261 def test_caps(self):
262 assert_equal({}, validate_command(sigdict, ['auth',
263 'caps']))
264 assert_equal({}, validate_command(sigdict, ['auth',
265 'caps',
266 'string']))
267 self.assert_valid_command(['auth',
268 'caps',
269 'string',
270 'more string'])
271
272 def test_del(self):
273 self.check_1_string_arg('auth', 'del')
274
275
276 class TestMonitor(TestArgparse):
277
278 def test_compact(self):
279 self.assert_valid_command(['compact'])
280
281 def test_scrub(self):
282 self.assert_valid_command(['scrub'])
283
284 def test_fsid(self):
285 self.assert_valid_command(['fsid'])
286
287 def test_log(self):
288 assert_equal({}, validate_command(sigdict, ['log']))
289 self.assert_valid_command(['log', 'a logtext'])
290 self.assert_valid_command(['log', 'a logtext', 'and another'])
291
292 def test_injectargs(self):
293 assert_equal({}, validate_command(sigdict, ['injectargs']))
294 self.assert_valid_command(['injectargs', 'one'])
295 self.assert_valid_command(['injectargs', 'one', 'two'])
296
297 def test_status(self):
298 self.assert_valid_command(['status'])
299
300 def test_health(self):
301 self.assert_valid_command(['health'])
302 self.assert_valid_command(['health', 'detail'])
303 assert_equal({}, validate_command(sigdict, ['health', 'invalid']))
304 assert_equal({}, validate_command(sigdict, ['health', 'detail',
305 'toomany']))
306
307 def test_df(self):
308 self.assert_valid_command(['df'])
309 self.assert_valid_command(['df', 'detail'])
310 assert_equal({}, validate_command(sigdict, ['df', 'invalid']))
311 assert_equal({}, validate_command(sigdict, ['df', 'detail',
312 'toomany']))
313
314 def test_report(self):
315 self.assert_valid_command(['report'])
316 self.assert_valid_command(['report', 'tag1'])
317 self.assert_valid_command(['report', 'tag1', 'tag2'])
318
319 def test_quorum_status(self):
320 self.assert_valid_command(['quorum_status'])
321
322 def test_tell(self):
323 assert_equal({}, validate_command(sigdict, ['tell']))
324 assert_equal({}, validate_command(sigdict, ['tell', 'invalid']))
325 for name in ('osd', 'mon', 'client', 'mds'):
326 assert_equal({}, validate_command(sigdict, ['tell', name]))
327 assert_equal({}, validate_command(sigdict, ['tell',
328 name + ".42"]))
329 self.assert_valid_command(['tell', name + ".42", 'something'])
330 self.assert_valid_command(['tell', name + ".42",
331 'something',
332 'something else'])
333
334
335 class TestMDS(TestArgparse):
336
337 def test_stat(self):
338 self.check_no_arg('mds', 'stat')
339
340 def test_compat_show(self):
341 self.assert_valid_command(['mds', 'compat', 'show'])
342 assert_equal({}, validate_command(sigdict, ['mds', 'compat']))
343 assert_equal({}, validate_command(sigdict, ['mds', 'compat',
344 'show', 'toomany']))
345
346 def test_set_state(self):
347 self.assert_valid_command(['mds', 'set_state', '1', '2'])
348 assert_equal({}, validate_command(sigdict, ['mds', 'set_state']))
349 assert_equal({}, validate_command(sigdict, ['mds', 'set_state', '-1']))
350 assert_equal({}, validate_command(sigdict, ['mds', 'set_state',
351 '1', '-1']))
352 assert_equal({}, validate_command(sigdict, ['mds', 'set_state',
353 '1', '21']))
354
355 def test_fail(self):
356 self.check_1_string_arg('mds', 'fail')
357
358 def test_rm(self):
359 # Valid: single GID argument present
360 self.assert_valid_command(['mds', 'rm', '1'])
361
362 # Missing GID arg: invalid
363 assert_equal({}, validate_command(sigdict, ['mds', 'rm']))
364 # Extra arg: invalid
365 assert_equal({}, validate_command(sigdict, ['mds', 'rm', '1', 'mds.42']))
366
367 def test_rmfailed(self):
368 self.assert_valid_command(['mds', 'rmfailed', '0'])
369 self.assert_valid_command(['mds', 'rmfailed', '0', '--yes-i-really-mean-it'])
370 assert_equal({}, validate_command(sigdict, ['mds', 'rmfailed', '0',
371 '--yes-i-really-mean-it',
372 'toomany']))
373
374 def test_compat_rm_compat(self):
375 self.assert_valid_command(['mds', 'compat', 'rm_compat', '1'])
376 assert_equal({}, validate_command(sigdict, ['mds',
377 'compat',
378 'rm_compat']))
379 assert_equal({}, validate_command(sigdict, ['mds',
380 'compat',
381 'rm_compat', '-1']))
382 assert_equal({}, validate_command(sigdict, ['mds',
383 'compat',
384 'rm_compat', '1', '1']))
385
386 def test_incompat_rm_incompat(self):
387 self.assert_valid_command(['mds', 'compat', 'rm_incompat', '1'])
388 assert_equal({}, validate_command(sigdict, ['mds',
389 'compat',
390 'rm_incompat']))
391 assert_equal({}, validate_command(sigdict, ['mds',
392 'compat',
393 'rm_incompat', '-1']))
394 assert_equal({}, validate_command(sigdict, ['mds',
395 'compat',
396 'rm_incompat', '1', '1']))
397
398
399 class TestFS(TestArgparse):
400
401 def test_dump(self):
402 self.check_0_or_1_natural_arg('fs', 'dump')
403
404 def test_fs_new(self):
405 self.assert_valid_command(['fs', 'new', 'default', 'metadata', 'data'])
406
407 def test_fs_set_max_mds(self):
408 self.assert_valid_command(['fs', 'set', 'default', 'max_mds', '1'])
409 self.assert_valid_command(['fs', 'set', 'default', 'max_mds', '2'])
410
411 def test_fs_set_cluster_down(self):
412 self.assert_valid_command(['fs', 'set', 'default', 'down', 'true'])
413
414 def test_fs_set_cluster_up(self):
415 self.assert_valid_command(['fs', 'set', 'default', 'down', 'false'])
416
417 def test_fs_set_cluster_joinable(self):
418 self.assert_valid_command(['fs', 'set', 'default', 'joinable', 'true'])
419
420 def test_fs_set_cluster_not_joinable(self):
421 self.assert_valid_command(['fs', 'set', 'default', 'joinable', 'false'])
422
423 def test_fs_set(self):
424 self.assert_valid_command(['fs', 'set', 'default', 'max_file_size', '2'])
425 self.assert_valid_command(['fs', 'set', 'default', 'allow_new_snaps', 'no'])
426 assert_equal({}, validate_command(sigdict, ['fs',
427 'set',
428 'invalid']))
429
430 def test_fs_add_data_pool(self):
431 self.assert_valid_command(['fs', 'add_data_pool', 'default', '1'])
432 self.assert_valid_command(['fs', 'add_data_pool', 'default', 'foo'])
433
434 def test_fs_remove_data_pool(self):
435 self.assert_valid_command(['fs', 'rm_data_pool', 'default', '1'])
436 self.assert_valid_command(['fs', 'rm_data_pool', 'default', 'foo'])
437
438 def test_fs_rm(self):
439 self.assert_valid_command(['fs', 'rm', 'default'])
440 self.assert_valid_command(['fs', 'rm', 'default', '--yes-i-really-mean-it'])
441 assert_equal({}, validate_command(sigdict, ['fs', 'rm', 'default', '--yes-i-really-mean-it', 'toomany']))
442
443 def test_fs_ls(self):
444 self.assert_valid_command(['fs', 'ls'])
445 assert_equal({}, validate_command(sigdict, ['fs', 'ls', 'toomany']))
446
447 def test_fs_set_default(self):
448 self.assert_valid_command(['fs', 'set-default', 'cephfs'])
449 assert_equal({}, validate_command(sigdict, ['fs', 'set-default']))
450 assert_equal({}, validate_command(sigdict, ['fs', 'set-default', 'cephfs', 'toomany']))
451
452
453 class TestMon(TestArgparse):
454
455 def test_dump(self):
456 self.check_0_or_1_natural_arg('mon', 'dump')
457
458 def test_stat(self):
459 self.check_no_arg('mon', 'stat')
460
461 def test_getmap(self):
462 self.check_0_or_1_natural_arg('mon', 'getmap')
463
464 def test_add(self):
465 self.assert_valid_command(['mon', 'add', 'name', '1.2.3.4:1234'])
466 assert_equal({}, validate_command(sigdict, ['mon', 'add']))
467 assert_equal({}, validate_command(sigdict, ['mon', 'add', 'name']))
468 assert_equal({}, validate_command(sigdict, ['mon', 'add',
469 'name',
470 '400.500.600.700']))
471
472 def test_remove(self):
473 self.assert_valid_command(['mon', 'remove', 'name'])
474 assert_equal({}, validate_command(sigdict, ['mon', 'remove']))
475 assert_equal({}, validate_command(sigdict, ['mon', 'remove',
476 'name', 'toomany']))
477
478
479 class TestOSD(TestArgparse):
480
481 def test_stat(self):
482 self.check_no_arg('osd', 'stat')
483
484 def test_dump(self):
485 self.check_0_or_1_natural_arg('osd', 'dump')
486
487 def test_osd_tree(self):
488 self.check_0_or_1_natural_arg('osd', 'tree')
489
490 def test_osd_ls(self):
491 self.check_0_or_1_natural_arg('osd', 'ls')
492
493 def test_osd_getmap(self):
494 self.check_0_or_1_natural_arg('osd', 'getmap')
495
496 def test_osd_getcrushmap(self):
497 self.check_0_or_1_natural_arg('osd', 'getcrushmap')
498
499 def test_perf(self):
500 self.check_no_arg('osd', 'perf')
501
502 def test_getmaxosd(self):
503 self.check_no_arg('osd', 'getmaxosd')
504
505 def test_find(self):
506 self.check_1_natural_arg('osd', 'find')
507
508 def test_map(self):
509 self.assert_valid_command(['osd', 'map', 'poolname', 'objectname'])
510 self.assert_valid_command(['osd', 'map', 'poolname', 'objectname', 'nspace'])
511 assert_equal({}, validate_command(sigdict, ['osd', 'map']))
512 assert_equal({}, validate_command(sigdict, ['osd', 'map', 'poolname']))
513 assert_equal({}, validate_command(sigdict, ['osd', 'map',
514 'poolname', 'objectname', 'nspace',
515 'toomany']))
516
517 def test_metadata(self):
518 self.check_0_or_1_natural_arg('osd', 'metadata')
519
520 def test_scrub(self):
521 self.check_1_string_arg('osd', 'scrub')
522
523 def test_deep_scrub(self):
524 self.check_1_string_arg('osd', 'deep-scrub')
525
526 def test_repair(self):
527 self.check_1_string_arg('osd', 'repair')
528
529 def test_lspools(self):
530 self.assert_valid_command(['osd', 'lspools'])
531 assert_equal({}, validate_command(sigdict, ['osd', 'lspools',
532 'toomany']))
533
534 def test_blocklist_ls(self):
535 self.assert_valid_command(['osd', 'blocklist', 'ls'])
536 assert_equal({}, validate_command(sigdict, ['osd', 'blocklist']))
537 assert_equal({}, validate_command(sigdict, ['osd', 'blocklist',
538 'ls', 'toomany']))
539
540 def test_crush_rule(self):
541 assert_equal({}, validate_command(sigdict, ['osd', 'crush']))
542 assert_equal({}, validate_command(sigdict, ['osd', 'crush', 'rule']))
543 for subcommand in ('list', 'ls'):
544 self.assert_valid_command(['osd', 'crush', 'rule', subcommand])
545 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
546 'rule', subcommand,
547 'toomany']))
548
549 def test_crush_rule_dump(self):
550 self.assert_valid_command(['osd', 'crush', 'rule', 'dump'])
551 self.assert_valid_command(['osd', 'crush', 'rule', 'dump', 'RULE'])
552 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
553 'rule', 'dump',
554 'RULE',
555 'toomany']))
556
557 def test_crush_dump(self):
558 self.assert_valid_command(['osd', 'crush', 'dump'])
559 assert_equal({}, validate_command(sigdict, ['osd', 'crush']))
560 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
561 'dump',
562 'toomany']))
563
564 def test_setcrushmap(self):
565 self.check_no_arg('osd', 'setcrushmap')
566
567 def test_crush_add_bucket(self):
568 self.assert_valid_command(['osd', 'crush', 'add-bucket',
569 'name', 'type'])
570 self.assert_valid_command(['osd', 'crush', 'add-bucket',
571 'name', 'type', 'root=foo-root', 'host=foo-host'])
572 assert_equal({}, validate_command(sigdict, ['osd', 'crush']))
573 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
574 'add-bucket']))
575 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
576 'add-bucket', '^^^',
577 'type']))
578
579 def test_crush_rename_bucket(self):
580 self.assert_valid_command(['osd', 'crush', 'rename-bucket',
581 'srcname', 'dstname'])
582 assert_equal({}, validate_command(sigdict, ['osd', 'crush']))
583 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
584 'rename-bucket']))
585 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
586 'rename-bucket',
587 'srcname']))
588 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
589 'rename-bucket', 'srcname',
590 'dstname',
591 'toomany']))
592 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
593 'rename-bucket', '^^^',
594 'dstname']))
595 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
596 'rename-bucket', 'srcname',
597 '^^^^']))
598
599 def check_crush_setter(self, setter):
600 self.assert_valid_command(['osd', 'crush', setter,
601 '*', '2.3', 'AZaz09-_.='])
602 self.assert_valid_command(['osd', 'crush', setter,
603 'osd.0', '2.3', 'AZaz09-_.='])
604 self.assert_valid_command(['osd', 'crush', setter,
605 '0', '2.3', 'AZaz09-_.='])
606 self.assert_valid_command(['osd', 'crush', setter,
607 '0', '2.3', 'AZaz09-_.=', 'AZaz09-_.='])
608 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
609 setter,
610 'osd.0']))
611 ret = validate_command(sigdict, ['osd', 'crush',
612 setter,
613 'osd.0',
614 '-1.0'])
615 assert ret in [None, {}]
616 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
617 setter,
618 'osd.0',
619 '1.0',
620 '^^^']))
621
622 def test_crush_set(self):
623 assert_equal({}, validate_command(sigdict, ['osd', 'crush']))
624 self.check_crush_setter('set')
625
626 def test_crush_add(self):
627 assert_equal({}, validate_command(sigdict, ['osd', 'crush']))
628 self.check_crush_setter('add')
629
630 def test_crush_create_or_move(self):
631 assert_equal({}, validate_command(sigdict, ['osd', 'crush']))
632 self.check_crush_setter('create-or-move')
633
634 def test_crush_move(self):
635 self.assert_valid_command(['osd', 'crush', 'move',
636 'AZaz09-_.', 'AZaz09-_.='])
637 self.assert_valid_command(['osd', 'crush', 'move',
638 '0', 'AZaz09-_.=', 'AZaz09-_.='])
639 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
640 'move']))
641 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
642 'move', 'AZaz09-_.']))
643 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
644 'move', '^^^',
645 'AZaz09-_.=']))
646 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
647 'move', 'AZaz09-_.',
648 '^^^']))
649
650 def test_crush_link(self):
651 self.assert_valid_command(['osd', 'crush', 'link',
652 'name', 'AZaz09-_.='])
653 self.assert_valid_command(['osd', 'crush', 'link',
654 'name', 'AZaz09-_.=', 'AZaz09-_.='])
655 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
656 'link']))
657 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
658 'link',
659 'name']))
660
661 def test_crush_rm(self):
662 for alias in ('rm', 'remove', 'unlink'):
663 self.assert_valid_command(['osd', 'crush', alias, 'AZaz09-_.'])
664 self.assert_valid_command(['osd', 'crush', alias,
665 'AZaz09-_.', 'AZaz09-_.'])
666 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
667 alias]))
668 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
669 alias,
670 'AZaz09-_.',
671 'AZaz09-_.',
672 'toomany']))
673
674 def test_crush_reweight(self):
675 self.assert_valid_command(['osd', 'crush', 'reweight',
676 'AZaz09-_.', '2.3'])
677 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
678 'reweight']))
679 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
680 'reweight',
681 'AZaz09-_.']))
682 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
683 'reweight',
684 'AZaz09-_.',
685 '-1.0']))
686 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
687 'reweight',
688 '^^^',
689 '2.3']))
690
691 def test_crush_tunables(self):
692 for tunable in ('legacy', 'argonaut', 'bobtail', 'firefly',
693 'optimal', 'default'):
694 self.assert_valid_command(['osd', 'crush', 'tunables',
695 tunable])
696 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
697 'tunables']))
698 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
699 'tunables',
700 'default', 'toomany']))
701
702 def test_crush_rule_create_simple(self):
703 self.assert_valid_command(['osd', 'crush', 'rule', 'create-simple',
704 'AZaz09-_.', 'AZaz09-_.', 'AZaz09-_.'])
705 assert_equal({}, validate_command(sigdict, ['osd', 'crush', 'rule',
706 'create-simple']))
707 assert_equal({}, validate_command(sigdict, ['osd', 'crush', 'rule',
708 'create-simple',
709 'AZaz09-_.']))
710 assert_equal({}, validate_command(sigdict, ['osd', 'crush', 'rule',
711 'create-simple',
712 'AZaz09-_.',
713 'AZaz09-_.']))
714 assert_equal({}, validate_command(sigdict, ['osd', 'crush', 'rule',
715 'create-simple',
716 '^^^',
717 'AZaz09-_.',
718 'AZaz09-_.']))
719 assert_equal({}, validate_command(sigdict, ['osd', 'crush', 'rule',
720 'create-simple',
721 'AZaz09-_.',
722 '|||',
723 'AZaz09-_.']))
724 assert_equal({}, validate_command(sigdict, ['osd', 'crush', 'rule',
725 'create-simple',
726 'AZaz09-_.',
727 'AZaz09-_.',
728 '+++']))
729 assert_equal({}, validate_command(sigdict, ['osd', 'crush', 'rule',
730 'create-simple',
731 'AZaz09-_.',
732 'AZaz09-_.',
733 'AZaz09-_.',
734 'toomany']))
735
736 def test_crush_rule_create_erasure(self):
737 self.assert_valid_command(['osd', 'crush', 'rule', 'create-erasure',
738 'AZaz09-_.'])
739 self.assert_valid_command(['osd', 'crush', 'rule', 'create-erasure',
740 'AZaz09-_.', 'whatever'])
741 assert_equal({}, validate_command(sigdict, ['osd', 'crush', 'rule',
742 'create-erasure']))
743 assert_equal({}, validate_command(sigdict, ['osd', 'crush', 'rule',
744 'create-erasure',
745 '^^^']))
746 assert_equal({}, validate_command(sigdict, ['osd', 'crush', 'rule',
747 'create-erasure',
748 'name', '^^^']))
749
750 def test_crush_rule_rm(self):
751 self.assert_valid_command(['osd', 'crush', 'rule', 'rm', 'AZaz09-_.'])
752 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
753 'rule', 'rm']))
754 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
755 'rule', 'rm',
756 '^^^^']))
757 assert_equal({}, validate_command(sigdict, ['osd', 'crush',
758 'rule', 'rm',
759 'AZaz09-_.',
760 'toomany']))
761
762 def test_setmaxosd(self):
763 self.check_1_natural_arg('osd', 'setmaxosd')
764
765 def test_pause(self):
766 self.check_no_arg('osd', 'pause')
767
768 def test_unpause(self):
769 self.check_no_arg('osd', 'unpause')
770
771 def test_erasure_code_profile_set(self):
772 self.assert_valid_command(['osd', 'erasure-code-profile', 'set',
773 'name'])
774 self.assert_valid_command(['osd', 'erasure-code-profile', 'set',
775 'name', 'A=B'])
776 self.assert_valid_command(['osd', 'erasure-code-profile', 'set',
777 'name', 'A=B', 'C=D'])
778 assert_equal({}, validate_command(sigdict, ['osd',
779 'erasure-code-profile',
780 'set']))
781 assert_equal({}, validate_command(sigdict, ['osd',
782 'erasure-code-profile',
783 'set',
784 '^^^^']))
785
786 def test_erasure_code_profile_get(self):
787 self.assert_valid_command(['osd', 'erasure-code-profile', 'get',
788 'name'])
789 assert_equal({}, validate_command(sigdict, ['osd',
790 'erasure-code-profile',
791 'get']))
792 assert_equal({}, validate_command(sigdict, ['osd',
793 'erasure-code-profile',
794 'get',
795 '^^^^']))
796
797 def test_erasure_code_profile_rm(self):
798 self.assert_valid_command(['osd', 'erasure-code-profile', 'rm',
799 'name'])
800 assert_equal({}, validate_command(sigdict, ['osd',
801 'erasure-code-profile',
802 'rm']))
803 assert_equal({}, validate_command(sigdict, ['osd',
804 'erasure-code-profile',
805 'rm',
806 '^^^^']))
807
808 def test_erasure_code_profile_ls(self):
809 self.assert_valid_command(['osd', 'erasure-code-profile', 'ls'])
810 assert_equal({}, validate_command(sigdict, ['osd',
811 'erasure-code-profile',
812 'ls',
813 'toomany']))
814
815 def test_set_unset(self):
816 for action in ('set', 'unset'):
817 for flag in ('pause', 'noup', 'nodown', 'noout', 'noin',
818 'nobackfill', 'norecover', 'noscrub', 'nodeep-scrub'):
819 self.assert_valid_command(['osd', action, flag])
820 assert_equal({}, validate_command(sigdict, ['osd', action]))
821 assert_equal({}, validate_command(sigdict, ['osd', action,
822 'invalid']))
823 assert_equal({}, validate_command(sigdict, ['osd', action,
824 'pause', 'toomany']))
825
826 def test_down(self):
827 self.check_1_or_more_string_args('osd', 'down')
828
829 def test_out(self):
830 self.check_1_or_more_string_args('osd', 'out')
831
832 def test_in(self):
833 self.check_1_or_more_string_args('osd', 'in')
834
835 def test_rm(self):
836 self.check_1_or_more_string_args('osd', 'rm')
837
838 def test_reweight(self):
839 self.assert_valid_command(['osd', 'reweight', '1', '0.1'])
840 assert_equal({}, validate_command(sigdict, ['osd', 'reweight']))
841 assert_equal({}, validate_command(sigdict, ['osd', 'reweight',
842 '1']))
843 assert_equal({}, validate_command(sigdict, ['osd', 'reweight',
844 '1', '2.0']))
845 assert_equal({}, validate_command(sigdict, ['osd', 'reweight',
846 '-1', '0.1']))
847 assert_equal({}, validate_command(sigdict, ['osd', 'reweight',
848 '1', '0.1',
849 'toomany']))
850
851 def test_lost(self):
852 self.assert_valid_command(['osd', 'lost', '1',
853 '--yes-i-really-mean-it'])
854 self.assert_valid_command(['osd', 'lost', '1'])
855 assert_equal({}, validate_command(sigdict, ['osd', 'lost']))
856 assert_equal({}, validate_command(sigdict, ['osd', 'lost',
857 '1',
858 'what?']))
859 assert_equal({}, validate_command(sigdict, ['osd', 'lost',
860 '-1',
861 '--yes-i-really-mean-it']))
862 assert_equal({}, validate_command(sigdict, ['osd', 'lost',
863 '1',
864 '--yes-i-really-mean-it',
865 'toomany']))
866
867 def test_create(self):
868 uuid = '12345678123456781234567812345678'
869 self.assert_valid_command(['osd', 'create'])
870 self.assert_valid_command(['osd', 'create',
871 uuid])
872 assert_equal({}, validate_command(sigdict, ['osd', 'create',
873 'invalid']))
874 assert_equal({}, validate_command(sigdict, ['osd', 'create',
875 uuid,
876 'toomany']))
877
878 def test_blocklist(self):
879 for action in ('add', 'rm'):
880 self.assert_valid_command(['osd', 'blocklist', action,
881 '1.2.3.4/567'])
882 self.assert_valid_command(['osd', 'blocklist', action,
883 '1.2.3.4'])
884 self.assert_valid_command(['osd', 'blocklist', action,
885 '1.2.3.4/567', '600.40'])
886 self.assert_valid_command(['osd', 'blocklist', action,
887 '1.2.3.4', '600.40'])
888 assert_equal({}, validate_command(sigdict, ['osd', 'blocklist',
889 action,
890 'invalid',
891 '600.40']))
892 assert_equal({}, validate_command(sigdict, ['osd', 'blocklist',
893 action,
894 '1.2.3.4/567',
895 '-1.0']))
896 assert_equal({}, validate_command(sigdict, ['osd', 'blocklist',
897 action,
898 '1.2.3.4/567',
899 '600.40',
900 'toomany']))
901
902 def test_pool_mksnap(self):
903 self.assert_valid_command(['osd', 'pool', 'mksnap',
904 'poolname', 'snapname'])
905 assert_equal({}, validate_command(sigdict, ['osd', 'pool', 'mksnap']))
906 assert_equal({}, validate_command(sigdict, ['osd', 'pool', 'mksnap',
907 'poolname']))
908 assert_equal({}, validate_command(sigdict, ['osd', 'pool', 'mksnap',
909 'poolname', 'snapname',
910 'toomany']))
911
912 def test_pool_rmsnap(self):
913 self.assert_valid_command(['osd', 'pool', 'rmsnap',
914 'poolname', 'snapname'])
915 assert_equal({}, validate_command(sigdict, ['osd', 'pool', 'rmsnap']))
916 assert_equal({}, validate_command(sigdict, ['osd', 'pool', 'rmsnap',
917 'poolname']))
918 assert_equal({}, validate_command(sigdict, ['osd', 'pool', 'rmsnap',
919 'poolname', 'snapname',
920 'toomany']))
921
922 def test_pool_kwargs(self):
923 """
924 Use the pool creation command to exercise keyword-style arguments
925 since it has lots of parameters
926 """
927 # Simply use a keyword arg instead of a positional arg, in its
928 # normal order (pgp_num after pg_num)
929 assert_equal(
930 {
931 "prefix": "osd pool create",
932 "pool": "foo",
933 "pg_num": 8,
934 "pgp_num": 16
935 }, validate_command(sigdict, [
936 'osd', 'pool', 'create', "foo", "8", "--pgp_num", "16"]))
937
938 # Again, but using the "--foo=bar" style
939 assert_equal(
940 {
941 "prefix": "osd pool create",
942 "pool": "foo",
943 "pg_num": 8,
944 "pgp_num": 16
945 }, validate_command(sigdict, [
946 'osd', 'pool', 'create', "foo", "8", "--pgp_num=16"]))
947
948 # Specify keyword args in a different order than their definitions
949 # (pgp_num after pool_type)
950 assert_equal(
951 {
952 "prefix": "osd pool create",
953 "pool": "foo",
954 "pg_num": 8,
955 "pgp_num": 16,
956 "pool_type": "replicated"
957 }, validate_command(sigdict, [
958 'osd', 'pool', 'create', "foo", "8",
959 "--pool_type", "replicated",
960 "--pgp_num", "16"]))
961
962 # Use a keyword argument that doesn't exist, should fail validation
963 assert_equal({}, validate_command(sigdict,
964 ['osd', 'pool', 'create', "foo", "8", "--foo=bar"]))
965
966 def test_foo(self):
967 # Long form of a boolean argument (--foo=true)
968 assert_equal(
969 {
970 "prefix": "osd pool delete",
971 "pool": "foo",
972 "pool2": "foo",
973 "yes_i_really_really_mean_it": True
974 }, validate_command(sigdict, [
975 'osd', 'pool', 'delete', "foo", "foo",
976 "--yes-i-really-really-mean-it=true"]))
977
978 def test_pool_bool_args(self):
979 """
980 Use pool deletion to exercise boolean arguments since it has
981 the --yes-i-really-really-mean-it flags
982 """
983
984 # Short form of a boolean argument (--foo)
985 assert_equal(
986 {
987 "prefix": "osd pool delete",
988 "pool": "foo",
989 "pool2": "foo",
990 "yes_i_really_really_mean_it": True
991 }, validate_command(sigdict, [
992 'osd', 'pool', 'delete', "foo", "foo",
993 "--yes-i-really-really-mean-it"]))
994
995 # Long form of a boolean argument (--foo=true)
996 assert_equal(
997 {
998 "prefix": "osd pool delete",
999 "pool": "foo",
1000 "pool2": "foo",
1001 "yes_i_really_really_mean_it": True
1002 }, validate_command(sigdict, [
1003 'osd', 'pool', 'delete', "foo", "foo",
1004 "--yes-i-really-really-mean-it=true"]))
1005
1006 # Negative form of a boolean argument (--foo=false)
1007 assert_equal(
1008 {
1009 "prefix": "osd pool delete",
1010 "pool": "foo",
1011 "pool2": "foo",
1012 "yes_i_really_really_mean_it": False
1013 }, validate_command(sigdict, [
1014 'osd', 'pool', 'delete', "foo", "foo",
1015 "--yes-i-really-really-mean-it=false"]))
1016
1017 # Invalid value boolean argument (--foo=somethingelse)
1018 assert_equal({}, validate_command(sigdict, [
1019 'osd', 'pool', 'delete', "foo", "foo",
1020 "--yes-i-really-really-mean-it=rhubarb"]))
1021
1022 def test_pool_create(self):
1023 self.assert_valid_command(['osd', 'pool', 'create',
1024 'poolname', '128'])
1025 self.assert_valid_command(['osd', 'pool', 'create',
1026 'poolname', '128', '128'])
1027 self.assert_valid_command(['osd', 'pool', 'create',
1028 'poolname', '128', '128',
1029 'replicated'])
1030 self.assert_valid_command(['osd', 'pool', 'create',
1031 'poolname', '128', '128',
1032 'erasure', 'A-Za-z0-9-_.', 'ruleset^^'])
1033 self.assert_valid_command(['osd', 'pool', 'create', 'poolname'])
1034 assert_equal({}, validate_command(sigdict, ['osd', 'pool', 'create']))
1035 # invalid pg_num and pgp_num, like "-1", could spill over to
1036 # erasure_code_profile and rule as they are valid profile and rule
1037 # names, so validate_commands() cannot identify such cases.
1038 # but if they are matched by profile and rule, the "rule" argument
1039 # won't get a chance to be matched anymore.
1040 assert_equal({}, validate_command(sigdict, ['osd', 'pool', 'create',
1041 'poolname',
1042 '-1', '-1',
1043 'ruleset']))
1044 assert_equal({}, validate_command(sigdict, ['osd', 'pool', 'create',
1045 'poolname',
1046 '128', '128',
1047 'erasure', '^^^',
1048 'ruleset']))
1049 assert_equal({}, validate_command(sigdict, ['osd', 'pool', 'create',
1050 'poolname',
1051 '128', '128',
1052 'erasure', 'profile',
1053 'ruleset',
1054 'toomany']))
1055 assert_equal({}, validate_command(sigdict, ['osd', 'pool', 'create',
1056 'poolname',
1057 '128', '128',
1058 'INVALID', 'profile',
1059 'ruleset']))
1060
1061 def test_pool_delete(self):
1062 self.assert_valid_command(['osd', 'pool', 'delete',
1063 'poolname', 'poolname',
1064 '--yes-i-really-really-mean-it'])
1065 self.assert_valid_command(['osd', 'pool', 'delete',
1066 'poolname', 'poolname'])
1067 self.assert_valid_command(['osd', 'pool', 'delete',
1068 'poolname'])
1069 assert_equal({}, validate_command(sigdict, ['osd', 'pool', 'delete']))
1070 assert_equal({}, validate_command(sigdict,
1071 ['osd', 'pool', 'delete',
1072 'poolname', 'poolname',
1073 '--yes-i-really-really-mean-it',
1074 'toomany']))
1075
1076 def test_pool_rename(self):
1077 self.assert_valid_command(['osd', 'pool', 'rename',
1078 'poolname', 'othername'])
1079 assert_equal({}, validate_command(sigdict, ['osd', 'pool', 'rename']))
1080 assert_equal({}, validate_command(sigdict, ['osd', 'pool', 'rename',
1081 'poolname']))
1082 assert_equal({}, validate_command(sigdict, ['osd', 'pool', 'rename',
1083 'poolname', 'othername',
1084 'toomany']))
1085
1086 def test_pool_get(self):
1087 for var in ('size', 'min_size',
1088 'pg_num', 'pgp_num', 'crush_rule', 'fast_read',
1089 'scrub_min_interval', 'scrub_max_interval',
1090 'deep_scrub_interval', 'recovery_priority',
1091 'recovery_op_priority'):
1092 self.assert_valid_command(['osd', 'pool', 'get', 'poolname', var])
1093 assert_equal({}, validate_command(sigdict, ['osd', 'pool']))
1094 assert_equal({}, validate_command(sigdict, ['osd', 'pool',
1095 'get']))
1096 assert_equal({}, validate_command(sigdict, ['osd', 'pool',
1097 'get', 'poolname']))
1098 assert_equal({}, validate_command(sigdict, ['osd', 'pool',
1099 'get', 'poolname',
1100 'size', 'toomany']))
1101 assert_equal({}, validate_command(sigdict, ['osd', 'pool',
1102 'get', 'poolname',
1103 'invalid']))
1104
1105 def test_pool_set(self):
1106 for var in ('size', 'min_size',
1107 'pg_num', 'pgp_num', 'crush_rule',
1108 'hashpspool', 'fast_read',
1109 'scrub_min_interval', 'scrub_max_interval',
1110 'deep_scrub_interval', 'recovery_priority',
1111 'recovery_op_priority'):
1112 self.assert_valid_command(['osd', 'pool',
1113 'set', 'poolname', var, 'value'])
1114 assert_equal({}, validate_command(sigdict, ['osd', 'pool',
1115 'set']))
1116 assert_equal({}, validate_command(sigdict, ['osd', 'pool',
1117 'set', 'poolname']))
1118 assert_equal({}, validate_command(sigdict, ['osd', 'pool',
1119 'set', 'poolname',
1120 'size', 'value',
1121 'toomany']))
1122
1123 def test_pool_set_quota(self):
1124 for field in ('max_objects', 'max_bytes'):
1125 self.assert_valid_command(['osd', 'pool', 'set-quota',
1126 'poolname', field, '10K'])
1127 assert_equal({}, validate_command(sigdict, ['osd', 'pool',
1128 'set-quota']))
1129 assert_equal({}, validate_command(sigdict, ['osd', 'pool',
1130 'set-quota',
1131 'poolname']))
1132 assert_equal({}, validate_command(sigdict, ['osd', 'pool',
1133 'set-quota',
1134 'poolname',
1135 'max_objects']))
1136 assert_equal({}, validate_command(sigdict, ['osd', 'pool',
1137 'set-quota',
1138 'poolname',
1139 'invalid',
1140 '10K']))
1141 assert_equal({}, validate_command(sigdict, ['osd', 'pool',
1142 'set-quota',
1143 'poolname',
1144 'max_objects',
1145 '10K',
1146 'toomany']))
1147
1148 def test_reweight_by_utilization(self):
1149 self.assert_valid_command(['osd', 'reweight-by-utilization'])
1150 self.assert_valid_command(['osd', 'reweight-by-utilization', '100'])
1151 self.assert_valid_command(['osd', 'reweight-by-utilization', '100', '.1'])
1152 assert_equal({}, validate_command(sigdict, ['osd',
1153 'reweight-by-utilization',
1154 '100',
1155 'toomany']))
1156
1157 def test_tier_op(self):
1158 for op in ('add', 'remove', 'set-overlay'):
1159 self.assert_valid_command(['osd', 'tier', op,
1160 'poolname', 'othername'])
1161 assert_equal({}, validate_command(sigdict, ['osd', 'tier', op]))
1162 assert_equal({}, validate_command(sigdict, ['osd', 'tier', op,
1163 'poolname']))
1164 assert_equal({}, validate_command(sigdict, ['osd', 'tier', op,
1165 'poolname',
1166 'othername',
1167 'toomany']))
1168
1169 def test_tier_cache_mode(self):
1170 for mode in ('none', 'writeback', 'forward', 'readonly', 'readforward', 'readproxy'):
1171 self.assert_valid_command(['osd', 'tier', 'cache-mode',
1172 'poolname', mode])
1173 assert_equal({}, validate_command(sigdict, ['osd', 'tier',
1174 'cache-mode']))
1175 assert_equal({}, validate_command(sigdict, ['osd', 'tier',
1176 'cache-mode',
1177 'invalid']))
1178
1179 def test_tier_remove_overlay(self):
1180 self.assert_valid_command(['osd', 'tier', 'remove-overlay',
1181 'poolname'])
1182 assert_equal({}, validate_command(sigdict, ['osd', 'tier',
1183 'remove-overlay']))
1184 assert_equal({}, validate_command(sigdict, ['osd', 'tier',
1185 'remove-overlay',
1186 'poolname',
1187 'toomany']))
1188
1189 def set_ratio(self, command):
1190 self.assert_valid_command(['osd',
1191 command,
1192 '0.0'])
1193 assert_equal({}, validate_command(sigdict, ['osd', command]))
1194 assert_equal({}, validate_command(sigdict, ['osd',
1195 command,
1196 '2.0']))
1197
1198 def test_set_full_ratio(self):
1199 self.set_ratio('set-full-ratio')
1200
1201 def test_set_backfillfull_ratio(self):
1202 self.set_ratio('set-backfillfull-ratio')
1203
1204 def test_set_nearfull_ratio(self):
1205 self.set_ratio('set-nearfull-ratio')
1206
1207
1208 class TestConfigKey(TestArgparse):
1209
1210 def test_get(self):
1211 self.check_1_string_arg('config-key', 'get')
1212
1213 def test_put(self):
1214 self.assert_valid_command(['config-key', 'put',
1215 'key'])
1216 self.assert_valid_command(['config-key', 'put',
1217 'key', 'value'])
1218 assert_equal({}, validate_command(sigdict, ['config-key', 'put']))
1219 assert_equal({}, validate_command(sigdict, ['config-key', 'put',
1220 'key', 'value',
1221 'toomany']))
1222
1223 def test_del(self):
1224 self.check_1_string_arg('config-key', 'del')
1225
1226 def test_exists(self):
1227 self.check_1_string_arg('config-key', 'exists')
1228
1229 def test_dump(self):
1230 self.check_0_or_1_string_arg('config-key', 'dump')
1231
1232 def test_list(self):
1233 self.check_no_arg('config-key', 'list')
1234
1235
1236 class TestValidate(TestCase):
1237
1238 ARGS = 0
1239 KWARGS = 1
1240 KWARGS_EQ = 2
1241 MIXED = 3
1242
1243 def setUp(self):
1244 self.prefix = ['some', 'random', 'cmd']
1245 self.args_dict = [
1246 {'name': 'variable_one', 'type': 'CephString'},
1247 {'name': 'variable_two', 'type': 'CephString'},
1248 {'name': 'variable_three', 'type': 'CephString'},
1249 {'name': 'variable_four', 'type': 'CephInt'},
1250 {'name': 'variable_five', 'type': 'CephString'}]
1251 self.args = []
1252 for d in self.args_dict:
1253 if d['type'] == 'CephInt':
1254 val = "{}".format(random.randint(0, 100))
1255 elif d['type'] == 'CephString':
1256 letters = string.ascii_letters
1257 str_len = random.randint(5, 10)
1258 val = ''.join(random.choice(letters) for _ in range(str_len))
1259 else:
1260 self.skipTest()
1261
1262 self.args.append((d['name'], val))
1263
1264 self.sig = parse_funcsig(self.prefix + self.args_dict)
1265
1266 def arg_kwarg_test(self, prefix, args, sig, arg_type=0):
1267 """
1268 Runs validate in different arg/kargs ways.
1269
1270 :param prefix: List of prefix commands (that can't be kwarged)
1271 :param args: a list of kwarg, arg pairs: [(k1, v1), (k2, v2), ...]
1272 :param sig: The sig to match
1273 :param arg_type: how to build the args to send. As positional args (ARGS),
1274 as long kwargs (KWARGS [--k v]), other style long kwargs
1275 (KWARGS_EQ (--k=v]), and mixed (MIXED) where there will be
1276 a random mix of the above.
1277 :return: None, the method will assert.
1278 """
1279 final_args = list(prefix)
1280 for k, v in args:
1281 a_type = arg_type
1282 if a_type == self.MIXED:
1283 a_type = random.choice((self.ARGS,
1284 self.KWARGS,
1285 self.KWARGS_EQ))
1286 if a_type == self.ARGS:
1287 final_args.append(v)
1288 elif a_type == self.KWARGS:
1289 final_args.extend(["--{}".format(k), v])
1290 else:
1291 final_args.append("--{}={}".format(k, v))
1292
1293 try:
1294 validate(final_args, sig)
1295 except (ArgumentError, ArgumentMissing,
1296 ArgumentNumber, ArgumentTooFew, ArgumentValid) as ex:
1297 self.fail("Validation failed: {}".format(str(ex)))
1298
1299 def test_args_and_kwargs_validate(self):
1300 for arg_type in (self.ARGS, self.KWARGS, self.KWARGS_EQ, self.MIXED):
1301 self.arg_kwarg_test(self.prefix, self.args, self.sig, arg_type)
1302
1303 # Local Variables:
1304 # compile-command: "cd ../../..; cmake --build build --target get_command_descriptions -j4 &&
1305 # CEPH_BIN=build/bin \
1306 # PYTHONPATH=src/pybind nosetests --stop \
1307 # src/test/pybind/test_ceph_argparse.py:TestOSD.test_rm"
1308 # End: