Browse Source

improve UT coverage of tacker-server related files

improve UT coverage for:
  api/vnflcm
  |-controller.py
  api/vnfpkgm
  |-controller.py
  contuctor
  |-vnf_lcm_rpc.py

Implements: blueprint improve-server-related-coverage
Change-Id: I49c9c67d6c8fd5c7b5fe161190308b29400fe83b
changes/41/772441/15 5.0.0.0rc1
LiangLu 2 months ago
parent
commit
550b39277f
3 changed files with 276 additions and 3 deletions
  1. +133
    -0
      tacker/tests/unit/conductor/conductorrpc/test_vnf_lcm_rpc.py
  2. +54
    -0
      tacker/tests/unit/vnflcm/test_controller.py
  3. +89
    -3
      tacker/tests/unit/vnfpkgm/test_controller.py

+ 133
- 0
tacker/tests/unit/conductor/conductorrpc/test_vnf_lcm_rpc.py View File

@ -0,0 +1,133 @@
# 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.
from unittest import mock
from tacker.common.rpc import BackingOffClient
from tacker.conductor.conductorrpc import vnf_lcm_rpc
from tacker.tests import base
class VnfLcmRPCTestCase(base.BaseTestCase):
def setUp(self):
super(VnfLcmRPCTestCase, self).setUp()
self.context = self.fake_admin_context()
self.rpc_api = vnf_lcm_rpc.VNFLcmRPCAPI()
self.cctxt_mock = mock.MagicMock()
@mock.patch.object(BackingOffClient, 'prepare')
def test_instantiate(self, prepare_mock):
vnf_instance = ""
vnf = ""
instantiate_vnf = ""
vnf_lcm_op_occs_id = ""
prepare_mock.return_value = self.cctxt_mock
self.rpc_api.instantiate(
self.context, vnf_instance,
vnf, instantiate_vnf, vnf_lcm_op_occs_id)
prepare_mock.assert_called()
self.cctxt_mock.cast.assert_called_once_with(
self.context, 'instantiate',
vnf_instance=vnf_instance,
vnf_dict=vnf,
instantiate_vnf=instantiate_vnf,
vnf_lcm_op_occs_id=vnf_lcm_op_occs_id)
@mock.patch.object(BackingOffClient, 'prepare')
def test_terminate(self, prepare_mock):
vnf_instance = ""
vnf = ""
terminate_vnf_req = ""
vnf_lcm_op_occs_id = ""
prepare_mock.return_value = self.cctxt_mock
self.rpc_api.terminate(
self.context, terminate_vnf_req,
vnf, terminate_vnf_req, vnf_lcm_op_occs_id)
prepare_mock.assert_called()
self.cctxt_mock.cast.assert_called_once_with(
self.context, 'terminate',
vnf_instance=vnf_instance,
vnf_dict=vnf,
terminate_vnf_req=terminate_vnf_req,
vnf_lcm_op_occs_id=vnf_lcm_op_occs_id)
@mock.patch.object(BackingOffClient, 'prepare')
def test_heal(self, prepare_mock):
vnf_instance = ""
vnf = ""
heal_vnf_request = ""
vnf_lcm_op_occs_id = ""
prepare_mock.return_value = self.cctxt_mock
self.rpc_api.heal(
self.context, heal_vnf_request,
vnf, heal_vnf_request, vnf_lcm_op_occs_id)
prepare_mock.assert_called()
self.cctxt_mock.cast.assert_called_once_with(
self.context, 'heal',
vnf_instance=vnf_instance,
vnf_dict=vnf,
heal_vnf_request=heal_vnf_request,
vnf_lcm_op_occs_id=vnf_lcm_op_occs_id)
@mock.patch.object(BackingOffClient, 'prepare')
def test_scale(self, prepare_mock):
vnf_info = ""
vnf_instance = ""
scale_vnf_request = ""
prepare_mock.return_value = self.cctxt_mock
self.rpc_api.scale(
self.context, vnf_info,
vnf_instance, scale_vnf_request)
prepare_mock.assert_called()
self.cctxt_mock.cast.assert_called_once_with(
self.context, 'scale',
vnf_info=vnf_info,
vnf_instance=vnf_instance,
scale_vnf_request=scale_vnf_request)
@mock.patch.object(BackingOffClient, 'prepare')
def test_update(self, prepare_mock):
vnf_lcm_opoccs = ""
body_data = ""
vnfd_pkg_data = ""
vnfd_id = ""
prepare_mock.return_value = self.cctxt_mock
self.rpc_api.update(
self.context, vnf_lcm_opoccs,
body_data, vnfd_pkg_data, vnfd_id)
prepare_mock.assert_called()
self.cctxt_mock.cast.assert_called_once_with(
self.context, 'update',
vnf_lcm_opoccs=vnf_lcm_opoccs,
body_data=body_data,
vnfd_pkg_data=vnfd_pkg_data,
vnfd_id=vnfd_id)
@mock.patch.object(BackingOffClient, 'prepare')
def test_rollback(self, prepare_mock):
vnf_info = ""
vnf_instance = ""
operation_params = ""
prepare_mock.return_value = self.cctxt_mock
self.rpc_api.rollback(
self.context, vnf_info,
vnf_instance, operation_params)
prepare_mock.assert_called()
self.cctxt_mock.cast.assert_called_once_with(
self.context, 'rollback',
vnf_info=vnf_info,
vnf_instance=vnf_instance,
operation_params=operation_params)

