3 ##############################################################################
4 # Copyright (c) 2015 Ericsson AB and others.
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 ##############################################################################
12 # Unittest for yardstick.benchmark.contexts.heat
14 from __future__ import absolute_import
23 from yardstick.benchmark.contexts import heat
26 LOG = logging.getLogger(__name__)
29 class HeatContextTestCase(unittest.TestCase):
32 self.test_context = heat.HeatContext()
33 self.mock_context = mock.Mock(spec=heat.HeatContext())
35 def test_construct(self):
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)
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):
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,
70 self.test_context.init(attrs)
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")
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)
83 mock_network.assert_called_with(
84 'bar', self.test_context, networks['bar'])
85 self.assertTrue(len(self.test_context.networks) == 1)
87 mock_server.assert_called_with('baz', self.test_context,
89 self.assertTrue(len(self.test_context.servers) == 1)
91 if os.path.exists(self.test_context.key_filename):
93 os.remove(self.test_context.key_filename)
94 os.remove(self.test_context.key_filename + ".pub")
96 LOG.exception("key_filename: %s",
97 self.test_context.key_filename)
99 @mock.patch('yardstick.benchmark.contexts.heat.HeatTemplate')
100 def test__add_resources_to_template_no_servers(self, mock_template):
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"
106 self.test_context._add_resources_to_template(mock_template)
107 mock_template.add_keypair.assert_called_with(
109 "2f2e4997-0a8e-4eb7-9fa4-f3f8fbbc393b")
110 mock_template.add_security_group.assert_called_with("foo-secgroup")
112 @mock.patch('yardstick.benchmark.contexts.heat.HeatTemplate')
113 def test_deploy(self, mock_template):
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()
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)
125 @mock.patch('yardstick.benchmark.contexts.heat.HeatTemplate')
126 def test_undeploy(self, mock_template):
128 self.test_context.stack = mock_template
129 self.test_context.undeploy()
131 self.assertTrue(mock_template.delete.called)
133 def test__get_server(self):
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()
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)
145 self.assertEqual(result['ip'], '127.0.0.1')
146 self.assertEqual(result['private_ip'], '10.0.0.1')