aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/tests/unit/benchmark/contexts/test_node.py
blob: da16074d9cc3a2e77df03acac388641e2b56766d (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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
##############################################################################
# Copyright (c) 2015-2017 Huawei Technologies Co.,Ltd and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

import os
import errno

import mock
import unittest

from yardstick.benchmark.contexts import base
from yardstick.benchmark.contexts import node
from yardstick.common import constants as consts
from yardstick.common import exceptions
from yardstick.common import yaml_loader


class NodeContextTestCase(unittest.TestCase):

    PREFIX = 'yardstick.benchmark.contexts.node'

    NODES_SAMPLE = "nodes_sample.yaml"
    NODES_DUPLICATE_SAMPLE = "nodes_duplicate_sample.yaml"

    def setUp(self):
        self.test_context = node.NodeContext()
        self.addCleanup(self._remove_contexts)
        self.os_path_join = os.path.join
        self.attrs = {
            'name': 'foo',
            'task_id': '1234567890',
            'file': self._get_file_abspath(self.NODES_SAMPLE)
        }

    @staticmethod
    def _remove_contexts():
        for context in base.Context.list:
            context._delete_context()
        base.Context.list = []

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

    def test___init__(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.controllers, [])
        self.assertEqual(self.test_context.computes, [])
        self.assertEqual(self.test_context.baremetals, [])
        self.assertEqual(self.test_context.env, {})
        self.assertEqual(self.test_context.attrs, {})

    @mock.patch.object(yaml_loader, 'read_yaml_file')
    @mock.patch('{}.os.path.join'.format(PREFIX))
    def test_init_negative(self, mock_path_join, read_mock):
        special_path = '/foo/bar/error_file'
        error_path = self._get_file_abspath("error_file")

        def path_join(*args):
            if args == (consts.YARDSTICK_ROOT_PATH, error_path):
                return special_path
            return self.os_path_join(*args)

        # we can't count mock_path_join calls because
        # it can catch join calls for .pyc files.
        mock_path_join.side_effect = path_join
        read_calls = 0

        with self.assertRaises(KeyError):
            self.test_context.init({})

        self.assertEqual(read_mock.call_count, read_calls)

        attrs = {
            'name': 'foo',
            'task_id': '1234567890',
            'file': error_path,
        }
        read_mock.side_effect = IOError(errno.EBUSY, 'busy')
        with self.assertRaises(IOError) as raised:
            self.test_context.init(attrs)

        read_calls += 1
        self.assertEqual(read_mock.call_count, read_calls)
        self.assertIn(attrs['file'], self.test_context.file_path)
        self.assertEqual(raised.exception.errno, errno.EBUSY)
        self.assertEqual(str(raised.exception), str(read_mock.side_effect))

        read_mock.side_effect = IOError(errno.ENOENT, 'not found')
        with self.assertRaises(IOError) as raised:
            self.test_context.init(attrs)

        read_calls += 2
        self.assertEqual(read_mock.call_count, read_calls)
        self.assertEqual(self.test_context.file_path, special_path)
        self.assertEqual(raised.exception.errno, errno.ENOENT)
        self.assertEqual(str(raised.exception), str(read_mock.side_effect))

    def test__dispatch_script(self):
        self.test_context.init(self.attrs)

        self.test_context.env = {'bash': [{'script': 'dummy'}]}
        self.test_context._execute_script = mock.Mock()
        self.assertEqual(self.test_context._dispatch_script('bash'), None)

    def test__dispatch_ansible(self):
        self.test_context.init(self.attrs)

        self.test_context.env = {'ansible': [{'script': 'dummy'}]}
        self.test_context._do_ansible_job = mock.Mock()
        self.assertEqual(self.test_context._dispatch_ansible('ansible'), None)
        self.test_context.env = {}
        self.assertEqual(self.test_context._dispatch_ansible('ansible'), None)

    @mock.patch("{}.AnsibleCommon".format(PREFIX))
    def test__do_ansible_job(self, *args):
        self.assertIsNone(self.test_context._do_ansible_job('dummy'))

    def test_init(self):
        self.test_context.init(self.attrs)

        self.assertEqual(self.test_context.name, "foo-12345678")
        self.assertEqual(len(self.test_context.nodes), 4)
        self.assertEqual(len(self.test_context.controllers), 2)
        self.assertEqual(len(self.test_context.computes), 1)
        self.assertEqual(self.test_context.computes[0]["name"], "node3")
        self.assertEqual(len(self.test_context.baremetals), 1)
        self.assertEqual(self.test_context.baremetals[0]["name"], "node4")

    def test__get_server_with_dict_attr_name(self):
        self.test_context.init(self.attrs)
        result = self.test_context._get_server({'name': 'node1.foo-12345678'})

        self.assertIsNone(result, None)

    def test__get_server_not_found(self):
        self.test_context.init(self.attrs)

        self.assertIsNone(self.test_context._get_server('bar.foo-12345678'))

    def test__get_server_mismatch(self):
        self.test_context.init(self.attrs)

        self.assertIsNone(self.test_context._get_server('bar.foo1'))

    def test__get_server_duplicate(self):
        self.attrs['file'] = self._get_file_abspath(
            self.NODES_DUPLICATE_SAMPLE)
        self.test_context.init(self.attrs)

        with self.assertRaises(ValueError):
            self.test_context._get_server('node1.foo-12345678')

    def test__get_server_found(self):
        self.test_context.init(self.attrs)

        result = self.test_context._get_server('node1.foo-12345678')

        self.assertEqual(result['ip'], '10.229.47.137')
        self.assertEqual(result['name'], 'node1.foo-12345678')
        self.assertEqual(result['user'], 'root')
        self.assertEqual(result['key_filename'], '/root/.yardstick_key')

    def test__get_physical_nodes(self):
        self.test_context.init(self.attrs)
        nodes = self.test_context._get_physical_nodes()
        self.assertEqual(nodes, self.test_context.nodes)

    def test__get_physical_node_for_server(self):
        self.test_context.init(self.attrs)

        # When server is not from this context
        result = self.test_context._get_physical_node_for_server('node1.another-context')
        self.assertIsNone(result)

        # When node_name is not from this context
        result = self.test_context._get_physical_node_for_server('fake.foo-12345678')
        self.assertIsNone(result)

        result = self.test_context._get_physical_node_for_server('node1.foo-12345678')
        self.assertEqual(result, 'node1.foo')

    def test_update_collectd_options_for_node(self):
        self.test_context.init(self.attrs)
        options = {'collectd': {'interval': 5}}

        with self.assertRaises(exceptions.ContextUpdateCollectdForNodeError):
            self.test_context.update_collectd_options_for_node(options, 'fake.foo-12345678')

        self.test_context.update_collectd_options_for_node(options, 'node1.foo-12345678')

        node_collectd_options = [node for node in self.test_context.nodes
                                 if node['name'] == 'node1'][0]['collectd']

        self.assertEqual(node_collectd_options, options)

    @mock.patch('{}.NodeContext._dispatch_script'.format(PREFIX))
    def test_deploy(self, dispatch_script_mock):
        obj = node.NodeContext()
        self.addCleanup(obj._delete_context)
        obj.env = {
            'type': 'script'
        }
        obj.deploy()
        dispatch_script_mock.assert_called_once()

    @mock.patch('{}.NodeContext._dispatch_ansible'.format(PREFIX))
    def test_deploy_anisible(self, dispatch_ansible_mock):
        obj = node.NodeContext()
        self.addCleanup(obj._delete_context)
        obj.env = {
            'type': 'ansible'
        }
        obj.deploy()
        dispatch_ansible_mock.assert_called_once()

    @mock.patch('{}.NodeContext._dispatch_script'.format(PREFIX))
    def test_undeploy(self, dispatch_script_mock):
        obj = node.NodeContext()
        obj.env = {
            'type': 'script'
        }
        obj.undeploy()
        dispatch_script_mock.assert_called_once()

    @mock.patch('{}.NodeContext._dispatch_ansible'.format(PREFIX))
    def test_undeploy_anisble(self, dispatch_ansible_mock):
        obj = node.NodeContext()
        obj.env = {
            'type': 'ansible'
        }
        obj.undeploy()
        dispatch_ansible_mock.assert_called_once()

    @mock.patch('{}.ssh.SSH._put_file_shell'.format(PREFIX))
    @mock.patch('{}.ssh.SSH.execute'.format(PREFIX))
    def test_execute_remote_script(self, execute_mock, put_file_mock):
        obj = node.NodeContext()
        self.addCleanup(obj._delete_context)
        obj.env = {'prefix': 'yardstick.benchmark.scenarios.compute'}
        node_name_args = 'node5'
        obj.nodes = [{
            'name': node_name_args,
            'user': 'ubuntu',
            'ip': '10.10.10.10',
            'pwd': 'ubuntu',
        }]

        info = {'script': 'computecapacity.bash'}
        execute_mock.return_value = (0, '', '')
        obj._execute_remote_script('node5', info)

        put_file_mock.assert_called_once()
        execute_mock.assert_called()

    @mock.patch('{}.NodeContext._execute_local_script'.format(PREFIX))
    def test_execute_script_local(self, local_execute_mock):
        node_name = 'local'
        info = {}
        obj = node.NodeContext()
        self.addCleanup(obj._delete_context)
        obj._execute_script(node_name, info)
        local_execute_mock.assert_called_once()

    @mock.patch('{}.NodeContext._execute_remote_script'.format(PREFIX))
    def test_execute_script_remote(self, remote_execute_mock):
        node_name = 'node5'
        info = {}
        obj = node.NodeContext()
        self.addCleanup(obj._delete_context)
        obj._execute_script(node_name, info)
        remote_execute_mock.assert_called_once()

    def test_get_script(self):
        script_args = 'hello.bash'
        info_args = {
            'script': script_args
        }
        obj = node.NodeContext()
        self.addCleanup(obj._delete_context)
        script, options = obj._get_script(info_args)
        self.assertEqual(script_args, script)
        self.assertEqual('', options)

    def test_node_info(self):
        node_name_args = 'node5'
        obj = node.NodeContext()
        self.addCleanup(obj._delete_context)
        obj.nodes = [{'name': node_name_args, 'check': node_name_args}]
        node_info = obj._get_node_info(node_name_args)
        self.assertEqual(node_info.get('check'), node_name_args)

    @mock.patch('{}.ssh.SSH.wait'.format(PREFIX))
    def test_get_client(self, wait_mock):
        node_name_args = 'node5'
        obj = node.NodeContext()
        self.addCleanup(obj._delete_context)
        obj.nodes = [{
            'name': node_name_args,
            'user': 'ubuntu',
            'ip': '10.10.10.10',
            'pwd': 'ubuntu',
        }]
        obj._get_client(node_name_args)
        wait_mock.assert_called_once()

    def test_get_server(self):
        self.test_context.init(self.attrs)
        self.test_context._name = 'foo'
        self.test_context._task_id = '1234567890'
        self.test_context._name_task_id = '{}-{}'.format(
            self.test_context._name, self.test_context._task_id[:8])
        self.assertEqual('foo-12345678', self.test_context.name)
        self.assertIsNotNone(self.test_context._task_id)

        result = self.test_context.get_server('node1.foo-12345678')

        self.assertEqual(result['ip'], '10.229.47.137')
        self.assertEqual(result['name'], 'node1.foo-12345678')
        self.assertEqual(result['user'], 'root')
        self.assertEqual(result['key_filename'], '/root/.yardstick_key')

    def test_get_server_server_not_in_context(self):
        self.test_context.init(self.attrs)

        with self.assertRaises(ValueError):
            self.test_context.get_server('my2.foo-12345678')

    def test_get_context_from_server(self):
        self.test_context._name = 'vnf1'
        self.test_context._task_id = '1234567890'
        self.test_context._name_task_id = '{}-{}'.format(
            self.test_context._name, self.test_context._task_id[:8])
        self.test_context.nodes = [{'name': 'my', 'value': 100}]
        self.test_context.attrs = {'attr1': 200}

        self.assertIs(
            self.test_context.get_context_from_server('my.vnf1-12345678'),
            self.test_context)

    # TODO: Split this into more granular tests
    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 = {}
        self.assertIsNone(self.test_context._get_network(attr_name))

        attr_name = {'vld_id': 'vld777'}
        self.assertIsNone(self.test_context._get_network(attr_name))

        self.assertIsNone(self.test_context._get_network(None))

        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)