+ 54
- 0
tacker/tests/unit/vnflcm/test_controller.py View File

@ -4068,3 +4068,57 @@ class TestController(base.TestCase):
resp = req.get_response(self.app)
self.assertEqual(500, resp.status_code)
@mock.patch.object(vim_client.VimClient, "get_vim")
@mock.patch('tacker.api.vnflcm.v1.controller.'
'VnfLcmController._update_package_usage_state')
@mock.patch.object(objects.VnfPackage, 'get_by_id')
@mock.patch.object(objects.vnf_package.VnfPackage, 'save')
@mock.patch.object(TackerManager, 'get_service_plugins',
return_value={'VNFM':
test_nfvo_plugin.FakeVNFMPlugin()})
@mock.patch.object(objects.vnf_instance, '_vnf_instance_create')
@mock.patch.object(objects.vnf_package_vnfd.VnfPackageVnfd, 'get_by_id')
def test_create_using_internal_methods(
self, mock_get_by_id,
mock_vnf_instance_create,
mock_get_service_plugins,
mock_package_save,
mock_vnf_package_get_by_id,
mock_update_package_usage_state,
mock_get_vim):
mock_get_vim.return_value = self.vim_info
mock_get_by_id.return_value = fakes.return_vnf_package_vnfd()
mock_vnf_package_get_by_id.return_value = \
fakes.return_vnf_package_with_deployment_flavour()
updates = {'vnfd_id': uuidsentinel.vnfd_id,
'vnf_instance_description': 'SampleVnf Description',
'vnf_instance_name': 'SampleVnf',
'vnf_pkg_id': uuidsentinel.vnf_pkg_id,
'vnf_metadata': {"key": "value"}}
mock_vnf_instance_create.return_value =\
fakes.return_vnf_instance_model(**updates)
body = {'vnfdId': uuidsentinel.vnfd_id,
"vnfInstanceName": "SampleVnf",
"vnfInstanceDescription": "SampleVnf Description",
'metadata': {"key": "value"}}
req = fake_request.HTTPRequest.blank('/vnf_instances')
req.body = jsonutils.dump_as_bytes(body)
req.headers['Content-Type'] = 'application/json'
req.method = 'POST'
req.environ['tacker.context'] = self.context
# Call Create API
resp = req.get_response(self.app)
self.assertEqual(500, resp.status_code)
expected_vnf = {'tackerFault': {'code': 500,
'message': 'Unexpected API Error. Please report this at '
'http://bugs.launchpad.net/tacker/ and attach the '
'Tacker API log if possible.\n'
"<class 'webob.exc.HTTPInternalServerError'>"}}
self.assertEqual(expected_vnf, resp.json)

+ 89
- 3
tacker/tests/unit/vnfpkgm/test_controller.py View File

