Example #1
0
    def test_queue_timeout(self):

        taskrunner = TaskRunner("guest", "guest", "localhost", "/", "ots",
                                5672, "test_taskrunner", 1, 1, 1, 1)

        _init_queue(self.channel, "test_taskrunner",
                    taskrunner._services_exchange, "test_taskrunner")

        taskrunner.add_task("dummycommand")
        self.assertRaises(OtsQueueTimeoutError, taskrunner.run)
Example #2
0
 def setUp(self):
     self.taskrunner = TaskRunner("guest", "guest", "localhost", "/", "ots",
                                  5672, "test_taskrunner", 1, 60, 1, 1)
     self.connection = amqp.Connection(host="localhost",
                                       userid="guest",
                                       password="******",
                                       virtual_host="/",
                                       insist=False)
     self.channel = self.connection.channel()
     _init_queue(self.channel, "test_taskrunner",
                 self.taskrunner._services_exchange, "test_taskrunner")
Example #3
0
    def test_queue_timeout(self):

        taskrunner = TaskRunner("guest", "guest", "localhost",
                                "/", "ots", 5672, "test_taskrunner", 
                                1, 1, 1, 1)

        _init_queue(self.channel, 
                    "test_taskrunner", 
                    taskrunner._services_exchange,
                    "test_taskrunner")


        taskrunner.add_task("dummycommand")
        self.assertRaises(OtsQueueTimeoutError, taskrunner.run)
Example #4
0
    def test_server_side_global_timeout(self):

        # taskrunner with long enough queue timeout to get message processed
        # and very short global timeout to get hit after task started
        taskrunner = TaskRunner("guest", "guest", "localhost", "/", "ots",
                                5672, "test_taskrunner", 1, 1, 1, 1)

        _init_queue(self.channel, "test_taskrunner",
                    taskrunner._services_exchange, "test_taskrunner")

        # Create a task
        task_1 = Task(["1", "2"], 10)

        # Create a "started" state change message

        taskrunner._tasks = [task_1]
        self._publish_message(task_1.task_id, taskrunner._testrun_queue)
        self.assertRaises(OtsExecutionTimeoutError, taskrunner.run)
Example #5
0
 def setUp(self):
     self.taskrunner = TaskRunner("guest", "guest", "localhost",
                                  "/", "ots", 5672, "test_taskrunner", 
                                  1, 60, 1, 1)
     self.connection = amqp.Connection(host = "localhost", 
                                       userid = "guest",
                                       password = "******",
                                       virtual_host = "/", 
                                       insist = False)
     self.channel = self.connection.channel()
     _init_queue(self.channel, 
                 "test_taskrunner", 
                 self.taskrunner._services_exchange,
                 "test_taskrunner")
Example #6
0
    def test_server_side_global_timeout(self):

        # taskrunner with long enough queue timeout to get message processed
        # and very short global timeout to get hit after task started
        taskrunner = TaskRunner("guest", "guest", "localhost",
                                "/", "ots", 5672, "test_taskrunner", 
                                1, 1, 1, 1)

        _init_queue(self.channel, 
                    "test_taskrunner", 
                    taskrunner._services_exchange,
                    "test_taskrunner")


        # Create a task
        task_1 = Task(["1", "2"], 10)
     
        # Create a "started" state change message

        taskrunner._tasks = [task_1] 
        self._publish_message(task_1.task_id, taskrunner._testrun_queue)
        self.assertRaises(OtsExecutionTimeoutError,
                          taskrunner.run)
