Merge "Change PTL informatin in INFO"
[bottlenecks.git] / testsuites / vstf / vstf_scripts / vstf / controller / vstfadm.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 import sys
11 import logging
12 import json
13 from vstf.common.vstfcli import VstfParser
14 from vstf.common import cliutil, constants, unix, message
15 from vstf.common.log import setup_logging
16 import vstf.common.constants as cst
17 import pprint
18
19 CONN = None
20
21
22 def print_stdout(msg):
23     # out = json.dumps(message.get_body(message.decode(msg)), indent=2)
24     out = message.get_body(message.decode(msg))
25     pprint.pprint(out, indent=2)
26
27
28 def call(msg):
29     """msg must be a dict"""
30     msg = message.add_context(msg, corr=message.gen_corrid())
31     CONN.send(message.encode(msg))
32     return message.decode(CONN.recv())
33
34
35 def make_msg(method, **kwargs):
36     return {"method": method, "args": kwargs}
37
38
39 @cliutil.arg("--host", dest="host", default="", action="store",
40              help="list nic devices of specified host")
41 def do_list_devs(args):
42     """List the host's all netdev."""
43     ret = call(make_msg("list_devs", host=args.host))
44     print_stdout(ret)
45
46
47 @cliutil.arg("--host", dest="host", action="store", default=None,
48              help="which host to run src_install.")
49 @cliutil.arg("--config_file", dest="config_file", action="store", default=None,
50              help="the git repo config.")
51 def do_src_install(args):
52     """work agent to pull source code and compile.
53     use git as underlying mechanism, please make sure the host has access to git repo.
54     """
55     ret = call(
56         make_msg(
57             "src_install",
58             host=args.host,
59             config_file=args.config_file))
60     print_stdout(ret)
61
62
63 @cliutil.arg(
64     "--host",
65     dest="host",
66     action="store",
67     default=None,
68     help="which host to build, must exists in your config file, use default[None] value to build all hosts.")
69 @cliutil.arg(
70     "--model",
71     dest="model",
72     action="store",
73     choices=(
74         'Tn',
75         'Ti',
76         'Tu',
77         'Tnv'),
78     help="which model to build, if specified, the according config file /etc/vstf/env/{model}.json must exist.")
79 @cliutil.arg(
80     "--config_file",
81     dest="config_file",
82     action="store",
83     default=None,
84     help="if specified, the config file will replace the default config file from /etc/vstf/env.")
85 def do_apply_model(args):
86     """Apply model to the host."""
87     ret = call(
88         make_msg(
89             "apply_model",
90             host=args.host,
91             model=args.model,
92             config_file=args.config_file))
93     print_stdout(ret)
94
95
96 @cliutil.arg("--host", dest="host", action="store", default=None,
97              help="to which host you wish to create images")
98 @cliutil.arg("--config_file", dest="config_file", action="store", default=None,
99              help="configuration file for image creation.")
100 def do_create_images(args):
101     """create images on host, images are configed by configuration file."""
102     ret = call(
103         make_msg(
104             "create_images",
105             host=args.host,
106             config_file=args.config_file))
107     print_stdout(ret)
108
109
110 @cliutil.arg("--host", dest="host", action="store", default=None,
111              help="to which host you wish to clean images")
112 @cliutil.arg("--config_file", dest="config_file", action="store", default=None,
113              help="configuration file for images.")
114 def do_clean_images(args):
115     """clean images on host, images are configed by configuration file."""
116     ret = call(
117         make_msg(
118             "clean_images",
119             host=args.host,
120             config_file=args.config_file))
121     print_stdout(ret)
122
123
124 @cliutil.arg(
125     "--host",
126     dest="host",
127     action="store",
128     default=None,
129     help="which host to clean, must exists in your config file, use default[None] value to clean all hosts.")
130 @cliutil.arg(
131     "--model",
132     dest="model",
133     action="store",
134     choices=(
135         'Tn',
136         'Ti',
137         'Tu',
138         'Tnv'),
139     help="if specified, the according config file /etc/vstf/env/{model}.json must exist.")
140 @cliutil.arg(
141     "--config_file",
142     dest="config_file",
143     action="store",
144     default=None,
145     help="if specified, the config file will replace the default config file from /etc/vstf/env.")
146 def do_disapply_model(args):
147     """Apply model to the host."""
148     ret = call(
149         make_msg(
150             "disapply_model",
151             host=args.host,
152             model=args.model,
153             config_file=args.config_file))
154     print_stdout(ret)
155
156
157 @cliutil.arg("--host", dest="host", action="store",
158              help="collect host information about cpu/mem etc")
159 def do_collect_host_info(args):
160     """Show the host's CPU/MEN info"""
161     ret = call(make_msg("collect_host_info", target=args.host))
162     print_stdout(ret)
163
164
165 def do_show_tasks(args):
166     """List history performance test tasks. Can be used by report cmd to generate reports.
167     """
168     ret = call(make_msg("list_tasks"))
169     print_stdout(ret)
170
171
172 @cliutil.arg(
173     "case",
174     action="store",
175     help="test case like Ti-1, Tn-1, Tnv-1, Tu-1, see case definition in documents")
176 @cliutil.arg("tool", action="store", choices=cst.TOOLS)
177 @cliutil.arg("protocol", action="store", choices=cst.TPROTOCOLS)
178 @cliutil.arg("type", action="store", choices=cst.TTYPES)
179 @cliutil.arg(
180     "sizes",
181     action="store",
182     default="64",
183     help='test size list "64 128"')
184 @cliutil.arg(
185     "--affctl",
186     action="store_true",
187     help="when affctl is True, it will do affctl before testing")
188 def do_perf_test(args):
189     """Runs a quick single software performance test without envbuild and generating reports.
190     Outputs the result to the stdout immediately."""
191     case_info = {
192         'case': args.case,
193         'tool': args.tool,
194         'protocol': args.protocol,
195         'type': args.type,
196         'sizes': map(lambda x: int(x), args.sizes.strip().split())
197     }
198     ret = call(make_msg("run_perf_cmd",
199                         case=case_info,
200                         rpath=cst.REPORT_DEFAULTS,
201                         affctl=args.affctl,
202                         build_on=False,
203                         save_on=False,
204                         report_on=False,
205                         mail_on=False
206                         ))
207     print_stdout(ret)
208
209
210 @cliutil.arg("-rpath",
211              help="path of result",
212              default=cst.REPORT_DEFAULTS,
213              action="store")
214 @cliutil.arg("--report_off",
215              help="when report_off is True, it will not generate the report",
216              action="store_true")
217 @cliutil.arg("--mail_off",
218              help="when mail_off is True, it will not send mail",
219              action="store_true")
220 @cliutil.arg("--affctl",
221              help="when affctl is True, it will do affctl before testing",
222              action="store_true")
223 def do_batch_perf_test(args):
224     """run soft performance test cases defined in /etc/vstf/perf/sw_perf.batch-settings"""
225     ret = call(make_msg("run_perf_file",
226                         affctl=args.affctl,
227                         rpath=args.rpath,
228                         report_on=not args.report_off,
229                         mail_on=not args.mail_off
230                         ))
231     print_stdout(ret)
232
233
234 @cliutil.arg('-rpath',
235              action='store',
236              default=cst.REPORT_DEFAULTS,
237              help=" the path name of test results  ")
238 @cliutil.arg("--mail_off",
239              help="when mail_off is True, it will not send mail",
240              action="store_true")
241 @cliutil.arg("--taskid",
242              help="report depend of a history task id",
243              default=-1,
244              action="store")
245 def do_report(args):
246     """generate the report from the database"""
247     ret = call(make_msg("report",
248                         rpath=args.rpath,
249                         mail_off=args.mail_off,
250                         taskid=args.taskid
251                         ))
252     print_stdout(ret)
253
254
255 @cliutil.arg("--conner",
256              dest="conner",
257              action="store",
258              help="tester")
259 @cliutil.arg("--measurand",
260              dest="measurand",
261              action="store",
262              help="tested")
263 @cliutil.arg("-m", "--model",
264              dest="model",
265              action="store",
266              help="Test scene name : Tnv")
267 @cliutil.arg("-e", "--virtenv",
268              dest="virtenv",
269              action="store",
270              help="virt env_build number(s): [1-8]")
271 @cliutil.arg("-q", "--queues",
272              dest="queues",
273              action="store",
274              help="VM nic queues.")
275 @cliutil.arg("-f", "--flows",
276              dest="flows",
277              action="store",
278              help="Flow queue(s) : [1-8]")
279 @cliutil.arg("-v", "--vlans",
280              dest="vlans",
281              action="store_true",
282              help="vlan setting : 100-150;200-250")
283 @cliutil.arg("-d", "--direct",
284              dest="direct",
285              action="store",
286              choices=["single", "double"],
287              help="Flow Direction")
288 @cliutil.arg("-b", "--bind",
289              dest="strategy",
290              action="store",
291              help="CPU bind strategy :  1 | 2 | 3 ")
292 @cliutil.arg("--config_file",
293              dest="config_file",
294              default='/etc/vstf/spirent/optimize.ini',
295              action="store",
296              help="config file for optimize.")
297 @cliutil.arg("--strategyfile",
298              dest="strategyfile",
299              default='/etc/vstf/spirent/strategy.ini',
300              action="store",
301              help="config file for strategy.")
302 def do_spirent_test(args):
303     ret = call(make_msg("perf_test",
304                         plugin="spirent",
305                         conner=args.conner,
306                         measurand=args.measurand,
307                         virtenv=args.virtenv,
308                         queues=args.queues,
309                         direct=args.direct,
310                         flows=args.flows,
311                         strategy=args.strategy,
312                         model=args.model,
313                         vlans=args.vlans,
314                         configfile=args.config_file,
315                         strategyfile=args.strategyfile))
316     print_stdout(ret)
317
318
319 @cliutil.arg("--host", dest="host", action="store", default=None,
320              help="which host to list affctl info")
321 def do_affctl_list(args):
322     ret = call(make_msg("affctl_list", host=args.host))
323     print_stdout(ret)
324
325
326 @cliutil.arg("head", action="store", help="ip of head")
327 @cliutil.arg("tail", action="store", help="ip of tail")
328 def do_settings(args):
329     ret = call(make_msg("settings", head=args.head, tail=args.tail))
330     print_stdout(ret)
331
332
333 def main():
334     parser = VstfParser(prog="vstfadm", description="vstf administration")
335     parser.set_subcommand_parser(sys.modules[__name__], "functions")
336     args = parser.parse_args()
337     if args.func is None:
338         sys.exit(-1)
339     setup_logging(
340         level=logging.DEBUG,
341         log_file="/var/log/vstf/vstf-adm.log",
342         clevel=logging.INFO)
343     # connect to manage
344     global CONN
345     try:
346         CONN = unix.UdpClient()
347         CONN.connect(constants.sockaddr)
348     except Exception as e:
349         raise e
350
351     args.func(args)
352     # call functions of manage
353     sys.exit(CONN.close())