Merge "Add unit test file for ArithmeticRunner"
[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 import logging
11
12 from yardstick.benchmark.core.task import Task
13 from yardstick.common.utils import cliargs
14 from yardstick.common.utils import write_json_to_file
15 from yardstick.cmd.commands import change_osloobj_to_paras
16
17 output_file_default = "/tmp/yardstick.out"
18
19
20 LOG = logging.getLogger(__name__)
21
22
23 class TaskCommands(object):     # pragma: no cover
24     """Task commands.
25
26        Set of commands to manage benchmark tasks.
27        """
28     EXIT_TEST_FAILED = 2
29
30     @cliargs("inputfile", type=str, help="path to task or suite file", nargs=1)
31     @cliargs("--task-args", dest="task_args",
32              help="Input task args (dict in json). These args are used"
33              "to render input task that is jinja2 template.")
34     @cliargs("--task-args-file", dest="task_args_file",
35              help="Path to the file with input task args (dict in "
36              "json/yaml). These args are used to render input"
37              "task that is jinja2 template.")
38     @cliargs("--keep-deploy", help="keep context deployed in cloud",
39              action="store_true")
40     @cliargs("--parse-only", help="parse the config file and exit",
41              action="store_true")
42     @cliargs("--render-only", help="Render the tasks files, store the result "
43              "in the directory given and exit", type=str, dest="render_only")
44     @cliargs("--output-file", help="file where output is stored, default %s" %
45              output_file_default, default=output_file_default)
46     @cliargs("--suite", help="process test suite file instead of a task file",
47              action="store_true")
48     def do_start(self, args, **kwargs):
49         param = change_osloobj_to_paras(args)
50         self.output_file = param.output_file
51
52         LOG.info('Task START')
53         try:
54             result = Task().start(param, **kwargs)
55         except Exception as e:  # pylint: disable=broad-except
56             self._write_error_data(e)
57             LOG.info('Task FAILED')
58             raise
59         else:
60             if result.get('result', {}).get('criteria') == 'PASS':
61                 LOG.info('Task SUCCESS')
62             else:
63                 LOG.info('Task FAILED')
64                 # exit without backtrace
65                 raise SystemExit(self.EXIT_TEST_FAILED)
66
67     def _write_error_data(self, error):
68         data = {'status': 2, 'result': str(error)}
69         write_json_to_file(self.output_file, data)