JIRA: BOTTLENECKS-29
[bottlenecks.git] / vstf / 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     def __init__(self, path="/etc/vstf", filename="sw_perf.tool-settings", mode=sets.SETS_DEFAULT):
22         super(ToolSettings, self).__init__(path, filename, mode)
23
24     def _register_func(self):
25         body = set(
26             self._fset.keys()
27         )
28         LOG.debug(body)
29         for item in body:
30             item = item.encode()
31             func_name = "set_%s" % (item)
32             setattr(self, func_name,
33                     self._setting_file(func_name, self._mset, self._fset, item, check=self._check_keys))
34
35     def _check_keys(self, value):
36         keys = ['threads', 'wait', 'time']
37         if not isinstance(value, dict):
38             raise Exception("type is error: %s" % (str(value)))
39         for key in keys:
40             if key not in value.keys():
41                 raise Exception("keys[%s] is missing: %s" % (key, str(value)))
42
43     def sinput(self):
44         body = set(
45             self._fset.keys()
46         )
47         for tool in body:
48             info = "if set %s properties" % tool
49             if raw_choice(info):
50                 properties = self.raw_properties()
51                 func = getattr(self, "set_%s" % tool)
52                 func(properties)
53
54         print "%s set finish: " % self._filename
55         print "+++++++++++++++++++++++++++++++++++++++++"
56         pprint.pprint(self.settings, indent=4)
57         print "+++++++++++++++++++++++++++++++++++++++++"
58
59     @deco.vstf_input("time", types=int)
60     @deco.vstf_input("wait", types=int)
61     @deco.vstf_input("threads", types=int)
62     def raw_properties(self):
63         print "---------------------------------------"
64         print "Please vstf set tool properties like:"
65         print "    'threads': 2,"
66         print "    'wait': 2,"
67         print "    'time': 10,"
68         print "---------------------------------------"
69
70
71 def unit_test():
72     from vstf.common.log import setup_logging
73     setup_logging(level=logging.DEBUG, log_file="/var/log/vstf/tool-settings.log", clevel=logging.INFO)
74     tool_settings = ToolSettings()
75     value = {
76         "time": 10,
77         "wait": 4,
78         "threads": 1
79     }
80     tool_settings.set_pktgen(value)
81     tool_settings.set_netperf(value)
82     tool_settings.set_iperf(value)
83     tool_settings.set_qperf(value)
84     LOG.info(tool_settings.settings)
85
86
87 if __name__ == '__main__':
88     unit_test()