Browse Source

Fixes dhcp checking with no interface provided

When running 'dhcpcheck discover' without --iface param,
there is an error 'NoneType object is not iterable'. This
patch fixes that error according '--ifaces' help message
and if '--ifaces' option not specified, dhcp check runs
discover for all interfaces except lo.

Change-Id: Iec4b01792d36a91ec9afca415bcc2fc602be1e00
Closes-Bug: #1396085
Signed-off-by: Ruslan Aliev <raliev@mirantis.com>
Ruslan Aliev 3 years ago
parent
commit
bf78100d2d

+ 4
- 1
dhcp_checker/api.py View File

@@ -16,6 +16,7 @@ import itertools
16 16
 import logging
17 17
 import time
18 18
 
19
+from dhcp_checker.utils import get_ifaces_exclude_lo
19 20
 from scapy import config as scapy_config
20 21
 
21 22
 scapy_config.use_pcap = True
@@ -70,12 +71,14 @@ def check_dhcp_on_eth(iface, timeout):
70 71
 def check_dhcp(ifaces, timeout=5, repeat=2):
71 72
     """Given list of ifaces. Process them in separate processes
72 73
 
73
-    @ifaces - lsit of ifaces
74
+    @ifaces - list of ifaces
74 75
     @timeout - timeout for scapy to wait for response
75 76
     @repeat - number of packets sended
76 77
     >>> check_dhcp(['eth1', 'eth2'])
77 78
     """
78 79
     config = {}
80
+    if not ifaces:
81
+        ifaces = get_ifaces_exclude_lo()
79 82
     for iface in ifaces:
80 83
         config[iface] = ()
81 84
     return check_dhcp_with_vlans(config, timeout=timeout, repeat=repeat)

+ 5
- 0
dhcp_checker/tests/system/tests.py View File

@@ -114,6 +114,11 @@ class TestMainFunctions(unittest.TestCase):
114 114
         result = api.check_dhcp(ifaces, repeat=3)
115 115
         self.assertEqual(len(list(result)), 2)
116 116
 
117
+    def test_with_no_ifaces(self):
118
+        ifaces = None
119
+        result = api.check_dhcp(ifaces, repeat=3)
120
+        self.assertEqual(len(list(result)), 2)
121
+
117 122
 
118 123
 if __name__ == '__main__':
119 124
     unittest.main()

+ 12
- 0
dhcp_checker/tests/unit/test_api.py View File

@@ -98,3 +98,15 @@ class TestDhcpApi(unittest.TestCase):
98 98
         self.assertEqual(sleep_mock.call_count, 3)
99 99
         make_listeners.assert_called_once_with(('eth0',))
100 100
         self.assertEqual(send_discover.call_count, 3)
101
+
102
+    @patch('dhcp_checker.api.utils.filtered_ifaces')
103
+    @patch('dhcp_checker.api.get_ifaces_exclude_lo')
104
+    @patch('dhcp_checker.api.send_dhcp_discover')
105
+    @patch('dhcp_checker.api.make_listeners')
106
+    def test_check_dhcp_with_no_ifaces(
107
+            self, make_listeners, send_discover, interfaces, filtered_ifaces):
108
+        interfaces.return_value = ['eth1']
109
+        filtered_ifaces.return_value = ['eth1']
110
+        api.check_dhcp(None, timeout=1, repeat=2)
111
+        make_listeners.assert_called_once_with(('eth1',))
112
+        self.assertEqual(send_discover.call_count, 2)

+ 5
- 0
dhcp_checker/tests/unit/test_utils.py View File

@@ -67,6 +67,11 @@ class TestDhcpUtils(unittest.TestCase):
67 67
         command_util().stderr.read.return_value = ''
68 68
         self.assertTrue(utils.check_iface_exist('eth0'))
69 69
 
70
+    @patch('dhcp_checker.utils.interfaces')
71
+    def test_get_ifaces_exclude_lo(self, interfaces):
72
+        interfaces.return_value = ['lo', 'eth1', 'eth2']
73
+        self.assertEqual(utils.get_ifaces_exclude_lo(), ['eth1', 'eth2'])
74
+
70 75
     def test_filter_duplicated_results(self):
71 76
         test_data = [{'first': 'value'}, {'first': 'value'}]
72 77
 

+ 8
- 0
dhcp_checker/utils.py View File

@@ -16,6 +16,7 @@ import re
16 16
 import subprocess
17 17
 import sys
18 18
 
19
+from netifaces import interfaces
19 20
 from scapy import all as scapy
20 21
 
21 22
 
@@ -71,6 +72,13 @@ def filtered_ifaces(ifaces):
71 72
                 yield iface
72 73
 
73 74
 
75
+def get_ifaces_exclude_lo():
76
+    ifaces = interfaces()
77
+    if 'lo' in ifaces:
78
+        ifaces.remove('lo')
79
+    return ifaces
80
+
81
+
74 82
 def pick_ip(range_start, range_end):
75 83
     """Given start_range, end_range generate list of ips
76 84
 

Loading…
Cancel
Save