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

Commit

Permalink
Fix tests broken by introducing DockerCPUEnvironment
Browse files Browse the repository at this point in the history
Setting up DockerCPUEnvironment in TaskComputer.__init__() requires two
conditions to be met:
  * Twisted reactor running (because prepare() returns a Deferred)
  * ClientConfigDescriptor with proper memory and CPU settings
  • Loading branch information
Wiezzel committed May 24, 2019
1 parent 8fe7bdd commit 6b2d253
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 43 deletions.
12 changes: 11 additions & 1 deletion tests/factories/taskserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@
from golem.task import taskserver


class ClientConfigDescriptor(factory.Factory):
class Meta:
model = clientconfigdescriptor.ClientConfigDescriptor

@factory.post_generation
def set_min_hardware_requirements(self, *_, **__):
self.max_memory_size = 1024 * 1024 # 1 GiB
self.num_cores = 1


class TaskServer(factory.Factory):
class Meta:
model = taskserver.TaskServer

node = factory.SubFactory(
'golem_messages.factories.datastructures.p2p.Node',
)
config_desc = clientconfigdescriptor.ClientConfigDescriptor()
config_desc = factory.SubFactory(ClientConfigDescriptor)
use_docker_manager = False


Expand Down
4 changes: 4 additions & 0 deletions tests/golem/docker/test_docker_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ def _get_test_task(self) -> Task:
task.max_pending_client_results = 5
return task

@patch('golem.envs.docker.cpu.deferToThread',
lambda f, *args, **kwargs: f(*args, **kwargs))
def _run_task(self, task: Task, timeout: int = 60 * 5, *_) \
-> Optional[DockerTaskThread]:
task_id = task.header.task_id
Expand All @@ -131,6 +133,8 @@ def _run_task(self, task: Task, timeout: int = 60 * 5, *_) \
self.node._run()

ccd = ClientConfigDescriptor()
ccd.max_memory_size = 1024 * 1024 # 1 GiB
ccd.num_cores = 1

with patch("golem.network.concent.handlers_library.HandlersLibrary"
".register_handler"):
Expand Down
6 changes: 5 additions & 1 deletion tests/golem/docker/test_docker_task_thread.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time
from threading import Thread
from unittest import TestCase
from unittest.mock import Mock
from unittest.mock import Mock, patch

from golem.clientconfigdescriptor import ClientConfigDescriptor
from golem.docker.image import DockerImage
Expand All @@ -22,9 +22,13 @@ def tearDown(self):
TestDockerJob.tearDown(self)
TestWithDatabase.tearDown(self)

@patch('golem.envs.docker.cpu.deferToThread',
lambda f, *args, **kwargs: f(*args, **kwargs))
def test_termination(self):
task_server = Mock()
task_server.config_desc = ClientConfigDescriptor()
task_server.config_desc.max_memory_size = 1024 * 1024 # 1 GiB
task_server.config_desc.num_cores = 1
task_server.client.datadir = self.test_dir
task_server.benchmark_manager = Mock()
task_server.benchmark_manager.benchmarks_needed.return_value = False
Expand Down
2 changes: 2 additions & 0 deletions tests/golem/network/concent/test_received_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ def test_reject_from_requestor(self, add_mock):
class TaskServerMessageHandlerTestBase(
testutils.DatabaseFixture, testutils.TestWithClient):

@mock.patch('golem.envs.docker.cpu.deferToThread',
lambda f, *args, **kwargs: f(*args, **kwargs))
def setUp(self):
# Avoid warnings caused by previous tests leaving handlers
library._handlers = {}
Expand Down
4 changes: 4 additions & 0 deletions tests/golem/network/p2p/test_peersession.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class TestPeerSession(testutils.DatabaseFixture, LogTestCase,
testutils.PEP8MixIn):
PEP8_FILES = ['golem/network/p2p/peersession.py', ]

@patch('golem.envs.docker.cpu.deferToThread',
lambda f, *args, **kwargs: f(*args, **kwargs))
def setUp(self):
super().setUp()
random.seed()
Expand Down Expand Up @@ -490,6 +492,8 @@ def test_send_remove_task(self, send_mock):
send_mock.assert_called()
assert isinstance(send_mock.call_args[0][0], message.p2p.RemoveTask)

