diff options
author | Rex Lee <limingjiang@huawei.com> | 2017-01-24 08:45:19 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2017-01-24 08:45:19 +0000 |
commit | 7d22c6c852ea0af41bda232c05be6a09fb816737 (patch) | |
tree | 75636603f78fcfb86c18fd6606d268d7fda13886 | |
parent | 5babed1aa6b2f867e639193841b231c3a0200b87 (diff) | |
parent | 6f8fbe8acb94448ef2eb188193e9bea534021e89 (diff) |
Merge "Generic helper function to provision and get path from config"
-rw-r--r-- | tests/unit/network_services/test_utils.py | 57 | ||||
-rw-r--r-- | yardstick/network_services/utils.py | 62 |
2 files changed, 119 insertions, 0 deletions
diff --git a/tests/unit/network_services/test_utils.py b/tests/unit/network_services/test_utils.py new file mode 100644 index 000000000..ecacac7c3 --- /dev/null +++ b/tests/unit/network_services/test_utils.py @@ -0,0 +1,57 @@ +# Copyright (c) 2016-2017 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# Unittest for yardstick.network_services.utils + +from __future__ import absolute_import +import unittest +import mock + +from yardstick.network_services import utils + + +class UtilsTestCase(unittest.TestCase): + """Test all VNF helper methods.""" + + DPDK_PATH = "/opt/nsb_bin/dpdk_nic_bind.py" + + def setUp(self): + super(UtilsTestCase, self).setUp() + + def test_get_nsb_options(self): + result = utils.get_nsb_option("bin_path", None) + self.assertEqual(result, "/opt/nsb_bin") + + def test_get_nsb_optionsi_invalid_key(self): + result = utils.get_nsb_option("bin", None) + self.assertEqual(result, None) + + def test_provision_tool(self): + with mock.patch("yardstick.ssh.SSH") as ssh: + ssh_mock = mock.Mock(autospec=ssh.SSH) + ssh_mock.execute = \ + mock.Mock(return_value=(0, self.DPDK_PATH, "")) + ssh.return_value = ssh_mock + tool_path = utils.provision_tool(ssh_mock, self.DPDK_PATH) + self.assertEqual(tool_path, self.DPDK_PATH) + + def test_provision_tool_no_path(self): + with mock.patch("yardstick.ssh.SSH") as ssh: + ssh_mock = mock.Mock(autospec=ssh.SSH) + ssh_mock.execute = \ + mock.Mock(return_value=(1, self.DPDK_PATH, "")) + ssh.return_value = ssh_mock + tool_path = utils.provision_tool(ssh_mock, self.DPDK_PATH) + self.assertEqual(tool_path, self.DPDK_PATH) diff --git a/yardstick/network_services/utils.py b/yardstick/network_services/utils.py new file mode 100644 index 000000000..80e0a631c --- /dev/null +++ b/yardstick/network_services/utils.py @@ -0,0 +1,62 @@ +# Copyright (c) 2016-2017 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" Helper function to get Network Service testing configuration """ + +from __future__ import absolute_import +import logging + +from oslo_config import cfg +from oslo_config.cfg import NoSuchOptError +from oslo_utils import encodeutils + +CONF = cfg.CONF +OPTS = [ + cfg.StrOpt('bin_path', + default='/opt/nsb_bin', + help='bin_path for VNFs location.'), + cfg.StrOpt('trex_path', + default='/opt/nsb_bin/trex/scripts', + help='trex automation lib pathh.'), +] +CONF.register_opts(OPTS, group="nsb") + + +def get_nsb_option(option, default=None): + """return requested option for yardstick.conf""" + + try: + return CONF.nsb.__getitem__(option) + except NoSuchOptError: + logging.debug("Invalid key %s", option) + else: + return default + + +def provision_tool(connection, tool_path): + """ + verify if the tool path exits on the node, + if not push the local binary to remote node + + :return - Tool path + """ + bin_path = get_nsb_option("bin_path") + exit_status, stdout = connection.execute("which %s" % tool_path)[:2] + if exit_status == 0: + return encodeutils.safe_encode(stdout, incoming='utf-8').rstrip() + + logging.warning("%s not found on %s, will try to copy from localhost", + tool_path, connection.host) + connection.execute('mkdir -p "%s"' % bin_path) + connection.put(tool_path, tool_path) + return tool_path |