Browse Source

Add unit test for copies API

Change-Id: I069a3483c1365461818c6e293e5b4e58cb68fa5b
jiaopengju 5 months ago
parent
commit
31eab8eb6c
1 changed files with 122 additions and 0 deletions
  1. 122
    0
      karbor/tests/unit/api/v1/test_copies.py

+ 122
- 0
karbor/tests/unit/api/v1/test_copies.py View File

@@ -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.
12
+from mock import mock
13
+
14
+from karbor.api.v1 import copies
15
+from karbor import context
16
+from karbor import exception
17
+from karbor.tests import base
18
+from karbor.tests.unit.api import fakes
19
+from webob import exc
20
+
21
+PROVIDER_ID_1 = 'efc6a88b-9096-4bb6-8634-cda182a6e12a'
22
+PROVIDER_ID_2 = '3241a88b-9096-4bb6-8634-cda182a6e12a'
23
+DEFAULT_PROJECT_ID = '39bb894794b741e982bd26144d2949f6'
24
+DEFAULT_PLAN_ID = '603b894794b741e982bd26144d2949f6'
25
+
26
+
27
+class 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
+
34
+class 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
+        }

Loading…
Cancel
Save