Fixed vm instance instantiation from Heat when using nested resources
[snaps.git] / snaps / openstack / tests / validation_utils.py
1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 #                    and others.  All rights reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 from neutronclient.v2_0.client import _DictWithMeta
16
17 __author__ = 'spisarski'
18
19
20 def objects_equivalent(obj1, obj2):
21     """
22     Returns true if both objects are equivalent
23     :param obj1:
24     :param obj2:
25     :return: T/F
26     """
27     if obj1 is None and obj2 is None:
28         return True
29     if type(obj1) is dict or type(obj1) is _DictWithMeta:
30         return dicts_equivalent(obj1, obj2)
31     elif type(obj1) is list:
32         return lists_equivalent(obj1, obj2)
33     else:
34         return obj1 == obj2
35
36
37 def dicts_equivalent(dict1, dict2):
38     """
39     Returns true when each key/value pair is equal
40     :param dict1: dict 1
41     :param dict2: dict 2
42     :return: T/F
43     """
44     if (type(dict1) is dict or type(dict1) is _DictWithMeta) and (type(dict2) is dict or type(dict2) is _DictWithMeta):
45         for key, value1 in dict1.items():
46             if not objects_equivalent(value1, dict2.get(key)):
47                 return False
48         return True
49     return False
50
51
52 def lists_equivalent(list1, list2):
53     """
54     Returns true when an item in list1 is also contained in list2
55     :param list1: list 1
56     :param list2: list 2
57     :return: T/F
58     """
59     if len(list1) == len(list2) and type(list1) is list and type(list2) is list:
60         for item1 in list1:
61             has_equivalent = False
62             for item2 in list2:
63                 has_equivalent = objects_equivalent(item1, item2)
64                 if has_equivalent:
65                     break
66             if not has_equivalent:
67                 return False
68         return True
69     return False