Merge "add support for suite to support constraints and task_args"
[yardstick.git] / yardstick / benchmark / scenarios / parser / parser.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and other.
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 import pkg_resources
10 import logging
11 import subprocess
12 from yardstick.benchmark.scenarios import base
13
14 LOG = logging.getLogger(__name__)
15
16
17 class Parser(base.Scenario):
18     """running Parser Yang-to-Tosca module as a tool
19     validating output against expected outcome
20
21     more info https://wiki.opnfv.org/parser
22     """
23     __scenario_type__ = "Parser"
24
25     SETUP_SCRIPT = "parser_setup.sh"
26     TEARDOWN_SCRIPT = "parser_teardown.sh"
27     PARSER_SCRIPT = "parser.sh"
28
29     def __init__(self, scenario_cfg, context_cfg):
30         self.scenario_cfg = scenario_cfg
31         self.context_cfg = context_cfg
32         self.setup_done = False
33
34     def setup(self):
35         """scenario setup"""
36         self.setup_script = pkg_resources.resource_filename(
37             "yardstick.benchmark.scenarios.parser",
38             Parser.SETUP_SCRIPT)
39         cmd = "%s" % (self.setup_script)
40
41         subprocess.call(cmd, shell=True)
42
43         self.setup_done = True
44
45     def run(self, result):
46         """execute the translation"""
47         options = self.scenario_cfg['options']
48         yangfile = options.get("yangfile", '~/yardstick/samples/yang.yaml')
49         toscafile = options.get("toscafile", '~/yardstick/samples/tosca.yaml')
50
51         self.parser_script = pkg_resources.resource_filename(
52             "yardstick.benchmark.scenarios.parser",
53             Parser.PARSER_SCRIPT)
54
55         if not self.setup_done:
56             self.setup()
57
58         cmd1 = "%s %s %s" % (self.parser_script, yangfile, toscafile)
59         cmd2 = "chmod 777 %s" % (self.parser_script)
60         subprocess.call(cmd2, shell=True)
61         output = subprocess.call(cmd1, shell=True, stdout=subprocess.PIPE)
62         print "yangtotosca finished"
63
64         result['yangtotosca'] = "success" if output == 0 else "fail"
65
66     def teardown(self):
67         ''' for scenario teardown remove parser and pyang '''
68         self.teardown_script = pkg_resources.resource_filename(
69             "yardstick.benchmark.scenarios.parser",
70             Parser.TEARDOWN_SCRIPT)
71         subprocess.call(self.teardown_script, shell=True)
72         self.teardown_done = True
73
74
75 def _test():
76     '''internal test function'''
77     pass
78
79 if __name__ == '__main__':
80     _test()