1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
|
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import string
from unittest import mock
import netaddr
from neutron_lib._i18n import _
from neutron_lib.api import converters
from neutron_lib.api.definitions import extra_dhcp_opt
from neutron_lib.api import validators
from neutron_lib import constants
from neutron_lib import exceptions as n_exc
from neutron_lib import fixture
from neutron_lib.plugins import directory
from neutron_lib.tests import _base as base
def dummy_validator(data, valid_values=None):
pass
class TestAttributeValidation(base.BaseTestCase):
def _construct_dict_and_constraints(self):
"""Constructs a test dictionary and a definition of constraints.
:return: A (dictionary, constraint) tuple
"""
constraints = {'key1': {'type:values': ['val1', 'val2'],
'required': True},
'key2': {'type:string': None,
'required': False},
'key3': {'type:dict': {'k4': {'type:string': None,
'required': True}},
'required': True}}
dictionary = {'key1': 'val1',
'key2': 'a string value',
'key3': {'k4': 'a string value'}}
return dictionary, constraints
def test_type_prefixing(self):
validators.add_validator('type:prefixed_type', dummy_validator)
validators.add_validator('unprefixed_type', dummy_validator)
self.assertEqual(dummy_validator,
validators.get_validator('type:prefixed_type'))
self.assertEqual(dummy_validator,
validators.get_validator('prefixed_type'))
self.assertEqual(dummy_validator,
validators.get_validator('type:unprefixed_type'))
self.assertEqual(dummy_validator,
validators.get_validator('unprefixed_type'))
def test_adding_validator(self):
validators.add_validator('new_type', dummy_validator)
self.assertIn('type:new_type', validators.validators)
self.assertEqual(dummy_validator,
validators.validators['type:new_type'])
def test_get_validator_default(self):
self.assertEqual(dummy_validator,
validators.get_validator('nope',
default=dummy_validator))
def test_fail_adding_duplicate_validator(self):
self.assertRaises(KeyError,
validators.add_validator,
'dict', lambda x: x)
def test_success_adding_duplicate_validator(self):
validators.add_validator('dummy', dummy_validator)
validators.add_validator('dummy', dummy_validator)
self.assertEqual(dummy_validator, validators.get_validator('dummy'))
def test_is_attr_set(self):
data = constants.ATTR_NOT_SPECIFIED
self.assertIs(validators.is_attr_set(data), False)
data = None
self.assertIs(validators.is_attr_set(data), False)
data = "I'm set"
self.assertIs(validators.is_attr_set(data), True)
def test_validate_values(self):
# Check that validation is not performed if valid_values is not set
msg = validators.validate_values(4)
self.assertIsNone(msg)
# Check that value is within valid_values
msg = validators.validate_values(4, [4, 6])
self.assertIsNone(msg)
# Check that value is within valid_values
msg = validators.validate_values(4, (4, 6))
self.assertIsNone(msg)
# Check that value is within valid_values with strings
msg = validators.validate_values("1", ["2", "1", "4", "5"])
self.assertIsNone(msg)
# Check that value is not compatible for comparison
response = "'valid_values' does not support membership operations"
self.assertRaisesRegex(TypeError, response,
validators.validate_values, data=None,
valid_values=True)
def test_validate_values_display(self):
# Check that value is NOT within valid_values and report values
msg = validators.validate_values(7, [4, 6],
valid_values_display="[4, 6]")
self.assertEqual("7 is not in [4, 6]", msg)
# Check that value is NOT within valid_values and report values
msg = validators.validate_values(7, (4, 6),
valid_values_display="(4, 6)")
self.assertEqual("7 is not in (4, 6)", msg)
# Check values with a range function showing a custom string
msg = validators.validate_values(8, range(8),
valid_values_display="[0..7]")
self.assertEqual("8 is not in [0..7]", msg)
# Check that value is not within valid_values and custom string
msg = validators.validate_values(1, [2, 3, 4, 5],
valid_values_display="[2, 3, 4, 5]")
self.assertEqual("1 is not in [2, 3, 4, 5]", msg)
# Check that value is not within valid_values and custom string
msg = validators.validate_values("1", ["2", "3", "4", "5"],
valid_values_display="'valid_values"
"_to_show'")
self.assertEqual("1 is not in 'valid_values_to_show'", msg)
# Check that value is not comparable to valid_values and got Exception
data = 1
valid_values = '[2, 3, 4, 5]'
response = "'data' of type '%s' and 'valid_values' of type" \
" '%s' are not compatible for comparison" % (
type(data), type(valid_values))
self.assertRaisesRegex(TypeError, response,
validators.validate_values, data,
valid_values,
valid_values_display="[2, 3, 4, 5]")
def test_validate_not_empty_string(self):
msg = validators.validate_not_empty_string(' ', None)
self.assertEqual("' ' Blank strings are not permitted", msg)
msg = validators.validate_not_empty_string(123, None)
self.assertEqual("'123' is not a valid string", msg)
def test_validate_not_empty_string_or_none(self):
msg = validators.validate_not_empty_string_or_none(' ', None)
self.assertEqual("' ' Blank strings are not permitted", msg)
msg = validators.validate_not_empty_string_or_none(None, None)
self.assertIsNone(msg)
def test_validate_string_or_none(self):
msg = validators.validate_string_or_none('test', None)
self.assertIsNone(msg)
msg = validators.validate_string_or_none(None, None)
self.assertIsNone(msg)
def test_validate_string(self):
msg = validators.validate_string(None, None)
self.assertEqual("'None' is not a valid string", msg)
# 0 == len(data) == max_len
msg = validators.validate_string("", 0)
self.assertIsNone(msg)
# 0 == len(data) < max_len
msg = validators.validate_string("", 9)
self.assertIsNone(msg)
# 0 < len(data) < max_len
msg = validators.validate_string("123456789", 10)
self.assertIsNone(msg)
# 0 < len(data) == max_len
msg = validators.validate_string("123456789", 9)
self.assertIsNone(msg)
# 0 < max_len < len(data)
msg = validators.validate_string("1234567890", 9)
self.assertEqual("'1234567890' exceeds maximum length of 9", msg)
msg = validators.validate_string("123456789", None)
self.assertIsNone(msg)
def test_validate_list_of_unique_strings(self):
data = "TEST"
msg = validators.validate_list_of_unique_strings(data, None)
self.assertEqual("'TEST' is not a list", msg)
data = ["TEST01", "TEST02", "TEST01"]
msg = validators.validate_list_of_unique_strings(data, None)
self.assertEqual(
"Duplicate items in the list: 'TEST01'", msg)
data = ["12345678", "123456789"]
msg = validators.validate_list_of_unique_strings(data, 8)
self.assertEqual("'123456789' exceeds maximum length of 8", msg)
data = ["TEST01", "TEST02", "TEST03"]
msg = validators.validate_list_of_unique_strings(data, None)
self.assertIsNone(msg)
def test_validate_oneline_not_empty_string(self):
data = "Test"
msg = validators.validate_oneline_not_empty_string(data, None)
self.assertIsNone(msg)
data = "Test but this is too long"
max_len = 4
msg = validators.validate_oneline_not_empty_string(data, max_len)
self.assertEqual(
"'{}' exceeds maximum length of {}".format(data, max_len),
msg)
data = "First line\nsecond line"
msg = validators.validate_oneline_not_empty_string(data, None)
self.assertEqual(
"Multi-line string is not allowed: '%s'" % data,
msg)
data = ""
msg = validators.validate_oneline_not_empty_string(data, None)
self.assertEqual(
"'%s' Blank strings are not permitted" % data,
msg)
def test_validate_oneline_not_empty_string_or_none(self):
data = "Test"
msg = validators.validate_oneline_not_empty_string_or_none(data, None)
self.assertIsNone(msg)
data = None
msg = validators.validate_oneline_not_empty_string_or_none(data, None)
self.assertIsNone(msg)
data = "Test but this is too long"
max_len = 4
msg = validators.validate_oneline_not_empty_string_or_none(
data, max_len)
self.assertEqual(
"'{}' exceeds maximum length of {}".format(data, max_len),
msg)
data = "First line\nsecond line"
msg = validators.validate_oneline_not_empty_string_or_none(data, None)
self.assertEqual(
"Multi-line string is not allowed: '%s'" % data,
msg)
data = ""
msg = validators.validate_oneline_not_empty_string(data, None)
self.assertEqual(
"'%s' Blank strings are not permitted" % data,
msg)
def test_validate_boolean(self):
msg = validators.validate_boolean(True)
self.assertIsNone(msg)
msg = validators.validate_boolean(0)
self.assertIsNone(msg)
msg = validators.validate_boolean("false")
self.assertIsNone(msg)
msg = validators.validate_boolean("fasle")
self.assertEqual("'fasle' is not a valid boolean value", msg)
def test_validate_integer(self):
msg = validators.validate_integer(1)
self.assertIsNone(msg)
msg = validators.validate_integer(0.1)
self.assertEqual("'0.1' is not an integer", msg)
msg = validators.validate_integer("1")
self.assertIsNone(msg)
msg = validators.validate_integer("0.1")
self.assertEqual("'0.1' is not an integer", msg)
msg = validators.validate_integer(True)
self.assertEqual("'True' is not an integer:boolean", msg)
msg = validators.validate_integer(False)
self.assertEqual("'False' is not an integer:boolean", msg)
msg = validators.validate_integer(float('Inf'))
self.assertEqual("'inf' is not an integer", msg)
msg = validators.validate_integer(None)
self.assertEqual("'None' is not an integer", msg)
def test_validate_integer_values(self):
msg = validators.validate_integer(2, [2, 3, 4, 5])
self.assertIsNone(msg)
msg = validators.validate_integer(1, [2, 3, 4, 5])
self.assertEqual("1 is not in valid_values", msg)
def test_validate_no_whitespace(self):
data = 'no_white_space'
result = validators.validate_no_whitespace(data)
self.assertEqual(data, result)
self.assertRaises(n_exc.InvalidInput,
validators.validate_no_whitespace,
'i have whitespace')
self.assertRaises(n_exc.InvalidInput,
validators.validate_no_whitespace,
'i\thave\twhitespace')
for ws in string.whitespace:
self.assertRaises(n_exc.InvalidInput,
validators.validate_no_whitespace,
'%swhitespace-at-head' % ws)
self.assertRaises(n_exc.InvalidInput,
validators.validate_no_whitespace,
'whitespace-at-tail%s' % ws)
def test_validate_range(self):
msg = validators.validate_range(1, [1, 9])
self.assertIsNone(msg)
msg = validators.validate_range(5, [1, 9])
self.assertIsNone(msg)
msg = validators.validate_range(9, [1, 9])
self.assertIsNone(msg)
msg = validators.validate_range(1, (1, 9))
self.assertIsNone(msg)
msg = validators.validate_range(5, (1, 9))
self.assertIsNone(msg)
msg = validators.validate_range(9, (1, 9))
self.assertIsNone(msg)
msg = validators.validate_range(0, [1, 9])
self.assertEqual("'0' is too small - must be at least '1'", msg)
msg = validators.validate_range(10, (1, 9))
self.assertEqual("'10' is too large - must be no larger than '9'", msg)
msg = validators.validate_range("bogus", (1, 9))
self.assertEqual("'bogus' is not an integer", msg)
msg = validators.validate_range(10, (validators.UNLIMITED,
validators.UNLIMITED))
self.assertIsNone(msg)
msg = validators.validate_range(10, (1, validators.UNLIMITED))
self.assertIsNone(msg)
msg = validators.validate_range(1, (validators.UNLIMITED, 9))
self.assertIsNone(msg)
msg = validators.validate_range(-1, (0, validators.UNLIMITED))
self.assertEqual("'-1' is too small - must be at least '0'", msg)
msg = validators.validate_range(10, (validators.UNLIMITED, 9))
self.assertEqual("'10' is too large - must be no larger than '9'", msg)
@mock.patch("neutron_lib.api.validators.validate_range")
def test_validate_range_or_none(self, mock_validate_range):
msg = validators.validate_range_or_none(None, [1, 9])
self.assertFalse(mock_validate_range.called)
self.assertIsNone(msg)
validators.validate_range_or_none(1, [1, 9])
mock_validate_range.assert_called_once_with(1, [1, 9])
def _test_validate_mac_address(self, validator, allow_none=False):
mac_addr = "ff:16:3e:4f:00:00"
msg = validator(mac_addr)
self.assertIsNone(msg)
mac_addr = "ffa:16:3e:4f:00:00"
msg = validator(mac_addr)
err_msg = "'%s' is not a valid MAC address"
self.assertEqual(err_msg % mac_addr, msg)
for invalid_mac_addr in constants.INVALID_MAC_ADDRESSES:
msg = validator(invalid_mac_addr)
self.assertEqual(err_msg % invalid_mac_addr, msg)
mac_addr = "123"
msg = validator(mac_addr)
self.assertEqual(err_msg % mac_addr, msg)
mac_addr = None
msg = validator(mac_addr)
if allow_none:
self.assertIsNone(msg)
else:
self.assertEqual(err_msg % mac_addr, msg)
mac_addr = "ff:16:3e:4f:00:00\r"
msg = validator(mac_addr)
self.assertEqual(err_msg % mac_addr, msg)
def test_validate_mac_address(self):
self._test_validate_mac_address(validators.validate_mac_address)
def test_validate_mac_address_or_none(self):
self._test_validate_mac_address(
validators.validate_mac_address_or_none, allow_none=True)
def test_validate_ip_address(self):
ip_addr = '1.1.1.1'
msg = validators.validate_ip_address(ip_addr)
self.assertIsNone(msg)
ip_addr = '1111.1.1.1'
msg = validators.validate_ip_address(ip_addr)
self.assertEqual("'%s' is not a valid IP address" % ip_addr, msg)
# Depending on platform to run UTs, this case might or might not be
# an equivalent to test_validate_ip_address_bsd.
ip_addr = '1' * 59
msg = validators.validate_ip_address(ip_addr)
self.assertEqual("'%s' is not a valid IP address" % ip_addr, msg)
ip_addr = '1.1.1.1 has whitespace'
msg = validators.validate_ip_address(ip_addr)
self.assertEqual("'%s' is not a valid IP address" % ip_addr, msg)
ip_addr = '111.1.1.1\twhitespace'
msg = validators.validate_ip_address(ip_addr)
self.assertEqual("'%s' is not a valid IP address" % ip_addr, msg)
ip_addr = '111.1.1.1\nwhitespace'
msg = validators.validate_ip_address(ip_addr)
self.assertEqual("'%s' is not a valid IP address" % ip_addr, msg)
for ws in string.whitespace:
ip_addr = '%s111.1.1.1' % ws
msg = validators.validate_ip_address(ip_addr)
self.assertEqual("'%s' is not a valid IP address" % ip_addr, msg)
for ws in string.whitespace:
ip_addr = '111.1.1.1%s' % ws
msg = validators.validate_ip_address(ip_addr)
self.assertEqual("'%s' is not a valid IP address" % ip_addr, msg)
def test_validate_ip_address_with_leading_zero(self):
ip_addr = '1.1.1.01'
expected_msg = ("'%(data)s' is not an accepted IP address, "
"'%(ip)s' is recommended")
msg = validators.validate_ip_address(ip_addr)
self.assertEqual(expected_msg % {"data": ip_addr, "ip": '1.1.1.1'},
msg)
ip_addr = '1.1.1.011'
msg = validators.validate_ip_address(ip_addr)
self.assertEqual(expected_msg % {"data": ip_addr, "ip": '1.1.1.11'},
msg)
ip_addr = '1.1.1.09'
msg = validators.validate_ip_address(ip_addr)
self.assertEqual(expected_msg % {"data": ip_addr, "ip": '1.1.1.9'},
msg)
ip_addr = "fe80:0:0:0:0:0:0:0001"
msg = validators.validate_ip_address(ip_addr)
self.assertIsNone(msg)
def test_validate_ip_address_bsd(self):
# NOTE(yamamoto): On NetBSD and OS X, netaddr.IPAddress() accepts
# '1' * 59 as a valid address. The behaviour is inherited from
# libc behaviour there. This test ensures that our validator reject
# such addresses on such platforms by mocking netaddr to emulate
# the behaviour.
ip_addr = '1' * 59
with mock.patch('netaddr.IPAddress') as ip_address_cls:
msg = validators.validate_ip_address(ip_addr)
ip_address_cls.assert_called_once_with(ip_addr,
flags=netaddr.core.ZEROFILL)
self.assertEqual("'%s' is not a valid IP address" % ip_addr, msg)
def test_validate_ip_pools(self):
pools = [[{'end': '10.0.0.254'}],
[{'start': '10.0.0.254'}],
[{'start': '1000.0.0.254',
'end': '1.1.1.1'}],
[{'start': '10.0.0.2', 'end': '10.0.0.254',
'forza': 'juve'}],
[{'start': '10.0.0.2', 'end': '10.0.0.254'},
{'end': '10.0.0.254'}],
[None],
None]
for pool in pools:
msg = validators.validate_ip_pools(pool)
self.assertIsNotNone(msg)
pools = [[{'end': '10.0.0.254', 'start': '10.0.0.2'},
{'start': '11.0.0.2', 'end': '11.1.1.1'}],
[{'start': '11.0.0.2', 'end': '11.0.0.100'}]]
for pool in pools:
msg = validators.validate_ip_pools(pool)
self.assertIsNone(msg)
invalid_ip = '10.0.0.2\r'
pools = [[{'end': '10.0.0.254', 'start': invalid_ip}]]
for pool in pools:
msg = validators.validate_ip_pools(pool)
self.assertEqual("'%s' is not a valid IP address" % invalid_ip,
msg)
def test_validate_fixed_ips(self):
fixed_ips = [
{'data': [{'subnet_id': '00000000-ffff-ffff-ffff-000000000000',
'ip_address': '1111.1.1.1'}],
'error_msg': "'1111.1.1.1' is not a valid IP address"},
{'data': [{'subnet_id': 'invalid',
'ip_address': '1.1.1.1'}],
'error_msg': "'invalid' is not a valid UUID"},
{'data': None,
'error_msg': "Invalid data format for fixed IP: 'None'"},
{'data': "1.1.1.1",
'error_msg': "Invalid data format for fixed IP: '1.1.1.1'"},
{'data': ['00000000-ffff-ffff-ffff-000000000000', '1.1.1.1'],
'error_msg': "Invalid data format for fixed IP: "
"'00000000-ffff-ffff-ffff-000000000000'"},
{'data': [['00000000-ffff-ffff-ffff-000000000000', '1.1.1.1']],
'error_msg': "Invalid data format for fixed IP: "
"'['00000000-ffff-ffff-ffff-000000000000', "
"'1.1.1.1']'"},
{'data': [{'subnet_id': '00000000-0fff-ffff-ffff-000000000000',
'ip_address': '1.1.1.1'},
{'subnet_id': '00000000-ffff-ffff-ffff-000000000000',
'ip_address': '1.1.1.1'}],
'error_msg': "Duplicate IP address '1.1.1.1'"}]
for fixed in fixed_ips:
msg = validators.validate_fixed_ips(fixed['data'])
self.assertEqual(fixed['error_msg'], msg)
fixed_ips = [[{'subnet_id': '00000000-ffff-ffff-ffff-000000000000',
'ip_address': '1.1.1.1'}],
[{'subnet_id': '00000000-0fff-ffff-ffff-000000000000',
'ip_address': '1.1.1.1'},
{'subnet_id': '00000000-ffff-ffff-ffff-000000000000',
'ip_address': '1.1.1.2'}]]
for fixed in fixed_ips:
msg = validators.validate_fixed_ips(fixed)
self.assertIsNone(msg)
def test_validate_nameservers(self):
ns_pools = [['1.1.1.2', '1.1.1.2'],
['www.hostname.com', 'www.hostname.com'],
['1000.0.0.1'],
['www.hostname.com'],
['www.great.marathons.to.travel'],
['valid'],
['77.hostname.com'],
['1' * 59],
['www.internal.hostname.com'],
None]
for ns in ns_pools:
msg = validators.validate_nameservers(ns, None)
self.assertIsNotNone(msg)
ns_pools = [['100.0.0.2'],
['1.1.1.1', '1.1.1.2']]
for ns in ns_pools:
msg = validators.validate_nameservers(ns, None)
self.assertIsNone(msg)
def test_validate_hostroutes(self):
hostroute_pools = [[{'destination': '100.0.0.0/24'}],
[{'nexthop': '10.0.2.20'}],
[{'nexthop': '10.0.2.20',
'forza': 'juve',
'destination': '100.0.0.0/8'}],
[{'nexthop': '1110.0.2.20',
'destination': '100.0.0.0/8'}],
[{'nexthop': '10.0.2.20',
'destination': '100.0.0.0'}],
[{'nexthop': '10.0.2.20',
'destination': '100.0.0.0/8'},
{'nexthop': '10.0.2.20',
'destination': '100.0.0.0/8'}],
[None],
None]
for host_routes in hostroute_pools:
msg = validators.validate_hostroutes(host_routes, None)
self.assertIsNotNone(msg)
hostroute_pools = [[{'destination': '100.0.0.0/24',
'nexthop': '10.0.2.20'}],
[{'nexthop': '10.0.2.20',
'destination': '100.0.0.0/8'},
{'nexthop': '10.0.2.20',
'destination': '101.0.0.0/8'}]]
for host_routes in hostroute_pools:
msg = validators.validate_hostroutes(host_routes, None)
self.assertIsNone(msg)
def test_validate_ip_address_or_none(self):
ip_addr = None
msg = validators.validate_ip_address_or_none(ip_addr)
self.assertIsNone(msg)
ip_addr = '1.1.1.1'
msg = validators.validate_ip_address_or_none(ip_addr)
self.assertIsNone(msg)
ip_addr = '1111.1.1.1'
msg = validators.validate_ip_address_or_none(ip_addr)
self.assertEqual("'%s' is not a valid IP address" % ip_addr, msg)
def test_uuid_pattern(self):
data = 'garbage'
msg = validators.validate_regex(data, constants.UUID_PATTERN)
self.assertIsNotNone(msg)
data = '00000000-ffff-ffff-ffff-000000000000'
msg = validators.validate_regex(data, constants.UUID_PATTERN)
self.assertIsNone(msg)
def test_mac_pattern(self):
# Valid - 3 octets
base_mac = "fa:16:3e:00:00:00"
msg = validators.validate_regex(base_mac, validators.MAC_PATTERN)
self.assertIsNone(msg)
# Valid - 4 octets
base_mac = "fa:16:3e:4f:00:00"
msg = validators.validate_regex(base_mac, validators.MAC_PATTERN)
self.assertIsNone(msg)
# Invalid - not unicast
base_mac = "01:16:3e:4f:00:00"
msg = validators.validate_regex(base_mac, validators.MAC_PATTERN)
self.assertIsNotNone(msg)
# Invalid - invalid format
base_mac = "a:16:3e:4f:00:00"
msg = validators.validate_regex(base_mac, validators.MAC_PATTERN)
self.assertIsNotNone(msg)
# Invalid - invalid format
base_mac = "ffa:16:3e:4f:00:00"
msg = validators.validate_regex(base_mac, validators.MAC_PATTERN)
self.assertIsNotNone(msg)
# Invalid - invalid format
base_mac = "01163e4f0000"
msg = validators.validate_regex(base_mac, validators.MAC_PATTERN)
self.assertIsNotNone(msg)
# Invalid - invalid format
base_mac = "01-16-3e-4f-00-00"
msg = validators.validate_regex(base_mac, validators.MAC_PATTERN)
self.assertIsNotNone(msg)
# Invalid - invalid format
base_mac = "00:16:3:f:00:00"
msg = validators.validate_regex(base_mac, validators.MAC_PATTERN)
self.assertIsNotNone(msg)
# Invalid - invalid format
base_mac = "12:3:4:5:67:89ab"
msg = validators.validate_regex(base_mac, validators.MAC_PATTERN)
self.assertIsNotNone(msg)
def _test_validate_subnet(self, validator, allow_none=False):
# Valid - IPv4
cidr = "10.0.2.0/24"
msg = validator(cidr, None)
self.assertIsNone(msg)
# Valid - IPv6 without final octets
cidr = "fe80::/24"
msg = validator(cidr, None)
self.assertIsNone(msg)
# Valid - IPv6 with final octets
cidr = "fe80::/24"
msg = validator(cidr, None)
self.assertIsNone(msg)
# Valid - uncompressed ipv6 address
cidr = "fe80:0:0:0:0:0:0:0/128"
msg = validator(cidr, None)
self.assertIsNone(msg)
# Valid - ipv6 address with multiple consecutive zero
cidr = "2001:0db8:0:0:1::1/128"
msg = validator(cidr, None)
self.assertIsNone(msg)
# Valid - ipv6 address with multiple consecutive zero
cidr = "2001:0db8::1:0:0:1/128"
msg = validator(cidr, None)
self.assertIsNone(msg)
# Valid - ipv6 address with multiple consecutive zero
cidr = "2001::0:1:0:0:1100/120"
msg = validator(cidr, None)
self.assertIsNone(msg)
# Invalid - abbreviated ipv4 address
cidr = "10/24"
msg = validator(cidr, None)
error = "'%s' is not a valid IP subnet" % cidr
self.assertEqual(error, msg)
# Invalid - IPv4 missing mask
cidr = "10.0.2.0"
msg = validator(cidr, None)
error = "'%s' is not a valid IP subnet" % cidr
self.assertEqual(error, msg)
# Valid - IPv4 with non-zero masked bits is ok
for i in range(1, 255):
cidr = "192.168.1.%s/24" % i
msg = validator(cidr, None)
self.assertIsNone(msg)
# Invalid - IPv6 without final octets, missing mask
cidr = "fe80::"
msg = validator(cidr, None)
error = "'%s' is not a valid IP subnet" % cidr
self.assertEqual(error, msg)
# Invalid - IPv6 with final octets, missing mask
cidr = "fe80::0"
msg = validator(cidr, None)
error = "'%s' is not a valid IP subnet" % cidr
self.assertEqual(error, msg)
# Invalid - Address format error
cidr = 'invalid'
msg = validator(cidr, None)
error = "'%s' is not a valid IP subnet" % cidr
self.assertEqual(error, msg)
cidr = None
msg = validator(cidr, None)
if allow_none:
self.assertIsNone(msg)
else:
error = "'%s' is not a valid IP subnet" % cidr
self.assertEqual(error, msg)
# Invalid - IPv4 with trailing CR
cidr = "10.0.2.0/24\r"
msg = validator(cidr, None)
error = "'%s' is not a valid IP subnet" % cidr
self.assertEqual(error, msg)
def test_validate_subnet(self):
self._test_validate_subnet(validators.validate_subnet)
def test_validate_route_cidr(self):
# Valid - CIDR
cidr = "10.0.0.0/8"
msg = validators.validate_route_cidr(cidr, None)
self.assertIsNone(msg)
# Valid - CIDR
cidr = "192.168.1.1/32"
msg = validators.validate_route_cidr(cidr, None)
self.assertIsNone(msg)
# Invalid - CIDR
cidr = "192.168.1.1/8"
msg = validators.validate_route_cidr(cidr, None)
error = "'%s' is not a valid CIDR" % cidr
self.assertEqual(error, msg)
# Invalid - loopback CIDR
cidr = "127.0.0.0/8"
msg = validators.validate_route_cidr(cidr, None)
error = _("'%(data)s' is not a routable CIDR") % {"data": cidr}
self.assertEqual(error, msg)
# Invalid - CIDR format error
cidr = 'invalid'
msg = validators.validate_route_cidr(cidr, None)
error = "'%s' is not a valid CIDR" % cidr
self.assertEqual(error, msg)
def test_validate_subnet_or_none(self):
self._test_validate_subnet(validators.validate_subnet_or_none,
allow_none=True)
def test_validate_subnet_list(self):
msg = validators.validate_subnet_list('abc')
self.assertEqual("'abc' is not a list", msg)
msg = validators.validate_subnet_list(['10.1.0.0/24',
'10.2.0.0/24',
'10.1.0.0/24'])
self.assertEqual("Duplicate items in the list: '10.1.0.0/24'", msg)
cidrs = ['10.1.0.0/24', '10.2.0.0']
msg = validators.validate_subnet_list(cidrs)
error = "'%s' is not a valid IP subnet" % cidrs[1]
self.assertEqual(error, msg)
def _test_validate_regex(self, validator, allow_none=False):
pattern = '[hc]at'
data = None
msg = validator(data, pattern)
if allow_none:
self.assertIsNone(msg)
else:
self.assertEqual("'None' is not a valid input", msg)
data = 'bat'
msg = validator(data, pattern)
self.assertEqual("'%s' is not a valid input" % data, msg)
data = 'hat'
msg = validator(data, pattern)
self.assertIsNone(msg)
data = 'cat'
msg = validator(data, pattern)
self.assertIsNone(msg)
def test_validate_regex(self):
self._test_validate_regex(validators.validate_regex)
def test_validate_regex_or_none(self):
self._test_validate_regex(validators.validate_regex_or_none,
allow_none=True)
def test_validate_list_of_regex_or_none(self):
pattern = '[hc]at|^$'
list_of_regex = ['hat', 'cat', '']
msg = validators.validate_list_of_regex_or_none(list_of_regex, pattern)
self.assertIsNone(msg)
list_of_regex = ['bat', 'hat', 'cat', '']
msg = validators.validate_list_of_regex_or_none(list_of_regex, pattern)
self.assertEqual("'bat' is not a valid input", msg)
empty_list = []
msg = validators.validate_list_of_regex_or_none(empty_list, pattern)
def test_validate_subnetpool_id(self):
msg = validators.validate_subnetpool_id(constants.IPV6_PD_POOL_ID)
self.assertIsNone(msg)
msg = validators.validate_subnetpool_id(
'00000000-ffff-ffff-ffff-000000000000')
self.assertIsNone(msg)
def test_validate_subnetpool_id_or_none(self):
msg = validators.validate_subnetpool_id_or_none(None)
self.assertIsNone(msg)
msg = validators.validate_subnetpool_id_or_none(
'00000000-ffff-ffff-ffff-000000000000')
self.assertIsNone(msg)
def test_validate_uuid(self):
invalid_uuids = [None,
123,
'123',
't5069610-744b-42a7-8bd8-ceac1a229cd4',
'e5069610-744bb-42a7-8bd8-ceac1a229cd4']
for uuid in invalid_uuids:
msg = validators.validate_uuid(uuid)
error = "'%s' is not a valid UUID" % uuid
self.assertEqual(error, msg)
msg = validators.validate_uuid('00000000-ffff-ffff-ffff-000000000000')
self.assertIsNone(msg)
def test_validate_uuid_list(self):
bad_uuid_list = ['00000000-ffff-ffff-ffff-000000000000',
'00000000-ffff-ffff-ffff-000000000001',
'123']
msg = validators.validate_uuid_list(bad_uuid_list,
valid_values='parameter not used')
error = "'%s' is not a valid UUID" % bad_uuid_list[2]
self.assertEqual(error, msg)
good_uuid_list = ['00000000-ffff-ffff-ffff-000000000000',
'00000000-ffff-ffff-ffff-000000000001']
msg = validators.validate_uuid_list(good_uuid_list,
valid_values='parameter not used')
self.assertIsNone(msg)
def test__validate_list_of_items(self):
# check not a list
items = [None,
123,
'e5069610-744b-42a7-8bd8-ceac1a229cd4',
'12345678123456781234567812345678',
{'uuid': 'e5069610-744b-42a7-8bd8-ceac1a229cd4'}]
for item in items:
msg = validators._validate_list_of_items(mock.Mock(), item)
error = "'%s' is not a list" % item
self.assertEqual(error, msg)
# check duplicate items in a list
duplicate_items = ['e5069610-744b-42a7-8bd8-ceac1a229cd4',
'f3eeab00-8367-4524-b662-55e64d4cacb5',
'e5069610-744b-42a7-8bd8-ceac1a229cd4']
msg = validators._validate_list_of_items(mock.Mock(), duplicate_items)
error = ("Duplicate items in the list: "
"'e5069610-744b-42a7-8bd8-ceac1a229cd4'")
self.assertEqual(error, msg)
# check valid lists
valid_lists = [[],
[1, 2, 3],
['a', 'b', 'c']]
for list_obj in valid_lists:
msg = validators._validate_list_of_items(
mock.Mock(return_value=None), list_obj)
self.assertIsNone(msg)
def test__test__validate_list_of_items_non_empty(self):
items = None
msg = validators._validate_list_of_items_non_empty(mock.Mock(), items)
error = "'%s' is not a list" % items
self.assertEqual(error, msg)
items = []
msg = validators._validate_list_of_items_non_empty(mock.Mock(), items)
self.assertEqual("List should not be empty", msg)
def test__validate_list_of_items_collect_duplicates(self):
items = ['a', 'b', 'duplicate_1', 'duplicate_2', 'duplicate_1',
'duplicate_2', 'duplicate_2', 'c']
msg = validators._validate_list_of_items(mock.Mock(), items)
error = ("Duplicate items in the list: '%s'"
% 'duplicate_1, duplicate_2')
self.assertEqual(error, msg)
items = [['a', 'b'], ['c', 'd'], ['a', 'b']]
msg = validators._validate_list_of_items(mock.Mock(), items)
error = "Duplicate items in the list: '%s'" % str(['a', 'b'])
self.assertEqual(error, msg)
items = [{'a': 'b'}, {'c': 'd'}, {'a': 'b'}]
msg = validators._validate_list_of_items(mock.Mock(), items)
error = "Duplicate items in the list: '%s'" % str({'a': 'b'})
self.assertEqual(error, msg)
def test_validate_dict_type(self):
for value in (None, True, '1', []):
self.assertEqual("'%s' is not a dictionary" % value,
validators.validate_dict(value))
def test_validate_dict_without_constraints(self):
msg = validators.validate_dict({})
self.assertIsNone(msg)
# Validate a dictionary without constraints.
msg = validators.validate_dict({'key': 'value'})
self.assertIsNone(msg)
def test_validate_a_valid_dict_with_constraints(self):
dictionary, constraints = self._construct_dict_and_constraints()
msg = validators.validate_dict(dictionary, constraints)
self.assertIsNone(msg, 'Validation of a valid dictionary failed.')
def test_validate_dict_with_invalid_validator(self):
dictionary, constraints = self._construct_dict_and_constraints()
constraints['key1'] = {'type:unsupported': None, 'required': True}
msg = validators.validate_dict(dictionary, constraints)
self.assertEqual("Validator 'type:unsupported' does not exist", msg)
def test_validate_dict_not_required_keys(self):
dictionary, constraints = self._construct_dict_and_constraints()
del dictionary['key2']
msg = validators.validate_dict(dictionary, constraints)
self.assertIsNone(msg, 'Field that was not required by the specs was'
'required by the validator.')
def test_validate_dict_required_keys(self):
dictionary, constraints = self._construct_dict_and_constraints()
del dictionary['key1']
msg = validators.validate_dict(dictionary, constraints)
self.assertIn('Expected keys:', msg)
def test_validate_dict_wrong_values(self):
dictionary, constraints = self._construct_dict_and_constraints()
dictionary['key1'] = 'UNSUPPORTED'
msg = validators.validate_dict(dictionary, constraints)
self.assertIsNotNone(msg)
def test_validate_dict_unexpected_keys(self):
dictionary, constraints = self._construct_dict_and_constraints()
dictionary['unexpected_key'] = 'val'
msg = validators.validate_dict(dictionary, constraints)
self.assertIn('Unexpected keys supplied:', msg)
def test_validate_dict_convert_boolean(self):
dictionary, constraints = self._construct_dict_and_constraints()
constraints['key_bool'] = {
'type:boolean': None,
'required': False,
'convert_to': converters.convert_to_boolean}
dictionary['key_bool'] = 'true'
msg = validators.validate_dict(dictionary, constraints)
self.assertIsNone(msg)
# Explicitly comparing with literal 'True' as assertTrue
# succeeds also for 'true'
self.assertIs(True, dictionary['key_bool'])
def test_subdictionary(self):
dictionary, constraints = self._construct_dict_and_constraints()
del dictionary['key3']['k4']
dictionary['key3']['k5'] = 'a string value'
msg = validators.validate_dict(dictionary, constraints)
self.assertIn('Expected keys:', msg)
def test_validate_dict_or_none(self):
dictionary, constraints = self._construct_dict_and_constraints()
# Check whether None is a valid value.
msg = validators.validate_dict_or_none(None, constraints)
self.assertIsNone(msg, 'Validation of a None dictionary failed.')
# Check validation of a regular dictionary.
msg = validators.validate_dict_or_none(dictionary, constraints)
self.assertIsNone(msg, 'Validation of a valid dictionary failed.')
def test_validate_dict_or_empty(self):
dictionary, constraints = self._construct_dict_and_constraints()
# Check whether an empty dictionary is valid.
msg = validators.validate_dict_or_empty({}, constraints)
self.assertIsNone(msg, 'Validation of a None dictionary failed.')
# Check validation of a regular dictionary.
msg = validators.validate_dict_or_empty(dictionary, constraints)
self.assertIsNone(msg, 'Validation of a valid dictionary failed.')
def test_validate_dict_or_nodata(self):
dictionary, constraints = self._construct_dict_and_constraints()
# Check whether no data is a valid value.
msg = validators.validate_dict_or_nodata(None, constraints)
self.assertIsNone(msg, 'Validation of None for no-data failed.')
msg = validators.validate_dict_or_nodata({}, constraints)
self.assertIsNone(msg, 'Validation of empty dict for no-data failed.')
# Check validation of a regular dictionary.
msg = validators.validate_dict_or_nodata(dictionary, constraints)
self.assertIsNone(msg, 'Validation of a valid dictionary failed.')
def test_validate_non_negative(self):
msg = validators.validate_non_negative('abc')
self.assertEqual("'abc' is not an integer", msg)
for value in (-1, '-2'):
self.assertEqual("'%s' should be non-negative" % value,
validators.validate_non_negative(value))
for value in (0, 1, '2', True, False):
msg = validators.validate_non_negative(value)
self.assertIsNone(msg)
def test_validate_subports_invalid_body(self):
self.assertIsNotNone(validators.validate_subports(None))
def test_validate_subports_invalid_subport_object(self):
self.assertIsNotNone(validators.validate_subports(['foo_port']))
def test_validate_subports_invalid_port_uuid(self):
body = [{'port_id': 'foo_port'}]
self.assertIsNotNone(validators.validate_subports(body))
def test_validate_subports_invalid_missing_port_id(self):
body = [{'poort_id': 'foo_port'}]
self.assertIsNotNone(validators.validate_subports(body))
def test_validate_subports_invalid_duplicate_port_ids(self):
body = [
{'port_id': '00000000-ffff-ffff-ffff-000000000000'},
{'port_id': '00000000-ffff-ffff-ffff-000000000000'}
]
self.assertIsNotNone(validators.validate_subports(body))
def test_validate_subports_invalid_incomplete_segmentation_details(self):
body = [
{'port_id': '00000000-ffff-ffff-ffff-000000000000',
'segmentation_id': '3'}
]
self.assertIsNotNone(validators.validate_subports(body))
def test_validate_subports_invalid_unknown_paramenter(self):
body = [
{'port_id': '00000000-ffff-ffff-ffff-000000000000',
'segmentation_id': '3', 'segmeNAtion_type': 'vlan'}
]
self.assertIsNotNone(validators.validate_subports(body))
def test_validate_subports_invalid_duplicate_segmentation_id(self):
body = [
{'port_id': '00000000-ffff-ffff-ffff-000000000000',
'segmentation_id': 0, 'segmentation_type': 'vlan'},
{'port_id': '11111111-ffff-ffff-ffff-000000000000',
'segmentation_id': 0, 'segmentation_type': 'vlan'}
]
self.assertIsNotNone(validators.validate_subports(body))
def test_validate_subports_with_segmentation_id_0(self):
body = [
{'port_id': '00000000-ffff-ffff-ffff-000000000000',
'segmentation_id': '0', 'segmentation_type': 'vlan'}
]
self.assertIsNone(validators.validate_subports(body))
def test_validate_subports_inherit_segmentation_details(self):
body = [
{'port_id': '00000000-ffff-ffff-ffff-000000000000',
'segmentation_type': 'inherit'}
]
self.assertIsNone(validators.validate_subports(body))
def test_validate_subports_valid_unique_segmentation_id(self):
body = [
{'port_id': '00000000-ffff-ffff-ffff-000000000000',
'segmentation_id': '3', 'segmentation_type': 'vlan'},
{'port_id': '11111111-ffff-ffff-ffff-000000000000',
'segmentation_id': '3', 'segmentation_type': 'vxlan'}
]
self.assertIsNone(validators.validate_subports(body))
def test_validate_subports_valid_empty_body(self):
self.assertIsNone(validators.validate_subports([]))
def test_validate_subports_valid_subports_with_segmentation_details(self):
body = [
{'port_id': '00000000-ffff-ffff-ffff-000000000000',
'segmentation_id': '3', 'segmentation_type': 'vlan'},
{'port_id': '11111111-ffff-ffff-ffff-000000000000',
'segmentation_id': '5', 'segmentation_type': 'vlan'}
]
self.assertIsNone(validators.validate_subports(body))
def test_validate_subports_valid_subports(self):
body = [
{'port_id': '00000000-ffff-ffff-ffff-000000000000'},
{'port_id': '11111111-ffff-ffff-ffff-000000000000'},
]
self.assertIsNone(validators.validate_subports(body))
@mock.patch('oslo_config.cfg.CONF')
def test_validate_ethertype_valid_string_new(self, CONF):
CONF.sg_filter_ethertypes = True
self.assertIsNone(validators.validate_ethertype('IPv4'))
self.assertIsNone(validators.validate_ethertype('IPv6'))
@mock.patch('oslo_config.cfg.CONF')
def test_validate_ethertype_valid_string_old(self, CONF):
CONF.sg_filter_ethertypes = False
self.assertIsNone(validators.validate_ethertype('IPv4'))
self.assertIsNone(validators.validate_ethertype('IPv6'))
@mock.patch('oslo_config.cfg.CONF')
def test_validate_ethertype_invalid_string(self, CONF):
CONF.sg_filter_ethertypes = False
self.assertEqual(('Ethertype 0x4008 is not a valid ethertype, must be '
'one of IPv4,IPv6.'),
validators.validate_ethertype('0x4008'))
@mock.patch('oslo_config.cfg.CONF')
def test_validate_ethertype_extended_valid_string(self, CONF):
CONF.sg_filter_ethertypes = True
self.assertIsNone(validators.validate_ethertype('0x4008'))
@mock.patch('oslo_config.cfg.CONF')
def test_validate_ethertype_extended_valid_hexint(self, CONF):
CONF.sg_filter_ethertypes = True
self.assertIsNone(validators.validate_ethertype(0x4008))
@mock.patch('oslo_config.cfg.CONF')
def test_validate_ethertype_extended_invalid_negative(self, CONF):
CONF.sg_filter_ethertypes = True
self.assertEqual(("Ethertype -16392 is not a two octet "
"hexadecimal number or ethertype name."),
validators.validate_ethertype('-0x4008'))
@mock.patch('oslo_config.cfg.CONF')
def test_validate_ethertype_extended_invalid_nonhex(self, CONF):
CONF.sg_filter_ethertypes = True
self.assertEqual(("Ethertype invalid is not a two octet "
"hexadecimal number or ethertype name."),
validators.validate_ethertype('invalid'))
@mock.patch('oslo_config.cfg.CONF')
def test_validate_ethertype_extended_invalid_toobig(self, CONF):
CONF.sg_filter_ethertypes = True
self.assertEqual(("Ethertype 3735928559 is not a two octet "
"hexadecimal number or ethertype name."),
validators.validate_ethertype('0xdeadbeef'))
class TestValidateIPSubnetNone(base.BaseTestCase):
def test_validate_none(self):
self.assertIsNone(validators.validate_ip_or_subnet_or_none(None))
def test_validate_ipv4(self):
testdata = "172.0.0.1"
self.assertIsNone(validators.validate_ip_or_subnet_or_none(testdata))
def test_validate_ipv4_subnet(self):
testdata = "172.0.0.1/24"
self.assertIsNone(validators.validate_ip_or_subnet_or_none(testdata))
def test_validate_ipv6(self):
testdata = "2001:0db8:0a0b:12f0:0000:0000:0000:0001"
self.assertIsNone(validators.validate_ip_or_subnet_or_none(testdata))
def test_validate_ipv6_subnet(self):
testdata = "::1/128"
self.assertIsNone(validators.validate_ip_or_subnet_or_none(testdata))
def test_validate_ipv4_invalid(self):
testdata = "300.0.0.1"
self.assertEqual(("'300.0.0.1' is neither a valid IP address, nor is "
"it a valid IP subnet"),
validators.validate_ip_or_subnet_or_none(testdata))
def test_validate_ipv4_subnet_invalid(self):
testdata = "172.0.0.1/45"
self.assertEqual(("'172.0.0.1/45' is neither a valid IP address, nor "
"is it a valid IP subnet"),
validators.validate_ip_or_subnet_or_none(testdata))
def test_validate_ipv6_invalid(self):
testdata = "xxxx:0db8:0a0b:12f0:0000:0000:0000:0001"
self.assertEqual(("'xxxx:0db8:0a0b:12f0:0000:0000:0000:0001' is "
"neither a valid IP address, nor is it a valid IP "
"subnet"),
validators.validate_ip_or_subnet_or_none(testdata))
def test_validate_ipv6_subnet_invalid(self):
testdata = "::1/2048"
self.assertEqual(("'::1/2048' is neither a valid IP address, nor is "
"it a valid IP subnet"),
validators.validate_ip_or_subnet_or_none(testdata))
class TestPortRangeValidation(base.BaseTestCase):
def test_valid_port_specific_range(self):
result = validators.validate_port_range_or_none("4:5",
[1, 65535])
self.assertIsNone(result)
def test_invalid_port_specific_range(self):
result = validators.validate_port_range_or_none("4:500000",
[1, 65535])
self.assertEqual("Invalid port: 500000", result)
def test_invalid_port_for_specific_range(self):
result = validators.validate_port_range_or_none("0:10",
[1, 65535])
self.assertEqual("Invalid port: 0, the port must be in"
" the range [1, 65535]", result)
def test_valid_port(self):
result = validators.validate_port_range_or_none("80")
self.assertIsNone(result)
def test_valid_port_integer(self):
result = validators.validate_port_range_or_none(80)
self.assertIsNone(result)
def test_valid_range(self):
# NOTE(huntxu): This case would fail when ports are compared as
# strings, since '9' > '1111'.
result = validators.validate_port_range_or_none("9:1111")
self.assertIsNone(result)
def test_port_too_high(self):
result = validators.validate_port_range_or_none("99999")
self.assertEqual("Invalid port: 99999", result)
def test_port_too_low(self):
result = validators.validate_port_range_or_none("-1")
self.assertEqual("Invalid port: -1", result)
def test_range_too_high(self):
result = validators.validate_port_range_or_none("80:99999")
self.assertEqual("Invalid port: 99999", result)
def test_range_too_low(self):
result = validators.validate_port_range_or_none("-1:8888")
self.assertEqual("Invalid port: -1", result)
def test_range_wrong_way(self):
# NOTE(huntxu): This case would fail when ports are compared as
# strings, since '1111' < '9'.
result = validators.validate_port_range_or_none("1111:9")
self.assertEqual("First port in a port range must be lower than the "
"second port", result)
def test_range_invalid(self):
result = validators.validate_port_range_or_none("DEAD:BEEF")
self.assertEqual("Invalid port: DEAD", result)
def test_range_bad_input(self):
result = validators.validate_port_range_or_none(['a', 'b', 'c'])
self.assertEqual("Invalid port: ['a', 'b', 'c']", result)
def test_range_colon(self):
result = validators.validate_port_range_or_none(":")
self.assertEqual("Port range must be two integers separated by a "
"colon", result)
def test_too_many_colons(self):
result = validators.validate_port_range_or_none("80:888:8888")
self.assertEqual("Port range must be two integers separated by a "
"colon", result)
class TestAnyKeySpecs(base.BaseTestCase):
def test_data_is_none(self):
self.assertIsNone(
validators.validate_any_key_specs_or_none(None, key_specs={}))
def test_data_is_not_list(self):
for t in [dict(), set(), 'abc', 1, True]:
self.assertRaises(
n_exc.InvalidInput,
validators.validate_any_key_specs_or_none, t, key_specs={})
def test_data_invalid_keys(self):
data = [{'opt_name': 'a', 'opt_value': 'A'},
{'opt_name': 'b', 'opt_valuee': 'B'}]
self.assertRaisesRegex(
n_exc.InvalidInput,
"No valid key specs",
validators.validate_any_key_specs_or_none,
data, key_specs=extra_dhcp_opt.EXTRA_DHCP_OPT_KEY_SPECS)
def test_data_optional_key(self):
data = [{'opt_name': 'a', 'opt_value': 'A'},
{'opt_name': 'b', 'opt_value': 'B', 'ip_version': '4'}]
self.assertIsNone(
validators.validate_any_key_specs_or_none(
data, key_specs=extra_dhcp_opt.EXTRA_DHCP_OPT_KEY_SPECS))
def test_data_optional_key_invalid(self):
data = [{'opt_name': 'a', 'opt_value': 'A'},
{'opt_name': 'b', 'opt_value': 'B', 'ip_version': '3'}]
self.assertRaisesRegex(
n_exc.InvalidInput,
"No valid key specs",
validators.validate_any_key_specs_or_none,
data, key_specs=extra_dhcp_opt.EXTRA_DHCP_OPT_KEY_SPECS)
def test_data_conditional_spec(self):
data = [{'opt_name': 'router', 'opt_value': None},
{'opt_name': 'b', 'opt_value': 'B', 'ip_version': '4'}]
self.assertIsNone(
validators.validate_any_key_specs_or_none(
data, key_specs=extra_dhcp_opt.EXTRA_DHCP_OPT_KEY_SPECS))
class TestServicePluginType(base.BaseTestCase):
def setUp(self):
super().setUp()
self._plugins = directory._PluginDirectory()
self._plugins.add_plugin('stype', mock.Mock())
self.useFixture(fixture.PluginDirectoryFixture(
plugin_directory=self._plugins))
def test_valid_plugin_type(self):
self.assertIsNone(validators.validate_service_plugin_type('stype'))
def test_invalid_plugin_type(self):
self.assertRaisesRegex(
n_exc.InvalidServiceType,
'Invalid service type',
validators.validate_service_plugin_type, 'ntype')
|