summaryrefslogtreecommitdiffstats
path: root/.gitreview
AgeCommit message (Collapse)AuthorFilesLines
2015-07-10Adding .gitreview INFO and LicenseAric Gardner1-0/+5
Change-Id: I31333946c4095c3e96f541f3fcc62cdeabd17a77 Signed-off-by: Aric Gardner <agardner@linuxfoundation.org>
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
# 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

import os
import unittest
import mock

from yardstick.network_services import utils


class UtilsTestCase(unittest.TestCase):
    """Test all VNF helper methods."""

    DPDK_PATH = os.path.join(utils.NSB_ROOT, "dpdk-devbind.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, utils.NSB_ROOT)

    def test_get_nsb_option_is_invalid_key(self):
        result = utils.get_nsb_option("bin", None)
        self.assertEqual(result, None)

    def test_get_nsb_option_default(self):
        default = object()
        result = utils.get_nsb_option("nosuch", default)
        self.assertIs(result, default)

    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)


class PciAddressTestCase(unittest.TestCase):

    PCI_ADDRESS_DBSF = '000A:07:03.2'
    PCI_ADDRESS_BSF = '06:02.1'
    PCI_ADDRESS_DBSF_MULTILINE_1 = '0001:08:04.3\nother text\n'
    PCI_ADDRESS_DBSF_MULTILINE_2 = 'first line\n   0001:08:04.3 \nother text\n'
    # Will match and return the first address found.
    PCI_ADDRESS_DBSF_MULTILINE_3 = '  0001:08:04.1  \n  05:03.1 \nother\n'
    PCI_ADDRESS_BSF_MULTILINE_1 = 'first line\n   08:04.3 \n 0002:05:03.1\n'
    BAD_INPUT_1 = 'no address found'
    BAD_INPUT_2 = '001:08:04.1'
    BAD_INPUT_3 = '08:4.1'

    def test_pciaddress_dbsf(self):
        pci_address = utils.PciAddress(PciAddressTestCase.PCI_ADDRESS_DBSF)
        self.assertEqual('000a', pci_address.domain)
        self.assertEqual('07', pci_address.bus)
        self.assertEqual('03', pci_address.slot)
        self.assertEqual('2', pci_address.function)

    def test_pciaddress_bsf(self):
        pci_address = utils.PciAddress(PciAddressTestCase.PCI_ADDRESS_BSF)
        self.assertEqual('0000', pci_address.domain)
        self.assertEqual('06', pci_address.bus)
        self.assertEqual('02', pci_address.slot)
        self.assertEqual('1', pci_address.function)

    def test_pciaddress_dbsf_multiline_1(self):
        pci_address = utils.PciAddress(
            PciAddressTestCase.PCI_ADDRESS_DBSF_MULTILINE_1)
        self.assertEqual('0001', pci_address.domain)
        self.assertEqual('08', pci_address.bus)
        self.assertEqual('04', pci_address.slot)
        self.assertEqual('3', pci_address.function)

    def test_pciaddress_dbsf_multiline_2(self):
        pci_address = utils.PciAddress(
            PciAddressTestCase.PCI_ADDRESS_DBSF_MULTILINE_2)
        self.assertEqual('0001', pci_address.domain)
        self.assertEqual('08', pci_address.bus)
        self.assertEqual('04', pci_address.slot)
        self.assertEqual('3', pci_address.function)

    def test_pciaddress_dbsf_multiline_3(self):
        pci_address = utils.PciAddress(
            PciAddressTestCase.PCI_ADDRESS_DBSF_MULTILINE_3)
        self.assertEqual('0001', pci_address.domain)
        self.assertEqual('08', pci_address.bus)
        self.assertEqual('04', pci_address.slot)
        self.assertEqual('1', pci_address.function)

    def test_pciaddress_bsf_multiline_1(self):
        pci_address = utils.PciAddress(
            PciAddressTestCase.PCI_ADDRESS_BSF_MULTILINE_1)
        self.assertEqual('0000', pci_address.domain)
        self.assertEqual('08', pci_address.bus)
        self.assertEqual('04', pci_address.slot)
        self.assertEqual('3', pci_address.function)

    def test_pciaddress_bad_input_no_address(self):
        with self.assertRaises(ValueError) as exception:
            utils.PciAddress(PciAddressTestCase.BAD_INPUT_1)
        self.assertEqual('Invalid PCI address: {}'.format(
                PciAddressTestCase.BAD_INPUT_1), str(exception.exception))

    def test_pciaddress_bad_input_dbsf_bad_formatted(self):
        # In this test case, the domain has only 3 characters instead of 4.
        pci_address = utils.PciAddress(
            PciAddressTestCase.BAD_INPUT_2)
        self.assertEqual('0000', pci_address.domain)
        self.assertEqual('08', pci_address.bus)
        self.assertEqual('04', pci_address.slot)
        self.assertEqual('1', pci_address.function)

    def test_pciaddress_bad_input_bsf_bad_formatted(self):
        with self.assertRaises(ValueError) as exception:
            utils.PciAddress(PciAddressTestCase.BAD_INPUT_3)
        self.assertEqual('Invalid PCI address: {}'.format(
                PciAddressTestCase.BAD_INPUT_3), str(exception.exception))