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 (
20 assert_regexp_matches,
23 from apex.network.ip_utils import (
31 ip4_pattern = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
32 ip4_range_pattern = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3},\d{1,'
33 '3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
36 def get_default_gateway_linux():
37 """Read the default gateway directly from /proc."""
38 with open("/proc/net/route") as fh:
40 fields = line.strip().split()
41 if fields[2] not in ('00000000', 'Gateway'):
48 """This method is run once for each class before any tests are run"""
49 cls.iface_name = get_default_gateway_linux()
50 iface = get_interface(cls.iface_name)
54 def teardown_class(cls):
55 """This method is run once for each class _after_ all tests are run"""
58 """This method is run once before _each_ test method is executed"""
61 """This method is run once after _each_ test method is executed"""
63 def test_get_interface(self):
64 assert_equal(get_interface(''), None)
65 assert_equal(get_interface('notreal'), None)
66 assert_is_instance(get_interface(self.iface_name,
69 # can't enable this until there's a v6 address on the ci hosts
70 # assert_is_instance(get_interface(
72 # address_family=6), IPv6Address)
73 assert_raises(IPUtilsException,
74 get_interface, self.iface_name, 0)
76 def test_find_gateway(self):
77 assert_is_instance(find_gateway(self.iface), str)
78 iface_virbr0 = get_interface('virbr0')
79 assert_equal(find_gateway(iface_virbr0), None)
81 def test_get_ip(self):
82 cidr = ipaddress.ip_network("10.10.10.0/24")
83 assert_equal(get_ip(1, cidr=cidr), "10.10.10.1")
84 assert_raises(IPUtilsException, get_ip, 1000, interface=self.iface)
85 assert_regexp_matches(get_ip(1, interface=self.iface), ip4_pattern)
86 assert_raises(IPUtilsException, get_ip, 1)
88 def test_get_ip_range_raises(self):
89 assert_raises(IPUtilsException, get_ip_range)
90 assert_raises(IPUtilsException, get_ip_range, interface=self.iface)
92 def test_get_ip_range_with_interface(self):
93 assert_regexp_matches(get_ip_range(interface=self.iface,
94 start_offset=1, end_offset=20),
96 assert_regexp_matches(get_ip_range(interface=self.iface,
97 start_offset=1, count=10),
99 assert_regexp_matches(get_ip_range(interface=self.iface, end_offset=20,
100 count=10), ip4_range_pattern)
102 def test_get_ip_range_with_cidr(self):
103 cidr = ip_network('10.10.10.0/24')
104 assert_raises(IPUtilsException, get_ip_range, cidr=cidr)
105 assert_regexp_matches(get_ip_range(cidr=cidr, start_offset=1,
106 end_offset=20), ip4_pattern)
107 assert_regexp_matches(get_ip_range(cidr=cidr, start_offset=1,
108 count=10), ip4_pattern)
109 assert_regexp_matches(get_ip_range(cidr=cidr, end_offset=20,
110 count=10), ip4_pattern)
112 def test__validate_ip_range(self):
113 cidr = ip_network('10.10.10.0/24')
114 assert_true(_validate_ip_range(
115 start_ip=ipaddress.IPv4Address('10.10.10.1'),
116 end_ip=ipaddress.IPv4Address('10.10.10.10'),
118 assert_false(_validate_ip_range(
119 start_ip=ipaddress.IPv4Address('10.10.10.10'),
120 end_ip=ipaddress.IPv4Address('10.10.10.1'),
122 assert_false(_validate_ip_range(
123 start_ip=ipaddress.IPv4Address('10.10.0.1'),
124 end_ip=ipaddress.IPv4Address('10.10.10.10'),
126 assert_false(_validate_ip_range(
127 start_ip=ipaddress.IPv4Address('10.10.10.1'),
128 end_ip=ipaddress.IPv4Address('10.10.11.10'),
131 def test_exception(self):
132 e = IPUtilsException("test")
134 assert_is_instance(e, IPUtilsException)