1 # Copyright (c) 2016-2017 Intel Corporation
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
20 from yardstick.network_services import utils
23 class UtilsTestCase(unittest.TestCase):
24 """Test all VNF helper methods."""
26 DPDK_PATH = os.path.join(utils.NSB_ROOT, "dpdk-devbind.py")
29 super(UtilsTestCase, self).setUp()
31 def test_get_nsb_options(self):
32 result = utils.get_nsb_option("bin_path", None)
33 self.assertEqual(result, utils.NSB_ROOT)
35 def test_get_nsb_option_is_invalid_key(self):
36 result = utils.get_nsb_option("bin", None)
37 self.assertEqual(result, None)
39 def test_get_nsb_option_default(self):
41 result = utils.get_nsb_option("nosuch", default)
42 self.assertIs(result, default)
44 def test_provision_tool(self):
45 with mock.patch("yardstick.ssh.SSH") as ssh:
46 ssh_mock = mock.Mock(autospec=ssh.SSH)
48 mock.Mock(return_value=(0, self.DPDK_PATH, ""))
49 ssh.return_value = ssh_mock
50 tool_path = utils.provision_tool(ssh_mock, self.DPDK_PATH)
51 self.assertEqual(tool_path, self.DPDK_PATH)
53 def test_provision_tool_no_path(self):
54 with mock.patch("yardstick.ssh.SSH") as ssh:
55 ssh_mock = mock.Mock(autospec=ssh.SSH)
57 mock.Mock(return_value=(1, self.DPDK_PATH, ""))
58 ssh.return_value = ssh_mock
59 tool_path = utils.provision_tool(ssh_mock, self.DPDK_PATH)
60 self.assertEqual(tool_path, self.DPDK_PATH)
63 class PciAddressTestCase(unittest.TestCase):
65 PCI_ADDRESS_DBSF = '000A:07:03.2'
66 PCI_ADDRESS_BSF = '06:02.1'
67 PCI_ADDRESS_DBSF_MULTILINE_1 = '0001:08:04.3\nother text\n'
68 PCI_ADDRESS_DBSF_MULTILINE_2 = 'first line\n 0001:08:04.3 \nother text\n'
69 # Will match and return the first address found.
70 PCI_ADDRESS_DBSF_MULTILINE_3 = ' 0001:08:04.1 \n 05:03.1 \nother\n'
71 PCI_ADDRESS_BSF_MULTILINE_1 = 'first line\n 08:04.3 \n 0002:05:03.1\n'
72 BAD_INPUT_1 = 'no address found'
73 BAD_INPUT_2 = '001:08:04.1'
74 BAD_INPUT_3 = '08:4.1'
76 def test_pciaddress_dbsf(self):
77 pci_address = utils.PciAddress(PciAddressTestCase.PCI_ADDRESS_DBSF)
78 self.assertEqual('000a', pci_address.domain)
79 self.assertEqual('07', pci_address.bus)
80 self.assertEqual('03', pci_address.slot)
81 self.assertEqual('2', pci_address.function)
83 def test_pciaddress_bsf(self):
84 pci_address = utils.PciAddress(PciAddressTestCase.PCI_ADDRESS_BSF)
85 self.assertEqual('0000', pci_address.domain)
86 self.assertEqual('06', pci_address.bus)
87 self.assertEqual('02', pci_address.slot)
88 self.assertEqual('1', pci_address.function)
90 def test_pciaddress_dbsf_multiline_1(self):
91 pci_address = utils.PciAddress(
92 PciAddressTestCase.PCI_ADDRESS_DBSF_MULTILINE_1)
93 self.assertEqual('0001', pci_address.domain)
94 self.assertEqual('08', pci_address.bus)
95 self.assertEqual('04', pci_address.slot)
96 self.assertEqual('3', pci_address.function)
98 def test_pciaddress_dbsf_multiline_2(self):
99 pci_address = utils.PciAddress(
100 PciAddressTestCase.PCI_ADDRESS_DBSF_MULTILINE_2)
101 self.assertEqual('0001', pci_address.domain)
102 self.assertEqual('08', pci_address.bus)
103 self.assertEqual('04', pci_address.slot)
104 self.assertEqual('3', pci_address.function)
106 def test_pciaddress_dbsf_multiline_3(self):
107 pci_address = utils.PciAddress(
108 PciAddressTestCase.PCI_ADDRESS_DBSF_MULTILINE_3)
109 self.assertEqual('0001', pci_address.domain)
110 self.assertEqual('08', pci_address.bus)
111 self.assertEqual('04', pci_address.slot)
112 self.assertEqual('1', pci_address.function)
114 def test_pciaddress_bsf_multiline_1(self):
115 pci_address = utils.PciAddress(
116 PciAddressTestCase.PCI_ADDRESS_BSF_MULTILINE_1)
117 self.assertEqual('0000', pci_address.domain)
118 self.assertEqual('08', pci_address.bus)
119 self.assertEqual('04', pci_address.slot)
120 self.assertEqual('3', pci_address.function)
122 def test_pciaddress_bad_input_no_address(self):
123 with self.assertRaises(ValueError) as exception:
124 utils.PciAddress(PciAddressTestCase.BAD_INPUT_1)
125 self.assertEqual('Invalid PCI address: {}'.format(
126 PciAddressTestCase.BAD_INPUT_1), str(exception.exception))
128 def test_pciaddress_bad_input_dbsf_bad_formatted(self):
129 # In this test case, the domain has only 3 characters instead of 4.
130 pci_address = utils.PciAddress(
131 PciAddressTestCase.BAD_INPUT_2)
132 self.assertEqual('0000', pci_address.domain)
133 self.assertEqual('08', pci_address.bus)
134 self.assertEqual('04', pci_address.slot)
135 self.assertEqual('1', pci_address.function)
137 def test_pciaddress_bad_input_bsf_bad_formatted(self):
138 with self.assertRaises(ValueError) as exception:
139 utils.PciAddress(PciAddressTestCase.BAD_INPUT_3)
140 self.assertEqual('Invalid PCI address: {}'.format(
141 PciAddressTestCase.BAD_INPUT_3), str(exception.exception))