2
# Licensed under the Apache License, Version 2.0 (the "License"); you may
3
# not use this file except in compliance with the License. You may obtain
4
# a copy of the License at
6
# http://www.apache.org/licenses/LICENSE-2.0
8
# Unless required by applicable law or agreed to in writing, software
9
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11
# License for the specific language governing permissions and limitations
16
from heat.engine import parser
17
from heat.engine import resource
18
from heat.engine import template
19
from heat.tests import common
20
from heat.tests.nova import fakes
21
from heat.tests import utils
23
from ..resources import nova_flavor # noqa
26
'heat_template_version': '2013-05-23',
29
'type': 'OS::Nova::Flavor',
37
'extra_specs': {"foo": "bar"}
44
class NovaFlavorTest(common.HeatTestCase):
46
super(NovaFlavorTest, self).setUp()
48
self.ctx = utils.dummy_context()
50
# For unit testing purpose. Register resource provider
52
resource._register_class("OS::Nova::Flavor", nova_flavor.NovaFlavor)
54
self.stack = parser.Stack(
55
self.ctx, 'nova_flavor_test_stack',
56
template.Template(flavor_template)
59
self.my_flavor = self.stack['my_flavor']
60
nova = mock.MagicMock()
61
self.novaclient = mock.MagicMock()
62
self.my_flavor.nova = nova
63
nova.return_value = self.novaclient
64
self.flavors = self.novaclient.flavors
66
def test_resource_mapping(self):
67
mapping = nova_flavor.resource_mapping()
68
self.assertEqual(1, len(mapping))
69
self.assertEqual(nova_flavor.NovaFlavor, mapping['OS::Nova::Flavor'])
70
self.assertIsInstance(self.my_flavor, nova_flavor.NovaFlavor)
72
def test_flavor_handle_create(self):
73
value = mock.MagicMock()
74
flavor_id = '927202df-1afb-497f-8368-9c2d2f26e5db'
76
self.flavors.create.return_value = value
77
self.my_flavor.handle_create()
78
value.set_keys.assert_called_once_with({"foo": "bar"})
79
self.assertEqual(flavor_id, self.my_flavor.resource_id)
81
def test_flavor_handle_update_keys(self):
82
value = mock.MagicMock()
83
self.flavors.get.return_value = value
84
value.get_keys.return_value = {}
86
new_keys = {"new_foo": "new_bar"}
87
prop_diff = {'extra_specs': new_keys}
88
self.my_flavor.handle_update(json_snippet=None,
89
tmpl_diff=None, prop_diff=prop_diff)
90
value.unset_keys.assert_called_once_with({})
91
value.set_keys.assert_called_once_with(new_keys)
93
def test_flavor_handle_delete(self):
94
self.resource_id = None
95
self.assertIsNone(self.my_flavor.handle_delete())
96
flavor_id = '927202df-1afb-497f-8368-9c2d2f26e5db'
97
self.my_flavor.resource_id = flavor_id
98
self.flavors.delete.return_value = None
99
self.assertIsNone(self.my_flavor.handle_delete())
100
self.flavors.delete.side_effect = fakes.fake_exception()
101
self.assertIsNone(self.my_flavor.handle_delete())