Merge "Print undercloud IP after deployment"
[apex.git] / tests / test_apex_ip_utils.py
1 ##############################################################################
2 # Copyright (c) 2016 Dan Radez (Red Hat)
3 #
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 ##############################################################################
9
10 import re
11
12 from apex.ip_utils import IPUtilsException
13 from apex.ip_utils import get_interface
14 from apex.ip_utils import find_gateway
15 from apex.ip_utils import get_ip
16 from apex.ip_utils import get_ip_range
17
18 from nose.tools import assert_equal
19 from nose.tools import assert_raises
20 from nose.tools import assert_is_instance
21 from nose.tools import assert_regexp_matches
22
23 from ipaddress import IPv4Address
24 from ipaddress import IPv6Address
25 from ipaddress import ip_network
26
27
28 ip4_pattern = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
29 ip4_range_pattern = re.compile('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3},\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
30
31 def get_default_gateway_linux():
32     """Read the default gateway directly from /proc."""
33     with open("/proc/net/route") as fh:
34         for line in fh:
35             fields = line.strip().split()
36             if fields[2] not in ('00000000', 'Gateway'):
37                 return fields[0]
38
39
40 class TestIpUtils(object):
41     @classmethod
42     def setup_class(klass):
43         """This method is run once for each class before any tests are run"""
44         klass.iface_name = get_default_gateway_linux()
45         iface = get_interface(klass.iface_name)
46         klass.iface = iface
47
48     @classmethod
49     def teardown_class(klass):
50         """This method is run once for each class _after_ all tests are run"""
51
52     def setUp(self):
53         """This method is run once before _each_ test method is executed"""
54
55     def teardown(self):
56         """This method is run once after _each_ test method is executed"""
57
58     def test_get_interface(self):
59         assert_equal(get_interface(''), None)
60         assert_equal(get_interface('notreal'), None)
61         assert_is_instance(get_interface(
62                                self.iface_name,
63                                address_family=4), IPv4Address)
64         assert_is_instance(get_interface(
65                                self.iface_name,
66                                address_family=6), IPv6Address)
67         assert_raises(IPUtilsException,
68                       get_interface, self.iface_name, 0)
69
70     def test_find_gateway(self):
71         assert_is_instance(find_gateway(self.iface), str)
72         iface_virbr0 = get_interface('virbr0')
73         assert_equal(find_gateway(iface_virbr0), None)
74
75     def test_get_ip(self):
76         assert_equal(get_ip(1, cidr="10.10.10.0/24"), "0")
77         assert_regexp_matches(get_ip(1, interface=self.iface), ip4_pattern)
78         assert_raises(IPUtilsException, get_ip, 1)
79
80
81     def test_get_ip_range_raises(self):
82         assert_raises(IPUtilsException, get_ip_range)
83         assert_raises(IPUtilsException, get_ip_range, interface=self.iface)
84
85     def test_get_ip_range_with_interface(self):
86         assert_regexp_matches(get_ip_range(interface=self.iface, start_offset=1, end_offset=20), ip4_range_pattern)
87         assert_regexp_matches(get_ip_range(interface=self.iface, start_offset=1, count=10), ip4_range_pattern)
88         assert_regexp_matches(get_ip_range(interface=self.iface, end_offset=20, count=10), ip4_range_pattern)
89
90     def test_get_ip_range_with_cidr(self):
91         cidr = ip_network('10.10.10.0/24')
92         assert_raises(IPUtilsException, get_ip_range, cidr=cidr)
93         assert_regexp_matches(get_ip_range(cidr=cidr, start_offset=1, end_offset=20), ip4_pattern)
94         assert_regexp_matches(get_ip_range(cidr=cidr, start_offset=1, count=10), ip4_pattern)
95         assert_regexp_matches(get_ip_range(cidr=cidr, end_offset=20, count=10), ip4_pattern)