def taskrunner_factory(routing_key,
                       execution_timeout,
                       testrun_id,
                       config_file=None):
    """
    Instantiate a Taskrunner from the config file

    @type routing_key : C{str}  
    @param routing_key : The routing_key for the Task

    @type execution_timeout: C{int}  
    @param execution_timeout: The timeout for the remote commands

    @type testrun_id: C{int}  
    @param testrun_id: The Testrun id 

    @type config_file: C{str}  
    @param config_file: The fqname of the config file

    @rtype: L{TaskRunner}  
    @return: The TaskRunner
    """
    if not config_file:
        config_file = server_config_filename()

    config = configobj.ConfigObj(config_file).get("ots.server.distributor")

    taskrunner = TaskRunner(username = config["username"],
                            password = config["password"],
                            host = config["host"],
                            vhost = config["vhost"],
                            services_exchange = routing_key,
                            port = config.as_int("port"),
                            routing_key = routing_key,
                            testrun_id = testrun_id,
                            execution_timeout = execution_timeout,
                            queue_timeout = config.as_int("timeout_task_start"),
                            controller_timeout = \
                                config.as_int("timeout_for_preparation"))
    return taskrunner
Example #8
0
class TestTaskRunner(unittest.TestCase):
    def setUp(self):
        self.taskrunner = TaskRunner("guest", "guest", "localhost", "/", "ots",
                                     5672, "test_taskrunner", 1, 60, 1, 1)
        self.connection = amqp.Connection(host="localhost",
                                          userid="guest",
                                          password="******",
                                          virtual_host="/",
                                          insist=False)
        self.channel = self.connection.channel()
        _init_queue(self.channel, "test_taskrunner",
                    self.taskrunner._services_exchange, "test_taskrunner")

    def tearDown(self):
        self.channel.queue_delete(queue="test_taskrunner", nowait=True)

    def test_on_message_status(self):
        task_1 = Task([1, 2], 10)
        task_2 = Task([1, 2], 10)

        start_msg = StateChangeMessage(task_1.task_id, TaskCondition.START)
        message = AMQPMessageStub()
        message.body = dumps(start_msg)

        self.taskrunner._tasks = [task_1, task_2]
        self.taskrunner._on_message(message)
        self.assertEquals(2, len(self.taskrunner._tasks))

        end_msg = StateChangeMessage(task_1.task_id, TaskCondition.FINISH)
        message = AMQPMessageStub()
        message.body = dumps(end_msg)

        self.taskrunner._on_message(message)
        self.assertEquals(1, len(self.taskrunner._tasks))
        self.assertEquals(task_2, self.taskrunner._tasks[0])

    def test_on_message_relay(self):
        message = AMQPMessageStub()
        message.body = dumps(Foo())

        def test_handler(signal, dto, **kwargs):
            self.assertTrue(isinstance(dto, Foo))
            self.assertEquals(1, dto.bar)

        DTO_SIGNAL.connect(test_handler)
        self.taskrunner._on_message(message)

    def test_dispatch_tasks(self):
        task_1 = Task(["1", "2"], 10)
        task_2 = Task(["1", "2"], 10)
        self.taskrunner._tasks = [task_1, task_2]
        self.taskrunner._dispatch_tasks()

        def test_cb(message):
            self.channel.basic_ack(delivery_tag=message.delivery_tag)
            self.assertEquals("1 2", loads(message.body).command)

        self.channel.basic_qos(0, 1, False)
        self.channel.basic_consume(queue="test_taskrunner", callback=test_cb)
        self.channel.wait()
        self.channel.wait()

    def test_wait_for_all_tasks(self):
        class ChannelStub:
            count = 5

            def __init__(self, taskrunner=None):
                self.taskrunner = taskrunner

            def wait(self):
                if self.taskrunner:
                    assert (range(self.count) == self.taskrunner._tasks)
                    self.taskrunner._tasks.pop()
                    self.count -= 1

        self.taskrunner._channel = ChannelStub()
        self.taskrunner._wait_for_all_tasks()
        self.taskrunner._tasks = [0, 1, 2, 3, 4]
        self.taskrunner._channel = ChannelStub(self.taskrunner)
        self.taskrunner._wait_for_all_tasks()

    def test_add_task(self):
        self.taskrunner.add_task([1, 2, 3])
        self.assertEquals(1, len(self.taskrunner._tasks))
        task = self.taskrunner._tasks[0]
        self.assertEquals([1, 2, 3], task.command)
        self.taskrunner._is_run = True
        self.assertRaises(TaskRunnerException, self.taskrunner.add_task, [1])

    def test_cannot_run_multiple_times(self):
        self.taskrunner._is_run = True
        self.assertRaises(TaskRunnerException, self.taskrunner.run)
