Skip to content

Commit

Permalink
Merge branch 'main' into revert-559
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinlawnik authored Dec 12, 2024
2 parents 5849a50 + 4afa716 commit 4bd44f0
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 31 deletions.
8 changes: 4 additions & 4 deletions docs/mock_example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/teambition/rrule-go v1.8.2 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.23.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.6.0 // indirect
golang.org/x/tools v0.24.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
Expand Down
16 changes: 8 additions & 8 deletions docs/mock_example/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,20 @@ github.com/teambition/rrule-go v1.8.2 h1:lIjpjvWTj9fFUZCmuoVDrKVOtdiyzbzc93qTmRV
github.com/teambition/rrule-go v1.8.2/go.mod h1:Ieq5AbrKGciP1V//Wq8ktsTXwSwJHDD5mD/wLBGl3p4=
go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU=
go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa h1:ELnwvuAXPNtPk1TJRuGkI9fDTwym6AYBu0qzT8AcHdI=
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0=
golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.24.0 h1:J1shsA93PJUEVaUSaay7UXAyE8aimq3GW0pjlolpa24=
Expand Down
11 changes: 10 additions & 1 deletion manifest/v1alpha/budgetadjustment/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ var specValidation = govy.New[Spec](
Rules(validationV1Alpha.StringDescription()),
govy.For(func(s Spec) time.Time { return s.FirstEventStart }).
WithName("firstEventStart").
Required(),
Required().
Rules(secondTimePrecision),
govy.Transform(func(s Spec) string { return s.Duration }, time.ParseDuration).
WithName("duration").
Required().
Expand Down Expand Up @@ -98,3 +99,11 @@ var atLeastHourlyFreq = govy.NewRule(func(rule *rrule.RRule) error {

return nil
})

var secondTimePrecision = govy.NewRule(func(t time.Time) error {
if t.Nanosecond() != 0 {
return errors.New("time must be defined with 1s precision")
}

return nil
})
76 changes: 58 additions & 18 deletions manifest/v1alpha/budgetadjustment/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestValidate_Spec(t *testing.T) {
name: "description too long",
spec: Spec{
Description: strings.Repeat("A", 2000),
FirstEventStart: time.Now(),
FirstEventStart: time.Now().Truncate(time.Second),
Duration: "1m",
Filters: Filters{SLOs: []SLORef{{Name: "my-slo", Project: "default"}}},
},
Expand All @@ -101,7 +101,7 @@ func TestValidate_Spec(t *testing.T) {
{
name: "duration required",
spec: Spec{
FirstEventStart: time.Now(),
FirstEventStart: time.Now().Truncate(time.Second),
Filters: Filters{SLOs: []SLORef{{Name: "my-slo", Project: "default"}}},
},
expectedErrors: []testutils.ExpectedError{
Expand All @@ -114,7 +114,7 @@ func TestValidate_Spec(t *testing.T) {
{
name: "no slo filters",
spec: Spec{
FirstEventStart: time.Now(),
FirstEventStart: time.Now().Truncate(time.Second),
Duration: "1m",
Filters: Filters{},
},
Expand All @@ -128,7 +128,7 @@ func TestValidate_Spec(t *testing.T) {
{
name: "too short duration",
spec: Spec{
FirstEventStart: time.Now(),
FirstEventStart: time.Now().Truncate(time.Second),
Duration: "1s",
Filters: Filters{
SLOs: []SLORef{{
Expand All @@ -147,7 +147,7 @@ func TestValidate_Spec(t *testing.T) {
{
name: "duration contains seconds",
spec: Spec{
FirstEventStart: time.Now(),
FirstEventStart: time.Now().Truncate(time.Second),
Duration: "1m1s",
Filters: Filters{
SLOs: []SLORef{{
Expand All @@ -166,7 +166,7 @@ func TestValidate_Spec(t *testing.T) {
{
name: "slo is defined without name",
spec: Spec{
FirstEventStart: time.Now(),
FirstEventStart: time.Now().Truncate(time.Second),
Duration: "1m",
Filters: Filters{
SLOs: []SLORef{{
Expand All @@ -184,7 +184,7 @@ func TestValidate_Spec(t *testing.T) {
{
name: "slo is defined with invalid slo name",
spec: Spec{
FirstEventStart: time.Now(),
FirstEventStart: time.Now().Truncate(time.Second),
Duration: "1m",
Filters: Filters{
SLOs: []SLORef{{
Expand All @@ -203,7 +203,7 @@ func TestValidate_Spec(t *testing.T) {
{
name: "slo is defined without project",
spec: Spec{
FirstEventStart: time.Now(),
FirstEventStart: time.Now().Truncate(time.Second),
Duration: "1m",
Filters: Filters{
SLOs: []SLORef{{
Expand All @@ -221,7 +221,7 @@ func TestValidate_Spec(t *testing.T) {
{
name: "slo is defined with invalid project name",
spec: Spec{
FirstEventStart: time.Now(),
FirstEventStart: time.Now().Truncate(time.Second),
Duration: "1m",
Filters: Filters{
SLOs: []SLORef{{
Expand All @@ -240,7 +240,7 @@ func TestValidate_Spec(t *testing.T) {
{
name: "wrong rrule format",
spec: Spec{
FirstEventStart: time.Now(),
FirstEventStart: time.Now().Truncate(time.Second),
Duration: "1m",
Rrule: "some test",
Filters: Filters{
Expand All @@ -260,7 +260,7 @@ func TestValidate_Spec(t *testing.T) {
{
name: "invalid rrule",
spec: Spec{
FirstEventStart: time.Now(),
FirstEventStart: time.Now().Truncate(time.Second),
Duration: "1m",
Rrule: "FREQ=TEST;INTERVAL=2",
Filters: Filters{
Expand All @@ -280,7 +280,7 @@ func TestValidate_Spec(t *testing.T) {
{
name: "invalid freq in rrule",
spec: Spec{
FirstEventStart: time.Now(),
FirstEventStart: time.Now().Truncate(time.Second),
Duration: "1m",
Rrule: "FREQ=MINUTELY;INTERVAL=2;COUNT=10",
Filters: Filters{
Expand All @@ -300,7 +300,7 @@ func TestValidate_Spec(t *testing.T) {
{
name: "rrule with dtstart trows transform error",
spec: Spec{
FirstEventStart: time.Now(),
FirstEventStart: time.Now().Truncate(time.Second),
Duration: "1m",
Rrule: "DTSTART:20240909T065900Z\\nRRULE:FREQ=MINUTELY;BYHOUR=6,8,9,10,11,12,13,14,15,16;COUNT=10",
Filters: Filters{
Expand All @@ -320,7 +320,7 @@ func TestValidate_Spec(t *testing.T) {
{
name: "proper freq in rrule",
spec: Spec{
FirstEventStart: time.Now(),
FirstEventStart: time.Now().Truncate(time.Second),
Duration: "1m",
Rrule: "FREQ=HOURLY;INTERVAL=1",
Filters: Filters{
Expand All @@ -335,7 +335,7 @@ func TestValidate_Spec(t *testing.T) {
{
name: "duplicate slo",
spec: Spec{
FirstEventStart: time.Now(),
FirstEventStart: time.Now().Truncate(time.Second),
Duration: "1m",
Rrule: "FREQ=WEEKLY;INTERVAL=2",
Filters: Filters{
Expand All @@ -355,7 +355,7 @@ func TestValidate_Spec(t *testing.T) {
{
name: "duplicate slo with multiple others",
spec: Spec{
FirstEventStart: time.Now(),
FirstEventStart: time.Now().Truncate(time.Second),
Duration: "1m",
Rrule: "FREQ=WEEKLY;INTERVAL=2",
Filters: Filters{
Expand All @@ -380,7 +380,7 @@ func TestValidate_Spec(t *testing.T) {
{
name: "proper spec",
spec: Spec{
FirstEventStart: time.Now(),
FirstEventStart: time.Now().Truncate(time.Second),
Duration: "1m",
Rrule: "FREQ=WEEKLY;INTERVAL=2",
Filters: Filters{
Expand Down Expand Up @@ -418,7 +418,7 @@ func validBudgetAdjustment() BudgetAdjustment {
DisplayName: "My Budget Adjustment",
},
Spec: Spec{
FirstEventStart: time.Now(),
FirstEventStart: time.Now().Truncate(time.Second),
Duration: "1m",
Filters: Filters{
SLOs: []SLORef{
Expand Down Expand Up @@ -542,3 +542,43 @@ func TestAtLeastHourlyFreq(t *testing.T) {
})
}
}

func TestAtLeastSecondTimeResolution(t *testing.T) {
tests := []struct {
name string
time time.Time
expectedError string
}{
{
name: "time with nanosecond returns error",
time: time.Date(2023, time.January, 1, 0, 0, 0, 1, time.UTC),
expectedError: "time must be defined with 1s precision",
},
{
name: "time with second resolution returns no error",
time: time.Date(2023, time.January, 1, 0, 0, 0, 0, time.UTC),
expectedError: "",
},
{
name: "time with millisecond resolution returns error",
time: time.Date(2023, time.January, 1, 0, 0, 0, 1000000, time.UTC),
expectedError: "time must be defined with 1s precision",
},
{
name: "time with microsecond resolution returns error",
time: time.Date(2023, time.January, 1, 0, 0, 0, 526, time.UTC),
expectedError: "time must be defined with 1s precision",
},
}

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

0 comments on commit 4bd44f0

Please sign in to comment.