summaryrefslogtreecommitdiff
path: root/heat/tests/openstack/octavia/test_flavor.py
blob: 556b9d31fa838ab76100f997275473ea32e90f52 (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
#
#    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.common import template_format
from heat.tests import common
from heat.tests.openstack.octavia import inline_templates
from heat.tests import utils


class FlavorTest(common.HeatTestCase):

    def _create_stack(self, tmpl=inline_templates.FLAVOR_TEMPLATE):
        self.t = template_format.parse(tmpl)
        self.stack = utils.parse_stack(self.t)
        self.flavor = self.stack['flavor']

        self.octavia_client = mock.MagicMock()
        self.flavor.client = mock.MagicMock()
        self.flavor.client.return_value = self.octavia_client

        self.flavor.client_plugin().client = mock.MagicMock(
            return_value=self.octavia_client)
        self.patchobject(self.flavor, 'physical_resource_name',
                         return_value='resource_name')

    def test_create(self):
        self._create_stack()
        self.octavia_client.flavor_show.side_effect = [
            {'flavor': {'id': 'f123'}}
        ]
        expected = {
            'flavor': {
                'name': 'test_name',
                'description': 'test_description',
                'flavor_profile_id': 'test_flavor_profile_id',
                'enabled': True,
            }
        }

        self.flavor.handle_create()

        self.octavia_client.flavor_create.assert_called_with(
            json=expected)

    def test_update(self):
        self._create_stack()
        self.flavor.resource_id_set('f123')
        prop_diff = {
            'name': 'test_name2',
            'description': 'test_description2',
            'flavor_profile_id': 'test_flavor_profile_id2',
            'enabled': False,
        }

        self.flavor.handle_update(None, None, prop_diff)

        self.octavia_client.flavor_set.assert_called_once_with(
            'f123', json={'flavor': prop_diff})

        self.octavia_client.flavor_set.reset_mock()

        # Updating a flavor with None as name should use
        # physical_resource_name() as new name
        prop_diff = {
            'name': None,
            'description': 'test_description3',
            'flavor_profile_id': 'test_flavor_profile_id3',
            'enabled': True,
        }

        self.flavor.handle_update(None, None, prop_diff)

        self.assertEqual(prop_diff['name'], 'resource_name')
        self.octavia_client.flavor_set.assert_called_once_with(
            'f123', json={'flavor': prop_diff})

    def test_delete(self):
        self._create_stack()
        self.flavor.resource_id_set('f123')

        self.flavor.handle_delete()

        self.octavia_client.flavor_delete.assert_called_with('f123')