summaryrefslogtreecommitdiffstats
path: root/snaps/openstack/tests/validation_utils.py
blob: 7c9bd7f026f0d14d6ba22ba8d7b360163fbcf336 (plain)
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
# Copyright (c) 2016 Cable Television Laboratories, Inc. ("CableLabs")
#                    and others.  All rights reserved.
#
# 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.
from neutronclient.v2_0.client import _DictWithMeta

__author__ = 'spisarski'


def objects_equivalent(obj1, obj2):
    """
    Returns true if both objects are equivalent
    :param obj1:
    :param obj2:
    :return: T/F
    """
    if obj1 is None and obj2 is None:
        return True
    if type(obj1) is dict or type(obj1) is _DictWithMeta:
        return dicts_equivalent(obj1, obj2)
    elif type(obj1) is list:
        return lists_equivalent(obj1, obj2)
    else:
        return obj1 == obj2


def dicts_equivalent(dict1, dict2):
    """
    Returns true when each key/value pair is equal
    :param dict1: dict 1
    :param dict2: dict 2
    :return: T/F
    """
    if (type(dict1) is dict or type(dict1) is _DictWithMeta) and (type(dict2) is dict or type(dict2) is _DictWithMeta):
        for key, value1 in dict1.iteritems():
            if not objects_equivalent(value1, dict2.get(key)):
                return False
        return True
    return False


def lists_equivalent(list1, list2):
    """
    Returns true when an item in list1 is also contained in list2
    :param list1: list 1
    :param list2: list 2
    :return: T/F
    """
    if len(list1) == len(list2) and type(list1) is list and type(list2) is list:
        for item1 in list1:
            has_equivalent = False
            for item2 in list2:
                has_equivalent = objects_equivalent(item1, item2)
                if has_equivalent:
                    break
            if not has_equivalent:
                return False
        return True
    return False