Merge "Adding latency test for vfw"
[yardstick.git] / yardstick / network_services / utils.py
1 # Copyright (c) 2016-2017 Intel Corporation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """ Helper function to get Network Service testing configuration """
15
16 from __future__ import absolute_import
17 import logging
18 import os
19
20 from oslo_config import cfg
21 from oslo_config.cfg import NoSuchOptError
22 from oslo_utils import encodeutils
23
24 NSB_ROOT = "/opt/nsb_bin"
25
26 CONF = cfg.CONF
27 OPTS = [
28     cfg.StrOpt('bin_path',
29                default=NSB_ROOT,
30                help='bin_path for VNFs location.'),
31     cfg.StrOpt('trex_path',
32                default=os.path.join(NSB_ROOT, 'trex/scripts'),
33                help='trex automation lib path.'),
34     cfg.StrOpt('trex_client_lib',
35                default=os.path.join(NSB_ROOT, 'trex_client/stl'),
36                help='trex python library path.'),
37 ]
38 CONF.register_opts(OPTS, group="nsb")
39
40
41 def get_nsb_option(option, default=None):
42     """return requested option for yardstick.conf"""
43
44     try:
45         return CONF.nsb.__getitem__(option)
46     except NoSuchOptError:
47         logging.debug("Invalid key %s", option)
48     return default
49
50
51 def provision_tool(connection, tool_path, tool_file=None):
52     """
53     verify if the tool path exits on the node,
54     if not push the local binary to remote node
55
56     :return - Tool path
57     """
58     if tool_file:
59         tool_path = os.path.join(tool_path, tool_file)
60     bin_path = get_nsb_option("bin_path")
61     exit_status = connection.execute("which %s > /dev/null 2>&1" % tool_path)[0]
62     if exit_status == 0:
63         return encodeutils.safe_decode(tool_path, incoming='utf-8').rstrip()
64
65     logging.warning("%s not found on %s, will try to copy from localhost",
66                     tool_path, connection.host)
67     connection.execute('mkdir -p "%s"' % bin_path)
68     connection.put(tool_path, tool_path)
69     return tool_path