tests: Add tests for loading, unloading metadefs

These methods were not being tested. Add some basic tests to validate
loading and unloading of our stock definitions, preventing regressions
during e.g. the SQLAlchemy 2.0 preparation series.

Implementation of this test highlights an minor issue in the DB API,
whereby the default values for some methods with 'sort_key' and
'sort_dir' parameters are not valid. This means you can't call these
without overriding the parameters. This is corrected.

Change-Id: I6e76dcacc32f05a70757df28ab2f119b25da440b
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
This commit is contained in:
Stephen Finucane 2023-07-24 16:30:27 +01:00
parent d409b27a79
commit 604ffd390c
2 changed files with 120 additions and 5 deletions

View File

@ -1958,8 +1958,10 @@ def _task_format(task_ref, task_info_ref=None):
return task_dict
def metadef_namespace_get_all(context, marker=None, limit=None, sort_key=None,
sort_dir=None, filters=None, session=None):
def metadef_namespace_get_all(
context, marker=None, limit=None, sort_key='created_at',
sort_dir='desc', filters=None, session=None,
):
"""List all available namespaces."""
session = session or get_session()
namespaces = metadef_namespace_api.get_all(
@ -2161,8 +2163,9 @@ def metadef_resource_type_association_get_all_by_namespace(
def metadef_tag_get_all(
context, namespace_name, filters=None, marker=None, limit=None,
sort_key=None, sort_dir=None, session=None):
context, namespace_name, filters=None, marker=None, limit=None,
sort_key='created_at', sort_dir='desc', session=None,
):
"""Get metadata-schema tags or raise if none exist."""
session = session or get_session()
return metadef_tag_api.get_all(

View File

@ -13,14 +13,28 @@
# under the License.
import copy
import os
import os.path
from glance.common import config
from glance.common import exception
from glance import context
from glance.db.sqlalchemy import metadata
import glance.tests.functional.db as db_tests
from glance.tests import utils as test_utils
# root of repo
ROOT_DIR = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
os.pardir,
os.pardir,
os.pardir,
os.pardir,
)
METADEFS_DIR = os.path.join(ROOT_DIR, 'etc', 'metadefs')
def build_namespace_fixture(**kwargs):
namespace = {
'namespace': 'MyTestNamespace',
@ -102,6 +116,7 @@ class TestMetadefDriver(test_utils.BaseTestCase):
"""Run before each test method to initialize test environment."""
super(TestMetadefDriver, self).setUp()
config.parse_args(args=[])
self.config(metadata_source_path=METADEFS_DIR)
context_cls = context.RequestContext
self.adm_context = context_cls(is_admin=True,
auth_token='user:user:admin')
@ -741,11 +756,108 @@ class MetadefTagTests(object):
created_tag['name'])
class MetadefLoadUnloadTests:
# if additional default schemas are added, you need to update this
_namespace_count = 33
_namespace_object_counts = {
'OS::Compute::Quota': 3,
'OS::Software::WebServers': 3,
'OS::Software::DBMS': 12,
'OS::Software::Runtimes': 5,
}
_namespace_property_counts = {
'CIM::ProcessorAllocationSettingData': 3,
'CIM::ResourceAllocationSettingData': 19,
'CIM::StorageAllocationSettingData': 13,
'CIM::VirtualSystemSettingData': 17,
'OS::Compute::XenAPI': 1,
'OS::Compute::InstanceData': 2,
'OS::Compute::Libvirt': 3,
'OS::Compute::VMwareQuotaFlavor': 2,
'OS::Cinder::Volumetype': 1,
'OS::Glance::Signatures': 4,
'OS::Compute::AggregateIoOpsFilter': 1,
'OS::Compute::RandomNumberGenerator': 3,
'OS::Compute::VTPM': 2,
'OS::Compute::Hypervisor': 2,
'OS::Compute::CPUPinning': 2,
'OS::OperatingSystem': 3,
'OS::Compute::AggregateDiskFilter': 1,
'OS::Compute::AggregateNumInstancesFilter': 1,
'OS::Compute::CPUMode': 1,
'OS::Compute::HostCapabilities': 7,
'OS::Compute::VirtCPUTopology': 6,
'OS::Glance::CommonImageProperties': 10,
'OS::Compute::GuestShutdownBehavior': 1,
'OS::Compute::VMwareFlavor': 2,
'OS::Compute::TPM': 1,
'OS::Compute::GuestMemoryBacking': 1,
'OS::Compute::LibvirtImage': 16,
'OS::Compute::VMware': 6,
'OS::Compute::Watchdog': 1,
}
def test_metadef_load_unload(self):
# load the metadata definitions
metadata.db_load_metadefs(self.db_api.get_engine())
# trust but verify
expected = self._namespace_count
namespaces = self.db_api.metadef_namespace_get_all(self.adm_context)
actual = len(namespaces)
self.assertEqual(
expected,
actual,
f"expected {expected} namespaces but got {actual}"
)
for namespace in namespaces:
expected = self._namespace_object_counts.get(
namespace['namespace'],
0,
)
objects = self.db_api.metadef_object_get_all(
self.adm_context,
namespace['namespace'],
)
actual = len(objects)
self.assertEqual(
expected,
actual,
f"expected {expected} objects in {namespace['namespace']} "
f"namespace but got {actual}: "
f"{', '.join(o['name'] for o in objects)}"
)
for namespace in namespaces:
expected = self._namespace_property_counts.get(
namespace['namespace'],
0,
)
properties = self.db_api.metadef_property_get_all(
self.adm_context,
namespace['namespace'],
)
actual = len(properties)
self.assertEqual(
expected,
actual,
f"expected {expected} properties in {namespace['namespace']} "
f"namespace but got {actual}: "
f"{', '.join(p['name'] for p in properties)}"
)
# unload the definitions
metadata.db_unload_metadefs(self.db_api.get_engine())
class MetadefDriverTests(MetadefNamespaceTests,
MetadefResourceTypeTests,
MetadefResourceTypeAssociationTests,
MetadefPropertyTests,
MetadefObjectTests,
MetadefTagTests):
MetadefTagTests,
MetadefLoadUnloadTests):
# collection class
pass