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