summaryrefslogtreecommitdiffstats
path: root/tests/unit/benchmark/contexts/test_model.py
blob: 3e94168b5620e848e5fda755d56847316511a18a (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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#!/usr/bin/env python

##############################################################################
# Copyright (c) 2015 Ericsson AB 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
##############################################################################

# Unittest for yardstick.benchmark.contexts.model

from __future__ import absolute_import
import unittest
import mock

from yardstick.benchmark.contexts import model


class ObjectTestCase(unittest.TestCase):

    def setUp(self):
        self.mock_context = mock.Mock()

    def test_construct(self):

        test_object = model.Object('foo', self.mock_context)

        self.assertEqual(test_object.name, 'foo')
        self.assertEqual(test_object._context, self.mock_context)
        self.assertIsNone(test_object.stack_name)
        self.assertIsNone(test_object.stack_id)

    def test_dn(self):

        self.mock_context.name = 'bar'
        test_object = model.Object('foo', self.mock_context)

        self.assertEqual('foo.bar', test_object.dn)


class PlacementGroupTestCase(unittest.TestCase):

    def setUp(self):
        self.mock_context = mock.Mock()
        self.mock_context.name = 'bar'

    def tearDown(self):
        model.PlacementGroup.map = {}

    def test_sucessful_construct(self):

        test_pg = model.PlacementGroup('foo', self.mock_context, 'affinity')

        self.assertEqual(test_pg.name, 'foo')
        self.assertEqual(test_pg.members, set())
        self.assertEqual(test_pg.stack_name, 'bar-foo')
        self.assertEqual(test_pg.policy, 'affinity')

        test_map = {'foo': test_pg}
        self.assertEqual(model.PlacementGroup.map, test_map)

    def test_wrong_policy_in_construct(self):

        self.assertRaises(ValueError, model.PlacementGroup, 'foo',
                          self.mock_context, 'baz')

    def test_add_member(self):

        test_pg = model.PlacementGroup('foo', self.mock_context, 'affinity')
        test_pg.add_member('foo')

        self.assertEqual(test_pg.members, set(['foo']))

    def test_get_name_successful(self):

        model.PlacementGroup.map = {'foo': True}
        self.assertTrue(model.PlacementGroup.get('foo'))

    def test_get_name_unsuccessful(self):

        self.assertIsNone(model.PlacementGroup.get('foo'))


class RouterTestCase(unittest.TestCase):

    def test_construct(self):

        mock_context = mock.Mock()
        mock_context.name = 'baz'
        test_router = model.Router('foo', 'bar', mock_context, 'qux')

        self.assertEqual(test_router.stack_name, 'baz-bar-foo')
        self.assertEqual(test_router.stack_if_name, 'baz-bar-foo-if0')
        self.assertEqual(test_router.external_gateway_info, 'qux')


class NetworkTestCase(unittest.TestCase):

    def setUp(self):
        self.mock_context = mock.Mock()
        self.mock_context.name = 'bar'

    def tearDown(self):
        model.Network.list = []

    def test_construct_no_external_network(self):

        attrs = {'cidr': '10.0.0.0/24'}
        test_network = model.Network('foo', self.mock_context, attrs)

        self.assertEqual(test_network.stack_name, 'bar-foo')
        self.assertEqual(test_network.subnet_stack_name, 'bar-foo-subnet')
        self.assertEqual(test_network.subnet_cidr, attrs['cidr'])
        self.assertIsNone(test_network.router)
        self.assertIn(test_network, model.Network.list)

    def test_construct_has_external_network(self):

        attrs = {'external_network': 'ext_net'}
        test_network = model.Network('foo', self.mock_context, attrs)
        exp_router = model.Router('router', 'foo', self.mock_context,
                                  'ext_net')

        self.assertEqual(test_network.router.stack_name, exp_router.stack_name)
        self.assertEqual(test_network.router.stack_if_name,
                         exp_router.stack_if_name)
        self.assertEqual(test_network.router.external_gateway_info,
                         exp_router.external_gateway_info)

    def test_has_route_to(self):

        attrs = {'external_network': 'ext_net'}
        test_network = model.Network('foo', self.mock_context, attrs)

        self.assertTrue(test_network.has_route_to('ext_net'))

    def test_has_no_route_to(self):

        attrs = {}
        test_network = model.Network('foo', self.mock_context, attrs)

        self.assertFalse(test_network.has_route_to('ext_net'))

    @mock.patch('yardstick.benchmark.contexts.model.Network.has_route_to')
    def test_find_by_route_to(self, mock_has_route_to):

        mock_network = mock.Mock()
        model.Network.list = [mock_network]
        mock_has_route_to.return_value = True

        self.assertIs(mock_network, model.Network.find_by_route_to('foo'))

    def test_find_external_network(self):

        mock_network = mock.Mock()
        mock_network.router = mock.Mock()
        mock_network.router.external_gateway_info = 'ext_net'
        model.Network.list = [mock_network]

        self.assertEqual(model.Network.find_external_network(), 'ext_net')

    def test_construct_gateway_ip_is_null(self):

        attrs = {'gateway_ip': 'null'}
        test_network = model.Network('foo', self.mock_context, attrs)
        self.assertEqual(test_network.gateway_ip, 'null')

    def test_construct_gateway_ip_is_none(self):

        attrs = {'gateway_ip': None}
        test_network = model.Network('foo', self.mock_context, attrs)
        self.assertEqual(test_network.gateway_ip, 'null')

    def test_construct_gateway_ip_is_absent(self):

        attrs = {}
        test_network = model.Network('foo', self.mock_context, attrs)
        self.assertIsNone(test_network.gateway_ip)

class ServerTestCase(unittest.TestCase):

    def setUp(self):
        self.mock_context = mock.Mock()
        self.mock_context.name = 'bar'
        self.mock_context.keypair_name = 'some-keys'
        self.mock_context.secgroup_name = 'some-secgroup'
        self.mock_context.user = "some-user"
        netattrs = {'cidr': '10.0.0.0/24', 'provider': None, 'external_network': 'ext_net'}
        self.mock_context.networks = [model.Network("some-network", self.mock_context, netattrs)]


    def test_construct_defaults(self):

        attrs = None
        test_server = model.Server('foo', self.mock_context, attrs)

        self.assertEqual(test_server.stack_name, 'foo.bar')
        self.assertEqual(test_server.keypair_name, 'some-keys')
        self.assertEqual(test_server.secgroup_name, 'some-secgroup')
        self.assertEqual(test_server.placement_groups, [])
        self.assertIsNone(test_server.server_group)
        self.assertEqual(test_server.instances, 1)
        self.assertIsNone(test_server.floating_ip)
        self.assertIsNone(test_server._image)
        self.assertIsNone(test_server._flavor)
        self.assertIn(test_server, model.Server.list)

    @mock.patch('yardstick.benchmark.contexts.model.PlacementGroup')
    def test_construct_get_wrong_placement_group(self, mock_pg):

        attrs = {'placement': 'baz'}
        mock_pg.get.return_value = None

        self.assertRaises(ValueError, model.Server, 'foo',
                          self.mock_context, attrs)

    @mock.patch('yardstick.benchmark.contexts.model.PlacementGroup')
    def test_construct_get_wrong_server_group(self, mock_sg):

        attrs = {'server_group': 'baz'}
        mock_sg.get.return_value = None

        self.assertRaises(ValueError, model.Server, 'foo',
                          self.mock_context, attrs)

    @mock.patch('yardstick.benchmark.contexts.heat.HeatTemplate')
    def test__add_instance(self, mock_template):

        attrs = {'image': 'some-image', 'flavor': 'some-flavor', 'floating_ip': '192.168.1.10', 'floating_ip_assoc': 'some-vm'}
        test_server = model.Server('foo', self.mock_context, attrs)

        self.mock_context.flavors = ['flavor1', 'flavor2', 'some-flavor']

        mock_network = mock.Mock()
        mock_network.name = 'some-network'
        mock_network.stack_name = 'some-network-stack'
        mock_network.allowed_address_pairs = ["1", "2"]
        mock_network.vnic_type = 'normal'
        mock_network.subnet_stack_name = 'some-network-stack-subnet'
        mock_network.provider = 'sriov'
        mock_network.external_network = 'ext_net'
        mock_network.router = model.Router('some-router', 'some-network', self.mock_context, 'ext_net')

        test_server._add_instance(mock_template, 'some-server',
                                  [mock_network], 'hints')

        mock_template.add_port.assert_called_with(
            'some-server-some-network-port',
            mock_network.stack_name,
            mock_network.subnet_stack_name,
            mock_network.vnic_type,
            sec_group_id=self.mock_context.secgroup_name,
            provider=mock_network.provider,
            allowed_address_pairs=mock_network.allowed_address_pairs)

        mock_template.add_floating_ip.assert_called_with(
            'some-server-fip',
            mock_network.external_network,
            'some-server-some-network-port',
            'bar-some-network-some-router-if0',
            'some-secgroup'
        )

        mock_template.add_floating_ip_association.assert_called_with(
            'some-server-fip-assoc',
            'some-server-fip',
            'some-server-some-network-port'
        )

        mock_template.add_server.assert_called_with(
            'some-server', 'some-image',
            flavor='some-flavor',
            flavors=['flavor1', 'flavor2', 'some-flavor'],
            ports=['some-server-some-network-port'],
            user=self.mock_context.user,
            key_name=self.mock_context.keypair_name,
            user_data='',
            scheduler_hints='hints')

    def test_override_ip(self):
        network_ports = {
            'mgmt': ['mgmt'],
            'uplink_0': [
                {'xe0': {'local_ip': '10.44.0.20', 'netmask': '255.255.255.0'}},
            ],
            'downlink_0': [
                {'xe1': {'local_ip': '10.44.0.30', 'netmask': '255.255.255.0'}},
            ],
        }
        attrs = {
            'image': 'some-image', 'flavor': 'some-flavor',
        }
        test_server = model.Server('foo', self.mock_context, attrs)
        test_server.interfaces = {
            "xe0": {
                "local_ip": "1.2.3.4",
                "netmask": "255.255.255.0",
            },
            "xe1": {
                "local_ip": "1.2.3.5",
                "netmask": "255.255.255.0"
            }
        }
        test_server.network_ports = network_ports

        test_server.override_ip("uplink_0", {"port": "xe0"})
        self.assertEqual(test_server.interfaces["xe0"], network_ports["uplink_0"][0]["xe0"])

    def test_override_ip_multiple(self):
        network_ports = {
            'mgmt': ['mgmt'],
            'uplink_0': [
                {'xe0': {'local_ip': '10.44.0.20', 'netmask': '255.255.255.0'}},
                {'xe0': {'local_ip': '10.44.0.21', 'netmask': '255.255.255.0'}},
            ],
            'downlink_0': [
                {'xe1': {'local_ip': '10.44.0.30', 'netmask': '255.255.255.0'}},
            ],
        }
        attrs = {
            'image': 'some-image', 'flavor': 'some-flavor',
        }
        test_server = model.Server('foo', self.mock_context, attrs)
        test_server.interfaces = {
            "xe0": {
                "local_ip": "1.2.3.4",
                "netmask": "255.255.255.0",
            },
            "xe1": {
                "local_ip": "1.2.3.5",
                "netmask": "255.255.255.0"
            }
        }
        test_server.network_ports = network_ports
        test_server.override_ip("uplink_0", {"port": "xe0"})
        self.assertEqual(test_server.interfaces["xe0"], network_ports["uplink_0"][0]["xe0"])

    def test_override_ip_mixed(self):
        network_ports = {
            'mgmt': ['mgmt'],
            'uplink_0': [
                'xe0',
                {'xe0': {'local_ip': '10.44.0.21', 'netmask': '255.255.255.0'}},
            ],
            'downlink_0': [
                {'xe1': {'local_ip': '10.44.0.30', 'netmask': '255.255.255.0'}},
            ],
        }
        attrs = {
            'image': 'some-image', 'flavor': 'some-flavor',
        }
        test_server = model.Server('foo', self.mock_context, attrs)
        test_server.interfaces = {
            "xe0": {
                "local_ip": "1.2.3.4",
                "netmask": "255.255.255.0",
            },
            "xe1": {
                "local_ip": "1.2.3.5",
                "netmask": "255.255.255.0"
            }
        }
        test_server.network_ports = network_ports
        test_server.override_ip("uplink_0", {"port": "xe0"})
        self.assertEqual(test_server.interfaces["xe0"], network_ports["uplink_0"][1]["xe0"])

    @mock.patch('yardstick.benchmark.contexts.heat.HeatTemplate')
    def test__add_instance_with_ip_override_invalid_syntax(self, mock_template):
        network_ports = {
            'mgmt': ['mgmt'],
            'uplink_0': 'xe0',
            'downlink_0': [
                {'xe1': {'local_ip': '10.44.0.30', 'netmask': '255.255.255.0'}},
            ],
        }
        attrs = {
            'image': 'some-image', 'flavor': 'some-flavor',
        }
        test_server = model.Server('foo', self.mock_context, attrs)
        test_server.network_ports = network_ports
        context = type("Context", (object,), {})
        # can't use Mock because Mock.name is reserved
        context.name = "context"
        networks = [model.Network(n, context, {}) for n in network_ports]

        with self.assertRaises(SyntaxError):
            test_server._add_instance(mock_template, 'some-server',
                                      networks, 'hints')

    @mock.patch('yardstick.benchmark.contexts.heat.HeatTemplate')
    def test__add_instance_with_ip_override(self, mock_template):
        network_ports = {
            'mgmt': ['mgmt'],
            'uplink_0': [
                {'xe0': {'local_ip': '10.44.0.20', 'netmask': '255.255.255.0'}},
            ],
            'downlink_0': [
                {'xe1': {'local_ip': '10.44.0.30', 'netmask': '255.255.255.0'}},
            ],
        }
        attrs = {
            'image': 'some-image', 'flavor': 'some-flavor',
        }
        test_server = model.Server('foo', self.mock_context, attrs)
        test_server.network_ports = network_ports
        context = type("Context", (object,), {})
        # can't use Mock because Mock.name is reserved
        context.name = "context"
        networks = [model.Network(n, context, {}) for n in network_ports]

        test_server._add_instance(mock_template, 'some-server',
                                  networks, 'hints')
        self.assertEqual(test_server.ports, {
            'downlink_0': [{'port': 'xe1', 'stack_name': 'some-server-xe1-port'}],
            'mgmt': [{'port': 'mgmt', 'stack_name': 'some-server-mgmt-port'}],
            'uplink_0': [{'port': 'xe0', 'stack_name': 'some-server-xe0-port'}]
        })

    @mock.patch('yardstick.benchmark.contexts.heat.HeatTemplate')
    def test__add_instance_with_multiple_ip_override(self, mock_template):
        network_ports = {
            'mgmt': ['mgmt'],
            'uplink_0': [
                {'xe0': {'local_ip': '10.44.0.20', 'netmask': '255.255.255.0'}},
                {'xe0': {'local_ip': '10.44.0.21', 'netmask': '255.255.255.0'}},
            ],
            'downlink_0': [
                {'xe1': {'local_ip': '10.44.0.30', 'netmask': '255.255.255.0'}},
            ],
        }
        attrs = {
            'image': 'some-image', 'flavor': 'some-flavor',
        }
        test_server = model.Server('foo', self.mock_context, attrs)
        test_server.network_ports = network_ports
        context = type("Context", (object,), {})
        # can't use Mock because Mock.name is reserved
        context.name = "context"
        networks = [model.Network(n, context, {}) for n in network_ports]

        test_server._add_instance(mock_template, 'some-server',
                                  networks, 'hints')
        self.assertEqual(test_server.ports, {
            'downlink_0': [{'port': 'xe1', 'stack_name': 'some-server-xe1-port'}],
            'mgmt': [{'port': 'mgmt', 'stack_name': 'some-server-mgmt-port'}],
            'uplink_0': [{'port': 'xe0', 'stack_name': 'some-server-xe0-port'},
                         # this is not an error, we can produce this, it is left to Heat
                         # to detect duplicate ports and error
                         {'port': 'xe0', 'stack_name': 'some-server-xe0-port'}]
        })

    @mock.patch('yardstick.benchmark.contexts.heat.HeatTemplate')
    def test__add_instance_with_user_data(self, mock_template):
        user_data = "USER_DATA"
        attrs = {
            'image': 'some-image', 'flavor': 'some-flavor',
            'user_data': user_data,
        }
        test_server = model.Server('foo', self.mock_context, attrs)

        test_server._add_instance(mock_template, 'some-server',
                                  [], 'hints')

        mock_template.add_server.assert_called_with(
            'some-server', 'some-image',
            flavor='some-flavor',
            flavors=self.mock_context.flavors,
            ports=[],
            user=self.mock_context.user,
            key_name=self.mock_context.keypair_name,
            user_data=user_data,
            scheduler_hints='hints')

    @mock.patch('yardstick.benchmark.contexts.heat.HeatTemplate')
    def test__add_instance_plus_flavor(self, mock_template):

        user_data = ''
        attrs = {
            'image': 'some-image', 'flavor': 'flavor1',
            'flavors': ['flavor2'], 'user_data': user_data
        }
        test_server = model.Server('ServerFlavor-2', self.mock_context, attrs)

        self.mock_context.flavors = ['flavor2']
        mock_network = mock.Mock()
        mock_network.allowed_address_pairs = ["1", "2"]
        mock_network.vnic_type = 'normal'
        mock_network.configure_mock(name='some-network', stack_name='some-network-stack',
                                    subnet_stack_name='some-network-stack-subnet',
                                    provider='some-provider')

        test_server._add_instance(mock_template, 'ServerFlavor-2',
                                  [mock_network], 'hints')

        mock_template.add_port.assert_called_with(
            'ServerFlavor-2-some-network-port',
            mock_network.stack_name,
            mock_network.subnet_stack_name,
            mock_network.vnic_type,
            provider=mock_network.provider,
            sec_group_id=self.mock_context.secgroup_name,
            allowed_address_pairs=mock_network.allowed_address_pairs)

        mock_template.add_server.assert_called_with(
            'ServerFlavor-2', 'some-image',
            flavor='flavor1',
            flavors=['flavor2'],
            ports=['ServerFlavor-2-some-network-port'],
            user=self.mock_context.user,
            key_name=self.mock_context.keypair_name,
            user_data=user_data,
            scheduler_hints='hints')

    @mock.patch('yardstick.benchmark.contexts.heat.HeatTemplate')
    def test__add_instance_misc(self, mock_template):

        user_data = ''
        attrs = {
            'image': 'some-image', 'flavor': 'flavor1',
            'flavors': ['flavor2'], 'user_data': user_data
        }
        test_server = model.Server('ServerFlavor-3', self.mock_context, attrs)

        self.mock_context.flavors =  ['flavor2']
        self.mock_context.flavor = {'vcpus': 4}
        mock_network = mock.Mock()
        mock_network.name = 'some-network'
        mock_network.stack_name = 'some-network-stack'
        mock_network.subnet_stack_name = 'some-network-stack-subnet'

        test_server._add_instance(mock_template, 'ServerFlavor-3',
                                  [mock_network], 'hints')


        mock_template.add_port(
            'ServerFlavor-3-some-network-port',
            mock_network.stack_name,
            mock_network.subnet_stack_name,
            sec_group_id=self.mock_context.secgroup_name)

        mock_template.add_flavor(
            vcpus=4,
            ram=2048,
            disk=1)

        mock_template.add_flavor(
            vcpus=4,
            ram=2048,
            disk=1,
            extra_specs={'cat': 1, 'dog': 2, 'dragon': 1000})

        mock_template.add_server.assert_called_with(
            'ServerFlavor-3', 'some-image',
            flavor='flavor1',
            flavors=['flavor2'],
            ports=['ServerFlavor-3-some-network-port'],
            user=self.mock_context.user,
            key_name=self.mock_context.keypair_name,
            user_data=user_data,
            scheduler_hints='hints')