Merge "BugFix: Adopt to latest result structure while parsing the results"
[yardstick.git] / tests / unit / benchmark / contexts / test_heat.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2015 Ericsson AB and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 # Unittest for yardstick.benchmark.contexts.heat
13
14 from __future__ import absolute_import
15
16 import logging
17 import os
18 import unittest
19 import uuid
20
21 import mock
22
23 from yardstick.benchmark.contexts import heat
24
25
26 LOG = logging.getLogger(__name__)
27
28
29 class HeatContextTestCase(unittest.TestCase):
30
31     def setUp(self):
32         self.test_context = heat.HeatContext()
33         self.mock_context = mock.Mock(spec=heat.HeatContext())
34
35     def test_construct(self):
36
37         self.assertIsNone(self.test_context.name)
38         self.assertIsNone(self.test_context.stack)
39         self.assertEqual(self.test_context.networks, [])
40         self.assertEqual(self.test_context.servers, [])
41         self.assertEqual(self.test_context.placement_groups, [])
42         self.assertEqual(self.test_context.server_groups, [])
43         self.assertIsNone(self.test_context.keypair_name)
44         self.assertIsNone(self.test_context.secgroup_name)
45         self.assertEqual(self.test_context._server_map, {})
46         self.assertIsNone(self.test_context._image)
47         self.assertIsNone(self.test_context._flavor)
48         self.assertIsNone(self.test_context._user)
49         self.assertIsNone(self.test_context.template_file)
50         self.assertIsNone(self.test_context.heat_parameters)
51         self.assertIsNotNone(self.test_context.key_uuid)
52         self.assertIsNotNone(self.test_context.key_filename)
53
54     @mock.patch('yardstick.benchmark.contexts.heat.PlacementGroup')
55     @mock.patch('yardstick.benchmark.contexts.heat.ServerGroup')
56     @mock.patch('yardstick.benchmark.contexts.heat.Network')
57     @mock.patch('yardstick.benchmark.contexts.heat.Server')
58     def test_init(self, mock_server, mock_network, mock_sg, mock_pg):
59
60         pgs = {'pgrp1': {'policy': 'availability'}}
61         sgs = {'servergroup1': {'policy': 'affinity'}}
62         networks = {'bar': {'cidr': '10.0.1.0/24'}}
63         servers = {'baz': {'floating_ip': True, 'placement': 'pgrp1'}}
64         attrs = {'name': 'foo',
65                  'placement_groups': pgs,
66                  'server_groups': sgs,
67                  'networks': networks,
68                  'servers': servers}
69
70         self.test_context.init(attrs)
71
72         self.assertEqual(self.test_context.name, "foo")
73         self.assertEqual(self.test_context.keypair_name, "foo-key")
74         self.assertEqual(self.test_context.secgroup_name, "foo-secgroup")
75
76         mock_pg.assert_called_with('pgrp1', self.test_context,
77                                    pgs['pgrp1']['policy'])
78         mock_sg.assert_called_with('servergroup1', self.test_context,
79                                    sgs['servergroup1']['policy'])
80         self.assertTrue(len(self.test_context.placement_groups) == 1)
81         self.assertTrue(len(self.test_context.server_groups) == 1)
82
83         mock_network.assert_called_with(
84             'bar', self.test_context, networks['bar'])
85         self.assertTrue(len(self.test_context.networks) == 1)
86
87         mock_server.assert_called_with('baz', self.test_context,
88                                        servers['baz'])
89         self.assertTrue(len(self.test_context.servers) == 1)
90
91         if os.path.exists(self.test_context.key_filename):
92             try:
93                 os.remove(self.test_context.key_filename)
94                 os.remove(self.test_context.key_filename + ".pub")
95             except OSError:
96                 LOG.exception("key_filename: %s",
97                               self.test_context.key_filename)
98
99     @mock.patch('yardstick.benchmark.contexts.heat.HeatTemplate')
100     def test__add_resources_to_template_no_servers(self, mock_template):
101
102         self.test_context.keypair_name = "foo-key"
103         self.test_context.secgroup_name = "foo-secgroup"
104         self.test_context.key_uuid = "2f2e4997-0a8e-4eb7-9fa4-f3f8fbbc393b"
105
106         self.test_context._add_resources_to_template(mock_template)
107         mock_template.add_keypair.assert_called_with(
108             "foo-key",
109             "2f2e4997-0a8e-4eb7-9fa4-f3f8fbbc393b")
110         mock_template.add_security_group.assert_called_with("foo-secgroup")
111
112     @mock.patch('yardstick.benchmark.contexts.heat.HeatTemplate')
113     def test_deploy(self, mock_template):
114
115         self.test_context.name = 'foo'
116         self.test_context.template_file = '/bar/baz/some-heat-file'
117         self.test_context.heat_parameters = {'image': 'cirros'}
118         self.test_context.deploy()
119
120         mock_template.assert_called_with(self.test_context.name,
121                                          self.test_context.template_file,
122                                          self.test_context.heat_parameters)
123         self.assertIsNotNone(self.test_context.stack)
124
125     @mock.patch('yardstick.benchmark.contexts.heat.HeatTemplate')
126     def test_undeploy(self, mock_template):
127
128         self.test_context.stack = mock_template
129         self.test_context.undeploy()
130
131         self.assertTrue(mock_template.delete.called)
132
133     def test__get_server(self):
134
135         self.mock_context.name = 'bar'
136         self.mock_context.stack.outputs = {'public_ip': '127.0.0.1',
137                                            'private_ip': '10.0.0.1'}
138         self.mock_context.key_uuid = uuid.uuid4()
139
140         attr_name = {'name': 'foo.bar',
141                      'public_ip_attr': 'public_ip',
142                      'private_ip_attr': 'private_ip'}
143         result = heat.HeatContext._get_server(self.mock_context, attr_name)
144
145         self.assertEqual(result['ip'], '127.0.0.1')
146         self.assertEqual(result['private_ip'], '10.0.0.1')