conftest.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. """
  2. Copyright (c) Contributors to the Open 3D Engine Project.
  3. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. """
  6. import logging
  7. from os.path import abspath
  8. import pytest
  9. import typing
  10. from AWS.common.aws_utils import AwsUtils
  11. from AWS.common.aws_credentials import AwsCredentials
  12. from AWS.common.resource_mappings import ResourceMappings
  13. logger = logging.getLogger(__name__)
  14. @pytest.fixture(scope='function')
  15. def aws_utils(
  16. request: pytest.fixture,
  17. assume_role_arn: str,
  18. session_name: str,
  19. region_name: str):
  20. """
  21. Fixture for AWS util functions
  22. :param request: _pytest.fixtures.SubRequest class that handles getting
  23. a pytest fixture from a pytest function/fixture.
  24. :param assume_role_arn: Role used to fetch temporary aws credentials, configure service clients with obtained credentials.
  25. :param session_name: Session name to set.
  26. :param region_name: AWS account region to set for session.
  27. :return AWSUtils class object.
  28. """
  29. aws_utils_obj = AwsUtils(assume_role_arn, session_name, region_name)
  30. def teardown():
  31. aws_utils_obj.destroy()
  32. request.addfinalizer(teardown)
  33. return aws_utils_obj
  34. # Set global pytest variable for cdk to avoid recreating instance
  35. pytest.cdk_obj = None
  36. @pytest.fixture(scope='function')
  37. def resource_mappings(
  38. request: pytest.fixture,
  39. project: str,
  40. feature_name: str,
  41. resource_mappings_filename: str,
  42. stacks: typing.List,
  43. workspace: pytest.fixture,
  44. aws_utils: pytest.fixture) -> ResourceMappings:
  45. """
  46. Fixture for setting up resource mappings file.
  47. :param request: _pytest.fixtures.SubRequest class that handles getting
  48. a pytest fixture from a pytest function/fixture.
  49. :param project: Project to find resource mapping file.
  50. :param feature_name: AWS Gem name that is prepended to resource mapping keys.
  51. :param resource_mappings_filename: Name of resource mapping file.
  52. :param stacks: List of stack names to describe and populate resource mappings with.
  53. :param workspace: ly_test_tools workspace fixture.
  54. :param aws_utils: AWS utils fixture.
  55. :return: ResourceMappings class object.
  56. """
  57. path = f'{workspace.paths.engine_root()}/{project}/Config/{resource_mappings_filename}'
  58. logger.info(f'Resource mapping path : {path}')
  59. logger.info(f'Resource mapping resolved path : {abspath(path)}')
  60. resource_mappings_obj = ResourceMappings(abspath(path), aws_utils.assume_session().region_name, feature_name,
  61. aws_utils.assume_account_id(), aws_utils.client('cloudformation'))
  62. resource_mappings_obj.populate_output_keys(stacks)
  63. def teardown():
  64. resource_mappings_obj.clear_output_keys()
  65. request.addfinalizer(teardown)
  66. return resource_mappings_obj
  67. @pytest.fixture(scope='function')
  68. def aws_credentials(request: pytest.fixture, aws_utils: pytest.fixture, profile_name: str):
  69. """
  70. Fixture for setting up temporary AWS credentials from assume role.
  71. :param request: _pytest.fixtures.SubRequest class that handles getting
  72. a pytest fixture from a pytest function/fixture.
  73. :param aws_utils: aws_utils fixture.
  74. :param profile_name: Named AWS profile to store temporary credentials.
  75. """
  76. aws_credentials_obj = AwsCredentials(profile_name)
  77. original_access_key, original_secret_access_key, original_token = aws_credentials_obj.get_aws_credentials()
  78. aws_credentials_obj.set_aws_credentials_by_session(aws_utils.assume_session())
  79. def teardown():
  80. # Reset to the named profile using the original AWS credentials
  81. aws_credentials_obj.set_aws_credentials(original_access_key, original_secret_access_key, original_token)
  82. request.addfinalizer(teardown)
  83. return aws_credentials_obj