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 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196
|
# 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 copy
from random import choice
from random import randint
from unittest import mock
import uuid
from openstack.network.v2 import _proxy
from openstack.network.v2 import address_group as _address_group
from openstack.network.v2 import address_scope as _address_scope
from openstack.network.v2 import agent as network_agent
from openstack.network.v2 import auto_allocated_topology as allocated_topology
from openstack.network.v2 import availability_zone as _availability_zone
from openstack.network.v2 import extension as _extension
from openstack.network.v2 import flavor as _flavor
from openstack.network.v2 import local_ip as _local_ip
from openstack.network.v2 import local_ip_association as _local_ip_association
from openstack.network.v2 import ndp_proxy as _ndp_proxy
from openstack.network.v2 import network as _network
from openstack.network.v2 import network_ip_availability as _ip_availability
from openstack.network.v2 import network_segment_range as _segment_range
from openstack.network.v2 import port as _port
from openstack.network.v2 import rbac_policy as network_rbac
from openstack.network.v2 import security_group as _security_group
from openstack.network.v2 import segment as _segment
from openstack.network.v2 import service_profile as _service_profile
from openstack.network.v2 import trunk as _trunk
from openstackclient.tests.unit import fakes
from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes
from openstackclient.tests.unit import utils
RULE_TYPE_BANDWIDTH_LIMIT = 'bandwidth-limit'
RULE_TYPE_DSCP_MARKING = 'dscp-marking'
RULE_TYPE_MINIMUM_BANDWIDTH = 'minimum-bandwidth'
RULE_TYPE_MINIMUM_PACKET_RATE = 'minimum-packet-rate'
VALID_QOS_RULES = [
RULE_TYPE_BANDWIDTH_LIMIT,
RULE_TYPE_DSCP_MARKING,
RULE_TYPE_MINIMUM_BANDWIDTH,
RULE_TYPE_MINIMUM_PACKET_RATE,
]
VALID_DSCP_MARKS = [
0,
8,
10,
12,
14,
16,
18,
20,
22,
24,
26,
28,
30,
32,
34,
36,
38,
40,
46,
48,
56,
]
class FakeClientMixin:
def setUp(self):
super().setUp()
self.app.client_manager.network = mock.Mock(spec=_proxy.Proxy)
self.network_client = self.app.client_manager.network
class TestNetworkV2(
identity_fakes.FakeClientMixin,
FakeClientMixin,
utils.TestCommand,
): ...
def create_one_extension(attrs=None):
"""Create a fake extension.
:param Dictionary attrs:
A dictionary with all attributes
:return:
An Extension object with name, namespace, etc.
"""
attrs = attrs or {}
# Set default attributes.
extension_info = {
'name': 'name-' + uuid.uuid4().hex,
'description': 'description-' + uuid.uuid4().hex,
'alias': 'Dystopian',
'links': [],
'updated_at': '2013-07-09T12:00:0-00:00',
}
# Overwrite default attributes.
extension_info.update(attrs)
extension = _extension.Extension(**extension_info)
return extension
class FakeNetworkQosPolicy:
"""Fake one or more QoS policies."""
@staticmethod
def create_one_qos_policy(attrs=None):
"""Create a fake QoS policy.
:param Dictionary attrs:
A dictionary with all attributes
:return:
A FakeResource object with name, id, etc.
"""
attrs = attrs or {}
qos_id = attrs.get('id') or 'qos-policy-id-' + uuid.uuid4().hex
rule_attrs = {'qos_policy_id': qos_id}
rules = [FakeNetworkQosRule.create_one_qos_rule(rule_attrs)]
# Set default attributes.
qos_policy_attrs = {
'name': 'qos-policy-name-' + uuid.uuid4().hex,
'id': qos_id,
'is_default': False,
'project_id': 'project-id-' + uuid.uuid4().hex,
'shared': False,
'description': 'qos-policy-description-' + uuid.uuid4().hex,
'rules': rules,
'location': 'MUNCHMUNCHMUNCH',
}
# Overwrite default attributes.
qos_policy_attrs.update(attrs)
qos_policy = fakes.FakeResource(
info=copy.deepcopy(qos_policy_attrs), loaded=True
)
# Set attributes with special mapping in OpenStack SDK.
qos_policy.is_shared = qos_policy_attrs['shared']
return qos_policy
@staticmethod
def create_qos_policies(attrs=None, count=2):
"""Create multiple fake QoS policies.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of QoS policies to fake
:return:
A list of FakeResource objects faking the QoS policies
"""
qos_policies = []
for i in range(0, count):
qos_policies.append(
FakeNetworkQosPolicy.create_one_qos_policy(attrs)
)
return qos_policies
@staticmethod
def get_qos_policies(qos_policies=None, count=2):
"""Get an iterable MagicMock object with a list of faked QoS policies.
If qos policies list is provided, then initialize the Mock object
with the list. Otherwise create one.
:param List qos_policies:
A list of FakeResource objects faking qos policies
:param int count:
The number of QoS policies to fake
:return:
An iterable Mock object with side_effect set to a list of faked
QoS policies
"""
if qos_policies is None:
qos_policies = FakeNetworkQosPolicy.create_qos_policies(count)
return mock.Mock(side_effect=qos_policies)
class FakeNetworkSecGroup:
"""Fake one security group."""
@staticmethod
def create_one_security_group(attrs=None):
"""Create a fake security group.
:param Dictionary attrs:
A dictionary with all attributes
:return:
A FakeResource object with name, id, etc.
"""
attrs = attrs or {}
sg_id = attrs.get('id') or 'security-group-id-' + uuid.uuid4().hex
# Set default attributes.
security_group_attrs = {
'name': 'security-group-name-' + uuid.uuid4().hex,
'id': sg_id,
'project_id': 'project-id-' + uuid.uuid4().hex,
'description': 'security-group-description-' + uuid.uuid4().hex,
'location': 'MUNCHMUNCHMUNCH',
}
security_group = fakes.FakeResource(
info=copy.deepcopy(security_group_attrs), loaded=True
)
return security_group
class FakeNetworkQosRule:
"""Fake one or more Network QoS rules."""
@staticmethod
def create_one_qos_rule(attrs=None):
"""Create a fake Network QoS rule.
:param Dictionary attrs:
A dictionary with all attributes
:return:
A FakeResource object with name, id, etc.
"""
attrs = attrs or {}
# Set default attributes.
type = attrs.get('type') or choice(VALID_QOS_RULES)
qos_rule_attrs = {
'id': 'qos-rule-id-' + uuid.uuid4().hex,
'qos_policy_id': 'qos-policy-id-' + uuid.uuid4().hex,
'project_id': 'project-id-' + uuid.uuid4().hex,
'type': type,
'location': 'MUNCHMUNCHMUNCH',
}
if type == RULE_TYPE_BANDWIDTH_LIMIT:
qos_rule_attrs['max_kbps'] = randint(1, 10000)
qos_rule_attrs['max_burst_kbits'] = randint(1, 10000)
qos_rule_attrs['direction'] = 'egress'
elif type == RULE_TYPE_DSCP_MARKING:
qos_rule_attrs['dscp_mark'] = choice(VALID_DSCP_MARKS)
elif type == RULE_TYPE_MINIMUM_BANDWIDTH:
qos_rule_attrs['min_kbps'] = randint(1, 10000)
qos_rule_attrs['direction'] = 'egress'
elif type == RULE_TYPE_MINIMUM_PACKET_RATE:
qos_rule_attrs['min_kpps'] = randint(1, 10000)
qos_rule_attrs['direction'] = 'egress'
# Overwrite default attributes.
qos_rule_attrs.update(attrs)
qos_rule = fakes.FakeResource(
info=copy.deepcopy(qos_rule_attrs), loaded=True
)
return qos_rule
@staticmethod
def create_qos_rules(attrs=None, count=2):
"""Create multiple fake Network QoS rules.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of Network QoS rule to fake
:return:
A list of FakeResource objects faking the Network QoS rules
"""
qos_rules = []
for i in range(0, count):
qos_rules.append(FakeNetworkQosRule.create_one_qos_rule(attrs))
return qos_rules
@staticmethod
def get_qos_rules(qos_rules=None, count=2):
"""Get a list of faked Network QoS rules.
If Network QoS rules list is provided, then initialize the Mock
object with the list. Otherwise create one.
:param List qos_rules:
A list of FakeResource objects faking Network QoS rules
:param int count:
The number of QoS minimum bandwidth rules to fake
:return:
An iterable Mock object with side_effect set to a list of faked
qos minimum bandwidth rules
"""
if qos_rules is None:
qos_rules = FakeNetworkQosRule.create_qos_rules(count)
return mock.Mock(side_effect=qos_rules)
class FakeNetworkQosRuleType:
"""Fake one or more Network QoS rule types."""
@staticmethod
def create_one_qos_rule_type(attrs=None):
"""Create a fake Network QoS rule type.
:param Dictionary attrs:
A dictionary with all attributes
:return:
A FakeResource object with name, id, etc.
"""
attrs = attrs or {}
# Set default attributes.
qos_rule_type_attrs = {
'type': 'rule-type-' + uuid.uuid4().hex,
'location': 'MUNCHMUNCHMUNCH',
}
# Overwrite default attributes.
qos_rule_type_attrs.update(attrs)
return fakes.FakeResource(
info=copy.deepcopy(qos_rule_type_attrs), loaded=True
)
@staticmethod
def create_qos_rule_types(attrs=None, count=2):
"""Create multiple fake Network QoS rule types.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of QoS rule types to fake
:return:
A list of FakeResource objects faking the QoS rule types
"""
qos_rule_types = []
for i in range(0, count):
qos_rule_types.append(
FakeNetworkQosRuleType.create_one_qos_rule_type(attrs)
)
return qos_rule_types
class FakeRouter:
"""Fake one or more routers."""
@staticmethod
def create_one_router(attrs=None):
"""Create a fake router.
:param Dictionary attrs:
A dictionary with all attributes
:return:
A FakeResource object, with id, name, admin_state_up,
status, project_id
"""
attrs = attrs or {}
# Set default attributes.
router_attrs = {
'id': 'router-id-' + uuid.uuid4().hex,
'name': 'router-name-' + uuid.uuid4().hex,
'status': 'ACTIVE',
'admin_state_up': True,
'description': 'router-description-' + uuid.uuid4().hex,
'distributed': False,
'ha': False,
'project_id': 'project-id-' + uuid.uuid4().hex,
'routes': [],
'external_gateway_info': {},
'availability_zone_hints': [],
'availability_zones': [],
'tags': [],
'location': 'MUNCHMUNCHMUNCH',
}
# Overwrite default attributes.
router_attrs.update(attrs)
router = fakes.FakeResource(
info=copy.deepcopy(router_attrs), loaded=True
)
# Set attributes with special mapping in OpenStack SDK.
router.is_admin_state_up = router_attrs['admin_state_up']
router.is_distributed = router_attrs['distributed']
router.is_ha = router_attrs['ha']
return router
@staticmethod
def create_routers(attrs=None, count=2):
"""Create multiple fake routers.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of routers to fake
:return:
A list of FakeResource objects faking the routers
"""
routers = []
for i in range(0, count):
routers.append(FakeRouter.create_one_router(attrs))
return routers
@staticmethod
def get_routers(routers=None, count=2):
"""Get an iterable Mock object with a list of faked routers.
If routers list is provided, then initialize the Mock object with the
list. Otherwise create one.
:param List routers:
A list of FakeResource objects faking routers
:param int count:
The number of routers to fake
:return:
An iterable Mock object with side_effect set to a list of faked
routers
"""
if routers is None:
routers = FakeRouter.create_routers(count)
return mock.Mock(side_effect=routers)
class FakeSecurityGroup:
"""Fake one or more security groups."""
@staticmethod
def create_one_security_group(attrs=None):
"""Create a fake security group.
:param Dictionary attrs:
A dictionary with all attributes
:return:
A FakeResource object, with id, name, etc.
"""
attrs = attrs or {}
# Set default attributes.
security_group_attrs = {
'id': 'security-group-id-' + uuid.uuid4().hex,
'name': 'security-group-name-' + uuid.uuid4().hex,
'description': 'security-group-description-' + uuid.uuid4().hex,
'stateful': True,
'project_id': 'project-id-' + uuid.uuid4().hex,
'security_group_rules': [],
'tags': [],
'location': 'MUNCHMUNCHMUNCH',
}
# Overwrite default attributes.
security_group_attrs.update(attrs)
security_group = fakes.FakeResource(
info=copy.deepcopy(security_group_attrs), loaded=True
)
return security_group
@staticmethod
def create_security_groups(attrs=None, count=2):
"""Create multiple fake security groups.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of security groups to fake
:return:
A list of FakeResource objects faking the security groups
"""
security_groups = []
for i in range(0, count):
security_groups.append(
FakeSecurityGroup.create_one_security_group(attrs)
)
return security_groups
@staticmethod
def get_security_groups(security_groups=None, count=2):
"""Get an iterable Mock object with a list of faked security groups.
If security groups list is provided, then initialize the Mock object
with the list. Otherwise create one.
:param List security_groups:
A list of FakeResource objects faking security groups
:param int count:
The number of security groups to fake
:return:
An iterable Mock object with side_effect set to a list of faked
security groups
"""
if security_groups is None:
security_groups = FakeSecurityGroup.create_security_groups(count)
return mock.Mock(side_effect=security_groups)
class FakeSecurityGroupRule:
"""Fake one or more security group rules."""
@staticmethod
def create_one_security_group_rule(attrs=None):
"""Create a fake security group rule.
:param Dictionary attrs:
A dictionary with all attributes
:return:
A FakeResource object, with id, etc.
"""
attrs = attrs or {}
# Set default attributes.
security_group_rule_attrs = {
'description': 'security-group-rule-description-'
+ uuid.uuid4().hex,
'direction': 'ingress',
'ether_type': 'IPv4',
'id': 'security-group-rule-id-' + uuid.uuid4().hex,
'port_range_max': None,
'port_range_min': None,
'protocol': None,
'remote_group_id': None,
'remote_address_group_id': None,
'remote_ip_prefix': '0.0.0.0/0',
'security_group_id': 'security-group-id-' + uuid.uuid4().hex,
'project_id': 'project-id-' + uuid.uuid4().hex,
'location': 'MUNCHMUNCHMUNCH',
}
# Overwrite default attributes.
security_group_rule_attrs.update(attrs)
security_group_rule = fakes.FakeResource(
info=copy.deepcopy(security_group_rule_attrs), loaded=True
)
return security_group_rule
@staticmethod
def create_security_group_rules(attrs=None, count=2):
"""Create multiple fake security group rules.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of security group rules to fake
:return:
A list of FakeResource objects faking the security group rules
"""
security_group_rules = []
for i in range(0, count):
security_group_rules.append(
FakeSecurityGroupRule.create_one_security_group_rule(attrs)
)
return security_group_rules
@staticmethod
def get_security_group_rules(security_group_rules=None, count=2):
"""Get an iterable Mock with a list of faked security group rules.
If security group rules list is provided, then initialize the Mock
object with the list. Otherwise create one.
:param List security_group_rules:
A list of FakeResource objects faking security group rules
:param int count:
The number of security group rules to fake
:return:
An iterable Mock object with side_effect set to a list of faked
security group rules
"""
if security_group_rules is None:
security_group_rules = (
FakeSecurityGroupRule.create_security_group_rules(count)
)
return mock.Mock(side_effect=security_group_rules)
class FakeSubnet:
"""Fake one or more subnets."""
@staticmethod
def create_one_subnet(attrs=None):
"""Create a fake subnet.
:param Dictionary attrs:
A dictionary with all attributes
:return:
A FakeResource object faking the subnet
"""
attrs = attrs or {}
# Set default attributes.
project_id = 'project-id-' + uuid.uuid4().hex
subnet_attrs = {
'id': 'subnet-id-' + uuid.uuid4().hex,
'name': 'subnet-name-' + uuid.uuid4().hex,
'network_id': 'network-id-' + uuid.uuid4().hex,
'cidr': '10.10.10.0/24',
'project_id': project_id,
'enable_dhcp': True,
'dns_nameservers': [],
'allocation_pools': [],
'host_routes': [],
'ip_version': 4,
'gateway_ip': '10.10.10.1',
'ipv6_address_mode': None,
'ipv6_ra_mode': None,
'segment_id': None,
'service_types': [],
'subnetpool_id': None,
'description': 'subnet-description-' + uuid.uuid4().hex,
'tags': [],
'location': 'MUNCHMUNCHMUNCH',
}
# Overwrite default attributes.
subnet_attrs.update(attrs)
subnet = fakes.FakeResource(
info=copy.deepcopy(subnet_attrs), loaded=True
)
# Set attributes with special mappings in OpenStack SDK.
subnet.is_dhcp_enabled = subnet_attrs['enable_dhcp']
subnet.subnet_pool_id = subnet_attrs['subnetpool_id']
return subnet
@staticmethod
def create_subnets(attrs=None, count=2):
"""Create multiple fake subnets.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of subnets to fake
:return:
A list of FakeResource objects faking the subnets
"""
subnets = []
for i in range(0, count):
subnets.append(FakeSubnet.create_one_subnet(attrs))
return subnets
@staticmethod
def get_subnets(subnets=None, count=2):
"""Get an iterable Mock object with a list of faked subnets.
If subnets list is provided, then initialize the Mock object
with the list. Otherwise create one.
:param List subnets:
A list of FakeResource objects faking subnets
:param int count:
The number of subnets to fake
:return:
An iterable Mock object with side_effect set to a list of faked
subnets
"""
if subnets is None:
subnets = FakeSubnet.create_subnets(count)
return mock.Mock(side_effect=subnets)
class FakeFloatingIP:
"""Fake one or more floating ip."""
@staticmethod
def create_one_floating_ip(attrs=None):
"""Create a fake floating ip.
:param Dictionary attrs:
A dictionary with all attributes
:return:
A FakeResource object, with id, ip, and so on
"""
attrs = attrs or {}
# Set default attributes.
floating_ip_attrs = {
'id': 'floating-ip-id-' + uuid.uuid4().hex,
'floating_ip_address': '1.0.9.0',
'fixed_ip_address': '2.0.9.0',
'dns_domain': None,
'dns_name': None,
'status': 'DOWN',
'floating_network_id': 'network-id-' + uuid.uuid4().hex,
'router_id': 'router-id-' + uuid.uuid4().hex,
'port_id': 'port-id-' + uuid.uuid4().hex,
'project_id': 'project-id-' + uuid.uuid4().hex,
'description': 'floating-ip-description-' + uuid.uuid4().hex,
'qos_policy_id': 'qos-policy-id-' + uuid.uuid4().hex,
'tags': [],
'location': 'MUNCHMUNCHMUNCH',
}
# Overwrite default attributes.
floating_ip_attrs.update(attrs)
floating_ip = fakes.FakeResource(
info=copy.deepcopy(floating_ip_attrs), loaded=True
)
return floating_ip
@staticmethod
def create_floating_ips(attrs=None, count=2):
"""Create multiple fake floating ips.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of floating ips to fake
:return:
A list of FakeResource objects faking the floating ips
"""
floating_ips = []
for i in range(0, count):
floating_ips.append(FakeFloatingIP.create_one_floating_ip(attrs))
return floating_ips
@staticmethod
def get_floating_ips(floating_ips=None, count=2):
"""Get an iterable Mock object with a list of faked floating ips.
If floating_ips list is provided, then initialize the Mock object
with the list. Otherwise create one.
:param List floating_ips:
A list of FakeResource objects faking floating ips
:param int count:
The number of floating ips to fake
:return:
An iterable Mock object with side_effect set to a list of faked
floating ips
"""
if floating_ips is None:
floating_ips = FakeFloatingIP.create_floating_ips(count)
return mock.Mock(side_effect=floating_ips)
class FakeNetworkMeter:
"""Fake network meter"""
@staticmethod
def create_one_meter(attrs=None):
"""Create metering pool"""
attrs = attrs or {}
meter_attrs = {
'id': 'meter-id-' + uuid.uuid4().hex,
'name': 'meter-name-' + uuid.uuid4().hex,
'description': 'meter-description-' + uuid.uuid4().hex,
'project_id': 'project-id-' + uuid.uuid4().hex,
'shared': False,
'location': 'MUNCHMUNCHMUNCH',
}
meter_attrs.update(attrs)
meter = fakes.FakeResource(
info=copy.deepcopy(meter_attrs), loaded=True
)
return meter
@staticmethod
def create_meter(attrs=None, count=2):
"""Create multiple meters"""
meters = []
for i in range(0, count):
meters.append(FakeNetworkMeter.create_one_meter(attrs))
return meters
@staticmethod
def get_meter(meter=None, count=2):
"""Get a list of meters"""
if meter is None:
meter = FakeNetworkMeter.create_meter(count)
return mock.Mock(side_effect=meter)
class FakeNetworkMeterRule:
"""Fake metering rule"""
@staticmethod
def create_one_rule(attrs=None):
"""Create one meter rule"""
attrs = attrs or {}
meter_rule_attrs = {
'id': 'meter-label-rule-id-' + uuid.uuid4().hex,
'direction': 'ingress',
'excluded': False,
'metering_label_id': 'meter-label-id-' + uuid.uuid4().hex,
'remote_ip_prefix': '10.0.0.0/24',
'source_ip_prefix': '8.8.8.8/32',
'destination_ip_prefix': '10.0.0.0/24',
'project_id': 'project-id-' + uuid.uuid4().hex,
'location': 'MUNCHMUNCHMUNCH',
}
meter_rule_attrs.update(attrs)
meter_rule = fakes.FakeResource(
info=copy.deepcopy(meter_rule_attrs), loaded=True
)
return meter_rule
@staticmethod
def create_meter_rule(attrs=None, count=2):
"""Create multiple meter rules"""
meter_rules = []
for i in range(0, count):
meter_rules.append(FakeNetworkMeterRule.create_one_rule(attrs))
return meter_rules
@staticmethod
def get_meter_rule(meter_rule=None, count=2):
"""Get a list of meter rules"""
if meter_rule is None:
meter_rule = FakeNetworkMeterRule.create_meter_rule(count)
return mock.Mock(side_effect=meter_rule)
class FakeSubnetPool:
"""Fake one or more subnet pools."""
@staticmethod
def create_one_subnet_pool(attrs=None):
"""Create a fake subnet pool.
:param Dictionary attrs:
A dictionary with all attributes
:return:
A FakeResource object faking the subnet pool
"""
attrs = attrs or {}
# Set default attributes.
subnet_pool_attrs = {
'id': 'subnet-pool-id-' + uuid.uuid4().hex,
'name': 'subnet-pool-name-' + uuid.uuid4().hex,
'prefixes': ['10.0.0.0/24', '10.1.0.0/24'],
'default_prefixlen': '8',
'address_scope_id': 'address-scope-id-' + uuid.uuid4().hex,
'project_id': 'project-id-' + uuid.uuid4().hex,
'is_default': False,
'shared': False,
'max_prefixlen': '32',
'min_prefixlen': '8',
'default_quota': None,
'ip_version': '4',
'description': 'subnet-pool-description-' + uuid.uuid4().hex,
'tags': [],
'location': 'MUNCHMUNCHMUNCH',
}
# Overwrite default attributes.
subnet_pool_attrs.update(attrs)
subnet_pool = fakes.FakeResource(
info=copy.deepcopy(subnet_pool_attrs), loaded=True
)
# Set attributes with special mapping in OpenStack SDK.
subnet_pool.default_prefix_length = subnet_pool_attrs[
'default_prefixlen'
]
subnet_pool.is_shared = subnet_pool_attrs['shared']
subnet_pool.maximum_prefix_length = subnet_pool_attrs['max_prefixlen']
subnet_pool.minimum_prefix_length = subnet_pool_attrs['min_prefixlen']
return subnet_pool
@staticmethod
def create_subnet_pools(attrs=None, count=2):
"""Create multiple fake subnet pools.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of subnet pools to fake
:return:
A list of FakeResource objects faking the subnet pools
"""
subnet_pools = []
for i in range(0, count):
subnet_pools.append(FakeSubnetPool.create_one_subnet_pool(attrs))
return subnet_pools
@staticmethod
def get_subnet_pools(subnet_pools=None, count=2):
"""Get an iterable Mock object with a list of faked subnet pools.
If subnet_pools list is provided, then initialize the Mock object
with the list. Otherwise create one.
:param List subnet_pools:
A list of FakeResource objects faking subnet pools
:param int count:
The number of subnet pools to fake
:return:
An iterable Mock object with side_effect set to a list of faked
subnet pools
"""
if subnet_pools is None:
subnet_pools = FakeSubnetPool.create_subnet_pools(count)
return mock.Mock(side_effect=subnet_pools)
class FakeNetworkServiceProvider:
"""Fake Network Service Providers"""
@staticmethod
def create_one_network_service_provider(attrs=None):
"""Create service provider"""
attrs = attrs or {}
service_provider = {
'name': 'provider-name-' + uuid.uuid4().hex,
'service_type': 'service-type-' + uuid.uuid4().hex,
'default': False,
'location': 'MUNCHMUNCHMUNCH',
}
service_provider.update(attrs)
provider = fakes.FakeResource(
info=copy.deepcopy(service_provider), loaded=True
)
provider.is_default = service_provider['default']
return provider
@staticmethod
def create_network_service_providers(attrs=None, count=2):
"""Create multiple service providers"""
service_providers = []
for i in range(0, count):
service_providers.append(
FakeNetworkServiceProvider.create_one_network_service_provider(
attrs
)
)
return service_providers
class FakeFloatingIPPortForwarding:
"""Fake one or more Port forwarding"""
@staticmethod
def create_one_port_forwarding(attrs=None, use_range=False):
"""Create a fake Port Forwarding.
:param Dictionary attrs:
A dictionary with all attributes
:param Boolean use_range:
A boolean which defines if we will use ranges or not
:return:
A FakeResource object with name, id, etc.
"""
attrs = attrs or {}
floatingip_id = (
attrs.get('floatingip_id') or 'floating-ip-id-' + uuid.uuid4().hex
)
# Set default attributes.
port_forwarding_attrs = {
'id': uuid.uuid4().hex,
'floatingip_id': floatingip_id,
'internal_port_id': 'internal-port-id-' + uuid.uuid4().hex,
'internal_ip_address': '192.168.1.2',
'protocol': 'tcp',
'description': 'some description',
'location': 'MUNCHMUNCHMUNCH',
}
if use_range:
port_range = randint(0, 100)
internal_start = randint(1, 65535 - port_range)
internal_end = internal_start + port_range
internal_range = ':'.join(map(str, [internal_start, internal_end]))
external_start = randint(1, 65535 - port_range)
external_end = external_start + port_range
external_range = ':'.join(map(str, [external_start, external_end]))
port_forwarding_attrs['internal_port_range'] = internal_range
port_forwarding_attrs['external_port_range'] = external_range
port_forwarding_attrs['internal_port'] = None
port_forwarding_attrs['external_port'] = None
else:
port_forwarding_attrs['internal_port'] = randint(1, 65535)
port_forwarding_attrs['external_port'] = randint(1, 65535)
port_forwarding_attrs['internal_port_range'] = ''
port_forwarding_attrs['external_port_range'] = ''
# Overwrite default attributes.
port_forwarding_attrs.update(attrs)
port_forwarding = fakes.FakeResource(
info=copy.deepcopy(port_forwarding_attrs), loaded=True
)
return port_forwarding
@staticmethod
def create_port_forwardings(attrs=None, count=2, use_range=False):
"""Create multiple fake Port Forwarding.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of Port Forwarding rule to fake
:param Boolean use_range:
A boolean which defines if we will use ranges or not
:return:
A list of FakeResource objects faking the Port Forwardings
"""
port_forwardings = []
for i in range(0, count):
port_forwardings.append(
FakeFloatingIPPortForwarding.create_one_port_forwarding(
attrs, use_range=use_range
)
)
return port_forwardings
@staticmethod
def get_port_forwardings(port_forwardings=None, count=2, use_range=False):
"""Get a list of faked Port Forwardings.
If port forwardings list is provided, then initialize the Mock object
with the list. Otherwise create one.
:param List port forwardings:
A list of FakeResource objects faking port forwardings
:param int count:
The number of Port Forwardings to fake
:param Boolean use_range:
A boolean which defines if we will use ranges or not
:return:
An iterable Mock object with side_effect set to a list of faked
Port Forwardings
"""
if port_forwardings is None:
port_forwardings = (
FakeFloatingIPPortForwarding.create_port_forwardings(
count, use_range=use_range
)
)
return mock.Mock(side_effect=port_forwardings)
class FakeL3ConntrackHelper:
"""Fake one or more L3 conntrack helper"""
@staticmethod
def create_one_l3_conntrack_helper(attrs=None):
"""Create a fake L3 conntrack helper.
:param Dictionary attrs:
A dictionary with all attributes
:return:
A FakeResource object with protocol, port, etc.
"""
attrs = attrs or {}
router_id = attrs.get('router_id') or 'router-id-' + uuid.uuid4().hex
# Set default attributes.
ct_attrs = {
'id': uuid.uuid4().hex,
'router_id': router_id,
'helper': 'tftp',
'protocol': 'tcp',
'port': randint(1, 65535),
'location': 'MUNCHMUNCHMUNCH',
}
# Overwrite default attributes.
ct_attrs.update(attrs)
ct = fakes.FakeResource(info=copy.deepcopy(ct_attrs), loaded=True)
return ct
@staticmethod
def create_l3_conntrack_helpers(attrs=None, count=2):
"""Create multiple fake L3 Conntrack helpers.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of L3 Conntrack helper rule to fake
:return:
A list of FakeResource objects faking the Conntrack helpers
"""
ct_helpers = []
for i in range(0, count):
ct_helpers.append(
FakeL3ConntrackHelper.create_one_l3_conntrack_helper(attrs)
)
return ct_helpers
@staticmethod
def get_l3_conntrack_helpers(ct_helpers=None, count=2):
"""Get a list of faked L3 Conntrack helpers.
If ct_helpers list is provided, then initialize the Mock object
with the list. Otherwise create one.
:param List ct_helpers:
A list of FakeResource objects faking conntrack helpers
:param int count:
The number of L3 conntrack helpers to fake
:return:
An iterable Mock object with side_effect set to a list of faked
L3 conntrack helpers
"""
if ct_helpers is None:
ct_helpers = FakeL3ConntrackHelper.create_l3_conntrack_helpers(
count
)
return mock.Mock(side_effect=ct_helpers)
def create_one_address_group(attrs=None):
"""Create a fake address group.
:param Dictionary attrs:
A dictionary with all attributes
:return:
An AddressGroup object with name, id, etc.
"""
attrs = attrs or {}
# Set default attributes.
address_group_attrs = {
'name': 'address-group-name-' + uuid.uuid4().hex,
'description': 'address-group-description-' + uuid.uuid4().hex,
'id': 'address-group-id-' + uuid.uuid4().hex,
'project_id': 'project-id-' + uuid.uuid4().hex,
'addresses': ['10.0.0.1/32'],
'location': 'MUNCHMUNCHMUNCH',
}
# Overwrite default attributes.
address_group_attrs.update(attrs)
address_group = _address_group.AddressGroup(**address_group_attrs)
return address_group
def create_address_groups(attrs=None, count=2):
"""Create multiple fake address groups.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of address groups to fake
:return:
A list of AddressGroup objects faking the address groups
"""
address_groups = []
for i in range(0, count):
address_groups.append(create_one_address_group(attrs))
return address_groups
def get_address_groups(address_groups=None, count=2):
"""Get an iterable Mock object with a list of faked address groups.
If address groups list is provided, then initialize the Mock object
with the list. Otherwise create one.
:param List address_groups:
A list of FakeResource objects faking address groups
:param int count:
The number of address groups to fake
:return:
An iterable Mock object with side_effect set to a list of faked
AddressGroup objects
"""
if address_groups is None:
address_groups = create_address_groups(count)
return mock.Mock(side_effect=address_groups)
def create_one_address_scope(attrs=None):
"""Create a fake address scope.
:param Dictionary attrs:
A dictionary with all attributes
:return:
An AddressScope object with name, id, etc.
"""
attrs = attrs or {}
# Set default attributes.
address_scope_attrs = {
'name': 'address-scope-name-' + uuid.uuid4().hex,
'id': 'address-scope-id-' + uuid.uuid4().hex,
'project_id': 'project-id-' + uuid.uuid4().hex,
'shared': False,
'ip_version': 4,
'location': 'MUNCHMUNCHMUNCH',
}
# Overwrite default attributes.
address_scope_attrs.update(attrs)
address_scope = _address_scope.AddressScope(**address_scope_attrs)
# Set attributes with special mapping in OpenStack SDK.
address_scope.is_shared = address_scope_attrs['shared']
return address_scope
def create_address_scopes(attrs=None, count=2):
"""Create multiple fake address scopes.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of address scopes to fake
:return:
A list of AddressScope objects faking the address scopes
"""
address_scopes = []
for i in range(0, count):
address_scopes.append(create_one_address_scope(attrs))
return address_scopes
def get_address_scopes(address_scopes=None, count=2):
"""Get an iterable Mock object with a list of faked address scopes.
If address scopes list is provided, then initialize the Mock object
with the list. Otherwise create one.
:param List address_scopes:
A list of FakeResource objects faking address scopes
:param int count:
The number of address scopes to fake
:return:
An iterable Mock object with side_effect set to a list of faked
AddressScope objects
"""
if address_scopes is None:
address_scopes = create_address_scopes(count)
return mock.Mock(side_effect=address_scopes)
def create_one_topology(attrs=None):
attrs = attrs or {}
auto_allocated_topology_attrs = {
'id': 'network-id-' + uuid.uuid4().hex,
'project_id': 'project-id-' + uuid.uuid4().hex,
}
auto_allocated_topology_attrs.update(attrs)
auto_allocated_topology = allocated_topology.AutoAllocatedTopology(
**auto_allocated_topology_attrs
)
return auto_allocated_topology
def create_one_availability_zone(attrs=None):
"""Create a fake AZ.
:param Dictionary attrs:
A dictionary with all attributes
:return:
An AvailabilityZone object with name, state, etc.
"""
attrs = attrs or {}
# Set default attributes.
availability_zone = {
'name': uuid.uuid4().hex,
'state': 'available',
'resource': 'network',
}
# Overwrite default attributes.
availability_zone.update(attrs)
availability_zone = _availability_zone.AvailabilityZone(
**availability_zone
)
return availability_zone
def create_availability_zones(attrs=None, count=2):
"""Create multiple fake AZs.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of AZs to fake
:return:
A list of AvailabilityZone objects faking the AZs
"""
availability_zones = []
for i in range(0, count):
availability_zone = create_one_availability_zone(attrs)
availability_zones.append(availability_zone)
return availability_zones
def create_one_ip_availability(attrs=None):
"""Create a fake list with ip availability stats of a network.
:param Dictionary attrs:
A dictionary with all attributes
:return:
A NetworkIPAvailability object with network_name, network_id, etc.
"""
attrs = attrs or {}
# Set default attributes.
network_ip_attrs = {
'network_id': 'network-id-' + uuid.uuid4().hex,
'network_name': 'network-name-' + uuid.uuid4().hex,
'project_id': '',
'subnet_ip_availability': [],
'total_ips': 254,
'used_ips': 6,
'location': 'MUNCHMUNCHMUNCH',
}
network_ip_attrs.update(attrs)
network_ip_availability = _ip_availability.NetworkIPAvailability(
**network_ip_attrs
)
return network_ip_availability
def create_ip_availability(count=2):
"""Create fake list of ip availability stats of multiple networks.
:param int count:
The number of networks to fake
:return:
A list of NetworkIPAvailability objects faking
network ip availability stats
"""
network_ip_availabilities = []
for i in range(0, count):
network_ip_availability = create_one_ip_availability()
network_ip_availabilities.append(network_ip_availability)
return network_ip_availabilities
def create_one_network(attrs=None):
"""Create a fake network.
:param Dictionary attrs:
A dictionary with all attributes
:return:
An Network object, with id, name, etc.
"""
attrs = attrs or {}
# Set default attributes.
network_attrs = {
'created_at': '2021-11-29T10:10:23.000000',
'id': 'network-id-' + uuid.uuid4().hex,
'name': 'network-name-' + uuid.uuid4().hex,
'status': 'ACTIVE',
'description': 'network-description-' + uuid.uuid4().hex,
'dns_domain': 'example.org.',
'mtu': '1350',
'project_id': 'project-id-' + uuid.uuid4().hex,
'admin_state_up': True,
'shared': False,
'subnets': ['a', 'b'],
'segments': 'network-segment-' + uuid.uuid4().hex,
'provider:network_type': 'vlan',
'provider:physical_network': 'physnet1',
'provider:segmentation_id': "400",
'router:external': True,
'availability_zones': [],
'availability_zone_hints': [],
'is_default': False,
'is_vlan_transparent': True,
'is_vlan_qinq': False,
'port_security_enabled': True,
'qos_policy_id': 'qos-policy-id-' + uuid.uuid4().hex,
'ipv4_address_scope': 'ipv4' + uuid.uuid4().hex,
'ipv6_address_scope': 'ipv6' + uuid.uuid4().hex,
'tags': [],
'location': 'MUNCHMUNCHMUNCH',
'updated_at': '2021-11-29T10:10:25.000000',
}
# Overwrite default attributes.
network_attrs.update(attrs)
network = _network.Network(**network_attrs)
return network
def create_networks(attrs=None, count=2):
"""Create multiple fake networks.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of networks to fake
:return:
A list of Network objects faking the networks
"""
networks = []
for i in range(0, count):
networks.append(create_one_network(attrs))
return networks
def get_networks(networks=None, count=2):
"""Get an iterable Mock object with a list of faked networks.
If networks list is provided, then initialize the Mock object with the
list. Otherwise create one.
:param List networks:
A list of Network objects faking networks
:param int count:
The number of networks to fake
:return:
An iterable Mock object with side_effect set to a list of faked
networks
"""
if networks is None:
networks = create_networks(count)
return mock.Mock(side_effect=networks)
def create_one_network_flavor(attrs=None):
"""Create a fake network flavor.
:param Dictionary attrs:
A dictionary with all attributes
:return:
A Flavor object faking the network flavor
"""
attrs = attrs or {}
fake_uuid = uuid.uuid4().hex
network_flavor_attrs = {
'description': 'network-flavor-description-' + fake_uuid,
'is_enabled': True,
'id': 'network-flavor-id-' + fake_uuid,
'name': 'network-flavor-name-' + fake_uuid,
'service_type': 'vpn',
'location': 'MUNCHMUNCHMUNCH',
}
# Overwrite default attributes.
network_flavor_attrs.update(attrs)
network_flavor = _flavor.Flavor(**network_flavor_attrs)
return network_flavor
def create_flavor(attrs=None, count=2):
"""Create multiple fake network flavors.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of network flavors to fake
:return:
A list of Flavor objects faking the network falvors
"""
network_flavors = []
for i in range(0, count):
network_flavors.append(create_one_network_flavor(attrs))
return network_flavors
def get_flavor(network_flavors=None, count=2):
"""Get a list of flavors."""
if network_flavors is None:
network_flavors = create_flavor(count)
return mock.Mock(side_effect=network_flavors)
def create_one_network_segment(attrs=None):
"""Create a fake network segment.
:param Dictionary attrs:
A dictionary with all attributes
:return:
An Segment object faking the network segment
"""
attrs = attrs or {}
# Set default attributes.
fake_uuid = uuid.uuid4().hex
network_segment_attrs = {
'description': 'network-segment-description-' + fake_uuid,
'id': 'network-segment-id-' + fake_uuid,
'name': 'network-segment-name-' + fake_uuid,
'network_id': 'network-id-' + fake_uuid,
'network_type': 'vlan',
'physical_network': 'physical-network-name-' + fake_uuid,
'segmentation_id': 1024,
'location': 'MUNCHMUNCHMUNCH',
}
# Overwrite default attributes.
network_segment_attrs.update(attrs)
network_segment = _segment.Segment(**network_segment_attrs)
return network_segment
def create_network_segments(attrs=None, count=2):
"""Create multiple fake network segments.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of network segments to fake
:return:
A list of Segment objects faking the network segments
"""
network_segments = []
for i in range(0, count):
network_segments.append(create_one_network_segment(attrs))
return network_segments
def create_one_network_segment_range(attrs=None):
"""Create a fake network segment range.
:param Dictionary attrs:
A dictionary with all attributes
:return:
A NetworkSegmentRange object faking the network segment range
"""
attrs = attrs or {}
# Set default attributes.
fake_uuid = uuid.uuid4().hex
network_segment_range_attrs = {
'id': 'network-segment-range-id-' + fake_uuid,
'name': 'network-segment-name-' + fake_uuid,
'default': False,
'shared': False,
'project_id': 'project-id-' + fake_uuid,
'network_type': 'vlan',
'physical_network': 'physical-network-name-' + fake_uuid,
'minimum': 100,
'maximum': 106,
'used': {
104: '3312e4ba67864b2eb53f3f41432f8efc',
106: '3312e4ba67864b2eb53f3f41432f8efc',
},
'available': [100, 101, 102, 103, 105],
'location': 'MUNCHMUNCHMUNCH',
}
# Overwrite default attributes.
network_segment_range_attrs.update(attrs)
network_segment_range = _segment_range.NetworkSegmentRange(
**network_segment_range_attrs
)
return network_segment_range
def create_network_segment_ranges(attrs=None, count=2):
"""Create multiple fake network segment ranges.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of network segment ranges to fake
:return:
A list of NetworkSegmentRange objects faking
the network segment ranges
"""
network_segment_ranges = []
for i in range(0, count):
network_segment_ranges.append(create_one_network_segment_range(attrs))
return network_segment_ranges
def create_one_port(attrs=None):
"""Create a fake port.
:param Dictionary attrs:
A dictionary with all attributes
:return:
A Port object, with id, name, etc.
"""
attrs = attrs or {}
# Set default attributes.
port_attrs = {
'is_admin_state_up': True,
'allowed_address_pairs': [{}],
'binding:host_id': 'binding-host-id-' + uuid.uuid4().hex,
'binding:profile': {},
'binding:vif_details': {},
'binding:vif_type': 'ovs',
'binding:vnic_type': 'normal',
'data_plane_status': None,
'description': 'description-' + uuid.uuid4().hex,
'device_id': 'device-id-' + uuid.uuid4().hex,
'device_owner': 'compute:nova',
'device_profile': 'cyborg_device_profile_1',
'dns_assignment': [{}],
'dns_domain': 'dns-domain-' + uuid.uuid4().hex,
'dns_name': 'dns-name-' + uuid.uuid4().hex,
'extra_dhcp_opts': [{}],
'fixed_ips': [
{
'ip_address': '10.0.0.3',
'subnet_id': 'subnet-id-' + uuid.uuid4().hex,
}
],
'hardware_offload_type': None,
'hints': {},
'id': 'port-id-' + uuid.uuid4().hex,
'mac_address': 'fa:16:3e:a9:4e:72',
'name': 'port-name-' + uuid.uuid4().hex,
'network_id': 'network-id-' + uuid.uuid4().hex,
'numa_affinity_policy': 'required',
'is_port_security_enabled': True,
'security_group_ids': [],
'status': 'ACTIVE',
'project_id': 'project-id-' + uuid.uuid4().hex,
'qos_network_policy_id': 'qos-policy-id-' + uuid.uuid4().hex,
'qos_policy_id': 'qos-policy-id-' + uuid.uuid4().hex,
'tags': [],
'trusted': None,
'propagate_uplink_status': False,
'location': 'MUNCHMUNCHMUNCH',
'trunk_details': {},
}
# Overwrite default attributes.
port_attrs.update(attrs)
port = _port.Port(**port_attrs)
return port
def create_ports(attrs=None, count=2):
"""Create multiple fake ports.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of ports to fake
:return:
A list of Port objects faking the ports
"""
ports = []
for i in range(0, count):
ports.append(create_one_port(attrs))
return ports
def get_ports(ports=None, count=2):
"""Get an iterable Mock object with a list of faked ports.
If ports list is provided, then initialize the Mock object with the
list. Otherwise create one.
:param List ports:
A list of Port objects faking ports
:param int count:
The number of ports to fake
:return:
An iterable Mock object with side_effect set to a list of faked
ports
"""
if ports is None:
ports = create_ports(count)
return mock.Mock(side_effect=ports)
def create_one_network_agent(attrs=None):
"""Create a fake network agent
:param Dictionary attrs:
A dictionary with all attributes
:return:
An Agent object, with id, agent_type, and so on.
"""
attrs = attrs or {}
# Set default attributes
agent_attrs = {
'id': 'agent-id-' + uuid.uuid4().hex,
'agent_type': 'agent-type-' + uuid.uuid4().hex,
'host': 'host-' + uuid.uuid4().hex,
'availability_zone': 'zone-' + uuid.uuid4().hex,
'alive': True,
'admin_state_up': True,
'binary': 'binary-' + uuid.uuid4().hex,
'configurations': {'subnet': 2, 'networks': 1},
'location': 'MUNCHMUNCHMUNCH',
}
agent_attrs.update(attrs)
agent = network_agent.Agent(**agent_attrs)
return agent
def create_network_agents(attrs=None, count=2):
"""Create multiple fake network agents.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of network agents to fake
:return:
A list of Agent objects faking the network agents
"""
agents = []
for i in range(0, count):
agents.append(create_one_network_agent(attrs))
return agents
def get_network_agents(agents=None, count=2):
"""Get an iterable Mock object with a list of faked network agents.
If network agents list is provided, then initialize the Mock object
with the list. Otherwise create one.
:param List agents:
A list of Agent objects faking network agents
:param int count:
The number of network agents to fake
:return:
An iterable Mock object with side_effect set to a list of faked
network agents
"""
if agents is None:
agents = create_network_agents(count)
return mock.Mock(side_effect=agents)
def create_one_network_rbac(attrs=None):
"""Create a fake network rbac
:param Dictionary attrs:
A dictionary with all attributes
:return:
A RBACPolicy object, with id, action, target_tenant,
project_id, type
"""
attrs = attrs or {}
# Set default attributes
rbac_attrs = {
'id': 'rbac-id-' + uuid.uuid4().hex,
'object_type': 'network',
'object_id': 'object-id-' + uuid.uuid4().hex,
'action': 'access_as_shared',
'target_tenant': 'target-tenant-' + uuid.uuid4().hex,
'project_id': 'project-id-' + uuid.uuid4().hex,
'location': 'MUNCHMUNCHMUNCH',
}
rbac_attrs.update(attrs)
rbac = network_rbac.RBACPolicy(**rbac_attrs)
return rbac
def create_network_rbacs(attrs=None, count=2):
"""Create multiple fake network rbac policies.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of rbac policies to fake
:return:
A list of RBACPolicy objects faking the rbac policies
"""
rbac_policies = []
for i in range(0, count):
rbac_policies.append(create_one_network_rbac(attrs))
return rbac_policies
def get_network_rbacs(rbac_policies=None, count=2):
"""Get an iterable Mock object with a list of faked rbac policies.
If rbac policies list is provided, then initialize the Mock object
with the list. Otherwise create one.
:param List rbac_policies:
A list of RBACPolicy objects faking rbac policies
:param int count:
The number of rbac policies to fake
:return:
An iterable Mock object with side_effect set to a list of faked
rbac policies
"""
if rbac_policies is None:
rbac_policies = create_network_rbacs(count)
return mock.Mock(side_effect=rbac_policies)
def create_one_security_group(attrs=None):
"""Create a security group."""
attrs = attrs or {}
security_group_attrs = {
'name': 'security-group-name-' + uuid.uuid4().hex,
'id': 'security-group-id-' + uuid.uuid4().hex,
'project_id': 'project-id-' + uuid.uuid4().hex,
'description': 'security-group-description-' + uuid.uuid4().hex,
'location': 'MUNCHMUNCHMUNCH',
}
security_group_attrs.update(attrs)
security_group = _security_group.SecurityGroup(**security_group_attrs)
return security_group
def create_security_groups(attrs=None, count=2):
"""Create multiple fake security groups.
:param dict attrs: A dictionary with all attributes
:param int count: The number of security groups to fake
:return: A list of fake SecurityGroup objects
"""
security_groups = []
for i in range(0, count):
security_groups.append(create_one_security_group(attrs))
return security_groups
def create_one_service_profile(attrs=None):
"""Create service profile."""
attrs = attrs or {}
service_profile_attrs = {
'id': 'flavor-profile-id' + uuid.uuid4().hex,
'description': 'flavor-profile-description-' + uuid.uuid4().hex,
'project_id': 'project-id-' + uuid.uuid4().hex,
'driver': 'driver-' + uuid.uuid4().hex,
'metainfo': 'metainfo-' + uuid.uuid4().hex,
'enabled': True,
'location': 'MUNCHMUNCHMUNCH',
}
service_profile_attrs.update(attrs)
flavor_profile = _service_profile.ServiceProfile(**service_profile_attrs)
return flavor_profile
def create_service_profile(attrs=None, count=2):
"""Create multiple service profiles."""
service_profiles = []
for i in range(0, count):
service_profiles.append(create_one_service_profile(attrs))
return service_profiles
def get_service_profile(flavor_profile=None, count=2):
"""Get a list of flavor profiles."""
if flavor_profile is None:
flavor_profile = create_service_profile(count)
return mock.Mock(side_effect=flavor_profile)
def create_one_local_ip(attrs=None):
"""Create a fake local ip.
:param Dictionary attrs:
A dictionary with all attributes
:return:
A FakeResource object with name, id, etc.
"""
attrs = attrs or {}
# Set default attributes.
local_ip_attrs = {
'created_at': '2021-11-29T10:10:23.000000',
'name': 'local-ip-name-' + uuid.uuid4().hex,
'description': 'local-ip-description-' + uuid.uuid4().hex,
'id': 'local-ip-id-' + uuid.uuid4().hex,
'project_id': 'project-id-' + uuid.uuid4().hex,
'local_port_id': 'local_port_id-' + uuid.uuid4().hex,
'network_id': 'network_id-' + uuid.uuid4().hex,
'local_ip_address': '10.0.0.1',
'ip_mode': 'translate',
'revision_number': 'local-ip-revision-number-' + uuid.uuid4().hex,
'updated_at': '2021-11-29T10:10:25.000000',
}
# Overwrite default attributes.
local_ip_attrs.update(attrs)
local_ip = _local_ip.LocalIP(**local_ip_attrs)
return local_ip
def create_local_ips(attrs=None, count=2):
"""Create multiple fake local ips.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of local ips to fake
:return:
A list of FakeResource objects faking the local ips
"""
local_ips = []
for i in range(0, count):
local_ips.append(create_one_local_ip(attrs))
return local_ips
def get_local_ips(local_ips=None, count=2):
"""Get an iterable Mock object with a list of faked local ips.
If local ip list is provided, then initialize the Mock object
with the list. Otherwise create one.
:param List local_ips:
A list of FakeResource objects faking local ips
:param int count:
The number of local ips to fake
:return:
An iterable Mock object with side_effect set to a list of faked
local ips
"""
if local_ips is None:
local_ips = create_local_ips(count)
return mock.Mock(side_effect=local_ips)
def create_one_local_ip_association(attrs=None):
"""Create a fake local ip association.
:param Dictionary attrs:
A dictionary with all attributes
:return:
A FakeResource object with local_ip_id, local_ip_address, etc.
"""
attrs = attrs or {}
# Set default attributes.
local_ip_association_attrs = {
'local_ip_id': 'local-ip-id-' + uuid.uuid4().hex,
'local_ip_address': '172.24.4.228',
'fixed_port_id': 'fixed-port-id-' + uuid.uuid4().hex,
'fixed_ip': '10.0.0.5',
'host': 'host-' + uuid.uuid4().hex,
}
# Overwrite default attributes.
local_ip_association_attrs.update(attrs)
local_ip_association = _local_ip_association.LocalIPAssociation(
**local_ip_association_attrs
)
return local_ip_association
def create_local_ip_associations(attrs=None, count=2):
"""Create multiple fake local ip associations.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of local ip associations to fake
:return:
A list of FakeResource objects faking the local ip associations
"""
local_ip_associations = []
for i in range(0, count):
local_ip_associations.append(create_one_local_ip_association(attrs))
return local_ip_associations
def get_local_ip_associations(local_ip_associations=None, count=2):
"""Get a list of faked local ip associations
If local ip association list is provided, then initialize
the Mock object with the list. Otherwise create one.
:param List local_ip_associations:
A list of FakeResource objects faking local ip associations
:param int count:
The number of local ip associations to fake
:return:
An iterable Mock object with side_effect set to a list of faked
local ip associations
"""
if local_ip_associations is None:
local_ip_associations = create_local_ip_associations(count)
return mock.Mock(side_effect=local_ip_associations)
def create_one_ndp_proxy(attrs=None):
"""Create a fake NDP proxy.
:param Dictionary attrs:
A dictionary with all attributes
:return:
A FakeResource object with router_id, port_id, etc.
"""
attrs = attrs or {}
router_id = attrs.get('router_id') or 'router-id-' + uuid.uuid4().hex
port_id = attrs.get('port_id') or 'port-id-' + uuid.uuid4().hex
# Set default attributes.
np_attrs = {
'id': uuid.uuid4().hex,
'name': 'ndp-proxy-name-' + uuid.uuid4().hex,
'router_id': router_id,
'port_id': port_id,
'ip_address': '2001::1:2',
'description': 'ndp-proxy-description-' + uuid.uuid4().hex,
'project_id': 'project-id-' + uuid.uuid4().hex,
'location': 'MUNCHMUNCHMUNCH',
}
# Overwrite default attributes.
np_attrs.update(attrs)
return _ndp_proxy.NDPProxy(**np_attrs)
def create_ndp_proxies(attrs=None, count=2):
"""Create multiple fake NDP proxies.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of NDP proxxy to fake
:return:
A list of FakeResource objects faking the NDP proxies
"""
ndp_proxies = []
for i in range(0, count):
ndp_proxies.append(create_one_ndp_proxy(attrs))
return ndp_proxies
def get_ndp_proxies(ndp_proxies=None, count=2):
"""Get a list of faked NDP proxies.
If ndp_proxy list is provided, then initialize the Mock object
with the list. Otherwise create one.
:param List ndp_proxies:
A list of FakeResource objects faking ndp proxy
:param int count:
The number of ndp proxy to fake
:return:
An iterable Mock object with side_effect set to a list of faked
ndp proxy
"""
if ndp_proxies is None:
ndp_proxies = create_ndp_proxies(count)
return mock.Mock(side_effect=ndp_proxies)
def create_one_trunk(attrs=None):
"""Create a fake trunk.
:param Dictionary attrs:
A dictionary with all attributes
:return:
A FakeResource object with name, id, etc.
"""
attrs = attrs or {}
# Set default attributes.
trunk_attrs = {
'id': 'trunk-id-' + uuid.uuid4().hex,
'name': 'trunk-name-' + uuid.uuid4().hex,
'description': '',
'port_id': 'port-' + uuid.uuid4().hex,
'admin_state_up': True,
'project_id': 'project-id-' + uuid.uuid4().hex,
'status': 'ACTIVE',
'sub_ports': [
{
'port_id': 'subport-' + uuid.uuid4().hex,
'segmentation_type': 'vlan',
'segmentation_id': 100,
}
],
}
# Overwrite default attributes.
trunk_attrs.update(attrs)
trunk = _trunk.Trunk(**trunk_attrs)
return trunk
def create_trunks(attrs=None, count=2):
"""Create multiple fake trunks.
:param Dictionary attrs:
A dictionary with all attributes
:param int count:
The number of trunks to fake
:return:
A list of FakeResource objects faking the trunks
"""
trunks = []
for i in range(0, count):
trunks.append(create_one_trunk(attrs))
return trunks
def get_trunks(trunks=None, count=2):
"""Get an iterable Mock object with a list of faked trunks.
If trunk list is provided, then initialize the Mock object
with the list. Otherwise create one.
:param List trunks:
A list of FakeResource objects faking trunks
:param int count:
The number of trunks to fake
:return:
An iterable Mock object with side_effect set to a list of faked
trunks
"""
if trunks is None:
trunks = create_trunks(count)
return mock.Mock(side_effect=trunks)
|