Debian Codegraph @

neutron_lib/tests/tools.py

raw
1 # Copyright (c) 2013 NEC Corporation
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # 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, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 import os
17 import platform
18 import random
19 import time
20
21 from debtcollector import removals
22 import netaddr
23
24 from neutron_lib.utils import helpers
25 from neutron_lib.utils import net
26
27
28 class UnorderedList(list):
29 """A list that is equals to any permutation of itself."""
30
31 def __eq__(self, other):
32 if not isinstance(other, list):
33 return False
34 return (sorted(self, key=helpers.safe_sort_key) ==
35 sorted(other, key=helpers.safe_sort_key))
36
37 def __neq__(self, other):
38 return not self == other
39
40
41 @removals.remove
42 def is_bsd():
43 """Return True on BSD-based systems."""
44
45 system = platform.system()
46 if system == 'Darwin':
47 return True
48 if 'bsd' in system.lower():
49 return True
50 return False
51
52
53 def get_random_cidr(version=4):
54 if version == 4:
55 return '10.%d.%d.0/%d' % (random.randint(3, 254),
56 random.randint(3, 254),
57 24)
58 return '2001:db8:%x::/%d' % (random.getrandbits(16), 64)
59
60
61 def reset_random_seed():
62 # reset random seed to make sure other processes extracting values from RNG
63 # don't get the same results (useful especially when you then use the
64 # random values to allocate system resources from global pool, like ports
65 # to listen). Use both current time and pid to make sure no tests started
66 # at the same time get the same values from RNG
67 seed = time.time() + os.getpid()
68 random.seed(seed)
69
70
71 def get_random_EUI():
72 return netaddr.EUI(
73 net.get_random_mac(['fe', '16', '3e', '00', '00', '00'])
74 )
75
76
77 def get_random_ip_network(version=4):
78 return netaddr.IPNetwork(get_random_cidr(version=version))