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: Add e2e tests for Reports, extend reports validation #541

Merged
merged 4 commits into from
Sep 13, 2024
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
29 changes: 25 additions & 4 deletions manifest/v1alpha/report/validation_system_health_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ var reportThresholdsValidation = govy.New[Thresholds](
govy.ForPointer(func(s Thresholds) *float64 { return s.RedLessThanOrEqual }).
WithName("redLte").
Required().
Rules(rules.GTE(0.0), rules.LTE(1.0)),
Rules(rules.LTE(1.0)),
govy.ForPointer(func(s Thresholds) *float64 { return s.GreenGreaterThan }).
WithName("greenGt").
Required().
Rules(rules.GTE(0.0), rules.LTE(1.0)),
Rules(rules.LTE(1.0)),
)

var redLteValidation = govy.NewRule(func(v Thresholds) error {
Expand All @@ -92,9 +92,11 @@ var snapshotValidation = govy.New[SnapshotTimeFrame](
var snapshotTimeFramePastPointValidation = govy.New[SnapshotTimeFrame](
govy.ForPointer(func(s SnapshotTimeFrame) *time.Time { return s.DateTime }).
WithName("dateTime").
Required(),
Required().
Rules(dateTimeInThePast),
govy.Transform(func(s SnapshotTimeFrame) string { return s.Rrule }, rrule.StrToRRule).
WithName("rrule"),
WithName("rrule").
Rules(atLeastDailyFreq),
).When(
func(s SnapshotTimeFrame) bool { return s.Point == SnapshotPointPast },
govy.WhenDescription("past snapshot point"),
Expand All @@ -119,3 +121,22 @@ var snapshotTimeFrameLatestPointValidation = govy.New[SnapshotTimeFrame](
func(s SnapshotTimeFrame) bool { return s.Point == SnapshotPointLatest },
govy.WhenDescription("latest snapshot point"),
)

var atLeastDailyFreq = govy.NewRule(func(rule *rrule.RRule) error {
if rule == nil {
return nil
}
if rule.Options.Freq == rrule.HOURLY ||
rule.Options.Freq == rrule.MINUTELY ||
rule.Options.Freq == rrule.SECONDLY {
return errors.New("rrule must have at least daily frequency")
}
return nil
})

var dateTimeInThePast = govy.NewRule(func(dateTime time.Time) error {
if time.Now().Before(dateTime) {
return errors.New("dateTime must be in the past")
}
return nil
})
181 changes: 148 additions & 33 deletions manifest/v1alpha/report/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/teambition/rrule-go"

"github.com/nobl9/govy/pkg/rules"

Expand Down Expand Up @@ -513,8 +514,8 @@ func TestValidate_Spec_SystemHealthReview(t *testing.T) {
{DisplayName: "Column 1", Labels: properLabel},
},
Thresholds: Thresholds{
RedLessThanOrEqual: func(f float64) *float64 { return &f }(0.0),
GreenGreaterThan: func(f float64) *float64 { return &f }(0.2),
RedLessThanOrEqual: ptr(0.0),
GreenGreaterThan: ptr(0.2),
},
},
},
Expand All @@ -536,8 +537,8 @@ func TestValidate_Spec_SystemHealthReview(t *testing.T) {
RowGroupBy: RowGroupByProject,
Columns: []ColumnSpec{},
Thresholds: Thresholds{
RedLessThanOrEqual: func(f float64) *float64 { return &f }(0.0),
GreenGreaterThan: func(f float64) *float64 { return &f }(0.2),
RedLessThanOrEqual: ptr(0.0),
GreenGreaterThan: ptr(0.2),
},
},
},
Expand Down Expand Up @@ -591,8 +592,8 @@ func TestValidate_Spec_SystemHealthReview(t *testing.T) {
{DisplayName: "Column 31", Labels: properLabel},
},
Thresholds: Thresholds{
RedLessThanOrEqual: func(f float64) *float64 { return &f }(0.0),
GreenGreaterThan: func(f float64) *float64 { return &f }(0.2),
RedLessThanOrEqual: ptr(0.0),
GreenGreaterThan: ptr(0.2),
},
},
},
Expand All @@ -616,8 +617,8 @@ func TestValidate_Spec_SystemHealthReview(t *testing.T) {
{DisplayName: "Column 1", Labels: map[LabelKey][]LabelValue{}},
},
Thresholds: Thresholds{
RedLessThanOrEqual: func(f float64) *float64 { return &f }(0.0),
GreenGreaterThan: func(f float64) *float64 { return &f }(0.2),
RedLessThanOrEqual: ptr(0.0),
GreenGreaterThan: ptr(0.2),
},
},
},
Expand All @@ -641,8 +642,8 @@ func TestValidate_Spec_SystemHealthReview(t *testing.T) {
{Labels: properLabel},
},
Thresholds: Thresholds{
RedLessThanOrEqual: func(f float64) *float64 { return &f }(0.0),
GreenGreaterThan: func(f float64) *float64 { return &f }(0.2),
RedLessThanOrEqual: ptr(0.0),
GreenGreaterThan: ptr(0.2),
},
},
},
Expand All @@ -668,12 +669,8 @@ func TestValidate_Spec_SystemHealthReview(t *testing.T) {
},
},
"fails with invalid thresholds": {
ExpectedErrorsCount: 2,
ExpectedErrorsCount: 1,
ExpectedErrors: []testutils.ExpectedError{
{
Prop: "spec.systemHealthReview.thresholds.redLte",
Code: rules.ErrorCodeGreaterThanOrEqualTo,
},
{
Prop: "spec.systemHealthReview.thresholds.greenGt",
Code: rules.ErrorCodeLessThanOrEqualTo,
Expand All @@ -691,8 +688,8 @@ func TestValidate_Spec_SystemHealthReview(t *testing.T) {
{DisplayName: "Column 1", Labels: properLabel},
},
Thresholds: Thresholds{
RedLessThanOrEqual: func(f float64) *float64 { return &f }(-0.1),
GreenGreaterThan: func(f float64) *float64 { return &f }(1.1),
RedLessThanOrEqual: ptr(-0.1),
GreenGreaterThan: ptr(1.1),
},
},
},
Expand All @@ -719,8 +716,8 @@ func TestValidate_Spec_SystemHealthReview(t *testing.T) {
{DisplayName: "Column 1", Labels: properLabel},
},
Thresholds: Thresholds{
RedLessThanOrEqual: func(f float64) *float64 { return &f }(0.2),
GreenGreaterThan: func(f float64) *float64 { return &f }(0.1),
RedLessThanOrEqual: ptr(0.2),
GreenGreaterThan: ptr(0.1),
},
}
err := validate(report)
Expand Down Expand Up @@ -752,8 +749,8 @@ func TestValidate_Spec_SystemHealthReview_TimeFrame(t *testing.T) {
{DisplayName: "Column 1", Labels: map[LabelKey][]LabelValue{"key1": {"value1"}}},
},
Thresholds: Thresholds{
RedLessThanOrEqual: func(f float64) *float64 { return &f }(0.0),
GreenGreaterThan: func(f float64) *float64 { return &f }(0.2),
RedLessThanOrEqual: ptr(0.0),
GreenGreaterThan: ptr(0.2),
},
},
},
Expand All @@ -775,8 +772,8 @@ func TestValidate_Spec_SystemHealthReview_TimeFrame(t *testing.T) {
{DisplayName: "Column 1", Labels: map[LabelKey][]LabelValue{"key1": {"value1"}}},
},
Thresholds: Thresholds{
RedLessThanOrEqual: func(f float64) *float64 { return &f }(0.0),
GreenGreaterThan: func(f float64) *float64 { return &f }(0.2),
RedLessThanOrEqual: ptr(0.0),
GreenGreaterThan: ptr(0.2),
},
},
},
Expand All @@ -800,8 +797,8 @@ func TestValidate_Spec_SystemHealthReview_TimeFrame(t *testing.T) {
{DisplayName: "Column 1", Labels: map[LabelKey][]LabelValue{"key1": {"value1"}}},
},
Thresholds: Thresholds{
RedLessThanOrEqual: func(f float64) *float64 { return &f }(0.0),
GreenGreaterThan: func(f float64) *float64 { return &f }(0.2),
RedLessThanOrEqual: ptr(0.0),
GreenGreaterThan: ptr(0.2),
},
},
},
Expand Down Expand Up @@ -830,8 +827,8 @@ func TestValidate_Spec_SystemHealthReview_TimeFrame(t *testing.T) {
{DisplayName: "Column 1", Labels: map[LabelKey][]LabelValue{"key1": {"value1"}}},
},
Thresholds: Thresholds{
RedLessThanOrEqual: func(f float64) *float64 { return &f }(0.0),
GreenGreaterThan: func(f float64) *float64 { return &f }(0.2),
RedLessThanOrEqual: ptr(0.0),
GreenGreaterThan: ptr(0.2),
},
},
},
Expand Down Expand Up @@ -860,8 +857,35 @@ func TestValidate_Spec_SystemHealthReview_TimeFrame(t *testing.T) {
{DisplayName: "Column 1", Labels: map[LabelKey][]LabelValue{"key1": {"value1"}}},
},
Thresholds: Thresholds{
RedLessThanOrEqual: func(f float64) *float64 { return &f }(0.0),
GreenGreaterThan: func(f float64) *float64 { return &f }(0.2),
RedLessThanOrEqual: ptr(0.0),
GreenGreaterThan: ptr(0.2),
},
},
},
"fails with rrule frequency less than DAILY in past point snapshot": {
ExpectedErrorsCount: 1,
ExpectedErrors: []testutils.ExpectedError{
{
Prop: "spec.systemHealthReview.timeFrame.snapshot.rrule",
Message: "rrule must have at least daily frequency",
},
},
Config: SystemHealthReviewConfig{
TimeFrame: SystemHealthReviewTimeFrame{
Snapshot: SnapshotTimeFrame{
Point: SnapshotPointPast,
DateTime: ptr(time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)),
Rrule: "FREQ=SECONDLY;INTERVAL=2",
},
TimeZone: "Europe/Warsaw",
},
RowGroupBy: RowGroupByProject,
Columns: []ColumnSpec{
{DisplayName: "Column 1", Labels: map[LabelKey][]LabelValue{"key1": {"value1"}}},
},
Thresholds: Thresholds{
RedLessThanOrEqual: ptr(0.0),
GreenGreaterThan: ptr(0.2),
},
},
},
Expand Down Expand Up @@ -891,8 +915,34 @@ func TestValidate_Spec_SystemHealthReview_TimeFrame(t *testing.T) {
{DisplayName: "Column 1", Labels: map[LabelKey][]LabelValue{"key1": {"value1"}}},
},
Thresholds: Thresholds{
RedLessThanOrEqual: func(f float64) *float64 { return &f }(0.0),
GreenGreaterThan: func(f float64) *float64 { return &f }(0.2),
RedLessThanOrEqual: ptr(0.0),
GreenGreaterThan: ptr(0.2),
},
},
},
"fails with dateTime in the future in past point snapshot": {
ExpectedErrorsCount: 1,
ExpectedErrors: []testutils.ExpectedError{
{
Prop: "spec.systemHealthReview.timeFrame.snapshot.dateTime",
Message: "dateTime must be in the past",
},
},
Config: SystemHealthReviewConfig{
TimeFrame: SystemHealthReviewTimeFrame{
Snapshot: SnapshotTimeFrame{
Point: SnapshotPointPast,
DateTime: ptr(time.Now().Add(time.Hour)),
},
TimeZone: "Europe/Warsaw",
},
RowGroupBy: RowGroupByProject,
Columns: []ColumnSpec{
{DisplayName: "Column 1", Labels: map[LabelKey][]LabelValue{"key1": {"value1"}}},
},
Thresholds: Thresholds{
RedLessThanOrEqual: ptr(0.0),
GreenGreaterThan: ptr(0.2),
},
},
},
Expand All @@ -906,6 +956,71 @@ func TestValidate_Spec_SystemHealthReview_TimeFrame(t *testing.T) {
}
}

