/usr/lib/python2.7/dist-packages/oslo_versionedobjects/tests/test_objects.py is in python-oslo.versionedobjects 1.17.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 | # Copyright 2013 IBM Corp.
#
# 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
import datetime
import logging
import pytz
import six
import mock
from oslo_context import context
from oslo_serialization import jsonutils
from oslo_utils import timeutils
from testtools import matchers
from oslo_versionedobjects import base
from oslo_versionedobjects import exception
from oslo_versionedobjects import fields
from oslo_versionedobjects import fixture
from oslo_versionedobjects import test
LOG = logging.getLogger(__name__)
def is_test_object(cls):
"""Return True if class is defined in the tests.
:param cls: Class to inspect
"""
return 'oslo_versionedobjects.tests' in cls.__module__
@base.VersionedObjectRegistry.register
class MyOwnedObject(base.VersionedObject):
VERSION = '1.0'
fields = {'baz': fields.Field(fields.Integer())}
@base.VersionedObjectRegistry.register
class MyObj(base.VersionedObject, base.VersionedObjectDictCompat):
VERSION = '1.6'
fields = {'foo': fields.Field(fields.Integer(), default=1),
'bar': fields.Field(fields.String()),
'missing': fields.Field(fields.String()),
'readonly': fields.Field(fields.Integer(), read_only=True),
'rel_object': fields.ObjectField('MyOwnedObject', nullable=True),
'rel_objects': fields.ListOfObjectsField('MyOwnedObject',
nullable=True),
'mutable_default': fields.ListOfStringsField(default=[]),
'timestamp': fields.DateTimeField(nullable=True),
}
@staticmethod
def _from_db_object(context, obj, db_obj):
self = MyObj()
self.foo = db_obj['foo']
self.bar = db_obj['bar']
self.missing = db_obj['missing']
self.readonly = 1
return self
def obj_load_attr(self, attrname):
setattr(self, attrname, 'loaded!')
@base.remotable_classmethod
def query(cls, context):
obj = cls(context=context, foo=1, bar='bar')
obj.obj_reset_changes()
return obj
@base.remotable
def marco(self):
return 'polo'
@base.remotable
def _update_test(self):
project_id = getattr(context, 'tenant', None)
if project_id is None:
project_id = getattr(context, 'project_id', None)
if project_id == 'alternate':
self.bar = 'alternate-context'
else:
self.bar = 'updated'
@base.remotable
def save(self):
self.obj_reset_changes()
@base.remotable
def refresh(self):
self.foo = 321
self.bar = 'refreshed'
self.obj_reset_changes()
@base.remotable
def modify_save_modify(self):
self.bar = 'meow'
self.save()
self.foo = 42
self.rel_object = MyOwnedObject(baz=42)
def obj_make_compatible(self, primitive, target_version):
super(MyObj, self).obj_make_compatible(primitive, target_version)
# NOTE(danms): Simulate an older version that had a different
# format for the 'bar' attribute
if target_version == '1.1' and 'bar' in primitive:
primitive['bar'] = 'old%s' % primitive['bar']
@base.VersionedObjectRegistry.register
class MyComparableObj(MyObj, base.ComparableVersionedObject):
pass
@base.VersionedObjectRegistry.register
class MyObjDiffVers(MyObj):
VERSION = '1.5'
@classmethod
def obj_name(cls):
return 'MyObj'
@base.VersionedObjectRegistry.register_if(False)
class MyObj2(base.VersionedObject):
@classmethod
def obj_name(cls):
return 'MyObj'
@base.remotable_classmethod
def query(cls, *args, **kwargs):
pass
@base.VersionedObjectRegistry.register_if(False)
class MySensitiveObj(base.VersionedObject):
VERSION = '1.0'
fields = {
'data': fields.SensitiveStringField(nullable=True)
}
@base.remotable_classmethod
def query(cls, *args, **kwargs):
pass
class RandomMixInWithNoFields(object):
"""Used to test object inheritance using a mixin that has no fields."""
pass
@base.VersionedObjectRegistry.register
class TestSubclassedObject(RandomMixInWithNoFields, MyObj):
fields = {'new_field': fields.Field(fields.String())}
child_versions = {
'1.0': '1.0',
'1.1': '1.1',
'1.2': '1.1',
'1.3': '1.2',
'1.4': '1.3',
'1.5': '1.4',
'1.6': '1.5',
'1.7': '1.6',
}
@base.VersionedObjectRegistry.register
class MyCompoundObject(base.VersionedObject):
fields = {
"foo": fields.Field(fields.List(fields.Integer())),
"bar": fields.Field(fields.Dict(fields.Integer())),
"baz": fields.Field(fields.Set(fields.Integer()))
}
class TestRegistry(test.TestCase):
def test_obj_tracking(self):
@base.VersionedObjectRegistry.register
class NewBaseClass(object):
VERSION = '1.0'
fields = {}
@classmethod
def obj_name(cls):
return cls.__name__
@base.VersionedObjectRegistry.register
class Fake1TestObj1(NewBaseClass):
@classmethod
def obj_name(cls):
return 'fake1'
@base.VersionedObjectRegistry.register
class Fake1TestObj2(Fake1TestObj1):
pass
@base.VersionedObjectRegistry.register
class Fake1TestObj3(Fake1TestObj1):
VERSION = '1.1'
@base.VersionedObjectRegistry.register
class Fake2TestObj1(NewBaseClass):
@classmethod
def obj_name(cls):
return 'fake2'
@base.VersionedObjectRegistry.register
class Fake1TestObj4(Fake1TestObj3):
VERSION = '1.2'
@base.VersionedObjectRegistry.register
class Fake2TestObj2(Fake2TestObj1):
VERSION = '1.1'
@base.VersionedObjectRegistry.register
class Fake1TestObj5(Fake1TestObj1):
VERSION = '1.1'
@base.VersionedObjectRegistry.register_if(False)
class ConditionalObj1(NewBaseClass):
fields = {'foo': fields.IntegerField()}
@base.VersionedObjectRegistry.register_if(True)
class ConditionalObj2(NewBaseClass):
fields = {'foo': fields.IntegerField()}
# Newest versions first in the list. Duplicate versions take the
# newest object.
expected = {'fake1': [Fake1TestObj4, Fake1TestObj5, Fake1TestObj2],
'fake2': [Fake2TestObj2, Fake2TestObj1]}
self.assertEqual(expected['fake1'],
base.VersionedObjectRegistry.obj_classes()['fake1'])
self.assertEqual(expected['fake2'],
base.VersionedObjectRegistry.obj_classes()['fake2'])
self.assertEqual(
[],
base.VersionedObjectRegistry.obj_classes()['ConditionalObj1'])
self.assertTrue(hasattr(ConditionalObj1, 'foo'))
self.assertEqual(
[ConditionalObj2],
base.VersionedObjectRegistry.obj_classes()['ConditionalObj2'])
self.assertTrue(hasattr(ConditionalObj2, 'foo'))
def test_field_checking(self):
def create_class(field):
@base.VersionedObjectRegistry.register
class TestField(base.VersionedObject):
VERSION = '1.5'
fields = {'foo': field()}
return TestField
create_class(fields.DateTimeField)
self.assertRaises(exception.ObjectFieldInvalid,
create_class, fields.DateTime)
self.assertRaises(exception.ObjectFieldInvalid,
create_class, int)
def test_registration_hook(self):
class TestObject(base.VersionedObject):
VERSION = '1.0'
class TestObjectNewer(base.VersionedObject):
VERSION = '1.1'
@classmethod
def obj_name(cls):
return 'TestObject'
registry = base.VersionedObjectRegistry()
with mock.patch.object(registry, 'registration_hook') as mock_hook:
registry._register_class(TestObject)
mock_hook.assert_called_once_with(TestObject, 0)
with mock.patch.object(registry, 'registration_hook') as mock_hook:
registry._register_class(TestObjectNewer)
mock_hook.assert_called_once_with(TestObjectNewer, 0)
def test_subclassability(self):
class MyRegistryOne(base.VersionedObjectRegistry):
def registration_hook(self, cls, index):
cls.reg_to = "one"
class MyRegistryTwo(base.VersionedObjectRegistry):
def registration_hook(self, cls, index):
cls.reg_to = "two"
@MyRegistryOne.register
class AVersionedObject1(base.VersionedObject):
VERSION = '1.0'
fields = {'baz': fields.Field(fields.Integer())}
@MyRegistryTwo.register
class AVersionedObject2(base.VersionedObject):
VERSION = '1.0'
fields = {'baz': fields.Field(fields.Integer())}
self.assertIn('AVersionedObject1',
MyRegistryOne.obj_classes())
self.assertIn('AVersionedObject2',
MyRegistryOne.obj_classes())
self.assertIn('AVersionedObject1',
MyRegistryTwo.obj_classes())
self.assertIn('AVersionedObject2',
MyRegistryTwo.obj_classes())
self.assertIn('AVersionedObject1',
base.VersionedObjectRegistry.obj_classes())
self.assertIn('AVersionedObject2',
base.VersionedObjectRegistry.obj_classes())
self.assertEqual(AVersionedObject1.reg_to, "one")
self.assertEqual(AVersionedObject2.reg_to, "two")
@mock.patch.object(base.VersionedObjectRegistry, '__new__')
def test_register(self, mock_registry):
mock_reg_obj = mock.Mock()
mock_registry.return_value = mock_reg_obj
mock_reg_obj._register_class = mock.Mock()
class my_class(object):
pass
base.VersionedObjectRegistry.register(my_class)
mock_reg_obj._register_class.assert_called_once_with(my_class)
@mock.patch.object(base.VersionedObjectRegistry, 'register')
def test_register_if(self, mock_register):
class my_class(object):
pass
base.VersionedObjectRegistry.register_if(True)(my_class)
mock_register.assert_called_once_with(my_class)
@mock.patch.object(base, '_make_class_properties')
def test_register_if_false(self, mock_make_props):
class my_class(object):
pass
base.VersionedObjectRegistry.register_if(False)(my_class)
mock_make_props.assert_called_once_with(my_class)
@mock.patch.object(base.VersionedObjectRegistry, 'register_if')
def test_objectify(self, mock_register_if):
mock_reg_callable = mock.Mock()
mock_register_if.return_value = mock_reg_callable
class my_class(object):
pass
base.VersionedObjectRegistry.objectify(my_class)
mock_register_if.assert_called_once_with(False)
mock_reg_callable.assert_called_once_with(my_class)
class TestObjMakeList(test.TestCase):
def test_obj_make_list(self):
@base.VersionedObjectRegistry.register
class MyList(base.ObjectListBase, base.VersionedObject):
fields = {
'objects': fields.ListOfObjectsField('MyObj'),
}
db_objs = [{'foo': 1, 'bar': 'baz', 'missing': 'banana'},
{'foo': 2, 'bar': 'bat', 'missing': 'apple'},
]
mylist = base.obj_make_list('ctxt', MyList(), MyObj, db_objs)
self.assertEqual(2, len(mylist))
self.assertEqual('ctxt', mylist._context)
for index, item in enumerate(mylist):
self.assertEqual(db_objs[index]['foo'], item.foo)
self.assertEqual(db_objs[index]['bar'], item.bar)
self.assertEqual(db_objs[index]['missing'], item.missing)
class TestGetSubobjectVersion(test.TestCase):
def setUp(self):
super(TestGetSubobjectVersion, self).setUp()
self.backport_mock = mock.MagicMock()
self.rels = [('1.1', '1.0'), ('1.3', '1.1')]
def test_get_subobject_version_not_existing(self):
# Verify that exception is raised if we try backporting
# to a version where we did not contain the subobject
self.assertRaises(exception.TargetBeforeSubobjectExistedException,
base._get_subobject_version, '1.0', self.rels,
self.backport_mock)
def test_get_subobject_version_explicit_version(self):
# Verify that we backport to the correct subobject version when the
# version we are going back to is explicitly said in the relationships
base._get_subobject_version('1.3', self.rels, self.backport_mock)
self.backport_mock.assert_called_once_with('1.1')
def test_get_subobject_version_implicit_version(self):
# Verify that we backport to the correct subobject version when the
# version backporting to is not explicitly stated in the relationships
base._get_subobject_version('1.2', self.rels, self.backport_mock)
self.backport_mock.assert_called_once_with('1.0')
class TestDoSubobjectBackport(test.TestCase):
@base.VersionedObjectRegistry.register
class ParentObj(base.VersionedObject):
VERSION = '1.1'
fields = {'child': fields.ObjectField('ChildObj', nullable=True)}
obj_relationships = {'child': [('1.0', '1.0'), ('1.1', '1.1')]}
@base.VersionedObjectRegistry.register
class ParentObjList(base.VersionedObject, base.ObjectListBase):
VERSION = '1.1'
fields = {'objects': fields.ListOfObjectsField('ChildObj')}
obj_relationships = {'objects': [('1.0', '1.0'), ('1.1', '1.1')]}
@base.VersionedObjectRegistry.register
class ChildObj(base.VersionedObject):
VERSION = '1.1'
fields = {'foo': fields.IntegerField()}
def test_do_subobject_backport_without_manifest(self):
child = self.ChildObj(foo=1)
parent = self.ParentObj(child=child)
parent_primitive = parent.obj_to_primitive()['versioned_object.data']
primitive = child.obj_to_primitive()['versioned_object.data']
version = '1.0'
compat_func = 'obj_make_compatible_from_manifest'
with mock.patch.object(child, compat_func) as mock_compat:
base._do_subobject_backport(version, parent, 'child',
parent_primitive)
mock_compat.assert_called_once_with(primitive,
version,
version_manifest=None)
def test_do_subobject_backport_with_manifest(self):
child = self.ChildObj(foo=1)
parent = self.ParentObj(child=child)
parent_primitive = parent.obj_to_primitive()['versioned_object.data']
primitive = child.obj_to_primitive()['versioned_object.data']
version = '1.0'
manifest = {'ChildObj': '1.0'}
parent._obj_version_manifest = manifest
compat_func = 'obj_make_compatible_from_manifest'
with mock.patch.object(child, compat_func) as mock_compat:
base._do_subobject_backport(version, parent, 'child',
parent_primitive)
mock_compat.assert_called_once_with(primitive,
version,
version_manifest=manifest)
def test_do_subobject_backport_with_manifest_old_parent(self):
child = self.ChildObj(foo=1)
parent = self.ParentObj(child=child)
manifest = {'ChildObj': '1.0'}
parent_primitive = parent.obj_to_primitive(target_version='1.1',
version_manifest=manifest)
child_primitive = parent_primitive['versioned_object.data']['child']
self.assertEqual('1.0', child_primitive['versioned_object.version'])
def test_do_subobject_backport_list_object(self):
child = self.ChildObj(foo=1)
parent = self.ParentObjList(objects=[child])
parent_primitive = parent.obj_to_primitive()['versioned_object.data']
primitive = child.obj_to_primitive()['versioned_object.data']
version = '1.0'
compat_func = 'obj_make_compatible_from_manifest'
with mock.patch.object(child, compat_func) as mock_compat:
base._do_subobject_backport(version, parent, 'objects',
parent_primitive)
mock_compat.assert_called_once_with(primitive,
version,
version_manifest=None)
def test_do_subobject_backport_list_object_with_manifest(self):
child = self.ChildObj(foo=1)
parent = self.ParentObjList(objects=[child])
manifest = {'ChildObj': '1.0', 'ParentObjList': '1.0'}
parent_primitive = parent.obj_to_primitive(target_version='1.0',
version_manifest=manifest)
self.assertEqual('1.0', parent_primitive['versioned_object.version'])
child_primitive = parent_primitive['versioned_object.data']['objects']
self.assertEqual('1.0', child_primitive[0]['versioned_object.version'])
def test_do_subobject_backport_null_child(self):
parent = self.ParentObj(child=None)
parent_primitive = parent.obj_to_primitive()['versioned_object.data']
version = '1.0'
compat_func = 'obj_make_compatible_from_manifest'
with mock.patch.object(self.ChildObj, compat_func) as mock_compat:
base._do_subobject_backport(version, parent, 'child',
parent_primitive)
self.assertFalse(mock_compat.called,
"obj_make_compatible_from_manifest() should not "
"have been called because the subobject is "
"None.")
def test_to_primitive_calls_make_compatible_manifest(self):
obj = self.ParentObj()
with mock.patch.object(obj, 'obj_make_compatible_from_manifest') as m:
obj.obj_to_primitive(target_version='1.0',
version_manifest=mock.sentinel.manifest)
m.assert_called_once_with(mock.ANY, '1.0', mock.sentinel.manifest)
class _BaseTestCase(test.TestCase):
def setUp(self):
super(_BaseTestCase, self).setUp()
self.user_id = 'fake-user'
self.project_id = 'fake-project'
self.context = context.RequestContext(self.user_id, self.project_id)
def json_comparator(self, expected, obj_val):
# json-ify an object field for comparison with its db str
# equivalent
self.assertEqual(expected, jsonutils.dumps(obj_val))
def str_comparator(self, expected, obj_val):
"""Compare a field to a string value
Compare an object field to a string in the db by performing
a simple coercion on the object field value.
"""
self.assertEqual(expected, str(obj_val))
def assertNotIsInstance(self, obj, cls, msg=None):
"""Python < v2.7 compatibility. Assert 'not isinstance(obj, cls)."""
try:
f = super(_BaseTestCase, self).assertNotIsInstance
except AttributeError:
self.assertThat(obj,
matchers.Not(matchers.IsInstance(cls)),
message=msg or '')
else:
f(obj, cls, msg=msg)
class TestFixture(_BaseTestCase):
def test_fake_indirection_takes_serializer(self):
ser = mock.MagicMock()
iapi = fixture.FakeIndirectionAPI(ser)
ser.serialize_entity.return_value = mock.sentinel.serial
iapi.object_action(mock.sentinel.context, mock.sentinel.objinst,
mock.sentinel.objmethod, (), {})
ser.serialize_entity.assert_called_once_with(mock.sentinel.context,
mock.sentinel.objinst)
ser.deserialize_entity.assert_called_once_with(mock.sentinel.context,
mock.sentinel.serial)
def test_indirection_fixture_takes_indirection_api(self):
iapi = mock.sentinel.iapi
self.useFixture(fixture.IndirectionFixture(iapi))
self.assertEqual(iapi, base.VersionedObject.indirection_api)
def test_indirection_action(self):
self.useFixture(fixture.IndirectionFixture())
obj = MyObj(context=self.context)
with mock.patch.object(base.VersionedObject.indirection_api,
'object_action') as mock_action:
mock_action.return_value = ({}, 'foo')
obj.marco()
mock_action.assert_called_once_with(self.context,
obj, 'marco',
(), {})
@mock.patch('oslo_versionedobjects.base.obj_tree_get_versions')
def test_indirection_class_action(self, mock_otgv):
mock_otgv.return_value = mock.sentinel.versions
self.useFixture(fixture.IndirectionFixture())
with mock.patch.object(base.VersionedObject.indirection_api,
'object_class_action_versions') as mock_caction:
mock_caction.return_value = 'foo'
MyObj.query(self.context)
mock_caction.assert_called_once_with(self.context,
'MyObj', 'query',
mock.sentinel.versions,
(), {})
def test_fake_indirection_serializes_arguments(self):
ser = mock.MagicMock()
iapi = fixture.FakeIndirectionAPI(serializer=ser)
arg1 = mock.MagicMock()
arg2 = mock.MagicMock()
iapi.object_action(mock.sentinel.context, mock.sentinel.objinst,
mock.sentinel.objmethod, (arg1,), {'foo': arg2})
ser.serialize_entity.assert_any_call(mock.sentinel.context, arg1)
ser.serialize_entity.assert_any_call(mock.sentinel.context, arg2)
def test_get_hashes(self):
checker = fixture.ObjectVersionChecker()
hashes = checker.get_hashes()
# NOTE(danms): If this object's version or hash changes, this needs
# to change. Otherwise, leave it alone.
self.assertEqual('1.6-fb5f5379168bf08f7f2ce0a745e91027',
hashes['TestSubclassedObject'])
def test_test_hashes(self):
checker = fixture.ObjectVersionChecker()
hashes = checker.get_hashes()
actual_hash = hashes['TestSubclassedObject']
hashes['TestSubclassedObject'] = 'foo'
expected, actual = checker.test_hashes(hashes)
self.assertEqual(['TestSubclassedObject'], list(expected.keys()))
self.assertEqual(['TestSubclassedObject'], list(actual.keys()))
self.assertEqual('foo', expected['TestSubclassedObject'])
self.assertEqual(actual_hash, actual['TestSubclassedObject'])
def test_get_dependency_tree(self):
checker = fixture.ObjectVersionChecker()
tree = checker.get_dependency_tree()
# NOTE(danms): If this object's dependencies change, this n eeds
# to change. Otherwise, leave it alone.
self.assertEqual({'MyOwnedObject': '1.0'},
tree['TestSubclassedObject'])
def test_test_relationships(self):
checker = fixture.ObjectVersionChecker()
tree = checker.get_dependency_tree()
actual = tree['TestSubclassedObject']
tree['TestSubclassedObject']['Foo'] = '9.8'
expected, actual = checker.test_relationships(tree)
self.assertEqual(['TestSubclassedObject'], list(expected.keys()))
self.assertEqual(['TestSubclassedObject'], list(actual.keys()))
self.assertEqual({'MyOwnedObject': '1.0',
'Foo': '9.8'},
expected['TestSubclassedObject'])
self.assertEqual({'MyOwnedObject': '1.0'},
actual['TestSubclassedObject'])
def test_test_compatibility(self):
fake_classes = {mock.sentinel.class_one: [mock.sentinel.impl_one_one,
mock.sentinel.impl_one_two],
mock.sentinel.class_two: [mock.sentinel.impl_two_one,
mock.sentinel.impl_two_two],
}
checker = fixture.ObjectVersionChecker(fake_classes)
@mock.patch.object(checker, '_test_object_compatibility')
def test(mock_compat):
checker.test_compatibility_routines()
mock_compat.assert_has_calls(
[mock.call(mock.sentinel.impl_one_one, manifest=None,
init_args=[], init_kwargs={}),
mock.call(mock.sentinel.impl_one_two, manifest=None,
init_args=[], init_kwargs={}),
mock.call(mock.sentinel.impl_two_one, manifest=None,
init_args=[], init_kwargs={}),
mock.call(mock.sentinel.impl_two_two, manifest=None,
init_args=[], init_kwargs={})],
any_order=True)
test()
def test_test_compatibility_checks_obj_to_primitive(self):
fake = mock.MagicMock()
fake.VERSION = '1.3'
checker = fixture.ObjectVersionChecker()
checker._test_object_compatibility(fake)
fake().obj_to_primitive.assert_has_calls(
[mock.call(target_version='1.0'),
mock.call(target_version='1.1'),
mock.call(target_version='1.2'),
mock.call(target_version='1.3')])
def test_test_relationships_in_order(self):
fake_classes = {mock.sentinel.class_one: [mock.sentinel.impl_one_one,
mock.sentinel.impl_one_two],
mock.sentinel.class_two: [mock.sentinel.impl_two_one,
mock.sentinel.impl_two_two],
}
checker = fixture.ObjectVersionChecker(fake_classes)
@mock.patch.object(checker, '_test_relationships_in_order')
def test(mock_compat):
checker.test_relationships_in_order()
mock_compat.assert_has_calls(
[mock.call(mock.sentinel.impl_one_one),
mock.call(mock.sentinel.impl_one_two),
mock.call(mock.sentinel.impl_two_one),
mock.call(mock.sentinel.impl_two_two)],
any_order=True)
test()
def test_test_relationships_in_order_good(self):
fake = mock.MagicMock()
fake.VERSION = '1.5'
fake.fields = {'foo': fields.ObjectField('bar')}
fake.obj_relationships = {'foo': [('1.2', '1.0'),
('1.3', '1.2')]}
checker = fixture.ObjectVersionChecker()
checker._test_relationships_in_order(fake)
def _test_test_relationships_in_order_bad(self, fake_rels):
fake = mock.MagicMock()
fake.VERSION = '1.5'
fake.fields = {'foo': fields.ObjectField('bar')}
fake.obj_relationships = fake_rels
checker = fixture.ObjectVersionChecker()
self.assertRaises(AssertionError,
checker._test_relationships_in_order, fake)
def test_test_relationships_in_order_bad_my_version(self):
self._test_test_relationships_in_order_bad(
{'foo': [('1.4', '1.1'), ('1.3', '1.2')]})
def test_test_relationships_in_order_bad_child_version(self):
self._test_test_relationships_in_order_bad(
{'foo': [('1.2', '1.3'), ('1.3', '1.2')]})
def test_test_relationships_in_order_bad_both_versions(self):
self._test_test_relationships_in_order_bad(
{'foo': [('1.5', '1.4'), ('1.3', '1.2')]})
class _LocalTest(_BaseTestCase):
def setUp(self):
super(_LocalTest, self).setUp()
self.assertIsNone(base.VersionedObject.indirection_api)
class _RemoteTest(_BaseTestCase):
def setUp(self):
super(_RemoteTest, self).setUp()
self.useFixture(fixture.IndirectionFixture())
class _TestObject(object):
# def test_object_attrs_in_init(self):
# # Spot check a few
# objects.Instance
# objects.InstanceInfoCache
# objects.SecurityGroup
# # Now check the test one in this file. Should be newest version
# self.assertEqual('1.6', objects.MyObj.VERSION)
def test_hydration_type_error(self):
primitive = {'versioned_object.name': 'MyObj',
'versioned_object.namespace': 'versionedobjects',
'versioned_object.version': '1.5',
'versioned_object.data': {'foo': 'a'}}
self.assertRaises(ValueError, MyObj.obj_from_primitive, primitive)
def test_hydration(self):
primitive = {'versioned_object.name': 'MyObj',
'versioned_object.namespace': 'versionedobjects',
'versioned_object.version': '1.5',
'versioned_object.data': {'foo': 1}}
real_method = MyObj._obj_from_primitive
def _obj_from_primitive(*args):
return real_method(*args)
with mock.patch.object(MyObj, '_obj_from_primitive') as ofp:
ofp.side_effect = _obj_from_primitive
obj = MyObj.obj_from_primitive(primitive)
ofp.assert_called_once_with(None, '1.5', primitive)
self.assertEqual(obj.foo, 1)
def test_hydration_version_different(self):
primitive = {'versioned_object.name': 'MyObj',
'versioned_object.namespace': 'versionedobjects',
'versioned_object.version': '1.2',
'versioned_object.data': {'foo': 1}}
obj = MyObj.obj_from_primitive(primitive)
self.assertEqual(obj.foo, 1)
self.assertEqual('1.2', obj.VERSION)
def test_hydration_bad_ns(self):
primitive = {'versioned_object.name': 'MyObj',
'versioned_object.namespace': 'foo',
'versioned_object.version': '1.5',
'versioned_object.data': {'foo': 1}}
self.assertRaises(exception.UnsupportedObjectError,
MyObj.obj_from_primitive, primitive)
def test_hydration_additional_unexpected_stuff(self):
primitive = {'versioned_object.name': 'MyObj',
'versioned_object.namespace': 'versionedobjects',
'versioned_object.version': '1.5.1',
'versioned_object.data': {
'foo': 1,
'unexpected_thing': 'foobar'}}
obj = MyObj.obj_from_primitive(primitive)
self.assertEqual(1, obj.foo)
self.assertFalse(hasattr(obj, 'unexpected_thing'))
# NOTE(danms): If we call obj_from_primitive() directly
# with a version containing .z, we'll get that version
# in the resulting object. In reality, when using the
# serializer, we'll get that snipped off (tested
# elsewhere)
self.assertEqual('1.5.1', obj.VERSION)
def test_dehydration(self):
expected = {'versioned_object.name': 'MyObj',
'versioned_object.namespace': 'versionedobjects',
'versioned_object.version': '1.6',
'versioned_object.data': {'foo': 1}}
obj = MyObj(foo=1)
obj.obj_reset_changes()
self.assertEqual(obj.obj_to_primitive(), expected)
def test_dehydration_invalid_version(self):
obj = MyObj(foo=1)
obj.obj_reset_changes()
self.assertRaises(exception.InvalidTargetVersion,
obj.obj_to_primitive,
target_version='1.7')
def test_dehydration_same_version(self):
expected = {'versioned_object.name': 'MyObj',
'versioned_object.namespace': 'versionedobjects',
'versioned_object.version': '1.6',
'versioned_object.data': {'foo': 1}}
obj = MyObj(foo=1)
obj.obj_reset_changes()
with mock.patch.object(obj, 'obj_make_compatible') as mock_compat:
self.assertEqual(
obj.obj_to_primitive(target_version='1.6'), expected)
self.assertFalse(mock_compat.called)
def test_object_property(self):
obj = MyObj(foo=1)
self.assertEqual(obj.foo, 1)
def test_object_property_type_error(self):
obj = MyObj()
def fail():
obj.foo = 'a'
self.assertRaises(ValueError, fail)
def test_object_dict_syntax(self):
obj = MyObj(foo=123, bar=u'text')
self.assertEqual(obj['foo'], 123)
self.assertIn('bar', obj)
self.assertNotIn('missing', obj)
self.assertEqual(sorted(iter(obj)),
['bar', 'foo'])
self.assertEqual(sorted(obj.keys()),
['bar', 'foo'])
self.assertEqual(sorted(obj.iterkeys()),
['bar', 'foo'])
self.assertEqual(sorted(obj.values(), key=str),
[123, u'text'])
self.assertEqual(sorted(obj.itervalues(), key=str),
[123, u'text'])
self.assertEqual(sorted(obj.items()),
[('bar', u'text'), ('foo', 123)])
self.assertEqual(sorted(list(obj.iteritems())),
[('bar', u'text'), ('foo', 123)])
self.assertEqual(dict(obj),
{'foo': 123, 'bar': u'text'})
def test_non_dict_remotable(self):
@base.VersionedObjectRegistry.register
class TestObject(base.VersionedObject):
@base.remotable
def test_method(self):
return 123
obj = TestObject(context=self.context)
self.assertEqual(123, obj.test_method())
def test_load(self):
obj = MyObj()
self.assertEqual(obj.bar, 'loaded!')
def test_load_in_base(self):
@base.VersionedObjectRegistry.register
class Foo(base.VersionedObject):
fields = {'foobar': fields.Field(fields.Integer())}
obj = Foo()
with self.assertRaisesRegex(NotImplementedError, ".*foobar.*"):
obj.foobar
def test_loaded_in_primitive(self):
obj = MyObj(foo=1)
obj.obj_reset_changes()
self.assertEqual(obj.bar, 'loaded!')
expected = {'versioned_object.name': 'MyObj',
'versioned_object.namespace': 'versionedobjects',
'versioned_object.version': '1.6',
'versioned_object.changes': ['bar'],
'versioned_object.data': {'foo': 1,
'bar': 'loaded!'}}
self.assertEqual(obj.obj_to_primitive(), expected)
def test_changes_in_primitive(self):
obj = MyObj(foo=123)
self.assertEqual(obj.obj_what_changed(), set(['foo']))
primitive = obj.obj_to_primitive()
self.assertIn('versioned_object.changes', primitive)
obj2 = MyObj.obj_from_primitive(primitive)
self.assertEqual(obj2.obj_what_changed(), set(['foo']))
obj2.obj_reset_changes()
self.assertEqual(obj2.obj_what_changed(), set())
def test_obj_class_from_name(self):
obj = base.VersionedObject.obj_class_from_name('MyObj', '1.5')
self.assertEqual('1.5', obj.VERSION)
def test_obj_class_from_name_latest_compatible(self):
obj = base.VersionedObject.obj_class_from_name('MyObj', '1.1')
self.assertEqual('1.6', obj.VERSION)
def test_unknown_objtype(self):
self.assertRaises(exception.UnsupportedObjectError,
base.VersionedObject.obj_class_from_name,
'foo', '1.0')
def test_obj_class_from_name_supported_version(self):
self.assertRaises(exception.IncompatibleObjectVersion,
base.VersionedObject.obj_class_from_name,
'MyObj', '1.25')
try:
base.VersionedObject.obj_class_from_name('MyObj', '1.25')
except exception.IncompatibleObjectVersion as error:
self.assertEqual('1.6', error.kwargs['supported'])
def test_orphaned_object(self):
obj = MyObj.query(self.context)
obj._context = None
self.assertRaises(exception.OrphanedObjectError,
obj._update_test)
def test_changed_1(self):
obj = MyObj.query(self.context)
obj.foo = 123
self.assertEqual(obj.obj_what_changed(), set(['foo']))
obj._update_test()
self.assertEqual(obj.obj_what_changed(), set(['foo', 'bar']))
self.assertEqual(obj.foo, 123)
def test_changed_2(self):
obj = MyObj.query(self.context)
obj.foo = 123
self.assertEqual(obj.obj_what_changed(), set(['foo']))
obj.save()
self.assertEqual(obj.obj_what_changed(), set([]))
self.assertEqual(obj.foo, 123)
def test_changed_3(self):
obj = MyObj.query(self.context)
obj.foo = 123
self.assertEqual(obj.obj_what_changed(), set(['foo']))
obj.refresh()
self.assertEqual(obj.obj_what_changed(), set([]))
self.assertEqual(obj.foo, 321)
self.assertEqual(obj.bar, 'refreshed')
def test_changed_4(self):
obj = MyObj.query(self.context)
obj.bar = 'something'
self.assertEqual(obj.obj_what_changed(), set(['bar']))
obj.modify_save_modify()
self.assertEqual(obj.obj_what_changed(), set(['foo', 'rel_object']))
self.assertEqual(obj.foo, 42)
self.assertEqual(obj.bar, 'meow')
self.assertIsInstance(obj.rel_object, MyOwnedObject)
def test_changed_with_sub_object(self):
@base.VersionedObjectRegistry.register
class ParentObject(base.VersionedObject):
fields = {'foo': fields.IntegerField(),
'bar': fields.ObjectField('MyObj'),
}
obj = ParentObject()
self.assertEqual(set(), obj.obj_what_changed())
obj.foo = 1
self.assertEqual(set(['foo']), obj.obj_what_changed())
bar = MyObj()
obj.bar = bar
self.assertEqual(set(['foo', 'bar']), obj.obj_what_changed())
obj.obj_reset_changes()
self.assertEqual(set(), obj.obj_what_changed())
bar.foo = 1
self.assertEqual(set(['bar']), obj.obj_what_changed())
def test_changed_with_bogus_field(self):
obj = MyObj()
obj.foo = 123
# Add a bogus field name to the changed list, as could be the
# case if we're sent some broken primitive from another node.
obj._changed_fields.add('does_not_exist')
self.assertEqual(set(['foo']), obj.obj_what_changed())
self.assertEqual({'foo': 123}, obj.obj_get_changes())
def test_static_result(self):
obj = MyObj.query(self.context)
self.assertEqual(obj.bar, 'bar')
result = obj.marco()
self.assertEqual(result, 'polo')
def test_updates(self):
obj = MyObj.query(self.context)
self.assertEqual(obj.foo, 1)
obj._update_test()
self.assertEqual(obj.bar, 'updated')
def test_contains(self):
obj = MyOwnedObject()
self.assertNotIn('baz', obj)
obj.baz = 1
self.assertIn('baz', obj)
self.assertNotIn('does_not_exist', obj)
def test_obj_attr_is_set(self):
obj = MyObj(foo=1)
self.assertTrue(obj.obj_attr_is_set('foo'))
self.assertFalse(obj.obj_attr_is_set('bar'))
self.assertRaises(AttributeError, obj.obj_attr_is_set, 'bang')
def test_obj_reset_changes_recursive(self):
obj = MyObj(rel_object=MyOwnedObject(baz=123),
rel_objects=[MyOwnedObject(baz=456)])
self.assertEqual(set(['rel_object', 'rel_objects']),
obj.obj_what_changed())
obj.obj_reset_changes()
self.assertEqual(set(['rel_object']), obj.obj_what_changed())
self.assertEqual(set(['baz']), obj.rel_object.obj_what_changed())
self.assertEqual(set(['baz']), obj.rel_objects[0].obj_what_changed())
obj.obj_reset_changes(recursive=True, fields=['foo'])
self.assertEqual(set(['rel_object']), obj.obj_what_changed())
self.assertEqual(set(['baz']), obj.rel_object.obj_what_changed())
self.assertEqual(set(['baz']), obj.rel_objects[0].obj_what_changed())
obj.obj_reset_changes(recursive=True)
self.assertEqual(set([]), obj.rel_object.obj_what_changed())
self.assertEqual(set([]), obj.obj_what_changed())
def test_get(self):
obj = MyObj(foo=1)
# Foo has value, should not get the default
self.assertEqual(obj.get('foo', 2), 1)
# Foo has value, should return the value without error
self.assertEqual(obj.get('foo'), 1)
# Bar is not loaded, so we should get the default
self.assertEqual(obj.get('bar', 'not-loaded'), 'not-loaded')
# Bar without a default should lazy-load
self.assertEqual(obj.get('bar'), 'loaded!')
# Bar now has a default, but loaded value should be returned
self.assertEqual(obj.get('bar', 'not-loaded'), 'loaded!')
# Invalid attribute should raise AttributeError
self.assertRaises(AttributeError, obj.get, 'nothing')
# ...even with a default
self.assertRaises(AttributeError, obj.get, 'nothing', 3)
def test_object_inheritance(self):
base_fields = []
myobj_fields = (['foo', 'bar', 'missing',
'readonly', 'rel_object',
'rel_objects', 'mutable_default', 'timestamp'] +
base_fields)
myobj3_fields = ['new_field']
self.assertTrue(issubclass(TestSubclassedObject, MyObj))
self.assertEqual(len(myobj_fields), len(MyObj.fields))
self.assertEqual(set(myobj_fields), set(MyObj.fields.keys()))
self.assertEqual(len(myobj_fields) + len(myobj3_fields),
len(TestSubclassedObject.fields))
self.assertEqual(set(myobj_fields) | set(myobj3_fields),
set(TestSubclassedObject.fields.keys()))
def test_obj_as_admin(self):
self.skip('oslo.context does not support elevated()')
obj = MyObj(context=self.context)
def fake(*args, **kwargs):
self.assertTrue(obj._context.is_admin)
with mock.patch.object(obj, 'obj_reset_changes') as mock_fn:
mock_fn.side_effect = fake
with obj.obj_as_admin():
obj.save()
self.assertTrue(mock_fn.called)
self.assertFalse(obj._context.is_admin)
def test_get_changes(self):
obj = MyObj()
self.assertEqual({}, obj.obj_get_changes())
obj.foo = 123
self.assertEqual({'foo': 123}, obj.obj_get_changes())
obj.bar = 'test'
self.assertEqual({'foo': 123, 'bar': 'test'}, obj.obj_get_changes())
obj.obj_reset_changes()
self.assertEqual({}, obj.obj_get_changes())
timestamp = datetime.datetime(2001, 1, 1, tzinfo=pytz.utc)
with mock.patch.object(timeutils, 'utcnow') as mock_utcnow:
mock_utcnow.return_value = timestamp
obj.timestamp = timeutils.utcnow()
self.assertEqual({'timestamp': timestamp}, obj.obj_get_changes())
obj.obj_reset_changes()
self.assertEqual({}, obj.obj_get_changes())
# Timestamp without tzinfo causes mismatch
timestamp = datetime.datetime(2001, 1, 1)
with mock.patch.object(timeutils, 'utcnow') as mock_utcnow:
mock_utcnow.return_value = timestamp
obj.timestamp = timeutils.utcnow()
self.assertRaises(TypeError, obj.obj_get_changes())
obj.obj_reset_changes()
self.assertEqual({}, obj.obj_get_changes())
def test_obj_fields(self):
class TestObj(base.VersionedObject):
fields = {'foo': fields.Field(fields.Integer())}
obj_extra_fields = ['bar']
@property
def bar(self):
return 'this is bar'
obj = TestObj()
self.assertEqual(['foo', 'bar'], obj.obj_fields)
def test_obj_context(self):
class TestObj(base.VersionedObject):
pass
# context is available through the public property
context = mock.Mock()
obj = TestObj(context)
self.assertEqual(context, obj.obj_context)
# ..but it's not available for update
new_context = mock.Mock()
self.assertRaises(
AttributeError,
setattr, obj, 'obj_context', new_context)
def test_obj_constructor(self):
obj = MyObj(context=self.context, foo=123, bar='abc')
self.assertEqual(123, obj.foo)
self.assertEqual('abc', obj.bar)
self.assertEqual(set(['foo', 'bar']), obj.obj_what_changed())
def test_obj_read_only(self):
obj = MyObj(context=self.context, foo=123, bar='abc')
obj.readonly = 1
self.assertRaises(exception.ReadOnlyFieldError, setattr,
obj, 'readonly', 2)
def test_obj_mutable_default(self):
obj = MyObj(context=self.context, foo=123, bar='abc')
obj.mutable_default = None
obj.mutable_default.append('s1')
self.assertEqual(obj.mutable_default, ['s1'])
obj1 = MyObj(context=self.context, foo=123, bar='abc')
obj1.mutable_default = None
obj1.mutable_default.append('s2')
self.assertEqual(obj1.mutable_default, ['s2'])
def test_obj_mutable_default_set_default(self):
obj1 = MyObj(context=self.context, foo=123, bar='abc')
obj1.obj_set_defaults('mutable_default')
self.assertEqual(obj1.mutable_default, [])
obj1.mutable_default.append('s1')
self.assertEqual(obj1.mutable_default, ['s1'])
obj2 = MyObj(context=self.context, foo=123, bar='abc')
obj2.obj_set_defaults('mutable_default')
self.assertEqual(obj2.mutable_default, [])
obj2.mutable_default.append('s2')
self.assertEqual(obj2.mutable_default, ['s2'])
def test_obj_repr(self):
obj = MyObj(foo=123)
self.assertEqual('MyObj(bar=<?>,foo=123,missing=<?>,'
'mutable_default=<?>,readonly=<?>,'
'rel_object=<?>,rel_objects=<?>,timestamp=<?>)',
repr(obj))
def test_obj_repr_sensitive(self):
obj = MySensitiveObj(data="""{'admin_password':'mypassword'}""")
self.assertEqual(
'MySensitiveObj(data=\'{\'admin_password\':\'***\'}\')', repr(obj))
obj2 = MySensitiveObj()
self.assertEqual('MySensitiveObj(data=<?>)', repr(obj2))
def test_obj_make_obj_compatible_with_relationships(self):
subobj = MyOwnedObject(baz=1)
obj = MyObj(rel_object=subobj)
obj.obj_relationships = {
'rel_object': [('1.5', '1.1'), ('1.7', '1.2')],
}
primitive = obj.obj_to_primitive()['versioned_object.data']
with mock.patch.object(subobj, 'obj_make_compatible') as mock_compat:
obj._obj_make_obj_compatible(copy.copy(primitive), '1.8',
'rel_object')
self.assertFalse(mock_compat.called)
with mock.patch.object(subobj, 'obj_make_compatible') as mock_compat:
obj._obj_make_obj_compatible(copy.copy(primitive),
'1.7', 'rel_object')
mock_compat.assert_called_once_with(
primitive['rel_object']['versioned_object.data'], '1.2')
self.assertEqual(
'1.2', primitive['rel_object']['versioned_object.version'])
with mock.patch.object(subobj, 'obj_make_compatible') as mock_compat:
obj._obj_make_obj_compatible(copy.copy(primitive),
'1.6', 'rel_object')
mock_compat.assert_called_once_with(
primitive['rel_object']['versioned_object.data'], '1.1')
self.assertEqual(
'1.1', primitive['rel_object']['versioned_object.version'])
with mock.patch.object(subobj, 'obj_make_compatible') as mock_compat:
obj._obj_make_obj_compatible(copy.copy(primitive), '1.5',
'rel_object')
mock_compat.assert_called_once_with(
primitive['rel_object']['versioned_object.data'], '1.1')
self.assertEqual(
'1.1', primitive['rel_object']['versioned_object.version'])
with mock.patch.object(subobj, 'obj_make_compatible') as mock_compat:
_prim = copy.copy(primitive)
obj._obj_make_obj_compatible(_prim, '1.4', 'rel_object')
self.assertFalse(mock_compat.called)
self.assertNotIn('rel_object', _prim)
def test_obj_make_compatible_hits_sub_objects_with_rels(self):
subobj = MyOwnedObject(baz=1)
obj = MyObj(foo=123, rel_object=subobj)
obj.obj_relationships = {'rel_object': [('1.0', '1.0')]}
with mock.patch.object(obj, '_obj_make_obj_compatible') as mock_compat:
obj.obj_make_compatible({'rel_object': 'foo'}, '1.10')
mock_compat.assert_called_once_with({'rel_object': 'foo'}, '1.10',
'rel_object')
def test_obj_make_compatible_skips_unset_sub_objects_with_rels(self):
obj = MyObj(foo=123)
obj.obj_relationships = {'rel_object': [('1.0', '1.0')]}
with mock.patch.object(obj, '_obj_make_obj_compatible') as mock_compat:
obj.obj_make_compatible({'rel_object': 'foo'}, '1.10')
self.assertFalse(mock_compat.called)
def test_obj_make_compatible_complains_about_missing_rel_rules(self):
subobj = MyOwnedObject(baz=1)
obj = MyObj(foo=123, rel_object=subobj)
obj.obj_relationships = {}
self.assertRaises(exception.ObjectActionError,
obj.obj_make_compatible, {}, '1.0')
def test_obj_make_compatible_handles_list_of_objects_with_rels(self):
subobj = MyOwnedObject(baz=1)
obj = MyObj(rel_objects=[subobj])
obj.obj_relationships = {'rel_objects': [('1.0', '1.123')]}
def fake_make_compat(primitive, version, **k):
self.assertEqual('1.123', version)
self.assertIn('baz', primitive)
with mock.patch.object(subobj, 'obj_make_compatible') as mock_mc:
mock_mc.side_effect = fake_make_compat
obj.obj_to_primitive('1.0')
self.assertTrue(mock_mc.called)
def test_obj_make_compatible_with_manifest(self):
subobj = MyOwnedObject(baz=1)
obj = MyObj(rel_object=subobj)
obj.obj_relationships = {}
orig_primitive = obj.obj_to_primitive()['versioned_object.data']
with mock.patch.object(subobj, 'obj_make_compatible') as mock_compat:
manifest = {'MyOwnedObject': '1.2'}
primitive = copy.deepcopy(orig_primitive)
obj.obj_make_compatible_from_manifest(primitive, '1.5', manifest)
mock_compat.assert_called_once_with(
primitive['rel_object']['versioned_object.data'], '1.2')
self.assertEqual(
'1.2',
primitive['rel_object']['versioned_object.version'])
with mock.patch.object(subobj, 'obj_make_compatible') as mock_compat:
manifest = {'MyOwnedObject': '1.0'}
primitive = copy.deepcopy(orig_primitive)
obj.obj_make_compatible_from_manifest(primitive, '1.5', manifest)
mock_compat.assert_called_once_with(
primitive['rel_object']['versioned_object.data'], '1.0')
self.assertEqual(
'1.0',
primitive['rel_object']['versioned_object.version'])
with mock.patch.object(subobj, 'obj_make_compatible') as mock_compat:
manifest = {}
primitive = copy.deepcopy(orig_primitive)
obj.obj_make_compatible_from_manifest(primitive, '1.5', manifest)
self.assertFalse(mock_compat.called)
self.assertEqual(
'1.0',
primitive['rel_object']['versioned_object.version'])
def test_obj_make_compatible_with_manifest_subobj(self):
# Make sure that we call the subobject's "from_manifest" method
# as well
subobj = MyOwnedObject(baz=1)
obj = MyObj(rel_object=subobj)
obj.obj_relationships = {}
manifest = {'MyOwnedObject': '1.2'}
primitive = obj.obj_to_primitive()['versioned_object.data']
method = 'obj_make_compatible_from_manifest'
with mock.patch.object(subobj, method) as mock_compat:
obj.obj_make_compatible_from_manifest(primitive, '1.5', manifest)
mock_compat.assert_called_once_with(
primitive['rel_object']['versioned_object.data'],
'1.2', version_manifest=manifest)
def test_obj_make_compatible_with_manifest_subobj_list(self):
# Make sure that we call the subobject's "from_manifest" method
# as well
subobj = MyOwnedObject(baz=1)
obj = MyObj(rel_objects=[subobj])
obj.obj_relationships = {}
manifest = {'MyOwnedObject': '1.2'}
primitive = obj.obj_to_primitive()['versioned_object.data']
method = 'obj_make_compatible_from_manifest'
with mock.patch.object(subobj, method) as mock_compat:
obj.obj_make_compatible_from_manifest(primitive, '1.5', manifest)
mock_compat.assert_called_once_with(
primitive['rel_objects'][0]['versioned_object.data'],
'1.2', version_manifest=manifest)
def test_obj_make_compatible_removes_field_cleans_changes(self):
@base.VersionedObjectRegistry.register_if(False)
class TestObject(base.VersionedObject):
VERSION = '1.1'
fields = {'foo': fields.StringField(),
'bar': fields.StringField()}
def obj_make_compatible(self, primitive, target_version):
del primitive['bar']
obj = TestObject(foo='test1', bar='test2')
prim = obj.obj_to_primitive('1.0')
self.assertEqual(['foo'], prim['versioned_object.changes'])
def test_delattr(self):
obj = MyObj(bar='foo')
del obj.bar
# Should appear unset now
self.assertFalse(obj.obj_attr_is_set('bar'))
# Make sure post-delete, references trigger lazy loads
self.assertEqual('loaded!', getattr(obj, 'bar'))
def test_delattr_unset(self):
obj = MyObj()
self.assertRaises(AttributeError, delattr, obj, 'bar')
def test_obj_make_compatible_on_list_base(self):
@base.VersionedObjectRegistry.register_if(False)
class MyList(base.ObjectListBase, base.VersionedObject):
VERSION = '1.1'
fields = {'objects': fields.ListOfObjectsField('MyObj')}
childobj = MyObj(foo=1)
listobj = MyList(objects=[childobj])
compat_func = 'obj_make_compatible_from_manifest'
with mock.patch.object(childobj, compat_func) as mock_compat:
listobj.obj_to_primitive(target_version='1.0')
mock_compat.assert_called_once_with({'foo': 1}, '1.0',
version_manifest=None)
def test_comparable_objects(self):
class NonVersionedObject(object):
pass
obj1 = MyComparableObj(foo=1)
obj2 = MyComparableObj(foo=1)
obj3 = MyComparableObj(foo=2)
obj4 = NonVersionedObject()
self.assertTrue(obj1 == obj2)
self.assertFalse(obj1 == obj3)
self.assertFalse(obj1 == obj4)
self.assertNotEqual(obj1, None)
def test_compound_clone(self):
obj = MyCompoundObject()
obj.foo = [1, 2, 3]
obj.bar = {"a": 1, "b": 2, "c": 3}
obj.baz = set([1, 2, 3])
copy = obj.obj_clone()
self.assertEqual(obj.foo, copy.foo)
self.assertEqual(obj.bar, copy.bar)
self.assertEqual(obj.baz, copy.baz)
# ensure that the cloned object still coerces values in its compounds
copy.foo.append("4")
copy.bar.update(d="4")
copy.baz.add("4")
self.assertEqual([1, 2, 3, 4], copy.foo)
self.assertEqual({"a": 1, "b": 2, "c": 3, "d": 4}, copy.bar)
self.assertEqual(set([1, 2, 3, 4]), copy.baz)
def test_obj_list_fields_modifications(self):
@base.VersionedObjectRegistry.register
class ObjWithList(base.VersionedObject):
fields = {
'list_field': fields.Field(fields.List(fields.Integer())),
}
obj = ObjWithList()
def set_by_index(val):
obj.list_field[0] = val
def append(val):
obj.list_field.append(val)
def extend(val):
obj.list_field.extend([val])
def add(val):
obj.list_field = obj.list_field + [val]
def iadd(val):
"""Test += corner case
a=a+b and a+=b use different magic methods under the hood:
first one calls __add__ which clones initial value before the
assignment, second one call __iadd__ which modifies the initial
list.
Assignment should cause coercing in both cases, but __iadd__ may
corrupt the initial value even if the assignment fails.
So it should be overridden as well, and this test is needed to
verify it
"""
obj.list_field += [val]
def insert(val):
obj.list_field.insert(0, val)
def simple_slice(val):
obj.list_field[:] = [val]
def extended_slice(val):
"""Extended slice case
Extended slice (and regular slices in py3) are handled differently
thus needing a separate test
"""
obj.list_field[::2] = [val]
# positive tests to ensure that coercing works properly
obj.list_field = ["42"]
set_by_index("1")
append("2")
extend("3")
add("4")
iadd("5")
insert("0")
self.assertEqual([0, 1, 2, 3, 4, 5], obj.list_field)
simple_slice("10")
self.assertEqual([10], obj.list_field)
extended_slice("42")
self.assertEqual([42], obj.list_field)
obj.obj_reset_changes()
# negative tests with non-coerceable values
self.assertRaises(ValueError, set_by_index, "abc")
self.assertRaises(ValueError, append, "abc")
self.assertRaises(ValueError, extend, "abc")
self.assertRaises(ValueError, add, "abc")
self.assertRaises(ValueError, iadd, "abc")
self.assertRaises(ValueError, insert, "abc")
self.assertRaises(ValueError, simple_slice, "abc")
self.assertRaises(ValueError, extended_slice, "abc")
# ensure that nothing has been changed
self.assertEqual([42], obj.list_field)
self.assertEqual({}, obj.obj_get_changes())
def test_obj_dict_field_modifications(self):
@base.VersionedObjectRegistry.register
class ObjWithDict(base.VersionedObject):
fields = {
'dict_field': fields.Field(fields.Dict(fields.Integer())),
}
obj = ObjWithDict()
obj.dict_field = {"1": 1, "3": 3, "4": 4}
def set_by_key(key, value):
obj.dict_field[key] = value
def add_by_key(key, value):
obj.dict_field[key] = value
def update_w_dict(key, value):
obj.dict_field.update({key: value})
def update_w_kwargs(key, value):
obj.dict_field.update(**{key: value})
def setdefault(key, value):
obj.dict_field.setdefault(key, value)
# positive tests to ensure that coercing works properly
set_by_key("1", "10")
add_by_key("2", "20")
update_w_dict("3", "30")
update_w_kwargs("4", "40")
setdefault("5", "50")
self.assertEqual({"1": 10, "2": 20, "3": 30, "4": 40, "5": 50},
obj.dict_field)
obj.obj_reset_changes()
# negative tests with non-coerceable values
self.assertRaises(ValueError, set_by_key, "key", "abc")
self.assertRaises(ValueError, add_by_key, "other", "abc")
self.assertRaises(ValueError, update_w_dict, "key", "abc")
self.assertRaises(ValueError, update_w_kwargs, "key", "abc")
self.assertRaises(ValueError, setdefault, "other", "abc")
# ensure that nothing has been changed
self.assertEqual({"1": 10, "2": 20, "3": 30, "4": 40, "5": 50},
obj.dict_field)
self.assertEqual({}, obj.obj_get_changes())
def test_obj_set_field_modifications(self):
@base.VersionedObjectRegistry.register
class ObjWithSet(base.VersionedObject):
fields = {
'set_field': fields.Field(fields.Set(fields.Integer()))
}
obj = ObjWithSet()
obj.set_field = set([42])
def add(value):
obj.set_field.add(value)
def update_w_set(value):
obj.set_field.update(set([value]))
def update_w_list(value):
obj.set_field.update([value, value, value])
def sym_diff_upd(value):
obj.set_field.symmetric_difference_update(set([value]))
def union(value):
obj.set_field = obj.set_field | set([value])
def iunion(value):
obj.set_field |= set([value])
def xor(value):
obj.set_field = obj.set_field ^ set([value])
def ixor(value):
obj.set_field ^= set([value])
# positive tests to ensure that coercing works properly
sym_diff_upd("42")
add("1")
update_w_list("2")
update_w_set("3")
union("4")
iunion("5")
xor("6")
ixor("7")
self.assertEqual(set([1, 2, 3, 4, 5, 6, 7]), obj.set_field)
obj.set_field = set([42])
obj.obj_reset_changes()
# negative tests with non-coerceable values
self.assertRaises(ValueError, add, "abc")
self.assertRaises(ValueError, update_w_list, "abc")
self.assertRaises(ValueError, update_w_set, "abc")
self.assertRaises(ValueError, sym_diff_upd, "abc")
self.assertRaises(ValueError, union, "abc")
self.assertRaises(ValueError, iunion, "abc")
self.assertRaises(ValueError, xor, "abc")
self.assertRaises(ValueError, ixor, "abc")
# ensure that nothing has been changed
self.assertEqual(set([42]), obj.set_field)
self.assertEqual({}, obj.obj_get_changes())
class TestObject(_LocalTest, _TestObject):
def test_set_defaults(self):
obj = MyObj()
obj.obj_set_defaults('foo')
self.assertTrue(obj.obj_attr_is_set('foo'))
self.assertEqual(1, obj.foo)
def test_set_defaults_no_default(self):
obj = MyObj()
self.assertRaises(exception.ObjectActionError,
obj.obj_set_defaults, 'bar')
def test_set_all_defaults(self):
obj = MyObj()
obj.obj_set_defaults()
self.assertEqual(set(['mutable_default', 'foo']),
obj.obj_what_changed())
self.assertEqual(1, obj.foo)
def test_set_defaults_not_overwrite(self):
# NOTE(danms): deleted defaults to False, so verify that it does
# not get reset by obj_set_defaults()
obj = MyObj(deleted=True)
obj.obj_set_defaults()
self.assertEqual(1, obj.foo)
self.assertTrue(obj.deleted)
class TestRemoteObject(_RemoteTest, _TestObject):
@mock.patch('oslo_versionedobjects.base.obj_tree_get_versions')
def test_major_version_mismatch(self, mock_otgv):
mock_otgv.return_value = {'MyObj': '2.0'}
self.assertRaises(exception.IncompatibleObjectVersion,
MyObj2.query, self.context)
@mock.patch('oslo_versionedobjects.base.obj_tree_get_versions')
def test_minor_version_greater(self, mock_otgv):
mock_otgv.return_value = {'MyObj': '1.7'}
self.assertRaises(exception.IncompatibleObjectVersion,
MyObj2.query, self.context)
@mock.patch('oslo_versionedobjects.base.obj_tree_get_versions')
def test_minor_version_less(self, mock_otgv):
mock_otgv.return_value = {'MyObj': '1.2'}
obj = MyObj2.query(self.context)
self.assertEqual(obj.bar, 'bar')
@mock.patch('oslo_versionedobjects.base.obj_tree_get_versions')
def test_compat(self, mock_otgv):
mock_otgv.return_value = {'MyObj': '1.1'}
obj = MyObj2.query(self.context)
self.assertEqual('oldbar', obj.bar)
@mock.patch('oslo_versionedobjects.base.obj_tree_get_versions')
def test_revision_ignored(self, mock_otgv):
mock_otgv.return_value = {'MyObj': '1.1.456'}
obj = MyObj2.query(self.context)
self.assertEqual('bar', obj.bar)
def test_class_action_falls_back_compat(self):
with mock.patch.object(base.VersionedObject, 'indirection_api') as ma:
ma.object_class_action_versions.side_effect = NotImplementedError
MyObj.query(self.context)
ma.object_class_action.assert_called_once_with(
self.context, 'MyObj', 'query', MyObj.VERSION, (), {})
class TestObjectListBase(test.TestCase):
def test_list_like_operations(self):
@base.VersionedObjectRegistry.register
class MyElement(base.VersionedObject):
fields = {'foo': fields.IntegerField()}
def __init__(self, foo):
super(MyElement, self).__init__()
self.foo = foo
class Foo(base.ObjectListBase, base.VersionedObject):
fields = {'objects': fields.ListOfObjectsField('MyElement')}
objlist = Foo(context='foo',
objects=[MyElement(1), MyElement(2), MyElement(3)])
self.assertEqual(list(objlist), objlist.objects)
self.assertEqual(len(objlist), 3)
self.assertIn(objlist.objects[0], objlist)
self.assertEqual(list(objlist[:1]), [objlist.objects[0]])
self.assertEqual(objlist[:1]._context, 'foo')
self.assertEqual(objlist[2], objlist.objects[2])
self.assertEqual(objlist.count(objlist.objects[0]), 1)
self.assertEqual(objlist.index(objlist.objects[1]), 1)
objlist.sort(key=lambda x: x.foo, reverse=True)
self.assertEqual([3, 2, 1],
[x.foo for x in objlist])
def test_serialization(self):
@base.VersionedObjectRegistry.register
class Foo(base.ObjectListBase, base.VersionedObject):
fields = {'objects': fields.ListOfObjectsField('Bar')}
@base.VersionedObjectRegistry.register
class Bar(base.VersionedObject):
fields = {'foo': fields.Field(fields.String())}
obj = Foo(objects=[])
for i in 'abc':
bar = Bar(foo=i)
obj.objects.append(bar)
obj2 = base.VersionedObject.obj_from_primitive(obj.obj_to_primitive())
self.assertFalse(obj is obj2)
self.assertEqual([x.foo for x in obj],
[y.foo for y in obj2])
def _test_object_list_version_mappings(self, list_obj_class):
# Figure out what sort of object this list is for
list_field = list_obj_class.fields['objects']
item_obj_field = list_field._type._element_type
item_obj_name = item_obj_field._type._obj_name
# Look through all object classes of this type and make sure that
# the versions we find are covered by the parent list class
obj_classes = base.VersionedObjectRegistry.obj_classes()[item_obj_name]
for item_class in obj_classes:
if is_test_object(item_class):
continue
self.assertIn(
item_class.VERSION,
list_obj_class.child_versions.values(),
'Version mapping is incomplete for %s' % (
list_obj_class.__name__))
def test_object_version_mappings(self):
self.skip('this needs to be generalized')
# Find all object list classes and make sure that they at least handle
# all the current object versions
for obj_classes in base.VersionedObjectRegistry.obj_classes().values():
for obj_class in obj_classes:
if issubclass(obj_class, base.ObjectListBase):
self._test_object_list_version_mappings(obj_class)
def test_obj_make_compatible_child_versions(self):
@base.VersionedObjectRegistry.register
class MyElement(base.VersionedObject):
fields = {'foo': fields.IntegerField()}
@base.VersionedObjectRegistry.register
class Foo(base.ObjectListBase, base.VersionedObject):
VERSION = '1.1'
fields = {'objects': fields.ListOfObjectsField('MyElement')}
child_versions = {'1.0': '1.0', '1.1': '1.0'}
subobj = MyElement(foo=1)
obj = Foo(objects=[subobj])
primitive = obj.obj_to_primitive()['versioned_object.data']
with mock.patch.object(subobj, 'obj_make_compatible') as mock_compat:
obj.obj_make_compatible(copy.copy(primitive), '1.1')
self.assertTrue(mock_compat.called)
def test_obj_make_compatible_obj_relationships(self):
@base.VersionedObjectRegistry.register
class MyElement(base.VersionedObject):
fields = {'foo': fields.IntegerField()}
@base.VersionedObjectRegistry.register
class Bar(base.ObjectListBase, base.VersionedObject):
VERSION = '1.1'
fields = {'objects': fields.ListOfObjectsField('MyElement')}
obj_relationships = {
'objects': [('1.0', '1.0'), ('1.1', '1.0')]
}
subobj = MyElement(foo=1)
obj = Bar(objects=[subobj])
primitive = obj.obj_to_primitive()['versioned_object.data']
with mock.patch.object(subobj, 'obj_make_compatible') as mock_compat:
obj.obj_make_compatible(copy.copy(primitive), '1.1')
self.assertTrue(mock_compat.called)
def test_obj_make_compatible_no_relationships(self):
@base.VersionedObjectRegistry.register
class MyElement(base.VersionedObject):
fields = {'foo': fields.IntegerField()}
@base.VersionedObjectRegistry.register
class Baz(base.ObjectListBase, base.VersionedObject):
VERSION = '1.1'
fields = {'objects': fields.ListOfObjectsField('MyElement')}
subobj = MyElement(foo=1)
obj = Baz(objects=[subobj])
primitive = obj.obj_to_primitive()['versioned_object.data']
with mock.patch.object(subobj, 'obj_make_compatible') as mock_compat:
obj.obj_make_compatible(copy.copy(primitive), '1.1')
self.assertTrue(mock_compat.called)
def test_list_changes(self):
@base.VersionedObjectRegistry.register
class Foo(base.ObjectListBase, base.VersionedObject):
fields = {'objects': fields.ListOfObjectsField('Bar')}
@base.VersionedObjectRegistry.register
class Bar(base.VersionedObject):
fields = {'foo': fields.StringField()}
obj = Foo(objects=[])
self.assertEqual(set(['objects']), obj.obj_what_changed())
obj.objects.append(Bar(foo='test'))
self.assertEqual(set(['objects']), obj.obj_what_changed())
obj.obj_reset_changes()
# This should still look dirty because the child is dirty
self.assertEqual(set(['objects']), obj.obj_what_changed())
obj.objects[0].obj_reset_changes()
# This should now look clean because the child is clean
self.assertEqual(set(), obj.obj_what_changed())
def test_initialize_objects(self):
class Foo(base.ObjectListBase, base.VersionedObject):
fields = {'objects': fields.ListOfObjectsField('Bar')}
class Bar(base.VersionedObject):
fields = {'foo': fields.StringField()}
obj = Foo()
self.assertEqual([], obj.objects)
self.assertEqual(set(), obj.obj_what_changed())
def test_obj_repr(self):
@base.VersionedObjectRegistry.register
class Foo(base.ObjectListBase, base.VersionedObject):
fields = {'objects': fields.ListOfObjectsField('Bar')}
@base.VersionedObjectRegistry.register
class Bar(base.VersionedObject):
fields = {'uuid': fields.StringField()}
obj = Foo(objects=[Bar(uuid='fake-uuid')])
self.assertEqual('Foo(objects=[Bar(fake-uuid)])', repr(obj))
class TestObjectSerializer(_BaseTestCase):
def test_serialize_entity_primitive(self):
ser = base.VersionedObjectSerializer()
for thing in (1, 'foo', [1, 2], {'foo': 'bar'}):
self.assertEqual(thing, ser.serialize_entity(None, thing))
def test_deserialize_entity_primitive(self):
ser = base.VersionedObjectSerializer()
for thing in (1, 'foo', [1, 2], {'foo': 'bar'}):
self.assertEqual(thing, ser.deserialize_entity(None, thing))
def test_serialize_set_to_list(self):
ser = base.VersionedObjectSerializer()
self.assertEqual([1, 2], ser.serialize_entity(None, set([1, 2])))
@mock.patch('oslo_versionedobjects.base.VersionedObject.indirection_api')
def _test_deserialize_entity_newer(self, obj_version, backported_to,
mock_iapi,
my_version='1.6'):
ser = base.VersionedObjectSerializer()
mock_iapi.object_backport_versions.return_value = 'backported'
@base.VersionedObjectRegistry.register
class MyTestObj(MyObj):
VERSION = my_version
obj = MyTestObj()
obj.VERSION = obj_version
primitive = obj.obj_to_primitive()
result = ser.deserialize_entity(self.context, primitive)
if backported_to is None:
self.assertFalse(mock_iapi.object_backport_versions.called)
else:
self.assertEqual('backported', result)
mock_iapi.object_backport_versions.assert_called_with(
self.context, primitive, {'MyTestObj': my_version,
'MyOwnedObject': '1.0'})
def test_deserialize_entity_newer_version_backports(self):
self._test_deserialize_entity_newer('1.25', '1.6')
def test_deserialize_entity_newer_revision_does_not_backport_zero(self):
self._test_deserialize_entity_newer('1.6.0', None)
def test_deserialize_entity_newer_revision_does_not_backport(self):
self._test_deserialize_entity_newer('1.6.1', None)
def test_deserialize_entity_newer_version_passes_revision(self):
self._test_deserialize_entity_newer('1.7', '1.6.1', my_version='1.6.1')
def test_deserialize_dot_z_with_extra_stuff(self):
primitive = {'versioned_object.name': 'MyObj',
'versioned_object.namespace': 'versionedobjects',
'versioned_object.version': '1.6.1',
'versioned_object.data': {
'foo': 1,
'unexpected_thing': 'foobar'}}
ser = base.VersionedObjectSerializer()
obj = ser.deserialize_entity(self.context, primitive)
self.assertEqual(1, obj.foo)
self.assertFalse(hasattr(obj, 'unexpected_thing'))
# NOTE(danms): The serializer is where the logic lives that
# avoids backports for cases where only a .z difference in
# the received object version is detected. As a result, we
# end up with a version of what we expected, effectively the
# .0 of the object.
self.assertEqual('1.6', obj.VERSION)
def test_deserialize_entity_newer_version_no_indirection(self):
ser = base.VersionedObjectSerializer()
obj = MyObj()
obj.VERSION = '1.25'
primitive = obj.obj_to_primitive()
self.assertRaises(exception.IncompatibleObjectVersion,
ser.deserialize_entity, self.context, primitive)
def _test_nested_backport(self, old):
@base.VersionedObjectRegistry.register
class Parent(base.VersionedObject):
VERSION = '1.0'
fields = {
'child': fields.ObjectField('MyObj'),
}
@base.VersionedObjectRegistry.register # noqa
class Parent(base.VersionedObject):
VERSION = '1.1'
fields = {
'child': fields.ObjectField('MyObj'),
}
child = MyObj(foo=1)
parent = Parent(child=child)
prim = parent.obj_to_primitive()
child_prim = prim['versioned_object.data']['child']
child_prim['versioned_object.version'] = '1.10'
ser = base.VersionedObjectSerializer()
with mock.patch.object(base.VersionedObject, 'indirection_api') as a:
if old:
a.object_backport_versions.side_effect = NotImplementedError
ser.deserialize_entity(self.context, prim)
a.object_backport_versions.assert_called_once_with(
self.context, prim, {'Parent': '1.1',
'MyObj': '1.6',
'MyOwnedObject': '1.0'})
if old:
# NOTE(danms): This should be the version of the parent object,
# not the child. If wrong, this will be '1.6', which is the max
# child version in our registry.
a.object_backport.assert_called_once_with(
self.context, prim, '1.1')
def test_nested_backport_new_method(self):
self._test_nested_backport(old=False)
def test_nested_backport_old_method(self):
self._test_nested_backport(old=True)
def test_object_serialization(self):
ser = base.VersionedObjectSerializer()
obj = MyObj()
primitive = ser.serialize_entity(self.context, obj)
self.assertIn('versioned_object.name', primitive)
obj2 = ser.deserialize_entity(self.context, primitive)
self.assertIsInstance(obj2, MyObj)
self.assertEqual(self.context, obj2._context)
def test_object_serialization_iterables(self):
ser = base.VersionedObjectSerializer()
obj = MyObj()
for iterable in (list, tuple, set):
thing = iterable([obj])
primitive = ser.serialize_entity(self.context, thing)
self.assertEqual(1, len(primitive))
for item in primitive:
self.assertNotIsInstance(item, base.VersionedObject)
thing2 = ser.deserialize_entity(self.context, primitive)
self.assertEqual(1, len(thing2))
for item in thing2:
self.assertIsInstance(item, MyObj)
# dict case
thing = {'key': obj}
primitive = ser.serialize_entity(self.context, thing)
self.assertEqual(1, len(primitive))
for item in six.itervalues(primitive):
self.assertNotIsInstance(item, base.VersionedObject)
thing2 = ser.deserialize_entity(self.context, primitive)
self.assertEqual(1, len(thing2))
for item in six.itervalues(thing2):
self.assertIsInstance(item, MyObj)
# object-action updates dict case
thing = {'foo': obj.obj_to_primitive()}
primitive = ser.serialize_entity(self.context, thing)
self.assertEqual(thing, primitive)
thing2 = ser.deserialize_entity(self.context, thing)
self.assertIsInstance(thing2['foo'], base.VersionedObject)
def test_serializer_subclass_namespace(self):
@base.VersionedObjectRegistry.register
class MyNSObj(base.VersionedObject):
OBJ_SERIAL_NAMESPACE = 'foo'
fields = {'foo': fields.IntegerField()}
class MySerializer(base.VersionedObjectSerializer):
OBJ_BASE_CLASS = MyNSObj
ser = MySerializer()
obj = MyNSObj(foo=123)
obj2 = ser.deserialize_entity(None, ser.serialize_entity(None, obj))
self.assertIsInstance(obj2, MyNSObj)
self.assertEqual(obj.foo, obj2.foo)
def test_serializer_subclass_namespace_mismatch(self):
@base.VersionedObjectRegistry.register
class MyNSObj(base.VersionedObject):
OBJ_SERIAL_NAMESPACE = 'foo'
fields = {'foo': fields.IntegerField()}
class MySerializer(base.VersionedObjectSerializer):
OBJ_BASE_CLASS = MyNSObj
myser = MySerializer()
voser = base.VersionedObjectSerializer()
obj = MyObj(foo=123)
obj2 = myser.deserialize_entity(None,
voser.serialize_entity(None, obj))
# NOTE(danms): The new serializer should have ignored the objects
# serialized by the base serializer, so obj2 here should be a dict
# primitive and not a hydrated object
self.assertNotIsInstance(obj2, MyNSObj)
self.assertIn('versioned_object.name', obj2)
def test_serializer_subclass_base_object_indirection(self):
@base.VersionedObjectRegistry.register
class MyNSObj(base.VersionedObject):
OBJ_SERIAL_NAMESPACE = 'foo'
fields = {'foo': fields.IntegerField()}
indirection_api = mock.MagicMock()
class MySerializer(base.VersionedObjectSerializer):
OBJ_BASE_CLASS = MyNSObj
ser = MySerializer()
prim = MyNSObj(foo=1).obj_to_primitive()
prim['foo.version'] = '2.0'
ser.deserialize_entity(mock.sentinel.context, prim)
indirection_api = MyNSObj.indirection_api
indirection_api.object_backport_versions.assert_called_once_with(
mock.sentinel.context, prim, {'MyNSObj': '1.0'})
@mock.patch('oslo_versionedobjects.base.VersionedObject.indirection_api')
def test_serializer_calls_old_backport_interface(self, indirection_api):
@base.VersionedObjectRegistry.register
class MyOldObj(base.VersionedObject):
pass
ser = base.VersionedObjectSerializer()
prim = MyOldObj(foo=1).obj_to_primitive()
prim['versioned_object.version'] = '2.0'
indirection_api.object_backport_versions.side_effect = (
NotImplementedError('Old'))
ser.deserialize_entity(mock.sentinel.context, prim)
indirection_api.object_backport.assert_called_once_with(
mock.sentinel.context, prim, '1.0')
class TestSchemaGeneration(test.TestCase):
class FakeFieldType(fields.FieldType):
pass
def setUp(self):
super(TestSchemaGeneration, self).setUp()
self.nonNullableField = fields.Field(self.FakeFieldType)
self.nullableField = fields.Field(self.FakeFieldType)
class TestObject(base.VersionedObject):
fields = {'foo': self.nonNullableField,
'bar': self.nullableField}
self.test_class = TestObject
self.nonNullableField.get_schema = \
mock.Mock(return_value={'type': ['fake']})
self.nullableField.get_schema = \
mock.Mock(return_value={'type': ['fake', 'null']})
self.test_class.obj_name = mock.Mock(return_value='TestObject')
def test_to_json_schema(self):
schema = self.test_class.to_json_schema()
self.nonNullableField.get_schema.assert_called_once_with()
self.nullableField.get_schema.assert_called_once_with()
self.assertEqual({
'$schema': 'http://json-schema.org/draft-04/schema#',
'title': 'TestObject',
'type': 'object',
'properties': {
'versioned_object.namespace': {
'type': 'string'
},
'versioned_object.name': {
'type': 'string'
},
'versioned_object.version': {
'type': 'string'
},
'versioned_object.changes': {
'type': 'array',
'items': {
'type': 'string'
}
},
'versioned_object.data': {
'type': 'object',
'description': 'fields of TestObject',
'properties': {
'foo': {'type': ['fake']},
'bar': {'type': ['fake', 'null']}
},
'required': ['bar', 'foo'],
},
},
'required': ['versioned_object.namespace', 'versioned_object.name',
'versioned_object.version', 'versioned_object.data']
}, schema)
class TestNamespaceCompatibility(test.TestCase):
def setUp(self):
super(TestNamespaceCompatibility, self).setUp()
@base.VersionedObjectRegistry.register_if(False)
class TestObject(base.VersionedObject):
OBJ_SERIAL_NAMESPACE = 'foo'
OBJ_PROJECT_NAMESPACE = 'tests'
self.test_class = TestObject
def test_obj_primitive_key(self):
self.assertEqual('foo.data',
self.test_class._obj_primitive_key('data'))
def test_obj_primitive_field(self):
primitive = {
'foo.data': mock.sentinel.data,
}
self.assertEqual(mock.sentinel.data,
self.test_class._obj_primitive_field(primitive,
'data'))
def test_obj_primitive_field_namespace(self):
primitive = {
'foo.name': 'TestObject',
'foo.namespace': 'tests',
'foo.version': '1.0',
'foo.data': {},
}
with mock.patch.object(self.test_class, 'obj_class_from_name'):
self.test_class.obj_from_primitive(primitive)
def test_obj_primitive_field_namespace_wrong(self):
primitive = {
'foo.name': 'TestObject',
'foo.namespace': 'wrong',
'foo.version': '1.0',
'foo.data': {},
}
self.assertRaises(exception.UnsupportedObjectError,
self.test_class.obj_from_primitive, primitive)
class TestUtilityMethods(test.TestCase):
def test_flat(self):
@base.VersionedObjectRegistry.register
class TestObject(base.VersionedObject):
VERSION = '1.23'
fields = {}
tree = base.obj_tree_get_versions('TestObject')
self.assertEqual({'TestObject': '1.23'}, tree)
def test_parent_child(self):
@base.VersionedObjectRegistry.register
class TestChild(base.VersionedObject):
VERSION = '2.34'
@base.VersionedObjectRegistry.register
class TestObject(base.VersionedObject):
VERSION = '1.23'
fields = {
'child': fields.ObjectField('TestChild'),
}
tree = base.obj_tree_get_versions('TestObject')
self.assertEqual({'TestObject': '1.23',
'TestChild': '2.34'},
tree)
def test_complex(self):
@base.VersionedObjectRegistry.register
class TestChild(base.VersionedObject):
VERSION = '2.34'
@base.VersionedObjectRegistry.register
class TestChildTwo(base.VersionedObject):
VERSION = '4.56'
fields = {
'sibling': fields.ObjectField('TestChild'),
}
@base.VersionedObjectRegistry.register
class TestObject(base.VersionedObject):
VERSION = '1.23'
fields = {
'child': fields.ObjectField('TestChild'),
'childtwo': fields.ListOfObjectsField('TestChildTwo'),
}
tree = base.obj_tree_get_versions('TestObject')
self.assertEqual({'TestObject': '1.23',
'TestChild': '2.34',
'TestChildTwo': '4.56'},
tree)
def test_complex_loopy(self):
@base.VersionedObjectRegistry.register
class TestChild(base.VersionedObject):
VERSION = '2.34'
fields = {
'sibling': fields.ObjectField('TestChildTwo'),
}
@base.VersionedObjectRegistry.register
class TestChildTwo(base.VersionedObject):
VERSION = '4.56'
fields = {
'sibling': fields.ObjectField('TestChild'),
'parents': fields.ListOfObjectsField('TestObject'),
}
@base.VersionedObjectRegistry.register
class TestObject(base.VersionedObject):
VERSION = '1.23'
fields = {
'child': fields.ObjectField('TestChild'),
'childtwo': fields.ListOfObjectsField('TestChildTwo'),
}
tree = base.obj_tree_get_versions('TestObject')
self.assertEqual({'TestObject': '1.23',
'TestChild': '2.34',
'TestChildTwo': '4.56'},
tree)
|