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
|
#
# 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.
import mock
from heat.engine.clients.os import nova
from heat.engine.resources.openstack.nova import host_aggregate
from heat.engine import stack
from heat.engine import template
from heat.tests import common
from heat.tests import utils
AGGREGATE_TEMPLATE = {
'heat_template_version': '2013-05-23',
'description': 'Heat Aggregate creation example',
'resources': {
'my_aggregate': {
'type': 'OS::Nova::HostAggregate',
'properties': {
'name': 'host_aggregate',
'availability_zone': 'nova',
'hosts': ['host_1', 'host_2'],
'metadata': {"availability_zone": "nova"}
}
}
}
}
class NovaHostAggregateTest(common.HeatTestCase):
def setUp(self):
super(NovaHostAggregateTest, self).setUp()
self.patchobject(nova.NovaClientPlugin,
'has_extension',
return_value=True)
self.ctx = utils.dummy_context()
self.stack = stack.Stack(
self.ctx, 'nova_aggregate_test_stack',
template.Template(AGGREGATE_TEMPLATE)
)
self.my_aggregate = self.stack['my_aggregate']
nova_client = mock.MagicMock()
self.novaclient = mock.MagicMock()
self.my_aggregate.client = nova_client
nova_client.return_value = self.novaclient
self.aggregates = self.novaclient.aggregates
def test_resource_mapping(self):
mapping = host_aggregate.resource_mapping()
self.assertEqual(1, len(mapping))
self.assertEqual(host_aggregate.HostAggregate,
mapping['OS::Nova::HostAggregate'])
self.assertIsInstance(self.my_aggregate,
host_aggregate.HostAggregate)
def test_aggregate_handle_create(self):
value = mock.MagicMock()
aggregate_id = '927202df-1afb-497f-8368-9c2d2f26e5db'
value.id = aggregate_id
self.aggregates.create.return_value = value
self.my_aggregate.handle_create()
value.set_metadata.assert_called_once_with(
{"availability_zone": "nova"})
self.assertEqual(2, value.add_host.call_count)
self.assertEqual(aggregate_id, self.my_aggregate.resource_id)
def test_aggregate_handle_update_name(self):
value = mock.MagicMock()
self.aggregates.get.return_value = value
prop_diff = {'name': 'new_host_aggregate',
'metadata': {'availability_zone': 'new_nova'},
'availability_zone': 'new_nova'}
expected = {'name': 'new_host_aggregate',
'availability_zone': 'new_nova'}
self.my_aggregate.handle_update(
json_snippet=None, tmpl_diff=None, prop_diff=prop_diff
)
value.update.assert_called_once_with(expected)
def test_aggregate_handle_update_hosts(self):
ag = mock.MagicMock()
ag.hosts = ['host_1', 'host_2']
self.aggregates.get.return_value = ag
prop_diff = {'hosts': ['host_1', 'host_3']}
add_host_expected = 'host_3'
remove_host_expected = 'host_2'
self.my_aggregate.handle_update(
json_snippet=None, tmpl_diff=None, prop_diff=prop_diff
)
self.assertEqual(0, ag.update.call_count)
self.assertEqual(0, ag.set_metadata.call_count)
ag.add_host.assert_called_once_with(add_host_expected)
ag.remove_host.assert_called_once_with(remove_host_expected)
def test_aggregate_handle_update_metadata(self):
ag = mock.MagicMock()
self.aggregates.get.return_value = ag
prop_diff = {'metadata': {'availability_zone': 'nova3'}}
set_metadata_expected = {'availability_zone': 'nova3'}
self.my_aggregate.handle_update(
json_snippet=None, tmpl_diff=None, prop_diff=prop_diff
)
self.assertEqual(0, ag.update.call_count)
self.assertEqual(0, ag.add_host.call_count)
self.assertEqual(0, ag.remove_host.call_count)
ag.set_metadata.assert_called_once_with(set_metadata_expected)
def test_aggregate_handle_delete(self):
ag = mock.MagicMock()
ag.id = '927202df-1afb-497f-8368-9c2d2f26e5db'
ag.hosts = ['host_1']
self.aggregates.get.return_value = ag
self.aggregates.hosts = ag.hosts
self.my_aggregate.resource_id = ag.id
self.my_aggregate.handle_delete()
self.assertEqual(1, self.aggregates.get.call_count)
self.assertEqual(ag.hosts, self.aggregates.hosts)
ag.remove_host.assert_called_once_with(ag.hosts[0])
self.aggregates.delete.assert_called_once_with(ag.id)
|