Merge "Add task-args(from yaml file) candidates in /api/v2/yardstick/testcases API"
[yardstick.git] / yardstick / cmd / commands / task.py
1 #############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 """ Handler for yardstick command 'task' """
11 from __future__ import print_function
12 from __future__ import absolute_import
13
14 import logging
15
16 from yardstick.benchmark.core.task import Task
17 from yardstick.common.utils import cliargs
18 from yardstick.common.utils import write_json_to_file
19 from yardstick.cmd.commands import change_osloobj_to_paras
20
21 output_file_default = "/tmp/yardstick.out"
22
23
24 LOG = logging.getLogger(__name__)
25
26
27 class TaskCommands(object):     # pragma: no cover
28     """Task commands.
29
30        Set of commands to manage benchmark tasks.
31        """
32
33     @cliargs("inputfile", type=str, help="path to task or suite file", nargs=1)
34     @cliargs("--task-args", dest="task_args",
35              help="Input task args (dict in json). These args are used"
36              "to render input task that is jinja2 template.")
37     @cliargs("--task-args-file", dest="task_args_file",
38              help="Path to the file with input task args (dict in "
39              "json/yaml). These args are used to render input"
40              "task that is jinja2 template.")
41     @cliargs("--keep-deploy", help="keep context deployed in cloud",
42              action="store_true")
43     @cliargs("--parse-only", help="parse the config file and exit",
44              action="store_true")
45     @cliargs("--output-file", help="file where output is stored, default %s" %
46              output_file_default, default=output_file_default)
47     @cliargs("--suite", help="process test suite file instead of a task file",
48              action="store_true")
49     def do_start(self, args, **kwargs):
50         param = change_osloobj_to_paras(args)
51         self.output_file = param.output_file
52
53         try:
54             result = Task().start(param, **kwargs)
55         except Exception as e:
56             self._write_error_data(e)
57             LOG.exception("")
58
59         if result.get('result', {}).get('criteria') == 'PASS':
60             LOG.info('Task Success')
61         else:
62             LOG.info('Task Failed')
63             raise RuntimeError('Task Failed')
64
65     def _write_error_data(self, error):
66         data = {'status': 2, 'result': str(error)}
67         write_json_to_file(self.output_file, data)