Example #9
0
class TestTaskRunner(unittest.TestCase):

    def setUp(self):
        self.taskrunner = TaskRunner("guest", "guest", "localhost",
                                     "/", "ots", 5672, "test_taskrunner", 
                                     1, 60, 1, 1)
        self.connection = amqp.Connection(host = "localhost", 
                                          userid = "guest",
                                          password = "******",
                                          virtual_host = "/", 
                                          insist = False)
        self.channel = self.connection.channel()
        _init_queue(self.channel, 
                    "test_taskrunner", 
                    self.taskrunner._services_exchange,
                    "test_taskrunner")

    def tearDown(self):
        self.channel.queue_delete(queue = "test_taskrunner", nowait=True)

    def test_on_message_status(self):
        task_1 = Task([1, 2], 10)
        task_2 = Task([1, 2], 10)
        
        start_msg = StateChangeMessage(task_1.task_id,
                                       TaskCondition.START)
        message = AMQPMessageStub()
        message.body = dumps(start_msg)

        self.taskrunner._tasks = [task_1, task_2] 
        self.taskrunner._on_message(message)
        self.assertEquals(2, len(self.taskrunner._tasks))

        
        end_msg = StateChangeMessage(task_1.task_id,
                                       TaskCondition.FINISH)
        message = AMQPMessageStub()
        message.body = dumps(end_msg)
        
        self.taskrunner._on_message(message)
        self.assertEquals(1, len(self.taskrunner._tasks))
        self.assertEquals(task_2, self.taskrunner._tasks[0])

    def test_on_message_relay(self):
        message = AMQPMessageStub()
        message.body = dumps(Foo())        
        def test_handler(signal, dto, **kwargs):
            self.assertTrue(isinstance(dto, Foo))
            self.assertEquals(1, dto.bar)
        DTO_SIGNAL.connect(test_handler) 
        self.taskrunner._on_message(message)

    def test_dispatch_tasks(self):
        task_1 = Task(["1", "2"], 10)
        task_2 = Task(["1", "2"], 10)
        self.taskrunner._tasks = [task_1, task_2]
        self.taskrunner._dispatch_tasks()
        def test_cb(message):
            self.channel.basic_ack(delivery_tag = message.delivery_tag)
            self.assertEquals("1 2", loads(message.body).command)
        self.channel.basic_qos(0, 1, False)
        self.channel.basic_consume(queue = "test_taskrunner", 
                                   callback = test_cb)
        self.channel.wait()
        self.channel.wait()

    def test_wait_for_all_tasks(self):
        class ChannelStub:
            count = 5
            def __init__(self, taskrunner = None):
                self.taskrunner = taskrunner
            def wait(self):
                if self.taskrunner:
                    assert(range(self.count) == self.taskrunner._tasks)
                    self.taskrunner._tasks.pop()
                    self.count -= 1
        self.taskrunner._channel = ChannelStub()
        self.taskrunner._wait_for_all_tasks()
        self.taskrunner._tasks = [0,1,2,3,4]
        self.taskrunner._channel = ChannelStub(self.taskrunner)
        self.taskrunner._wait_for_all_tasks()

    def test_add_task(self):
        self.taskrunner.add_task([1,2,3])
        self.assertEquals(1, len(self.taskrunner._tasks))
        task = self.taskrunner._tasks[0]
        self.assertEquals([1,2,3], task.command)
        self.taskrunner._is_run = True
        self.assertRaises(TaskRunnerException, self.taskrunner.add_task, [1])


    def test_cannot_run_multiple_times(self):
        self.taskrunner._is_run = True
        self.assertRaises(TaskRunnerException, self.taskrunner.run)