test_iputil.py: fix and improve tests on Windows.

Test all platform variants (Unix, Windows, Cygwin) on each platform.

Signed-off-by: Daira Hopwood <david-sarah@jacaranda.org>
This commit is contained in:
Daira Hopwood 2013-06-26 16:44:05 +01:00
parent f97b8e5e1d
commit b088380736
2 changed files with 52 additions and 13 deletions

View File

@ -1,5 +1,5 @@
import re, errno, subprocess, os, sys import re, errno, subprocess, os
from twisted.trial import unittest from twisted.trial import unittest
@ -60,6 +60,33 @@ wlan0 Link encap:Ethernet HWaddr 90:f6:52:27:15:0a \n\
RX bytes:3916475942 (3.6 GiB) TX bytes:458353654 (437.1 MiB) RX bytes:3916475942 (3.6 GiB) TX bytes:458353654 (437.1 MiB)
""" """
# This is actually from a VirtualBox VM running XP.
MOCK_ROUTE_OUTPUT = """\
===========================================================================
Interface List
0x1 ........................... MS TCP Loopback interface
0x2 ...08 00 27 c3 80 ad ...... AMD PCNET Family PCI Ethernet Adapter - Packet Scheduler Miniport
===========================================================================
===========================================================================
Active Routes:
Network Destination Netmask Gateway Interface Metric
0.0.0.0 0.0.0.0 10.0.2.2 10.0.2.15 20
10.0.2.0 255.255.255.0 10.0.2.15 10.0.2.15 20
10.0.2.15 255.255.255.255 127.0.0.1 127.0.0.1 20
10.255.255.255 255.255.255.255 10.0.2.15 10.0.2.15 20
127.0.0.0 255.0.0.0 127.0.0.1 127.0.0.1 1
224.0.0.0 240.0.0.0 10.0.2.15 10.0.2.15 20
255.255.255.255 255.255.255.255 10.0.2.15 10.0.2.15 1
Default Gateway: 10.0.2.2
===========================================================================
Persistent Routes:
None
"""
UNIX_TEST_ADDRESSES = set(["127.0.0.1", "192.168.0.6", "192.168.0.2", "192.168.0.10"])
WINDOWS_TEST_ADDRESSES = set(["127.0.0.1", "10.0.2.15", "192.168.0.10"])
CYGWIN_TEST_ADDRESSES = set(["127.0.0.1", "192.168.0.10"])
class FakeProcess: class FakeProcess:
def __init__(self, output, err): def __init__(self, output, err):
@ -84,7 +111,7 @@ class ListAddresses(testutil.SignalMixin, unittest.TestCase):
# David A.'s OpenSolaris box timed out on this test one time when it was at 2s. # David A.'s OpenSolaris box timed out on this test one time when it was at 2s.
test_list_async.timeout=4 test_list_async.timeout=4
def _test_list_async_mock(self, command, output): def _test_list_async_mock(self, command, output, expected):
ns = Namespace() ns = Namespace()
ns.first = True ns.first = True
@ -111,18 +138,28 @@ class ListAddresses(testutil.SignalMixin, unittest.TestCase):
return "192.168.0.10" return "192.168.0.10"
self.patch(iputil, 'get_local_ip_for', call_get_local_ip_for) self.patch(iputil, 'get_local_ip_for', call_get_local_ip_for)
def call_which(name):
return [name]
self.patch(iputil, 'which', call_which)
d = iputil.get_local_addresses_async() d = iputil.get_local_addresses_async()
def _check(addresses): def _check(addresses):
if sys.platform == "cygwin": self.failUnlessEquals(set(addresses), set(expected))
expected = set(["127.0.0.1", "192.168.0.10"])
else:
expected = set(["127.0.0.1", "192.168.0.6", "192.168.0.2", "192.168.0.10"])
self.failUnlessEquals(set(addresses), expected)
d.addCallbacks(_check) d.addCallbacks(_check)
return d return d
def test_list_async_mock_ip_addr(self): def test_list_async_mock_ip_addr(self):
return self._test_list_async_mock("ip", MOCK_IPADDR_OUTPUT) self.patch(iputil, 'platform', "linux2")
return self._test_list_async_mock("ip", MOCK_IPADDR_OUTPUT, UNIX_TEST_ADDRESSES)
def test_list_async_mock_ifconfig(self): def test_list_async_mock_ifconfig(self):
return self._test_list_async_mock("ifconfig", MOCK_IFCONFIG_OUTPUT) self.patch(iputil, 'platform', "linux2")
return self._test_list_async_mock("ifconfig", MOCK_IFCONFIG_OUTPUT, UNIX_TEST_ADDRESSES)
def test_list_async_mock_route(self):
self.patch(iputil, 'platform', "win32")
return self._test_list_async_mock("route.exe", MOCK_ROUTE_OUTPUT, WINDOWS_TEST_ADDRESSES)
def test_list_async_mock_cygwin(self):
self.patch(iputil, 'platform', "cygwin")
return self._test_list_async_mock(None, None, CYGWIN_TEST_ADDRESSES)

View File

@ -1,5 +1,7 @@
# from the Python Standard Library # from the Python Standard Library
import os, re, socket, sys, subprocess, errno import os, re, socket, subprocess, errno
from sys import platform
# from Twisted # from Twisted
from twisted.internet import defer, threads, reactor from twisted.internet import defer, threads, reactor
@ -84,7 +86,7 @@ def get_local_addresses_async(target="198.41.0.4"): # A.ROOT-SERVERS.NET
if local_ip is not None: if local_ip is not None:
addresses.append(local_ip) addresses.append(local_ip)
if sys.platform == "cygwin": if platform == "cygwin":
d = _cygwin_hack_find_addresses() d = _cygwin_hack_find_addresses()
else: else:
d = _find_addresses_via_config() d = _find_addresses_via_config()
@ -161,7 +163,7 @@ def _synchronously_find_addresses_via_config():
# originally by Greg Smith, hacked by Zooko and then Daira # originally by Greg Smith, hacked by Zooko and then Daira
# We don't reach here for cygwin. # We don't reach here for cygwin.
if sys.platform == 'win32': if platform == 'win32':
commands = _win32_commands commands = _win32_commands
else: else:
commands = _unix_commands commands = _unix_commands
@ -205,7 +207,7 @@ def _query(path, args, regex):
for outline in outputsplit: for outline in outputsplit:
m = regex.match(outline) m = regex.match(outline)
if m: if m:
addr = m.groupdict()['address'] addr = m.group('address')
if addr not in addresses: if addr not in addresses:
addresses.append(addr) addresses.append(addr)