From 02fa4a1fbfbbb843a02c9d1aeb45c250373d937c Mon Sep 17 00:00:00 2001 From: Kevin Benton Date: Fri, 16 Dec 2016 08:24:51 -0800 Subject: [PATCH] Ensure random object unique constraints aren't violated The unit test framework for objects generates random values for the objects it creates. We need to pay attention to previously generated values to avoid using the same value for data models that have unique constrains. Closes-Bug: #1650615 Change-Id: I0c54b0d82b8c15f2740cce5a850c8fa50acba536 --- neutron/tests/unit/objects/test_base.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/neutron/tests/unit/objects/test_base.py b/neutron/tests/unit/objects/test_base.py index 96909310481..c13d2720cfc 100644 --- a/neutron/tests/unit/objects/test_base.py +++ b/neutron/tests/unit/objects/test_base.py @@ -464,6 +464,7 @@ class _BaseObjectTestCase(object): # make sure all objects are loaded and registered in the registry utils.import_modules_recursively(os.path.dirname(objects.__file__)) self.context = context.get_admin_context() + self._unique_tracker = collections.defaultdict(set) self.db_objs = [ self._test_class.db_model(**self.get_random_db_fields()) for _ in range(3) @@ -503,6 +504,14 @@ class _BaseObjectTestCase(object): if field not in obj_cls.synthetic_fields: generator = FIELD_TYPE_VALUE_GENERATOR_MAP[type(field_obj)] fields[field] = get_value(generator, ip_version) + for keys in obj_cls.unique_keys: + keytup = tuple(keys) + unique_values = tuple(fields[k] for k in keytup) + if unique_values in self._unique_tracker[keytup]: + # if you get a recursion depth error here, it means + # your random generator didn't generate unique values + return self.get_random_object_fields(obj_cls) + self._unique_tracker[keytup].add(unique_values) return fields def get_random_db_fields(self, obj_cls=None):