diff options
Diffstat (limited to 'ansible_collections/purestorage/fusion/tests')
21 files changed, 311 insertions, 38 deletions
diff --git a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_api_client.py b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_api_client.py index 77f753656..295c62bd6 100644 --- a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_api_client.py +++ b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_api_client.py @@ -147,7 +147,19 @@ def test_api_client_create(m_im_api, current_clients): api_obj = MagicMock() api_obj.list_api_clients = MagicMock(return_value=current_clients) api_obj.get_api_client = MagicMock(side_effect=purefusion.rest.ApiException) - api_obj.create_api_client = MagicMock() + api_obj.create_api_client = MagicMock( + return_value=FakeApiClient( + "321321", + "self_link_value", + "client_test", + "client_test", + "apikey:name:test", + "321321", + 321321, + 321321, + "321321", + ) + ) api_obj.delete_api_client = MagicMock() m_im_api.return_value = api_obj @@ -156,6 +168,7 @@ def test_api_client_create(m_im_api, current_clients): fusion_api_client.main() assert exc.value.changed is True + assert exc.value.id == "321321" # check api was called correctly api_obj.list_api_clients.assert_called_once_with() diff --git a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_array.py b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_array.py index 0343bb1dc..6af1e1136 100644 --- a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_array.py +++ b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_array.py @@ -22,6 +22,7 @@ from ansible_collections.purestorage.fusion.tests.functional.utils import ( FailedOperationMock, OperationMock, SuccessfulOperationMock, + FAKE_RESOURCE_ID, exit_json, fail_json, set_module_args, @@ -46,6 +47,7 @@ def module_args(): "region": "region1", "availability_zone": "az1", "appliance_id": "23984573498573", + "apartment_id": "76586785687", "host_name": "array_1", "hardware_type": "flash-array-x", "maintenance_mode": False, @@ -65,7 +67,7 @@ def current_array(module_args): "region": module_args["region"], "availability_zone": module_args["availability_zone"], "appliance_id": module_args["appliance_id"], - "apartment_id": "76586785687", + "apartment_id": module_args["apartment_id"], "host_name": module_args["host_name"], "hardware_type": module_args["hardware_type"], "maintenance_mode": module_args["maintenance_mode"], @@ -332,7 +334,7 @@ def test_array_create(m_array_api, m_op_api, hw_type, main_m, unav_m, module_arg "region": module_args["region"], "availability_zone": module_args["availability_zone"], "appliance_id": module_args["appliance_id"], - "apartment_id": "76586785687", + "apartment_id": module_args["apartment_id"], "host_name": module_args["host_name"], "hardware_type": module_args["hardware_type"], "maintenance_mode": not module_args[ @@ -364,6 +366,7 @@ def test_array_create(m_array_api, m_op_api, hw_type, main_m, unav_m, module_arg fusion_array.main() assert exc.value.changed + assert exc.value.id == FAKE_RESOURCE_ID # check api was called correctly api_obj.get_array.assert_called_with( @@ -378,6 +381,7 @@ def test_array_create(m_array_api, m_op_api, hw_type, main_m, unav_m, module_arg host_name=module_args["host_name"], name=module_args["name"], appliance_id=module_args["appliance_id"], + apartment_id=module_args["apartment_id"], ), availability_zone_name=module_args["availability_zone"], region_name=module_args["region"], @@ -429,7 +433,7 @@ def test_array_create_without_display_name(m_array_api, m_op_api, module_args): "region": module_args["region"], "availability_zone": module_args["availability_zone"], "appliance_id": module_args["appliance_id"], - "apartment_id": "76586785687", + "apartment_id": module_args["apartment_id"], "host_name": module_args["host_name"], "hardware_type": module_args["hardware_type"], "maintenance_mode": not module_args["maintenance_mode"], @@ -457,6 +461,7 @@ def test_array_create_without_display_name(m_array_api, m_op_api, module_args): fusion_array.main() assert exc.value.changed + assert exc.value.id == FAKE_RESOURCE_ID # check api was called correctly api_obj.get_array.assert_called_with( @@ -471,6 +476,7 @@ def test_array_create_without_display_name(m_array_api, m_op_api, module_args): host_name=module_args["host_name"], name=module_args["name"], appliance_id=module_args["appliance_id"], + apartment_id=module_args["apartment_id"], ), availability_zone_name=module_args["availability_zone"], region_name=module_args["region"], @@ -554,6 +560,7 @@ def test_array_create_exception( host_name=module_args["host_name"], name=module_args["name"], appliance_id=module_args["appliance_id"], + apartment_id=module_args["apartment_id"], ), availability_zone_name=module_args["availability_zone"], region_name=module_args["region"], @@ -583,7 +590,7 @@ def test_array_create_second_exception( "region": module_args["region"], "availability_zone": module_args["availability_zone"], "appliance_id": module_args["appliance_id"], - "apartment_id": "76586785687", + "apartment_id": module_args["apartment_id"], "host_name": module_args["host_name"], "hardware_type": module_args["hardware_type"], "maintenance_mode": not module_args["maintenance_mode"], @@ -623,6 +630,7 @@ def test_array_create_second_exception( host_name=module_args["host_name"], name=module_args["name"], appliance_id=module_args["appliance_id"], + apartment_id=module_args["apartment_id"], ), availability_zone_name=module_args["availability_zone"], region_name=module_args["region"], @@ -667,6 +675,7 @@ def test_array_create_op_fails(m_array_api, m_op_api, module_args): host_name=module_args["host_name"], name=module_args["name"], appliance_id=module_args["appliance_id"], + apartment_id=module_args["apartment_id"], ), availability_zone_name=module_args["availability_zone"], region_name=module_args["region"], @@ -687,7 +696,7 @@ def test_array_create_second_op_fails(m_array_api, m_op_api, module_args): "region": module_args["region"], "availability_zone": module_args["availability_zone"], "appliance_id": module_args["appliance_id"], - "apartment_id": "76586785687", + "apartment_id": module_args["apartment_id"], "host_name": module_args["host_name"], "hardware_type": module_args["hardware_type"], "maintenance_mode": not module_args["maintenance_mode"], @@ -729,6 +738,7 @@ def test_array_create_second_op_fails(m_array_api, m_op_api, module_args): host_name=module_args["host_name"], name=module_args["name"], appliance_id=module_args["appliance_id"], + apartment_id=module_args["apartment_id"], ), availability_zone_name=module_args["availability_zone"], region_name=module_args["region"], @@ -787,6 +797,7 @@ def test_array_create_op_exception( host_name=module_args["host_name"], name=module_args["name"], appliance_id=module_args["appliance_id"], + apartment_id=module_args["apartment_id"], ), availability_zone_name=module_args["availability_zone"], region_name=module_args["region"], @@ -816,7 +827,7 @@ def test_array_create_second_op_exception( "region": module_args["region"], "availability_zone": module_args["availability_zone"], "appliance_id": module_args["appliance_id"], - "apartment_id": "76586785687", + "apartment_id": module_args["apartment_id"], "host_name": module_args["host_name"], "hardware_type": module_args["hardware_type"], "maintenance_mode": not module_args["maintenance_mode"], @@ -858,6 +869,7 @@ def test_array_create_second_op_exception( host_name=module_args["host_name"], name=module_args["name"], appliance_id=module_args["appliance_id"], + apartment_id=module_args["apartment_id"], ), availability_zone_name=module_args["availability_zone"], region_name=module_args["region"], @@ -899,6 +911,7 @@ def test_array_update(m_array_api, m_op_api, module_args, current_array): fusion_array.main() assert exc.value.changed + assert exc.value.id == current_array["id"] # check api was called correctly api_obj.get_array.assert_called_with( diff --git a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_az.py b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_az.py index c49f958a2..d19e41827 100644 --- a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_az.py +++ b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_az.py @@ -22,6 +22,7 @@ from ansible_collections.purestorage.fusion.tests.functional.utils import ( FailedOperationMock, OperationMock, SuccessfulOperationMock, + FAKE_RESOURCE_ID, exit_json, fail_json, set_module_args, @@ -139,6 +140,7 @@ def test_az_create(m_az_api, m_op_api): fusion_az.main() assert exc.value.changed + assert exc.value.id == FAKE_RESOURCE_ID api_obj.get_region.get_availability_zone( availability_zone_name=module_args["name"], @@ -186,6 +188,7 @@ def test_az_create_without_display_name(m_az_api, m_op_api): fusion_az.main() assert exc.value.changed + assert exc.value.id == FAKE_RESOURCE_ID api_obj.get_region.get_availability_zone( availability_zone_name=module_args["name"], diff --git a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_hap.py b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_hap.py index 6491c71da..258ca2034 100644 --- a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_hap.py +++ b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_hap.py @@ -23,6 +23,7 @@ from ansible_collections.purestorage.fusion.tests.functional.utils import ( FailedOperationMock, OperationMock, SuccessfulOperationMock, + FAKE_RESOURCE_ID, exit_json, fail_json, set_module_args, @@ -295,6 +296,7 @@ def test_hap_create(m_hap_api, m_op_api, module_args, current_hap_list): fusion_hap.main() assert exc.value.changed is True + assert exc.value.id == FAKE_RESOURCE_ID # check api was called correctly api_obj.list_host_access_policies.assert_called_once_with() @@ -341,6 +343,7 @@ def test_hap_create_without_display_name( fusion_hap.main() assert exc.value.changed is True + assert exc.value.id == FAKE_RESOURCE_ID # check api was called correctly api_obj.list_host_access_policies.assert_called_once_with() diff --git a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_info.py b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_info.py index 784b550cd..c542cddc0 100644 --- a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_info.py +++ b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_info.py @@ -847,6 +847,7 @@ RESP_VS = purefusion.VolumeSnapshotList( @patch.dict(os.environ, {"TZ": "UTC"}) +@patch.dict(os.environ, {"LC_TIME": "en_US.utf8"}) @patch("fusion.DefaultApi") @patch("fusion.IdentityManagerApi") @patch("fusion.ProtectionPoliciesApi") diff --git a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_nig.py b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_nig.py index 3a7b7ca5c..e8a2eb0ac 100644 --- a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_nig.py +++ b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_nig.py @@ -22,6 +22,7 @@ from ansible_collections.purestorage.fusion.tests.functional.utils import ( FailedOperationMock, OperationMock, SuccessfulOperationMock, + FAKE_RESOURCE_ID, exit_json, fail_json, set_module_args, @@ -236,6 +237,7 @@ def test_nig_create(m_nig_api, m_op_api): fusion_nig.main() assert exc.value.changed is True + assert exc.value.id == FAKE_RESOURCE_ID # check api was called correctly api_obj.get_network_interface_group.assert_called_once_with( @@ -299,6 +301,7 @@ def test_nig_create_without_display_name(m_nig_api, m_op_api): fusion_nig.main() assert exc.value.changed is True + assert exc.value.id == FAKE_RESOURCE_ID # check api was called correctly api_obj.get_network_interface_group.assert_called_once_with( @@ -362,6 +365,7 @@ def test_nig_create_without_gateway(m_nig_api, m_op_api): fusion_nig.main() assert exc.value.changed is True + assert exc.value.id == FAKE_RESOURCE_ID # check api was called correctly api_obj.get_network_interface_group.assert_called_once_with( @@ -631,6 +635,7 @@ def test_nig_update(m_nig_api, m_op_api): fusion_nig.main() assert exc.value.changed is True + assert exc.value.id == current_nig.id # check api was called correctly api_obj.get_network_interface_group.assert_called_once_with( diff --git a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_pg.py b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_pg.py index 2f0601e12..2a9419a8e 100644 --- a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_pg.py +++ b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_pg.py @@ -20,6 +20,7 @@ from ansible_collections.purestorage.fusion.tests.functional.utils import ( AnsibleExitJson, AnsibleFailJson, OperationMock, + FAKE_RESOURCE_ID, exit_json, fail_json, set_module_args, @@ -221,6 +222,7 @@ def test_pg_create_ok(pg_api_init, op_api_init, module_args_present): with pytest.raises(AnsibleExitJson) as excinfo: fusion_pg.main() assert excinfo.value.changed + assert excinfo.value.id == FAKE_RESOURCE_ID pg_mock.get_placement_group.assert_called_with( tenant_name="tenant1", @@ -265,6 +267,7 @@ def test_pg_create_without_display_name_ok( with pytest.raises(AnsibleExitJson) as excinfo: fusion_pg.main() assert excinfo.value.changed + assert excinfo.value.id == FAKE_RESOURCE_ID pg_mock.get_placement_group.assert_called_with( tenant_name="tenant1", @@ -450,6 +453,7 @@ def test_pg_create_triggers_update_ok(pg_api_init, op_api_init): with pytest.raises(AnsibleExitJson) as excinfo: fusion_pg.main() assert excinfo.value.changed + assert excinfo.value.id == FAKE_RESOURCE_ID pg_mock.get_placement_group.assert_has_calls( [ @@ -946,6 +950,7 @@ def test_pg_update_ok(pg_api_init, op_api_init, test_case): with pytest.raises(AnsibleExitJson) as excinfo: fusion_pg.main() assert excinfo.value.changed + assert excinfo.value.id == test_case["current_state"].id pg_mock.get_placement_group.assert_called_with( tenant_name="tenant1", diff --git a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_pp.py b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_pp.py index 519caea40..359d4ca7e 100644 --- a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_pp.py +++ b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_pp.py @@ -20,6 +20,7 @@ from ansible_collections.purestorage.fusion.tests.functional.utils import ( AnsibleExitJson, AnsibleFailJson, OperationMock, + FAKE_RESOURCE_ID, exit_json, fail_json, set_module_args, @@ -39,7 +40,7 @@ basic.AnsibleModule.fail_json = fail_json def module_args_present(): return { "name": "protection_policy1", - "local_rpo": 43, + "local_rpo": "1H43M", "local_retention": "2H", "state": "present", "issuer_id": "ABCD1234", @@ -181,6 +182,7 @@ def test_pp_create_ok(pp_api_init, op_api_init, module_args_present): with pytest.raises(AnsibleExitJson) as excinfo: fusion_pp.main() assert excinfo.value.changed + assert excinfo.value.id == FAKE_RESOURCE_ID pp_mock.get_protection_policy.assert_called_with( protection_policy_name="protection_policy1" @@ -190,7 +192,7 @@ def test_pp_create_ok(pp_api_init, op_api_init, module_args_present): name="protection_policy1", display_name="some_display_name", objectives=[ - purefusion.RPO(type="RPO", rpo="PT43M"), + purefusion.RPO(type="RPO", rpo="PT103M"), purefusion.Retention(type="Retention", after="PT120M"), ], ) @@ -220,6 +222,7 @@ def test_pp_create_without_display_name_ok( with pytest.raises(AnsibleExitJson) as excinfo: fusion_pp.main() assert excinfo.value.changed + assert excinfo.value.id == FAKE_RESOURCE_ID pp_mock.get_protection_policy.assert_called_with( protection_policy_name="protection_policy1" @@ -229,7 +232,7 @@ def test_pp_create_without_display_name_ok( name="protection_policy1", display_name="protection_policy1", objectives=[ - purefusion.RPO(type="RPO", rpo="PT43M"), + purefusion.RPO(type="RPO", rpo="PT103M"), purefusion.Retention(type="Retention", after="PT120M"), ], ) @@ -274,7 +277,7 @@ def test_pp_create_exception( name="protection_policy1", display_name="protection_policy1", objectives=[ - purefusion.RPO(type="RPO", rpo="PT43M"), + purefusion.RPO(type="RPO", rpo="PT103M"), purefusion.Retention(type="Retention", after="PT120M"), ], ) @@ -310,7 +313,7 @@ def test_pp_create_op_fails(pp_api_init, op_api_init, module_args_present): name="protection_policy1", display_name="protection_policy1", objectives=[ - purefusion.RPO(type="RPO", rpo="PT43M"), + purefusion.RPO(type="RPO", rpo="PT103M"), purefusion.Retention(type="Retention", after="PT120M"), ], ) @@ -333,7 +336,7 @@ def test_pp_delete_ok(pp_api_init, op_api_init, module_args_absent): display_name="protection_policy1_display_name", self_link="test_self_link", objectives=[ - purefusion.RPO(type="RPO", rpo="PT43M"), + purefusion.RPO(type="RPO", rpo="PT103M"), purefusion.Retention(type="Retention", after="PT120M"), ], ) @@ -385,7 +388,7 @@ def test_pp_delete_exception( display_name="protection_policy1_display_name", self_link="test_self_link", objectives=[ - purefusion.RPO(type="RPO", rpo="PT43M"), + purefusion.RPO(type="RPO", rpo="PT103M"), purefusion.Retention(type="Retention", after="PT120M"), ], ) @@ -425,7 +428,7 @@ def test_pp_delete_op_fails(pp_api_init, op_api_init, module_args_absent): display_name="protection_policy1_display_name", self_link="test_self_link", objectives=[ - purefusion.RPO(type="RPO", rpo="PT43M"), + purefusion.RPO(type="RPO", rpo="PT103M"), purefusion.Retention(type="Retention", after="PT120M"), ], ) @@ -459,7 +462,7 @@ def test_pp_present_not_changed(pp_api_init, op_api_init): module_args = { "name": "protection_policy1", "display_name": "some_display_name", - "local_rpo": 43, + "local_rpo": "43M", "local_retention": "2H", "state": "present", "issuer_id": "ABCD1234", diff --git a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_ra.py b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_ra.py index 6456fa7d7..d8cac74a5 100644 --- a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_ra.py +++ b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_ra.py @@ -20,6 +20,7 @@ from ansible_collections.purestorage.fusion.tests.functional.utils import ( AnsibleExitJson, AnsibleFailJson, OperationMock, + FAKE_RESOURCE_ID, exit_json, fail_json, set_module_args, @@ -330,6 +331,7 @@ def test_ra_create_ok(ra_api_init, im_api_init, op_api_init, args_and_scope): with pytest.raises(AnsibleExitJson) as excinfo: fusion_ra.main() assert excinfo.value.changed + assert excinfo.value.id == FAKE_RESOURCE_ID ra_mock.list_role_assignments.assert_called_with( role_name=module_args["role"], principal="principal1" diff --git a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_region.py b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_region.py index 6b13adecf..42d14d56e 100644 --- a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_region.py +++ b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_region.py @@ -22,6 +22,7 @@ from ansible_collections.purestorage.fusion.tests.functional.utils import ( FailedOperationMock, OperationMock, SuccessfulOperationMock, + FAKE_RESOURCE_ID, exit_json, fail_json, set_module_args, @@ -126,6 +127,7 @@ def test_region_create(m_region_api, m_op_api): fusion_region.main() assert exc.value.changed + assert exc.value.id == FAKE_RESOURCE_ID # check api was called correctly api_obj.get_region.assert_called_once_with(region_name=module_args["name"]) @@ -168,6 +170,7 @@ def test_region_create_without_display_name(m_region_api, m_op_api): fusion_region.main() assert exc.value.changed + assert exc.value.id == FAKE_RESOURCE_ID # check api was called correctly api_obj.get_region.assert_called_once_with(region_name=module_args["name"]) @@ -354,6 +357,7 @@ def test_region_update(m_region_api, m_op_api): fusion_region.main() assert exc.value.changed + assert exc.value.id == current_region["id"] # check api was called correctly api_obj.get_region.assert_called_once_with(region_name=module_args["name"]) diff --git a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_sc.py b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_sc.py index 1a2db191c..4d44e7fcb 100644 --- a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_sc.py +++ b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_sc.py @@ -22,6 +22,7 @@ from ansible_collections.purestorage.fusion.tests.functional.utils import ( FailedOperationMock, OperationMock, SuccessfulOperationMock, + FAKE_RESOURCE_ID, exit_json, fail_json, set_module_args, @@ -167,6 +168,7 @@ def test_sc_create( fusion_sc.main() assert exc.value.changed + assert exc.value.id == FAKE_RESOURCE_ID # check api was called correctly api_obj.get_storage_class.assert_called_once_with( @@ -224,6 +226,7 @@ def test_sc_create_without_display_name(m_sc_api, m_op_api): fusion_sc.main() assert exc.value.changed + assert exc.value.id == FAKE_RESOURCE_ID # check api was called correctly api_obj.get_storage_class.assert_called_once_with( @@ -608,6 +611,7 @@ def test_sc_update(m_sc_api, m_op_api): fusion_sc.main() assert exc.value.changed + assert exc.value.id == current_sc["id"] # check api was called correctly api_obj.get_storage_class.assert_called_once_with( diff --git a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_se.py b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_se.py index a071190db..9d9559c12 100644 --- a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_se.py +++ b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_se.py @@ -22,6 +22,7 @@ from ansible_collections.purestorage.fusion.tests.functional.utils import ( FailedOperationMock, OperationMock, SuccessfulOperationMock, + FAKE_RESOURCE_ID, exit_json, fail_json, set_module_args, @@ -285,6 +286,7 @@ def test_se_create_iscsi(m_se_api, m_op_api, module_args): fusion_se.main() assert exc.value.changed + assert exc.value.id == FAKE_RESOURCE_ID # check api was called correctly api_obj.get_storage_endpoint.assert_called_once_with( @@ -341,6 +343,7 @@ def test_se_create_cbs_azure_iscsi(m_se_api, m_op_api, module_args): fusion_se.main() assert exc.value.changed is True + assert exc.value.id == FAKE_RESOURCE_ID # check api was called correctly api_obj.get_storage_endpoint.assert_called_once_with( @@ -395,6 +398,7 @@ def test_se_create_without_display_name(m_se_api, m_op_api, module_args): fusion_se.main() assert exc.value.changed + assert exc.value.id == FAKE_RESOURCE_ID # check api was called correctly api_obj.get_storage_endpoint.assert_called_once_with( @@ -610,6 +614,7 @@ def test_se_update(m_se_api, m_op_api, module_args, current_se): fusion_se.main() assert exc.value.changed + assert exc.value.id == current_se["id"] # check api was called correctly api_obj.get_storage_endpoint.assert_called_once_with( diff --git a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_ss.py b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_ss.py index d784b1a52..f1514b8e6 100644 --- a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_ss.py +++ b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_ss.py @@ -22,6 +22,7 @@ from ansible_collections.purestorage.fusion.tests.functional.utils import ( FailedOperationMock, OperationMock, SuccessfulOperationMock, + FAKE_RESOURCE_ID, exit_json, fail_json, set_module_args, @@ -139,6 +140,7 @@ def test_ss_create(m_ss_api, m_op_api): fusion_ss.main() assert exc.value.changed + assert exc.value.id == FAKE_RESOURCE_ID # check api was called correctly api_obj.get_storage_service.assert_called_once_with( @@ -186,6 +188,7 @@ def test_ss_create_without_display_name(m_ss_api, m_op_api): fusion_ss.main() assert exc.value.changed + assert exc.value.id == FAKE_RESOURCE_ID # check api was called correctly api_obj.get_storage_service.assert_called_once_with( @@ -434,6 +437,7 @@ def test_ss_update(m_ss_api, m_op_api): fusion_ss.main() assert exc.value.changed + assert exc.value.id == current_ss["id"] # check api was called correctly api_obj.get_storage_service.assert_called_once_with( diff --git a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_tenant.py b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_tenant.py index bb0521b01..11cd71171 100644 --- a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_tenant.py +++ b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_tenant.py @@ -22,6 +22,7 @@ from ansible_collections.purestorage.fusion.tests.functional.utils import ( FailedOperationMock, OperationMock, SuccessfulOperationMock, + FAKE_RESOURCE_ID, exit_json, fail_json, set_module_args, @@ -126,6 +127,7 @@ def test_tenant_create(m_tenant_api, m_op_api): fusion_tenant.main() assert exc.value.changed + assert exc.value.id == FAKE_RESOURCE_ID # check api was called correctly api_obj.get_tenant.assert_called_once_with(tenant_name=module_args["name"]) @@ -169,6 +171,7 @@ def test_tenant_create_without_display_name(m_tenant_api, m_op_api): fusion_tenant.main() assert exc.value.changed + assert exc.value.id == FAKE_RESOURCE_ID # check api was called correctly api_obj.get_tenant.assert_called_once_with(tenant_name=module_args["name"]) @@ -359,6 +362,7 @@ def test_tenant_update(m_tenant_api, m_op_api): fusion_tenant.main() assert exc.value.changed + assert exc.value.id == current_tenant["id"] # check api was called correctly api_obj.get_tenant.assert_called_once_with(tenant_name=module_args["name"]) diff --git a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_ts.py b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_ts.py index 0d9cbb25a..0e1260858 100644 --- a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_ts.py +++ b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_ts.py @@ -22,6 +22,7 @@ from ansible_collections.purestorage.fusion.tests.functional.utils import ( FailedOperationMock, OperationMock, SuccessfulOperationMock, + FAKE_RESOURCE_ID, exit_json, fail_json, set_module_args, @@ -138,6 +139,7 @@ def test_ts_create(m_ts_api, m_op_api): fusion_ts.main() assert exc.value.changed + assert exc.value.id == FAKE_RESOURCE_ID # check api was called correctly api_obj.get_tenant_space.assert_called_once_with( @@ -186,6 +188,7 @@ def test_ts_create_without_display_name(m_ts_api, m_op_api): fusion_ts.main() assert exc.value.changed + assert exc.value.id == FAKE_RESOURCE_ID # check api was called correctly api_obj.get_tenant_space.assert_called_once_with( @@ -399,6 +402,7 @@ def test_ts_update(m_ts_api, m_op_api): fusion_ts.main() assert exc.value.changed + assert exc.value.id == current_ts["id"] # check api was called correctly api_obj.get_tenant_space.assert_called_once_with( diff --git a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_volume.py b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_volume.py index 592bda32e..43f69666e 100644 --- a/ansible_collections/purestorage/fusion/tests/functional/test_fusion_volume.py +++ b/ansible_collections/purestorage/fusion/tests/functional/test_fusion_volume.py @@ -7,7 +7,7 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type -from unittest.mock import MagicMock, patch +from unittest.mock import MagicMock, call, patch import fusion as purefusion import pytest @@ -23,6 +23,7 @@ from ansible_collections.purestorage.fusion.tests.functional.utils import ( AnsibleFailJson, OperationMock, SuccessfulOperationMock, + FAKE_RESOURCE_ID, exit_json, fail_json, set_module_args, @@ -126,7 +127,7 @@ def destroyed_volume(volume): ), ( "size", - "missing required arguments: size", + "Either `size`, `source_volume` or `source_snapshot` parameter is required when creating a volume.", ), ], ) @@ -164,6 +165,18 @@ def test_module_fails_on_missing_parameters( {"size": "1K"}, "Size is not within the required range", ), + ( + {"source_volume": "vol_name"}, + "parameters are mutually exclusive: source_volume|source_snapshot|size", + ), + ( + {"source_snapshot": "snap_name"}, + "parameters are mutually exclusive: source_volume|source_snapshot|size", + ), + ( + {"source_volume_snapshot": "vol_snap_name"}, + "parameters are required together: source_snapshot, source_volume_snapshot", + ), ], ) def test_module_fails_on_incorrect_parameters( @@ -216,6 +229,8 @@ def test_volume_create_successfully(mock_volumes_api, mock_operations_api, modul with pytest.raises(AnsibleExitJson) as exception: fusion_volume.main() assert exception.value.changed is True + assert exception.value.id == FAKE_RESOURCE_ID + volumes_api.get_volume.assert_called_with( volume_name=module_args["name"], tenant_name=module_args["tenant"], @@ -238,6 +253,90 @@ def test_volume_create_successfully(mock_volumes_api, mock_operations_api, modul @patch("fusion.OperationsApi") @patch("fusion.VolumesApi") +def test_volume_create_from_volume_successfully( + mock_volumes_api, mock_operations_api, module_args +): + del module_args["size"] + module_args["source_volume"] = "source_volume_name" + + operations_api = purefusion.OperationsApi() + volumes_api = purefusion.VolumesApi() + volumes_api.get_volume = MagicMock(side_effect=purefusion.rest.ApiException) + volumes_api.create_volume = MagicMock(return_value=OperationMock(1)) + operations_api.get_operation = MagicMock(return_value=SuccessfulOperationMock) + mock_volumes_api.return_value = volumes_api + mock_operations_api.return_value = operations_api + set_module_args(module_args) + # run module + with pytest.raises(AnsibleExitJson) as exception: + fusion_volume.main() + assert exception.value.changed is True + assert exception.value.id == FAKE_RESOURCE_ID + volumes_api.get_volume.assert_called_with( + volume_name=module_args["name"], + tenant_name=module_args["tenant"], + tenant_space_name=module_args["tenant_space"], + ) + volumes_api.create_volume.assert_called_once_with( + purefusion.VolumePost( + source_link=f"/tenants/{module_args['tenant']}/tenant-spaces/{module_args['tenant_space']}/volumes/{module_args['source_volume']}", + storage_class=module_args["storage_class"], + placement_group=module_args["placement_group"], + name=module_args["name"], + display_name=module_args["display_name"], + protection_policy=module_args["protection_policy"], + ), + tenant_name=module_args["tenant"], + tenant_space_name=module_args["tenant_space"], + ) + operations_api.get_operation.assert_called_once_with(1) + + +@patch("fusion.OperationsApi") +@patch("fusion.VolumesApi") +def test_volume_create_from_volume_snapshot_successfully( + mock_volumes_api, mock_operations_api, module_args +): + del module_args["size"] + module_args["source_snapshot"] = "source_snapshot_name" + module_args["source_volume_snapshot"] = "source_volume_snapshot_name" + + operations_api = purefusion.OperationsApi() + volumes_api = purefusion.VolumesApi() + volumes_api.get_volume = MagicMock(side_effect=purefusion.rest.ApiException) + volumes_api.create_volume = MagicMock(return_value=OperationMock(1)) + operations_api.get_operation = MagicMock(return_value=SuccessfulOperationMock) + mock_volumes_api.return_value = volumes_api + mock_operations_api.return_value = operations_api + set_module_args(module_args) + # run module + with pytest.raises(AnsibleExitJson) as exception: + fusion_volume.main() + assert exception.value.changed is True + assert exception.value.id == FAKE_RESOURCE_ID + volumes_api.get_volume.assert_called_with( + volume_name=module_args["name"], + tenant_name=module_args["tenant"], + tenant_space_name=module_args["tenant_space"], + ) + volumes_api.create_volume.assert_called_once_with( + purefusion.VolumePost( + source_link=f"/tenants/{module_args['tenant']}/tenant-spaces/{module_args['tenant_space']}/snapshots/" + f"{module_args['source_snapshot']}/volume-snapshots/{module_args['source_volume_snapshot']}", + storage_class=module_args["storage_class"], + placement_group=module_args["placement_group"], + name=module_args["name"], + display_name=module_args["display_name"], + protection_policy=module_args["protection_policy"], + ), + tenant_name=module_args["tenant"], + tenant_space_name=module_args["tenant_space"], + ) + operations_api.get_operation.assert_called_once_with(1) + + +@patch("fusion.OperationsApi") +@patch("fusion.VolumesApi") def test_volume_create_without_display_name_successfully( mock_volumes_api, mock_operations_api, module_args ): @@ -254,6 +353,7 @@ def test_volume_create_without_display_name_successfully( with pytest.raises(AnsibleExitJson) as exception: fusion_volume.main() assert exception.value.changed is True + assert exception.value.id == FAKE_RESOURCE_ID volumes_api.get_volume.assert_called_with( volume_name=module_args["name"], tenant_name=module_args["tenant"], @@ -399,6 +499,7 @@ def test_volume_update_with_state_present_executed_correctly( with pytest.raises(AnsibleExitJson) as exception: fusion_volume.main() assert exception.value.changed is True + assert exception.value.id == volume["id"] volumes_api.get_volume.assert_called_with( volume_name=module_args["name"], tenant_name=module_args["tenant"], @@ -447,6 +548,7 @@ def test_volume_update_with_state_absent_executed_correctly( with pytest.raises(AnsibleExitJson) as exception: fusion_volume.main() assert exception.value.changed is True + assert exception.value.id == volume["id"] volumes_api.get_volume.assert_called_with( volume_name=module_args["name"], tenant_name=module_args["tenant"], @@ -713,3 +815,47 @@ def test_volume_delete_operation_throws_exception( tenant_space_name=absent_module_args["tenant_space"], ) operations_api.get_operation.assert_called_once_with(2) + + +@patch("fusion.OperationsApi") +@patch("fusion.VolumesApi") +def test_module_updates_on_empty_array_of_haps( + mock_volumes_api, mock_operations_api, module_args, volume +): + volumes_api = purefusion.VolumesApi() + operations_api = purefusion.OperationsApi() + volumes_api.get_volume = MagicMock(return_value=purefusion.Volume(**volume)) + volumes_api.update_volume = MagicMock(return_value=OperationMock(1)) + operations_api.get_operation = MagicMock(return_value=SuccessfulOperationMock) + mock_operations_api.return_value = operations_api + mock_volumes_api.return_value = volumes_api + module_args.update({"state": "absent", "host_access_policies": []}) + set_module_args(module_args) + # run module + with pytest.raises(AnsibleExitJson) as exception: + fusion_volume.main() + assert exception.value.changed is True + assert exception.value.id == volume["id"] + volumes_api.get_volume.assert_called_with( + volume_name=module_args["name"], + tenant_name=module_args["tenant"], + tenant_space_name=module_args["tenant_space"], + ) + volumes_api.update_volume.assert_has_calls( + [ + call( + purefusion.VolumePatch( + host_access_policies=purefusion.NullableString(",".join([])) + ), + volume_name=volume["name"], + tenant_name=volume["tenant"], + tenant_space_name=volume["tenant_space"], + ), + call( + purefusion.VolumePatch(destroyed=purefusion.NullableBoolean(True)), + volume_name=volume["name"], + tenant_name=volume["tenant"], + tenant_space_name=volume["tenant_space"], + ), + ] + ) diff --git a/ansible_collections/purestorage/fusion/tests/functional/utils.py b/ansible_collections/purestorage/fusion/tests/functional/utils.py index 24d6f0328..53e501bc0 100644 --- a/ansible_collections/purestorage/fusion/tests/functional/utils.py +++ b/ansible_collections/purestorage/fusion/tests/functional/utils.py @@ -7,6 +7,11 @@ from dataclasses import dataclass from ansible.module_utils import basic from ansible.module_utils.common.text.converters import to_bytes +from ansible_collections.purestorage.fusion.tests.helpers import ( + OperationResultsDict, +) + +FAKE_RESOURCE_ID = "fake-id-12345" @dataclass @@ -20,6 +25,9 @@ class OperationMock: self.status = "Pending" elif success: self.status = "Succeeded" + self.result = OperationResultsDict( + {"resource": OperationResultsDict({"id": FAKE_RESOURCE_ID})} + ) else: self.status = "Failed" self.id = id @@ -30,6 +38,9 @@ class SuccessfulOperationMock: Mock object for successful operation. This object is returned by mocked Operation API if the operation was successful. """ + result = OperationResultsDict( + {"resource": OperationResultsDict({"id": FAKE_RESOURCE_ID})} + ) status = "Succeeded" @@ -65,6 +76,10 @@ class AnsibleExitJson(Exception): return self.kwargs["changed"] @property + def id(self): + return self.kwargs["id"] + + @property def fusion_info(self): return self.kwargs["fusion_info"] if "fusion_info" in self.kwargs else None diff --git a/ansible_collections/purestorage/fusion/tests/helpers.py b/ansible_collections/purestorage/fusion/tests/helpers.py index 40d98cf0e..76d51b6f7 100644 --- a/ansible_collections/purestorage/fusion/tests/helpers.py +++ b/ansible_collections/purestorage/fusion/tests/helpers.py @@ -27,3 +27,11 @@ class ApiExceptionsMockGenerator: def create_not_found(): status = HTTPStatus.NOT_FOUND return purefusion.rest.ApiException(status=status, reason=status.phrase) + + +class OperationResultsDict(dict): + """dot.notation access to dictionary attributes""" + + __getattr__ = dict.get + __setattr__ = dict.__setitem__ + __delattr__ = dict.__delitem__ diff --git a/ansible_collections/purestorage/fusion/tests/unit/mocks/operation_mock.py b/ansible_collections/purestorage/fusion/tests/unit/mocks/operation_mock.py index 99487ddfa..a3a70c67d 100644 --- a/ansible_collections/purestorage/fusion/tests/unit/mocks/operation_mock.py +++ b/ansible_collections/purestorage/fusion/tests/unit/mocks/operation_mock.py @@ -8,6 +8,9 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type from enum import Enum +from ansible_collections.purestorage.fusion.tests.helpers import ( + OperationResultsDict, +) class OperationStatus(str, Enum): @@ -18,7 +21,16 @@ class OperationStatus(str, Enum): class OperationMock: - def __init__(self, id, status, retry_in=1): + def __init__( + self, + id, + status, + result=OperationResultsDict( + {"resource": OperationResultsDict({"id": "fake-id"})} + ), + retry_in=1, + ): self.id = id self.status = status self.retry_in = retry_in + self.result = result diff --git a/ansible_collections/purestorage/fusion/tests/unit/module_utils/test_parsing.py b/ansible_collections/purestorage/fusion/tests/unit/module_utils/test_parsing.py index 7e2a1cc78..230d0ff01 100644 --- a/ansible_collections/purestorage/fusion/tests/unit/module_utils/test_parsing.py +++ b/ansible_collections/purestorage/fusion/tests/unit/module_utils/test_parsing.py @@ -7,13 +7,12 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type +import pytest from ansible_collections.purestorage.fusion.plugins.module_utils.parsing import ( - parse_number_with_metric_suffix, parse_minutes, + parse_number_with_metric_suffix, ) -import pytest - class MockException(Exception): pass @@ -83,17 +82,37 @@ def test_parsing_invalid_number(): def test_parsing_valid_time_period(): module = MockModule() + assert parse_minutes(module, "0") == 0 + assert parse_minutes(module, "00") == 0 + assert parse_minutes(module, "00M") == 0 assert parse_minutes(module, "10") == 10 - assert parse_minutes(module, "2h") == 120 - assert parse_minutes(module, "2H") == 120 + assert parse_minutes(module, "015") == 15 + assert parse_minutes(module, "0023") == 23 + assert parse_minutes(module, "0H10M") == 10 + assert parse_minutes(module, "2h") == 2 * 60 + assert parse_minutes(module, "2H") == 2 * 60 + assert parse_minutes(module, "02h") == 2 * 60 + assert parse_minutes(module, "02H") == 2 * 60 + assert parse_minutes(module, "002h") == 2 * 60 + assert parse_minutes(module, "002H") == 2 * 60 + assert parse_minutes(module, "0D10H10M") == 10 * 60 + 10 assert parse_minutes(module, "14D") == 14 * 24 * 60 + assert parse_minutes(module, "014D") == 14 * 24 * 60 + assert parse_minutes(module, "0000014D") == 14 * 24 * 60 assert parse_minutes(module, "1W") == 7 * 24 * 60 + assert parse_minutes(module, "01W") == 7 * 24 * 60 + assert parse_minutes(module, "01Y0H10M") == 365 * 24 * 60 + 10 assert parse_minutes(module, "12Y") == 12 * 365 * 24 * 60 + assert parse_minutes(module, "012Y") == 12 * 365 * 24 * 60 assert ( parse_minutes(module, "10Y20W30D40H50M") == 10 * 365 * 24 * 60 + 20 * 7 * 24 * 60 + 30 * 24 * 60 + 40 * 60 + 50 ) assert ( + parse_minutes(module, "010Y20W30D40H50M") + == 10 * 365 * 24 * 60 + 20 * 7 * 24 * 60 + 30 * 24 * 60 + 40 * 60 + 50 + ) + assert ( parse_minutes(module, "10Y20W30D40H") == 10 * 365 * 24 * 60 + 20 * 7 * 24 * 60 + 30 * 24 * 60 + 40 * 60 ) @@ -110,6 +129,10 @@ def test_parsing_valid_time_period(): assert parse_minutes(module, "40H50M") == 40 * 60 + 50 assert parse_minutes(module, "30D50M") == 30 * 24 * 60 + 50 assert parse_minutes(module, "20W40H") == 20 * 7 * 24 * 60 + 40 * 60 + assert ( + parse_minutes(module, "01W000010D10H10M") + == 7 * 24 * 60 + 10 * 24 * 60 + 10 * 60 + 10 + ) def test_parsing_invalid_time_period(): @@ -123,16 +146,8 @@ def test_parsing_invalid_time_period(): with pytest.raises(MockException): assert parse_minutes(module, "1V") with pytest.raises(MockException): - assert parse_minutes(module, "0M") - with pytest.raises(MockException): - assert parse_minutes(module, "0H10M") - with pytest.raises(MockException): - assert parse_minutes(module, "0H10M") + assert parse_minutes(module, "1v") with pytest.raises(MockException): - assert parse_minutes(module, "0D10H10M") + assert parse_minutes(module, "10M2H") with pytest.raises(MockException): - assert parse_minutes(module, "01W10D10H10M") - with pytest.raises(MockException): - assert parse_minutes(module, "01Y0H10M") - with pytest.raises(MockException): - assert parse_minutes(module, "1V") + assert parse_minutes(module, "0H10M01Y") diff --git a/ansible_collections/purestorage/fusion/tests/unit/modules/test_fusion_az.py b/ansible_collections/purestorage/fusion/tests/unit/modules/test_fusion_az.py index a384506d8..ee300638e 100644 --- a/ansible_collections/purestorage/fusion/tests/unit/modules/test_fusion_az.py +++ b/ansible_collections/purestorage/fusion/tests/unit/modules/test_fusion_az.py @@ -81,7 +81,9 @@ class TestCreateAZ: azone, region_name=module_params["region"] ) await_operation_mock.assert_called_once_with(fusion_mock, op) - moduleMock.exit_json.assert_called_once_with(changed=True) + moduleMock.exit_json.assert_called_once_with( + changed=True, id=op.result.resource.id + ) @patch(f"{current_module}.fusion_az.purefusion.AvailabilityZonesApi.__new__") @patch(f"{current_module}.fusion_az.await_operation") @@ -113,7 +115,7 @@ class TestCreateAZ: # Assertions mock_az_api_obj.create_availability_zone.assert_not_called() await_operation_mock.assert_not_called() - moduleMock.exit_json.assert_called_once_with(changed=True) + moduleMock.exit_json.assert_called_once_with(changed=True, id=None) @patch(f"{current_module}.fusion_az.purefusion.AvailabilityZonesApi.__new__") @patch(f"{current_module}.fusion_az.await_operation") @@ -151,7 +153,9 @@ class TestCreateAZ: azone, region_name=module_params["region"] ) await_operation_mock.assert_called_once_with(fusion_mock, op) - moduleMock.exit_json.assert_called_once_with(changed=True) + moduleMock.exit_json.assert_called_once_with( + changed=True, id=op.result.resource.id + ) @patch(f"{current_module}.fusion_az.purefusion.AvailabilityZonesApi.__new__") @patch(f"{current_module}.fusion_az.await_operation") |