@patch('golem.envs.docker.cpu.deferToThread',
lambda f, *args, **kwargs: f(*args, **kwargs))
def _gen_data_for_test_react_to_remove_task(self):
keys_auth = KeysAuth(self.path, 'priv_key', 'password')
previous_ka = self.peer_session.p2p_service.keys_auth
Expand Down
2 changes: 2 additions & 0 deletions tests/golem/task/dummy/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def create_client(datadir, node_name):
config_desc.key_difficulty = 0
config_desc.use_upnp = False
config_desc.node_name = node_name
config_desc.max_memory_size = 1024 * 1024 # 1 GiB
config_desc.num_cores = 1

from golem.core.keysauth import KeysAuth
with mock.patch.dict('ethereum.keys.PBKDF2_CONSTANTS', {'c': 1}):
Expand Down
2 changes: 2 additions & 0 deletions tests/golem/task/dummy/test_runner_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from tests.golem.task.dummy import runner, task


@mock.patch('golem.envs.docker.cpu.deferToThread',
lambda f, *args, **kwargs: f(*args, **kwargs))
class TestDummyTaskRunnerScript(DatabaseFixture):
"""Tests for the runner script"""

Expand Down
8 changes: 7 additions & 1 deletion tests/golem/task/test_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,16 @@ def setUp(self):
with mock.patch(
'golem.network.concent.handlers_library.HandlersLibrary'
'.register_handler',
), mock.patch(
'golem.envs.docker.cpu.deferToThread',
lambda f, *args, **kwargs: f(*args, **kwargs)
):
config_desc = clientconfigdescriptor.ClientConfigDescriptor()
config_desc.max_memory_size = 1024 * 1024 # 1 GiB
config_desc.num_cores = 1
self.client.task_server = taskserver.TaskServer(
node=dt_p2p_factory.Node(),
config_desc=clientconfigdescriptor.ClientConfigDescriptor(),
config_desc=config_desc,
client=self.client,
use_docker_manager=False,
apps_manager=self.client.apps_manager,
Expand Down
132 changes: 102 additions & 30 deletions tests/golem/task/test_taskcomputer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import random
from pathlib import Path
from threading import Lock
import time
import unittest.mock as mock
Expand All @@ -11,26 +12,33 @@
from golem.clientconfigdescriptor import ClientConfigDescriptor
from golem.core.common import timeout_to_deadline
from golem.core.deferred import sync_wait
from golem.docker.manager import DockerManager
from golem.task.taskcomputer import TaskComputer, PyTaskThread
from golem.testutils import DatabaseFixture
from golem.testutils import DatabaseFixture, TempDirFixture
from golem.tools.ci import ci_skip
from golem.tools.assertlogs import LogTestCase
from golem.tools.os_info import OSInfo
from tests.factories.taskserver import TaskServer as TaskServerFactory


@ci_skip
class TestTaskComputer(DatabaseFixture, LogTestCase):
class TestTaskComputerBase(DatabaseFixture, LogTestCase):

def setUp(self):
super(TestTaskComputer, self).setUp()
super().setUp()
task_server = mock.MagicMock()
task_server.benchmark_manager.benchmarks_needed.return_value = False
task_server.get_task_computer_root.return_value = self.path
task_server.config_desc = ClientConfigDescriptor()
task_server.config_desc.max_memory_size = 1024 * 1024 # 1 GiB
task_server.config_desc.num_cores = 1

self.task_server = task_server


@ci_skip
@mock.patch('golem.envs.docker.cpu.deferToThread',
lambda f, *args, **kwargs: f(*args, **kwargs))
class TestTaskComputer(TestTaskComputerBase):

def test_init(self):
task_server = self.task_server
tc = TaskComputer(task_server, use_docker_manager=False)
Expand Down Expand Up @@ -224,31 +232,6 @@ def test_host_state(self):
tc.counting_thread = mock.Mock()
self.assertEqual(tc.get_host_state(), "Computing")

def test_change_config(self):
task_server = self.task_server

tc = TaskComputer(task_server, use_docker_manager=False)
tc.docker_manager = mock.Mock(spec=DockerManager, hypervisor=None)

tc.use_docker_manager = False
tc.change_config(mock.Mock(), in_background=False)
assert not tc.docker_manager.update_config.called

tc.use_docker_manager = True

def _update_config(status_callback, *_, **__):
status_callback()
tc.docker_manager.update_config = _update_config

tc.change_config(mock.Mock(), in_background=False)

# pylint: disable=unused-argument
def _update_config_2(status_callback, done_callback, *_, **__):
done_callback(False)
tc.docker_manager.update_config = _update_config_2

tc.change_config(mock.Mock(), in_background=False)

def test_event_listeners(self):
client = mock.Mock()
task_server = self.task_server
Expand Down Expand Up @@ -333,9 +316,15 @@ def test_get_environment(self):

@ci_skip
class TestTaskThread(DatabaseFixture):

@mock.patch(
'golem.envs.docker.cpu.deferToThread',
lambda f, *args, **kwargs: f(*args, **kwargs))
def test_thread(self):
ts = mock.MagicMock()
ts.config_desc = ClientConfigDescriptor()
ts.config_desc.max_memory_size = 1024 * 1024 # 1 GiB
ts.config_desc.num_cores = 1
ts.benchmark_manager.benchmarks_needed.return_value = False
ts.get_task_computer_root.return_value = self.new_path

Expand Down Expand Up @@ -379,6 +368,10 @@ def _new_task_thread(self, task_computer):


class TestTaskMonitor(DatabaseFixture):

@mock.patch(
'golem.envs.docker.cpu.deferToThread',
lambda f, *args, **kwargs: f(*args, **kwargs))
def test_task_computed(self):
"""golem.monitor signal"""
from golem.monitor.model.nodemetadatamodel import NodeMetadataModel
Expand All @@ -400,6 +393,8 @@ def test_task_computed(self):
MONITOR_CONFIG)
task_server = mock.MagicMock()
task_server.config_desc = ClientConfigDescriptor()
task_server.config_desc.max_memory_size = 1024 * 1024 # 1 GiB
task_server.config_desc.num_cores = 1
task_server.benchmark_manager.benchmarks_needed.return_value = False
task_server.get_task_computer_root.return_value = self.new_path

