-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Fix: validate schedule_expression for EventBridge Scheduler #12191
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
Conversation
LocalStack Community integration with Pro 2 files ± 0 2 suites ±0 21s ⏱️ - 1h 51m 20s Results for commit 6c02cc2. ± Comparison against base commit b0627a1. This pull request removes 4017 and adds 17 tests. Note that renamed tests count towards both.
♻️ This comment has been updated with latest results. |
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! Nice port of your previous fix 👌 nice usage of the patch
function.
I only have a comment, it seems there code that cannot be executed, and that path is also untested. Once this is addressed, I believe the PR is ready to be merged! 🚀
"at(2021-12-31)", | ||
], | ||
) | ||
def tests_create_schedule_with_invalid_schedule_expression( |
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.
nice test! 👌
with pytest.raises(ParamValidationError) as e: | ||
aws_client.scheduler.create_schedule( | ||
Name=rule_name, | ||
ScheduleExpression="", | ||
FlexibleTimeWindow={ | ||
"MaximumWindowInMinutes": 4, | ||
"Mode": "FLEXIBLE", | ||
}, | ||
Target={ | ||
"Arn": f"arn:aws:lambda:{region_name}:{account_id}:function:dummy", | ||
"RoleArn": f"arn:aws:iam::{account_id}:role/role-name", | ||
}, | ||
) | ||
snapshot.match("empty-schedule-expression", e.value) |
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.
ParamValidationError
indicates that the error is coming from the client itself, and the request is not sent to AWS or LocalStack. We don't need to snapshot those kind of exception as they are dependent on the client only.
If the exception is of type ParamValidationError
, we should not snapshot it.
To configure the client to not have validation, and send the invalid request against AWS, you can configure you client this way:
from botocore.client import Config
def tests_create_schedule_with_empty_schedule_expression(
aws_client_factory, region_name, account_id, snapshot
):
scheduler_client = aws_client_factory(config=Config(parameter_validation=False)).scheduler
rule_name = f"rule-{short_uid()}"
with pytest.raises(ClientError) as e:
scheduler_client.create_schedule(
[...]
This way, the client won't validate the request and will let you send the invalid request against AWS, and see the proper exception.
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.
understood, thanks for sharing this. I'll keep it in mind for the future and will proceed removing these two dummy tests.
Address comments
3cc3702
to
6c02cc2
Compare
@bentsku Thanks for your comments. Could you please double check this when you have a moment? Thanks 🙌🏼 |
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! Thanks for addressing the comments.
I think we could have kept the tests when the schedule_expression
was missing, but with the client parameter validation disabled to see what AWS returned, however it's not fully in the scope of the issue, so that's alright, it was just a nice to have as you had already added it 😄 and it's pretty niche as most of the SDK will validate the input.
Anyway, very nice fix and parametrized tests! 🚀💯
Sorry I deleted everything. I'll merge this as is and if I come back to eventbridge might add that test 🙏🏼 |
No worries! You already went through the effort, that was all 😄 but this PR is good as it is, so let's get this merged! 🤝 |
I now get the error: Invalid Schedule Expression at(2025-01-31T15:59:42) with value:
Looking at AWS documentation https://docs.aws.amazon.com/scheduler/latest/UserGuide/schedule-types.html#one-time it's valid, but looking at the regex in the PR:
it's expecting d-m-Y instead of Y-m-d? |
Hi @ridgey-dev. Thanks for reporting this 🙏🏼
No, it's "Y-m-d", I'll fix the validation rule since I closed it too much. |
Thanks for your quick answer. Looking forward to your fix! Our pipeline that runs the tests on LocalStack is now failing because of this. |
Hi @ridgey-dev, thank you for your patience. We have implemented changes that should resolve your reported issue. Kindly update to the latest LocalStack container image and let us know if your issue has been resolved. |
It's working again. Thanks for your quick fix! |
Motivation
Customer reported an issue for scheduler validation of schedule_expression #9284.
The scheduler relies fully on moto for now, and validation is not implemented for
schedule_expression
.Schedule_expression should support
at
,cron
andrate
.Changes
Patched the
create_schedule
method in order to validateschedule_expression
.Reused some REGEX rules from events service.