summaryrefslogtreecommitdiffstats
path: root/tests/unit/benchmark/contexts/test_ovsdpdk.py
blob: 125e475af76cad3db1e88f342c0e94db14dd1b52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# 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.

from __future__ import absolute_import
import os
import mock
import unittest

from yardstick.benchmark.contexts import ovsdpdk
from yardstick.benchmark.contexts.ovsdpdk import Ovsdpdk

NIC_INPUT = {
    'interface': {},
    'vports_mac': ['00:00:00:71:7d:25', '00:00:00:71:7d:26'],
    'pci': ['0000:06:00.0', '0000:06:00.1'],
    'phy_driver': 'i40e'}
DRIVER = "i40e"
NIC_DETAILS = {
    'interface': {0: 'enp6s0f0', 1: 'enp6s0f1'},
    'vports_mac': ['00:00:00:71:7d:25', '00:00:00:71:7d:26'],
    'pci': ['0000:06:00.0', '0000:06:00.1'],
    'phy_driver': 'i40e'}

CORRECT_FILE_PATH = "/etc/yardstick/nodes/pod_ovs.yaml"
WRONG_FILE_PATH = "/etc/yardstick/wrong.yaml"
SAMPLE_FILE = "ovs_sample_write_to_file.txt"

OVS = [{
    'auth_type': 'ssh_key',
    'name': 'ovs',
    'ssh_port': 22,
    'ip': '10.10.10.11',
    'key_filename': '/root/.ssh/id_rsa',
    'vports_mac': ['00:00:00:00:00:03', '00:00:00:00:00:04'],
    'vpath': '/usr/local/',
    'role': 'Ovsdpdk',
    'user': 'root',
    'images': '/var/lib/libvirt/images/ubuntu1.img',
    'flow': ['ovs-ofctl add-flow br0 in_port=1,action=output:3',
             'ovs-ofctl add-flow br0 in_port=3,action=output:1',
             'ovs-ofctl add-flow br0 in_port=4,action=output:2',
             'ovs-ofctl add-flow br0 in_port=2,action=output:4'],
    'phy_driver': 'i40e',
    'phy_ports': ['0000:06:00.0', '0000:06:00.1']}]

OVS_PASSWORD = [{
    'auth_type': 'password',
    'name': 'ovs',
    'vports_mac': ['00:00:00:00:00:03', '00:00:00:00:00:04'],
    'ip': '10.10.10.11',
    'role': 'Ovsdpdk',
    'user': 'root',
    'vpath': '/usr/local/',
    'images': '/var/lib/libvirt/images/ubuntu1.img',
    'flow': ['ovs-ofctl add-flow br0 in_port=1,action=output:3',
             'ovs-ofctl add-flow br0 in_port=3,action=output:1',
             'ovs-ofctl add-flow br0 in_port=4,action=output:2',
             'ovs-ofctl add-flow br0 in_port=2,action=output:4'],
    'phy_driver': 'i40e',
    'password': 'password',
    'phy_ports': ['0000:06:00.0', '0000:06:00.1']}]

#vfnic = "i40evf"
PCIS = ['0000:06:00.0', '0000:06:00.1']


