Merge "Change PTL informatin in INFO"
[bottlenecks.git] / testsuites / vstf / vstf_scripts / vstf / controller / settings / tool_settings.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd 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
11 import logging
12 import pprint
13 import vstf.controller.settings.settings as sets
14 import vstf.common.decorator as deco
15 from vstf.common.input import raw_choice
16
17 LOG = logging.getLogger(__name__)
18
19
20 class ToolSettings(sets.Settings):
21
22     def __init__(
23             self,
24             path="/etc/vstf",
25             filename="sw_perf.tool-settings",
26             mode=sets.SETS_DEFAULT):
27         super(ToolSettings, self).__init__(path, filename, mode)
28
29     def _register_func(self):
30         body = set(
31             self._fset.keys()
32         )
33         LOG.debug(body)
34         for item in body:
35             item = item.encode()
36             func_name = "set_%s" % (item)
37             setattr(
38                 self,
39                 func_name,
40                 self._setting_file(
41                     func_name,
42                     self._mset,
43                     self._fset,
44                     item,
45                     check=self._check_keys))
46
47     def _check_keys(self, value):
48         keys = ['threads', 'wait', 'time']
49         if not isinstance(value, dict):
50             raise Exception("type is error: %s" % (str(value)))
51         for key in keys:
52             if key not in value.keys():
53                 raise Exception("keys[%s] is missing: %s" % (key, str(value)))
54
55     def sinput(self):
56         body = set(
57             self._fset.keys()
58         )
59         for tool in body:
60             info = "if set %s properties" % tool
61             if raw_choice(info):
62                 properties = self.raw_properties()
63                 func = getattr(self, "set_%s" % tool)
64                 func(properties)
65
66         print "%s set finish: " % self._filename
67         print "+++++++++++++++++++++++++++++++++++++++++"
68         pprint.pprint(self.settings, indent=4)
69         print "+++++++++++++++++++++++++++++++++++++++++"
70
71     @deco.vstf_input("time", types=int)
72     @deco.vstf_input("wait", types=int)
73     @deco.vstf_input("threads", types=int)
74     def raw_properties(self):
75         print "---------------------------------------"
76         print "Please vstf set tool properties like:"
77         print "    'threads': 2,"
78         print "    'wait': 2,"
79         print "    'time': 10,"
80         print "---------------------------------------"
81
82
83 def unit_test():
84     from vstf.common.log import setup_logging
85     setup_logging(
86         level=logging.DEBUG,
87         log_file="/var/log/vstf/tool-settings.log",
88         clevel=logging.INFO)
89     tool_settings = ToolSettings()
90     value = {
91         "time": 10,
92         "wait": 4,
93         "threads": 1
94     }
95     tool_settings.set_pktgen(value)
96     tool_settings.set_netperf(value)
97     tool_settings.set_iperf(value)
98     tool_settings.set_qperf(value)
99     LOG.info(tool_settings.settings)
100
101
102 if __name__ == '__main__':
103     unit_test()