Skip to content

Commit

Permalink
fix: replication policy cron setting - the 1st field must be 0; the M…
Browse files Browse the repository at this point in the history
…inutes field cannot be *

Signed-off-by: Shengwen Yu <[email protected]>
  • Loading branch information
Shengwen Yu committed Jul 13, 2023
1 parent c707106 commit e1c2ea3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
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 @@ -76,7 +76,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 @@ -104,7 +104,7 @@ func (r *replicationTestSuite) TestUpdatePolicy() {
Trigger: &model.Trigger{
Type: model.TriggerTypeScheduled,
Settings: &model.TriggerSettings{
Cron: "0 * * * * *",
Cron: "0 0 * * * *",
},
},
Enabled: true,
Expand Down

0 comments on commit e1c2ea3

Please sign in to comment.