Browse Source

FIX other issues for Python 2/3 compatible code

change func.func_name to func.__name__
change itertools.izip_longest to six.moves.zip_longest
change self.assertRaisesRegexp(xxx) to six.assertRaisesRegex(self,xx)
use absolute import path

Story: 2004585
Task: 28447

Change-Id: I2b4814fb6f96feb90ad0a081ffcab5c72160b0f7
Signed-off-by: Sun Austin <austin.sun@intel.com>
Sun Austin 3 months ago
parent
commit
6022c0420d

+ 2
- 1
dcmanager/common/utils.py View File

@@ -21,6 +21,7 @@
21 21
 #
22 22
 
23 23
 import itertools
24
+import six.moves
24 25
 
25 26
 from dcmanager.common import consts
26 27
 from dcmanager.common import exceptions
@@ -35,7 +36,7 @@ def get_import_path(cls):
35 36
 # Returns a iterator of tuples containing batch_size number of objects in each
36 37
 def get_batch_projects(batch_size, project_list, fillvalue=None):
37 38
     args = [iter(project_list)] * batch_size
38
-    return itertools.izip_longest(fillvalue=fillvalue, *args)
39
+    return six.moves.zip_longest(fillvalue=fillvalue, *args)
39 40
 
40 41
 
41 42
 # to do validate the quota limits

+ 43
- 42
dcmanager/tests/unit/api/v1/controllers/test_subclouds.py View File

@@ -22,6 +22,7 @@
22 22
 
23 23
 import copy
24 24
 import mock
25
+import six
25 26
 import webtest
26 27
 
27 28
 from dcmanager.api.controllers.v1 import subclouds
@@ -96,9 +97,9 @@ class TestSubclouds(testroot.DCManagerApiTest):
96 97
                                                   '192.168.204.2',
97 98
                                                   '192.168.204.100')
98 99
         mock_get_management_address_pool.return_value = management_address_pool
99
-        self.assertRaisesRegexp(webtest.app.AppError, "400 *",
100
-                                self.app.post_json, FAKE_URL,
101
-                                headers=FAKE_HEADERS, params=data)
100
+        six.assertRaisesRegex(self, webtest.app.AppError, "400 *",
101
+                              self.app.post_json, FAKE_URL,
102
+                              headers=FAKE_HEADERS, params=data)
102 103
 
103 104
     @mock.patch.object(subclouds.SubcloudsController,
104 105
                        '_get_management_address_pool')
@@ -111,18 +112,18 @@ class TestSubclouds(testroot.DCManagerApiTest):
111 112
                                                   '192.168.204.2',
112 113
                                                   '192.168.204.254')
113 114
         mock_get_management_address_pool.return_value = management_address_pool
114
-        self.assertRaisesRegexp(webtest.app.AppError, "400 *",
115
-                                self.app.post_json, FAKE_URL,
116
-                                headers=FAKE_HEADERS, params=data)
115
+        six.assertRaisesRegex(self, webtest.app.AppError, "400 *",
116
+                              self.app.post_json, FAKE_URL,
117
+                              headers=FAKE_HEADERS, params=data)
117 118
 
118 119
     @mock.patch.object(rpc_client, 'ManagerClient')
119 120
     @mock.patch.object(subclouds, 'db_api')
120 121
     def test_post_subcloud_bad_subnet(self, mock_db_api, mock_rpc_client):
121 122
         data = copy.copy(FAKE_SUBCLOUD_DATA)
122 123
         data["management-subnet"] = "192.168.101.0/32"
123
-        self.assertRaisesRegexp(webtest.app.AppError, "400 *",
124
-                                self.app.post_json, FAKE_URL,
125
-                                headers=FAKE_HEADERS, params=data)
124
+        six.assertRaisesRegex(self, webtest.app.AppError, "400 *",
125
+                              self.app.post_json, FAKE_URL,
126
+                              headers=FAKE_HEADERS, params=data)
126 127
 
127 128
     @mock.patch.object(rpc_client, 'ManagerClient')
128 129
     @mock.patch.object(subclouds, 'db_api')
@@ -131,9 +132,9 @@ class TestSubclouds(testroot.DCManagerApiTest):
131 132
         data["management-subnet"] = "192.168.101.0/24"
132 133
         data["management-start-ip"] = "192.168.100.2"
133 134
         data["management-end-ip"] = "192.168.100.50"
134
-        self.assertRaisesRegexp(webtest.app.AppError, "400 *",
135
-                                self.app.post_json, FAKE_URL,
136
-                                headers=FAKE_HEADERS, params=data)
135
+        six.assertRaisesRegex(self, webtest.app.AppError, "400 *",
136
+                              self.app.post_json, FAKE_URL,
137
+                              headers=FAKE_HEADERS, params=data)
137 138
 
