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

feat: PC-14241 budget adjustment spec duration should not have precision set to 1 m #632

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
13 changes: 12 additions & 1 deletion manifest/v1alpha/budgetadjustment/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var specValidation = govy.New[Spec](
govy.Transform(func(s Spec) string { return s.Duration }, time.ParseDuration).
WithName("duration").
Required().
Rules(rules.DurationPrecision(time.Minute)),
Rules(durationPrecision),
govy.Transform(func(s Spec) string { return s.Rrule }, rrule.StrToRRule).
WithName("rrule").
Rules(atLeastHourlyFreq),
Expand Down Expand Up @@ -107,3 +107,14 @@ var secondTimePrecision = govy.NewRule(func(t time.Time) error {

return nil
})

var durationPrecision = govy.NewRule(func(t time.Duration) error {
if t.Minutes() < 1 {
return errors.New("duration must be at least 1 minute")
}
if t.Nanoseconds()%int64(time.Second) != 0 {
return errors.New("duration must be defined with 1s precision")
}

return nil
})
76 changes: 68 additions & 8 deletions manifest/v1alpha/budgetadjustment/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ func TestValidate_Spec(t *testing.T) {
},
expectedErrors: []testutils.ExpectedError{
{
Prop: "spec.duration",
Code: rules.ErrorCodeDurationPrecision,
Prop: "spec.duration",
Message: "duration must be at least 1 minute",
},
},
},
Expand All @@ -156,12 +156,7 @@ func TestValidate_Spec(t *testing.T) {
}},
},
},
expectedErrors: []testutils.ExpectedError{
{
Prop: "spec.duration",
Code: rules.ErrorCodeDurationPrecision,
},
},
expectedErrors: nil,
},
{
name: "slo is defined without name",
Expand Down Expand Up @@ -582,3 +577,68 @@ func TestAtLeastSecondTimeResolution(t *testing.T) {
})
}
}

func TestDurationPrecision(t *testing.T) {
tests := []struct {
name string
duration time.Duration
expectedError string
}{
{
name: "59ns returns error",
duration: time.Nanosecond * 59,
expectedError: "duration must be at least 1 minute",
},
{
name: "59s returns error",
duration: time.Second * 59,
expectedError: "duration must be at least 1 minute",
},
{
name: "1m no error",
duration: time.Minute,
expectedError: "",
},
{
name: "1m60ns returns error",
duration: time.Minute + 60*time.Nanosecond,
expectedError: "duration must be defined with 1s precision",
},
{
name: "1m1s no error",
duration: time.Minute + time.Second,
expectedError: "",
},
{
name: "1h returns no error",
duration: time.Hour,
expectedError: "",
},
{
name: "1h1s no error",
duration: time.Hour + time.Second,
expectedError: "",
},
{
name: "1h1m1s returns no error",
duration: time.Hour + time.Minute + time.Second,
expectedError: "",
},
{
name: "1h1m1ns returns error",
duration: time.Hour + time.Minute + time.Nanosecond,
expectedError: "duration must be defined with 1s precision",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := durationPrecision.Validate(tt.duration)
if tt.expectedError == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tt.expectedError)
}
})
}
}
Loading