testing/mozbase/moznetwork/tests/test.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/mozbase/moznetwork/tests/test.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,78 @@
     1.4 +#!/usr/bin/env python
     1.5 +"""
     1.6 +Unit-Tests for moznetwork
     1.7 +"""
     1.8 +
     1.9 +import mock
    1.10 +import mozinfo
    1.11 +import moznetwork
    1.12 +import re
    1.13 +import subprocess
    1.14 +import unittest
    1.15 +
    1.16 +
    1.17 +def verify_ip_in_list(ip):
    1.18 +    """
    1.19 +    Helper Method to check if `ip` is listed in Network Adresses
    1.20 +    returned by ipconfig/ifconfig, depending on the platform in use
    1.21 +
    1.22 +    :param ip: IPv4 address in the xxx.xxx.xxx.xxx format as a string
    1.23 +                Example Usage:
    1.24 +                    verify_ip_in_list('192.168.0.1')
    1.25 +
    1.26 +    returns True if the `ip` is in the list of IPs in ipconfig/ifconfig
    1.27 +    """
    1.28 +
    1.29 +    # Regex to match IPv4 addresses.
    1.30 +    # 0-255.0-255.0-255.0-255, note order is important here.
    1.31 +    regexip = re.compile("((25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){3}"
    1.32 +                              "(25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)")
    1.33 +
    1.34 +    if mozinfo.isLinux or mozinfo.isMac or mozinfo.isBsd:
    1.35 +        args = ["ifconfig"]
    1.36 +
    1.37 +    if mozinfo.isWin:
    1.38 +        args = ["ipconfig"]
    1.39 +
    1.40 +    ps = subprocess.Popen(args, stdout=subprocess.PIPE)
    1.41 +    standardoutput, standarderror = ps.communicate()
    1.42 +
    1.43 +    # Generate a list of IPs by parsing the output of ip/ifconfig
    1.44 +    ip_list = [x.group() for x in re.finditer(regexip, standardoutput)]
    1.45 +
    1.46 +    # Check if ip is in list
    1.47 +    if ip in ip_list:
    1.48 +        return True
    1.49 +    else:
    1.50 +        return False
    1.51 +
    1.52 +
    1.53 +class TestGetIP(unittest.TestCase):
    1.54 +
    1.55 +    def test_get_ip(self):
    1.56 +        """ Attempt to test the IP address returned by
    1.57 +        moznetwork.get_ip() is valid """
    1.58 +
    1.59 +        ip = moznetwork.get_ip()
    1.60 +
    1.61 +        # Check the IP returned by moznetwork is in the list
    1.62 +        self.assertTrue(verify_ip_in_list(ip))
    1.63 +
    1.64 +    def test_get_ip_using_get_interface(self):
    1.65 +        """ Test that the control flow path for get_ip() using
    1.66 +        _get_interface_list() is works """
    1.67 +
    1.68 +        if mozinfo.isLinux or mozinfo.isMac:
    1.69 +
    1.70 +            with mock.patch('socket.gethostbyname') as byname:
    1.71 +                # Force socket.gethostbyname to return None
    1.72 +                byname.return_value = None
    1.73 +
    1.74 +                ip = moznetwork.get_ip()
    1.75 +
    1.76 +                # Check the IP returned by moznetwork is in the list
    1.77 +                self.assertTrue(verify_ip_in_list(ip))
    1.78 +
    1.79 +
    1.80 +if __name__ == '__main__':
    1.81 +    unittest.main()

mercurial