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)
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)