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
|
#!/usr/bin/env python
# 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.benchmark.contexts.standalone
from __future__ import absolute_import
import os
import unittest
import mock
from yardstick.benchmark.contexts import standalone
from yardstick.benchmark.contexts import sriov
MOCKS = {
'yardstick.benchmark.contexts': mock.MagicMock(),
'yardstick.benchmark.contexts.sriov': mock.MagicMock(),
'yardstick.benchmark.contexts.standalone': mock.MagicMock(),
}
class StandaloneContextTestCase(unittest.TestCase):
NODES_SAMPLE = "nodes_sample_new.yaml"
NODES_DUPLICATE_SAMPLE = "nodes_duplicate_sample_new.yaml"
def setUp(self):
self.test_context = standalone.StandaloneContext()
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.nfvi_node, [])
def test_unsuccessful_init(self):
attrs = {
'name': 'foo',
'file': self._get_file_abspath("error_file")
}
self.assertRaises(IOError, self.test_context.init, attrs)
def test_successful_init(self):
attrs = {
'name': 'sriov',
'file': self._get_file_abspath(self.NODES_SAMPLE)
}
self.test_context.nfvi_node = [{
'name': 'sriov',
'vf_macs': ['00:00:00:71:7d:25', '00:00:00:71:7d:26'],
'ip': '10.123.123.122',
'role': 'Sriov',
'user': 'root',
'images': '/var/lib/libvirt/images/ubuntu1.img',
'phy_driver': 'i40e',
'password': 'password',
'phy_ports': ['0000:06:00.0', '0000:06:00.1']}]
self.test_context.get_nfvi_obj = mock.Mock()
self.test_context.init(attrs)
self.assertEqual(self.test_context.name, "sriov")
self.assertEqual(len(self.test_context.nodes), 3)
self.assertEqual(len(self.test_context.nfvi_node), 2)
self.assertEqual(self.test_context.nfvi_node[0]["name"], "sriov")
def test__get_server_with_dic_attr_name(self):
attrs = {
'name': 'foo',
'file': self._get_file_abspath(self.NODES_SAMPLE)
}
self.test_context.nfvi_node = [{
'name': 'sriov',
'vf_macs': ['00:00:00:71:7d:25', '00:00:00:71:7d:26'],
'ip': '10.123.123.122',
'role': 'Sriov',
'user': 'root',
'images': '/var/lib/libvirt/images/ubuntu1.img',
'phy_driver': 'i40e',
'password': 'password',
'phy_ports': ['0000:06:00.0', '0000:06:00.1']}]
self.test_context.init(attrs)
attr_name = {'name': 'foo.bar'}
result = self.test_context._get_server(attr_name)
self.assertEqual(result, None)
def test__get_server_not_found(self):
attrs = {
'name': 'foo',
'file': self._get_file_abspath(self.NODES_SAMPLE)
}
self.test_context.nfvi_node = [{
'name': 'sriov',
'vf_macs': ['00:00:00:71:7d:25', '00:00:00:71:7d:26'],
'ip': '10.123.123.122',
'role': 'Sriov',
'user': 'root',
'images': '/var/lib/libvirt/images/ubuntu1.img',
'phy_driver': 'i40e',
'password': 'password',
'phy_ports': ['0000:06:00.0', '0000:06:00.1']}]
self.test_context.init(attrs)
attr_name = 'bar.foo'
result = self.test_context._get_server(attr_name)
self.assertEqual(result, None)
def test__get_server_duplicate(self):
attrs = {
'name': 'foo',
'file': self._get_file_abspath(self.NODES_DUPLICATE_SAMPLE)
}
self.test_context.nfvi_node = [{
'name': 'sriov',
'vf_macs': ['00:00:00:71:7d:25', '00:00:00:71:7d:26'],
'ip': '10.123.123.122',
'role': 'Sriov',
'user': 'root',
'images': '/var/lib/libvirt/images/ubuntu1.img',
'phy_driver': 'i40e',
'password': 'password',
'phy_ports': ['0000:06:00.0', '0000:06:00.1']}]
self.test_context.init(attrs)
attr_name = 'sriov.foo'
self.assertRaises(
ValueError,
self.test_context._get_server,
attr_name)
def test__get_server_found(self):
attrs = {
'name': 'sriov',
'file': self._get_file_abspath(self.NODES_SAMPLE)
}
self.test_context.nfvi_node = [{
'name': 'sriov',
'vf_macs': ['00:00:00:71:7d:25', '00:00:00:71:7d:26'],
'ip': '10.123.123.122',
'role': 'Sriov',
'user': 'root',
'images': '/var/lib/libvirt/images/ubuntu1.img',
'phy_driver': 'i40e',
'password': 'password',
'phy_ports': ['0000:06:00.0', '0000:06:00.1']}]
self.test_context.init(attrs)
attr_name = 'sriov.sriov'
result = self.test_context._get_server(attr_name)
self.assertEqual(result['ip'], '10.123.123.122')
self.assertEqual(result['name'], 'sriov.sriov')
self.assertEqual(result['user'], 'root')
def test_deploy(self):
attrs = {
'name': 'foo',
'file': self._get_file_abspath(self.NODES_SAMPLE)
}
self.test_context.nfvi_node = [{
'name': 'sriov',
'vf_macs': ['00:00:00:71:7d:25', '00:00:00:71:7d:26'],
'ip': '10.123.123.122',
'role': 'Sriov',
'user': 'root',
'images': '/var/lib/libvirt/images/ubuntu1.img',
'phy_driver': 'i40e',
'password': 'password',
'phy_ports': ['0000:06:00.0', '0000:06:00.1']}]
self.test_context.get_nfvi_obj = mock.MagicMock()
self.test_context.init(attrs)
self.test_context.nfvi_obj.ssh_remote_machine = mock.Mock()
self.test_context.nfvi_obj.first_run = True
self.test_context.nfvi_obj.get_nic_details = mock.Mock()
PORTS = ['0000:06:00.0', '0000:06:00.1']
NIC_DETAILS = {
'interface': {0: 'enp6s0f0', 1: 'enp6s0f1'},
'vf_macs': ['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'
result = self.test_context.nfvi_obj.setup_sriov_context(
PORTS,
NIC_DETAILS,
DRIVER)
print("{0}".format(result))
self.assertIsNone(self.test_context.deploy())
def test_undeploy(self):
attrs = {
'name': 'foo',
'file': self._get_file_abspath(self.NODES_SAMPLE)
}
self.test_context.nfvi_node = [{
'name': 'sriov',
'vf_macs': ['00:00:00:71:7d:25', '00:00:00:71:7d:26'],
'ip': '10.123.123.122',
'role': 'Sriov',
'user': 'root',
'images': '/var/lib/libvirt/images/ubuntu1.img',
'phy_driver': 'i40e',
'password': 'password',
'phy_ports': ['0000:06:00.0', '0000:06:00.1']}]
self.test_context.get_nfvi_obj = mock.MagicMock()
self.test_context.init(attrs)
self.test_context.nfvi_obj.destroy_vm = mock.Mock()
self.assertIsNone(self.test_context.undeploy())
def test_get_nfvi_obj(self):
with mock.patch('yardstick.benchmark.contexts.sriov'):
attrs = {
'name': 'sriov',
'file': self._get_file_abspath(self.NODES_SAMPLE)
}
self.test_context.init(attrs)
self.test_context.nfvi_obj.file_path = self._get_file_abspath(
self.NODES_SAMPLE)
self.test_context.nfvi_node = [{
'name': 'sriov',
'vf_macs': ['00:00:00:71:7d:25', '00:00:00:71:7d:26'],
'ip': '10.123.123.122',
'role': 'Sriov',
'user': 'root',
'images': '/var/lib/libvirt/images/ubuntu1.img',
'phy_driver': 'i40e',
'password': 'password',
'phy_ports': ['0000:06:00.0', '0000:06:00.1']}]
self.test_context.get_nfvi_obj = mock.MagicMock()
self.test_context.init(attrs)
self.test_context.get_context_impl = mock.Mock(
return_value=sriov.Sriov)
self.assertIsNotNone(self.test_context.get_nfvi_obj())
def test_get_context_impl_correct_obj(self):
with mock.patch.dict("sys.modules", MOCKS):
self.assertIsNotNone(self.test_context.get_context_impl('Sriov'))
def test_get_context_impl_wrong_obj(self):
with mock.patch.dict("sys.modules", MOCKS):
self.assertRaises(
ValueError,
lambda: self.test_context.get_context_impl('wrong_object'))
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
def test__get_network(self):
network1 = {
'name': 'net_1',
'vld_id': 'vld111',
'segmentation_id': 'seg54',
'network_type': 'type_a',
'physical_network': 'phys',
}
network2 = {
'name': 'net_2',
'vld_id': 'vld999',
}
self.test_context.networks = {
'a': network1,
'b': network2,
}
attr_name = None
self.assertIsNone(self.test_context._get_network(attr_name))
attr_name = {}
self.assertIsNone(self.test_context._get_network(attr_name))
attr_name = {'vld_id': 'vld777'}
self.assertIsNone(self.test_context._get_network(attr_name))
attr_name = 'vld777'
self.assertIsNone(self.test_context._get_network(attr_name))
attr_name = {'vld_id': 'vld999'}
expected = {
"name": 'net_2',
"vld_id": 'vld999',
"segmentation_id": None,
"network_type": None,
"physical_network": None,
}
result = self.test_context._get_network(attr_name)
self.assertDictEqual(result, expected)
attr_name = 'a'
expected = network1
result = self.test_context._get_network(attr_name)
self.assertDictEqual(result, expected)
if __name__ == '__main__':
unittest.main()
|