Skip to content

Commit

Permalink
Add tests for coriolis.api.v1.replica_actions module
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristi1324 committed Dec 14, 2023
1 parent c999dfe commit 44d2f42
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions coriolis/tests/api/v1/test_replica_actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Copyright 2023 Cloudbase Solutions Srl
# All Rights Reserved.

from unittest import mock

from webob import exc

from coriolis.api.v1 import replica_actions
from coriolis.api.v1.views import replica_tasks_execution_view
from coriolis import exception
from coriolis.replicas import api
from coriolis.tests import test_base
from coriolis.tests import testutils


class ReplicaActionsControllerTestCase(test_base.CoriolisBaseTestCase):
"""Test suite for the Coriolis Replica Actions v1 API"""

def setUp(self):
super(ReplicaActionsControllerTestCase, self).setUp()
self.replica_actions = replica_actions.ReplicaActionsController()

@mock.patch.object(replica_tasks_execution_view, 'single')
@mock.patch.object(api.API, 'delete_disks')
def test_delete_disks(
self,
mock_delete_disks,
mock_single
):
mock_req = mock.Mock()
mock_context = mock.Mock()
mock_req.environ = {'coriolis.context': mock_context}
id = mock.sentinel.id
body = mock.sentinel.body

result = testutils.get_wrapped_function(
self.replica_actions._delete_disks)(
mock_req,
id,
body
)

self.assertEqual(
mock_single.return_value,
result
)

mock_context.can.assert_called_once_with(
"migration:replicas:delete_disks")
mock_delete_disks.assert_called_once_with(mock_context, id)
mock_single.assert_called_once_with(mock_delete_disks.return_value)

@mock.patch.object(replica_tasks_execution_view, 'single')
@mock.patch.object(api.API, 'delete_disks')
def test_delete_disks_not_found(
self,
mock_delete_disks,
mock_single
):
mock_req = mock.Mock()
mock_context = mock.Mock()
mock_req.environ = {'coriolis.context': mock_context}
id = mock.sentinel.id
body = mock.sentinel.body
mock_delete_disks.side_effect = exception.NotFound()

self.assertRaises(
exc.HTTPNotFound,
testutils.get_wrapped_function(self.replica_actions._delete_disks),
req=mock_req,
id=id,
body=body
)

mock_context.can.assert_called_once_with(
"migration:replicas:delete_disks")
mock_delete_disks.assert_called_once_with(mock_context, id)
mock_single.assert_not_called()

@mock.patch.object(replica_tasks_execution_view, 'single')
@mock.patch.object(api.API, 'delete_disks')
def test_delete_disks_invalid_parameter_value(
self,
mock_delete_disks,
mock_single
):
mock_req = mock.Mock()
mock_context = mock.Mock()
mock_req.environ = {'coriolis.context': mock_context}
id = mock.sentinel.id
body = mock.sentinel.body
mock_delete_disks.side_effect = exception.InvalidParameterValue("err")

self.assertRaises(
exc.HTTPNotFound,
testutils.get_wrapped_function(self.replica_actions._delete_disks),
req=mock_req,
id=id,
body=body
)

mock_context.can.assert_called_once_with(
"migration:replicas:delete_disks")
mock_delete_disks.assert_called_once_with(mock_context, id)
mock_single.assert_not_called()

0 comments on commit 44d2f42

Please sign in to comment.