fix flavor.swap attribute type

swap attribute of flavor is expected to be int. It is already checked as
int in compute unittests, but not once returned as a fake.

Change-Id: I4da810cd9828374cebb2a120cce6a8d55f182ea9
This commit is contained in:
Artem Goncharov
2023-06-07 16:35:31 +02:00
parent d2a166a98e
commit fd2c41c697
5 changed files with 20 additions and 5 deletions

View File

@@ -57,7 +57,7 @@ class Flavor(resource.Resource):
#: The number of virtual CPUs this flavor offers. *Type: int*
vcpus = resource.Body('vcpus', type=int, default=0)
#: Size of the swap partitions.
swap = resource.Body('swap', default=0)
swap = resource.Body('swap', type=int, default=0)
#: Size of the ephemeral data disk attached to this server. *Type: int*
ephemeral = resource.Body('OS-FLV-EXT-DATA:ephemeral', type=int, default=0)
#: ``True`` if this flavor is disabled, ``False`` if not. *Type: bool*

View File

@@ -83,7 +83,14 @@ def _convert_type(value, data_type, list_type=None):
# and the AbsoluteLimits type for an example.
if isinstance(value, dict):
return data_type(**value)
return data_type(value)
try:
return data_type(value)
except ValueError:
# If we can not convert data to the expected type return empty
# instance of the expected type.
# This is necessary to handle issues like with flavor.swap where
# empty string means "0".
return data_type()
class _BaseComponent:

View File

@@ -70,7 +70,7 @@ def make_fake_flavor(flavor_id, name, ram=100, disk=1600, vcpus=24):
u'os-flavor-access:is_public': True,
u'ram': ram,
u'rxtx_factor': 1.0,
u'swap': u'',
u'swap': 0,
u'vcpus': vcpus,
}

View File

@@ -26,6 +26,7 @@ from openstack.identity.v3 import project as _project
from openstack.identity.v3 import user as _user
from openstack.image.v2 import image as _image
from openstack.network.v2 import port as _port
from openstack.test import fakes as _fakes
from openstack.tests import fakes
from openstack.tests.unit import base
from openstack.tests.unit.cloud import test_port
@@ -539,6 +540,7 @@ class TestMemoryCache(base.TestCase):
mock_uri = '{endpoint}/flavors/detail?is_public=None'.format(
endpoint=fakes.COMPUTE_ENDPOINT
)
flavors = list(_fakes.generate_fake_resources(_flavor.Flavor, count=2))
uris_to_mock = [
dict(
@@ -555,7 +557,7 @@ class TestMemoryCache(base.TestCase):
validate=dict(
headers={'OpenStack-API-Version': 'compute 2.53'}
),
json={'flavors': fakes.FAKE_FLAVOR_LIST},
json={'flavors': flavors},
),
]
self.use_compute_discovery()
@@ -568,7 +570,7 @@ class TestMemoryCache(base.TestCase):
self.cloud.list_flavors.invalidate(self.cloud)
self.assertResourceListEqual(
self.cloud.list_flavors(), fakes.FAKE_FLAVOR_LIST, _flavor.Flavor
self.cloud.list_flavors(), flavors, _flavor.Flavor
)
self.assert_calls()

View File

@@ -91,6 +91,12 @@ class TestFlavor(base.TestCase):
)
self.assertEqual(BASIC_EXAMPLE['rxtx_factor'], sot.rxtx_factor)
def test_make_basic_swap(self):
sot = flavor.Flavor(id=IDENTIFIER, swap="")
self.assertEqual(0, sot.swap)
sot1 = flavor.Flavor(id=IDENTIFIER, swap=0)
self.assertEqual(0, sot1.swap)
def test_make_defaults(self):
sot = flavor.Flavor(**DEFAULTS_EXAMPLE)
self.assertEqual(DEFAULTS_EXAMPLE['original_name'], sot.name)