func TestAtLeastDailyFreq(t *testing.T) {
atLeastDailyFreqErr := "rrule must have at least daily frequency"
tests := []struct {
name string
rule string
expectedError string
}{
{
name: "nil rule returns no error",
rule: "",
expectedError: "",
},
{
name: "hourly frequency returns error",
rule: "FREQ=HOURLY;INTERVAL=1",
expectedError: atLeastDailyFreqErr,
},
{
name: "minutely frequency returns error",
rule: "FREQ=MINUTELY;INTERVAL=1",
expectedError: atLeastDailyFreqErr,
},
{
name: "secondly frequency returns error",
rule: "FREQ=SECONDLY;INTERVAL=1",
expectedError: atLeastDailyFreqErr,
},
{
name: "daily frequency returns no error",
rule: "FREQ=DAILY;INTERVAL=59",
expectedError: "",
},
{
name: "weekly frequency returns no error",
rule: "FREQ=WEEKLY;INTERVAL=59",
expectedError: "",
},
{
name: "monthly frequency returns no error",
rule: "FREQ=MONTHLY;INTERVAL=59",
expectedError: "",
},
{
name: "yearly frequency returns no error",
rule: "FREQ=YEARLY;INTERVAL=59",
expectedError: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var rule *rrule.RRule
if tt.rule != "" {
rule, _ = rrule.StrToRRule(tt.rule)
}
err := atLeastDailyFreq.Validate(rule)
if tt.expectedError == "" {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, tt.expectedError)
}
})
}
}

func validReport() Report {
return Report{
APIVersion: manifest.VersionV1alpha,
Expand Down Expand Up @@ -974,8 +1089,8 @@ func validReport() Report {
},
},
Thresholds: Thresholds{
RedLessThanOrEqual: func(f float64) *float64 { return &f }(0.8),
GreenGreaterThan: func(f float64) *float64 { return &f }(0.95),
RedLessThanOrEqual: ptr(0.8),
GreenGreaterThan: ptr(0.95),
ShowNoData: true,
},
},
Expand Down
Loading
Loading