138 139
     @mock.patch.object(rpc_client, 'ManagerClient')
139 140
     @mock.patch.object(subclouds, 'db_api')
@@ -141,9 +142,9 @@ class TestSubclouds(testroot.DCManagerApiTest):
141 142
         data = copy.copy(FAKE_SUBCLOUD_DATA)
142 143
         data["management-start-ip"] = "192.168.101.2"
143 144
         data["management-end-ip"] = "192.168.100.100"
144
-        self.assertRaisesRegexp(webtest.app.AppError, "400 *",
145
-                                self.app.post_json, FAKE_URL,
146
-                                headers=FAKE_HEADERS, params=data)
145
+        six.assertRaisesRegex(self, webtest.app.AppError, "400 *",
146
+                              self.app.post_json, FAKE_URL,
147
+                              headers=FAKE_HEADERS, params=data)
147 148
 
148 149
     @mock.patch.object(rpc_client, 'ManagerClient')
149 150
     @mock.patch.object(subclouds, 'db_api')
@@ -151,9 +152,9 @@ class TestSubclouds(testroot.DCManagerApiTest):
151 152
         data = copy.copy(FAKE_SUBCLOUD_DATA)
152 153
         data["management-start-ip"] = "192.168.101.2"
153 154
         data["management-end-ip"] = "192.168.101.4"
154
-        self.assertRaisesRegexp(webtest.app.AppError, "400 *",
155
-                                self.app.post_json, FAKE_URL,
156
-                                headers=FAKE_HEADERS, params=data)
155
+        six.assertRaisesRegex(self, webtest.app.AppError, "400 *",
156
+                              self.app.post_json, FAKE_URL,
157
+                              headers=FAKE_HEADERS, params=data)
157 158
 
158 159
     @mock.patch.object(rpc_client, 'ManagerClient')
159 160
     @mock.patch.object(subclouds, 'db_api')
@@ -161,31 +162,31 @@ class TestSubclouds(testroot.DCManagerApiTest):
161 162
         data = copy.copy(FAKE_SUBCLOUD_DATA)
162 163
         data["management-start-ip"] = "192.168.101.20"
163 164
         data["management-end-ip"] = "192.168.101.4"
164
-        self.assertRaisesRegexp(webtest.app.AppError, "400 *",
165
-                                self.app.post_json, FAKE_URL,
166
-                                headers=FAKE_HEADERS, params=data)
165
+        six.assertRaisesRegex(self, webtest.app.AppError, "400 *",
166
+                              self.app.post_json, FAKE_URL,
167
+                              headers=FAKE_HEADERS, params=data)
167 168
 
168 169
     @mock.patch.object(rpc_client, 'ManagerClient')
169 170
     def test_post_subcloud_wrong_url(self, mock_rpc_client):
170 171
         data = FAKE_SUBCLOUD_DATA
171
-        self.assertRaisesRegexp(webtest.app.AppError, "404 *",
172
-                                self.app.post_json, WRONG_URL,
173
-                                headers=FAKE_HEADERS, params=data)
172
+        six.assertRaisesRegex(self, webtest.app.AppError, "404 *",
173
+                              self.app.post_json, WRONG_URL,
174
+                              headers=FAKE_HEADERS, params=data)
174 175
 
175 176
     @mock.patch.object(rpc_client, 'ManagerClient')
176 177
     def test_post_no_body(self, mock_rpc_client):
177 178
         data = {}
178
-        self.assertRaisesRegexp(webtest.app.AppError, "400 *",
179
-                                self.app.post_json, FAKE_URL,
180
-                                headers=FAKE_HEADERS, params=data)
179
+        six.assertRaisesRegex(self, webtest.app.AppError, "400 *",
180
+                              self.app.post_json, FAKE_URL,
181
+                              headers=FAKE_HEADERS, params=data)
181 182
 
182 183
     @mock.patch.object(rpc_client, 'ManagerClient')
183 184
     def test_post_no_name(self, mock_rpc_client):
184 185
         data = dict(FAKE_SUBCLOUD_DATA)
185 186
         del data['name']
186
-        self.assertRaisesRegexp(webtest.app.AppError, "400 *",
187
-                                self.app.post_json, FAKE_URL,
188
-                                headers=FAKE_HEADERS, params=data)
187
+        six.assertRaisesRegex(self, webtest.app.AppError, "400 *",
188
+                              self.app.post_json, FAKE_URL,
189
+                              headers=FAKE_HEADERS, params=data)
189 190
 
190 191
     @mock.patch.object(subclouds.SubcloudsController,
191 192
                        '_create_subcloud_config_file')
@@ -212,9 +213,9 @@ class TestSubclouds(testroot.DCManagerApiTest):
212 213
     @mock.patch.object(rpc_client, 'ManagerClient')