Expand Down Expand Up @@ -450,3 +445,80 @@ def check(expected):
prepare()
task_thread.result = None
check(False)


@mock.patch('golem.task.taskcomputer.TaskComputer.change_docker_config')
class TestChangeConfig(TestTaskComputerBase):

@mock.patch('golem.envs.docker.cpu.deferToThread',
lambda f, *args, **kwargs: f(*args, **kwargs))
def setUp(self):
super().setUp()
self.task_computer = TaskComputer(self.task_server)

def test_root_path(self, change_docker_config):
self.task_server.get_task_computer_root.return_value = '/test'
config_desc = ClientConfigDescriptor()
self.task_computer.change_config(config_desc)
self.assertEqual(self.task_computer.dir_manager.root_path, '/test')
change_docker_config.assert_called_once_with(
config_desc=config_desc,
work_dir=Path('/test'),
run_benchmarks=False,
in_background=True
)

def test_task_request_frequency(self, _):
config_desc = ClientConfigDescriptor()
config_desc.task_request_interval = 100
self.task_computer.change_config(config_desc)
self.assertEqual(self.task_computer.task_request_frequency, 100)

def _test_compute_tasks(self, accept_tasks, in_shutdown, expected):
config_desc = ClientConfigDescriptor()
config_desc.accept_tasks = accept_tasks
config_desc.in_shutdown = in_shutdown
self.task_computer.change_config(config_desc)
self.assertEqual(self.task_computer.compute_tasks, expected)

def test_compute_tasks(self, _):
self._test_compute_tasks(
accept_tasks=True,
in_shutdown=True,
expected=False
)
self._test_compute_tasks(
accept_tasks=True,
in_shutdown=False,
expected=True
)
self._test_compute_tasks(
accept_tasks=False,
in_shutdown=True,
expected=False
)
self._test_compute_tasks(
accept_tasks=False,
in_shutdown=False,
expected=False
)

def test_not_in_background(self, change_docker_config):
config_desc = ClientConfigDescriptor()
self.task_computer.change_config(config_desc, in_background=False)
change_docker_config.assert_called_once_with(
config_desc=config_desc,
work_dir=mock.ANY,
run_benchmarks=False,
in_background=False
)

def test_run_benchmarks(self, change_docker_config):
config_desc = ClientConfigDescriptor()
self.task_computer.change_config(config_desc, run_benchmarks=True)
change_docker_config.assert_called_once_with(
config_desc=config_desc,
work_dir=mock.ANY,
run_benchmarks=True,
in_background=True
)
Loading

0 comments on commit 6b2d253

Please sign in to comment.