Merge "[bugfix]tc006 failed due to volume attached to different location "/dev/vdc""
[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
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("--render-only", help="Render the tasks files, store the result "
42              "in the directory given and exit", type=str, dest="render_only")
43     @cliargs("--output-file", help="file where output is stored, default %s" %
44              output_file_default, default=output_file_default)
45     @cliargs("--suite", help="process test suite file instead of a task file",
46              action="store_true")
47     def do_start(self, args, **kwargs):
48         param = change_osloobj_to_paras(args)
49         self.output_file = param.output_file
50
51         result = {}
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
58         if result.get('result', {}).get('criteria') == 'PASS':
59             LOG.info('Task SUCCESS')
60         else:
61             LOG.info('Task FAILED')
62             raise RuntimeError('Task Failed')
63
64     def _write_error_data(self, error):
65         data = {'status': 2, 'result': str(error)}
66         write_json_to_file(self.output_file, data)