Merge lp:~alisonken1/openlp/windows_lo_fix into lp:openlp

Proposed by Ken Roberts
Status: Merged
Merged at revision: 2825
Proposed branch: lp:~alisonken1/openlp/windows_lo_fix
Merge into: lp:openlp
Diff against target: 128 lines (+91/-2)
2 files modified
openlp/core/common/__init__.py (+13/-2)
tests/functional/openlp_core/common/test_network_interfaces.py (+78/-0)
To merge this branch: bzr merge lp:~alisonken1/openlp/windows_lo_fix
Reviewer Review Type Date Requested Status
Tomas Groth Approve
Tim Bentley Approve
Review via email: mp+352333@code.launchpad.net

Commit message

Fix windows not naming localhost interface to lo

Description of the change

- Rename localhost to lo if found in iterfaces
- Add check for lo not in interface list before trying to remove lo
- Add extra log debug info
- Add test to check for get_local_ip4() returning no interfaces

--------------------------------------------------------------------------------
lp:~alisonken1/openlp/windows_lo_fix (revision 2825)
https://ci.openlp.io/job/Branch-01-Pull/2562/ [SUCCESS]
https://ci.openlp.io/job/Branch-02a-Linux-Tests/2460/ [SUCCESS]
https://ci.openlp.io/job/Branch-02b-macOS-Tests/242/ [SUCCESS]
https://ci.openlp.io/job/Branch-03a-Build-Source/149/ [SUCCESS]
https://ci.openlp.io/job/Branch-03b-Build-macOS/135/ [SUCCESS]
https://ci.openlp.io/job/Branch-04a-Code-Analysis/1611/ [SUCCESS]
https://ci.openlp.io/job/Branch-04b-Test-Coverage/1424/ [SUCCESS]
https://ci.openlp.io/job/Branch-05-AppVeyor-Tests/330/ [FAILURE]

To post a comment you must log in.
Revision history for this message
Tim Bentley (trb143) wrote :

Looks Ok to me.

review: Approve
Revision history for this message
Tomas Groth (tomasgroth) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'openlp/core/common/__init__.py'
2--- openlp/core/common/__init__.py 2018-04-17 19:26:18 +0000
3+++ openlp/core/common/__init__.py 2018-08-03 22:40:28 +0000
4@@ -64,13 +64,17 @@
5 log.debug('Getting local IPv4 interface(es) information')
6 my_ip4 = {}
7 for iface in QNetworkInterface.allInterfaces():
8+ log.debug('Checking for isValid and flags == IsUP | IsRunning')
9 if not iface.isValid() or not (iface.flags() & (QNetworkInterface.IsUp | QNetworkInterface.IsRunning)):
10 continue
11+ log.debug('Checking address(es) protocol')
12 for address in iface.addressEntries():
13 ip = address.ip()
14 # NOTE: Next line will skip if interface is localhost - keep for now until we decide about it later
15 # if (ip.protocol() == QAbstractSocket.IPv4Protocol) and (ip != QHostAddress.LocalHost):
16+ log.debug('Checking for protocol == IPv4Protocol')
17 if ip.protocol() == QAbstractSocket.IPv4Protocol:
18+ log.debug('Getting interface information')
19 my_ip4[iface.name()] = {'ip': ip.toString(),
20 'broadcast': address.broadcast().toString(),
21 'netmask': address.netmask().toString(),
22@@ -79,14 +83,21 @@
23 ip.toIPv4Address()).toString()
24 }
25 log.debug('Adding {iface} to active list'.format(iface=iface.name()))
26+ if 'localhost' in my_ip4:
27+ log.debug('Renaming windows localhost to lo')
28+ my_ip4['lo'] = my_ip4['localhost']
29+ my_ip4.pop('localhost')
30+ if len(my_ip4) == 0:
31+ log.warning('No active IPv4 network interfaces detected')
32 if len(my_ip4) == 1:
33 if 'lo' in my_ip4:
34 # No active interfaces - so leave localhost in there
35 log.warning('No active IPv4 interfaces found except localhost')
36 else:
37 # Since we have a valid IP4 interface, remove localhost
38- log.debug('Found at least one IPv4 interface, removing localhost')
39- my_ip4.pop('lo')
40+ if 'lo' in my_ip4:
41+ log.debug('Found at least one IPv4 interface, removing localhost')
42+ my_ip4.pop('lo')
43
44 return my_ip4
45
46
47=== added file 'tests/functional/openlp_core/common/test_network_interfaces.py'
48--- tests/functional/openlp_core/common/test_network_interfaces.py 1970-01-01 00:00:00 +0000
49+++ tests/functional/openlp_core/common/test_network_interfaces.py 2018-08-03 22:40:28 +0000
50@@ -0,0 +1,78 @@
51+# -*- coding: utf-8 -*-
52+# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
53+
54+###############################################################################
55+# OpenLP - Open Source Lyrics Projection #
56+# --------------------------------------------------------------------------- #
57+# Copyright (c) 2008-2017 OpenLP Developers #
58+# --------------------------------------------------------------------------- #
59+# This program is free software; you can redistribute it and/or modify it #
60+# under the terms of the GNU General Public License as published by the Free #
61+# Software Foundation; version 2 of the License. #
62+# #
63+# This program is distributed in the hope that it will be useful, but WITHOUT #
64+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
65+# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
66+# more details. #
67+# #
68+# You should have received a copy of the GNU General Public License along #
69+# with this program; if not, write to the Free Software Foundation, Inc., 59 #
70+# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
71+###############################################################################
72+"""
73+Functional tests to test calls for network interfaces.
74+"""
75+from unittest import TestCase
76+from unittest.mock import MagicMock, call, patch
77+
78+import openlp.core.common
79+from openlp.core.common import get_local_ip4
80+
81+from tests.helpers.testmixin import TestMixin
82+
83+lo_address_attrs = {'isValid.return_value': True,
84+ 'flags.return_value': True,
85+ 'InterfaceFlags.return_value': True,
86+ 'name.return_value': 'lo',
87+ 'broadcast.toString.return_value': '127.0.0.255',
88+ 'netmask.toString.return_value': '255.0.0.0',
89+ 'prefixLength.return_value': 8,
90+ 'ip.protocol.return_value': True}
91+
92+
93+class TestInterfaces(TestCase, TestMixin):
94+ """
95+ A test suite to test out functions/methods that use network interface(s).
96+ """
97+ def setUp(self):
98+ """
99+ Create an instance and a few example actions.
100+ """
101+ self.build_settings()
102+
103+ self.ip4_lo_address = MagicMock()
104+ self.ip4_lo_address.configure_mock(**lo_address_attrs)
105+
106+ def tearDown(self):
107+ """
108+ Clean up
109+ """
110+ self.destroy_settings()
111+
112+ @patch.object(openlp.core.common, 'log')
113+ def test_ip4_no_interfaces(self, mock_log):
114+ """
115+ Test no interfaces available
116+ """
117+ # GIVEN: Test environment
118+ call_warning = [call('No active IPv4 network interfaces detected')]
119+
120+ with patch('openlp.core.common.QNetworkInterface') as mock_newtork_interface:
121+ mock_newtork_interface.allInterfaces.return_value = []
122+
123+ # WHEN: get_local_ip4 is called
124+ ifaces = get_local_ip4()
125+
126+ # THEN: There should not be any interfaces detected
127+ assert not ifaces, 'There should have been no active interfaces'
128+ mock_log.warning.assert_has_calls(call_warning)