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

fix: replication policy cron setting - the 1st field must be 0; the Minutes field cannot be * #18923

Merged
merged 3 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/controller/replication/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ func (p *Policy) Validate() error {
return errors.New(nil).WithCode(errors.BadRequestCode).
WithMessage("invalid cron string for scheduled trigger: %s", p.Trigger.Settings.Cron)
}
cronParts := strings.Split(p.Trigger.Settings.Cron, " ")
if cronParts[0] != "0" {
return errors.New(nil).WithCode(errors.BadRequestCode).WithMessage("the 1st field (indicating Seconds of time) of the cron setting must be 0")
}
if cronParts[1] == "*" {
return errors.New(nil).WithCode(errors.BadRequestCode).WithMessage("* is not allowed for the Minutes field of the cron setting of replication policy")
}
default:
return errors.New(nil).WithCode(errors.BadRequestCode).
WithMessage("invalid trigger type")
Expand Down
60 changes: 59 additions & 1 deletion src/controller/replication/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func TestValidate(t *testing.T) {
err = policy.Validate()
assert.True(errors.IsErr(err, errors.BadRequestCode))

// pass
// invalid cron: the 1st field (indicating Seconds of time) of the cron setting must be 0
policy = &Policy{
Name: "policy01",
SrcRegistry: &model.Registry{
Expand All @@ -226,5 +226,63 @@ func TestValidate(t *testing.T) {
},
}
err = policy.Validate()
assert.True(errors.IsErr(err, errors.BadRequestCode))

// invalid cron: * is not allowed for the Minutes field of the cron setting of replication policy
policy = &Policy{
Name: "policy01",
SrcRegistry: &model.Registry{
ID: 0,
},
DestRegistry: &model.Registry{
ID: 1,
},
Filters: []*model.Filter{
{
Type: model.FilterTypeResource,
Value: "image",
},
{
Type: model.FilterTypeName,
Value: "library/**",
},
},
Trigger: &model.Trigger{
Type: model.TriggerTypeScheduled,
Settings: &model.TriggerSettings{
Cron: "0 * * * * *",
},
},
}
err = policy.Validate()
assert.True(errors.IsErr(err, errors.BadRequestCode))

// pass
policy = &Policy{
Name: "policy01",
SrcRegistry: &model.Registry{
ID: 0,
},
DestRegistry: &model.Registry{
ID: 1,
},
Filters: []*model.Filter{
{
Type: model.FilterTypeResource,
Value: "image",
},
{
Type: model.FilterTypeName,
Value: "library/**",
},
},
Trigger: &model.Trigger{
Type: model.TriggerTypeScheduled,
Settings: &model.TriggerSettings{
Cron: "0 0 * * * *",
},
},
}
err = policy.Validate()
assert.Nil(err)
}
4 changes: 2 additions & 2 deletions src/controller/replication/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (r *replicationTestSuite) TestCreatePolicy() {
Trigger: &model.Trigger{
Type: model.TriggerTypeScheduled,
Settings: &model.TriggerSettings{
Cron: "0 * * * * *",
Cron: "0 0 * * * *",
},
},
Enabled: true,
Expand Down Expand Up @@ -106,7 +106,7 @@ func (r *replicationTestSuite) TestUpdatePolicy() {
Trigger: &model.Trigger{
Type: model.TriggerTypeScheduled,
Settings: &model.TriggerSettings{
Cron: "0 * * * * *",
Cron: "0 0 * * * *",
},
},
Enabled: true,
Expand Down