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 ##############################################################################
12 from ipaddress import IPv4Address
13 from ipaddress import ip_network
15 from nose.tools import assert_equal
16 from nose.tools import assert_false
17 from nose.tools import assert_is_instance
18 from nose.tools import assert_raises
19 from nose.tools import assert_regexp_matches
20 from nose.tools import assert_true
22 from apex.network.ip_utils import IPUtilsException
23 from apex.network.ip_utils import _validate_ip_range
24 from apex.network.ip_utils import find_gateway
25 from apex.network.ip_utils import get_interface
26 from apex.network.ip_utils import get_ip
27 from apex.network.ip_utils import get_ip_range
29 ip4_pattern = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
30 ip4_range_pattern = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3},\d{1,'
31 '3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
34 def get_default_gateway_linux():
35 """Read the default gateway directly from /proc."""
36 with open("/proc/net/route") as fh:
38 fields = line.strip().split()
39 if fields[2] not in ('00000000', 'Gateway'):
43 class TestIpUtils(object):
45 def setup_class(klass):
46 """This method is run once for each class before any tests are run"""
47 klass.iface_name = get_default_gateway_linux()
48 iface = get_interface(klass.iface_name)
52 def teardown_class(klass):
53 """This method is run once for each class _after_ all tests are run"""
56 """This method is run once before _each_ test method is executed"""
59 """This method is run once after _each_ test method is executed"""
61 def test_get_interface(self):
62 assert_equal(get_interface(''), None)
63 assert_equal(get_interface('notreal'), None)
64 assert_is_instance(get_interface(self.iface_name,
67 # can't enable this until there's a v6 address on the ci hosts
68 # assert_is_instance(get_interface(
70 # address_family=6), IPv6Address)
71 assert_raises(IPUtilsException,
72 get_interface, self.iface_name, 0)
74 def test_find_gateway(self):
75 assert_is_instance(find_gateway(self.iface), str)
76 iface_virbr0 = get_interface('virbr0')
77 assert_equal(find_gateway(iface_virbr0), None)
79 def test_get_ip(self):
80 cidr = ipaddress.ip_network("10.10.10.0/24")
81 assert_equal(get_ip(1, cidr=cidr), "10.10.10.1")
82 assert_raises(IPUtilsException, get_ip, 1000, interface=self.iface)
83 assert_regexp_matches(get_ip(1, interface=self.iface), ip4_pattern)
84 assert_raises(IPUtilsException, get_ip, 1)
86 def test_get_ip_range_raises(self):
87 assert_raises(IPUtilsException, get_ip_range)
88 assert_raises(IPUtilsException, get_ip_range, interface=self.iface)
90 def test_get_ip_range_with_interface(self):
91 assert_regexp_matches(get_ip_range(interface=self.iface,
92 start_offset=1, end_offset=20),
94 assert_regexp_matches(get_ip_range(interface=self.iface,
95 start_offset=1, count=10),
97 assert_regexp_matches(get_ip_range(interface=self.iface, end_offset=20,
98 count=10), ip4_range_pattern)
100 def test_get_ip_range_with_cidr(self):
101 cidr = ip_network('10.10.10.0/24')
102 assert_raises(IPUtilsException, get_ip_range, cidr=cidr)
103 assert_regexp_matches(get_ip_range(cidr=cidr, start_offset=1,
104 end_offset=20), ip4_pattern)
105 assert_regexp_matches(get_ip_range(cidr=cidr, start_offset=1,
106 count=10), ip4_pattern)
107 assert_regexp_matches(get_ip_range(cidr=cidr, end_offset=20,
108 count=10), ip4_pattern)
110 def test__validate_ip_range(self):
111 cidr = ip_network('10.10.10.0/24')
112 assert_true(_validate_ip_range(
113 start_ip=ipaddress.IPv4Address('10.10.10.1'),
114 end_ip=ipaddress.IPv4Address('10.10.10.10'),
116 assert_false(_validate_ip_range(
117 start_ip=ipaddress.IPv4Address('10.10.10.10'),
118 end_ip=ipaddress.IPv4Address('10.10.10.1'),
120 assert_false(_validate_ip_range(
121 start_ip=ipaddress.IPv4Address('10.10.0.1'),
122 end_ip=ipaddress.IPv4Address('10.10.10.10'),
124 assert_false(_validate_ip_range(
125 start_ip=ipaddress.IPv4Address('10.10.10.1'),
126 end_ip=ipaddress.IPv4Address('10.10.11.10'),
129 def test_exception(self):
130 e = IPUtilsException("test")
132 assert_is_instance(e, IPUtilsException)