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 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 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 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 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 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 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 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 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_logic_task(task_id): deferred = Deferred() task = logic.tasks.get(task_id) deferred.callback(DictSerializer.dump(task)) return deferred
def to_dict(self): return DictSerializer.dump(self, typed=False)
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 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))
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_settings(self): return DictSerializer.dump(self.config_desc)
def get_connected_peers(self): peers = self.get_peers() or [] return [ DictSerializer.dump(PeerSessionInfo(p), typed=False) for p in peers ]
def get_known_peers(self): peers = self.p2pservice.free_peers or [] return [ DictSerializer.dump(PeerSessionInfo(p), typed=False) for p in peers ]
def get_node(self): return DictSerializer.dump(self.node)