213 214
     def test_delete_wrong_request(self, mock_rpc_client):
214 215
         delete_url = WRONG_URL + '/' + FAKE_ID
215
-        self.assertRaisesRegex(webtest.app.AppError, "404 *",
216
-                               self.app.delete_json, delete_url,
217
-                               headers=FAKE_HEADERS)
216
+        six.assertRaisesRegex(self, webtest.app.AppError, "404 *",
217
+                              self.app.delete_json, delete_url,
218
+                              headers=FAKE_HEADERS)
218 219
 
219 220
     @mock.patch.object(rpc_client, 'ManagerClient')
220 221
     @mock.patch.object(subclouds, 'db_api')
@@ -227,9 +228,9 @@ class TestSubclouds(testroot.DCManagerApiTest):
227 228
     @mock.patch.object(subclouds, 'db_api')
228 229
     def test_get_wrong_request(self, mock_db_api, mock_rpc_client):
229 230
         get_url = WRONG_URL + '/' + FAKE_ID
230
-        self.assertRaisesRegex(webtest.app.AppError, "404 *",
231
-                               self.app.get, get_url,
232
-                               headers=FAKE_HEADERS)
231
+        six.assertRaisesRegex(self, webtest.app.AppError, "404 *",
232
+                              self.app.get, get_url,
233
+                              headers=FAKE_HEADERS)
233 234
 
234 235
     @mock.patch.object(rpc_client, 'ManagerClient')
235 236
     @mock.patch.object(subclouds, 'db_api')
@@ -258,14 +259,14 @@ class TestSubclouds(testroot.DCManagerApiTest):
258 259
     @mock.patch.object(rpc_client, 'ManagerClient')
259 260
     def test_patch_subcloud_no_body(self, mock_rpc_client):
260 261
         data = {}
261
-        self.assertRaisesRegexp(webtest.app.AppError, "400 *",
262
-                                self.app.patch_json, FAKE_URL + '/' + FAKE_ID,
263
-                                headers=FAKE_HEADERS, params=data)
262
+        six.assertRaisesRegex(self, webtest.app.AppError, "400 *",
263
+                              self.app.patch_json, FAKE_URL + '/' + FAKE_ID,
264
+                              headers=FAKE_HEADERS, params=data)
264 265
 
265 266
     @mock.patch.object(rpc_client, 'ManagerClient')
266 267
     @mock.patch.object(subclouds, 'db_api')
267 268
     def test_patch_subcloud_bad_status(self, mock_db_api, mock_rpc_client):
268 269
         data = {'management-state': 'bad-status'}
269
-        self.assertRaisesRegex(webtest.app.AppError, "400 *",
270
-                               self.app.patch_json, FAKE_URL + '/' + FAKE_ID,
271
-                               headers=FAKE_HEADERS, params=data)
270
+        six.assertRaisesRegex(self, webtest.app.AppError, "400 *",
271
+                              self.app.patch_json, FAKE_URL + '/' + FAKE_ID,
272
+                              headers=FAKE_HEADERS, params=data)

+ 19
- 18
dcmanager/tests/unit/api/v1/controllers/test_sw_update_strategy.py View File

@@ -22,6 +22,7 @@
22 22
 
23 23
 import copy
24 24
 import mock
25
+import six
25 26
 import webtest
26 27
 
27 28
 from dcmanager.api.controllers.v1 import sw_update_strategy
@@ -75,18 +76,18 @@ class TestSwUpdateStrategy(testroot.DCManagerApiTest):
75 76
     def test_post_sw_update_bad_type(self, mock_db_api, mock_rpc_client):
76 77
         data = copy.copy(FAKE_SW_UPDATE_DATA)
77 78
         data["type"] = "bad type"
78
-        self.assertRaisesRegexp(webtest.app.AppError, "400 *",
79
-                                self.app.post_json, FAKE_URL,
80
-                                headers=FAKE_HEADERS, params=data)
79
+        six.assertRaisesRegex(self, webtest.app.AppError, "400 *",
80
+                              self.app.post_json, FAKE_URL,
81
+                              headers=FAKE_HEADERS, params=data)
81 82
 
82 83
     @mock.patch.object(rpc_client, 'ManagerClient')
83 84
     @mock.patch.object(sw_update_strategy, 'db_api')
84 85
     def test_post_sw_update_bad_apply_type(self, mock_db_api, mock_rpc_client):
85 86
         data = copy.copy(FAKE_SW_UPDATE_DATA)
86 87
         data["subcloud-apply-type"] = "bad type"
87
-        self.assertRaisesRegexp(webtest.app.AppError, "400 *",
88
-                                self.app.post_json, FAKE_URL,
89
-                                headers=FAKE_HEADERS, params=data)
88
+        six.assertRaisesRegex(self, webtest.app.AppError, "400 *",
89
+                              self.app.post_json, FAKE_URL,
90
+                              headers=FAKE_HEADERS, params=data)
90 91
 
