def test_get_my_ipv4_address_bad_process(self): def fake_execute(*args, **kwargs): raise processutils.ProcessExecutionError() self.stubs.Set(utils, 'execute', fake_execute) address = utils.get_my_ipv4_address() self.assertEqual(address, '127.0.0.1')
def test_get_my_ipv4_address_with_no_ipv4(self): response = """172.16.0.0/16 via 172.16.251.13 dev tun1 172.16.251.1 via 172.16.251.13 dev tun1 172.16.251.13 dev tun1 proto kernel scope link src 172.16.251.14 172.24.0.0/16 via 172.16.251.13 dev tun1 192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1""" def fake_execute(*args, **kwargs): return response, None self.stubs.Set(utils, 'execute', fake_execute) address = utils.get_my_ipv4_address() self.assertEqual(address, '127.0.0.1')
def _get_my_ip(): """Returns the actual ip of the local machine. This code figures out what source address would be used if some traffic were to be sent out to some well known address on the Internet. In this case, a Google DNS server is used, but the specific address does not matter much. No traffic is actually sent. """ try: csock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) csock.connect(('8.8.8.8', 80)) (addr, port) = csock.getsockname() csock.close() return addr except socket.error: return utils.get_my_ipv4_address()
def test_get_my_ipv4_address_with_single_interface(self): response_route = """default via 192.168.1.1 dev wlan0 proto static 192.168.1.0/24 dev wlan0 proto kernel scope link src 192.168.1.137 metric 9 """ response_addr = """ 1: lo inet 127.0.0.1/8 scope host lo 3: wlan0 inet 192.168.1.137/24 brd 192.168.1.255 scope global wlan0 """ def fake_execute(*args, **kwargs): if 'route' in args: return response_route, None return response_addr, None self.stubs.Set(utils, 'execute', fake_execute) address = utils.get_my_ipv4_address() self.assertEqual(address, '192.168.1.137')
def test_get_my_ipv4_address_with_multi_ipv4_on_single_interface(self): response_route = """ 172.18.56.0/24 dev customer proto kernel scope link src 172.18.56.22 169.254.0.0/16 dev customer scope link metric 1031 default via 172.18.56.1 dev customer """ response_addr = ("" "31: customer inet 172.18.56.22/24 brd 172.18.56.255 scope global" " customer\n" "31: customer inet 172.18.56.32/24 brd 172.18.56.255 scope global " "secondary customer") def fake_execute(*args, **kwargs): if 'route' in args: return response_route, None return response_addr, None self.stubs.Set(utils, 'execute', fake_execute) address = utils.get_my_ipv4_address() self.assertEqual(address, '172.18.56.22')
def test_get_my_ipv4_address_with_multiple_interfaces(self): response_route = """ 169.1.9.0/24 dev eth1 proto kernel scope link src 169.1.9.10 172.17.248.0/21 dev eth0 proto kernel scope link src 172.17.255.9 169.254.0.0/16 dev eth0 scope link metric 1002 169.254.0.0/16 dev eth1 scope link metric 1003 default via 172.17.248.1 dev eth0 proto static """ response_addr = """ 1: lo inet 127.0.0.1/8 scope host lo 2: eth0 inet 172.17.255.9/21 brd 172.17.255.255 scope global eth0 3: eth1 inet 169.1.9.10/24 scope global eth1 """ def fake_execute(*args, **kwargs): if 'route' in args: return response_route, None return response_addr, None self.stubs.Set(utils, 'execute', fake_execute) address = utils.get_my_ipv4_address() self.assertEqual(address, '172.17.255.9')