summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjiaopengju <jiaopengju@cmss.chinamobile.com>2018-11-23 10:46:34 +0800
committerjiaopengju <jiaopengju@cmss.chinamobile.com>2018-11-23 10:46:34 +0800
commit31eab8eb6ce40d2d57034bdb4992a545c34dcb08 (patch)
tree4aa31f44479a322e382141bc761dbaae009582ba
parentb6342feeb6b8f7f8984f10962c3286416100ade8 (diff)
Add unit test for copies API
Notes
Notes (review): Code-Review+2: Jiao Pengju <jiaopengju@cmss.chinamobile.com> Workflow+1: Jiao Pengju <jiaopengju@cmss.chinamobile.com> Verified+2: Zuul Submitted-by: Zuul Submitted-at: Fri, 23 Nov 2018 05:49:01 +0000 Reviewed-on: https://review.openstack.org/619658 Project: openstack/karbor Branch: refs/heads/master
-rw-r--r--karbor/tests/unit/api/v1/test_copies.py122
1 files changed, 122 insertions, 0 deletions
diff --git a/karbor/tests/unit/api/v1/test_copies.py b/karbor/tests/unit/api/v1/test_copies.py
new file mode 100644
index 0000000..a8fed27
--- /dev/null
+++ b/karbor/tests/unit/api/v1/test_copies.py
@@ -0,0 +1,122 @@
1# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12from mock import mock
13
14from karbor.api.v1 import copies
15from karbor import context
16from karbor import exception
17from karbor.tests import base
18from karbor.tests.unit.api import fakes
19from webob import exc
20
21PROVIDER_ID_1 = 'efc6a88b-9096-4bb6-8634-cda182a6e12a'
22PROVIDER_ID_2 = '3241a88b-9096-4bb6-8634-cda182a6e12a'
23DEFAULT_PROJECT_ID = '39bb894794b741e982bd26144d2949f6'
24DEFAULT_PLAN_ID = '603b894794b741e982bd26144d2949f6'
25
26
27class FakePlan(object):
28 def __init__(self, values):
29 self.id = values.get('id')
30 self.provider_id = values.get('provider_id')
31 self.parameters = values.get('parameters')
32
33
34class CopiesApiTest(base.TestCase):
35
36 def setUp(self):
37 super(CopiesApiTest, self).setUp()
38 self.controller = copies.CopiesController()
39 self.ctxt = context.RequestContext('demo',
40 DEFAULT_PROJECT_ID, True)
41
42 @mock.patch('karbor.objects.Plan.get_by_id')
43 @mock.patch('karbor.services.protection.api.API.list_checkpoints')
44 @mock.patch('karbor.services.protection.api.API.copy')
45 def test_copies_create(self, mock_copy,
46 mock_list_checkpoints, mock_plan_get):
47 mock_plan_get.return_value = FakePlan(
48 {'id': DEFAULT_PLAN_ID,
49 'provider_id': PROVIDER_ID_1,
50 'parameters': {}})
51 mock_list_checkpoints.return_value = ['fake_checkpoint_id']
52 copy = self._copy_in_request_body(DEFAULT_PLAN_ID, {})
53 body = {"copy": copy}
54 req = fakes.HTTPRequest.blank('/v1/copies')
55 self.controller.create(req, PROVIDER_ID_1, body=body)
56 self.assertEqual(True, mock_copy.called)
57
58 def test_copies_create_with_invalid_provider_id(self):
59 copy = self._copy_in_request_body(DEFAULT_PLAN_ID, {})
60 body = {"copy": copy}
61 req = fakes.HTTPRequest.blank('/v1/copies')
62 self.assertRaises(exception.InvalidInput, self.controller.create, req,
63 'fake_invalid_provider_id', body=body)
64
65 @mock.patch('karbor.objects.Plan.get_by_id')
66 def test_copies_create_with_invalid_plan(self, mock_plan_get):
67 mock_plan_get.side_effect = exception.PlanNotFound
68 copy = self._copy_in_request_body(DEFAULT_PLAN_ID, {})
69 body = {"copy": copy}
70 req = fakes.HTTPRequest.blank('/v1/copies')
71 self.assertRaises(exc.HTTPNotFound, self.controller.create, req,
72 PROVIDER_ID_1, body=body)
73
74 @mock.patch('karbor.objects.Plan.get_by_id')
75 def test_copies_create_with_different_provider_id(self, mock_plan_get):
76 mock_plan_get.return_value = FakePlan(
77 {'id': DEFAULT_PLAN_ID,
78 'provider_id': PROVIDER_ID_2,
79 'parameters': {}})
80 copy = self._copy_in_request_body(DEFAULT_PLAN_ID, {})
81 body = {"copy": copy}
82 req = fakes.HTTPRequest.blank('/v1/copies')
83 self.assertRaises(exception.InvalidInput, self.controller.create, req,
84 PROVIDER_ID_1, body=body)
85
86 @mock.patch('karbor.objects.Plan.get_by_id')
87 @mock.patch('karbor.services.protection.api.API.list_checkpoints')
88 def test_copies_create_with_no_checkpoints_exist(
89 self, mock_list_checkpoints, mock_plan_get):
90 mock_plan_get.return_value = FakePlan(
91 {'id': DEFAULT_PLAN_ID,
92 'provider_id': PROVIDER_ID_1,
93 'parameters': {}})
94 mock_list_checkpoints.return_value = []
95 copy = self._copy_in_request_body(DEFAULT_PLAN_ID, {})
96 body = {"copy": copy}
97 req = fakes.HTTPRequest.blank('/v1/copies')
98 self.assertRaises(exception.InvalidInput, self.controller.create, req,
99 PROVIDER_ID_1, body=body)
100
101 @mock.patch('karbor.objects.Plan.get_by_id')
102 @mock.patch('karbor.services.protection.api.API.list_checkpoints')
103 @mock.patch('karbor.services.protection.api.API.copy')
104 def test_copies_create_with_protection_copy_failed(
105 self, mock_copy, mock_list_checkpoints, mock_plan_get):
106 mock_plan_get.return_value = FakePlan(
107 {'id': DEFAULT_PLAN_ID,
108 'provider_id': PROVIDER_ID_1,
109 'parameters': {}})
110 mock_list_checkpoints.return_value = ['fake_checkpoint_id']
111 mock_copy.side_effect = exception.FlowError
112 copy = self._copy_in_request_body(DEFAULT_PLAN_ID, {})
113 body = {"copy": copy}
114 req = fakes.HTTPRequest.blank('/v1/copies')
115 self.assertRaises(exception.FlowError, self.controller.create,
116 req, PROVIDER_ID_1, body=body)
117
118 def _copy_in_request_body(self, plan_id, parameters):
119 return {
120 'plan_id': plan_id,
121 'parameters': parameters
122 }