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

Signed-off-by: Shengwen Yu <yshengwen@vmware.com>
This commit is contained in:
Shengwen YU 2023-07-20 17:55:48 +08:00 committed by GitHub
parent ce893635aa
commit 9091661539
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 3 deletions

View File

@ -108,6 +108,13 @@ func (p *Policy) Validate() error {
return errors.New(nil).WithCode(errors.BadRequestCode). return errors.New(nil).WithCode(errors.BadRequestCode).
WithMessage("invalid cron string for scheduled trigger: %s", p.Trigger.Settings.Cron) 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: default:
return errors.New(nil).WithCode(errors.BadRequestCode). return errors.New(nil).WithCode(errors.BadRequestCode).
WithMessage("invalid trigger type") WithMessage("invalid trigger type")

View File

@ -199,7 +199,7 @@ func TestValidate(t *testing.T) {
err = policy.Validate() err = policy.Validate()
assert.True(errors.IsErr(err, errors.BadRequestCode)) 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{ policy = &Policy{
Name: "policy01", Name: "policy01",
SrcRegistry: &model.Registry{ SrcRegistry: &model.Registry{
@ -226,5 +226,63 @@ func TestValidate(t *testing.T) {
}, },
} }
err = policy.Validate() 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) assert.Nil(err)
} }

View File

@ -78,7 +78,7 @@ func (r *replicationTestSuite) TestCreatePolicy() {
Trigger: &model.Trigger{ Trigger: &model.Trigger{
Type: model.TriggerTypeScheduled, Type: model.TriggerTypeScheduled,
Settings: &model.TriggerSettings{ Settings: &model.TriggerSettings{
Cron: "0 * * * * *", Cron: "0 0 * * * *",
}, },
}, },
Enabled: true, Enabled: true,
@ -106,7 +106,7 @@ func (r *replicationTestSuite) TestUpdatePolicy() {
Trigger: &model.Trigger{ Trigger: &model.Trigger{
Type: model.TriggerTypeScheduled, Type: model.TriggerTypeScheduled,
Settings: &model.TriggerSettings{ Settings: &model.TriggerSettings{
Cron: "0 * * * * *", Cron: "0 0 * * * *",
}, },
}, },
Enabled: true, Enabled: true,