Skip to content

Commit

Permalink
Add tests for coriolis.api.v1.minion_pool_actions module
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristi1324 committed Dec 6, 2023
1 parent 0c2da2d commit 1cb5ca2
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 0 deletions.
2 changes: 2 additions & 0 deletions coriolis/tests/api/v1/data/minion_pool_exceptions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- NotFound
- InvalidParameterValue
208 changes: 208 additions & 0 deletions coriolis/tests/api/v1/test_minion_pool_actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
# Copyright 2023 Cloudbase Solutions Srl
# All Rights Reserved.

from unittest import mock

import ddt
from webob import exc

from coriolis.api.v1 import minion_pool_actions as minion
from coriolis.api.v1.views import minion_pool_view
from coriolis import exception
from coriolis.minion_pools import api
from coriolis.tests import test_base
from coriolis.tests import testutils


@ddt.ddt
class MinionPoolActionsControllerTestCase(test_base.CoriolisBaseTestCase):
"""Test suite for the Coriolis Minion Pool Actions v1 API"""

def setUp(self):
super(MinionPoolActionsControllerTestCase, self).setUp()
self.minion = minion.MinionPoolActionsController()

@mock.patch.object(minion_pool_view, 'single')
@mock.patch.object(api.API, 'allocate_minion_pool')
def test__allocate_pool(
self,
mock_allocate_minion_pool,
mock_single
):
mock_req = mock.Mock()
mock_context = mock.Mock()
mock_req.environ = {'coriolis.context': mock_context}
id = mock.sentinel.id
mock_body = {}

result = testutils.get_wrapped_function(
self.minion._allocate_pool)(
mock_req,
id,
mock_body
)

self.assertEqual(
mock_single.return_value,
result
)

mock_context.can.assert_called_once_with(
"migration:minion_pools:allocate")
mock_allocate_minion_pool.assert_called_once_with(mock_context, id)
mock_single.assert_called_once_with(
mock_allocate_minion_pool.return_value)

@mock.patch.object(minion_pool_view, 'single')
@mock.patch.object(api.API, 'allocate_minion_pool')
@ddt.file_data('data/minion_pool_exceptions.yml')
def test__allocate_pool_raises(
self,
exception_raised,
mock_allocate_minion_pool,
mock_single,
):
mock_req = mock.Mock()
mock_context = mock.Mock()
mock_req.environ = {'coriolis.context': mock_context}
id = mock.sentinel.id
mock_body = {}
expected_exception = getattr(exception, exception_raised)
mock_allocate_minion_pool.side_effect = expected_exception('err')

self.assertRaises(
exc.HTTPNotFound,
testutils.get_wrapped_function(self.minion._allocate_pool),
mock_req,
id,
mock_body
)

mock_context.can.assert_called_once_with(
"migration:minion_pools:allocate")
mock_allocate_minion_pool.assert_called_once_with(mock_context, id)
mock_single.assert_not_called()

@mock.patch.object(minion_pool_view, 'single')
@mock.patch.object(api.API, 'refresh_minion_pool')
def test__refresh_pool(
self,
mock_refresh_minion_pool,
mock_single
):
mock_req = mock.Mock()
mock_context = mock.Mock()
mock_req.environ = {'coriolis.context': mock_context}
id = mock.sentinel.id
mock_body = {}

result = testutils.get_wrapped_function(
self.minion._refresh_pool)(
mock_req,
id,
mock_body
)

self.assertEqual(
mock_single.return_value,
result
)

mock_context.can.assert_called_once_with(
"migration:minion_pools:refresh")
mock_refresh_minion_pool.assert_called_once_with(mock_context, id)
mock_single.assert_called_once_with(
mock_refresh_minion_pool.return_value)

@mock.patch.object(minion_pool_view, 'single')
@mock.patch.object(api.API, 'refresh_minion_pool')
@ddt.file_data('data/minion_pool_exceptions.yml')
def test__refresh_pool_raises(
self,
exception_raised,
mock_refresh_minion_pool,
mock_single,
):
mock_req = mock.Mock()
mock_context = mock.Mock()
mock_req.environ = {'coriolis.context': mock_context}
id = mock.sentinel.id
mock_body = {}
expected_exception = getattr(exception, exception_raised)
mock_refresh_minion_pool.side_effect = expected_exception('err')

self.assertRaises(
exc.HTTPNotFound,
testutils.get_wrapped_function(self.minion._refresh_pool),
mock_req,
id,
mock_body
)

mock_context.can.assert_called_once_with(
"migration:minion_pools:refresh")
mock_refresh_minion_pool.assert_called_once_with(mock_context, id)
mock_single.assert_not_called()

@mock.patch.object(minion_pool_view, 'single')
@mock.patch.object(api.API, 'deallocate_minion_pool')
def test__deallocate_pool(
self,
mock_deallocate_minion_pool,
mock_single
):
mock_req = mock.Mock()
mock_context = mock.Mock()
mock_req.environ = {'coriolis.context': mock_context}
id = mock.sentinel.id
mock_body = {'deallocate': {}}

result = testutils.get_wrapped_function(
self.minion._deallocate_pool)(
mock_req,
id,
mock_body
)

self.assertEqual(
mock_single.return_value,
result
)

mock_context.can.assert_called_once_with(
"migration:minion_pools:deallocate")
mock_deallocate_minion_pool.assert_called_once_with(
mock_context, id, force=False)
mock_single.assert_called_once_with(
mock_deallocate_minion_pool.return_value)

@mock.patch.object(minion_pool_view, 'single')
@mock.patch.object(api.API, 'deallocate_minion_pool')
@ddt.file_data('data/minion_pool_exceptions.yml')
def test__deallocate_pool_raises(
self,
exception_raised,
mock_deallocate_minion_pool,
mock_single,
):
mock_req = mock.Mock()
mock_context = mock.Mock()
mock_req.environ = {'coriolis.context': mock_context}
id = mock.sentinel.id
mock_body = {'deallocate': {}}
expected_exception = getattr(exception, exception_raised)
mock_deallocate_minion_pool.side_effect = expected_exception('err')

self.assertRaises(
exc.HTTPNotFound,
testutils.get_wrapped_function(self.minion._deallocate_pool),
mock_req,
id,
mock_body
)

mock_context.can.assert_called_once_with(
"migration:minion_pools:deallocate")
mock_deallocate_minion_pool.assert_called_once_with(
mock_context, id, force=False)
mock_single.assert_not_called()

0 comments on commit 1cb5ca2

Please sign in to comment.