@ -992,7 +992,8 @@ class TestController(base.TestCase):
def test_fetch_vnf_package_content_valid_range(self):
request = fake_request.HTTPRequest.blank(
'/vnf_packages/%s/package_content/')
'/vnf_packages/%s/package_content/'
% constants.UUID)
request.headers["Range"] = 'bytes=10-99'
range_ = self.controller._get_range_from_request(request, 120)
self.assertEqual(10, range_.start)
@ -1000,7 +1001,8 @@ class TestController(base.TestCase):
def test_fetch_vnf_package_content_invalid_range(self):
request = fake_request.HTTPRequest.blank(
'/vnf_packages/%s/package_content/')
'/vnf_packages/%s/package_content/'
% constants.UUID)
request.headers["Range"] = 'bytes=150-'
self.assertRaises(exc.HTTPRequestRangeNotSatisfiable,
self.controller._get_range_from_request,
@ -1008,12 +1010,96 @@ class TestController(base.TestCase):
def test_fetch_vnf_package_content_invalid_multiple_range(self):
request = fake_request.HTTPRequest.blank(
'/vnf_packages/%s/package_content/')
'/vnf_packages/%s/package_content/'
% constants.UUID)
request.headers["Range"] = 'bytes=10-20,21-30'
self.assertRaises(exc.HTTPBadRequest,
self.controller._get_range_from_request, request,
120)
@mock.patch.object(controller.VnfPkgmController, "_download")
@mock.patch.object(controller.VnfPkgmController, "_get_range_from_request")
@mock.patch.object(glance_store, 'get_csar_size')
@mock.patch.object(controller.VnfPkgmController, '_get_vnf_package')
@mock.patch.object(vnf_package.VnfPackage, 'save')
def test_fetch_vnf_package_content(
self,
mock_save,
mock_get_vnf_package,
mock_get_csar_size,
mock_get_range,
mock_download):
request = fake_request.HTTPRequest.blank(
'/vnf_packages/%s/package_content/'
% constants.UUID)
request.headers["Range"] = 'bytes=10-20,21-30'
request.response = ""
mock_get_vnf_package.return_value = fakes.return_vnfpkg_obj()
mock_get_csar_size.return_value = 1000
mock_get_range.return_value = "10-20, 21-30"
mock_download.return_value = "Response"
id = constants.UUID
result = self.controller.fetch_vnf_package_content(request, id)
self.assertEqual(result, "Response")
mock_get_csar_size.assert_called_once_with(
id, fakes.return_vnfpkg_obj().location_glance_store)
mock_get_vnf_package.assert_called_once_with(
id, request)
mock_get_range.assert_called_once_with(
request, 1000)
@mock.patch.object(controller.VnfPkgmController, '_get_vnf_package')
def test_fetch_vnf_package_content_invalid_onboarding(
self, mock_get):
request = fake_request.HTTPRequest.blank(
'/vnf_packages/%s/package_content/'
% constants.UUID)
request.headers["Range"] = 'bytes=10-20,21-30'
request.response = ""
pkgobj = fakes.return_vnfpkg_obj()
pkgobj.onboarding_state = fields.PackageOnboardingStateType.PROCESSING
mock_get.return_value = pkgobj
id = constants.UUID
self.assertRaises(exc.HTTPConflict,
self.controller.fetch_vnf_package_content, request,
id)
@mock.patch.object(controller.VnfPkgmController, '_get_vnf_package')
def test_fetch_vnf_package_content_not_present(self, mock_get):
request = fake_request.HTTPRequest.blank(
'/vnf_packages/%s/package_content/'
% constants.UUID)
request.headers["Range"] = 'bytes=10-20,21-30'
request.response = ""
pkgobj = fakes.return_vnfpkg_obj()
mock_get.return_value = pkgobj
id = constants.UUID
self.assertRaises(exc.HTTPNotFound,
self.controller.fetch_vnf_package_content, request,
id)
@mock.patch.object(controller.VnfPkgmController, "_download")
@mock.patch.object(controller.VnfPkgmController, "_get_range_from_request")
@mock.patch.object(controller.VnfPkgmController, '_get_vnf_package')
def test_fetch_vnf_package_content_with_size(
self,
mock_get,
mock_get_range,
mock_download):
request = fake_request.HTTPRequest.blank(
'/vnf_packages/%s/package_content/'
% constants.UUID)
request.headers["Range"] = 'bytes=10-20,21-30'
request.response = ""
pkgobj = fakes.return_vnfpkg_obj()
pkgobj.size = 1000
mock_get_range.return_value = "10-20, 21-30"
mock_download.return_value = 1000
mock_get.return_value = pkgobj
id = constants.UUID
result = self.controller.fetch_vnf_package_content(request, id)
self.assertEqual(result, 1000)
def test_fetch_vnf_package_artifacts_with_invalid_uuid(
self):
# invalid_uuid


Loading…
Cancel
Save