Skip to content

Commit

Permalink
fix(sdk): add cron validation for cloud.Schedule (#6090)
Browse files Browse the repository at this point in the history
Closes #5985

## Checklist

- [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] Tests added (always)
- [ ] Docs updated (only required for features)
- [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
  • Loading branch information
garysassano committed Mar 29, 2024
1 parent 4e7b0d2 commit 9eb53bb
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 19 deletions.
16 changes: 12 additions & 4 deletions examples/tests/sdk_tests/schedule/init.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ skip: true

bring cloud;
bring util;
bring expect;

new cloud.Schedule( rate: 5m ) as "s0";

Expand All @@ -16,27 +17,34 @@ if (util.env("WING_TARGET") != "sim") {
} catch e {
error = e;
}
assert(error == "rate or cron need to be filled.");
expect.equal(error, "rate or cron need to be filled.");

try {
new cloud.Schedule( rate: 2m, cron: "* * * * *" ) as "s2";
} catch e {
error = e;
}
assert(error == "rate and cron cannot be configured simultaneously.");
expect.equal(error, "rate and cron cannot be configured simultaneously.");


try {
new cloud.Schedule( rate: 1s ) as "s3";
} catch e {
error = e;
}
assert(error == "rate can not be set to less than 1 minute.");
expect.equal(error, "rate can not be set to less than 1 minute.");

try {
new cloud.Schedule( cron: "* * * * * *" ) as "s4";
} catch e {
error = e;
}
assert(error == "cron string must be UNIX cron format [minute] [hour] [day of month] [month] [day of week]");
expect.equal(error, "cron string must be in UNIX cron format");

try {
new cloud.Schedule( cron: "* * * * ?" ) as "s5";
} catch e {
error = e;
}
expect.equal(error, "cron string must be in UNIX cron format");
}
2 changes: 1 addition & 1 deletion examples/tests/sdk_tests/schedule/on_tick.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ bring cloud;
bring util;

// every minute
let from_cron = new cloud.Schedule( cron: "* * * * ?" ) as "from_cron";
let from_cron = new cloud.Schedule( cron: "* * * * *" ) as "from_cron";
let from_rate = new cloud.Schedule( rate: 1m ) as "from_rate";
let c1 = new cloud.Counter() as "c1";
let c2 = new cloud.Counter() as "c2";
Expand Down
8 changes: 4 additions & 4 deletions libs/awscdk/test/schedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,7 @@ test("cron with more than five values", () => {
new cloud.Schedule(app, "Schedule", {
cron: "0/1 * ? * * *",
})
).toThrow(
"cron string must be UNIX cron format [minute] [hour] [day of month] [month] [day of week]"
);
).toThrow("cron string must be in UNIX cron format");
});

test("schedule without rate or cron", () => {
Expand Down Expand Up @@ -196,5 +194,7 @@ test("cron with day of month and day of week configured at the same time", () =>
new cloud.Schedule(app, "Schedule", {
cron: "* * 1 * 1",
})
).toThrow("Cannot restrict both 'day-of-month' and 'day-of-week' in a cron expression, at least one must be '*'");
).toThrow(
"Cannot restrict both 'day-of-month' and 'day-of-week' in a cron expression, at least one must be '*'"
);
});
4 changes: 4 additions & 0 deletions libs/wingsdk/.projen/deps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions libs/wingsdk/.projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const project = new cdk.JsiiProject({
"ioredis",
"jsonschema",
"ajv",
"cron-validator",
// fs module dependency
"yaml",
"toml",
Expand Down
2 changes: 2 additions & 0 deletions libs/wingsdk/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions libs/wingsdk/src/cloud/schedule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Construct } from "constructs";
import { isValidCron } from "cron-validator";
import { Function, FunctionProps } from "./function";
import { fqnForType } from "../constants";
import { AbstractMemberError } from "../core/errors";
Expand Down Expand Up @@ -70,10 +71,18 @@ export class Schedule extends Resource {
if (rate && rate.seconds < 60) {
throw new Error("rate can not be set to less than 1 minute.");
}
if (cron && cron.split(" ").length > 5) {
throw new Error(
"cron string must be UNIX cron format [minute] [hour] [day of month] [month] [day of week]"
);
// Check for valid UNIX cron format
// https://www.ibm.com/docs/en/db2/11.5?topic=task-unix-cron-format
if (
cron &&
!isValidCron(cron, {
alias: true,
allowSevenAsSunday: true,
allowBlankDay: false,
seconds: false,
})
) {
throw new Error("cron string must be in UNIX cron format");
}
}

Expand Down
4 changes: 1 addition & 3 deletions libs/wingsdk/test/target-tf-aws/schedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,7 @@ test("cron with more than five values", () => {
new cloud.Schedule(app, "Schedule", {
cron: "0/1 * * * * *",
})
).toThrow(
"cron string must be UNIX cron format [minute] [hour] [day of month] [month] [day of week]"
);
).toThrow("cron string must be in UNIX cron format");
});

test("schedule without rate or cron", () => {
Expand Down
13 changes: 10 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9eb53bb

Please sign in to comment.