| 1 |
# Copyright 2012-2013 OpenStack Foundation |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
import argparse |
| 16 |
|
| 17 |
from osc_lib.cli import parseractions |
| 18 |
from osc_lib.test import base |
| 19 |
|
| 20 |
|
| 21 |
class TestKeyValueAction(base.TestCase): |
| 22 |
def setUp(self): |
| 23 |
super().setUp() |
| 24 |
|
| 25 |
self.parser = argparse.ArgumentParser() |
| 26 |
|
| 27 |
# Set up our typical usage |
| 28 |
self.parser.add_argument( |
| 29 |
'--property', |
| 30 |
metavar='<key=value>', |
| 31 |
action=parseractions.KeyValueAction, |
| 32 |
default={'green': '20%', 'format': '#rgb'}, |
| 33 |
help='Property to store for this volume ' |
| 34 |
'(repeat option to set multiple properties)', |
| 35 |
) |
| 36 |
|
| 37 |
def test_good_values(self): |
| 38 |
results = self.parser.parse_args( |
| 39 |
[ |
| 40 |
'--property', |
| 41 |
'red=', |
| 42 |
'--property', |
| 43 |
'green=100%', |
| 44 |
'--property', |
| 45 |
'blue=50%', |
| 46 |
] |
| 47 |
) |
| 48 |
|
| 49 |
actual = getattr(results, 'property', {}) |
| 50 |
# All should pass through unmolested |
| 51 |
expect = {'red': '', 'green': '100%', 'blue': '50%', 'format': '#rgb'} |
| 52 |
self.assertEqual(expect, actual) |
| 53 |
|
| 54 |
def test_error_values(self): |
| 55 |
data_list = [ |
| 56 |
[ |
| 57 |
'--property', |
| 58 |
'red', |
| 59 |
], |
| 60 |
[ |
| 61 |
'--property', |
| 62 |
'=', |
| 63 |
], |
| 64 |
[ |
| 65 |
'--property', |
| 66 |
'=red', |
| 67 |
], |
| 68 |
] |
| 69 |
for data in data_list: |
| 70 |
self.assertRaises(SystemExit, self.parser.parse_args, data) |
| 71 |
|
| 72 |
|
| 73 |
class TestKeyValueAppendAction(base.TestCase): |
| 74 |
def setUp(self): |
| 75 |
super().setUp() |
| 76 |
|
| 77 |
self.parser = argparse.ArgumentParser() |
| 78 |
|
| 79 |
# Set up our typical usage |
| 80 |
self.parser.add_argument( |
| 81 |
'--hint', |
| 82 |
metavar='<key=value>', |
| 83 |
action=parseractions.KeyValueAppendAction, |
| 84 |
help='Arbitrary key/value pairs to be sent to the scheduler for ' |
| 85 |
'custom use', |
| 86 |
) |
| 87 |
|
| 88 |
def test_good_values(self): |
| 89 |
print(self.parser._get_optional_actions()) |
| 90 |
results = self.parser.parse_args( |
| 91 |
[ |
| 92 |
'--hint', |
| 93 |
'same_host=a0cf03a5-d921-4877-bb5c-86d26cf818e1', |
| 94 |
'--hint', |
| 95 |
'same_host=8c19174f-4220-44f0-824a-cd1eeef10287', |
| 96 |
'--hint', |
| 97 |
'query=[>=,$free_ram_mb,1024]', |
| 98 |
] |
| 99 |
) |
| 100 |
|
| 101 |
actual = getattr(results, 'hint', {}) |
| 102 |
expect = { |
| 103 |
'same_host': [ |
| 104 |
'a0cf03a5-d921-4877-bb5c-86d26cf818e1', |
| 105 |
'8c19174f-4220-44f0-824a-cd1eeef10287', |
| 106 |
], |
| 107 |
'query': [ |
| 108 |
'[>=,$free_ram_mb,1024]', |
| 109 |
], |
| 110 |
} |
| 111 |
self.assertEqual(expect, actual) |
| 112 |
|
| 113 |
def test_error_values(self): |
| 114 |
data_list = [ |
| 115 |
[ |
| 116 |
'--hint', |
| 117 |
'red', |
| 118 |
], |
| 119 |
[ |
| 120 |
'--hint', |
| 121 |
'=', |
| 122 |
], |
| 123 |
[ |
| 124 |
'--hint', |
| 125 |
'=red', |
| 126 |
], |
| 127 |
] |
| 128 |
for data in data_list: |
| 129 |
self.assertRaises(SystemExit, self.parser.parse_args, data) |
| 130 |
|
| 131 |
|
| 132 |
class TestMultiKeyValueAction(base.TestCase): |
| 133 |
def setUp(self): |
| 134 |
super().setUp() |
| 135 |
|
| 136 |
self.parser = argparse.ArgumentParser() |
| 137 |
|
| 138 |
# Set up our typical usage |
| 139 |
self.parser.add_argument( |
| 140 |
'--test', |
| 141 |
metavar='req1=xxx,req2=yyy', |
| 142 |
action=parseractions.MultiKeyValueAction, |
| 143 |
dest='test', |
| 144 |
default=None, |
| 145 |
required_keys=['req1', 'req2'], |
| 146 |
optional_keys=['opt1', 'opt2'], |
| 147 |
help='Test', |
| 148 |
) |
| 149 |
|
| 150 |
def test_good_values(self): |
| 151 |
results = self.parser.parse_args( |
| 152 |
[ |
| 153 |
'--test', |
| 154 |
'req1=aaa,req2=bbb', |
| 155 |
'--test', |
| 156 |
'req1=,req2=', |
| 157 |
] |
| 158 |
) |
| 159 |
|
| 160 |
actual = getattr(results, 'test', []) |
| 161 |
expect = [ |
| 162 |
{'req1': 'aaa', 'req2': 'bbb'}, |
| 163 |
{'req1': '', 'req2': ''}, |
| 164 |
] |
| 165 |
self.assertCountEqual(expect, actual) |
| 166 |
|
| 167 |
def test_empty_required_optional(self): |
| 168 |
self.parser.add_argument( |
| 169 |
'--test-empty', |
| 170 |
metavar='req1=xxx,req2=yyy', |
| 171 |
action=parseractions.MultiKeyValueAction, |
| 172 |
dest='test_empty', |
| 173 |
default=None, |
| 174 |
required_keys=[], |
| 175 |
optional_keys=[], |
| 176 |
help='Test', |
| 177 |
) |
| 178 |
|
| 179 |
results = self.parser.parse_args( |
| 180 |
[ |
| 181 |
'--test-empty', |
| 182 |
'req1=aaa,req2=bbb', |
| 183 |
'--test-empty', |
| 184 |
'req1=,req2=', |
| 185 |
] |
| 186 |
) |
| 187 |
|
| 188 |
actual = getattr(results, 'test_empty', []) |
| 189 |
expect = [ |
| 190 |
{'req1': 'aaa', 'req2': 'bbb'}, |
| 191 |
{'req1': '', 'req2': ''}, |
| 192 |
] |
| 193 |
self.assertCountEqual(expect, actual) |
| 194 |
|
| 195 |
def test_error_values_with_comma(self): |
| 196 |
data_list = [ |
| 197 |
[ |
| 198 |
'--test', |
| 199 |
'mmm,nnn=zzz', |
| 200 |
], |
| 201 |
[ |
| 202 |
'--test', |
| 203 |
'nnn=zzz,=', |
| 204 |
], |
| 205 |
[ |
| 206 |
'--test', |
| 207 |
'nnn=zzz,=zzz', |
| 208 |
], |
| 209 |
] |
| 210 |
for data in data_list: |
| 211 |
self.assertRaises(SystemExit, self.parser.parse_args, data) |
| 212 |
|
| 213 |
def test_error_values_without_comma(self): |
| 214 |
self.assertRaises( |
| 215 |
SystemExit, |
| 216 |
self.parser.parse_args, |
| 217 |
[ |
| 218 |
'--test', |
| 219 |
'mmmnnn', |
| 220 |
], |
| 221 |
) |
| 222 |
|
| 223 |
def test_missing_key(self): |
| 224 |
self.assertRaises( |
| 225 |
SystemExit, |
| 226 |
self.parser.parse_args, |
| 227 |
[ |
| 228 |
'--test', |
| 229 |
'req2=ddd', |
| 230 |
], |
| 231 |
) |
| 232 |
|
| 233 |
def test_invalid_key(self): |
| 234 |
self.assertRaises( |
| 235 |
SystemExit, |
| 236 |
self.parser.parse_args, |
| 237 |
[ |
| 238 |
'--test', |
| 239 |
'req1=aaa,req2=bbb,aaa=req1', |
| 240 |
], |
| 241 |
) |
| 242 |
|
| 243 |
def test_required_keys_not_list(self): |
| 244 |
self.assertRaises( |
| 245 |
TypeError, |
| 246 |
self.parser.add_argument, |
| 247 |
'--test-required-dict', |
| 248 |
metavar='req1=xxx,req2=yyy', |
| 249 |
action=parseractions.MultiKeyValueAction, |
| 250 |
dest='test_required_dict', |
| 251 |
default=None, |
| 252 |
required_keys={'aaa': 'bbb'}, |
| 253 |
optional_keys=['opt1', 'opt2'], |
| 254 |
help='Test', |
| 255 |
) |
| 256 |
|
| 257 |
def test_optional_keys_not_list(self): |
| 258 |
self.assertRaises( |
| 259 |
TypeError, |
| 260 |
self.parser.add_argument, |
| 261 |
'--test-optional-dict', |
| 262 |
metavar='req1=xxx,req2=yyy', |
| 263 |
action=parseractions.MultiKeyValueAction, |
| 264 |
dest='test_optional_dict', |
| 265 |
default=None, |
| 266 |
required_keys=['req1', 'req2'], |
| 267 |
optional_keys={'aaa': 'bbb'}, |
| 268 |
help='Test', |
| 269 |
) |
| 270 |
|
| 271 |
|
| 272 |
class TestMultiKeyValueCommaAction(base.TestCase): |
| 273 |
def setUp(self): |
| 274 |
super().setUp() |
| 275 |
self.parser = argparse.ArgumentParser() |
| 276 |
|
| 277 |
# Typical usage |
| 278 |
self.parser.add_argument( |
| 279 |
'--test', |
| 280 |
metavar='req1=xxx,yyy', |
| 281 |
action=parseractions.MultiKeyValueCommaAction, |
| 282 |
dest='test', |
| 283 |
default=None, |
| 284 |
required_keys=['req1'], |
| 285 |
optional_keys=['opt2'], |
| 286 |
help='Test', |
| 287 |
) |
| 288 |
|
| 289 |
def test_mkvca_required(self): |
| 290 |
results = self.parser.parse_args( |
| 291 |
[ |
| 292 |
'--test', |
| 293 |
'req1=aaa,bbb', |
| 294 |
] |
| 295 |
) |
| 296 |
actual = getattr(results, 'test', []) |
| 297 |
expect = [ |
| 298 |
{'req1': 'aaa,bbb'}, |
| 299 |
] |
| 300 |
self.assertCountEqual(expect, actual) |
| 301 |
|
| 302 |
results = self.parser.parse_args( |
| 303 |
[ |
| 304 |
'--test', |
| 305 |
'req1=', |
| 306 |
] |
| 307 |
) |
| 308 |
actual = getattr(results, 'test', []) |
| 309 |
expect = [ |
| 310 |
{'req1': ''}, |
| 311 |
] |
| 312 |
self.assertCountEqual(expect, actual) |
| 313 |
|
| 314 |
results = self.parser.parse_args( |
| 315 |
[ |
| 316 |
'--test', |
| 317 |
'req1=aaa,bbb', |
| 318 |
'--test', |
| 319 |
'req1=', |
| 320 |
] |
| 321 |
) |
| 322 |
actual = getattr(results, 'test', []) |
| 323 |
expect = [ |
| 324 |
{'req1': 'aaa,bbb'}, |
| 325 |
{'req1': ''}, |
| 326 |
] |
| 327 |
self.assertCountEqual(expect, actual) |
| 328 |
|
| 329 |
def test_mkvca_optional(self): |
| 330 |
results = self.parser.parse_args( |
| 331 |
[ |
| 332 |
'--test', |
| 333 |
'req1=aaa,bbb', |
| 334 |
] |
| 335 |
) |
| 336 |
actual = getattr(results, 'test', []) |
| 337 |
expect = [ |
| 338 |
{'req1': 'aaa,bbb'}, |
| 339 |
] |
| 340 |
self.assertCountEqual(expect, actual) |
| 341 |
|
| 342 |
results = self.parser.parse_args( |
| 343 |
[ |
| 344 |
'--test', |
| 345 |
'req1=aaa,bbb', |
| 346 |
'--test', |
| 347 |
'req1=,opt2=ccc', |
| 348 |
] |
| 349 |
) |
| 350 |
actual = getattr(results, 'test', []) |
| 351 |
expect = [ |
| 352 |
{'req1': 'aaa,bbb'}, |
| 353 |
{'req1': '', 'opt2': 'ccc'}, |
| 354 |
] |
| 355 |
self.assertCountEqual(expect, actual) |
| 356 |
|
| 357 |
self.assertRaises( |
| 358 |
SystemExit, |
| 359 |
self.parser.parse_args, |
| 360 |
[ |
| 361 |
'--test', |
| 362 |
'req1=aaa,bbb', |
| 363 |
'--test', |
| 364 |
'opt2=ccc', |
| 365 |
], |
| 366 |
) |
| 367 |
|
| 368 |
def test_mkvca_multiples(self): |
| 369 |
results = self.parser.parse_args( |
| 370 |
[ |
| 371 |
'--test', |
| 372 |
'req1=aaa,bbb,opt2=ccc', |
| 373 |
] |
| 374 |
) |
| 375 |
actual = getattr(results, 'test', []) |
| 376 |
expect = [ |
| 377 |
{ |
| 378 |
'req1': 'aaa,bbb', |
| 379 |
'opt2': 'ccc', |
| 380 |
} |
| 381 |
] |
| 382 |
self.assertCountEqual(expect, actual) |
| 383 |
|
| 384 |
def test_mkvca_no_required_optional(self): |
| 385 |
self.parser.add_argument( |
| 386 |
'--test-empty', |
| 387 |
metavar='req1=xxx,yyy', |
| 388 |
action=parseractions.MultiKeyValueCommaAction, |
| 389 |
dest='test_empty', |
| 390 |
default=None, |
| 391 |
required_keys=[], |
| 392 |
optional_keys=[], |
| 393 |
help='Test', |
| 394 |
) |
| 395 |
|
| 396 |
results = self.parser.parse_args( |
| 397 |
[ |
| 398 |
'--test-empty', |
| 399 |
'req1=aaa,bbb', |
| 400 |
] |
| 401 |
) |
| 402 |
actual = getattr(results, 'test_empty', []) |
| 403 |
expect = [ |
| 404 |
{'req1': 'aaa,bbb'}, |
| 405 |
] |
| 406 |
self.assertCountEqual(expect, actual) |
| 407 |
|
| 408 |
results = self.parser.parse_args( |
| 409 |
[ |
| 410 |
'--test-empty', |
| 411 |
'xyz=aaa,bbb', |
| 412 |
] |
| 413 |
) |
| 414 |
|
| 415 |
actual = getattr(results, 'test_empty', []) |
| 416 |
expect = [ |
| 417 |
{'xyz': 'aaa,bbb'}, |
| 418 |
] |
| 419 |
self.assertCountEqual(expect, actual) |
| 420 |
|
| 421 |
def test_mkvca_invalid_key(self): |
| 422 |
self.assertRaises( |
| 423 |
SystemExit, |
| 424 |
self.parser.parse_args, |
| 425 |
['--test', 'req1=aaa,bbb='], |
| 426 |
) |
| 427 |
|
| 428 |
self.assertRaises( |
| 429 |
SystemExit, |
| 430 |
self.parser.parse_args, |
| 431 |
['--test', 'nnn=aaa'], |
| 432 |
) |
| 433 |
|
| 434 |
def test_mkvca_value_no_key(self): |
| 435 |
self.assertRaises( |
| 436 |
SystemExit, |
| 437 |
self.parser.parse_args, |
| 438 |
['--test', 'req1=aaa,=bbb'], |
| 439 |
) |
| 440 |
self.assertRaises( |
| 441 |
SystemExit, |
| 442 |
self.parser.parse_args, |
| 443 |
['--test', '=nnn'], |
| 444 |
) |
| 445 |
self.assertRaises( |
| 446 |
SystemExit, |
| 447 |
self.parser.parse_args, |
| 448 |
['--test', 'nnn'], |
| 449 |
) |
| 450 |
|
| 451 |
def test_mkvca_required_keys_not_list(self): |
| 452 |
self.assertRaises( |
| 453 |
TypeError, |
| 454 |
self.parser.add_argument, |
| 455 |
'--test-required-dict', |
| 456 |
metavar='req1=xxx', |
| 457 |
action=parseractions.MultiKeyValueCommaAction, |
| 458 |
dest='test_required_dict', |
| 459 |
default=None, |
| 460 |
required_keys={'aaa': 'bbb'}, |
| 461 |
optional_keys=['opt1', 'opt2'], |
| 462 |
help='Test', |
| 463 |
) |
| 464 |
|
| 465 |
def test_mkvca_optional_keys_not_list(self): |
| 466 |
self.assertRaises( |
| 467 |
TypeError, |
| 468 |
self.parser.add_argument, |
| 469 |
'--test-optional-dict', |
| 470 |
metavar='req1=xxx', |
| 471 |
action=parseractions.MultiKeyValueCommaAction, |
| 472 |
dest='test_optional_dict', |
| 473 |
default=None, |
| 474 |
required_keys=['req1', 'req2'], |
| 475 |
optional_keys={'aaa': 'bbb'}, |
| 476 |
help='Test', |
| 477 |
) |
| 478 |
|
| 479 |
|
| 480 |
class TestNonNegativeAction(base.TestCase): |
| 481 |
def setUp(self): |
| 482 |
super().setUp() |
| 483 |
|
| 484 |
self.parser = argparse.ArgumentParser() |
| 485 |
|
| 486 |
# Set up our typical usage |
| 487 |
self.parser.add_argument( |
| 488 |
'--foo', |
| 489 |
metavar='<foo>', |
| 490 |
type=int, |
| 491 |
action=parseractions.NonNegativeAction, |
| 492 |
) |
| 493 |
|
| 494 |
def test_negative_values(self): |
| 495 |
self.assertRaises( |
| 496 |
SystemExit, |
| 497 |
self.parser.parse_args, |
| 498 |
"--foo -1".split(), |
| 499 |
) |
| 500 |
|
| 501 |
def test_zero_values(self): |
| 502 |
results = self.parser.parse_args('--foo 0'.split()) |
| 503 |
|
| 504 |
actual = getattr(results, 'foo', None) |
| 505 |
self.assertEqual(actual, 0) |
| 506 |
|
| 507 |
def test_positive_values(self): |
| 508 |
results = self.parser.parse_args('--foo 1'.split()) |
| 509 |
|
| 510 |
actual = getattr(results, 'foo', None) |
| 511 |
self.assertEqual(actual, 1) |