-
Notifications
You must be signed in to change notification settings - Fork 48
Fix condition when no handlers have been defined #182
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
Fix condition when no handlers have been defined #182
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Latest GitHub Action workflow run fails due to missing test coverage: https://github.com/aws-cloudformation/cloudformation-cli-python-plugin/runs/5875423952?check_suite_focus=true#step:6:93 |
826f565
to
519a988
Compare
Rebased from the #183 |
It's not the pre-commit dependency - it's just missing a single branch for test coverage. Probably just needs a simple test added here. |
Thanks for this contribution John, I was over-eager approving this and didn't notice the failing checks. Are you able to add a test for the case where no logging is defined ? This project has a requirement for 100% branch test coverage 🥃 . As Darryl mentioned, current tests for this function are here: https://github.com/aws-cloudformation/cloudformation-cli-python-plugin/blob/master/tests/lib/log_delivery_test.py#L147-L230 haven't looked in great depth, but you should be able to re-use the pattern used in those. |
Hi John - thanks for your contribution. I did not initially realize a test for the corresponding change is missing. Please see comments above - thank you!
Sure, let me add a test for this |
First time I used this lib I worked on a py3.9 machine and all the tests passed fine. Now on a py 3.10 the local tests with pylint and pytest won't work. Will try to create a new venv with the python versions of this lib. |
John, would you mind opening a new issue with the full pylint/pytest output so we can properly debug this? If it's an issue for you, it may be an issue for others as well. Thanks! |
So just a couple things
************* Module src.cloudformation_cli_python_lib.resource
src/cloudformation_cli_python_lib/resource.py:224:25: C0209: Formatting a regular string which could be a f-string (consider-using-f-string)
src/cloudformation_cli_python_lib/resource.py:227:25: C0209: Formatting a regular string which could be a f-string (consider-using-f-string)
************* Module src.cloudformation_cli_python_lib.metrics
src/cloudformation_cli_python_lib/metrics.py:145:4: W0237: Parameter 'action' has been renamed to 'invocation_point' in overridden 'HookMetricsPublisher.publish_exception_metric' method (arguments-renamed)
src/cloudformation_cli_python_lib/metrics.py:165:4: W0237: Parameter 'action' has been renamed to 'invocation_point' in overridden 'HookMetricsPublisher.publish_invocation_metric' method (arguments-renamed)
src/cloudformation_cli_python_lib/metrics.py:181:4: W0237: Parameter 'action' has been renamed to 'invocation_point' in overridden 'HookMetricsPublisher.publish_duration_metric' method (arguments-renamed)
************* Module src.cloudformation_cli_python_lib.hook
src/cloudformation_cli_python_lib/hook.py:278:25: C0209: Formatting a regular string which could be a f-string (consider-using-f-string)
src/cloudformation_cli_python_lib/hook.py:281:25: C0209: Formatting a regular string which could be a f-string (consider-using-f-string)
************* Module python.rpdk.python.parser
python/rpdk/python/parser.py:4:20: C0209: Formatting a regular string which could be a f-string (consider-using-f-string)
************* Module tests.lib.metrics_test
tests/lib/metrics_test.py:373:11: C1803: 'proxy._publishers == []' can be simplified to 'not proxy._publishers' as an empty sequence is falsey (use-implicit-booleaness-not-comparison)
------------------------------------------------------------------
Your code has been rated at 9.97/10 (previous run: 9.89/10, +0.08)
@classmethod
def _get_existing_logger(cls) -> Optional["ProviderLogHandler"]: works. My python is mid-level at best considering the wizardry going on. So any input on that would be great |
I can confirm the change passes the actual tests on my setup (Python 3.9.12)
Though we still need to address the test coverage complaint. Note that I can't merge the PR even with the necessary approvals. |
In the coverage report, the condition never evaluates to true in any of the tests. if logging.getLogger().handlers:
logging.getLogger().handlers[0].addFilter(ProviderFilter(provider)) Alternatively, the following never evaluates to false: if logging.getLogger().hasHandlers():
logging.getLogger().handlers[0].addFilter(ProviderFilter(provider)) |
Admittedly, I'm not well-versed enough in this library/code base to know the impact of this change, but I'm curious if this would resolve the issue: # filter provider messages from platform
provider = request.resourceType.replace("::", "_").lower()
- logging.getLogger().handlers[0].addFilter(ProviderFilter(provider))
log_handler = cls(
group=log_group, stream=stream_name, session=provider_sess
)
# add log handler to root, so that provider gets plugin logs too
logging.getLogger().addHandler(log_handler)
+ logging.getLogger().handlers[0].addFilter(ProviderFilter(provider)) This passes all tests, including 100% coverage. @JohnPreston does this solve your issue? |
For context, #36 is where this particular |
Your tests pass 100%, but did it work when trying with a new CFN resource type in "actual" CFN ? |
I didn't manually test this against CFN yet. I can carve out time to do that, but I want to make sure the change I proposed actually works for your use-case first. As it stands, I'm a little skeptical of the conditional expression in front of the That's why I'm curious if simply moving the |
The problem is accessing edit: I did not originally get your change until I pulled it @darrylabbate |
@JohnPreston I went ahead and submitted the change (#184) to make it a little more convenient for you to pull and test the change on your end. |
Hi @darrylabbate |
Description of changes:
Lack of validation that there are handlers set at all leads to trying to access at invalid index for the
handlers
.So, if there are handlers, we can indeed at least access
0
and then add the filter.I won't pretend to understand why this is here / what's the use-case for it, given that's not documented, but at least with that simple fix my code that works in sam local, in cfn test resource, and now in actually using it in CFN.
I also then now get logs into CloudWatch logs.
Sad note:
Not sure how, if at all, this passed real life tests when working on the new version release.
So that begs the question, for me and anyone who wants to use this library: is it at all tested in CFN or just with cfn test on local lambda ? :/
And especially if not at all, I'd recommend to have beta versions, users notified of these new beta versions for us to test with (which won't affect current working published resource versions either) so that can be tested, and very happy to part take in that.