Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhandled Exception with cfn test - Permission error with pytest ini file #876

Closed
tjbrockmeyer opened this issue Apr 11, 2022 · 3 comments · Fixed by #924
Closed

Unhandled Exception with cfn test - Permission error with pytest ini file #876

tjbrockmeyer opened this issue Apr 11, 2022 · 3 comments · Fixed by #924
Assignees
Labels
bug Something isn't working

Comments

@tjbrockmeyer
Copy link

tjbrockmeyer commented Apr 11, 2022

OS: Windows 10 Pro 64-bit

I am attempting to use cfn test after developing a resource type, and I am receiving a permission error related to a generated pytest ini file.

This is the output of running cfn test:

Resource schema is valid.
=== Unhandled exception ===
Please report this issue to the team.
Issue tracker: github.com/aws-cloudformation/cloudformation-cli/issues
Please include the log file 'rpdk.log'

Here is the full log output:

[2022-04-11T00:16:21Z] DEBUG    - Logging set up successfully
[2022-04-11T00:16:21Z] DEBUG    - Running test: Namespace(version=False, subparser_name='test', command=<function test at 0x00000241BA2D7280>, verbose=0, endpoint='http://127.0.0.1:3001', function_name='TypeFunction', region='us-east-1', role_arn=None, cloudformation_endpoint_url=None, enforce_timeout='30', log_group_name=None, log_role_arn=None, passed_to_pytest=[], docker_image=None)
[2022-04-11T00:16:21Z] DEBUG    - Root directory: C:\Coding\code\web\infra\aws\custom-resources\tjb-ssm-securestring
[2022-04-11T00:16:21Z] DEBUG    - Loading project file 'C:\Coding\code\web\infra\aws\custom-resources\tjb-ssm-securestring\.rpdk-config'
[2022-04-11T00:16:21Z] INFO     - Validating your resource specification...
[2022-04-11T00:16:21Z] DEBUG    - Rewriting refs in '<BASE>' (file:///C:/Coding/code/web/infra/aws/custom-resources/tjb-ssm-securestring/tjb-ssm-securestring.json)
[2022-04-11T00:16:21Z] WARNING  - Resource schema is valid.
[2022-04-11T00:16:21Z] INFO     - Validating your resource schema...
[2022-04-11T00:16:21Z] DEBUG    - Override file 'C:\Coding\code\web\infra\aws\custom-resources\tjb-ssm-securestring\overrides.json' not found. No overrides will be applied
[2022-04-11T00:16:22Z] DEBUG    - Setup plugin for RESOURCE type
[2022-04-11T00:16:22Z] DEBUG    - temporary pytest.ini path: C:\Users\Tyler\AppData\Local\Temp\pytest_mn_hg_yk.ini
[2022-04-11T00:16:22Z] DEBUG    - Unhandled exception
Traceback (most recent call last):
  File "c:\coding\frameworks\python\python3.9.1\lib\site-packages\rpdk\core\cli.py", line 100, in main
    args.command(args)
  File "c:\coding\frameworks\python\python3.9.1\lib\site-packages\rpdk\core\test.py", line 384, in test
    invoke_test(args, project, overrides, None)
  File "c:\coding\frameworks\python\python3.9.1\lib\site-packages\rpdk\core\test.py", line 390, in invoke_test
    with temporary_ini_file() as path:
  File "c:\coding\frameworks\python\python3.9.1\lib\contextlib.py", line 117, in __enter__
    return next(self.gen)
  File "c:\coding\frameworks\python\python3.9.1\lib\site-packages\rpdk\core\test.py", line 87, in temporary_ini_file
    copy_resource(__name__, "data/pytest-contract.ini", path)
  File "c:\coding\frameworks\python\python3.9.1\lib\site-packages\rpdk\core\data_loaders.py", line 54, in copy_resource
    ) as fsrc, out_path.open("wb") as fdst:
  File "c:\coding\frameworks\python\python3.9.1\lib\pathlib.py", line 1241, in open
    return io.open(self, mode, buffering, encoding, errors, newline,
  File "c:\coding\frameworks\python\python3.9.1\lib\pathlib.py", line 1109, in _opener
    return self._accessor.open(self, flags, mode)
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Tyler\\AppData\\Local\\Temp\\pytest_mn_hg_yk.ini'
@Pettles
Copy link

Pettles commented May 11, 2022

I'm hitting this same issue when using the cfn cli.
Looking through the code, and doing some Googling, it's because the rpdk.core.test.py is using tempfile.NamedTemporaryFile in the temporary_ini_file method. And because this class deletes the file when the context manager is exited, the file cannot be re-accessed when the data_loaders.py module calls out_path.open().

Context for this here: https://stackoverflow.com/a/23212515

I managed a temporary workaround for this issue by updating the temporary_ini_file to open the NamedTemporaryFile with delete=False. It does mean that you will need to delete the created *.ini temp files manually after testing, but it does allow the cfn test to be executed without error.

# in ~/site-packages/rpdk/core/test.py

@contextmanager
def temporary_ini_file():
    with NamedTemporaryFile(
            mode="w", encoding="utf-8", prefix="pytest_", suffix=".ini", delete=False,  # Added this kwarg
    ) as temp:
        LOG.debug("temporary pytest.ini path: %s", temp.name)
        path = Path(temp.name).resolve(strict=True)
        copy_resource(__name__, "data/pytest-contract.ini", path)
        yield str(path)

@iarovyi
Copy link

iarovyi commented Sep 27, 2022

I got exactly the same error but changing library source code is not a good option since I don't know python.

@mmaeng mmaeng self-assigned this Oct 20, 2022
@mmaeng mmaeng added the bug Something isn't working label Oct 20, 2022
@mmaeng
Copy link
Contributor

mmaeng commented Oct 20, 2022

Thanks for bringing this to our attention!

This looks to be a behavior in one of the functions that creates a temporary config file and only occurs on Windows. It looks like the python community is still discussing the fix but it hasn't been resolved yet.

I've created a fix PR #924 to work around the issue. It will clean up the temporary files afterwards as well so you don't have to do it manually outside of the cloudformation cli run.

Please join us on our AWS CloudFormation Discord channel, we are on it regularly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants