Merge "Change the name of flavor and disabled ssh-hostname verification."
[functest.git] / functest / api / common / thread.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 """
11 Used to handle multi-thread tasks
12 """
13
14 import logging
15 import threading
16
17 from oslo_serialization import jsonutils
18
19
20 LOGGER = logging.getLogger(__name__)
21
22
23 class TaskThread(threading.Thread):
24     """ Task Thread Class """
25
26     def __init__(self, target, args, handler):
27         super(TaskThread, self).__init__(target=target, args=args)
28         self.target = target
29         self.args = args
30         self.handler = handler
31
32     def run(self):
33         """ Override the function run: run testcase and update database """
34         update_data = {'task_id': self.args.get('task_id'),
35                        'status': 'IN PROGRESS'}
36         self.handler.insert(update_data)
37
38         LOGGER.info('Starting running test case')
39
40         try:
41             data = self.target(self.args)
42         except Exception as err:  # pylint: disable=broad-except
43             LOGGER.exception('Task Failed')
44             update_data = {'status': 'FAIL', 'error': str(err)}
45             self.handler.update_attr(self.args.get('task_id'), update_data)
46         else:
47             LOGGER.info('Task Finished')
48             LOGGER.debug('Result: %s', data)
49             new_data = {'status': 'FINISHED',
50                         'result': jsonutils.dumps(data.get('result', {}))}
51
52             self.handler.update_attr(self.args.get('task_id'), new_data)