def test_subtasks_error(self): with client_ctx(Tasks, self.client): self.client.get_subtasks = Mock(return_value=None) tasks = Tasks() result = tasks.subtasks('task_id', None) self.assertEqual(result, 'No subtasks') self.client.get_subtasks.assert_called_once_with('task_id')
def test_unsupport(self): client = self.client with client_ctx(Tasks, client): tasks = Tasks() unsupport = tasks.unsupport(0) assert isinstance(unsupport, CommandResult) assert unsupport.data[1][0] == ['app_version', 3, '0.8.1'] assert unsupport.data[1][1] == ['max_price', 2, 7] assert unsupport.data[1][2] == ['environment_missing', 1, None]
def test_show_result_is_none(self): client = Mock() client.get_tasks.return_value = None with client_ctx(Tasks, client): tasks = Tasks() one_task = tasks.show("non existing task_id", None, current=True) self.assertIsNone(one_task)
def test_subtasks_ok(self): client = self.client with client_ctx(Tasks, client): tasks = Tasks() subtasks = tasks.subtasks('745c1d01', None) assert isinstance(subtasks, CommandResult) assert subtasks.data[1][0] == [ 'node_1', 'subtask_1', '0:00:09', 'waiting', '1.00 %' ]
def test_restart_error(self): with client_ctx(Tasks, self.client): self.client._call.return_value = None, 'error' tasks = Tasks() with self.assertRaises(CommandException): tasks.restart('task_id') self.client._call.assert_called_once_with( 'comp.task.restart', 'task_id', force=False, )
def test_restart_success(self): with client_ctx(Tasks, self.client): self.client._call.return_value = 'new_task_id', None tasks = Tasks() result = tasks.restart('task_id') self.assertEqual(result, 'new_task_id') self.client._call.assert_called_once_with( 'comp.task.restart', 'task_id', force=False, )
def test_basic_commands(self): client = self.client with client_ctx(Tasks, client): tasks = Tasks() assert tasks.abort('valid') client.abort_task.assert_called_with('valid') assert tasks.delete('valid') client.delete_task.assert_called_with('valid') assert tasks.stats() client.get_task_stats.assert_called_with()
def test_template(self) -> None: tasks = Tasks() with patch('sys.stdout', io.StringIO()) as mock_io: tasks.template(None) output = mock_io.getvalue() self.assertIn("bid", output) self.assertIn("0.0", output) self.assertIn('"subtask_timeout": "0:00:00"', output) self.assertEqual(json.loads(output), TaskDefinition().to_dict()) temp = self.temp_file_name("test_template") tasks.template(temp) with open(temp) as f: content = f.read() self.assertEqual(content, output) with client_ctx(Tasks, self.client): Tasks.client.get_task.return_value = TaskDefinition().to_dict() tasks.dump('id', temp) with open(temp) as f: content_dump = f.read() self.assertEqual(content, content_dump)
def test_show_current(self): client = self.client with client_ctx(Tasks, client): tasks = Tasks() current_tasks = tasks.show(None, None, True) self.assertIsInstance(current_tasks, CommandResult) self.assertEqual(len(current_tasks.data[1]), 4) self.assertEqual(current_tasks.data[1][0], ['745c1d02', '0:00:02', '4', 'Sending', '2.00 %']) self.assertEqual(current_tasks.data[1][1], ['745c1d03', '0:00:03', '5', 'Waiting', '3.00 %']) self.assertEqual( current_tasks.data[1][2], ['745c1d04', '0:00:04', '6', 'Starting', '4.00 %']) self.assertEqual( current_tasks.data[1][3], ['745c1d05', '0:00:05', '7', 'Computing', '5.00 %'])
def test_show(self): client = self.client with client_ctx(Tasks, client): tasks = Tasks() one_task = tasks.show('745c1d01', None, False) all_tasks = tasks.show(None, None, False) self.assertIsInstance(one_task, dict) self.assertIsInstance(all_tasks, CommandResult) self.assertEqual( one_task, { 'time_remaining': '0:00:01', 'status': 'Not started', 'subtasks_count': 3, 'id': '745c1d01', 'progress': '1.00 %' }) self.assertEqual( all_tasks.data[1][0], ['745c1d01', '0:00:01', '3', 'Not started', '1.00 %']) self.client.get_tasks = lambda _: { 'time_remaining': None, 'status': 'XXX', 'substasks': 1, 'id': 'XXX', 'progress': 0 } task = tasks.show('XXX', None, False) self.assertDictEqual( task, { 'time_remaining': '???', 'status': 'XXX', 'substasks': 1, 'id': 'XXX', 'progress': '0.00 %' })
def test_create(self) -> None: client = self.client definition = TaskDefinition() definition.name = "The greatest task ever" def_str = json.dumps(definition.to_dict()) with client_ctx(Tasks, client): tasks = Tasks() # pylint: disable=no-member tasks._Tasks__create_from_json(def_str) # type: ignore # pylint: enable=no-member client._call.assert_called_once_with( 'comp.task.create', definition.to_dict(), ) client._call.reset_mock() patched_open = "golem.interface.client.tasks.open" with patch(patched_open, mock_open(read_data='{"name": "Golem task"}')): client._call.return_value = ('task_id', None) tasks.create("foo") task_def = json.loads('{"name": "Golem task"}') client._call.assert_called_once_with( 'comp.task.create', task_def, force=False, )
def test_show(self): client = self.client with client_ctx(Tasks, client): tasks = Tasks() one_task = tasks.show('745c1d01', None) all_tasks = tasks.show(None, None) assert one_task and all_tasks assert isinstance(one_task, dict) assert isinstance(all_tasks, CommandResult) assert one_task == { 'time_remaining': 1, 'status': 'waiting', 'subtasks': 3, 'id': '745c1d01', 'progress': '1.00 %' } assert all_tasks.data[1][0] == [ '745c1d01', '1', '3', 'waiting', '1.00 %' ]
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)