def test_get_task(self): tk = TaskHeaderKeeper(EnvironmentsManager(), 10) self.assertIsNone(tk.get_task()) task_header = get_dict_task_header() task_header["task_id"] = "uvw" self.assertTrue(tk.add_task_header(task_header)) self.assertIsNone(tk.get_task()) e = Environment() e.accept_tasks = True tk.environments_manager.add_environment(e) task_header["task_id"] = "xyz" self.assertTrue(tk.add_task_header(task_header)) th = tk.get_task() assert isinstance(th.task_owner, Node) self.assertEqual(task_header["task_id"], th.task_id) self.assertEqual(task_header["max_price"], th.max_price) self.assertEqual(task_header["node_name"], th.node_name) self.assertEqual(task_header["task_owner_port"], th.task_owner_port) self.assertEqual(task_header["task_owner_key_id"], th.task_owner_key_id) self.assertEqual(task_header["environment"], th.environment) self.assertEqual(task_header["deadline"], th.deadline) self.assertEqual(task_header["subtask_timeout"], th.subtask_timeout) self.assertEqual(task_header["max_price"], th.max_price) self.assertEqual(task_header["task_id"], th.task_id)
def test_task_header_update(self): e = Environment() e.accept_tasks = True tk = TaskHeaderKeeper(environments_manager=EnvironmentsManager(), node=p2p.Node(), min_price=10) tk.environments_manager.add_environment(e) task_header = get_task_header() task_id = task_header.task_id task_header.fixed_header.deadline = timeout_to_deadline(10) task_header.fixed_header.update_checksum() assert tk.add_task_header(task_header) assert task_id in tk.supported_tasks assert tk.add_task_header(task_header) assert task_id in tk.supported_tasks task_header = copy.deepcopy(task_header) task_header.fixed_header.max_price = 1 task_header.fixed_header.update_checksum() # An attempt to update fixed header should *not* succeed assert not tk.add_task_header(task_header) assert task_id in tk.supported_tasks tk.task_headers = {} tk.supported_tasks = [] assert tk.add_task_header(task_header) assert task_id not in tk.supported_tasks
def test_is_supported(self): tk = TaskHeaderKeeper(EnvironmentsManager(), 10.0) self.assertFalse(tk.is_supported({})) task = {"environment": Environment.get_id(), 'max_price': 0} self.assertFalse(tk.is_supported(task)) e = Environment() e.accept_tasks = True tk.environments_manager.add_environment(e) self.assertFalse(tk.is_supported(task)) task["max_price"] = 10.0 self.assertFalse(tk.is_supported(task)) task["min_version"] = APP_VERSION self.assertTrue(tk.is_supported(task)) task["max_price"] = 10.5 self.assertTrue(tk.is_supported(task)) config_desc = Mock() config_desc.min_price = 13.0 tk.change_config(config_desc) self.assertFalse(tk.is_supported(task)) config_desc.min_price = 10.0 tk.change_config(config_desc) self.assertTrue(tk.is_supported(task)) task["min_version"] = "120" self.assertFalse(tk.is_supported(task)) task["min_version"] = tk.app_version self.assertTrue(tk.is_supported(task)) task["min_version"] = "abc" with self.assertLogs(logger=logger, level=1): self.assertFalse(tk.is_supported(task))
def test_get_performance(self): env = Environment() perf = 6666.6 cfg_desc = ClientConfigDescriptor() cfg_desc.estimated_performance = perf result = env.get_performance(cfg_desc) self.assertTrue(result == perf)
def test_old_tasks(frozen_time, _): # pylint: disable=no-self-argument tk = TaskHeaderKeeper(environments_manager=EnvironmentsManager(), node=p2p.Node(), min_price=10) e = Environment() e.accept_tasks = True tk.environments_manager.add_environment(e) task_header = get_task_header() task_header.fixed_header.deadline = timeout_to_deadline(10) assert tk.add_task_header(task_header) task_id = task_header.task_id task_header2 = get_task_header("abc") task_header2.fixed_header.deadline = timeout_to_deadline(1) task_id2 = task_header2.task_id assert tk.add_task_header(task_header2) assert tk.task_headers.get(task_id2) is not None assert tk.task_headers.get(task_id) is not None assert tk.removed_tasks.get(task_id2) is None assert tk.removed_tasks.get(task_id) is None assert len(tk.supported_tasks) == 2 frozen_time.tick(timedelta(seconds=1.1)) # pylint: disable=no-member tk.remove_old_tasks() assert tk.task_headers.get(task_id2) is None assert tk.task_headers.get(task_id) is not None assert tk.removed_tasks.get(task_id2) is not None assert tk.removed_tasks.get(task_id) is None assert len(tk.supported_tasks) == 1 assert tk.supported_tasks[0] == task_id
def test_task_header_update(self): e = Environment() e.accept_tasks = True tk = TaskHeaderKeeper(EnvironmentsManager(), 10) tk.environments_manager.add_environment(e) assert not tk.add_task_header(dict()) task_header = get_dict_task_header() task_id = task_header["task_id"] task_header["deadline"] = timeout_to_deadline(10) assert tk.add_task_header(task_header) assert task_id in tk.supported_tasks assert tk.add_task_header(task_header) assert task_id in tk.supported_tasks task_header["max_price"] = 1 assert tk.add_task_header(task_header) assert task_id not in tk.supported_tasks tk.task_headers = {} tk.supported_tasks = [] task_header["max_price"] = 1 assert tk.add_task_header(task_header) assert task_id not in tk.supported_tasks task_header['task_id'] = "newtaskID" task_header['deadline'] = "WRONG DEADLINE" assert not tk.add_task_header(task_header)
def test_change_config(self): tk = TaskHeaderKeeper(EnvironmentsManager(), 10.0) e = Environment() e.accept_tasks = True tk.environments_manager.add_environment(e) task_header = get_dict_task_header() task_header["max_price"] = 9.0 tk.add_task_header(task_header) self.assertNotIn("xyz", tk.supported_tasks) self.assertIsNotNone(tk.task_headers["xyz"]) task_header["task_id"] = "abc" task_header["max_price"] = 10.0 tk.add_task_header(task_header) self.assertIn("abc", tk.supported_tasks) self.assertIsNotNone(tk.task_headers["abc"]) config_desc = Mock() config_desc.min_price = 10.0 tk.change_config(config_desc) self.assertNotIn("xyz", tk.supported_tasks) self.assertIn("abc", tk.supported_tasks) config_desc.min_price = 8.0 tk.change_config(config_desc) self.assertIn("xyz", tk.supported_tasks) self.assertIn("abc", tk.supported_tasks) config_desc.min_price = 11.0 tk.change_config(config_desc) self.assertNotIn("xyz", tk.supported_tasks) self.assertNotIn("abc", tk.supported_tasks)
def test_is_supported(self): em = EnvironmentsManager() em.environments = {} em.support_statuses = {} tk = TaskHeaderKeeper(environments_manager=EnvironmentsManager(), node=p2p.Node(), min_price=10.0) header = get_task_header() header.fixed_header.environment = None header.fixed_header.max_price = None header.fixed_header.min_version = None self.assertFalse(tk.check_support(header)) header.fixed_header.environment = Environment.get_id() header.fixed_header.max_price = 0 supported = tk.check_support(header) self.assertFalse(supported) self.assertIn(UnsupportReason.ENVIRONMENT_MISSING, supported.desc) e = Environment() e.accept_tasks = True tk.environments_manager.add_environment(e) supported = tk.check_support(header) self.assertFalse(supported) self.assertIn(UnsupportReason.MAX_PRICE, supported.desc) header.fixed_header.max_price = 10.0 supported = tk.check_support(header) self.assertFalse(supported) self.assertIn(UnsupportReason.APP_VERSION, supported.desc) header.fixed_header.min_version = golem.__version__ self.assertTrue(tk.check_support(header)) header.fixed_header.max_price = 10.0 self.assertTrue(tk.check_support(header)) config_desc = mock.Mock() config_desc.min_price = 13.0 tk.change_config(config_desc) self.assertFalse(tk.check_support(header)) config_desc.min_price = 10.0 tk.change_config(config_desc) self.assertTrue(tk.check_support(header)) header.fixed_header.min_version = "120" self.assertFalse(tk.check_support(header)) header.fixed_header.min_version = tk.app_version self.assertTrue(tk.check_support(header)) header.fixed_header.min_version = "abc" with self.assertLogs(logger=logger, level='WARNING'): self.assertFalse(tk.check_support(header))
def __init__(self, docker_images): """ :param list(DockerImage) docker_images: nonempty list of Docker images, at least one should be available for this environment to be supported. :return: """ if docker_images is None: raise AttributeError("docker_images is None") self.docker_images = docker_images Environment.__init__(self) self.software.append('Docker')
def test_change_config(self, tar): tk = TaskHeaderKeeper(environments_manager=EnvironmentsManager(), node=p2p.Node(), min_price=10.0, task_archiver=tar) e = Environment() e.accept_tasks = True tk.environments_manager.add_environment(e) task_header = get_task_header() task_id = task_header.task_id task_header.fixed_header.max_price = 9.0 tk.add_task_header(task_header) self.assertNotIn(task_id, tk.supported_tasks) self.assertIn(task_id, tk.task_headers) task_header = get_task_header("abc") task_id2 = task_header.task_id task_header.fixed_header.max_price = 10.0 tk.add_task_header(task_header) self.assertIn(task_id2, tk.supported_tasks) self.assertIn(task_id2, tk.task_headers) config_desc = mock.Mock() config_desc.min_price = 10.0 tk.change_config(config_desc) self.assertNotIn(task_id, tk.supported_tasks) self.assertIn(task_id2, tk.supported_tasks) config_desc.min_price = 8.0 tk.change_config(config_desc) self.assertIn(task_id, tk.supported_tasks) self.assertIn(task_id2, tk.supported_tasks) config_desc.min_price = 11.0 tk.change_config(config_desc) self.assertNotIn(task_id, tk.supported_tasks) self.assertNotIn(task_id2, tk.supported_tasks) # Make sure the tasks stats are properly archived tar.reset_mock() config_desc.min_price = 9.5 tk.change_config(config_desc) self.assertNotIn(task_id, tk.supported_tasks) self.assertIn(task_id2, tk.supported_tasks) tar.add_support_status.assert_any_call( task_id, SupportStatus(False, {UnsupportReason.MAX_PRICE: 9.0})) tar.add_support_status.assert_any_call(task_id2, SupportStatus(True, {}))
def test_get_environment_by_id(self): em = EnvironmentsManager() env1 = Environment() env2 = Environment() env3 = Environment() env1.get_id = lambda: "Env1" env2.get_id = lambda: "Env2" env3.get_id = lambda: "Env3" em.add_environment(env1) em.add_environment(env2) em.add_environment(env3) self.assertTrue(env1 == em.get_environment_by_id("Env1")) self.assertTrue(env2 == em.get_environment_by_id("Env2")) self.assertTrue(env3 == em.get_environment_by_id("Env3"))
def test_get_performance(self): # given perf_value = 6666.6 perf = Performance(environment_id=Environment.get_id(), value=perf_value) perf.save() # then self.assertEqual(self.env.get_performance(), perf_value)
def test_benchmarks_not_needed_when_results_saved(self): # given for env_id in self.b.benchmarks: Performance.update_or_create(env_id, 100) Performance.update_or_create(DefaultEnvironment.get_id(), 3) # then assert not self.b.benchmarks_needed()
def description(self): descr = Environment.description(self) descr += "DOCKER IMAGES (any of):\n" for img in self.docker_images: descr += "\t * " + img.name + "\n" descr += "\n" return descr
def test_get_min_accepted_performance(self): # given p = Performance(environment_id=Environment.get_id(), min_accepted_step=100) p.save() MinPerformanceMultiplier.set(3.141) # then self.assertEqual(MinPerformanceMultiplier.get(), 3.141) self.assertEqual(self.env.get_min_accepted_performance(), 314.1)
def run_benchmark_for_env_id(self, env_id, callback, errback): if env_id == DefaultEnvironment.get_id(): self.run_default_benchmark(callback, errback) else: benchmark_data = self.benchmarks.get(env_id) if benchmark_data: self.run_benchmark(benchmark_data[0], benchmark_data[1], env_id, callback, errback) else: raise Exception("Unknown environment: {}".format(env_id))
def run_all_benchmarks(self, success=None, error=None): logger.info('Running all benchmarks with num_cores=%r', self.task_server.client.config_desc.num_cores) def run_non_default_benchmarks(_performance=None): self.run_benchmarks(copy(self.benchmarks), success, error) if DefaultEnvironment.get_id() not in self.get_saved_benchmarks_ids(): # run once in lifetime, since it's for single CPU core self.run_default_benchmark(run_non_default_benchmarks, error) else: run_non_default_benchmarks()
def __init__(self, tag=None, image_id=None, additional_images: List[DockerImage] = None): if tag is None: tag = self.DOCKER_TAG image = DockerImage(image_id=image_id) if image_id \ else DockerImage(self.DOCKER_IMAGE, tag=tag) Environment.__init__(self) self.main_program_file = find_task_script(self.APP_DIR, self.SCRIPT_NAME) self.docker_images = [image] if additional_images: self.docker_images += additional_images if self.SHORT_DESCRIPTION: self.short_description = self.SHORT_DESCRIPTION
def test_get_task(self): em = EnvironmentsManager() em.environments = {} em.support_statuses = {} tk = TaskHeaderKeeper(environments_manager=em, node=p2p.Node(), min_price=10) self.assertIsNone(tk.get_task()) task_header = get_task_header("uvw") self.assertTrue(tk.add_task_header(task_header)) self.assertIsNone(tk.get_task()) e = Environment() e.accept_tasks = True tk.environments_manager.add_environment(e) task_header2 = get_task_header("xyz") self.assertTrue(tk.add_task_header(task_header2)) th = tk.get_task() assert isinstance(th.task_owner, Node) self.assertEqual(task_header2.to_dict(), th.to_dict())
def test_check_software(self): env = Environment() assert not env.check_software() env.allow_custom_main_program_file = True assert env.check_software() env.allow_custom_main_program_file = False file_name = path.join(self.path, "mainprogramfile") env.main_program_file = file_name with open(file_name, 'w') as f: f.write("PROGRAM CODE") assert env.check_software()
def test_task_header_update_stats(self, tar): e = Environment() e.accept_tasks = True tk = TaskHeaderKeeper(environments_manager=EnvironmentsManager(), node=p2p.Node(), min_price=10, task_archiver=tar) tk.environments_manager.add_environment(e) task_header = get_task_header("good") assert tk.add_task_header(task_header) tar.add_task.assert_called_with(mock.ANY) task_id = task_header.task_id tar.add_support_status.assert_any_call(task_id, SupportStatus(True, {})) tar.reset_mock() task_header2 = get_task_header("bad") task_id2 = task_header2.task_id task_header2.fixed_header.max_price = 1.0 assert tk.add_task_header(task_header2) tar.add_task.assert_called_with(mock.ANY) tar.add_support_status.assert_any_call( task_id2, SupportStatus(False, {UnsupportReason.MAX_PRICE: 1.0}))
def test_get_source_code(self): env = Environment() assert env.get_source_code() is None file_name = path.join(self.path, "mainprogramfile") env.main_program_file = file_name assert env.get_source_code() is None with open(file_name, 'w') as f: f.write("PROGRAM CODE") env.main_program_file = file_name assert env.get_source_code() == "PROGRAM CODE"
def test_old_tasks(self): tk = TaskHeaderKeeper(EnvironmentsManager(), 10) e = Environment() e.accept_tasks = True tk.environments_manager.add_environment(e) task_header = get_dict_task_header() task_header["deadline"] = timeout_to_deadline(10) assert tk.add_task_header(task_header) task_header["deadline"] = timeout_to_deadline(1) task_header["task_id"] = "abc" assert tk.add_task_header(task_header) assert tk.task_headers.get("abc") is not None assert tk.task_headers.get("xyz") is not None assert tk.removed_tasks.get("abc") is None assert tk.removed_tasks.get("xyz") is None assert len(tk.supported_tasks) == 2 time.sleep(1.1) tk.remove_old_tasks() assert tk.task_headers.get("abc") is None assert tk.task_headers.get("xyz") is not None assert tk.removed_tasks.get("abc") is not None assert tk.removed_tasks.get("xyz") is None assert len(tk.supported_tasks) == 1 assert tk.supported_tasks[0] == "xyz"
class EnvTest(DatabaseFixture, PEP8MixIn): PEP8_FILES = ["golem/environments/environment.py"] def setUp(self): super().setUp() self.env = Environment() def test_get_performance(self): # given perf_value = 6666.6 perf = Performance(environment_id=Environment.get_id(), value=perf_value) perf.save() # then self.assertEqual(self.env.get_performance(), perf_value) def test_get_source_code(self): # check defaults assert self.env.get_source_code() is None # given file_name = path.join(self.path, "mainprogramfile") self.env.main_program_file = file_name # then assert self.env.get_source_code() is None # re-given with open(file_name, 'w') as f: f.write("PROGRAM CODE") # then self.env.main_program_file = file_name assert self.env.get_source_code() == "PROGRAM CODE" def test_run_default_benchmark(self): assert Environment.get_performance() == 0.0 assert Environment.run_default_benchmark(save=True) > 0.0 assert Environment.get_performance() > 0.0 def test_get_min_accepted_performance_default(self): self.assertEqual(MinPerformanceMultiplier.get(), 0.0) self.assertEqual(self.env.get_min_accepted_performance(), 0.0) def test_get_min_accepted_performance(self): # given p = Performance(environment_id=Environment.get_id(), min_accepted_step=100) p.save() MinPerformanceMultiplier.set(3.141) # then self.assertEqual(MinPerformanceMultiplier.get(), 3.141) self.assertEqual(self.env.get_min_accepted_performance(), 314.1)
def test_run_non_default_benchmarks(self, br_mock, mpt_mock, *_): # given Performance.update_or_create(DefaultEnvironment.get_id(), -7) def _run(): # call success callback with performance = call_count * 100 br_mock.assert_called() success_callback = br_mock.call_args[1].get('success_callback') assert callable(success_callback) return success_callback(br_mock.call_count * 100) br_mock.return_value.run.side_effect = _run # when self.b.run_all_benchmarks() # then assert mpt_mock.call_count == 0 assert br_mock.call_count == len(self.b.benchmarks) for idx, env_id in enumerate(reversed(list(self.b.benchmarks))): assert (1 + idx) * 100 == \ Performance.get(Performance.environment_id == env_id).value
def __init__(self): self.environment = Environment() self.name = ''
def test_load_config_manager(self): mgr = EnvironmentsManager() mgr.environments.add(Environment()) mgr.load_config(self.path) assert mgr.env_config
def benchmarks_needed(self): if self.benchmarks: ids = self.get_saved_benchmarks_ids() return not set(self.benchmarks.keys() | {DefaultEnvironment.get_id()}).issubset(ids) return False
def supported(self): return self.check_docker_images() and Environment.supported(self)
def check_support(self) -> SupportStatus: return self.check_docker_images().join(Environment.check_support(self))