Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
    def test_computer(self):
        with self.assertRaises(TypeError):
            LocalComputer(None, self.path, self._success_callback,
                          self._failure_callback, self._get_bad_task_def)
        files = self.additional_dir_content([1])
        task = Task(Mock(), Mock())
        lc = LocalComputer(task, self.path, self._success_callback,
                           self._failure_callback, self._get_bad_task_def)
        self.assertIsInstance(lc, LocalComputer)
        lc.run()
        assert self.last_error is not None
        assert self.last_result is None
        assert self.error_counter == 1

        lc = LocalComputer(task,
                           self.path,
                           self._success_callback,
                           self._failure_callback,
                           self._get_better_task_def,
                           use_task_resources=False,
                           additional_resources=files)
        lc.run()
        lc.tt.join(60.0)
        path_ = path.join(lc.test_task_res_path, path.basename(files[0]))
        assert path.isfile(path_)
        assert self.error_counter == 1
        assert self.success_counter == 1

        tt = self.TestTaskThread({'data': "some data"}, "some error")
        lc.task_computed(tt)
        assert self.last_result == {"data": "some data"}
        assert self.last_result != "some error"
        assert self.error_counter == 1
        assert self.success_counter == 2

        tt = self.TestTaskThread({}, "some error")
        lc.task_computed(tt)
        assert self.last_error == "some error"
        assert self.error_counter == 2
        assert self.success_counter == 2

        tt = self.TestTaskThread({}, None)
        lc.task_computed(tt)
        assert self.last_error is None
        assert self.error_counter == 3
        assert self.success_counter == 2

        tt = self.TestTaskThread({'data': "some data"}, None)
        tt.error = True
        lc.task_computed(tt)
        assert self.last_error is None
        assert self.error_counter == 4
        assert self.success_counter == 2
Exemplo n.º 3
0
    def test_task_simple_serializer(self):
        with self.assertRaises(TypeError):
            Task.build_task("Not Task Builder")
        with self.assertRaises(TypeError):
            Task.register_listener("Not Listener")
        t = Task(Mock(), "")
        self.assertIsInstance(t, Task)
        self.assertEqual(t.get_stdout("abc"), "")
        self.assertEqual(t.get_stderr("abc"), "")
        self.assertEqual(t.get_results("abc"), [])

        t = Task(
            TaskHeader("ABC", "xyz", "10.10.10.10", 1023, "key", "DEFAULT",
                       Node()), "print 'Hello world'")

        tl1 = TaskEventListener()
        tl2 = TaskEventListener()
        t.register_listener(tl1)
        t.register_listener(tl2)
        assert len(t.listeners) == 2
        p = SimpleSerializer.dumps(t)
        u = SimpleSerializer.loads(p)
        assert t.src_code == u.src_code
        assert t.header.task_id == u.header.task_id
        assert t.header.task_owner.node_name == u.header.task_owner.node_name
        assert u.get_results("abc") == []
        assert len(t.listeners) == 2
        assert len(u.listeners) == 0
        t.unregister_listener(tl2)
        assert len(t.listeners) == 1
        assert t.listeners[0] == tl1
        t.listeners[0].notify_update_task("abc")
        t.unregister_listener(tl1)
        assert len(t.listeners) == 0
        with self.assertLogs(logger, level="WARNING"):
            t.unregister_listener(tl1)
Exemplo n.º 4
0
    def test_is_success(self):
        task = Task(Mock(), Mock(), Mock())

        task.query_extra_data_for_test_task = Mock()
        tt = TaskTester(task, self.path, Mock(), Mock())
        task_thread = Mock()

        # Proper task
        task_thread.error = None
        task_thread.result = ({"data": True}, 123)
        assert tt.is_success(task_thread)

        # Task thead result first arg is not tuple
        task_thread.result = {"data": True}
        assert not tt.is_success(task_thread)
        assert task_thread.error == "Wrong result format"
Exemplo n.º 5
0
    def test_task_computed(self):
        task = Task(Mock(), Mock(), Mock())

        result = [{"data": True}, 123]

        task.header.node_name = self.node
        task.header.task_id = self.name
        task.root_path = self.path
        task.after_test = lambda res, tmp_dir: {}
        task.query_extra_data_for_test_task = Mock()

        tt = TaskTester(task, self.path, Mock(), Mock())
        tt.tmp_dir = self.path
        task_thread = TaskThread(result)
        tt.task_computed(task_thread)

        task_thread = MemTaskThread(None, 30210, "Some error")
        with self.assertLogs(logger, level='WARNING'):
            tt.task_computed(task_thread)
        tt.error_callback.assert_called_with("Some error")

        task_thread = MemTaskThread("result", 2010, "Another error")
        self.assertIsNone(tt.get_progress())
        tt.tt = task_thread
        self.assertEqual(tt.get_progress(), "30%")
        task_thread.error = True
        self.assertEqual(tt.get_progress(), 0)
        tt.task_computed(task_thread)
        tt.error_callback.assert_called_with("Another error")

        self.message = ""

        def success_callback(res, est_mem, time_spent, after_test_data):
            self.message = "Success " + after_test_data["warnings"]

        task.header.node_name = self.node
        task.header.task_id = self.name
        task.root_path = self.path
        task.after_test = lambda res, tmp_dir: {"warnings": "bla ble"}
        task.query_extra_data_for_test_task = Mock()

        tt = TaskTester(task, self.path, success_callback, None)
        tt.tmp_dir = self.path
        task_thread = TaskThread(result)
        tt.task_computed(task_thread)
        self.assertTrue("bla" in self.message)
        self.assertTrue("ble" in self.message)
