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_get_performance(self): env = LuxRenderEnvironment() perf = 1234.5 cfg_desc = ClientConfigDescriptor() cfg_desc.estimated_lux_performance = perf result = env.get_performance(cfg_desc) self.assertTrue(result == perf)
def _run_docker_task(self, render_task, timeout=60 * 5): task_id = render_task.header.task_id extra_data = render_task.query_extra_data(1.0) ctd = extra_data.ctd ctd.deadline = timeout_to_deadline(timeout) # Create the computing node self.node = OptNode(datadir=self.path) self.node.client.start = Mock() self.node.initialize() ccd = ClientConfigDescriptor() ccd.estimated_blender_performance = 2000.0 ccd.estimated_lux_performance = 2000.0 task_server = TaskServer(Mock(), ccd, Mock(), self.node.client, use_docker_machine_manager=False) task_computer = task_server.task_computer resource_dir = task_computer.resource_manager.get_resource_dir(task_id) temp_dir = task_computer.resource_manager.get_temporary_dir(task_id) self.dirs_to_remove.append(resource_dir) self.dirs_to_remove.append(temp_dir) # Copy the task resources common_prefix = path.commonprefix(render_task.task_resources) common_prefix = path.dirname(common_prefix) for res_file in render_task.task_resources: dest_file = path.join(resource_dir, res_file[len(common_prefix) + 1:]) dest_dirname = path.dirname(dest_file) if not path.exists(dest_dirname): makedirs(dest_dirname) shutil.copyfile(res_file, dest_file) def send_task_failed(self_, subtask_id, task_id, error_msg, *args): self.error_msg = error_msg TaskServer.send_task_failed = send_task_failed # Start task computation task_computer.task_given(ctd) result = task_computer.resource_given(ctd.task_id) self.assertTrue(result) # Thread for task computation should be created by now task_thread = None with task_computer.lock: if task_computer.current_computations: task_thread = task_computer.current_computations[0] if task_thread: task_thread.join(60.0) return task_thread, self.error_msg, temp_dir
def test_get_performance(self): """Changing estimated performance in ClientConfigDescriptor.""" env = BlenderEnvironment() fake_performance = 2345.2 cfg_desc = ClientConfigDescriptor() cfg_desc.estimated_blender_performance = fake_performance result = env.get_performance(cfg_desc) self.assertEquals(result, fake_performance)
def setUp(self): super(TestSettings, self).setUp() app_config = AppConfig.load_config(self.tempdir) config_desc = ClientConfigDescriptor() config_desc.init_from_app_config(app_config) client = Mock() self.client_config_dict = config_desc.__dict__ client.get_settings.return_value = self.client_config_dict self.client = client
def test_send_results(self, trust, *_): ccd = ClientConfigDescriptor() ccd.min_price = 11 keys_auth = KeysAuth(self.path, 'priv_key', '') task_header = get_example_task_header(keys_auth.public_key) n = Node.from_dict(task_header["fixed_header"]['task_owner']) ts = self.ts ts._is_address_accessible = Mock(return_value=True) ts.verify_header_sig = lambda x: True ts.client.get_suggested_addr.return_value = "10.10.10.10" ts.client.get_requesting_trust.return_value = ts.max_trust fd, result_file = tempfile.mkstemp() os.close(fd) results = {"data": [result_file]} task_header = get_example_task_header(keys_auth.public_key) task_id = task_header["fixed_header"]["task_id"] assert ts.add_task_header(task_header) assert ts.request_task() subtask_id = idgenerator.generate_new_id_from_id(task_id) subtask_id2 = idgenerator.generate_new_id_from_id(task_id) self.assertTrue(ts.send_results(subtask_id, task_id, results)) self.assertTrue(ts.send_results(subtask_id2, task_id, results)) wtr = ts.results_to_send[subtask_id] self.assertIsInstance(wtr, WaitingTaskResult) self.assertEqual(wtr.subtask_id, subtask_id) self.assertEqual(wtr.result, [result_file]) self.assertEqual(wtr.last_sending_trial, 0) self.assertEqual(wtr.delay_time, 0) self.assertEqual(wtr.owner, n) self.assertEqual(wtr.already_sending, False) self.assertIsNotNone(ts.task_keeper.task_headers.get(task_id)) ctd = ComputeTaskDef() ctd['task_id'] = task_id ctd['subtask_id'] = subtask_id ttc = msg_factories.tasks.TaskToComputeFactory(price=1) ttc.compute_task_def = ctd ts.task_manager.comp_task_keeper.receive_subtask(ttc) prev_call_count = trust.PAYMENT.increase.call_count ts.increase_trust_payment("xyz") self.assertGreater(trust.PAYMENT.increase.call_count, prev_call_count) prev_call_count = trust.PAYMENT.decrease.call_count ts.decrease_trust_payment("xyz") self.assertGreater(trust.PAYMENT.decrease.call_count, prev_call_count) os.remove(result_file)
def test_connection_for_task_request_established(self, *_): ccd = ClientConfigDescriptor() ccd.min_price = 11 ts = self.ts session = Mock() session.address = "10.10.10.10" session.port = 1020 ts.conn_established_for_type[TASK_CONN_TYPES['task_request']]( session, "abc", "nodename", "key", "xyz", 1010, 30, 3, 1, 2) self.assertEqual(session.task_id, "xyz") self.assertEqual(session.key_id, "key") self.assertEqual(session.conn_id, "abc") self.assertEqual(ts.task_sessions["xyz"], session) session.send_hello.assert_called_with() session.request_task.assert_called_with("nodename", "xyz", 1010, 30, 3, 1, 2)
def setUp(self): for parent in self.__class__.__bases__: parent.setUp(self) self.node = Mock( prv_addr='10.0.0.2', prv_port=40102, pub_addr='1.2.3.4', pub_port=40102, hyperg_prv_port=3282, hyperg_pub_port=3282, prv_addresses=['10.0.0.2'], ) self.resource_manager = Mock(add_task=Mock( side_effect=lambda *a, **b: ([], "a1b2c3"))) with patch( 'golem.network.concent.handlers_library.HandlersLibrary' '.register_handler', ): self.ts = TaskServer( node=self.node, config_desc=ClientConfigDescriptor(), client=self.client, use_docker_manager=False, ) self.ts.task_manager.notify_update_task = Mock( side_effect=self.ts.task_manager.notify_update_task) self.ts.task_manager.delete_task = Mock( side_effect=self.ts.task_manager.delete_task) self.ts._get_resource_manager = Mock( return_value=self.resource_manager) self.ts.task_manager.dump_task = Mock() self.task_count = 3
def test_approve(self): config = ClientConfigDescriptor() config.num_cores = '1' config.computing_trust = '1' config.key_difficulty = '0' approved_config = ConfigApprover(config).approve() assert isinstance(approved_config.num_cores, int) assert approved_config.num_cores == 1 assert isinstance(approved_config.computing_trust, float) assert approved_config.computing_trust == 1.0 assert isinstance(approved_config.key_difficulty, int) assert approved_config.key_difficulty == KEY_DIFFICULTY
def make_client(*_, **kwargs): default_kwargs = { 'app_config': Mock(), 'config_desc': ClientConfigDescriptor(), 'keys_auth': Mock( _private_key='a' * 32, key_id='a' * 64, public_key=b'a' * 128, ), 'database': Mock(), 'transaction_system': Mock(), 'connect_to_known_hosts': False, 'use_docker_manager': False, 'use_monitor': False, } default_kwargs.update(kwargs) client = Client(**default_kwargs) return client
def test_blender_customizer(self, mock_messagebox): self.logic.customizer = MainWindowCustomizer(self.gui.main_window, self.logic) self.logic.register_new_task_type( BlenderTaskTypeInfo(TaskWidget(Ui_BlenderWidget), BlenderRenderDialogCustomizer)) self.logic.client = Mock() self.logic.client.config_desc = ClientConfigDescriptor() self.logic.client.config_desc.use_ipv6 = False self.logic.client.config_desc.max_price = 0 self.logic.client.get_config.return_value = self.logic.client.config_desc self.logic.client.get_res_dirs.return_value = {'computing': self.path, 'received': self.path} self.logic.customizer.init_config() customizer = self.logic.customizer.new_task_dialog_customizer.task_customizer assert isinstance(customizer, FrameRendererCustomizer) assert not customizer.gui.ui.framesCheckBox.isChecked() customizer._change_options() assert customizer.options.frames == range(1, 11) customizer.gui.ui.framesCheckBox.setChecked(True) customizer.gui.ui.framesLineEdit.setText(u"{}".format("1;3;5-12")) customizer._change_options() assert customizer.options.frames == [1, 3] + range(5, 13) customizer.gui.ui.framesLineEdit.setText(u"{}".format("Not proper frames")) customizer._change_options() assert customizer.options.frames == [1, 3] + range(5, 13) mock_messagebox.assert_called_with(mock_messagebox.Critical, "Error", u"Wrong frame format. Frame list expected, e.g. 1;3;5-12.", ANY, ANY)
def meta_data(): client_mock = mock.MagicMock() client_mock.cliid = str(uuid4()) client_mock.sessid = str(uuid4()) client_mock.config_desc = ClientConfigDescriptor() client_mock.mainnet = False os_info = OSInfo('linux', 'Linux', '1', '1.2.3') return NodeMetadataModel(client_mock, os_info, 'app_version')
def setUp(self): super(TestTaskComputer, self).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() self.task_server = task_server
def setUp(self): super().setUp() self.keys_auth = KeysAuth(self.path, 'priv_key', 'password') self.service = P2PService( node=None, config_desc=ClientConfigDescriptor(), keys_auth=self.keys_auth, connect_to_known_hosts=False, ) self.service.seeds = set()
def setUp(self): super(TestP2PService, self).setUp() random.seed() # Mock saving keys as it takes long to compute and isn't needed here KeysAuth._save_private_key = mock.Mock() self.keys_auth = KeysAuth(self.path, 'priv_key', 'password') self.service = P2PService(None, ClientConfigDescriptor(), self.keys_auth, connect_to_known_hosts=False)
def test_config_override_valid(self, *_): self.assertTrue(hasattr(ClientConfigDescriptor(), "node_address")) c = Client(datadir=self.path, node_address='1.0.0.0', transaction_system=False, connect_to_known_hosts=False, use_docker_machine_manager=False, use_monitor=False) self.assertEqual(c.config_desc.node_address, '1.0.0.0') c.quit()
def test_broadcast_on_name_change(self): conn = MagicMock() peer = PeerSession(conn) peer.hello_called = False def fake_hello(self): self.hello_called = True import types peer.hello = types.MethodType(fake_hello, peer) keys_auth = EllipticalKeysAuth(self.path, "PUBTESTPATH1", "PUBTESTPATH2") peer.key_id = keys_auth.key_id self.service.add_peer(keys_auth.key_id, peer) ccd = ClientConfigDescriptor() assert not peer.hello_called self.service.change_config(ccd) assert not peer.hello_called # negative test ccd = ClientConfigDescriptor() ccd.node_name = "test sending hello on name change" self.service.change_config(ccd) assert peer.hello_called # positive test
def _create_client(self, task_id, postfix): directory = os.path.join(self.tempdir, 'node' + postfix) dir_manager = DirManager(directory) cls = self._resource_manager_class resource_manager = cls.__new__(cls) resource_manager.__init__(dir_manager) database = Database( db, fields=DB_FIELDS, models=DB_MODELS, db_dir=directory) with mock.patch('golem.client.node_info_str'): client = Client(datadir=dir_manager.root_path, app_config=mock.Mock(), config_desc=ClientConfigDescriptor(), keys_auth=mock.Mock(), database=database, transaction_system=mock.Mock(), connect_to_known_hosts=False, use_docker_manager=False, use_monitor=False) client.resource_server = BaseResourceServer(resource_manager, dir_manager, mock.Mock(), client) with mock.patch( "golem.network.concent.handlers_library" ".HandlersLibrary" ".register_handler"): client.task_server = TaskServer( node=Node(prv_addr='127.0.0.1', hyperdrive_prv_port=3282), config_desc=mock.Mock(), client=client, use_docker_manager=False, ) client.start = mock.Mock() client.start_network = mock.Mock() client.task_server.sync_network = mock.Mock() client.task_server.start_accepting = mock.Mock() client.task_server.task_computer = mock.Mock() get_peer = mock.Mock(return_value=mock.Mock(host='127.0.0.1', port='3282')) transport = mock.Mock(getPeer=get_peer) task_session = TaskSession(mock.Mock(server=client.task_server, transport=transport)) task_session.task_id = task_id resource_dir = resource_manager.storage.get_dir(task_id) return client, resource_dir, task_session
def test_monitor_messages(self): nmm = NodeMetadataModel("CLIID", "SESSID", "win32", "1.3", "Random description\n\t with additional data", ClientConfigDescriptor()) m = MONITOR_CONFIG.copy() m['HOST'] = "http://localhost/88881" monitor = SystemMonitor(nmm, m) monitor.start() monitor.on_login() monitor.on_payment(addr="some address", value=30139019301) monitor.on_income("different address", 319031904194810) monitor.on_peer_snapshot([{"node_id": "firt node", "port": 19301}, {"node_id": "second node", "port": 3193}]) ccd = ClientConfigDescriptor() ccd.node_name = "new node name" nmm = NodeMetadataModel("CLIID", "SESSID", "win32", "1.3", "Random description\n\t with additional data", ccd) monitor.on_config_update(nmm) monitor.on_logout() monitor.shut_down()
def __change_config(self): cfg_desc = ClientConfigDescriptor() self.__read_basic_config(cfg_desc) self.__read_advance_config(cfg_desc) self.__read_payment_config(cfg_desc) self.logic.change_config(cfg_desc, run_benchmarks=self.docker_config_changed) self.__recount_performance() self.load_data() self.docker_config_changed = False
def test_config_override_invalid(self, *_): """Test that Client() does not allow to override properties that are not in ClientConfigDescriptor. """ self.assertFalse(hasattr(ClientConfigDescriptor(), "node_colour")) with self.assertRaises(AttributeError): Client(datadir=self.path, node_colour='magenta', transaction_system=False, connect_to_known_hosts=False, use_docker_machine_manager=False, use_monitor=False)
def create_client(datadir): # executed in a subprocess from golem.network.stun import pystun pystun.get_ip_info = override_ip_info from golem.client import Client app_config = AppConfig.load_config(datadir) config_desc = ClientConfigDescriptor() config_desc.init_from_app_config(app_config) config_desc.key_difficulty = 0 config_desc.use_upnp = False from golem.core.keysauth import KeysAuth with mock.patch.dict('ethereum.keys.PBKDF2_CONSTANTS', {'c': 1}): keys_auth = KeysAuth( datadir=datadir, private_key_name=faker.Faker().pystr(), password='******', difficulty=config_desc.key_difficulty, ) database = Database( db, fields=DB_FIELDS, models=DB_MODELS, db_dir=datadir) ets = _make_mock_ets() return Client(datadir=datadir, app_config=app_config, config_desc=config_desc, keys_auth=keys_auth, database=database, transaction_system=ets, use_monitor=False, connect_to_known_hosts=False, use_docker_manager=False)
def setUp(self): super().setUp() random.seed() self.ccd = ClientConfigDescriptor() self.client.concent_service.enabled = False with patch( 'golem.network.concent.handlers_library.HandlersLibrary' '.register_handler', ): self.ts = TaskServer( node=NodeFactory(), config_desc=self.ccd, client=self.client, use_docker_manager=False, )
def test_termination(self): script = "import time\ntime.sleep(20)" task_server = Mock() task_server.config_desc = ClientConfigDescriptor() task_server.client.datadir = self.test_dir task_server.benchmark_manager = Mock() task_server.benchmark_manager.benchmarks_needed.return_value = False task_server.client.get_node_name.return_value = "test_node" task_server.get_task_computer_root.return_value = \ task_server.client.datadir task_computer = TaskComputer(task_server, use_docker_manager=False) image = DockerImage("golemfactory/base", tag="1.2") with self.assertRaises(AttributeError): dir_mapping = DockerTaskThread.generate_dir_mapping( self.resources_dir, self.output_dir) DockerTaskThread("subtask_id", None, script, None, dir_mapping, timeout=30) def test(): dir_mapping = DockerTaskThread.generate_dir_mapping( self.resources_dir, self.output_dir) tt = DockerTaskThread("subtask_id", [image], script, None, "test task thread", dir_mapping, timeout=30) task_computer.counting_thread = tt task_computer.counting_task = True tt.setDaemon(True) tt.start() time.sleep(1) started = time.time() parent_thread = Thread(target=test) parent_thread.start() time.sleep(1) ct = task_computer.counting_thread while ct and ct.is_alive(): task_computer.run() if time.time() - started > 15: self.fail("Job timed out") else: ct = task_computer.counting_thread time.sleep(1)
def test_thread(self): ts = mock.MagicMock() ts.config_desc = ClientConfigDescriptor() ts.benchmark_manager.benchmarks_needed.return_value = False ts.get_task_computer_root.return_value = self.new_path tc = TaskComputer(ts, use_docker_manager=False) tc.counting_task = True tc.waiting_for_task = None tt = self._new_task_thread(tc) sync_wait(tt.start()) self.assertGreater(tt.end_time - tt.start_time, 0) self.assertLess(tt.end_time - tt.start_time, 20)
def __create_task_server(session): from golem.clientconfigdescriptor import ClientConfigDescriptor from golem.task.taskserver import TaskServer client = Mock(datadir=session.data_dir) dir_manager = DirManager(session.data_dir) resource_manager = HyperdriveResourceManager(dir_manager=dir_manager) resource_manager.successful_uploads = True resource_manager.successful_downloads = True resource_manager.add_file_org = resource_manager.add_file resource_manager.add_file = types.MethodType(_add_file, resource_manager) resource_manager.pull_resource_org = resource_manager.pull_resource resource_manager.pull_resource = types.MethodType(_pull_resource, resource_manager) with patch("golem.network.concent.handlers_library" ".HandlersLibrary" ".register_handler"): task_server = TaskServer( node=Mock(client=client, key=str(uuid.uuid4())), config_desc=ClientConfigDescriptor(), client=client, use_docker_manager=False ) task_server.task_manager = Mock( task_result_manager=Mock( resource_manager=resource_manager ) ) peers = to_hyperg_peer('127.0.0.1', 3282) client_options = HyperdriveClientOptions( HyperdriveClient.CLIENT_ID, HyperdriveClient.VERSION, options=dict( peers=peers, filtered=peers ) ) task_server.get_share_options = Mock(return_value=client_options) task_server.get_download_options = Mock(return_value=client_options) session.task_server = task_server
def setUp(self): super().setUp() self.client.sync = Mock() self.client.p2pservice = Mock(peers={}) self.client.apps_manager._benchmark_enabled = Mock(return_value=True) self.client.apps_manager.load_all_apps() with patch( 'golem.network.concent.handlers_library.HandlersLibrary' '.register_handler', ): self.client.task_server = TaskServer( node=Node(), config_desc=ClientConfigDescriptor(), client=self.client, use_docker_manager=False, apps_manager=self.client.apps_manager, ) self.client.monitor = Mock()
def test_change_config(self, *_): ts = self.ts ccd2 = ClientConfigDescriptor() ccd2.task_session_timeout = 124 ccd2.min_price = 0.0057 ccd2.use_distributed_resource_management = 0 ccd2.task_request_interval = 31 # ccd2.use_waiting_ttl = False ts.change_config(ccd2) self.assertEqual(ts.config_desc, ccd2) self.assertEqual(ts.last_message_time_threshold, 124) self.assertEqual(ts.task_keeper.min_price, 0.0057) self.assertEqual(ts.task_manager.use_distributed_resources, False) self.assertEqual(ts.task_computer.task_request_frequency, 31)
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 test_lux_customizer(self, mock_file_dialog): self.logic.register_new_task_type(LuxRenderTaskTypeInfo( TaskWidget(Ui_LuxWidget), LuxRenderDialogCustomizer)) self.logic.customizer = MainWindowCustomizer(self.gui.main_window, self.logic) self.logic.dir_manager = Mock() self.logic.dir_manager.root_path = self.path self.logic.client = Mock() self.logic.client.config_desc = ClientConfigDescriptor() self.logic.client.config_desc.use_ipv6 = False self.logic.client.config_desc.max_price = 0 self.logic.client.get_config.return_value = self.logic.client.config_desc self.logic.client.get_res_dirs.return_value = {'computing': self.path, 'received': self.path} self.logic.customizer.init_config() lux_customizer = self.logic.customizer.new_task_dialog_customizer.task_customizer assert isinstance(lux_customizer, LuxRenderDialogCustomizer) assert lux_customizer.get_task_name() == "LuxRender" self.logic.customizer.gui.ui.resourceFilesLabel.setText("124") QTest.mouseClick(self.logic.customizer.gui.ui.resetToDefaultButton, Qt.LeftButton) assert self.logic.customizer.gui.ui.resourceFilesLabel.text() == "0" definition = RenderingTaskDefinition() lux_customizer.get_task_specific_options(definition) lux_customizer.load_task_definition(definition) path = u"{}".format(str(lux_customizer.load_setting('main_scene_path', os.path.expanduser('~')))) mock_file_dialog.getOpenFileName.return_value = path, None QTest.mouseClick(lux_customizer.gui.ui.chooseMainSceneFileButton, Qt.LeftButton) mock_file_dialog.getOpenFileName.assert_called_with(lux_customizer.gui, "Choose main scene file", path, u"Scene files (*.LXS *.lxs)") lux_customizer.gui.ui.stopByTimeRadioButton.setChecked(True) lux_customizer.gui.ui.haltTimeLineEdit.setText("60") lux_customizer._change_options() assert lux_customizer.options.haltspp == 0 assert lux_customizer.options.halttime == 60 lux_customizer.gui.ui.haltTimeLineEdit.setText("XYZ") with self.assertLogs(logger, level="ERROR"): lux_customizer._change_options() assert lux_customizer.options.haltspp == 0 lux_customizer.gui.ui.stopBySppRadioButton.setChecked(True) lux_customizer.gui.ui.haltTimeLineEdit.setText("30") lux_customizer.gui.ui.haltSppLineEdit.setText("ABC") with self.assertLogs(logger, level="ERROR"): lux_customizer._change_options() assert lux_customizer.options.halttime == 0 lux_customizer.gui.ui.haltSppLineEdit.setText("25") lux_customizer._change_options() assert lux_customizer.options.halttime == 0 assert lux_customizer.options.haltspp == 25 lux_customizer.options.haltspp = 0 lux_customizer._change_halts_values() assert lux_customizer.gui.ui.stopByTimeRadioButton.isChecked() assert not lux_customizer.gui.ui.stopBySppRadioButton.isChecked() lux_customizer.options.haltspp = 24 lux_customizer._change_halts_values() assert not lux_customizer.gui.ui.stopByTimeRadioButton.isChecked() assert lux_customizer.gui.ui.stopBySppRadioButton.isChecked()