summaryrefslogtreecommitdiff
path: root/nova/tests/unit/api/openstack/compute/test_flavor_manage.py
blob: f8412c772c415a872ae12e38e2f45f67e124f920 (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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# Copyright 2011 Andrew Bogott for the Wikimedia Foundation
# All Rights Reserved.
#
#    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 copy

import mock
from oslo_serialization import jsonutils
import webob

from nova.api.openstack.compute import flavor_access as flavor_access_v21
from nova.api.openstack.compute import flavor_manage as flavormanage_v21
from nova.compute import flavors
from nova.db import constants as db_const
from nova import exception
from nova import objects
from nova import test
from nova.tests.unit.api.openstack import fakes


def fake_create(newflavor):
    newflavor['flavorid'] = 1234
    newflavor["name"] = 'test'
    newflavor["memory_mb"] = 512
    newflavor["vcpus"] = 2
    newflavor["root_gb"] = 1
    newflavor["ephemeral_gb"] = 1
    newflavor["swap"] = 512
    newflavor["rxtx_factor"] = 1.0
    newflavor["is_public"] = True
    newflavor["disabled"] = False


def fake_create_without_swap(newflavor):
    newflavor['flavorid'] = 1234
    newflavor["name"] = 'test'
    newflavor["memory_mb"] = 512
    newflavor["vcpus"] = 2
    newflavor["root_gb"] = 1
    newflavor["ephemeral_gb"] = 1
    newflavor["swap"] = 0
    newflavor["rxtx_factor"] = 1.0
    newflavor["is_public"] = True
    newflavor["disabled"] = False
    newflavor["extra_specs"] = {"key1": "value1"}


class FlavorManageTestV21(test.NoDBTestCase):
    controller = flavormanage_v21.FlavorManageController()
    validation_error = exception.ValidationError
    base_url = '/v2/%s/flavors' % fakes.FAKE_PROJECT_ID
    microversion = '2.1'

    def setUp(self):
        super(FlavorManageTestV21, self).setUp()
        self.stub_out("nova.objects.Flavor.create", fake_create)

        self.request_body = {
            "flavor": {
                "name": "test",
                "ram": 512,
                "vcpus": 2,
                "disk": 1,
                "OS-FLV-EXT-DATA:ephemeral": 1,
                "id": '1234',
                "swap": 512,
                "rxtx_factor": 1,
                "os-flavor-access:is_public": True,
            }
        }
        self.expected_flavor = self.request_body

    def _get_http_request(self, url=''):
        return fakes.HTTPRequest.blank(url, version=self.microversion,
                                       use_admin_context=True)

    @property
    def app(self):
        return fakes.wsgi_app_v21()

    @mock.patch('nova.objects.Flavor.destroy')
    def test_delete(self, mock_destroy):
        res = self.controller._delete(self._get_http_request(), 1234)

        # NOTE: on v2.1, http status code is set as wsgi_code of API
        # method instead of status_int in a response object.
        if isinstance(self.controller,
                      flavormanage_v21.FlavorManageController):
            status_int = self.controller._delete.wsgi_code
        else:
            status_int = res.status_int
        self.assertEqual(202, status_int)

        # subsequent delete should fail
        mock_destroy.side_effect = exception.FlavorNotFound(flavor_id=1234)
        self.assertRaises(webob.exc.HTTPNotFound,
                          self.controller._delete, self._get_http_request(),
                          1234)

    def _test_create_missing_parameter(self, parameter):
        body = {
            "flavor": {
                "name": "azAZ09. -_",
                "ram": 512,
                "vcpus": 2,
                "disk": 1,
                "OS-FLV-EXT-DATA:ephemeral": 1,
                "id": '1234',
                "swap": 512,
                "rxtx_factor": 1,
                "os-flavor-access:is_public": True,
            }
        }

        del body['flavor'][parameter]

        self.assertRaises(self.validation_error, self.controller._create,
                          self._get_http_request(), body=body)

    def test_create_missing_name(self):
        self._test_create_missing_parameter('name')

    def test_create_missing_ram(self):
        self._test_create_missing_parameter('ram')

    def test_create_missing_vcpus(self):
        self._test_create_missing_parameter('vcpus')

    def test_create_missing_disk(self):
        self._test_create_missing_parameter('disk')

    def _create_flavor_success_case(self, body, req=None, version=None):
        req = req if req else self._get_http_request(url=self.base_url)
        req.headers['Content-Type'] = 'application/json'
        req.headers['X-OpenStack-Nova-API-Version'] = (
            version or self.microversion)
        req.method = 'POST'
        req.body = jsonutils.dump_as_bytes(body)
        res = req.get_response(self.app)
        self.assertEqual(200, res.status_code)
        return jsonutils.loads(res.body)

    def test_create(self):
        body = self._create_flavor_success_case(self.request_body)
        for key in self.expected_flavor["flavor"]:
            self.assertEqual(body["flavor"][key],
                             self.expected_flavor["flavor"][key])

    def test_create_public_default(self):
        del self.request_body['flavor']['os-flavor-access:is_public']
        body = self._create_flavor_success_case(self.request_body)
        for key in self.expected_flavor["flavor"]:
            self.assertEqual(body["flavor"][key],
                             self.expected_flavor["flavor"][key])

    def test_create_without_flavorid(self):
        del self.request_body['flavor']['id']
        body = self._create_flavor_success_case(self.request_body)
        for key in self.expected_flavor["flavor"]:
            self.assertEqual(body["flavor"][key],
                             self.expected_flavor["flavor"][key])

    def _create_flavor_bad_request_case(self, body):
        self.assertRaises(self.validation_error, self.controller._create,
                          self._get_http_request(), body=body)

    def test_create_invalid_name(self):
        self.request_body['flavor']['name'] = 'bad !@#!$%\x00 name'
        self._create_flavor_bad_request_case(self.request_body)

    def test_create_flavor_name_is_whitespace(self):
        self.request_body['flavor']['name'] = ' '
        self._create_flavor_bad_request_case(self.request_body)

    def test_create_with_name_too_long(self):
        self.request_body['flavor']['name'] = 'a' * 256
        self._create_flavor_bad_request_case(self.request_body)

    def test_create_with_short_name(self):
        self.request_body['flavor']['name'] = ''
        self._create_flavor_bad_request_case(self.request_body)

    def test_create_with_name_leading_trailing_spaces(self):
        self.request_body['flavor']['name'] = '  test  '
        self._create_flavor_bad_request_case(self.request_body)

    def test_create_with_name_leading_trailing_spaces_compat_mode(self):
        req = self._get_http_request(url=self.base_url)
        req.set_legacy_v2()
        self.request_body['flavor']['name'] = '  test  '
        body = self._create_flavor_success_case(self.request_body, req)
        self.assertEqual('test', body['flavor']['name'])

    def test_create_without_flavorname(self):
        del self.request_body['flavor']['name']
        self._create_flavor_bad_request_case(self.request_body)

    def test_create_empty_body(self):
        body = {
            "flavor": {}
        }
        self._create_flavor_bad_request_case(body)

    def test_create_no_body(self):
        body = {}
        self._create_flavor_bad_request_case(body)

    def test_create_invalid_format_body(self):
        body = {
            "flavor": []
        }
        self._create_flavor_bad_request_case(body)

    def test_create_invalid_flavorid(self):
        self.request_body['flavor']['id'] = "!@#!$#!$^#&^$&"
        self._create_flavor_bad_request_case(self.request_body)

    def test_create_check_flavor_id_length(self):
        MAX_LENGTH = 255
        self.request_body['flavor']['id'] = "a" * (MAX_LENGTH + 1)
        self._create_flavor_bad_request_case(self.request_body)

    def test_create_with_leading_trailing_whitespaces_in_flavor_id(self):
        self.request_body['flavor']['id'] = "   bad_id   "
        self._create_flavor_bad_request_case(self.request_body)

    def test_create_without_ram(self):
        del self.request_body['flavor']['ram']
        self._create_flavor_bad_request_case(self.request_body)

    def test_create_with_0_ram(self):
        self.request_body['flavor']['ram'] = 0
        self._create_flavor_bad_request_case(self.request_body)

    def test_create_with_ram_exceed_max_limit(self):
        self.request_body['flavor']['ram'] = db_const.MAX_INT + 1
        self._create_flavor_bad_request_case(self.request_body)

    def test_create_without_vcpus(self):
        del self.request_body['flavor']['vcpus']
        self._create_flavor_bad_request_case(self.request_body)

    def test_create_with_0_vcpus(self):
        self.request_body['flavor']['vcpus'] = 0
        self._create_flavor_bad_request_case(self.request_body)

    def test_create_with_vcpus_exceed_max_limit(self):
        self.request_body['flavor']['vcpus'] = db_const.MAX_INT + 1
        self._create_flavor_bad_request_case(self.request_body)

    def test_create_without_disk(self):
        del self.request_body['flavor']['disk']
        self._create_flavor_bad_request_case(self.request_body)

    def test_create_with_minus_disk(self):
        self.request_body['flavor']['disk'] = -1
        self._create_flavor_bad_request_case(self.request_body)

    def test_create_with_disk_exceed_max_limit(self):
        self.request_body['flavor']['disk'] = db_const.MAX_INT + 1
        self._create_flavor_bad_request_case(self.request_body)

    def test_create_with_minus_ephemeral(self):
        self.request_body['flavor']['OS-FLV-EXT-DATA:ephemeral'] = -1
        self._create_flavor_bad_request_case(self.request_body)

    def test_create_with_ephemeral_exceed_max_limit(self):
        self.request_body['flavor'][
            'OS-FLV-EXT-DATA:ephemeral'] = db_const.MAX_INT + 1
        self._create_flavor_bad_request_case(self.request_body)

    def test_create_with_minus_swap(self):
        self.request_body['flavor']['swap'] = -1
        self._create_flavor_bad_request_case(self.request_body)

    def test_create_with_swap_exceed_max_limit(self):
        self.request_body['flavor']['swap'] = db_const.MAX_INT + 1
        self._create_flavor_bad_request_case(self.request_body)

    def test_create_with_minus_rxtx_factor(self):
        self.request_body['flavor']['rxtx_factor'] = -1
        self._create_flavor_bad_request_case(self.request_body)

    def test_create_with_rxtx_factor_exceed_max_limit(self):
        self.request_body['flavor']['rxtx_factor'] = \
            db_const.SQL_SP_FLOAT_MAX * 2
        self._create_flavor_bad_request_case(self.request_body)

    def test_create_with_non_boolean_is_public(self):
        self.request_body['flavor']['os-flavor-access:is_public'] = 123
        self._create_flavor_bad_request_case(self.request_body)

    def test_flavor_exists_exception_returns_409(self):
        expected = {
            "flavor": {
                "name": "test",
                "ram": 512,
                "vcpus": 2,
                "disk": 1,
                "OS-FLV-EXT-DATA:ephemeral": 1,
                "id": 1235,
                "swap": 512,
                "rxtx_factor": 1,
                "os-flavor-access:is_public": True,
            }
        }

        def fake_create(name, memory_mb, vcpus, root_gb, ephemeral_gb,
                        flavorid, swap, rxtx_factor, is_public, description):
            raise exception.FlavorExists(name=name)

        self.stub_out('nova.compute.flavors.create', fake_create)
        self.assertRaises(webob.exc.HTTPConflict, self.controller._create,
                          self._get_http_request(), body=expected)

    def test_invalid_memory_mb(self):
        """Check negative and decimal number can't be accepted."""
        self.assertRaises(exception.InvalidInput, flavors.create, "abc",
                          -512, 2, 1, 1, 1234, 512, 1, True)
        self.assertRaises(exception.InvalidInput, flavors.create, "abcd",
                          512.2, 2, 1, 1, 1234, 512, 1, True)
        self.assertRaises(exception.InvalidInput, flavors.create, "abcde",
                          None, 2, 1, 1, 1234, 512, 1, True)
        self.assertRaises(exception.InvalidInput, flavors.create, "abcdef",
                          512, 2, None, 1, 1234, 512, 1, True)
        self.assertRaises(exception.InvalidInput, flavors.create, "abcdef",
                          "test_memory_mb", 2, None, 1, 1234, 512, 1, True)

    def test_create_with_description(self):
        """With microversion <2.55 this should return a failure."""
        self.request_body['flavor']['description'] = 'invalid'
        ex = self.assertRaises(
            self.validation_error, self.controller._create,
            self._get_http_request(), body=self.request_body)
        self.assertIn('description', str(ex))

    def test_flavor_update_description(self):
        """With microversion <2.55 this should return a failure."""
        flavor = self._create_flavor_success_case(self.request_body)['flavor']
        self.assertRaises(
            exception.VersionNotFoundForAPIMethod, self.controller._update,
            self._get_http_request(), flavor['id'],
            body={'flavor': {'description': 'nope'}})


class FlavorManageTestV2_55(FlavorManageTestV21):
    microversion = '2.55'

    def get_flavor(self, flavor, **kwargs):
        return objects.Flavor(
            flavorid=flavor['id'], name=flavor['name'],
            memory_mb=flavor['ram'], vcpus=flavor['vcpus'],
            root_gb=flavor['disk'], swap=flavor['swap'],
            ephemeral_gb=flavor['OS-FLV-EXT-DATA:ephemeral'],
            disabled=flavor['OS-FLV-DISABLED:disabled'],
            is_public=flavor['os-flavor-access:is_public'],
            rxtx_factor=flavor['rxtx_factor'],
            description=flavor['description'],
            **kwargs)

    def setUp(self):
        super(FlavorManageTestV2_55, self).setUp()
        # Send a description in POST /flavors requests.
        self.request_body['flavor']['description'] = 'test description'

    def test_create_with_description(self):
        # test_create already tests this.
        pass

    @mock.patch('nova.objects.Flavor.get_by_flavor_id')
    @mock.patch('nova.objects.Flavor.save')
    def test_flavor_update_description(self, mock_flavor_save, mock_get):
        """Tests updating a flavor description."""
        # First create a flavor.
        flavor = self._create_flavor_success_case(self.request_body)['flavor']
        self.assertEqual('test description', flavor['description'])
        mock_get.return_value = self.get_flavor(flavor)
        # Now null out the flavor description.
        flavor = self.controller._update(
            self._get_http_request(), flavor['id'],
            body={'flavor': {'description': None}})['flavor']
        self.assertIsNone(flavor['description'])
        mock_get.assert_called_once_with(
            test.MatchType(fakes.FakeRequestContext), flavor['id'])
        mock_flavor_save.assert_called_once_with()

    @mock.patch('nova.objects.Flavor.get_by_flavor_id',
                side_effect=exception.FlavorNotFound(flavor_id='notfound'))
    def test_flavor_update_not_found(self, mock_get):
        """Tests that a 404 is returned if the flavor is not found."""
        self.assertRaises(webob.exc.HTTPNotFound,
                          self.controller._update,
                          self._get_http_request(), 'notfound',
                          body={'flavor': {'description': None}})

    def test_flavor_update_missing_description(self):
        """Tests that a schema validation error is raised if no description
        is provided in the update request body.
        """
        self.assertRaises(self.validation_error,
                          self.controller._update,
                          self._get_http_request(), 'invalid',
                          body={'flavor': {}})

    def test_create_with_invalid_description(self):
        # NOTE(mriedem): Intentionally not using ddt for this since ddt will
        # create a test name that has 65536 'a's in the name which blows up
        # the console output.
        for description in ('bad !@#!$%\x00 description',   # printable chars
                            'a' * 65536):                   # maxLength
            self.request_body['flavor']['description'] = description
            self.assertRaises(self.validation_error, self.controller._create,
                              self._get_http_request(), body=self.request_body)

    @mock.patch('nova.objects.Flavor.get_by_flavor_id')
    @mock.patch('nova.objects.Flavor.save')
    def test_update_with_invalid_description(self, mock_flavor_save, mock_get):
        # First create a flavor.
        flavor = self._create_flavor_success_case(self.request_body)['flavor']
        self.assertEqual('test description', flavor['description'])
        mock_get.return_value = objects.Flavor(
            flavorid=flavor['id'], name=flavor['name'],
            memory_mb=flavor['ram'], vcpus=flavor['vcpus'],
            root_gb=flavor['disk'], swap=flavor['swap'],
            ephemeral_gb=flavor['OS-FLV-EXT-DATA:ephemeral'],
            disabled=flavor['OS-FLV-DISABLED:disabled'],
            is_public=flavor['os-flavor-access:is_public'],
            description=flavor['description'])
        # NOTE(mriedem): Intentionally not using ddt for this since ddt will
        # create a test name that has 65536 'a's in the name which blows up
        # the console output.
        for description in ('bad !@#!$%\x00 description',   # printable chars
                            'a' * 65536):                   # maxLength
            self.request_body['flavor']['description'] = description
            self.assertRaises(self.validation_error, self.controller._update,
                              self._get_http_request(), flavor['id'],
                              body={'flavor': {'description': description}})


class FlavorManageTestV2_61(FlavorManageTestV2_55):
    """Run the same tests as we would for v2.55 but with a extra_specs."""
    microversion = '2.61'

    def get_flavor(self, flavor):
        return super(FlavorManageTestV2_61, self).get_flavor(
            flavor, extra_specs={"key1": "value1"})

    def setUp(self):
        super(FlavorManageTestV2_61, self).setUp()
        self.expected_flavor = copy.deepcopy(self.request_body)
        self.expected_flavor['flavor']['extra_specs'] = {}

    @mock.patch('nova.objects.Flavor.get_by_flavor_id')
    @mock.patch('nova.objects.Flavor.save')
    def test_flavor_update_extra_spec(self, mock_flavor_save, mock_get):
        # First create a flavor.
        flavor = self._create_flavor_success_case(self.request_body)['flavor']
        mock_get.return_value = self.get_flavor(flavor)
        flavor = self.controller._update(
            self._get_http_request(), flavor['id'],
            body={'flavor': {'description': None}})['flavor']
        self.assertEqual({"key1": "value1"}, flavor['extra_specs'])


class FlavorManageTestV2_75(FlavorManageTestV2_61):
    microversion = '2.75'

    FLAVOR_WITH_NO_SWAP = objects.Flavor(
        name='test',
        memory_mb=512,
        vcpus=2,
        root_gb=1,
        ephemeral_gb=1,
        flavorid=1234,
        rxtx_factor=1.0,
        disabled=False,
        is_public=True,
        swap=0,
        extra_specs={"key1": "value1"}
    )

    def test_create_flavor_default_swap_value_old_version(self):
        self.stub_out("nova.objects.Flavor.create", fake_create_without_swap)
        del self.request_body['flavor']['swap']
        resp = self._create_flavor_success_case(self.request_body,
                                                version='2.74')
        self.assertEqual(resp['flavor']['swap'], "")

    @mock.patch('nova.objects.Flavor.get_by_flavor_id')
    @mock.patch('nova.objects.Flavor.save')
    def test_update_flavor_default_swap_value_old_version(self, mock_save,
                                                          mock_get):
        self.stub_out("nova.objects.Flavor.create", fake_create_without_swap)
        del self.request_body['flavor']['swap']
        flavor = self._create_flavor_success_case(self.request_body,
                                                version='2.74')['flavor']
        mock_get.return_value = self.FLAVOR_WITH_NO_SWAP
        req = fakes.HTTPRequest.blank('/%s/flavors' % fakes.FAKE_PROJECT_ID,
                                      version='2.74')
        req.method = 'PUT'
        response = self.controller._update(
            req, flavor['id'],
            body={'flavor': {'description': None}})['flavor']
        self.assertEqual(response['swap'], '')

    @mock.patch('nova.objects.FlavorList.get_all')
    def test_create_flavor_default_swap_value(self, mock_get):
        self.stub_out("nova.objects.Flavor.create", fake_create_without_swap)
        del self.request_body['flavor']['swap']
        resp = self._create_flavor_success_case(self.request_body)
        self.assertEqual(resp['flavor']['swap'], 0)

    @mock.patch('nova.objects.Flavor.get_by_flavor_id')
    @mock.patch('nova.objects.Flavor.save')
    def test_update_flavor_default_swap_value(self, mock_save, mock_get):
        self.stub_out("nova.objects.Flavor.create", fake_create_without_swap)
        del self.request_body['flavor']['swap']
        mock_get.return_value = self.FLAVOR_WITH_NO_SWAP
        flavor = self._create_flavor_success_case(self.request_body)['flavor']
        req = fakes.HTTPRequest.blank('/%s/flavors' % fakes.FAKE_PROJECT_ID,
                                      version=self.microversion)
        response = self.controller._update(
            req, flavor['id'],
            body={'flavor': {'description': None}})['flavor']
        self.assertEqual(response['swap'], 0)


class PrivateFlavorManageTestV21(test.TestCase):
    controller = flavormanage_v21.FlavorManageController()
    base_url = '/v2/%s/flavors' % fakes.FAKE_PROJECT_ID

    def setUp(self):
        super(PrivateFlavorManageTestV21, self).setUp()
        self.flavor_access_controller = (flavor_access_v21.
                                         FlavorAccessController())
        self.expected = {
            "flavor": {
                "name": "test",
                "ram": 512,
                "vcpus": 2,
                "disk": 1,
                "OS-FLV-EXT-DATA:ephemeral": 1,
                "swap": 512,
                "rxtx_factor": 1
            }
        }

    @property
    def app(self):
        return fakes.wsgi_app_v21(fake_auth_context=self._get_http_request().
                                     environ['nova.context'])

    def _get_http_request(self, url=''):
        return fakes.HTTPRequest.blank(url)

    def _get_response(self):
        req = self._get_http_request(self.base_url)
        req.headers['Content-Type'] = 'application/json'
        req.method = 'POST'
        req.body = jsonutils.dump_as_bytes(self.expected)
        res = req.get_response(self.app)
        return jsonutils.loads(res.body)

    def test_create_private_flavor_should_not_grant_flavor_access(self):
        self.expected["flavor"]["os-flavor-access:is_public"] = False
        body = self._get_response()
        for key in self.expected["flavor"]:
            self.assertEqual(body["flavor"][key], self.expected["flavor"][key])
        # Because for normal user can't access the non-public flavor without
        # access. So it need admin context at here.
        flavor_access_body = self.flavor_access_controller.index(
            fakes.HTTPRequest.blank('', use_admin_context=True),
            body["flavor"]["id"])
        expected_flavor_access_body = {
            "tenant_id": fakes.FAKE_PROJECT_ID,
            "flavor_id": "%s" % body["flavor"]["id"]
        }
        self.assertNotIn(expected_flavor_access_body,
                         flavor_access_body["flavor_access"])

    def test_create_public_flavor_should_not_create_flavor_access(self):
        self.expected["flavor"]["os-flavor-access:is_public"] = True
        body = self._get_response()
        for key in self.expected["flavor"]:
            self.assertEqual(body["flavor"][key], self.expected["flavor"][key])