Exemplo n.º 6
0
    def test_get_resources(self, mock_addr):
        mock_addr.return_value = self.addr_return
        task_id = "xyz"

        resources = ['first', 'second']

        task_mock = self._get_task_mock()
        with patch('golem.task.taskmanager.TaskManager.get_resources',
                   return_value=resources):
            sync_wait(self.tm.add_new_task(task_mock))
            assert self.tm.get_resources(task_id,
                                         task_mock.header) is resources

        task = Task(self._get_task_header("xyz", 120, 120),
                    "print 'hello world'")
        self.tm.tasks["xyz"] = task
        self.tm.get_resources("xyz", TaskResourceHeader(self.path), 0)
Exemplo n.º 7
0
    def test_resource_send(self, mock_addr):
        from pydispatch import dispatcher
        mock_addr.return_value = self.addr_return
        self.tm.task_persistence = True
        t = Task(
            TaskHeader("ABC", "xyz", "10.10.10.10", 1023, "abcde", "DEFAULT"),
            "print 'hello world'")
        listener_mock = Mock()

        def listener(sender, signal, event, task_id):
            self.assertEquals(event, 'task_status_updated')
            self.assertEquals(task_id, t.header.task_id)
            listener_mock()

        dispatcher.connect(listener, signal='golem.taskmanager')
        try:
            sync_wait(self.tm.add_new_task(t))
            self.tm.resources_send("xyz")
            self.assertEquals(listener_mock.call_count, 2)
        finally:
            dispatcher.disconnect(listener, signal='golem.taskmanager')
Exemplo n.º 8
0
    def test_update_signatures(self, _):

        node = Node("node", "key_id", "10.0.0.10", 40103, "1.2.3.4", 40103,
                    None, 40102, 40102)
        task = Task(
            TaskHeader("node",
                       "task_id",
                       "1.2.3.4",
                       1234,
                       "key_id",
                       "environment",
                       task_owner=node), '')

        self.tm.keys_auth = EllipticalKeysAuth(self.path)
        sync_wait(self.tm.add_new_task(task))
        sig = task.header.signature

        self.tm.update_task_signatures()
        assert task.header.signature == sig

        task.header.task_owner.pub_port = 40104
        self.tm.update_task_signatures()
        assert task.header.signature != sig
Exemplo n.º 9
0
class TestTaskTester(TestDirFixture, LogTestCase):
    task = Task(Mock(), Mock())
    node = 'node1'
    task_name = 'task1'

    def test_init(self):
        self.task.query_extra_data_for_test_task = Mock()
        self.assertIsNotNone(TaskTester(self.task, self.path, None, None))

    def test_task_computed(self):
        result = [{"data": True}, 123]

        self.task.header.node_name = self.node
        self.task.header.task_id = self.task_name
        self.task.root_path = self.path
        self.task.after_test = lambda res, tmp_dir: {}
        self.task.query_extra_data_for_test_task = Mock()

        tt = TaskTester(self.task, self.path, Mock(), Mock())
        tt.tmp_dir = self.path
        task_thread = TaskThread(result)
        tt.task_computed(task_thread)

        task_thread = MemTaskThread(None, 30210, "Some error")
        with self.assertLogs(logger, level=1):
            tt.task_computed(task_thread)
        tt.error_callback.assert_called_with("Some error")

        task_thread = MemTaskThread("result", 2010, "Another error")
        self.assertIsNone(tt.get_progress())
        tt.tt = task_thread
        self.assertEqual(tt.get_progress(), "30%")
        task_thread.error = True
        self.assertEqual(tt.get_progress(), 0)
        tt.task_computed(task_thread)
        tt.error_callback.assert_called_with("Another error")

        self.message = ""

        def success_callback(res, est_mem, time_spent, after_test_data):
            self.message = "Success " + after_test_data["warnings"]

        self.task.header.node_name = self.node
        self.task.header.task_id = self.task_name
        self.task.root_path = self.path
        self.task.after_test = lambda res, tmp_dir: {"warnings": "bla ble"}
        self.task.query_extra_data_for_test_task = Mock()

        tt = TaskTester(self.task, self.path, success_callback, None)
        tt.tmp_dir = self.path
        task_thread = TaskThread(result)
        tt.task_computed(task_thread)
        self.assertTrue("bla" in self.message)
        self.assertTrue("ble" in self.message)

    def test_is_success(self):
        self.task.query_extra_data_for_test_task = Mock()
        tt = TaskTester(self.task, self.path, Mock(), Mock())
        task_thread = Mock()

        # Proper task
        task_thread.error = None
        task_thread.result = ({"data": True}, 123)
        assert tt.is_success(task_thread)

        # Task thead result first arg is not tuple
        task_thread.result = {"data": True}
        assert not tt.is_success(task_thread)
        assert task_thread.error == "Wrong result format"
Exemplo n.º 10
0
 def test_init(self):
     task = Task(Mock(), Mock(), Mock())
     task.query_extra_data_for_test_task = Mock()
     self.assertIsNotNone(TaskTester(task, self.path, None, None))