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