Best Python code snippet using Airtest
test_hpe3par_snapshot.py
Source:test_hpe3par_snapshot.py
1# (C) Copyright 2018 Hewlett Packard Enterprise Development LP2#3# This program is free software; you can redistribute it and/or modify4# it under the terms of version 3 of the GNU General Public License as5# published by the Free Software Foundation. Alternatively, at your6# choice, you may also redistribute it and/or modify it under the terms7# of the Apache License, version 2.0, available at8#9# http://www.apache.org/licenses/LICENSE-2.010#11# This program is distributed in the hope that it will be useful, but12# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY13# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License14# for more details.15#16# You should have received a copy of the GNU General Public License along17# with this program. If not, see <https://www.gnu.org/licenses/>18import mock19import unittest20from Modules import hpe3par_snapshot21from ansible.module_utils.basic import AnsibleModule22class TestHpe3parSnapshot(unittest.TestCase):23 fields = {24 "state": {25 "required": True,26 "choices": ['present', 'absent', 'create_schedule', 'suspend_schedule', 'resume_schedule', 'delete_schedule', 'modify_schedule', 'modify', 'restore_offline', 'restore_online'],27 "type": 'str'28 },29 "storage_system_ip": {30 "required": True,31 "type": "str"32 },33 "storage_system_username": {34 "required": True,35 "type": "str",36 "no_log": True37 },38 "storage_system_password": {39 "required": True,40 "type": "str",41 "no_log": True42 },43 "snapshot_name": {44 "type": "str"45 },46 "base_volume_name": {47 "type": "str"48 },49 "read_only": {50 "type": "bool"51 },52 "expiration_time": {53 "type": "int",54 },55 "retention_time": {56 "type": "int"57 },58 "expiration_unit": {59 "type": "str",60 "choices": ['Hours', 'Days'],61 "default": 'Hours'62 },63 "retention_unit": {64 "type": "str",65 "choices": ['Hours', 'Days'],66 "default": 'Hours'67 },68 "expiration_hours": {69 "type": "int",70 "default": 071 },72 "retention_hours": {73 "type": "int",74 "default": 075 },76 "priority": {77 "type": "str",78 "choices": ['HIGH', 'MEDIUM', 'LOW'],79 },80 "allow_remote_copy_parent": {81 "type": "bool"82 },83 "new_name": {84 "type": "str"85 },86 "rm_exp_time": {87 "type": "bool"88 },89 "schedule_name": { 90 "type": "str"91 },92 "new_schedule_name": { 93 "type": "str"94 },95 "task_freq": {96 "type": "str"97 }98 }99 @mock.patch('Modules.hpe3par_snapshot.client')100 @mock.patch('Modules.hpe3par_snapshot.AnsibleModule')101 def test_module_args(self, mock_module, mock_client):102 """103 hpe3par online clone - test module arguments104 """105 PARAMS_FOR_PRESENT = {106 'storage_system_ip': '192.168.0.1',107 'storage_system_username': 'USER',108 'storage_system_password': 'PASS',109 'snapshot_name': 'test_snapshot',110 'base_volume_name': 'base_volume',111 'read_only': False,112 'expiration_time': 0,113 'retention_time': 0,114 'expiration_unit': 'Hours',115 'retention_unit': 'Hours',116 'expiration_hours': 0,117 'retention_hours': 0,118 'priority': 'MEDIUM',119 'allow_remote_copy_parent': False,120 'new_name': 'snapshot_new',121 'rm_exp_time': False,122 'state': 'present',123 'schedule_name': 'test_schedule',124 'task_freq': 'hourly',125 'new_schedule_name': 'new_schedule'126 }127 mock_module.params = PARAMS_FOR_PRESENT128 mock_module.return_value = mock_module129 hpe3par_snapshot.main()130 mock_module.assert_called_with(131 argument_spec=self.fields)132 @mock.patch('Modules.hpe3par_snapshot.client')133 @mock.patch('Modules.hpe3par_snapshot.AnsibleModule')134 @mock.patch('Modules.hpe3par_snapshot.create_snapshot')135 def test_main_exit_present(self, mock_create_snapshot, mock_module, mock_client):136 """137 hpe3par snapshot - success check138 """139 PARAMS_FOR_PRESENT = {140 'storage_system_ip': '192.168.0.1',141 'storage_system_name': '3PAR',142 'storage_system_username': 'USER',143 'storage_system_password': 'PASS',144 'snapshot_name': 'test_snapshot',145 'base_volume_name': 'base_volume',146 'read_only': False,147 'expiration_time': 0,148 'retention_time': 0,149 'expiration_unit': 'Hours',150 'retention_unit': 'Hours',151 'expiration_hours': None,152 'retention_hours': None,153 'priority': None,154 'allow_remote_copy_parent': None,155 'new_name': None,156 'rm_exp_time': None,157 'state': 'present',158 'schedule_name': 'test_schedule',159 'task_freq': 'hourly',160 'new_schedule_name': 'new_schedule'161 }162 # This creates a instance of the AnsibleModule mock.163 mock_module.params = PARAMS_FOR_PRESENT164 mock_module.return_value = mock_module165 instance = mock_module.return_value166 mock_create_snapshot.return_value = (167 True, True, "Created Snapshot successfully.", {})168 hpe3par_snapshot.main()169 # AnsibleModule.exit_json should be called170 instance.exit_json.assert_called_with(171 changed=True, msg="Created Snapshot successfully.")172 # AnsibleModule.fail_json should not be called173 self.assertEqual(instance.fail_json.call_count, 0)174 @mock.patch('Modules.hpe3par_snapshot.client')175 @mock.patch('Modules.hpe3par_snapshot.AnsibleModule')176 @mock.patch('Modules.hpe3par_snapshot.delete_snapshot')177 def test_main_exit_absent(self, mock_delete_snapshot, mock_module, mock_client):178 """179 hpe3par snapshot - success check180 """181 PARAMS_FOR_PRESENT = {182 'storage_system_ip': '192.168.0.1',183 'storage_system_name': '3PAR',184 'storage_system_username': 'USER',185 'storage_system_password': 'PASS',186 'snapshot_name': 'test_snapshot',187 'base_volume_name': None,188 'read_only': None,189 'expiration_time': None,190 'retention_time': None,191 'expiration_unit': None,192 'retention_unit': None,193 'expiration_hours': None,194 'retention_hours': None,195 'priority': None,196 'allow_remote_copy_parent': None,197 'new_name': None,198 'rm_exp_time': None,199 'state': 'absent',200 'schedule_name': 'test_schedule',201 'task_freq': 'hourly',202 'new_schedule_name': 'new_schedule'203 }204 # This creates a instance of the AnsibleModule mock.205 mock_module.params = PARAMS_FOR_PRESENT206 mock_module.return_value = mock_module207 instance = mock_module.return_value208 mock_delete_snapshot.return_value = (209 True, True, "Deleted Snapshot test_snapshot successfully.", {})210 hpe3par_snapshot.main()211 # AnsibleModule.exit_json should be called212 instance.exit_json.assert_called_with(213 changed=True, msg="Deleted Snapshot test_snapshot successfully.")214 # AnsibleModule.fail_json should not be called215 self.assertEqual(instance.fail_json.call_count, 0)216 @mock.patch('Modules.hpe3par_snapshot.client')217 @mock.patch('Modules.hpe3par_snapshot.AnsibleModule')218 @mock.patch('Modules.hpe3par_snapshot.modify_snapshot')219 def test_main_exit_modify(self, mock_modify_snapshot, mock_module, mock_client):220 """221 hpe3par snapshot - success check222 """223 PARAMS_FOR_PRESENT = {224 'storage_system_ip': '192.168.0.1',225 'storage_system_name': '3PAR',226 'storage_system_username': 'USER',227 'storage_system_password': 'PASS',228 'snapshot_name': 'test_snapshot',229 'base_volume_name': None,230 'read_only': None,231 'expiration_time': None,232 'retention_time': None,233 'expiration_unit': None,234 'retention_unit': None,235 'expiration_hours': 10,236 'retention_hours': 10,237 'priority': None,238 'allow_remote_copy_parent': None,239 'new_name': 'new_snapshot',240 'rm_exp_time': True,241 'state': 'modify',242 'schedule_name': 'test_schedule',243 'task_freq': 'hourly',244 'new_schedule_name': 'new_schedule'245 }246 # This creates a instance of the AnsibleModule mock.247 mock_module.params = PARAMS_FOR_PRESENT248 mock_module.return_value = mock_module249 instance = mock_module.return_value250 mock_modify_snapshot.return_value = (251 True, True, "Modified Snapshot test_snapshot successfully.", {})252 hpe3par_snapshot.main()253 # AnsibleModule.exit_json should be called254 instance.exit_json.assert_called_with(255 changed=True, msg="Modified Snapshot test_snapshot successfully.")256 # AnsibleModule.fail_json should not be called257 self.assertEqual(instance.fail_json.call_count, 0)258 @mock.patch('Modules.hpe3par_snapshot.client')259 @mock.patch('Modules.hpe3par_snapshot.AnsibleModule')260 @mock.patch('Modules.hpe3par_snapshot.restore_snapshot_offline')261 def test_main_exit_offline_snapshot(self, mock_restore_snapshot_offline, mock_module, mock_client):262 """263 hpe3par snapshot - success check264 """265 PARAMS_FOR_PRESENT = {266 'storage_system_ip': '192.168.0.1',267 'storage_system_name': '3PAR',268 'storage_system_username': 'USER',269 'storage_system_password': 'PASS',270 'snapshot_name': 'test_snapshot',271 'base_volume_name': None,272 'read_only': None,273 'expiration_time': None,274 'retention_time': None,275 'expiration_unit': None,276 'retention_unit': None,277 'expiration_hours': None,278 'retention_hours': None,279 'priority': 'MEDIUM',280 'allow_remote_copy_parent': False,281 'new_name': None,282 'rm_exp_time': None,283 'state': 'restore_offline',284 'schedule_name': 'test_schedule',285 'task_freq': 'hourly',286 'new_schedule_name': 'new_schedule'287 }288 # This creates a instance of the AnsibleModule mock.289 mock_module.params = PARAMS_FOR_PRESENT290 mock_module.return_value = mock_module291 instance = mock_module.return_value292 mock_restore_snapshot_offline.return_value = (293 True, True, "Restored offline snapshot test_snapshot successfully.", {})294 hpe3par_snapshot.main()295 # AnsibleModule.exit_json should be called296 instance.exit_json.assert_called_with(297 changed=True, msg="Restored offline snapshot test_snapshot successfully.")298 # AnsibleModule.fail_json should not be called299 self.assertEqual(instance.fail_json.call_count, 0)300 @mock.patch('Modules.hpe3par_snapshot.client')301 @mock.patch('Modules.hpe3par_snapshot.AnsibleModule')302 @mock.patch('Modules.hpe3par_snapshot.restore_snapshot_online')303 def test_main_exit_online_snapshot(self, mock_restore_snapshot_online, mock_module, mock_client):304 """305 hpe3par snapshot - success check306 """307 PARAMS_FOR_PRESENT = {308 'storage_system_ip': '192.168.0.1',309 'storage_system_name': '3PAR',310 'storage_system_username': 'USER',311 'storage_system_password': 'PASS',312 'snapshot_name': 'test_snapshot',313 'base_volume_name': None,314 'read_only': None,315 'expiration_time': None,316 'retention_time': None,317 'expiration_unit': None,318 'retention_unit': None,319 'expiration_hours': None,320 'retention_hours': None,321 'priority': None,322 'allow_remote_copy_parent': False,323 'new_name': None,324 'rm_exp_time': None,325 'state': 'restore_online',326 'schedule_name': 'test_schedule',327 'task_freq': 'hourly',328 'new_schedule_name': 'new_schedule'329 }330 # This creates a instance of the AnsibleModule mock.331 mock_module.params = PARAMS_FOR_PRESENT332 mock_module.return_value = mock_module333 instance = mock_module.return_value334 mock_restore_snapshot_online.return_value = (335 True, True, "Restored online snapshot test_snapshot successfully.", {})336 hpe3par_snapshot.main()337 # AnsibleModule.exit_json should be called338 instance.exit_json.assert_called_with(339 changed=True, msg="Restored online snapshot test_snapshot successfully.")340 # AnsibleModule.fail_json should not be called341 self.assertEqual(instance.fail_json.call_count, 0)342 @mock.patch('Modules.hpe3par_snapshot.client')343 @mock.patch('Modules.hpe3par_snapshot.AnsibleModule')344 @mock.patch('Modules.hpe3par_snapshot.create_schedule')345 def test_main_exit_create_schedule(self, mock_create_schedule, mock_module, mock_client):346 """347 hpe3par snapshot - success check348 """349 PARAMS_FOR_PRESENT = {350 'storage_system_ip': '192.168.0.1',351 'storage_system_name': '3PAR',352 'storage_system_username': 'USER',353 'storage_system_password': 'PASS',354 'snapshot_name': 'test_snapshot',355 'base_volume_name': 'base_volume',356 'read_only': False,357 'expiration_time': 0,358 'retention_time': 0,359 'expiration_unit': 'Hours',360 'retention_unit': 'Hours',361 'expiration_hours': None,362 'retention_hours': None,363 'priority': None,364 'allow_remote_copy_parent': None,365 'new_name': None,366 'rm_exp_time': None,367 'state': 'create_schedule',368 'schedule_name': 'test_schedule',369 'task_freq': 'hourly',370 'new_schedule_name': 'new_schedule' 371 }372 # This creates a instance of the AnsibleModule mock.373 mock_module.params = PARAMS_FOR_PRESENT374 mock_module.return_value = mock_module375 instance = mock_module.return_value376 mock_create_schedule.return_value = (377 True, True, "Created Schedule successfully.", {})378 hpe3par_snapshot.main()379 # AnsibleModule.exit_json should be called380 instance.exit_json.assert_called_with(381 changed=True, msg="Created Schedule successfully.")382 # AnsibleModule.fail_json should not be called383 self.assertEqual(instance.fail_json.call_count, 0)384 @mock.patch('Modules.hpe3par_snapshot.client')385 @mock.patch('Modules.hpe3par_snapshot.AnsibleModule')386 @mock.patch('Modules.hpe3par_snapshot.modify_schedule')387 def test_main_exit_modify_schedule(self, mock_modify_schedule, mock_module, mock_client):388 """389 hpe3par snapshot - success check390 """391 PARAMS_FOR_PRESENT = {392 'storage_system_ip': '192.168.0.1',393 'storage_system_name': '3PAR',394 'storage_system_username': 'USER',395 'storage_system_password': 'PASS',396 'snapshot_name': 'test_snapshot',397 'base_volume_name': 'base_volume',398 'read_only': False,399 'expiration_time': 0,400 'retention_time': 0,401 'expiration_unit': 'Hours',402 'retention_unit': 'Hours',403 'expiration_hours': None,404 'retention_hours': None,405 'priority': None,406 'allow_remote_copy_parent': None,407 'new_name': None,408 'rm_exp_time': None,409 'state': 'modify_schedule',410 'schedule_name': 'test_schedule',411 'task_freq': 'hourly',412 'new_schedule_name': 'new_schedule'413 }414 # This creates a instance of the AnsibleModule mock.415 mock_module.params = PARAMS_FOR_PRESENT416 mock_module.return_value = mock_module417 instance = mock_module.return_value418 mock_modify_schedule.return_value = (419 True, True, "Schedule Modified successfully.", {})420 hpe3par_snapshot.main()421 # AnsibleModule.exit_json should be called422 instance.exit_json.assert_called_with(423 changed=True, msg="Schedule Modified successfully.")424 # AnsibleModule.fail_json should not be called425 self.assertEqual(instance.fail_json.call_count, 0)426 @mock.patch('Modules.hpe3par_snapshot.client')427 @mock.patch('Modules.hpe3par_snapshot.AnsibleModule')428 @mock.patch('Modules.hpe3par_snapshot.suspend_schedule')429 def test_main_exit_suspend_schedule(self, mock_suspend_schedule, mock_module, mock_client):430 """431 hpe3par snapshot - success check432 """433 PARAMS_FOR_PRESENT = {434 'storage_system_ip': '192.168.0.1',435 'storage_system_name': '3PAR',436 'storage_system_username': 'USER',437 'storage_system_password': 'PASS',438 'snapshot_name': 'test_snapshot',439 'base_volume_name': None,440 'read_only': None,441 'expiration_time': None,442 'retention_time': None,443 'expiration_unit': None,444 'retention_unit': None,445 'expiration_hours': None,446 'retention_hours': None,447 'priority': None,448 'allow_remote_copy_parent': None,449 'new_name': None,450 'rm_exp_time': None,451 'state': 'suspend_schedule',452 'schedule_name': 'test_schedule',453 'task_freq': 'hourly',454 'new_schedule_name': 'new_schedule'455 }456 # This creates a instance of the AnsibleModule mock.457 mock_module.params = PARAMS_FOR_PRESENT458 mock_module.return_value = mock_module459 instance = mock_module.return_value460 mock_suspend_schedule.return_value = (461 True, True, "Schedule test_schedule suspended successfully.", {})462 hpe3par_snapshot.main()463 # AnsibleModule.exit_json should be called464 instance.exit_json.assert_called_with(465 changed=True, msg="Schedule test_schedule suspended successfully.")466 # AnsibleModule.fail_json should not be called467 self.assertEqual(instance.fail_json.call_count, 0)468 @mock.patch('Modules.hpe3par_snapshot.client')469 @mock.patch('Modules.hpe3par_snapshot.AnsibleModule')470 @mock.patch('Modules.hpe3par_snapshot.resume_schedule')471 def test_main_exit_resume_schedule(self, mock_resume_schedule, mock_module, mock_client):472 """473 hpe3par snapshot - success check474 """475 PARAMS_FOR_PRESENT = {476 'storage_system_ip': '192.168.0.1',477 'storage_system_name': '3PAR',478 'storage_system_username': 'USER',479 'storage_system_password': 'PASS',480 'snapshot_name': 'test_snapshot',481 'base_volume_name': None,482 'read_only': None,483 'expiration_time': None,484 'retention_time': None,485 'expiration_unit': None,486 'retention_unit': None,487 'expiration_hours': None,488 'retention_hours': None,489 'priority': None,490 'allow_remote_copy_parent': None,491 'new_name': None,492 'rm_exp_time': None,493 'state': 'resume_schedule',494 'schedule_name': 'test_schedule',495 'task_freq': 'hourly',496 'new_schedule_name': 'new_schedule'497 }498 # This creates a instance of the AnsibleModule mock.499 mock_module.params = PARAMS_FOR_PRESENT500 mock_module.return_value = mock_module501 instance = mock_module.return_value502 mock_resume_schedule.return_value = (503 True, True, "Schedule test_schedule resumed successfully.", {})504 hpe3par_snapshot.main()505 # AnsibleModule.exit_json should be called506 instance.exit_json.assert_called_with(507 changed=True, msg="Schedule test_schedule resumed successfully.")508 # AnsibleModule.fail_json should not be called509 self.assertEqual(instance.fail_json.call_count, 0) 510 511 @mock.patch('Modules.hpe3par_snapshot.client')512 @mock.patch('Modules.hpe3par_snapshot.AnsibleModule')513 @mock.patch('Modules.hpe3par_snapshot.delete_schedule')514 def test_main_exit_schedule_delete(self, mock_delete_schedule, mock_module, mock_client):515 """516 hpe3par snapshot - success check517 """518 PARAMS_FOR_PRESENT = {519 'storage_system_ip': '192.168.0.1',520 'storage_system_name': '3PAR',521 'storage_system_username': 'USER',522 'storage_system_password': 'PASS',523 'snapshot_name': 'test_snapshot',524 'base_volume_name': None,525 'read_only': None,526 'expiration_time': None,527 'retention_time': None,528 'expiration_unit': None,529 'retention_unit': None,530 'expiration_hours': None,531 'retention_hours': None,532 'priority': None,533 'allow_remote_copy_parent': None,534 'new_name': None,535 'rm_exp_time': None,536 'state': 'delete_schedule',537 'schedule_name': 'test_schedule',538 'task_freq': 'hourly',539 'new_schedule_name': 'new_schedule'540 }541 # This creates a instance of the AnsibleModule mock.542 mock_module.params = PARAMS_FOR_PRESENT543 mock_module.return_value = mock_module544 instance = mock_module.return_value545 mock_delete_schedule.return_value = (546 True, True, "Deleted Schedule test_schedule successfully.", {})547 hpe3par_snapshot.main()548 # AnsibleModule.exit_json should be called549 instance.exit_json.assert_called_with(550 changed=True, msg="Deleted Schedule test_schedule successfully.")551 # AnsibleModule.fail_json should not be called552 self.assertEqual(instance.fail_json.call_count, 0)553 @mock.patch('Modules.hpe3par_snapshot.client')554 def test_create_snapshot(self, mock_client):555 mock_client.HPE3ParClient.login.return_value = None556 mock_client.HPE3ParClient.volumeExists.return_value = False557 mock_client.HPE3ParClient.createSnapshot.return_value = None558 mock_client.HPE3ParClient.logout.return_value = None559 self.assertEqual(hpe3par_snapshot.create_snapshot(mock_client.HPE3ParClient,560 'USER',561 'PASS',562 'test_snapshot',563 'base_volume',564 False,565 10,566 10,567 'Hours',568 'Days'569 ), (True, True, "Created Snapshot %s successfully." % 'test_snapshot', {}))570 mock_client.HPE3ParClient.volumeExists.return_value = True571 self.assertEqual(hpe3par_snapshot.create_snapshot(mock_client.HPE3ParClient,572 'USER',573 'PASS',574 'test_snapshot',575 'base_volume',576 False,577 10,578 10,579 'Hours',580 'Days'581 ), (True, False, "Volume/Snapshot already present", {}))582 self.assertEqual(hpe3par_snapshot.create_snapshot(mock_client.HPE3ParClient,583 'USER',584 'PASS',585 'test_snapshot1222222222222223333333',586 'base_volume',587 False,588 10,589 10,590 'Hours',591 'Days',592 ), (False, False, "Snapshot create failed. Snapshot name must be atleast 1 character and not more than 31 characters", {}))593 self.assertEqual(hpe3par_snapshot.create_snapshot(mock_client.HPE3ParClient,594 'USER',595 'PASS',596 'test_snapshot',597 'base_volume1222222222222222222222222222',598 False,599 10,600 10,601 'Hours',602 'Days',603 ), (False, False, "Snapshot create failed. Base volume name must be atleast 1 character and not more than 31 characters", {}))604 self.assertEqual(hpe3par_snapshot.create_snapshot(mock_client.HPE3ParClient,605 'USER',606 None,607 'test_snapshot',608 'base_volume',609 False,610 10,611 10,612 'Hours',613 'Days'614 ), (False, False, "Snapshot create failed. Storage system username or password is null", {}))615 self.assertEqual(hpe3par_snapshot.create_snapshot(mock_client.HPE3ParClient,616 'USER',617 'PASS',618 None,619 'base_volume',620 False,621 10,622 10,623 'Hours',624 'Days'625 ), (False, False, "Snapshot create failed. Snapshot name is null", {}))626 self.assertEqual(hpe3par_snapshot.create_snapshot(mock_client.HPE3ParClient,627 'USER',628 'PASS',629 'test_snapshot',630 None,631 False,632 10,633 10,634 'Hours',635 'Days'636 ), (False, False, "Snapshot create failed. Base volume name is null", {}))637 @mock.patch('Modules.hpe3par_snapshot.client')638 def test_modify_snapshot(self, mock_client):639 mock_client.HPE3ParClient.login.return_value = None640 mock_client.HPE3ParClient.modifyVolume.return_value = None641 mock_client.HPE3ParClient.logout.return_value = None642 self.assertEqual(hpe3par_snapshot.modify_snapshot(mock_client.HPE3ParClient,643 'USER',644 'PASS',645 'test_snapshot',646 'new_snapshot',647 10,648 10,649 True650 ), (True, True, "Modified Snapshot %s successfully." % 'test_snapshot', {}))651 self.assertEqual(hpe3par_snapshot.modify_snapshot(mock_client.HPE3ParClient,652 'USER',653 None,654 'test_snapshot',655 'new_snapshot',656 10,657 10,658 True659 ), (False, False, "Modify snapshot failed. Storage system username or password is null", {}))660 self.assertEqual(hpe3par_snapshot.modify_snapshot(mock_client.HPE3ParClient,661 'USER',662 'PASS',663 'test_snapshot234333333333333333333333333333',664 'test_snapshot',665 10,666 10,667 True,668 ), (False, False, "Snapshot create failed. Snapshot name must be atleast 1 character and not more than 31 characters", {}))669 self.assertEqual(hpe3par_snapshot.modify_snapshot(mock_client.HPE3ParClient,670 'USER',671 'PASS',672 None,673 'new_snapshot',674 10,675 10,676 True677 ), (False, False, "Modify snapshot failed. Snapshot name is null", {}))678 @mock.patch('Modules.hpe3par_snapshot.client')679 def test_delete_snapshot(self, mock_client):680 mock_client.HPE3ParClient.login.return_value = None681 mock_client.HPE3ParClient.volumeExists.return_value = True682 mock_client.HPE3ParClient.deleteVolume.return_value = None683 mock_client.HPE3ParClient.logout.return_value = None684 self.assertEqual(hpe3par_snapshot.delete_snapshot(mock_client.HPE3ParClient,685 'USER',686 'PASS',687 'test_snapshot'688 ), (True, True, "Deleted Snapshot %s successfully." % 'test_snapshot', {}))689 mock_client.HPE3ParClient.volumeExists.return_value = False690 self.assertEqual(hpe3par_snapshot.delete_snapshot(mock_client.HPE3ParClient,691 'USER',692 'PASS',693 'test_snapshot'694 ), (True, False, "Volume/Snapshot does not exist", {}))695 self.assertEqual(hpe3par_snapshot.delete_snapshot(mock_client.HPE3ParClient,696 'USER',697 None,698 'test_snapshot'699 ), (False, False, "Snapshot delete failed. Storage system username or password is null", {}))700 self.assertEqual(hpe3par_snapshot.delete_snapshot(mock_client.HPE3ParClient,701 'USER',702 'PASS',703 None704 ), (False, False, "Snapshot delete failed. Snapshot name is null", {}))705 self.assertEqual(hpe3par_snapshot.delete_snapshot(mock_client.HPE3ParClient,706 'USER',707 'PASS',708 'test_snapshot1111111111111111111111111111'709 ), (False, False, "Snapshot create failed. Snapshot name must be atleast 1 character and not more than 31 characters", {}))710 @mock.patch('Modules.hpe3par_snapshot.client')711 def test_restore_snapshot_offline(self, mock_client):712 mock_client.HPE3ParClient.login.return_value = None713 mock_client.HPE3ParClient.promoteVirtualCopy.return_value = None714 mock_client.HPE3ParClient.logout.return_value = None715 self.assertEqual(hpe3par_snapshot.restore_snapshot_offline(mock_client.HPE3ParClient,716 'USER',717 'PASS',718 'test_snapshot',719 'MEDIUM',720 False721 ), (True, True, "Restored offline snapshot %s successfully." % 'test_snapshot', {}))722 self.assertEqual(hpe3par_snapshot.restore_snapshot_offline(mock_client.HPE3ParClient,723 'USER',724 'PASS',725 'test_snapshot1111111111111111111111111111',726 'MEDIUM',727 False728 ), (False, False, "Snapshot create failed. Snapshot name must be atleast 1 character and not more than 31 characters", {}))729 self.assertEqual(hpe3par_snapshot.restore_snapshot_offline(mock_client.HPE3ParClient,730 None,731 'PASS',732 'test_snapshot',733 'MEDIUM',734 False735 ), (False, False, "Offline snapshot restore failed. Storage system username or password \736is null", {}))737 self.assertEqual(hpe3par_snapshot.restore_snapshot_offline(mock_client.HPE3ParClient,738 'USER',739 'PASS',740 None,741 'MEDIUM',742 False743 ), (False, False, "Offline snapshot restore failed. Snapshot name is null", {}))744 @mock.patch('Modules.hpe3par_snapshot.client')745 def test_restore_snapshot_online(self, mock_client):746 mock_client.HPE3ParClient.login.return_value = None747 mock_client.HPE3ParClient.promoteVirtualCopy.return_value = None748 mock_client.HPE3ParClient.logout.return_value = None749 self.assertEqual(hpe3par_snapshot.restore_snapshot_online(mock_client.HPE3ParClient,750 'USER',751 'PASS',752 'test_snapshot',753 False754 ), (True, True, "Restored online Snapshot %s successfully." % 'test_snapshot', {}))755 self.assertEqual(hpe3par_snapshot.restore_snapshot_online(mock_client.HPE3ParClient,756 None,757 'PASS',758 'test_snapshot',759 False760 ), (False, False, "Online snapshot restore failed. Storage system username or password is \761null", {}))762 self.assertEqual(hpe3par_snapshot.restore_snapshot_online(mock_client.HPE3ParClient,763 'USER',764 'PASS',765 'test_snapshot1111111111111111111111111111',766 False ), (False, False, "Snapshot create failed. Snapshot name must be atleast 1 character and not more than 31 characters", {}))767 self.assertEqual(hpe3par_snapshot.restore_snapshot_online(mock_client.HPE3ParClient,768 'USER',769 'PASS',770 None,771 False772 ), (False, False, "Online snapshot restore failed. Snapshot name is null", {}))773 @mock.patch('Modules.hpe3par_snapshot.client')774 def test_create_schedule(self, mock_client):775 mock_client.HPE3ParClient.login.return_value = None776 mock_client.HPE3ParClient.scheduleExists.return_value = False777 mock_client.HPE3ParClient.volumeExists.return_value = True778 mock_client.HPE3ParClient.createSchedule.return_value = None779 mock_client.HPE3ParClient.logout.return_value = None780 self.assertEqual(hpe3par_snapshot.create_schedule(mock_client.HPE3ParClient,781 '192.168.0.1',782 'USER',783 'PASS',784 'test_schedule',785 'base_volume',786 True,787 10,788 10,789 'Hours',790 'Days',791 'hourly'792 ), (False, False, "Expiration time must be greater than retention time for non zero values", {}))793 self.assertEqual(hpe3par_snapshot.create_schedule(mock_client.HPE3ParClient,794 '192.168.0.1',795 'USER',796 'PASS',797 'test_schedule1222222222222223333333',798 'base_volume',799 True,800 10,801 9,802 'Hours',803 'Days',804 'hourly'805 ), (False, False, "Schedule creation failed. Schedule name must be atleast 1 character and not more than 31 characters", {}))806 self.assertEqual(hpe3par_snapshot.create_schedule(mock_client.HPE3ParClient,807 '192.168.0.1',808 'USER',809 'PASS',810 'test_schedule',811 'base_volume',812 True,813 10,814 9,815 'Hours',816 'Hours',817 'hourly'818 ), (True, True, "Created Schedule %s successfully." % 'test_schedule', {}))819 mock_client.HPE3ParClient.scheduleExists.return_value = True820 self.assertEqual(hpe3par_snapshot.create_schedule(mock_client.HPE3ParClient,821 '192.168.0.1',822 'USER',823 'PASS',824 'test_schedule',825 'base_volume',826 True,827 10,828 9,829 'Hours',830 'Hours',831 'hourly'832 ), (True, False, "Schedule already Exist", {}))833 834 mock_client.HPE3ParClient.scheduleExists.return_value = False835 self.assertEqual(hpe3par_snapshot.create_schedule(mock_client.HPE3ParClient,836 '192.168.0.1',837 'USER',838 None, 839 'test_schedule',840 'base_volume',841 True,842 10,843 9,844 'Hours',845 'Hours',846 'hourly'847 ), (False, False, "Schedule creation failed. Storage system username or password is null", {}))848 self.assertEqual(hpe3par_snapshot.create_schedule(mock_client.HPE3ParClient,849 '192.168.0.1',850 'USER',851 'PASS',852 None,853 'base_volume',854 True,855 10,856 9,857 'Hours',858 'Hours',859 'hourly'860 ), (False, False, "Schedule create failed. Schedule name is null", {}))861 self.assertEqual(hpe3par_snapshot.create_schedule(mock_client.HPE3ParClient,862 '192.168.0.1',863 'USER',864 'PASS',865 'test_schedule',866 'base_volume111111111111111111111111',867 True,868 10,869 9,870 'Hours',871 'Hours',872 'hourly'873 ), (False, False, "Schedule create failed. Base volume name must be atleast 1 character and not more than 19 characters", {}))874 self.assertEqual(hpe3par_snapshot.create_schedule(mock_client.HPE3ParClient,875 '192.168.0.1',876 'USER',877 'PASS',878 'test_schedule',879 'base_volume',880 '',881 10,882 9,883 'Hours',884 'Hours',885 'hourly'886 ), (True, True, "Created Schedule %s successfully." % 'test_schedule', {}))887 self.assertEqual(hpe3par_snapshot.create_schedule(mock_client.HPE3ParClient,888 '192.168.0.1',889 'USER',890 'PASS',891 'test_snapshot',892 None,893 True,894 10,895 9,896 'Hours',897 'Hours',898 '0 * * * *'899 ), (False, False, "Schedule create failed. Base volume name is null", {}))900 mock_client.HPE3ParClient.volumeExists.return_value = False901 self.assertEqual(hpe3par_snapshot.create_schedule(mock_client.HPE3ParClient,902 '192.168.0.1',903 'USER',904 'PASS',905 'test_schedule',906 'base_volume',907 True,908 10,909 9,910 'Hours',911 'Hours',912 '0 * * * *'913 ), (False, False, "Volume does not Exist", {}))914 @mock.patch('Modules.hpe3par_snapshot.client')915 def test_modify_schedule(self, mock_client):916 mock_client.HPE3ParClient.login.return_value = None917 mock_client.HPE3ParClient.scheduleExists.return_value = True918 mock_client.HPE3ParClient.modifySchedule.return_value = None919 mock_client.HPE3ParClient.logout.return_value = None920 self.assertEqual(hpe3par_snapshot.modify_schedule(mock_client.HPE3ParClient,921 '192.168.0.1',922 'USER',923 'PASS',924 'test_schedule1222222222222223333333',925 '',926 'hourly'927 ), (False, False, "Modify schedule failed. Schedule name must be atleast 1 character and not more than 31 characters", {}))928 self.assertEqual(hpe3par_snapshot.modify_schedule(mock_client.HPE3ParClient,929 '192.168.0.1',930 'USER',931 'PASS',932 'test_schedule',933 '',934 'hourly'935 ), (True, True, "Modified Schedule %s successfully." % 'test_schedule', {}))936 self.assertEqual(hpe3par_snapshot.modify_schedule(mock_client.HPE3ParClient,937 '192.168.0.1',938 'USER',939 'PASS',940 'test_schedule',941 'schedule_new',942 'hourly'943 ), (True, True, "Modified Schedule %s successfully." % 'test_schedule', {}))944 self.assertEqual(hpe3par_snapshot.modify_schedule(mock_client.HPE3ParClient,945 '192.168.0.1',946 'USER',947 'PASS',948 'test_schedule',949 'schedule_new111111111111111111111111111111',950 'hourly'951 ), (False, False, "Modify schedule failed. New Schedule name must be atleast 1 character and not more than 31 characters", {}))952 953 954 mock_client.HPE3ParClient.scheduleExists.return_value = False955 self.assertEqual(hpe3par_snapshot.modify_schedule(mock_client.HPE3ParClient,956 '192.168.0.1',957 'USER',958 'PASS',959 'test_schedule',960 '',961 'hourly'962 ), (False, False, "Schedule does not exist", {}))963 964 mock_client.HPE3ParClient.scheduleExists.return_value = True965 self.assertEqual(hpe3par_snapshot.modify_schedule(mock_client.HPE3ParClient,966 '192.168.0.1',967 'USER',968 None, 969 'test_schedule', 970 '',971 'hourly'972 ), (False, False, "Modify schedule failed. Storage system username or password is null", {}))973 self.assertEqual(hpe3par_snapshot.modify_schedule(mock_client.HPE3ParClient,974 '192.168.0.1',975 'USER',976 'PASS',977 None, 978 '',979 'hourly'980 ), (False, False, "Modify schedule failed. Schedule name is null", {}))981 self.assertEqual(hpe3par_snapshot.modify_schedule(mock_client.HPE3ParClient,982 '192.168.0.1',983 'USER',984 'PASS',985 'test_schedule', 986 'new_schedule',987 'hourly'988 ), (True, True, 'Modified Schedule test_schedule successfully.', {}))989 990 self.assertEqual(hpe3par_snapshot.modify_schedule(mock_client.HPE3ParClient,991 '192.168.0.1',992 'USER',993 'PASS',994 'test_schedule',995 'new_schedule',996 ''997 ), (True, True, "Modified Schedule %s successfully." % 'test_schedule', {}))998 @mock.patch('Modules.hpe3par_snapshot.client')999 def test_suspend_schedule(self, mock_client):1000 mock_client.HPE3ParClient.login.return_value = None1001 mock_client.HPE3ParClient.scheduleExists.return_value = True1002 mock_client.HPE3ParClient.suspendSchedule.return_value = None1003 mock_client.HPE3ParClient.isScheduleActive.return_value = True1004 mock_client.HPE3ParClient.logout.return_value = None1005 self.assertEqual(hpe3par_snapshot.suspend_schedule(mock_client.HPE3ParClient,1006 '192.168.0.1',1007 'USER',1008 'PASS',1009 'test_schedule'1010 ), (True, True, "Schedule suspended %s successfully." % 'test_schedule', {}))1011 mock_client.HPE3ParClient.isScheduleActive.return_value = False1012 self.assertEqual(hpe3par_snapshot.suspend_schedule(mock_client.HPE3ParClient,1013 '192.168.0.1',1014 'USER',1015 'PASS',1016 'test_schedule'1017 ), (True, False, "Schedule status is already suspended", {}))1018 self.assertEqual(hpe3par_snapshot.suspend_schedule(mock_client.HPE3ParClient,1019 '192.168.0.1',1020 'USER',1021 'PASS',1022 'test_schedule122122222333334444444'1023 ), (False, False, "Schedule suspended failed. Schedule name must be atleast 1 character and not more than 31 characters", {}))1024 mock_client.HPE3ParClient.scheduleExists.return_value = False1025 self.assertEqual(hpe3par_snapshot.suspend_schedule(mock_client.HPE3ParClient,1026 '192.168.0.1',1027 'USER',1028 'PASS',1029 'test_schedule'1030 ), (False, False, "Schedule does not exist", {}))1031 self.assertEqual(hpe3par_snapshot.suspend_schedule(mock_client.HPE3ParClient,1032 '192.168.0.1',1033 'USER',1034 None,1035 'test_schedule'1036 ), (False, False, "Schedule suspended failed. Storage system username or password is null", {}))1037 self.assertEqual(hpe3par_snapshot.suspend_schedule(mock_client.HPE3ParClient,1038 '192.168.0.1',1039 'USER',1040 'PASS',1041 None1042 ), (False, False, "Schedule suspended failed. Schedule name is null", {}))1043 @mock.patch('Modules.hpe3par_snapshot.client')1044 def test_resume_schedule(self, mock_client):1045 mock_client.HPE3ParClient.login.return_value = None1046 mock_client.HPE3ParClient.scheduleExists.return_value = True1047 mock_client.HPE3ParClient.resumeSchedule.return_value = None1048 mock_client.HPE3ParClient.isScheduleActive.return_value = False1049 mock_client.HPE3ParClient.logout.return_value = None1050 self.assertEqual(hpe3par_snapshot.resume_schedule(mock_client.HPE3ParClient,1051 '192.168.0.1',1052 'USER',1053 'PASS',1054 'test_schedule'1055 ), (True, True, "Schedule resumed %s successfully." % 'test_schedule', {}))1056 mock_client.HPE3ParClient.isScheduleActive.return_value = True1057 self.assertEqual(hpe3par_snapshot.resume_schedule(mock_client.HPE3ParClient,1058 '192.168.0.1',1059 'USER',1060 'PASS',1061 'test_schedule'1062 ), (True, False, "Schedule status is already active", {}))1063 self.assertEqual(hpe3par_snapshot.resume_schedule(mock_client.HPE3ParClient,1064 '192.168.0.1',1065 'USER',1066 'PASS',1067 'test_schedule122122222333334444444'1068 ), (False, False, "Schedule resumed failed. Schedule name must be atleast 1 character and not more than 31 characters", {}))1069 mock_client.HPE3ParClient.scheduleExists.return_value = False1070 self.assertEqual(hpe3par_snapshot.resume_schedule(mock_client.HPE3ParClient,1071 '192.168.0.1',1072 'USER',1073 'PASS',1074 'test_schedule'1075 ), (False, False, "Schedule does not exist", {}))1076 self.assertEqual(hpe3par_snapshot.resume_schedule(mock_client.HPE3ParClient,1077 '192.168.0.1',1078 'USER',1079 None,1080 'test_schedule'1081 ), (False, False, "Schedule resumed failed. Storage system username or password is null", {}))1082 self.assertEqual(hpe3par_snapshot.resume_schedule(mock_client.HPE3ParClient,1083 '192.168.0.1',1084 'USER',1085 'PASS',1086 None1087 ), (False, False, "Schedule resumed failed. Schedule name is null", {}))1088 1089 @mock.patch('Modules.hpe3par_snapshot.client')1090 def test_delete_schedule(self, mock_client):1091 mock_client.HPE3ParClient.login.return_value = None1092 mock_client.HPE3ParClient.scheduleExists.return_value = True1093 mock_client.HPE3ParClient.deleteSchedule.return_value = None1094 mock_client.HPE3ParClient.logout.return_value = None1095 ipadd = '192.168.0.1'1096 self.assertEqual(hpe3par_snapshot.delete_schedule(mock_client.HPE3ParClient,1097 '192.168.0.1',1098 'USER',1099 'PASS',1100 'test_schedule'1101 ), (True, True, "Deleted Schedule %s successfully." % 'test_schedule', {}))1102 self.assertEqual(hpe3par_snapshot.delete_schedule(mock_client.HPE3ParClient,1103 '192.168.0.1',1104 'USER',1105 'PASS',1106 'test_schedule122122222333334444444'1107 ), (False, False, "Schedule create failed. Schedule name must be atleast 1 character and not more than 31 characters", {}))1108 mock_client.HPE3ParClient.scheduleExists.return_value = False1109 self.assertEqual(hpe3par_snapshot.delete_schedule(mock_client.HPE3ParClient,1110 '192.168.0.1',1111 'USER',1112 'PASS',1113 'test_schedule'1114 ), (True, False, "Schedule does not exist", {}))1115 self.assertEqual(hpe3par_snapshot.delete_schedule(mock_client.HPE3ParClient,1116 '192.168.0.1',1117 'USER',1118 None,1119 'test_schedule'1120 ), (False, False, "Schedule delete failed. Storage system username or password is null", {}))1121 self.assertEqual(hpe3par_snapshot.delete_schedule(mock_client.HPE3ParClient,1122 '192.168.0.1',1123 None,1124 'PASS',1125 'test_schedule'1126 ), (False, False, "Schedule delete failed. Storage system username or password is null", {}))1127 self.assertEqual(hpe3par_snapshot.delete_schedule(mock_client.HPE3ParClient,1128 '192.168.0.1',1129 'USER',1130 'PASS',1131 None1132 ), (False, False, "Schedule delete failed. Schedule name is null", {}))1133if __name__ == '__main__':...
test_client.py
Source:test_client.py
1"""Test the SlackClient class."""2from typing import cast3from unittest import TestCase, mock4from pi_portal.modules.configuration.tests.fixtures import mock_state5from pi_portal.modules.integrations import motion6from pi_portal.modules.integrations.slack import client7from slack_sdk.errors import SlackRequestError8class TestSlackClient(TestCase):9 """Test the SlackClient class."""10 @mock_state.patch11 def setUp(self) -> None:12 self.slack_client = client.SlackClient()13 self.slack_client.motion_client = mock.MagicMock()14 self.slack_client.log = mock.Mock()15 def _mock_motion_client(self) -> mock.Mock:16 return cast(mock.Mock, self.slack_client.motion_client)17 def _mock_log(self) -> mock.Mock:18 return cast(mock.Mock, self.slack_client.log)19 @mock_state.patch20 def test_initialize(self) -> None:21 slack_client = client.SlackClient()22 self.assertEqual(slack_client.web.token, mock_state.MOCK_SLACK_TOKEN)23 self.assertEqual(slack_client.channel, mock_state.MOCK_SLACK_CHANNEL)24 self.assertIsInstance(slack_client.motion_client, motion.Motion)25 def test_send_message(self) -> None:26 test_message = "test message"27 self.slack_client.web = mock.MagicMock()28 self.slack_client.send_message(test_message)29 self.slack_client.web.chat_postMessage.assert_called_once_with(30 channel=self.slack_client.channel, text=test_message31 )32 def test_send_message_exception(self) -> None:33 test_message = "test message"34 with mock.patch.object(self.slack_client, "web") as m_web:35 m_web.chat_postMessage.side_effect = (SlackRequestError("Boom!"))36 self.slack_client.send_message(test_message)37 self.assertListEqual(38 m_web.chat_postMessage.mock_calls,39 [mock.call(40 channel=self.slack_client.channel,41 text=test_message,42 )] * self.slack_client.retries43 )44 self.assertListEqual(45 self._mock_log().error.mock_calls,46 [mock.call("Failed to send message: '%s'", test_message)] *47 self.slack_client.retries48 )49 def test_send_file(self) -> None:50 test_file = "/path/to/mock/file.txt"51 with mock.patch.object(self.slack_client, "web") as m_web:52 self.slack_client.send_file(test_file)53 m_web.files_upload.assert_called_once_with(54 channels=self.slack_client.channel,55 file=test_file,56 title=self.slack_client.config.upload_file_title57 )58 def test_send_file_exception(self) -> None:59 test_file = "/path/to/mock/file.txt"60 with mock.patch.object(self.slack_client, "web") as m_web:61 m_web.files_upload.side_effect = (SlackRequestError("Boom!"))62 self.slack_client.send_file(test_file)63 self.assertListEqual(64 m_web.files_upload.mock_calls, [65 mock.call(66 channels=self.slack_client.channel,67 file=test_file,68 title=self.slack_client.config.upload_file_title69 )70 ] * self.slack_client.retries71 )72 self.assertListEqual(73 self._mock_log().error.mock_calls,74 [mock.call("Failed to send file: '%s'", test_file)] *75 self.slack_client.retries76 )77 def test_send_snapshot(self) -> None:78 test_snapshot = "/path/to/mock/snapshot.jpg"79 with mock.patch.object(self.slack_client, "send_file") as m_send:80 self.slack_client.send_snapshot(test_snapshot)81 m_send.assert_called_once_with(test_snapshot)82 self._mock_motion_client().cleanup_snapshot.assert_called_once_with(83 test_snapshot,84 )85 def test_send_snapshot_exception(self) -> None:86 test_snapshot = "/path/to/mock/snapshot.jpg"87 with mock.patch.object(self.slack_client, "send_file") as m_send:88 with mock.patch.object(self.slack_client, "web") as m_web:89 self._mock_motion_client(90 ).cleanup_snapshot.side_effect = (motion.MotionException("Boom!"),)91 self.slack_client.send_snapshot(test_snapshot)92 m_send.assert_called_once_with(test_snapshot)93 self._mock_motion_client().cleanup_snapshot.assert_called_once_with(94 test_snapshot,95 )96 m_web.chat_postMessage.assert_called_once_with(97 channel=self.slack_client.channel,98 text="An error occurred cleaning up this snapshot.",99 )100 self._mock_log().error.assert_called_once_with(101 'Failed to remove old motion snapshot!',102 )103 def test_send_video(self) -> None:104 test_video = "/path/to/mock/video.mp4"105 with mock.patch.object(self.slack_client, "send_file") as m_send:106 self.slack_client.send_video(test_video)107 m_send.assert_called_once_with(test_video)108 self._mock_motion_client().archive_video.assert_called_once_with(test_video)109 def test_send_video_exception(self) -> None:110 test_video = "/path/to/mock/video.mp4"111 with mock.patch.object(self.slack_client, "send_file") as m_send:112 with mock.patch.object(self.slack_client, "web") as m_web:113 self._mock_motion_client(114 ).archive_video.side_effect = (motion.MotionException("Boom!"),)115 self.slack_client.send_video(test_video)116 m_send.assert_called_once_with(test_video)117 self._mock_motion_client().archive_video.assert_called_once_with(test_video)118 m_web.chat_postMessage.assert_called_once_with(119 channel=self.slack_client.channel,120 text="An error occurred archiving this video.",121 )122 self._mock_log().error.assert_called_once_with(123 "Failed to archive motion video capture!",...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!