From 265bdd80b30ecb2b4479e06bf94bb134d541418a Mon Sep 17 00:00:00 2001 From: liqiang Date: Tue, 29 Oct 2024 05:58:10 +0000 Subject: [PATCH 1/2] feat: add metric check methods for e2e --- test/engine/steps.go | 2 ++ test/engine/verify/count.go | 66 ++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/test/engine/steps.go b/test/engine/steps.go index b3ec512458..305e317465 100644 --- a/test/engine/steps.go +++ b/test/engine/steps.go @@ -48,6 +48,8 @@ func ScenarioInitializer(ctx *godog.ScenarioContext) { // Then ctx.Then(`^there is \{(\d+)\} logs$`, verify.LogCount) + ctx.Then(`^there is more than \{(\d+)\} metrics in \{(\d+)\} seconds and the value is greater than \{(\d+)\} and less than \{(\d+)\}$`, verify.MetricCountAndValueCompare) + ctx.Then(`^there is more than \{(\d+)\} metrics in \{(\d+)\} seconds and the value is \{(\d+)\}$`, verify.MetricCountAndValueEqual) ctx.Then(`^there is more than \{(\d+)\} metrics in \{(\d+)\} seconds$`, verify.MetricCount) ctx.Then(`^there is at least \{(\d+)\} logs$`, verify.LogCountAtLeast) ctx.Then(`^there is at least \{(\d+)\} logs with filter key \{(.*)\} value \{(.*)\}$`, verify.LogCountAtLeastWithFilter) diff --git a/test/engine/verify/count.go b/test/engine/verify/count.go index ea5052d350..2fadbc6d51 100644 --- a/test/engine/verify/count.go +++ b/test/engine/verify/count.go @@ -16,6 +16,7 @@ package verify import ( "context" "fmt" + "strconv" "time" "github.com/avast/retry-go/v4" @@ -70,7 +71,7 @@ func LogCount(ctx context.Context, expect int) (context.Context, error) { return ctx, nil } -func MetricCount(ctx context.Context, expect int, duration int64) (context.Context, error) { +func MetricCheck(ctx context.Context, expect int, duration int64, checker func([]*protocol.LogGroup) error) (context.Context, error) { timeoutCtx, cancel := context.WithTimeout(context.TODO(), config.TestConfig.RetryTimeout) defer cancel() var groups []*protocol.LogGroup @@ -94,6 +95,9 @@ func MetricCount(ctx context.Context, expect int, duration int64) (context.Conte if expect == 0 { return fmt.Errorf("metric count is 0") } + if err := checker(groups); err != nil { + return err + } return nil }, retry.Context(timeoutCtx), @@ -109,6 +113,66 @@ func MetricCount(ctx context.Context, expect int, duration int64) (context.Conte return ctx, nil } +func MetricCount(ctx context.Context, expect int, duration int64) (context.Context, error) { + return MetricCheck(ctx, expect, duration, func(groups []*protocol.LogGroup) error { + return nil + }) +} + +func MetricCountAndValueCompare(ctx context.Context, expect int, duration int64, minValue int64, maxValue int64) (context.Context, error) { + return MetricCheck(ctx, expect, duration, func(groups []*protocol.LogGroup) error { + lessCount := 0 + greaterCount := 0 + for _, group := range groups { + for _, log := range group.Logs { + for _, content := range log.Contents { + if content.Key == "value" { + value, err := strconv.ParseFloat(content.Value, 64) + if err != nil { + return fmt.Errorf("parse value failed: %v", err) + } + if value < float64(minValue) { + lessCount++ + } + if value > float64(maxValue) { + greaterCount++ + } + } + } + } + } + if lessCount > 0 || greaterCount > 0 { + return fmt.Errorf("metric value not match, lessCount %d, greaterCount %d", lessCount, greaterCount) + } + return nil + }) +} + +func MetricCountAndValueEqual(ctx context.Context, expect int, duration int64, expectValue int64) (context.Context, error) { + return MetricCheck(ctx, expect, duration, func(groups []*protocol.LogGroup) error { + notEqualCount := 0 + for _, group := range groups { + for _, log := range group.Logs { + for _, content := range log.Contents { + if content.Key == "value" { + value, err := strconv.ParseFloat(content.Value, 64) + if err != nil { + return fmt.Errorf("parse value failed: %v", err) + } + if value != float64(expectValue) { + notEqualCount++ + } + } + } + } + } + if notEqualCount > 0 { + return fmt.Errorf("metric value not match, not equal count %d", notEqualCount) + } + return nil + }) +} + func LogCountAtLeast(ctx context.Context, expect int) (context.Context, error) { var from int32 value := ctx.Value(config.StartTimeContextKey) From 5463f75b7eec23888211ee859785e428f29976b0 Mon Sep 17 00:00:00 2001 From: liqiang Date: Tue, 29 Oct 2024 11:30:03 +0000 Subject: [PATCH 2/2] fix: lint --- test/engine/verify/count.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/engine/verify/count.go b/test/engine/verify/count.go index 2fadbc6d51..53082ff184 100644 --- a/test/engine/verify/count.go +++ b/test/engine/verify/count.go @@ -95,7 +95,7 @@ func MetricCheck(ctx context.Context, expect int, duration int64, checker func([ if expect == 0 { return fmt.Errorf("metric count is 0") } - if err := checker(groups); err != nil { + if err = checker(groups); err != nil { return err } return nil