Improved pool move test coverage

Change-Id: I59926b701e375b01dd46fc0fad0ce2db6dd73fe7
This commit is contained in:
Erik Olof Gunnar Andersson 2023-12-16 06:08:22 -08:00
parent 57f2e367bf
commit 571d902c59
4 changed files with 95 additions and 5 deletions

View File

@ -46,16 +46,18 @@ class PoolMoveController(rest.RestController):
if 'pool_id' in body:
if zone.pool_id == body['pool_id']:
raise exceptions.BadRequest(
'Target pool must be different for zone pool move')
'Target pool must be different for zone pool move'
)
target_pool_id = body['pool_id']
# Update the zone object with the new values
zone = DesignateAdapter.parse('API_v2', body, zone)
zone.validate()
LOG.info("Triggered pool move for %(zone)s", {'zone': zone})
LOG.info('Triggered pool move for %(zone)s', {'zone': zone})
zone = self.central_api.pool_move_zone(
context, zone_id, target_pool_id)
context, zone_id, target_pool_id
)
if zone.status == 'PENDING':
response.status_int = 202
else:

View File

@ -4245,11 +4245,17 @@ class CentralServiceTest(designate.tests.functional.TestCase):
def test_pool_move_zone(self):
pool = self.create_pool(fixture=0)
zone = self.create_zone(context=self.admin_context, pool_id=pool.id)
self.storage.create_pool_ns_record(
self.admin_context, pool['id'],
objects.PoolNsRecord(priority=1, hostname='ns-old.example.org.')
)
# create second pool
second_pool = self.create_pool(fixture=1)
new_ns_record = objects.PoolNsRecord(hostname='ns-new.example.org.')
second_pool.ns_records.append(new_ns_record)
self.storage.create_pool_ns_record(
self.admin_context, second_pool['id'],
objects.PoolNsRecord(priority=1, hostname='ns-new.example.org.')
)
moved_zone = self.central_service.pool_move_zone(
self.admin_context,
@ -4257,6 +4263,18 @@ class CentralServiceTest(designate.tests.functional.TestCase):
self.assertEqual(zone.id, moved_zone.id)
self.assertEqual(moved_zone.pool_id, second_pool['id'])
def test_pool_move_zone_no_valid_pool_selected(self):
pool_id = '794ccc2c-d751-44fe-b57f-8894c9f5c842'
zone = self.create_zone(fixture=0, pool_id=pool_id)
with mock.patch.object(self.central_service.scheduler, 'schedule_zone',
return_value=pool_id):
exc = self.assertRaises(rpc_dispatcher.ExpectedException,
self.central_service.pool_move_zone,
self.admin_context, zone.id)
self.assertEqual(exceptions.BadRequest, exc.exc_info[0])
def test_pool_move_zone_without_target_pool(self):
pool = self.create_pool(fixture=0)
zone = self.create_zone(context=self.admin_context, pool_id=pool.id)

View File

View File

@ -0,0 +1,70 @@
# 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
import oslotest.base
from designate.api.v2.controllers.zones.tasks import pool_move
from designate.central import rpcapi
from designate import exceptions
from designate import objects
class TestPoolMoveAPI(oslotest.base.BaseTestCase):
def setUp(self):
super().setUp()
self.central_api = mock.Mock()
self.zone = objects.Zone(
id='c2c6381f-77f6-4e63-a63d-fda0fc22b0b2',
pool_id='bddce92d-5ca5-40e0-b787-c1ba9884278f',
name='example.com.',
email='example@example.com'
)
mock.patch.object(rpcapi.CentralAPI, 'get_instance',
return_value=self.central_api).start()
self.controller = pool_move.PoolMoveController()
@mock.patch('pecan.response', mock.Mock())
@mock.patch('pecan.request')
def test_post_all_target_pool_not_different(self, mock_request):
mock_request.environ = {'context': mock.Mock()}
mock_request.body_dict = {
'pool_id': 'bddce92d-5ca5-40e0-b787-c1ba9884278f'
}
mock_pool_move = mock.Mock()
mock_pool_move.status = 'ERROR'
self.central_api.get_zone.return_value = self.zone
self.central_api.pool_move_zone.return_value = mock_pool_move
self.assertRaisesRegex(
exceptions.BadRequest,
'Target pool must be different for zone pool move',
self.controller.post_all, self.zone.id
)
@mock.patch('pecan.response')
@mock.patch('pecan.request')
def test_post_all_move_error(self, mock_request, mock_response):
mock_request.environ = {'context': mock.Mock()}
mock_pool_move = mock.Mock()
mock_pool_move.status = 'ERROR'
self.central_api.get_zone.return_value = self.zone
self.central_api.pool_move_zone.return_value = mock_pool_move
self.controller.post_all(self.zone.id)
self.assertEqual(500, mock_response.status_int)