From 111bdbd51e6498fed8e81fa8b7f3d0cabe394b05 Mon Sep 17 00:00:00 2001 From: LiangLu Date: Tue, 26 Jan 2021 03:50:55 +0800 Subject: [PATCH] Improve tacker.objects related file UT coverage improve up coverage for: |-objects |-vnf_lcm_subscriptions.py |-vnf_package_vnfd.py |-vnfd.py |-vnfd_attribute.py Implements: blueprint improve-db-coverage Change-Id: I0e30b70d5ba800573966495b78b935b8fa5cd920 --- .../objects/test_vnf_lcm_subscriptions.py | 4 +- .../unit/objects/test_vnf_package_vnfd.py | 53 +++++++++++++ tacker/tests/unit/objects/test_vnfd.py | 66 +++++++++++++++++ .../tests/unit/objects/test_vnfd_attribute.py | 74 +++++++++++++++++++ 4 files changed, 195 insertions(+), 2 deletions(-) create mode 100644 tacker/tests/unit/objects/test_vnfd.py create mode 100644 tacker/tests/unit/objects/test_vnfd_attribute.py diff --git a/tacker/tests/unit/objects/test_vnf_lcm_subscriptions.py b/tacker/tests/unit/objects/test_vnf_lcm_subscriptions.py index fb990cd63..b635bcc5d 100644 --- a/tacker/tests/unit/objects/test_vnf_lcm_subscriptions.py +++ b/tacker/tests/unit/objects/test_vnf_lcm_subscriptions.py @@ -19,10 +19,10 @@ from tacker.tests.unit.objects import fakes from tacker.tests import uuidsentinel -class TestVnfd(SqlTestCase): +class TestVnfLcmSubScriptions(SqlTestCase): def setUp(self): - super(TestVnfd, self).setUp() + super(TestVnfLcmSubScriptions, self).setUp() self.context = context.get_admin_context() self.vnf_package = self._create_vnf_package() self.vnf_package_vnfd = self._create_and_upload_vnf_package_vnfd() diff --git a/tacker/tests/unit/objects/test_vnf_package_vnfd.py b/tacker/tests/unit/objects/test_vnf_package_vnfd.py index e35cd09ea..9d11f7819 100644 --- a/tacker/tests/unit/objects/test_vnf_package_vnfd.py +++ b/tacker/tests/unit/objects/test_vnf_package_vnfd.py @@ -60,3 +60,56 @@ class TestVnfPackageVnfd(SqlTestCase): self.context, vnf_pack_vnfd_obj.vnfd_id) self.assertEqual('test_provider', result.vnf_provider) self.assertEqual('test_version', result.vnf_software_version) + + def test_get_vnf_package_vnfd(self): + vnf_pack = vnf_package.VnfPackage(context=self.context, + **fakes.vnf_package_data) + vnf_pack.create() + vnf_pack_vnfd_obj = vnf_package_vnfd.VnfPackageVnfd( + context=self.context, **fakes.vnf_pack_vnfd_data(vnf_pack.id)) + vnf_pack_vnfd_obj.create() + result = vnf_pack_vnfd_obj.get_vnf_package_vnfd( + vnf_pack_vnfd_obj.id) + # due to vnf_pack_vnfd_obj.get_vnf_package_vnfd() is not connect to + # DB actually, it will return None as a result here + actual_result = None + self.assertEqual(result, actual_result) + + def test_get_vnf_package_vnfd_by_vnfid(self): + vnf_pack = vnf_package.VnfPackage(context=self.context, + **fakes.vnf_package_data) + vnf_pack.create() + vnf_pack_vnfd_obj = vnf_package_vnfd.VnfPackageVnfd( + context=self.context, **fakes.vnf_pack_vnfd_data(vnf_pack.id)) + vnf_pack_vnfd_obj.create() + result = vnf_pack_vnfd_obj.get_vnf_package_vnfd_by_vnfid( + self.context, vnf_pack_vnfd_obj.id) + # due to vnf_pack_vnfd_obj.get_vnf_package_vnfd() is not connect to + # DB actually, it will return None as a result here + actual_result = None + self.assertEqual(result, actual_result) + + def test_get_by_vnfd_id(self): + vnf_pack = vnf_package.VnfPackage(context=self.context, + **fakes.vnf_package_data) + vnf_pack.create() + vnf_pack_vnfd_obj = vnf_package_vnfd.VnfPackageVnfd( + context=self.context, **fakes.vnf_pack_vnfd_data(vnf_pack.id)) + vnf_pack_vnfd_obj.create() + vnf_pack_vnfd_obj.get_by_vnfdId( + self.context, vnf_pack_vnfd_obj.vnfd_id) + + def test_get_by_package_id(self): + vnf_pack = vnf_package.VnfPackage(context=self.context, + **fakes.vnf_package_data) + vnf_pack.create() + vnf_pack_vnfd_obj = vnf_package_vnfd.VnfPackageVnfd( + context=self.context, **fakes.vnf_pack_vnfd_data(vnf_pack.id)) + vnf_pack_vnfd_obj.create() + result = vnf_pack_vnfd_obj.get_by_vnfdId( + self.context, vnf_pack_vnfd_obj.package_uuid) + vnf_pack_vnfd_obj.deleted = True + # due to vnf_pack_vnfd_obj.get_vnf_package_vnfd() is not connect to + # DB actually, it will return None as a result here + actual_result = None + self.assertEqual(result, actual_result) diff --git a/tacker/tests/unit/objects/test_vnfd.py b/tacker/tests/unit/objects/test_vnfd.py new file mode 100644 index 000000000..80de7aaf6 --- /dev/null +++ b/tacker/tests/unit/objects/test_vnfd.py @@ -0,0 +1,66 @@ +# Copyright (C) 2020 NTT DATA +# 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 datetime +import ddt + +from tacker import context +from tacker.db import api as sqlalchemy_api +from tacker.db.nfvo import nfvo_db +from tacker import objects +from tacker.tests.unit.db.base import SqlTestCase +from tacker.tests.unit.objects import fakes +from tacker.tests import uuidsentinel + +get_engine = sqlalchemy_api.get_engine + + +@ddt.ddt +class TestVnfd(SqlTestCase): + + def setUp(self): + super(TestVnfd, self).setUp() + self.context = context.get_admin_context() + self.vims = nfvo_db.Vim(**fakes.vim_data) + self.engine = get_engine() + self.conn = self.engine.connect() + self.vim = nfvo_db.Vim() + self.vnfd_data = { + "id": uuidsentinel.id, + "tenant_id": uuidsentinel.vnfd_id, + "name": "name", + "description": "description", + "mgmt_driver": "mgmt_driver", + "deleted_at": datetime.datetime.now() + } + self.vnfd_object = objects.vnfd.Vnfd( + context=self.context, **self.vnfd_data) + + def test_obj_from_db_obj(self): + db_obj = {"key": "value"} + result = self.vnfd_object.obj_from_db_obj( + self.context, db_obj) + self.assertIsInstance(result, objects.vnfd.Vnfd) + + def test_destroy(self): + self.vnfd_object.destroy(self.vnfd_object.id) + + def test_delete(self): + self.vnfd_object.delete(self.vnfd_object.id) + + def test_check_vnfd_attribute(self): + result = self.vnfd_object.check_vnfd( + self.vnfd_object.id) + self.assertEqual(result, "FALSE") diff --git a/tacker/tests/unit/objects/test_vnfd_attribute.py b/tacker/tests/unit/objects/test_vnfd_attribute.py new file mode 100644 index 000000000..111132bed --- /dev/null +++ b/tacker/tests/unit/objects/test_vnfd_attribute.py @@ -0,0 +1,74 @@ +# Copyright (C) 2021 FUJITSU +# 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 ddt +from unittest import mock + +from tacker import context +from tacker.db import api as sqlalchemy_api +from tacker.db.nfvo import nfvo_db +from tacker import objects +from tacker.tests.unit.db.base import SqlTestCase +from tacker.tests.unit.objects import fakes +from tacker.tests import uuidsentinel + +get_engine = sqlalchemy_api.get_engine + + +@ddt.ddt +class TestVnfdAttribute(SqlTestCase): + + def setUp(self): + super(TestVnfdAttribute, self).setUp() + self.context = context.get_admin_context() + self.vims = nfvo_db.Vim(**fakes.vim_data) + self.engine = get_engine() + self.conn = self.engine.connect() + self.vim = nfvo_db.Vim() + self.vnfd_attribute_data = { + "id": uuidsentinel.id, + "vnfd_id": uuidsentinel.vnfd_id, + "key": "key", + "value": "value" + } + self.vnfd_attribute_object = objects.vnfd_attribute.VnfdAttribute( + context=self.context, **self.vnfd_attribute_data) + + @mock.patch.object(objects.vnfd_attribute, '_vnfd_attribute_create') + def test_create(self, mock_vnfd_create): + mock_vnfd_create.return_value = { + "id": uuidsentinel.id, + "vnfd_id": uuidsentinel.vnfd_id, + "key": "key", + "value": "value" + } + self.vnfd_attribute_object.create() + + def test_obj_from_db_obj(self): + db_obj = {"key": "value"} + result = self.vnfd_attribute_object.obj_from_db_obj( + self.context, db_obj) + self.assertIsInstance(result, objects.vnfd_attribute.VnfdAttribute) + + def test_destroy(self): + self.vnfd_attribute_object.destroy(self.vnfd_attribute_object.id) + + def test_delete(self): + self.vnfd_attribute_object.delete(self.vnfd_attribute_object.id) + + def test_check_vnfd_attribute(self): + result = self.vnfd_attribute_object.check_vnfd_attribute( + self.vnfd_attribute_object.id) + self.assertEqual(result, "FALSE")