1 ##############################################################################
2 # Copyright (c) 2016 Dan Radez (Red Hat)
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
13 from apex.ip_utils import IPUtilsException
14 from apex.ip_utils import get_interface
15 from apex.ip_utils import find_gateway
16 from apex.ip_utils import get_ip
17 from apex.ip_utils import get_ip_range
18 from apex.ip_utils import _validate_ip_range
20 from nose.tools import assert_true
21 from nose.tools import assert_false
22 from nose.tools import assert_equal
23 from nose.tools import assert_raises
24 from nose.tools import assert_is_instance
25 from nose.tools import assert_regexp_matches
27 from ipaddress import IPv4Address
28 from ipaddress import IPv6Address
29 from ipaddress import ip_network
32 ip4_pattern = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
33 ip4_range_pattern = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3},\d{1,'
34 '3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
37 def get_default_gateway_linux():
38 """Read the default gateway directly from /proc."""
39 with open("/proc/net/route") as fh:
41 fields = line.strip().split()
42 if fields[2] not in ('00000000', 'Gateway'):
46 class TestIpUtils(object):
48 def setup_class(klass):
49 """This method is run once for each class before any tests are run"""
50 klass.iface_name = get_default_gateway_linux()
51 iface = get_interface(klass.iface_name)
55 def teardown_class(klass):
56 """This method is run once for each class _after_ all tests are run"""
59 """This method is run once before _each_ test method is executed"""
62 """This method is run once after _each_ test method is executed"""
64 def test_get_interface(self):
65 assert_equal(get_interface(''), None)
66 assert_equal(get_interface('notreal'), None)
67 assert_is_instance(get_interface(self.iface_name,
70 # can't enable this until there's a v6 address on the ci hosts
71 # assert_is_instance(get_interface(
73 # address_family=6), IPv6Address)
74 assert_raises(IPUtilsException,
75 get_interface, self.iface_name, 0)
77 def test_find_gateway(self):
78 assert_is_instance(find_gateway(self.iface), str)
79 iface_virbr0 = get_interface('virbr0')
80 assert_equal(find_gateway(iface_virbr0), None)
82 def test_get_ip(self):
83 cidr = ipaddress.ip_network("10.10.10.0/24")
84 assert_equal(get_ip(1, cidr=cidr), "10.10.10.1")
85 assert_raises(IPUtilsException, get_ip, 1000, interface=self.iface)
86 assert_regexp_matches(get_ip(1, interface=self.iface), ip4_pattern)
87 assert_raises(IPUtilsException, get_ip, 1)
89 def test_get_ip_range_raises(self):
90 assert_raises(IPUtilsException, get_ip_range)
91 assert_raises(IPUtilsException, get_ip_range, interface=self.iface)
93 def test_get_ip_range_with_interface(self):
94 assert_regexp_matches(get_ip_range(interface=self.iface,
95 start_offset=1, end_offset=20),
97 assert_regexp_matches(get_ip_range(interface=self.iface,
98 start_offset=1, count=10),
100 assert_regexp_matches(get_ip_range(interface=self.iface, end_offset=20,
101 count=10), ip4_range_pattern)
103 def test_get_ip_range_with_cidr(self):
104 cidr = ip_network('10.10.10.0/24')
105 assert_raises(IPUtilsException, get_ip_range, cidr=cidr)
106 assert_regexp_matches(get_ip_range(cidr=cidr, start_offset=1,
107 end_offset=20), ip4_pattern)
108 assert_regexp_matches(get_ip_range(cidr=cidr, start_offset=1,
109 count=10), ip4_pattern)
110 assert_regexp_matches(get_ip_range(cidr=cidr, end_offset=20,
111 count=10), ip4_pattern)
113 def test__validate_ip_range(self):
114 cidr = ip_network('10.10.10.0/24')
115 assert_true(_validate_ip_range(
116 start_ip=ipaddress.IPv4Address('10.10.10.1'),
117 end_ip=ipaddress.IPv4Address('10.10.10.10'),
119 assert_false(_validate_ip_range(
120 start_ip=ipaddress.IPv4Address('10.10.10.10'),
121 end_ip=ipaddress.IPv4Address('10.10.10.1'),
123 assert_false(_validate_ip_range(
124 start_ip=ipaddress.IPv4Address('10.10.0.1'),
125 end_ip=ipaddress.IPv4Address('10.10.10.10'),
127 assert_false(_validate_ip_range(
128 start_ip=ipaddress.IPv4Address('10.10.10.1'),
129 end_ip=ipaddress.IPv4Address('10.10.11.10'),
132 def test_exception(self):
133 e = IPUtilsException("test")
135 assert_is_instance(e, IPUtilsException)