Call core code directly in the API of run test case
[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 from yardstick.benchmark.core.task import Task
15 from yardstick.common.utils import cliargs
16 from yardstick.common.utils import write_json_to_file
17 from yardstick.cmd.commands import change_osloobj_to_paras
18
19 output_file_default = "/tmp/yardstick.out"
20
21
22 class TaskCommands(object):     # pragma: no cover
23     """Task commands.
24
25        Set of commands to manage benchmark tasks.
26        """
27
28     @cliargs("inputfile", type=str, help="path to task or suite file", nargs=1)
29     @cliargs("--task-args", dest="task_args",
30              help="Input task args (dict in json). These args are used"
31              "to render input task that is jinja2 template.")
32     @cliargs("--task-args-file", dest="task_args_file",
33              help="Path to the file with input task args (dict in "
34              "json/yaml). These args are used to render input"
35              "task that is jinja2 template.")
36     @cliargs("--keep-deploy", help="keep context deployed in cloud",
37              action="store_true")
38     @cliargs("--parse-only", help="parse the config file and exit",
39              action="store_true")
40     @cliargs("--output-file", help="file where output is stored, default %s" %
41              output_file_default, default=output_file_default)
42     @cliargs("--suite", help="process test suite file instead of a task file",
43              action="store_true")
44     def do_start(self, args, **kwargs):
45         param = change_osloobj_to_paras(args)
46         self.output_file = param.output_file
47
48         try:
49             Task().start(param, **kwargs)
50         except Exception as e:
51             self._write_error_data(e)
52             raise
53
54     def _write_error_data(self, error):
55         data = {'status': 2, 'result': str(error)}
56         write_json_to_file(self.output_file, data)