class OvsdpdkTestCase(unittest.TestCase):

    NODES_SAMPLE_SSH = "ovs_sample_ssh_key.yaml"
    NODES_SAMPLE_PASSWORD = "ovs_sample_password.yaml"

    def setUp(self):
        self.test_context = ovsdpdk.Ovsdpdk()

    def test_construct(self):
        self.assertIsNone(self.test_context.name)
        self.assertIsNone(self.test_context.file_path)
        self.assertEqual(self.test_context.nodes, [])
        self.assertEqual(self.test_context.ovs, [])
        self.assertFalse(self.test_context.vm_deploy)
        self.assertTrue(self.test_context.first_run)
        self.assertEqual(self.test_context.user, "")
        self.assertEqual(self.test_context.ssh_ip, "")
        self.assertEqual(self.test_context.passwd, "")
        self.assertEqual(self.test_context.ssh_port, "")
        self.assertEqual(self.test_context.auth_type, "")

    def test_init(self):
        self.test_context.parse_pod_and_get_data = mock.Mock()
        self.test_context.file_path = CORRECT_FILE_PATH
        self.test_context.init()
        self.assertIsNone(self.test_context.init())

    def test_successful_init_with_ssh(self):
        CORRECT_FILE_PATH = self._get_file_abspath(self.NODES_SAMPLE_SSH)
        self.test_context.parse_pod_and_get_data(CORRECT_FILE_PATH)

    def test_successful_init_with_password(self):
        CORRECT_FILE_PATH = self._get_file_abspath(self.NODES_SAMPLE_PASSWORD)
        self.test_context.parse_pod_and_get_data(CORRECT_FILE_PATH)

    def test_unsuccessful_init(self):
        self.assertRaises(
            IOError,
            lambda: self.test_context.parse_pod_and_get_data(WRONG_FILE_PATH))

    def test_ssh_connection(self):
        with mock.patch("yardstick.ssh.SSH") as ssh:
            ssh_mock = mock.Mock(autospec=ssh.SSH)
            ssh_mock.execute = \
                mock.Mock(return_value=(0, {}, ""))
            ssh.return_value = ssh_mock

    @mock.patch('yardstick.network_services.utils.provision_tool', return_value="b")
    def test_ssh_connection(self, mock_prov):
        with mock.patch("yardstick.ssh.SSH") as ssh:
            ssh_mock = mock.Mock(autospec=ssh.SSH)
            ssh_mock.execute = \
                mock.Mock(return_value=(1, "b", ""))
            ssh.return_value = ssh_mock
            mock_prov.provision_tool = mock.Mock()
            ovs_obj = ovsdpdk.Ovsdpdk()
            ovs_obj.connection = ssh_mock
            ovs_obj.ovs = OVS_PASSWORD
            self.assertIsNone(ovs_obj.ssh_remote_machine())

    @mock.patch('yardstick.network_services.utils.provision_tool', return_value="b")
    def test_ssh_connection_ssh_key(self, mock_prov):
        with mock.patch("yardstick.ssh.SSH") as ssh:
            ssh_mock = mock.Mock(autospec=ssh.SSH)
            ssh_mock.execute = \
                mock.Mock(return_value=(1, "b", ""))
            ssh.return_value = ssh_mock
            mock_prov.provision_tool = mock.Mock()
            ovs_obj = ovsdpdk.Ovsdpdk()
            ovs_obj.connection = ssh_mock
            ovs_obj.ovs = OVS
            ovs_obj.key_filename = '/root/.ssh/id_rsa'
            self.assertIsNone(ovs_obj.ssh_remote_machine())

    def test_get_nic_details(self):
        with mock.patch("yardstick.ssh.SSH") as ssh:
            ssh_mock = mock.Mock(autospec=ssh.SSH)
            ssh_mock.execute = \
                mock.Mock(return_value=(0, "eth0 eth1", ""))
            ssh.return_value = ssh_mock
            ovs_obj = ovsdpdk.Ovsdpdk()
            ovs_obj.ovs = OVS
            ovs_obj.connection = ssh_mock
            self.assertIsNotNone(ovs_obj.get_nic_details())

    def test_install_req_libs(self):
        with mock.patch("yardstick.ssh.SSH") as ssh:
            ssh_mock = mock.Mock(autospec=ssh.SSH)
            ssh_mock.execute = \
                mock.Mock(return_value=(0, {}, ""))
            ssh.return_value = ssh_mock
            ovs_obj = ovsdpdk.Ovsdpdk()
            ovs_obj.first_run = True
            ovs_obj.connection = ssh_mock
            self.assertIsNone(ovs_obj.install_req_libs())

    def test_setup_ovs(self):
            with mock.patch("yardstick.ssh.SSH") as ssh:
                ssh_mock = mock.Mock(autospec=ssh.SSH)
                ssh_mock.execute = \
                    mock.Mock(return_value=(0, {}, ""))
                ssh.return_value = ssh_mock
                ovs_obj = ovsdpdk.Ovsdpdk()
                ovs_obj.connection = ssh_mock
                ovs_obj.ovs = OVS
                self.assertIsNone(ovs_obj.setup_ovs({"eth0 eth1"}))

    def test_start_ovs_serverswitch(self):
         with mock.patch("yardstick.ssh.SSH") as ssh:
             ssh_mock = mock.Mock(autospec=ssh.SSH)
             ssh_mock.execute = \
                  mock.Mock(return_value=(0, {}, ""))
             ssh.return_value = ssh_mock
             ovs_obj = ovsdpdk.Ovsdpdk()
             ovs_obj.connection = ssh_mock
             ovs_obj.ovs = OVS
             self.assertIsNone(ovs_obj.start_ovs_serverswitch())

    def test_setup_ovs_bridge(self):
          with mock.patch("yardstick.ssh.SSH") as ssh:
              ssh_mock = mock.Mock(autospec=ssh.SSH)
              ssh_mock.execute = \
                   mock.Mock(return_value=(0, {}, ""))
              ssh.return_value = ssh_mock
              ovs_obj = ovsdpdk.Ovsdpdk()
              ovs_obj.connection = ssh_mock
              ovs_obj.ovs = OVS
              self.assertIsNone(ovs_obj.setup_ovs_bridge())

    def test_add_oflows(self):
          with mock.patch("yardstick.ssh.SSH") as ssh:
              ssh_mock = mock.Mock(autospec=ssh.SSH)
              ssh_mock.execute = \
                   mock.Mock(return_value=(0, {}, ""))
              ssh.return_value = ssh_mock
              ovs_obj = ovsdpdk.Ovsdpdk()
              ovs_obj.connection = ssh_mock
              ovs_obj.ovs = OVS
              self.assertIsNone(ovs_obj.add_oflows())

    def test_setup_ovs_context_vm_already_present(self):
        with mock.patch("yardstick.ssh.SSH") as ssh:
            ssh_mock = mock.Mock(autospec=ssh.SSH)
            ssh_mock.execute = \
                mock.Mock(return_value=(0, {}, ""))
            ssh.return_value = ssh_mock
            ovs_obj = ovsdpdk.Ovsdpdk()
            ovs_obj.connection = ssh_mock
            ovs_obj.ovs = OVS
            mock_ovs = mock.Mock()
            ssh_mock.put = mock.Mock()
            ovs_obj.check_output = mock.Mock(return_value=(0, "vm1"))
            self.assertIsNone(ovs_obj.setup_ovs_context(
                PCIS,
                NIC_DETAILS,
                DRIVER))

    @mock.patch(
        'yardstick.benchmark.contexts.ovsdpdk',
        return_value="Domain vm1 created from /tmp/vm_ovs.xml")
    def test_is_vm_created(self, NIC_INPUT):
        with mock.patch("yardstick.ssh.SSH") as ssh:
            ssh_mock = mock.Mock(autospec=ssh.SSH)
            ssh_mock.execute = \
                mock.Mock(return_value=(0, {}, ""))
            ssh_mock.put = \
            mock.Mock(return_value=(0, {}, ""))
            ssh.return_value = ssh_mock
            mock_ovs = mock.Mock()
            ret_create = mock.Mock()
            pcis = NIC_DETAILS['pci']
            driver = NIC_DETAILS['phy_driver']
            self.assertIsNotNone(
            mock_ovs.ovs_obj.setup_ovs_context(
                pcis,
                NIC_DETAILS,
                driver))

    def test_check_output(self):
        with mock.patch("yardstick.ssh.SSH") as ssh:
            cmd = "command"
            ssh_mock = mock.Mock(autospec=ssh.SSH)
            ssh_mock.execute = \
                mock.Mock(return_value=(0, {}, ""))
            ssh.return_value = ssh_mock
            ovs_obj = ovsdpdk.Ovsdpdk()
            ovs_obj.connection = ssh_mock
            self.assertIsNotNone(ovs_obj.check_output(cmd, None))

    def test_split_cpu_list_available(self):
        with mock.patch("itertools.chain") as iter1:
            iter1 = mock.Mock()
            print("{0}".format(iter1))
            ovs_obj = ovsdpdk.Ovsdpdk()
            self.assertIsNotNone(ovs_obj.split_cpu_list('0,5'))

    def test_split_cpu_list_null(self):
        with mock.patch("itertools.chain") as iter1:
            iter1 = mock.Mock()
            print("{0}".format(iter1))
            ovs_obj = ovsdpdk.Ovsdpdk()
            self.assertEqual(ovs_obj.split_cpu_list([]), [])

    def test_destroy_vm_successful(self):
        with mock.patch("yardstick.ssh.SSH") as ssh:
            ssh_mock = mock.Mock(autospec=ssh.SSH)
            ssh_mock.execute = \
                mock.Mock(return_value=(0, {}, ""))
            ssh.return_value = ssh_mock
            ovs_obj = ovsdpdk.Ovsdpdk()
            ovs_obj.connection = ssh_mock
            ovs_obj.ovs = OVS
            ovs_obj.check_output = mock.Mock(return_value=(0, "vm1"))
            ssh_mock.execute = \
                mock.Mock(return_value=(0, {}, ""))
            ssh_mock.execute = \
                mock.Mock(return_value=(0, {}, ""))
            ssh_mock.execute = \
                mock.Mock(return_value=(0, "0 i40e"))
            ssh_mock.execute = \
                mock.Mock(return_value=(0, "0 i40e"))
            self.assertIsNone(ovs_obj.destroy_vm())

    def test_destroy_vm_unsuccessful(self):
        with mock.patch("yardstick.ssh.SSH") as ssh:
            ssh_mock = mock.Mock(autospec=ssh.SSH)
            ssh_mock.execute = \
                mock.Mock(return_value=(0, {}, ""))
            ssh.return_value = ssh_mock
            ovs_obj = ovsdpdk.Ovsdpdk()
            ovs_obj.connection = ssh_mock
            ovs_obj.ovs = OVS
            ovs_obj.check_output = mock.Mock(return_value=(1, {}))
            self.assertIsNone(ovs_obj.destroy_vm())

    def test_read_from_file(self):
        CORRECT_FILE_PATH = self._get_file_abspath(self.NODES_SAMPLE_PASSWORD)
        ovs_obj = ovsdpdk.Ovsdpdk()
        self.assertIsNotNone(ovs_obj.read_from_file(CORRECT_FILE_PATH))

    def test_write_to_file(self):
        ovs_obj = ovsdpdk.Ovsdpdk()
        self.assertIsNone(ovs_obj.write_to_file(SAMPLE_FILE, "some content"))

    def _get_file_abspath(self, filename):
        curr_path = os.path.dirname(os.path.abspath(__file__))
        file_path = os.path.join(curr_path, filename)
        return file_path

if __name__ == '__main__':
    unittest.main()