mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 19:07:30 +00:00
fix(service ping): correct endpoint, validate and randomize default interval (#10166)
# Which Problems Are Solved The production endpoint of the service ping was wrong. Additionally we discussed in the sprint review, that we could randomize the default interval to prevent all systems to report data at the very same time and also require a minimal interval. # How the Problems Are Solved - fixed the endpoint - If the interval is set to @daily (default), we generate a random time (minute, hour) as a cron format. - Check if the interval is more than 30min and return an error if not. - Fixed yaml indent on `ResourceCount` # Additional Changes None # Additional Context as discussed internally
This commit is contained in:
@@ -1050,3 +1050,77 @@ func TestWorker_Work(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_parseAndValidateSchedule(t *testing.T) {
|
||||
type args struct {
|
||||
interval string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantNextStart time.Time
|
||||
wantNextEnd time.Time
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "@daily, returns randomized daily schedule",
|
||||
args: args{
|
||||
interval: "@daily",
|
||||
},
|
||||
wantNextStart: time.Now(),
|
||||
wantNextEnd: time.Now().Add(24 * time.Hour),
|
||||
},
|
||||
{
|
||||
name: "invalid cron expression, returns error",
|
||||
args: args{
|
||||
interval: "invalid cron",
|
||||
},
|
||||
wantErr: zerrors.ThrowInvalidArgument(nil, "SERV-NJqiof", "invalid interval"),
|
||||
},
|
||||
{
|
||||
name: "valid cron expression, returns schedule",
|
||||
args: args{
|
||||
interval: "0 0 * * *",
|
||||
},
|
||||
wantNextStart: nextMidnight(),
|
||||
wantNextEnd: nextMidnight(),
|
||||
},
|
||||
{
|
||||
name: "valid cron expression (extended syntax), returns schedule",
|
||||
args: args{
|
||||
interval: "@midnight",
|
||||
},
|
||||
wantNextStart: nextMidnight(),
|
||||
wantNextEnd: nextMidnight(),
|
||||
},
|
||||
{
|
||||
name: "less than minInterval, returns error",
|
||||
args: args{
|
||||
interval: "0/15 * * * *",
|
||||
},
|
||||
wantErr: zerrors.ThrowInvalidArgumentf(nil, "SERV-FJ12", "interval must be at least %s", minInterval),
|
||||
},
|
||||
{
|
||||
name: "less than minInterval (extended syntax), returns error",
|
||||
args: args{
|
||||
interval: "@every 15m",
|
||||
},
|
||||
wantErr: zerrors.ThrowInvalidArgumentf(nil, "SERV-FJ12", "interval must be at least %s", minInterval),
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := parseAndValidateSchedule(tt.args.interval)
|
||||
assert.ErrorIs(t, err, tt.wantErr)
|
||||
if tt.wantErr == nil {
|
||||
now := time.Now()
|
||||
assert.WithinRange(t, got.Next(now), tt.wantNextStart, tt.wantNextEnd)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func nextMidnight() time.Time {
|
||||
year, month, day := time.Now().Date()
|
||||
return time.Date(year, month, day+1, 0, 0, 0, 0, time.Local)
|
||||
}
|
||||
|
Reference in New Issue
Block a user