def test_header_serialization(self): node = Node(node_name="test node", pub_addr="10.10.10.10", pub_port=1023) task_header = TaskHeader( task_id="xyz", environment="DEFAULT", task_owner=node) # ignore dynamic properties task_header.fixed_header.last_checking = 0 task_header_dict = task_header.to_dict() serialized = CBORSerializer.dumps(task_header_dict) deserialized = CBORSerializer.loads(serialized) task_header_from_dict = TaskHeader.from_dict(deserialized) # ignore dynamic properties task_header_from_dict.fixed_header.last_checking = 0 assert task_header_from_dict.to_dict() == task_header_dict assert isinstance(task_header_from_dict.task_owner, Node) task_header_bin = task_header.to_binary() bin_serialized = CBORSerializer.dumps(task_header_bin) bin_deserialized = CBORSerializer.loads(bin_serialized) assert bin_deserialized == task_header_bin
def get_mock_task(task_id, subtask_id): task_mock = Mock() task_mock.header = TaskHeader.from_dict(get_example_task_header()) task_mock.header.task_id = task_id task_mock.header.max_price = 10000 task_mock.query_extra_data.return_value.ctd.task_id = task_id task_mock.query_extra_data.return_value.ctd.subtask_id = subtask_id return task_mock
def get_mock_task(key_gen="whatsoever", subtask_id="whatever"): task_mock = Mock() key_id = str.encode(key_gen) task_mock.header = TaskHeader.from_dict(get_example_task_header(key_id)) task_id = task_mock.header.task_id task_mock.header.max_price = 1010 task_mock.query_extra_data.return_value.ctd = ComputeTaskDef( task_id=task_id, subtask_id=subtask_id, ) return task_mock
def add_task_header(self, th_dict_repr: dict) -> bool: try: TaskHeader.validate(th_dict_repr) header = TaskHeader.from_dict(th_dict_repr) if not self.verify_header_sig(header): raise ValueError("Invalid signature") if self.task_manager.is_this_my_task(header): return True # Own tasks are not added to task keeper return self.task_keeper.add_task_header(header) except exceptions.TaskHeaderError as e: logger.warning("Wrong task header received: %s", e) return False except Exception: # pylint: disable=broad-except logger.exception("Task header validation failed") return False
def test_header_serialization(self): node = dict(node_name="test node") docker_images = [ dict(repository="repo_{}".format(i), id="id_{}".format(i), tag="tag_{}".format(i)) for i in xrange(4) ] task_header = TaskHeader("ABC", "xyz", "10.10.10.10", 1023, "key", "DEFAULT", task_owner=node, docker_images=docker_images) # ignore dynamic properties task_header.last_checking = 0 task_header_dict = task_header.to_dict() serialized = CBORSerializer.dumps(task_header_dict) deserialized = CBORSerializer.loads(serialized) task_header_from_dict = TaskHeader.from_dict(deserialized) # ignore dynamic properties task_header_from_dict.last_checking = 0 assert task_header_from_dict.to_dict() == task_header_dict assert isinstance(task_header_from_dict.task_owner, Node) assert all([ isinstance(di, DockerImage) for di in task_header_from_dict.docker_images ]) task_header_bin = task_header.to_binary() bin_serialized = CBORSerializer.dumps(task_header_bin) bin_deserialized = CBORSerializer.loads(bin_serialized) assert bin_deserialized == task_header_bin
def test_from_dict_no_subtasks_count(self): del self.th_dict_repr['fixed_header']['subtasks_count'] with mock.patch("golem.task.taskbase.logger.debug") as log_mock: TaskHeader.from_dict(self.th_dict_repr) log_mock.assert_called_once()
def get_task_header(key_id_seed="kkk"): th_dict_repr = get_dict_task_header(key_id_seed=key_id_seed) return TaskHeader.from_dict(th_dict_repr)