conftest.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. """
  2. All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
  3. its licensors.
  4. For complete copyright and license terms please see the LICENSE at the root of this
  5. distribution (the "License"). All use of this software is governed by the License,
  6. or, if provided, by the license below or the license accompanying this file. Do not
  7. remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. """
  10. import pytest
  11. import logging
  12. from AWS.common.aws_utils import AwsUtils
  13. from AWS.Windows.cdk.cdk_utils import Cdk
  14. logger = logging.getLogger(__name__)
  15. @pytest.fixture(scope='function')
  16. def aws_utils(
  17. request: pytest.fixture,
  18. assume_role_arn: str,
  19. session_name: str,
  20. region_name: str):
  21. """
  22. Fixture for AWS util functions
  23. :param request: _pytest.fixtures.SubRequest class that handles getting
  24. a pytest fixture from a pytest function/fixture.
  25. :param assume_role_arn: Role used to fetch temporary aws credentials, configure service clients with obtained credentials.
  26. :param session_name: Session name to set.
  27. :param region_name: AWS account region to set for session.
  28. :return AWSUtils class object.
  29. """
  30. aws_utils_obj = AwsUtils(assume_role_arn, session_name, region_name)
  31. def teardown():
  32. aws_utils_obj.destroy()
  33. request.addfinalizer(teardown)
  34. return aws_utils_obj
  35. # Set global pytest variable for cdk to avoid recreating instance
  36. pytest.cdk_obj = None
  37. @pytest.fixture(scope='function')
  38. def cdk(
  39. request: pytest.fixture,
  40. project: str,
  41. feature_name: str,
  42. workspace: pytest.fixture,
  43. aws_utils: pytest.fixture,
  44. bootstrap_required: bool = True,
  45. destroy_stacks_on_teardown: bool = True) -> Cdk:
  46. """
  47. Fixture for setting up a Cdk
  48. :param request: _pytest.fixtures.SubRequest class that handles getting
  49. a pytest fixture from a pytest function/fixture.
  50. :param project: Project name used for cdk project name env variable.
  51. :param feature_name: Feature gem name to expect cdk folder in.
  52. :param workspace: ly_test_tools workspace fixture.
  53. :param aws_utils: aws_utils fixture.
  54. :param bootstrap_required: Whether the bootstrap stack needs to be created to
  55. provision resources the AWS CDK needs to perform the deployment.
  56. :param destroy_stacks_on_teardown: option to control calling destroy ot the end of test.
  57. :return Cdk class object.
  58. """
  59. cdk_path = f'{workspace.paths.engine_root()}/Gems/{feature_name}/cdk'
  60. logger.info(f'CDK Path {cdk_path}')
  61. if pytest.cdk_obj is None:
  62. pytest.cdk_obj = Cdk()
  63. pytest.cdk_obj.setup(cdk_path, project, aws_utils.assume_account_id(), workspace, aws_utils.assume_session(),
  64. bootstrap_required)
  65. def teardown():
  66. if destroy_stacks_on_teardown:
  67. pytest.cdk_obj.destroy()
  68. # Enable after https://github.com/aws/aws-cdk/issues/986 is fixed.
  69. # Until then clean the bootstrap bucket manually.
  70. # cdk_obj.remove_bootstrap_stack()
  71. request.addfinalizer(teardown)
  72. return pytest.cdk_obj