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