def test_update_peers_view(self): logic = self.logic gui = self.app logic.customizer = MainWindowCustomizer(gui.main_window, logic) logic.customizer.new_task_dialog_customizer = Mock() peer = Mock() peer.address = "10.10.10.10" peer.port = 1031 peer.key_id = "KEYID" peer.node_name = "NODE 1" peer2 = Mock() peer2.address = "10.10.10.20" peer2.port = 1034 peer2.key_id = "KEYID2" peer2.node_name = "NODE 2" logic._update_peers_view( [DictSerializer.dump(peer), DictSerializer.dump(peer2)]) table = logic.customizer.gui.ui.connectedPeersTable self.assertEqual(table.rowCount(), 2) self.assertEqual(table.item(0, 0).text(), "10.10.10.10") self.assertEqual(table.item(1, 0).text(), "10.10.10.20") self.assertEqual(table.item(0, 1).text(), "1031") self.assertEqual(table.item(1, 1).text(), "1034") self.assertEqual(table.item(0, 2).text(), "KEYID") self.assertEqual(table.item(1, 2).text(), "KEYID2") self.assertEqual(table.item(0, 3).text(), "NODE 1") self.assertEqual(table.item(1, 3).text(), "NODE 2")
def from_dict(dictionary): th = DictSerializer.load(dictionary, as_class=TaskHeader) th.last_checking = time.time() if isinstance(th.task_owner, dict): th.task_owner = DictSerializer.load(th.task_owner, as_class=Node) if hasattr(th, 'docker_images') and th.docker_images is not None: for i, di in enumerate(th.docker_images): if isinstance(di, dict): th.docker_images[i] = DictSerializer.load( di, as_class=DockerImage) return th
def test_properties(self): obj = MockSerializationSubject() dict_repr = DictSerializer.dump(obj) self.assertTrue('property_1' in dict_repr) self.assertTrue('property_2' in dict_repr) self.assertFalse('_property_3' in dict_repr) self.assertFalse('method_1' in dict_repr) self.assertFalse('_method_2' in dict_repr) deserialized = DictSerializer.load(dict_repr) assert_properties(deserialized, obj)
def test_serialization_result(self): obj = MockSerializationSubject() self.assertEqual( DictSerializer.dump(obj), { 'property_1': { 'k': 'v', 'u': { 'property_1': obj.property_1['u'].property_1, 'property_3': 'string', 'property_4': [ 'list', 'of', ('items', ), obj.property_1['u'].property_4[-1] ], DictCoder.cls_key: ('tests.golem.core.test_simpleserializer' '.MockSerializationInnerSubject') } }, 'property_2': { 'property_1': obj.property_2.property_1, 'property_3': 'string', 'property_4': ['list', 'of', ('items', ), obj.property_2.property_4[-1]], DictCoder.cls_key: ('tests.golem.core.test_simpleserializer' '.MockSerializationInnerSubject') }, 'property_4': [ 'v', 1, (1, 2, 3), { 'property_1': obj.property_4[-1].property_1, 'property_3': 'string', 'property_4': [ 'list', 'of', ('items', ), obj.property_4[-1].property_4[-1] ], DictCoder.cls_key: ('tests.golem.core.test_simpleserializer' '.MockSerializationInnerSubject') } ], DictCoder.cls_key: ('tests.golem.core.test_simpleserializer' '.MockSerializationSubject') }) self.assertFalse( DictCoder.cls_key in DictSerializer.dump(obj, typed=False))
def test_serialization_result(self): obj = MockSerializationSubject() self.assertEqual( DictSerializer.dump(obj), { u'property_1': { u'k': u'v', u'u': { u'property_1': obj.property_1[u'u'].property_1, u'property_3': u'string', u'property_4': [ u'list', u'of', (u'items', ), obj.property_1[u'u'].property_4[-1] ], DictCoder.cls_key: u'test_simpleserializer.MockSerializationInnerSubject' } }, u'property_2': { u'property_1': obj.property_2.property_1, u'property_3': u'string', u'property_4': [ u'list', u'of', (u'items', ), obj.property_2.property_4[-1] ], DictCoder.cls_key: u'test_simpleserializer.MockSerializationInnerSubject' }, u'property_4': [ u'v', 1, (1, 2, 3), { u'property_1': obj.property_4[-1].property_1, u'property_3': u'string', u'property_4': [ u'list', u'of', (u'items', ), obj.property_4[-1].property_4[-1] ], DictCoder.cls_key: u'test_simpleserializer.MockSerializationInnerSubject' } ], DictCoder.cls_key: u'test_simpleserializer.MockSerializationSubject' }) self.assertFalse( DictCoder.cls_key in DictSerializer.dump(obj, typed=False))
def create_task(self, t_dict): try: task = DictSerializer.load(t_dict) new_task = self.enqueue_new_task(task) return unicode(new_task.header.task_id) except Exception: log.exception("Cannot create task {}".format(t_dict))
def test_command_formatter(self): formatter = CommandFormatter() for prettify in [True, False]: formatter.prettify = prettify self.assertIsNone(formatter.format(None)) self.assertIsNone(formatter.format('')) self.assertEqual(formatter.format('Some text'), 'Some text') if not prettify: self.assertEqual(formatter.format(formatter), DictSerializer.dump(formatter, typed=False)) table_headers = ['First', 'Second', 'Third'] table_values = [ ['value 1', 'value 2', 'value 3'], ['value 1', 'value 2', 'value 3'], ['value 1', 'value 2', 'value 3'], ] tabular_result = CommandResult.to_tabular(table_headers, table_values) tabular_repr = formatter.format(tabular_result) tabular_data_repr = formatter.format(tabular_result.from_tabular()) self.assertIsNotNone(tabular_repr) self.assertIsNotNone(tabular_data_repr) self.assertNotEqual(tabular_data_repr, tabular_repr)
def _get_test_task_definition(cls) -> TaskDefinition: task_path = Path(__file__).parent / cls.TASK_FILE with open(task_path) as f: golem_path = get_golem_path() json_str = f.read().replace('$GOLEM_DIR', Path(golem_path).as_posix()) return DictSerializer.load(json.loads(json_str))
def from_dict(dictionary: dict) -> 'TaskHeader': th: TaskHeader = DictSerializer.load(dictionary, as_class=TaskHeader) if isinstance(th.fixed_header, dict): th.fixed_header = TaskFixedHeader.from_dict(th.fixed_header) if isinstance(th.mask, dict): th.mask = Mask.from_dict(th.mask) return th
def test_load(self): client = self.client task_file_name = self._create_blender_task(client.get_dir_manager()) def run_success(instance): instance.success_callback() def run_error(instance): instance.error_callback() with client_ctx(Tasks, client): with self._run_context(run_success): client.create_task.call_args = None client.create_task.called = False tasks = Tasks() tasks.load(task_file_name, True) call_args = client.create_task.call_args[0] assert len(call_args) == 1 assert isinstance(DictSerializer.load(call_args[0]), BlenderRenderTask) with self._run_context(run_error): client.create_task.call_args = None client.create_task.called = False tasks = Tasks() tasks.load(task_file_name, True) call_args = client.create_task.call_args[0] assert len(call_args) == 1 assert isinstance(DictSerializer.load(call_args[0]), BlenderRenderTask) with self._run_context(run_error): client.create_task.call_args = None client.create_task.called = False tasks = Tasks() with self.assertRaises(CommandException): tasks.load(task_file_name, False)
def load(self, file_name, skip_test): try: definition = self.__read_from_file(file_name) except Exception as exc: return CommandResult( error="Error reading task from file '{}': {}".format( file_name, exc)) if hasattr(definition, 'resources'): definition.resources = { os.path.normpath(res) for res in definition.resources } datadir = sync_wait(Tasks.client.get_datadir()) # TODO: unify GUI and CLI logic rendering_task_state = TaskDesc() rendering_task_state.definition = definition rendering_task_state.task_state.status = TaskStatus.starting if not Tasks.application_logic: Tasks.application_logic = CommandAppLogic.instantiate( Tasks.client, datadir) task_builder = Tasks.application_logic.get_builder( rendering_task_state) task = Task.build_task(task_builder) rendering_task_state.task_state.outputs = task.get_output_names() rendering_task_state.task_state.total_subtasks = task.get_total_tasks() task.header.task_id = str(uuid.uuid4()) if not skip_test: test_task = Task.build_task(task_builder) test_task.header.task_id = str(uuid.uuid4()) queue = Queue() TaskTester(test_task, datadir, success_callback=lambda *a, **kw: queue.put(True), error_callback=lambda *a, **kw: queue.put(a)).run() test_result = queue.get() if test_result is not True: return CommandResult( error="Test failed: {}".format(test_result)) task_dict = DictSerializer.dump(task) task_def = task_dict['task_definition'] task_def['resources'] = list(task_def.get('task_definition', [])) deferred = Tasks.client.create_task(task_dict) return sync_wait(deferred, timeout=1800)
def test_create_task(self, *_): c = self.client c.enqueue_new_task = Mock() # create a task t = Task(TaskHeader("node_name", "task_id", "10.10.10.10", 123, "owner_id", "DEFAULT"), src_code="print('hello')") c.create_task(DictSerializer.dump(t)) self.assertTrue(c.enqueue_new_task.called)
def format(self, result): result, result_type = self._initial_format(result) if result_type != CommandResult.NONE: if result_type == CommandResult.TABULAR: result = dict(headers=result[0], values=result[1]) else: result = DictSerializer.dump(result, typed=False) if self.prettify: return json.dumps(result, indent=4, sort_keys=True) return json.dumps(result)
def test_serialization_as_class(self): obj = MockSerializationSubject() dict_repr = DictSerializer.dump(obj) self.assertTrue(DictCoder.cls_key in dict_repr) self.assertTrue('property_1' in dict_repr) self.assertTrue('property_2' in dict_repr) self.assertTrue( isinstance(DictSerializer.load(dict_repr), MockSerializationSubject)) dict_repr = DictSerializer.dump(obj, typed=False) self.assertFalse(DictCoder.cls_key in dict_repr) self.assertTrue('property_1' in dict_repr) self.assertTrue('property_2' in dict_repr) self.assertTrue(isinstance(DictSerializer.load(dict_repr), dict)) self.assertTrue( isinstance( DictSerializer.load(dict_repr, as_class=MockSerializationSubject), MockSerializationSubject))
def _run_test_task(self, t_dict): def on_success(*args, **kwargs): self.task_tester = None self._publish(Task.evt_task_test_status, TaskTestStatus.success, *args, **kwargs) def on_error(*args, **kwargs): self.task_tester = None self._publish(Task.evt_task_test_status, TaskTestStatus.error, *args, **kwargs) t = DictSerializer.load(t_dict) self.task_tester = TaskTester(t, self.datadir, on_success, on_error) self.task_tester.run() self._publish(Task.evt_task_test_status, TaskTestStatus.started, True)
def test_node(self, *_): c = self.client self.assertIsInstance(c.get_node(), dict) self.assertIsInstance(DictSerializer.load(c.get_node()), Node) self.assertIsInstance(c.get_node_key(), unicode) self.assertIsNotNone(c.get_node_key()) c.node.key = None self.assertNotIsInstance(c.get_node_key(), unicode) self.assertIsNone(c.get_node_key()) self.assertIsInstance(c.get_public_key(), bytes) self.assertEqual(c.get_public_key(), c.keys_auth.public_key)
def build_and_serialize_task(self, task_state, cbk=None): tb = self.get_builder(task_state) t = Task.build_task(tb) t.header.max_price = str(t.header.max_price) t_serialized = DictSerializer.dump(t) if 'task_definition' in t_serialized: t_serialized_def = t_serialized['task_definition'] t_serialized_def['resources'] = list(t_serialized_def['resources']) if 'max_price' in t_serialized_def: t_serialized_def['max_price'] = str( t_serialized_def['max_price']) from pprint import pformat logger.debug('task serialized: %s', pformat(t_serialized)) if cbk: cbk(t) return t_serialized
def from_dict(dictionary) -> 'TaskFixedHeader': if 'subtasks_count' not in dictionary: logger.debug( "Subtasks count missing. Implicit 1. dictionary=%r", dictionary, ) dictionary['subtasks_count'] = 1 th: TaskFixedHeader = \ DictSerializer.load(dictionary, as_class=TaskFixedHeader) th.last_checking = time.time() if isinstance(th.task_owner, dict): th.task_owner = Node.from_dict(th.task_owner) th.update_checksum() return th
def task_status_changed(self, task_id): if task_id in self.tasks: ts_dict = yield self.client.query_task_state(task_id) ts = DictSerializer.load(ts_dict) if not isinstance(ts, TaskState): raise TypeError( "Incorrect task state type: {}. Should be TaskState". format(ts)) self.tasks[task_id].task_state = ts self.customizer.update_tasks(self.tasks) if ts.status in task_to_remove_status: self.client.delete_task(task_id) else: logger.warning("Unknown task_id {}".format(task_id)) if self.customizer.current_task_highlighted.definition.task_id == task_id: self.customizer.update_task_additional_info(self.tasks[task_id])
def test_task_status_changed(self): task_state = TaskState() task_dict = DictSerializer.dump(task_state) logic = GuiApplicationLogic() logic.tasks = dict(task_id=task_state, wrong_task=None) def get_logic_task(task_id): deferred = Deferred() task = logic.tasks.get(task_id) deferred.callback(DictSerializer.dump(task)) return deferred logic.client = Mock() logic.client.query_task_state = Mock() logic.client.query_task_state.side_effect = get_logic_task logic.customizer = Mock() logic.task_status_changed('wrong_task') assert not logic.customizer.update_tasks.called assert logic.client.query_task_state.called logic.client.query_task_state.called = False logic.customizer.current_task_highlighted.definition.task_id = \ str(uuid.uuid4()) logic.task_status_changed(str(uuid.uuid4())) assert not logic.client.query_task_state.called assert not logic.customizer.update_task_additional_info.called logic.customizer.current_task_highlighted.definition.task_id = 'task_id' logic.task_status_changed(str(uuid.uuid4())) assert not logic.client.query_task_state.called assert not logic.customizer.update_task_additional_info.called logic.task_status_changed('task_id') assert logic.client.query_task_state.called assert logic.customizer.update_task_additional_info.called assert not logic.client.delete_task.called task_state.status = task_to_remove_status[0] logic.task_status_changed('task_id') assert logic.client.query_task_state.called assert logic.customizer.update_task_additional_info.called assert logic.client.delete_task.called
def format(self, result): result, result_type = self._initial_format(result) if result_type != CommandResult.NONE: if result_type == CommandResult.TABULAR: return tabulate(result[1], headers=result[0], tablefmt="simple") elif isinstance(result, basestring): return result elif isinstance(result, CommandException): return repr(result) result = DictSerializer.dump(result, typed=False) if self.prettify: return yaml.safe_dump(result, allow_unicode=True, default_flow_style=False) return result
def register_client(self, client): # client is golem.rpc.session.Client datadir = yield client.get_datadir() config_dict = yield client.get_settings() client_id = yield client.get_key_id() payment_address = yield client.get_payment_address() description = yield client.get_description() config = DictSerializer.load(config_dict) self.client = client self.datadir = datadir self.node_name = config.node_name self.dir_manager = DirManager(self.datadir) self.customizer.init_config() self.customizer.set_options(config, client_id, payment_address, description) if not self.node_name: self.customizer.prompt_node_name(self.node_name)
def resolve_call(alias, *args, **kwargs): if alias == aliases.Environment.datadir: return self.path elif alias == aliases.Environment.opts: return DictSerializer.dump(ClientConfigDescriptor()) elif alias == aliases.Environment.opt_description: return u'test description' elif alias == aliases.Payments.ident: return u'0xdeadbeef' elif alias == aliases.Crypto.key_id: return u'0xbadfad' elif alias == aliases.Task.tasks_stats: return dict(in_network=0, supported=0, subtasks_computed=0, subtasks_with_errors=0, subtasks_with_timeout=0) elif alias == aliases.Payments.balance: return 0, 0, 0 elif alias == aliases.Network.peers_connected: return [] elif alias == aliases.Computation.status: return u'' return 1
def get_settings(self): return DictSerializer.dump(self.config_desc)
def to_dict(self): return DictSerializer.dump(self, typed=False)
def update_settings(self, settings_dict, run_benchmarks=False): cfg_desc = DictSerializer.load(settings_dict) self.change_config(cfg_desc, run_benchmarks)
def query_task_state(self, task_id): state = self.task_server.task_manager.query_task_state(task_id) if state: return DictSerializer.dump(state)
def get_known_tasks(self): headers = {} for key, header in self.task_server.task_keeper.task_headers.iteritems( ): # noqa headers[unicode(key)] = DictSerializer.dump(header) return headers
def get_config(self): config_dict = yield self.client.get_settings() returnValue(DictSerializer.load(config_dict))
def change_config(self, cfg_desc, run_benchmarks=False): cfg_dict = DictSerializer.dump(cfg_desc) yield self.client.update_settings(cfg_dict, run_benchmarks=run_benchmarks) self.node_name = yield self.client.get_setting('node_name') self.customizer.set_name(u"{}".format(self.node_name))