summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgengchc2 <geng.changcai2@zte.com.cn>2018-12-26 16:47:35 -0800
committergengchc2 <geng.changcai2@zte.com.cn>2018-12-26 16:47:35 -0800
commit0f7c37c7765a2905bfad834f82d9ec982a2c0763 (patch)
treebd534aa97cdd3939e69fad5084010bbf9bfd2004
parent81198a782a3be1976acea80afc5b9626bd2fd93d (diff)
Add unit test for action_manager and session_manager
Notes
Notes (review): Code-Review+2: Carl Caihui <cai.hui@zte.com.cn> Workflow+1: Carl Caihui <cai.hui@zte.com.cn> Verified+2: Zuul Submitted-by: Zuul Submitted-at: Thu, 27 Dec 2018 10:32:43 +0000 Reviewed-on: https://review.openstack.org/627428 Project: openstack/freezer-api Branch: refs/heads/master
-rw-r--r--freezer_api/tests/unit/test_elasticv2.py358
1 files changed, 358 insertions, 0 deletions
diff --git a/freezer_api/tests/unit/test_elasticv2.py b/freezer_api/tests/unit/test_elasticv2.py
index 3203b73..4eb896e 100644
--- a/freezer_api/tests/unit/test_elasticv2.py
+++ b/freezer_api/tests/unit/test_elasticv2.py
@@ -1141,3 +1141,361 @@ class TestElasticSearchEngine_job(unittest.TestCase, ElasticSearchDB):
1141 job_id=common.fake_job_0_job_id, 1141 job_id=common.fake_job_0_job_id,
1142 doc=common.get_fake_job_0()) 1142 doc=common.get_fake_job_0())
1143 self.assertEqual(3, res) 1143 self.assertEqual(3, res)
1144
1145
1146class TestElasticSearchEngine_action(unittest.TestCase, ElasticSearchDB):
1147 @patch('freezer_api.storage.elasticv2.logging')
1148 @patch('freezer_api.storage.elasticv2.elasticsearch')
1149 def setUp(self, mock_elasticsearch, mock_logging):
1150 backend = 'elasticsearch'
1151 grp = cfg.OptGroup(backend)
1152 CONF.register_group(grp)
1153 CONF.register_opts(self._ES_OPTS, group=backend)
1154 mock_elasticsearch.Elasticsearch.return_value = mock.Mock()
1155 kwargs = {'hosts': 'http://elasticservaddr:1997'}
1156 self.eng = elastic.ElasticSearchEngineV2(backend=backend)
1157 self.eng.init(index='freezer', **kwargs)
1158 self.eng.action_manager = mock.Mock()
1159
1160 def test_get_action_userid_and_action_id_return_doc(self):
1161 self.eng.action_manager.get.return_value = common.get_fake_action_0()
1162 res = self.eng.get_action(project_id='tecs',
1163 user_id=common.fake_action_0['user_id'],
1164 action_id=common.fake_action_0['action_id'])
1165 self.assertEqual(common.fake_action_0, res)
1166 self.eng.action_manager.get.assert_called_with(
1167 project_id='tecs',
1168 user_id=common.fake_action_0['user_id'],
1169 doc_id=common.fake_action_0['action_id'])
1170
1171 def test_get_action_userid_and_action_id_return_none(self):
1172 self.eng.action_manager.get.return_value = None
1173 res = self.eng.get_action(project_id='tecs',
1174 user_id=common.fake_action_0['user_id'],
1175 action_id=common.fake_action_0['action_id'])
1176 self.assertIsNone(res)
1177 self.eng.action_manager.get.assert_called_with(
1178 project_id='tecs',
1179 user_id=common.fake_action_0['user_id'],
1180 doc_id=common.fake_action_0['action_id'])
1181
1182 def test_get_action_with_userid_and_search_return_list(self):
1183 self.eng.action_manager.search.return_value = \
1184 [common.fake_action_0, common.fake_action_0]
1185 my_search = {'match': [{'some_field': 'some text'},
1186 {'description': 'some other text'}]}
1187 res = self.eng.search_action(project_id='tecs',
1188 user_id=common.fake_action_0['user_id'],
1189 offset=6, limit=15,
1190 search=my_search)
1191 self.assertEqual([common.fake_action_0, common.fake_action_0], res)
1192 self.eng.action_manager.search.assert_called_with(
1193 project_id='tecs',
1194 user_id=common.fake_action_0['user_id'],
1195 search=my_search,
1196 limit=15, offset=6)
1197
1198 def test_get_action_with_userid_and_search_return_empty_list(self):
1199 self.eng.action_manager.search.return_value = []
1200 my_search = {'match': [{'some_field': 'some text'},
1201 {'description': 'some other text'}]}
1202 res = self.eng.search_action(project_id='tecs',
1203 user_id=common.fake_action_0['user_id'],
1204 offset=6, limit=15,
1205 search=my_search)
1206 self.assertEqual([], res)
1207 self.eng.action_manager.search.assert_called_with(
1208 project_id='tecs',
1209 user_id=common.fake_action_0['user_id'],
1210 search=my_search,
1211 limit=15, offset=6)
1212
1213 @patch('freezer_api.common.elasticv2_utils.ActionDoc')
1214 def test_add_action_ok(self, mock_actiondoc):
1215 mock_actiondoc.create.return_value = common.get_fake_action_0()
1216 self.eng.action_manager.insert.return_value = (True, 1)
1217 res = self.eng.add_action(project_id='tecs',
1218 user_id=common.fake_action_0['user_id'],
1219 doc=common.get_fake_action_0())
1220 self.assertEqual(common.fake_action_0['action_id'], res)
1221 self.eng.action_manager.insert.assert_called_with(
1222 common.fake_action_0, common.fake_action_0['action_id']
1223 )
1224
1225 def test_add_action_raises_StorageEngineError_when_manager_insert_raises(
1226 self):
1227 self.eng.action_manager.get.return_value = None
1228 self.eng.action_manager.insert.side_effect = (
1229 exceptions.StorageEngineError('regular test failure')
1230 )
1231 self.assertRaises(exceptions.StorageEngineError, self.eng.add_action,
1232 project_id='tecs',
1233 user_id=common.fake_action_0['user_id'],
1234 doc=common.get_fake_action_0())
1235
1236 def test_delete_action_ok(self):
1237 self.eng.action_manager.delete.return_value = common.fake_action_0[
1238 'action_id']
1239 res = self.eng.delete_action(
1240 project_id='tecs',
1241 user_id=common.fake_action_0['action_id'],
1242 action_id=common.fake_action_0['action_id'])
1243 self.assertEqual(common.fake_action_0['action_id'], res)
1244
1245 def test_delete_client_raises_StorageEngineError_when_es_delete_raises(
1246 self):
1247 self.eng.action_manager.delete.side_effect = (
1248 exceptions.StorageEngineError()
1249 )
1250 self.assertRaises(exceptions.StorageEngineError,
1251 self.eng.delete_action,
1252 project_id='tecs',
1253 user_id=common.fake_action_0['action_id'],
1254 action_id=common.fake_action_0['action_id'])
1255
1256 def test_update_action_raises_DocumentNotFound_when_doc_not_exists(self):
1257 self.eng.action_manager.get.side_effect = exceptions.DocumentNotFound(
1258 'regular test failure')
1259 patch = {'action_id': 'black_milk'}
1260 self.assertRaises(exceptions.DocumentNotFound, self.eng.update_action,
1261 project_id='tecs',
1262 user_id=common.fake_action_0['action_id'],
1263 action_id=common.fake_action_0['action_id'],
1264 patch_doc=patch)
1265
1266 def test_update_action_raises_DocumentNotFound_when_update_raises(
1267 self):
1268 self.eng.action_manager.get.return_value = common.get_fake_action_0()
1269 patch = {'action_id': 'black_milk'}
1270 self.eng.action_manager.update.side_effect = (
1271 exceptions.DocumentNotFound('regular test failure')
1272 )
1273 self.assertRaises(exceptions.DocumentNotFound, self.eng.update_action,
1274 project_id='tecs',
1275 user_id=common.fake_action_0['action_id'],
1276 action_id=common.fake_action_0['action_id'],
1277 patch_doc=patch)
1278
1279 def test_update_action_returns_new_doc_version(self):
1280 self.eng.action_manager.get.return_value = common.get_fake_action_0()
1281 patch = {'action_id': 'group_four'}
1282 self.eng.action_manager.update.return_value = 11
1283 res = self.eng.update_action(
1284 project_id='tecs',
1285 user_id=common.fake_action_0['action_id'],
1286 action_id=common.fake_action_0['action_id'],
1287 patch_doc=patch)
1288 self.assertEqual(11, res)
1289
1290 def test_replace_action_raises_AccessForbidden_when_action_manager_raises(
1291 self):
1292 self.eng.action_manager.get.side_effect = exceptions.AccessForbidden(
1293 'regular test failure')
1294 self.eng.action_manager.insert.return_value = (True, 3)
1295 self.assertRaises(exceptions.AccessForbidden, self.eng.replace_action,
1296 project_id='tecs',
1297 user_id=common.fake_action_0['action_id'],
1298 action_id=common.fake_action_0['action_id'],
1299 doc=common.get_fake_action_0())
1300
1301 # def test_replace_action_returns_ok_when_doc_is_new(self):
1302 # self.eng.action_manager.get.side_effect = (
1303 # exceptions.DocumentNotFound('regular test failure')
1304 # )
1305 # self.eng.action_manager.insert.return_value = (True, 1)
1306 # res = self.eng.replace_action(
1307 # user_id=common.fake_action_0['action_id'],
1308 # action_id=common.fake_action_0['action_id'],
1309 # doc=common.get_fake_action_0())
1310 # self.assertEqual(res, 1)
1311 #
1312 # def test_replace_action_returns_version_1_when_doc_is_overwritten(
1313 # self):
1314 # self.eng.action_manager.get.return_value = common.get_fake_action_0()
1315 # self.eng.action_manager.insert.return_value = (False, 3)
1316 # res = self.eng.replace_action(
1317 # user_id=common.fake_action_0['action_id'],
1318 # action_id=common.fake_action_0['action_id'],
1319 # doc=common.get_fake_action_0())
1320 # self.assertEqual(res, 3)
1321
1322
1323class TestElasticSearchEngine_session(unittest.TestCase, ElasticSearchDB):
1324 @patch('freezer_api.storage.elasticv2.logging')
1325 @patch('freezer_api.storage.elasticv2.elasticsearch')
1326 def setUp(self, mock_elasticsearch, mock_logging):
1327 backend = 'elasticsearch'
1328 grp = cfg.OptGroup(backend)
1329 CONF.register_group(grp)
1330 CONF.register_opts(self._ES_OPTS, group=backend)
1331 mock_elasticsearch.Elasticsearch.return_value = mock.Mock()
1332 kwargs = {'hosts': 'http://elasticservaddr:1997'}
1333 self.eng = elastic.ElasticSearchEngineV2(backend=backend)
1334 self.eng.init(index='freezer', **kwargs)
1335 self.eng.session_manager = mock.Mock()
1336
1337 def test_get_session_userid_and_session_id_return_doc(self):
1338 self.eng.session_manager.get.return_value = common.get_fake_session_0()
1339 res = self.eng.get_session(project_id='tecs',
1340 user_id=common.fake_session_0['user_id'],
1341 session_id=common.fake_session_0[
1342 'session_id'])
1343 self.assertEqual(common.fake_session_0, res)
1344 self.eng.session_manager.get.assert_called_with(
1345 project_id='tecs',
1346 user_id=common.fake_session_0['user_id'],
1347 doc_id=common.fake_session_0['session_id'])
1348
1349 def test_get_session_userid_and_session_id_return_none(self):
1350 self.eng.session_manager.get.return_value = None
1351 res = self.eng.get_session(project_id='tecs',
1352 user_id=common.fake_session_0['user_id'],
1353 session_id=common.fake_session_0[
1354 'session_id'])
1355 self.assertIsNone(res)
1356 self.eng.session_manager.get.assert_called_with(
1357 project_id='tecs',
1358 user_id=common.fake_session_0['user_id'],
1359 doc_id=common.fake_session_0['session_id'])
1360
1361 def test_get_session_with_userid_and_search_return_list(self):
1362 self.eng.session_manager.search.return_value = \
1363 [common.fake_session_0, common.fake_session_0]
1364 my_search = {'match': [{'some_field': 'some text'},
1365 {'description': 'some other text'}]}
1366 res = self.eng.search_session(
1367 project_id='tecs',
1368 user_id=common.fake_session_0['user_id'],
1369 offset=6, limit=15,
1370 search=my_search)
1371 self.assertEqual([common.fake_session_0, common.fake_session_0], res)
1372 self.eng.session_manager.search.assert_called_with(
1373 project_id='tecs',
1374 user_id=common.fake_session_0['user_id'],
1375 search=my_search,
1376 limit=15, offset=6)
1377
1378 def test_get_session_with_userid_and_search_return_empty_list(self):
1379 self.eng.session_manager.search.return_value = []
1380 my_search = {'match': [{'some_field': 'some text'},
1381 {'description': 'some other text'}]}
1382 res = self.eng.search_session(
1383 project_id='tecs',
1384 user_id=common.fake_session_0['user_id'],
1385 offset=6, limit=15,
1386 search=my_search)
1387 self.assertEqual([], res)
1388 self.eng.session_manager.search.assert_called_with(
1389 project_id='tecs',
1390 user_id=common.fake_session_0['user_id'],
1391 search=my_search,
1392 limit=15, offset=6)
1393
1394 @patch('freezer_api.common.elasticv2_utils.SessionDoc')
1395 def test_add_session_ok(self, mock_sessiondoc):
1396 mock_sessiondoc.create.return_value = common.get_fake_session_0()
1397 self.eng.session_manager.insert.return_value = (True, 1)
1398 res = self.eng.add_session(project_id='tecs',
1399 user_id=common.fake_session_0['user_id'],
1400 doc=common.get_fake_session_0())
1401 self.assertEqual(common.fake_session_0['session_id'], res)
1402 self.eng.session_manager.insert.assert_called_with(
1403 common.fake_session_0,
1404 common.fake_session_0['session_id'])
1405
1406 def test_add_session_raises_StorageEngineError_when_manager_insert_raises(
1407 self):
1408 self.eng.session_manager.get.return_value = None
1409 self.eng.session_manager.insert.side_effect = (
1410 exceptions.StorageEngineError('regular test failure')
1411 )
1412 self.assertRaises(exceptions.StorageEngineError, self.eng.add_session,
1413 project_id='tecs',
1414 user_id=common.fake_session_0['user_id'],
1415 doc=common.get_fake_session_0())
1416
1417 def test_delete_session_ok(self):
1418 self.eng.session_manager.delete.return_value = common.fake_session_0[
1419 'session_id']
1420 res = self.eng.delete_session(
1421 project_id='tecs',
1422 user_id=common.fake_session_0['session_id'],
1423 session_id=common.fake_session_0['session_id'])
1424 self.assertEqual(common.fake_session_0['session_id'], res)
1425
1426 def test_delete_client_raises_StorageEngineError_when_es_delete_raises(
1427 self):
1428 self.eng.session_manager.delete.side_effect = (
1429 exceptions.StorageEngineError()
1430 )
1431 self.assertRaises(exceptions.StorageEngineError,
1432 self.eng.delete_session,
1433 project_id='tecs',
1434 user_id=common.fake_session_0['session_id'],
1435 session_id=common.fake_session_0['session_id'])
1436
1437 def test_update_session_raises_DocumentNotFound_when_doc_not_exists(self):
1438 self.eng.session_manager.get.side_effect = exceptions.DocumentNotFound(
1439 'regular test failure')
1440 patch = {'session_id': 'black_milk'}
1441 self.assertRaises(exceptions.DocumentNotFound, self.eng.update_session,
1442 project_id='tecs',
1443 user_id=common.fake_session_0['session_id'],
1444 session_id=common.fake_session_0['session_id'],
1445 patch_doc=patch)
1446
1447 def test_update_session_raises_DocumentNotFound_when_update_raises(
1448 self):
1449 self.eng.session_manager.get.return_value = common.get_fake_session_0()
1450 patch = {'session_id': 'black_milk'}
1451 self.eng.session_manager.update.side_effect = (
1452 exceptions.DocumentNotFound('regular test failure')
1453 )
1454 self.assertRaises(exceptions.DocumentNotFound, self.eng.update_session,
1455 project_id='tecs',
1456 user_id=common.fake_session_0['session_id'],
1457 session_id=common.fake_session_0['session_id'],
1458 patch_doc=patch)
1459
1460 def test_update_session_returns_new_doc_version(self):
1461 self.eng.session_manager.get.return_value = common.get_fake_session_0()
1462 patch = {'session_id': 'group_four'}
1463 self.eng.session_manager.update.return_value = 11
1464 res = self.eng.update_session(
1465 project_id='tecs',
1466 user_id=common.fake_session_0['session_id'],
1467 session_id=common.fake_session_0['session_id'],
1468 patch_doc=patch)
1469 self.assertEqual(11, res)
1470
1471 def test_replace_session_raises_AccessForbidden_when_session_manager_raise(
1472 self):
1473 self.eng.session_manager.get.side_effect = exceptions.AccessForbidden(
1474 'regular test failure')
1475 self.eng.session_manager.insert.return_value = (True, 3)
1476 self.assertRaises(exceptions.AccessForbidden, self.eng.replace_session,
1477 project_id='tecs',
1478 user_id=common.fake_session_0['session_id'],
1479 session_id=common.fake_session_0['session_id'],
1480 doc=common.get_fake_session_0())
1481
1482 def test_replace_session_returns_ok_when_doc_is_new(self):
1483 self.eng.session_manager.get.side_effect = exceptions.DocumentNotFound(
1484 'regular test failure')
1485 self.eng.session_manager.insert.return_value = (True, 1)
1486 res = self.eng.replace_session(
1487 project_id='tecs',
1488 user_id=common.fake_session_0['session_id'],
1489 session_id=common.fake_session_0['session_id'],
1490 doc=common.get_fake_session_0())
1491 self.assertEqual(1, res)
1492
1493 def test_replace_session_returns_version_1_when_doc_is_overwritten(self):
1494 self.eng.session_manager.get.return_value = common.get_fake_session_0()
1495 self.eng.session_manager.insert.return_value = (False, 3)
1496 res = self.eng.replace_session(
1497 project_id='tecs',
1498 user_id=common.fake_session_0['session_id'],
1499 session_id=common.fake_session_0['session_id'],
1500 doc=common.get_fake_session_0())
1501 self.assertEqual(3, res)