File: test_flavor.py

package info (click to toggle)
python-openstacksdk 4.4.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,352 kB
  • sloc: python: 122,960; sh: 153; makefile: 23
file content (190 lines) | stat: -rw-r--r-- 7,123 bytes parent folder | download
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
#
# 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.

"""
test_flavor
----------------------------------

Functional tests for flavor resource.
"""

from openstack import exceptions
from openstack.tests.functional import base


class TestFlavor(base.BaseFunctionalTest):
    def setUp(self):
        super().setUp()

        # Generate a random name for flavors in this test
        self.new_item_name = self.getUniqueString('flavor')

        self.addCleanup(self._cleanup_flavors)

    def _cleanup_flavors(self):
        exception_list = list()
        if self.operator_cloud:
            for f in self.operator_cloud.list_flavors(get_extra=False):
                if f['name'].startswith(self.new_item_name):
                    try:
                        self.operator_cloud.delete_flavor(f['id'])
                    except Exception as e:
                        # We were unable to delete a flavor, let's try with
                        # next
                        exception_list.append(str(e))
                    continue
        if exception_list:
            # Raise an error: we must make users aware that something went
            # wrong
            raise exceptions.SDKException('\n'.join(exception_list))

    def test_create_flavor(self):
        if not self.operator_cloud:
            self.skipTest("Operator cloud is required for this test")

        flavor_name = self.new_item_name + '_create'
        flavor_kwargs = dict(
            name=flavor_name,
            ram=1024,
            vcpus=2,
            disk=10,
            ephemeral=5,
            swap=100,
            rxtx_factor=1.5,
            is_public=True,
        )

        flavor = self.operator_cloud.create_flavor(**flavor_kwargs)

        self.assertIsNotNone(flavor['id'])

        # When properly normalized, we should always get an extra_specs
        # and expect empty dict on create.
        self.assertIn('extra_specs', flavor)
        self.assertEqual({}, flavor['extra_specs'])

        # We should also always have ephemeral and public attributes
        self.assertIn('ephemeral', flavor)
        self.assertEqual(5, flavor['ephemeral'])
        self.assertIn('is_public', flavor)
        self.assertTrue(flavor['is_public'])

        for key in flavor_kwargs.keys():
            self.assertIn(key, flavor)
        for key, value in flavor_kwargs.items():
            self.assertEqual(value, flavor[key])

    def test_list_flavors(self):
        pub_flavor_name = self.new_item_name + '_public'
        priv_flavor_name = self.new_item_name + '_private'
        public_kwargs = dict(
            name=pub_flavor_name, ram=1024, vcpus=2, disk=10, is_public=True
        )
        private_kwargs = dict(
            name=priv_flavor_name, ram=1024, vcpus=2, disk=10, is_public=False
        )

        if self.operator_cloud:
            # Create a public and private flavor. We expect both to be listed
            # for an operator.
            self.operator_cloud.create_flavor(**public_kwargs)
            self.operator_cloud.create_flavor(**private_kwargs)

            flavors = self.operator_cloud.list_flavors(get_extra=False)

            # Flavor list will include the standard devstack flavors. We just
            # want to make sure both of the flavors we just created are
            # present.
            found = []
            for f in flavors:
                # extra_specs should be added within list_flavors()
                self.assertIn('extra_specs', f)
                if f['name'] in (pub_flavor_name, priv_flavor_name):
                    found.append(f)
            self.assertEqual(2, len(found))
        else:
            self.user_cloud.list_flavors()

    def test_flavor_access(self):
        if not self.operator_cloud:
            self.skipTest("Operator cloud is required for this test")

        priv_flavor_name = self.new_item_name + '_private'
        private_kwargs = dict(
            name=priv_flavor_name, ram=1024, vcpus=2, disk=10, is_public=False
        )
        new_flavor = self.operator_cloud.create_flavor(**private_kwargs)

        # Validate the 'demo' user cannot see the new flavor
        flavors = self.user_cloud.search_flavors(priv_flavor_name)
        self.assertEqual(0, len(flavors))

        # We need the tenant ID for the 'demo' user
        project = self.operator_cloud.get_project('demo')
        self.assertIsNotNone(project)

        # Now give 'demo' access
        self.operator_cloud.add_flavor_access(new_flavor['id'], project['id'])

        # Now see if the 'demo' user has access to it
        flavors = self.user_cloud.search_flavors(priv_flavor_name)
        self.assertEqual(1, len(flavors))
        self.assertEqual(priv_flavor_name, flavors[0]['name'])

        # Now see if the 'demo' user has access to it without needing
        #  the demo_cloud access.
        acls = self.operator_cloud.list_flavor_access(new_flavor['id'])
        self.assertEqual(1, len(acls))
        self.assertEqual(project['id'], acls[0]['tenant_id'])

        # Now revoke the access and make sure we can't find it
        self.operator_cloud.remove_flavor_access(
            new_flavor['id'], project['id']
        )
        flavors = self.user_cloud.search_flavors(priv_flavor_name)
        self.assertEqual(0, len(flavors))

    def test_set_unset_flavor_specs(self):
        """
        Test setting and unsetting flavor extra specs
        """
        if not self.operator_cloud:
            self.skipTest("Operator cloud is required for this test")

        flavor_name = self.new_item_name + '_spec_test'
        kwargs = dict(name=flavor_name, ram=1024, vcpus=2, disk=10)
        new_flavor = self.operator_cloud.create_flavor(**kwargs)

        # Expect no extra_specs
        self.assertEqual({}, new_flavor['extra_specs'])

        # Now set them
        extra_specs = {'foo': 'aaa', 'bar': 'bbb'}
        self.operator_cloud.set_flavor_specs(new_flavor['id'], extra_specs)
        mod_flavor = self.operator_cloud.get_flavor(
            new_flavor['id'], get_extra=True
        )

        # Verify extra_specs were set
        self.assertIn('extra_specs', mod_flavor)
        self.assertEqual(extra_specs, mod_flavor['extra_specs'])

        # Unset the 'foo' value
        self.operator_cloud.unset_flavor_specs(mod_flavor['id'], ['foo'])
        mod_flavor = self.operator_cloud.get_flavor_by_id(
            new_flavor['id'], get_extra=True
        )

        # Verify 'foo' is unset and 'bar' is still set
        self.assertEqual({'bar': 'bbb'}, mod_flavor['extra_specs'])