91 92
     @mock.patch.object(rpc_client, 'ManagerClient')
92 93
     @mock.patch.object(sw_update_strategy, 'db_api')
@@ -94,24 +95,24 @@ class TestSwUpdateStrategy(testroot.DCManagerApiTest):
94 95
             self, mock_db_api, mock_rpc_client):
95 96
         data = copy.copy(FAKE_SW_UPDATE_DATA)
96 97
         data["max-parallel-subclouds"] = "not an integer"
97
-        self.assertRaisesRegexp(webtest.app.AppError, "400 *",
98
-                                self.app.post_json, FAKE_URL,
99
-                                headers=FAKE_HEADERS, params=data)
98
+        six.assertRaisesRegex(self, webtest.app.AppError, "400 *",
99
+                              self.app.post_json, FAKE_URL,
100
+                              headers=FAKE_HEADERS, params=data)
100 101
 
101 102
     @mock.patch.object(rpc_client, 'ManagerClient')
102 103
     def test_post_no_body(self, mock_rpc_client):
103 104
         data = {}
104
-        self.assertRaisesRegexp(webtest.app.AppError, "400 *",
105
-                                self.app.post_json, FAKE_URL,
106
-                                headers=FAKE_HEADERS, params=data)
105
+        six.assertRaisesRegex(self, webtest.app.AppError, "400 *",
106
+                              self.app.post_json, FAKE_URL,
107
+                              headers=FAKE_HEADERS, params=data)
107 108
 
108 109
     @mock.patch.object(rpc_client, 'ManagerClient')
109 110
     def test_post_no_type(self, mock_rpc_client):
110 111
         data = copy.copy(FAKE_SW_UPDATE_DATA)
111 112
         del data['type']
112
-        self.assertRaisesRegexp(webtest.app.AppError, "400 *",
113
-                                self.app.post_json, FAKE_URL,
114
-                                headers=FAKE_HEADERS, params=data)
113
+        six.assertRaisesRegex(self, webtest.app.AppError, "400 *",
114
+                              self.app.post_json, FAKE_URL,
115
+                              headers=FAKE_HEADERS, params=data)
115 116
 
116 117
     @mock.patch.object(rpc_client, 'ManagerClient')
117 118
     @mock.patch.object(sw_update_strategy, 'db_api')
@@ -140,9 +141,9 @@ class TestSwUpdateStrategy(testroot.DCManagerApiTest):
140 141
     def test_post_sw_update_bad_action(self, mock_db_api, mock_rpc_client):
141 142
         data = copy.copy(FAKE_SW_UPDATE_APPLY_DATA)
142 143
         data["action"] = "bad action"
143
-        self.assertRaisesRegexp(webtest.app.AppError, "400 *",
144
-                                self.app.post_json, FAKE_URL,
145
-                                headers=FAKE_HEADERS, params=data)
144
+        six.assertRaisesRegex(self, webtest.app.AppError, "400 *",
145
+                              self.app.post_json, FAKE_URL,
146
+                              headers=FAKE_HEADERS, params=data)
146 147
 
147 148
     @mock.patch.object(rpc_client, 'ManagerClient')
148 149
     @mock.patch.object(sw_update_strategy, 'db_api')

+ 2
- 1
dcorch/common/utils.py View File

@@ -14,6 +14,7 @@
14 14
 # limitations under the License.
15 15
 
16 16
 import itertools
17
+import six.moves
17 18
 
18 19
 from dcorch.common import consts
19 20
 from dcorch.common import exceptions
@@ -34,7 +35,7 @@ def get_import_path(cls):
34 35
 # Returns a iterator of tuples containing batch_size number of objects in each
35 36
 def get_batch_projects(batch_size, project_list, fillvalue=None):
36 37
     args = [iter(project_list)] * batch_size
37
-    return itertools.izip_longest(fillvalue=fillvalue, *args)
38
+    return six.moves.zip_longest(fillvalue=fillvalue, *args)
38 39
 
39 40
 
40 41
 # to do validate the quota limits

+ 1
- 1
dcorch/engine/sync_thread.py View File

@@ -220,7 +220,7 @@ class SyncThread(object):
220 220
                                            sync_request.orch_job.resource_id)
221 221
         handler = self.sync_handler_map[rsrc.resource_type]
222 222
         LOG.info("Invoking {} for {} [{}]".format(
223
-            handler.func_name, rsrc.resource_type,
223
+            handler.__name__, rsrc.resource_type,
224 224
             sync_request.orch_job.operation_type), extra=self.log_extra)
225 225
         handler(sync_request, rsrc)
226 226
 

Loading…
Cancel
Save