Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Fixed DummyHypervisor port mapping for Windows #4876

Merged
merged 3 commits into from
Nov 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion golem/docker/hypervisor/dummy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import Optional, Dict, Tuple

from golem.core.common import is_osx
from golem.core.common import is_osx, is_windows
from golem.docker.client import local_client
from golem.docker.commands.docker_machine import DockerMachineCommandHandler
from golem.docker.config import DOCKER_VM_NAME
from golem.docker.hypervisor import Hypervisor


Expand Down Expand Up @@ -45,8 +47,14 @@ def get_port_mapping(self, container_id: str, port: int) -> Tuple[str, int]:
config = api_client.inspect_container(container_id)
net_config = config['NetworkSettings']

# TODO: Remove if-s when NonHypervisedDockerCPUEnvironment is removed
if is_osx():
ip_address = '127.0.0.1'
elif is_windows():
vm_ip = DockerMachineCommandHandler.run('ip', DOCKER_VM_NAME)
if vm_ip is None:
raise RuntimeError('Cannot retrieve Docker VM IP address')
ip_address = vm_ip
else:
ip_address = net_config['Networks']['bridge']['IPAddress']

Expand Down
19 changes: 15 additions & 4 deletions tests/golem/docker/test_hypervisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import Optional, Dict
from unittest import mock, TestCase

from golem.core.common import is_osx
from golem.core.common import is_osx, is_windows
from golem.docker.commands.docker_machine import DockerMachineCommandHandler
from golem.docker.config import DOCKER_VM_NAME as VM_NAME, DEFAULTS
from golem.docker.hypervisor.docker_for_mac import DockerForMac
Expand Down Expand Up @@ -483,9 +483,10 @@ def test_get_port_mapping(self, local_client):

class TestDummyHypervisor(TestCase):

@mock.patch('golem.docker.hypervisor.dummy.DockerMachineCommandHandler')
@mock.patch('golem.docker.hypervisor.dummy.local_client')
def test_get_port_mapping(self, local_client):
container_ip = '127.0.0.1' if is_osx() else '172.17.0.2'
def test_get_port_mapping(self, local_client, command_handler):
container_ip = '172.17.0.2'
local_client().inspect_container.return_value = {
'NetworkSettings': {
'Networks': {
Expand All @@ -500,7 +501,17 @@ def test_get_port_mapping(self, local_client):
}
}
}
vm_ip = '10.0.0.3'
command_handler.run.return_value = vm_ip

hypervisor = DummyHypervisor(mock.Mock())
host, port = hypervisor.get_port_mapping('container_id', 12345)
self.assertEqual(host, container_ip)

self.assertEqual(port, 12345)
if is_osx():
self.assertEqual(host, '127.0.0.1')
elif is_windows():
self.assertEqual(host, vm_ip)
command_handler.run.assert_called_once_with('ip', VM_NAME)
else:
self.assertEqual(host, container_ip)