From a5f48b21358b7080b8f85876978eba66be92d1a6 Mon Sep 17 00:00:00 2001 From: Yaroslav Grebnov Date: Tue, 24 Nov 2026 19:25:17 +0100 Subject: [PATCH 1/6] Use github.com/ygrebnov/fs --- go.mod | 9 +-- go.sum | 18 +++--- util/file.go | 118 +++++++++++----------------------------- util/file_open_other.go | 30 ---------- util/file_open_unix.go | 17 ------ 5 files changed, 46 insertions(+), 146 deletions(-) delete mode 100644 util/file_open_other.go delete mode 100644 util/file_open_unix.go diff --git a/go.mod b/go.mod index d30d242..7d36268 100644 --- a/go.mod +++ b/go.mod @@ -7,10 +7,11 @@ require ( github.com/e2engine/instrumentation-go v0.2.3 github.com/e2engine/instrumentation-go/grpc v0.2.1 github.com/e2engine/instrumentation-go/http v0.2.2 - github.com/ygrebnov/errorc v0.6.1 + github.com/ygrebnov/errorc v0.7.0 + github.com/ygrebnov/fs v0.1.0 github.com/ygrebnov/keys v0.2.0 - github.com/ygrebnov/log v0.5.1 - github.com/ygrebnov/model v0.8.1 + github.com/ygrebnov/log v0.6.0 + github.com/ygrebnov/model v0.9.0 go.uber.org/mock v0.6.0 google.golang.org/grpc v1.85.0 google.golang.org/protobuf v1.36.12 @@ -22,5 +23,5 @@ require ( golang.org/x/sync v0.25.0 // indirect golang.org/x/sys v0.50.0 // indirect golang.org/x/text v0.44.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20261118221215-c8830f628b3f // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20261123191846-1e707ab9fbc2 // indirect ) diff --git a/go.sum b/go.sum index b6477c4..1a3cc36 100644 --- a/go.sum +++ b/go.sum @@ -16,14 +16,16 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -github.com/ygrebnov/errorc v0.6.1 h1:ICww1JOesR0HyE3cVWMr0SBmB8RV1XSmGwB5yVP1SqE= -github.com/ygrebnov/errorc v0.6.1/go.mod h1:akitMzADmnm0i1X5fSJfBqiTqCzckka9sgukTRQN5ns= +github.com/ygrebnov/errorc v0.7.0 h1:UBk753h/SNWfX9W6Byp+zckIdybXJ/ixc+VWukpQfdQ= +github.com/ygrebnov/errorc v0.7.0/go.mod h1:akitMzADmnm0i1X5fSJfBqiTqCzckka9sgukTRQN5ns= +github.com/ygrebnov/fs v0.1.0 h1:cswbccHB+YuluFYwes02NigqSjP3ZgM5stOvW+MxjMo= +github.com/ygrebnov/fs v0.1.0/go.mod h1:c3BP57rdNpeWJBW9a45dvsfbV6Yydxer3cEbSzSW2I8= github.com/ygrebnov/keys v0.2.0 h1:KQSQG1la9WkTW59WbB3CK1yhxZ92el2ZJfV1XtlHTp0= github.com/ygrebnov/keys v0.2.0/go.mod h1:4IfRPgv7tFSlToKzHtA9MPMwXP0+HRJ0Kk8XSrvhDvQ= -github.com/ygrebnov/log v0.5.1 h1:+Kkr6sjOHcYRvzLEDvEsRdDcQPRy0YUtmgBCNdio7Fk= -github.com/ygrebnov/log v0.5.1/go.mod h1:Y21zKtQ3rEy1FDhSli2NvgaqVyig3hhUfxegjLPuknw= -github.com/ygrebnov/model v0.8.1 h1:KvnS97b/q3Z1GKSHSDYlfHoL3bNDR+ZT9Kf4yc1WnIE= -github.com/ygrebnov/model v0.8.1/go.mod h1:WEMMqdGYF45Kfce0pAe0poiF9hu+PONRSHd2L9li3tw= +github.com/ygrebnov/log v0.6.0 h1:BwIDPh3uE/kCTRV0rsSp7i8NDv0ayfYOJRrypXhLdOI= +github.com/ygrebnov/log v0.6.0/go.mod h1:ks5SJPRtemWcKFpXwi9pMxJ92cMWjqnmNQa2QXiXd+4= +github.com/ygrebnov/model v0.9.0 h1:G6QaYVcm9Nqyed/lPzkWfQ543ol9D0C8Yjz9gVUDy9Q= +github.com/ygrebnov/model v0.9.0/go.mod h1:KA8zospuMg3/LbNc+Lgh+8nEz1uR9nv8SN4Vxxt2ruM= go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y= go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU= golang.org/x/mod v0.42.0 h1:ICeL3t2L30ljSPHHxDnR4fqVcBt/zR2zJAb9juavmQg= @@ -40,8 +42,8 @@ golang.org/x/tools v0.52.0 h1:ryxg4U63oaUYk7uOLTF37n43f4qgtGvqV4Ioit1BaEs= golang.org/x/tools v0.52.0/go.mod h1:ItcP5UNaVGG+Yc4eIYmaFLNJ4soEopkx5Y2d4lcEurs= gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4= gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E= -google.golang.org/genproto/googleapis/rpc v0.0.0-20261118221215-c8830f628b3f h1:aDW6Vb2T1K0xbAgW+ED8iz39WlgjjX/OKiNh6tXv/+g= -google.golang.org/genproto/googleapis/rpc v0.0.0-20261118221215-c8830f628b3f/go.mod h1:OaIUM3+LpYcK2GXM4FTmhWoIq371Owdr+Cc7/BsYHHc= +google.golang.org/genproto/googleapis/rpc v0.0.0-20261123191846-1e707ab9fbc2 h1:FCWn4wv5of/ooWpbWDIeOXqI3+h2MGnV7SvvcJ8f+bQ= +google.golang.org/genproto/googleapis/rpc v0.0.0-20261123191846-1e707ab9fbc2/go.mod h1:OaIUM3+LpYcK2GXM4FTmhWoIq371Owdr+Cc7/BsYHHc= google.golang.org/grpc v1.85.0 h1:BPBCfuVRQ1qvBhKDOq9uvQllMvO59rKsYhXpp7Dd8vw= google.golang.org/grpc v1.85.0/go.mod h1:0rQrOD58yjal/0EaVbGBANindixsCZNQPDV3VKtaKuQ= google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc= diff --git a/util/file.go b/util/file.go index 1b4b107..57b2c98 100644 --- a/util/file.go +++ b/util/file.go @@ -1,74 +1,31 @@ package util import ( - "io" + "errors" "os" "path/filepath" "github.com/ygrebnov/errorc" + sharedfs "github.com/ygrebnov/fs" - "github.com/e2engine/core/pkg/errors" + coreerrors "github.com/e2engine/core/pkg/errors" "github.com/e2engine/core/pkg/keys" ) // MaxFileSize limits files loaded into memory through this package. const MaxFileSize int64 = 16 << 20 // 16 MiB -// validateRegularFile checks the opened file rather than a separate path lookup. -func validateRegularFile(file *os.File, path string) error { - info, err := file.Stat() - if err != nil { - return errorc.With( - errors.ErrInaccessiblePath, - errorc.String(keys.FilePath, path), - errorc.Error(keys.Cause, err), - ) - } - - if !info.Mode().IsRegular() { - return errorc.With( - errors.ErrInaccessiblePath, - errorc.String(keys.FilePath, path), - errorc.String(keys.Validation, "not a regular file"), - ) +func fileError(path string, err error) error { + fields := []errorc.Field{errorc.String(keys.FilePath, path)} + switch { + case errors.Is(err, sharedfs.ErrNotRegularFile): + fields = append(fields, errorc.String(keys.Validation, "not a regular file")) + case errors.Is(err, sharedfs.ErrExceedsFileSizeLimit): + fields = append(fields, errorc.String(keys.Validation, "file size exceeds maximum allowed")) + default: + fields = append(fields, errorc.Error(keys.Cause, err)) } - - if info.Size() > MaxFileSize { - return errorc.With( - errors.ErrInaccessiblePath, - errorc.String(keys.FilePath, path), - errorc.String(keys.Validation, "file size exceeds maximum allowed"), - ) - } - - return nil -} - -func readOpenedFile(file *os.File, path string) ([]byte, error) { - if err := validateRegularFile(file, path); err != nil { - return nil, err - } - - data, err := io.ReadAll( - io.LimitReader(file, MaxFileSize+1), - ) - if err != nil { - return nil, errorc.With( - errors.ErrInaccessiblePath, - errorc.String(keys.FilePath, path), - errorc.Error(keys.Cause, err), - ) - } - - if int64(len(data)) > MaxFileSize { - return nil, errorc.With( - errors.ErrInaccessiblePath, - errorc.String(keys.FilePath, path), - errorc.String(keys.Validation, "file size exceeds maximum allowed"), - ) - } - - return data, nil + return errorc.With(coreerrors.ErrInaccessiblePath, fields...) } // ValidateFilePath checks that path names an accessible, regular file @@ -76,17 +33,20 @@ func readOpenedFile(file *os.File, path string) ([]byte, error) { // // It does not restrict the file to a particular directory. func ValidateFilePath(path string) error { - file, err := openReadFile(path) + file, err := sharedfs.OpenRegularRead(path) if err != nil { - return errorc.With( - errors.ErrInaccessiblePath, - errorc.String(keys.FilePath, path), - errorc.Error(keys.Cause, err), - ) + return fileError(path, err) } defer file.Close() - return validateRegularFile(file, path) + info, err := file.Stat() + if err != nil { + return fileError(path, err) + } + if info.Size() > MaxFileSize { + return fileError(path, sharedfs.ErrExceedsFileSizeLimit) + } + return nil } // ReadFile reads an accessible, regular file up to MaxFileSize. @@ -94,17 +54,11 @@ func ValidateFilePath(path string) error { // It is not a directory confinement boundary. // Use ReadFileWithinRoot when directory confinement is required. func ReadFile(path string) ([]byte, error) { - file, err := openReadFile(path) + data, err := sharedfs.ReadFile(path, MaxFileSize) if err != nil { - return nil, errorc.With( - errors.ErrInaccessiblePath, - errorc.String(keys.FilePath, path), - errorc.Error(keys.Cause, err), - ) + return nil, fileError(path, err) } - defer file.Close() - - return readOpenedFile(file, path) + return data, nil } // ReadFileWithinRoot reads a file confined to root. @@ -114,7 +68,7 @@ func ReadFile(path string) ([]byte, error) { func ReadFileWithinRoot(root, path string) ([]byte, error) { if filepath.IsAbs(path) || path == "" { return nil, errorc.With( - errors.ErrInaccessiblePath, + coreerrors.ErrInaccessiblePath, errorc.String(keys.FilePath, path), errorc.String(keys.Validation, "path must be non-empty and relative to root"), ) @@ -122,23 +76,13 @@ func ReadFileWithinRoot(root, path string) ([]byte, error) { dir, err := os.OpenRoot(root) if err != nil { - return nil, errorc.With( - errors.ErrInaccessiblePath, - errorc.String(keys.FilePath, root), - errorc.Error(keys.Cause, err), - ) + return nil, fileError(root, err) } defer dir.Close() - file, err := openRootReadFile(dir, path) + data, err := sharedfs.ReadFileWithinRoot(dir, path, MaxFileSize) if err != nil { - return nil, errorc.With( - errors.ErrInaccessiblePath, - errorc.String(keys.FilePath, path), - errorc.Error(keys.Cause, err), - ) + return nil, fileError(path, err) } - defer file.Close() - - return readOpenedFile(file, path) + return data, nil } diff --git a/util/file_open_other.go b/util/file_open_other.go deleted file mode 100644 index a20ade3..0000000 --- a/util/file_open_other.go +++ /dev/null @@ -1,30 +0,0 @@ -//go:build !aix && !darwin && !dragonfly && !freebsd && !linux && !netbsd && !openbsd && !solaris - -package util - -import ( - "errors" - "os" -) - -func openReadFile(path string) (*os.File, error) { - info, err := os.Stat(path) - if err != nil { - return nil, err - } - if !info.Mode().IsRegular() { - return nil, errors.New("not a regular file") - } - return os.Open(path) -} - -func openRootReadFile(root *os.Root, path string) (*os.File, error) { - info, err := root.Stat(path) - if err != nil { - return nil, err - } - if !info.Mode().IsRegular() { - return nil, errors.New("not a regular file") - } - return root.Open(path) -} diff --git a/util/file_open_unix.go b/util/file_open_unix.go deleted file mode 100644 index 62c28ae..0000000 --- a/util/file_open_unix.go +++ /dev/null @@ -1,17 +0,0 @@ -//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris - -package util - -import ( - "os" - "syscall" -) - -// A FIFO or device must not block a command before its file type is checked. -func openReadFile(path string) (*os.File, error) { - return os.OpenFile(path, os.O_RDONLY|syscall.O_NONBLOCK, 0) -} - -func openRootReadFile(root *os.Root, path string) (*os.File, error) { - return root.OpenFile(path, os.O_RDONLY|syscall.O_NONBLOCK, 0) -} From 9c5cab6f11fad368c2240b45804e3fc9b8367a2e Mon Sep 17 00:00:00 2001 From: Yaroslav Grebnov Date: Tue, 24 Nov 2026 19:34:17 +0100 Subject: [PATCH 2/6] Update tools --- README.md | 10 +++++++--- tools/go.mod | 11 +++++++---- tools/tools.go | 12 ------------ 3 files changed, 14 insertions(+), 19 deletions(-) delete mode 100644 tools/tools.go diff --git a/README.md b/README.md index b8163d0..2d757ba 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,9 @@ Core domain model, execution logic, and public APIs for E2Engine. -This module provides the shared models and execution components used across E2Engine, including test and environment definitions, test execution, runtime service management, scheduling, persistence interfaces, and transport primitives. - Part of [E2Engine](https://e2engine.dev), an open-source platform for declarative end-to-end testing. -E2Engine project on GitHub: [E2Engine on GitHub](https://github.com/e2engine). +This module provides the shared models and execution components used across E2Engine, including test and environment definitions, test execution, runtime service management, scheduling, persistence interfaces, and transport primitives. ## Installation @@ -66,6 +64,12 @@ Run the complete verification suite: make verify ``` +## Links + +- [E2Engine website](https://e2engine.dev) +- [E2Engine documentation](https://e2engine.dev/docs) +- [E2Engine on GitHub](https://github.com/e2engine) + ## License Licensed under the Apache License, Version 2.0. See [LICENSE](LICENSE). \ No newline at end of file diff --git a/tools/go.mod b/tools/go.mod index 9d886cc..627fe44 100644 --- a/tools/go.mod +++ b/tools/go.mod @@ -2,10 +2,10 @@ module tools go 1.27 -require ( - github.com/golangci/golangci-lint/v2 v2.15.0 - go.uber.org/mock v0.6.0 - golang.org/x/vuln v1.10.0 +tool ( + github.com/golangci/golangci-lint/v2/cmd/golangci-lint + go.uber.org/mock/mockgen + golang.org/x/vuln/cmd/govulncheck ) require ( @@ -92,6 +92,7 @@ require ( github.com/golangci/dupl v0.0.0-20260401084720-c99c5cf5c202 // indirect github.com/golangci/go-printf-func-name v0.1.1 // indirect github.com/golangci/gofmt v0.0.0-20260820135601-e84e05053792 // indirect + github.com/golangci/golangci-lint/v2 v2.15.0 // indirect github.com/golangci/golines v0.16.0 // indirect github.com/golangci/misspell v0.8.0 // indirect github.com/golangci/plugin-module-register v0.1.2 // indirect @@ -203,6 +204,7 @@ require ( go-simpler.org/sloglint v0.12.0 // indirect go.augendre.info/arangolint v0.4.0 // indirect go.augendre.info/fatcontext v0.10.1 // indirect + go.uber.org/mock v0.6.0 // indirect go.uber.org/multierr v1.10.0 // indirect go.uber.org/zap v1.27.0 // indirect go.yaml.in/yaml/v3 v3.0.5 // indirect @@ -213,6 +215,7 @@ require ( golang.org/x/telemetry v0.0.0-20261116191925-e77a43154acb // indirect golang.org/x/text v0.41.0 // indirect golang.org/x/tools v0.53.0 // indirect + golang.org/x/vuln v1.10.0 // indirect google.golang.org/protobuf v1.36.12 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/tools/tools.go b/tools/tools.go deleted file mode 100644 index 2b8f58d..0000000 --- a/tools/tools.go +++ /dev/null @@ -1,12 +0,0 @@ -//go:build tools - -// This file is for managing Go programs version with `go.mod`, which allows -// them to be kept up-to-date through tools like Dependabot. - -package tools - -import ( - _ "github.com/golangci/golangci-lint/v2/cmd/golangci-lint" - _ "go.uber.org/mock/mockgen" - _ "golang.org/x/vuln/cmd/govulncheck" -) From 9edad7075b884042ae97ee9985cf04bee4e10ff7 Mon Sep 17 00:00:00 2001 From: Yaroslav Grebnov Date: Wed, 25 Nov 2026 14:14:05 +0100 Subject: [PATCH 3/6] Use the updated github.com/ygrebnov/errorc API --- api/service.go | 4 +- execute/runtime/environment.go | 5 +- .../runtime/service/grpc/methodresolver.go | 6 +- .../service/grpc/methodresolver_test.go | 6 +- execute/worker/grpc/evaluate.go | 5 +- execute/worker/grpc/executor.go | 6 +- internal/cursor/cursor.go | 30 ++------- internal/cursor/cursor_test.go | 44 +++++++++++++ internal/resource/controller.go | 6 +- internal/resource/environment.go | 2 +- internal/resource/test.go | 2 +- internal/resource/testexecution.go | 3 +- internal/resource/testsuite.go | 2 +- internal/resource/testsuiteexecution.go | 6 +- model/environment.go | 14 +++-- model/test.go | 8 ++- model/testsuite.go | 8 ++- model/validation.go | 33 ---------- tests/environment_test.go | 10 +++ transport/socket/sender.go | 5 +- util/{ => fs}/file.go | 2 +- util/{ => fs}/file_fifo_unix_test.go | 2 +- util/{ => fs}/file_test.go | 2 +- util/render/validation_error.go | 56 +++++++++++++++++ util/render/validation_error_test.go | 61 +++++++++++++++++++ 25 files changed, 218 insertions(+), 110 deletions(-) rename util/{ => fs}/file.go (99%) rename util/{ => fs}/file_fifo_unix_test.go (98%) rename util/{ => fs}/file_test.go (99%) create mode 100644 util/render/validation_error.go create mode 100644 util/render/validation_error_test.go diff --git a/api/service.go b/api/service.go index 414b264..8ca772c 100644 --- a/api/service.go +++ b/api/service.go @@ -80,9 +80,7 @@ func WithRuntimeConfig(config *runtimeconfig.Config) Option { func getSettings(opts ...Option) (*settings, error) { logger, err := log.NewSilentLogger() if err != nil { - return nil, errorc.With( - errors.ErrCannotInitializeLogger, errorc.Error(keys.Cause, err), - ) + return nil, errorc.WithCause(errors.ErrCannotInitializeLogger, err) } s := &settings{ diff --git a/execute/runtime/environment.go b/execute/runtime/environment.go index 5d2293b..41d93ad 100644 --- a/execute/runtime/environment.go +++ b/execute/runtime/environment.go @@ -98,10 +98,7 @@ func (e *Environment) Mount(ctx context.Context) error { } if err := e.router.Mount(ctx); err != nil { - rErr := errorc.With( - errors.ErrFailedToMountRouter, - errorc.Error(keys.Cause, err), - ) + rErr := errorc.WithCause(errors.ErrFailedToMountRouter, err) return nativeerrors.Join(rErr, e.cleanupRouterAndServices(len(e.services)-1)) } diff --git a/execute/runtime/service/grpc/methodresolver.go b/execute/runtime/service/grpc/methodresolver.go index ce40055..3b90166 100644 --- a/execute/runtime/service/grpc/methodresolver.go +++ b/execute/runtime/service/grpc/methodresolver.go @@ -21,7 +21,7 @@ import ( "github.com/e2engine/core/model" "github.com/e2engine/core/pkg/errors" "github.com/e2engine/core/pkg/keys" - "github.com/e2engine/core/util" + "github.com/e2engine/core/util/fs" ) type DescriptorLoader interface { @@ -135,7 +135,7 @@ func (r *ProtoMethodResolver) loadFile( ) } - if errValidate := util.ValidateFilePath(absolutePath); errValidate != nil { + if errValidate := fs.ValidateFilePath(absolutePath); errValidate != nil { return nil, errorc.With( errors.ErrInaccessiblePath, errorc.String(keys.FilePath, path), @@ -156,7 +156,7 @@ func (r *ProtoMethodResolver) loadFile( Resolver: protocompile.WithStandardImports( &protocompile.SourceResolver{ Accessor: func(name string) (io.ReadCloser, error) { - data, errRead := util.ReadFileWithinRoot(root, name) + data, errRead := fs.ReadFileWithinRoot(root, name) if errRead != nil { return nil, errRead } diff --git a/execute/runtime/service/grpc/methodresolver_test.go b/execute/runtime/service/grpc/methodresolver_test.go index 53c465b..ac7762b 100644 --- a/execute/runtime/service/grpc/methodresolver_test.go +++ b/execute/runtime/service/grpc/methodresolver_test.go @@ -13,7 +13,7 @@ import ( "github.com/e2engine/core/model" "github.com/e2engine/core/pkg/errors" - "github.com/e2engine/core/util" + "github.com/e2engine/core/util/fs" ) func TestProtoMethodResolverResolveExternalModule(t *testing.T) { @@ -935,7 +935,7 @@ message Secret {} defer file.Close() if err := file.Truncate( - util.MaxFileSize + 1, + fs.MaxFileSize + 1, ); err != nil { t.Fatal(err) } @@ -1014,7 +1014,7 @@ func TestProtoMethodResolverResolveExternalFileTooLarge(t *testing.T) { t.Fatal(err) } - if err := file.Truncate(util.MaxFileSize + 1); err != nil { + if err := file.Truncate(fs.MaxFileSize + 1); err != nil { _ = file.Close() t.Fatal(err) } diff --git a/execute/worker/grpc/evaluate.go b/execute/worker/grpc/evaluate.go index dcecb59..579d8e2 100644 --- a/execute/worker/grpc/evaluate.go +++ b/execute/worker/grpc/evaluate.go @@ -222,10 +222,7 @@ func (e *Evaluator) Matches( actual.GRPC.Request.Message, actualMessage, ); err != nil { - return false, errorc.With( - ErrCannotDecodeCapturedRequest, - errorc.Error(keys.Cause, err), - ) + return false, errorc.WithCause(ErrCannotDecodeCapturedRequest, err) } return match.IsGRPCMessageMatched( diff --git a/execute/worker/grpc/executor.go b/execute/worker/grpc/executor.go index cfc362b..d8bf3a7 100644 --- a/execute/worker/grpc/executor.go +++ b/execute/worker/grpc/executor.go @@ -17,7 +17,6 @@ import ( servicegrpc "github.com/e2engine/core/execute/runtime/service/grpc" "github.com/e2engine/core/model" "github.com/e2engine/core/pkg/errors" - "github.com/e2engine/core/pkg/keys" ) type TestExecutor struct { @@ -82,10 +81,7 @@ func (e *TestExecutor) Execute( spec.Message, request, ); errUnmarshal != nil { - return errorc.With( - errors.ErrCannotBuildGRPCRequest, - errorc.Error(keys.Cause, errUnmarshal), - ) + return errorc.WithCause(errors.ErrCannotBuildGRPCRequest, errUnmarshal) } if proto.Size(request) > e.cfg.MaxMessageSize { diff --git a/internal/cursor/cursor.go b/internal/cursor/cursor.go index d069f0c..42143f3 100644 --- a/internal/cursor/cursor.go +++ b/internal/cursor/cursor.go @@ -54,18 +54,12 @@ func Parse(ctx context.Context, binding *modellib.Binding[Payload], opaque strin data, err := base64.RawURLEncoding.DecodeString(opaque) if err != nil { - return Payload{}, errorc.With( - errors.ErrCannotDecodeCursor, - errorc.Error(keys.Cause, err), - ) + return Payload{}, errorc.WithCause(errors.ErrCannotDecodeCursor, err) } var payload Payload if err := json.Unmarshal(data, &payload); err != nil { - return Payload{}, errorc.With( - errors.ErrCannotDecodeCursor, - errorc.Error(keys.Cause, err), - ) + return Payload{}, errorc.WithCause(errors.ErrCannotDecodeCursor, err) } if payload.Version != CurrentVersion { @@ -77,10 +71,7 @@ func Parse(ctx context.Context, binding *modellib.Binding[Payload], opaque strin } if err := binding.Validate(ctx, &payload); err != nil { - return Payload{}, errorc.With( - errors.ErrInvalidCursor, - errorc.Error(keys.Cause, err), - ) + return Payload{}, errorc.WithCause(errors.ErrInvalidCursor, err) } return payload, nil @@ -88,25 +79,16 @@ func Parse(ctx context.Context, binding *modellib.Binding[Payload], opaque strin func Encode(ctx context.Context, binding *modellib.Binding[Payload], payload Payload) (string, error) { if err := binding.ApplyDefaults(&payload); err != nil { - return "", errorc.With( - errors.ErrCannotComposeCursorPayload, - errorc.Error(keys.Cause, err), - ) + return "", errorc.WithCause(errors.ErrCannotComposeCursorPayload, err) } if err := binding.Validate(ctx, &payload); err != nil { - return "", errorc.With( - errors.ErrInvalidCursor, - errorc.Error(keys.Cause, err), - ) + return "", errorc.WithCause(errors.ErrInvalidCursor, err) } data, err := json.Marshal(payload) if err != nil { - return "", errorc.With( - errors.ErrCannotEncodeCursor, - errorc.Error(keys.Cause, err), - ) + return "", errorc.WithCause(errors.ErrCannotEncodeCursor, err) } encoded := base64.RawURLEncoding.EncodeToString(data) diff --git a/internal/cursor/cursor_test.go b/internal/cursor/cursor_test.go index 9b404b7..04210d3 100644 --- a/internal/cursor/cursor_test.go +++ b/internal/cursor/cursor_test.go @@ -4,11 +4,16 @@ import ( "context" "encoding/base64" "encoding/json" + nativeerrors "errors" "strings" "testing" + "github.com/ygrebnov/errorc" + "github.com/ygrebnov/model/validation" + "github.com/e2engine/core/model" "github.com/e2engine/core/pkg/errors" + "github.com/e2engine/core/pkg/keys" ) var id1 = "28b94fc0206dfb7960905c372905772441cc41d3cde3dcd274543e6b4435367f" @@ -402,6 +407,45 @@ func TestEncodeParseNearMaxCursorLength(t *testing.T) { } } +func TestParseRetainsCauses(t *testing.T) { + binding, err := GetBinding() + if err != nil { + t.Fatal(err) + } + + invalidJSON := base64.RawURLEncoding.EncodeToString([]byte(`{"version":`)) + _, err = Parse(t.Context(), binding, invalidJSON) + if !nativeerrors.Is(err, errors.ErrCannotDecodeCursor) { + t.Fatalf("Parse() error = %v, want ErrCannotDecodeCursor", err) + } + var syntaxErr *json.SyntaxError + if !nativeerrors.As(err, &syntaxErr) { + t.Fatalf("Parse() error = %v, want *json.SyntaxError in chain", err) + } + if cause, ok := errorc.Lookup[error](err, keys.Cause); !ok || cause != syntaxErr { + t.Fatalf("cause field = %v, %v; want %v", cause, ok, syntaxErr) + } + + invalidPayload := encodeRawPayload(t, Payload{ + Version: CurrentVersion, + ID: id1, + Position: PositionAfter, + OrderBy: model.OrderByID, + OrderDirection: model.OrderDirectionAsc, + }) + _, err = Parse(t.Context(), binding, invalidPayload) + if !nativeerrors.Is(err, errors.ErrInvalidCursor) { + t.Fatalf("Parse() error = %v, want ErrInvalidCursor", err) + } + var validationErr *validation.Error + if !nativeerrors.As(err, &validationErr) { + t.Fatalf("Parse() error = %v, want *validation.Error in chain", err) + } + if cause, ok := errorc.Lookup[error](err, keys.Cause); !ok || cause != validationErr { + t.Fatalf("cause field = %v, %v; want %v", cause, ok, validationErr) + } +} + func encodeRawPayload(t *testing.T, payload Payload) string { t.Helper() diff --git a/internal/resource/controller.go b/internal/resource/controller.go index 2705a58..6022a44 100644 --- a/internal/resource/controller.go +++ b/internal/resource/controller.go @@ -6,7 +6,6 @@ import ( "github.com/e2engine/core/internal/cursor" "github.com/e2engine/core/pkg/errors" - "github.com/e2engine/core/pkg/keys" "github.com/e2engine/core/repository" ) @@ -34,10 +33,7 @@ func NewController( ) (*Controller, error) { cursorBinding, err := cursor.GetBinding() if err != nil { - return nil, errorc.With( - errors.ErrCannotConstructCursorPayloadBinding, - errorc.Error(keys.Cause, err), - ) + return nil, errorc.WithCause(errors.ErrCannotConstructCursorPayloadBinding, err) } return &Controller{ diff --git a/internal/resource/environment.go b/internal/resource/environment.go index b109916..af056ac 100644 --- a/internal/resource/environment.go +++ b/internal/resource/environment.go @@ -177,7 +177,7 @@ func newEnvironmentFromRepository(r *repository.Environment) (*model.Environment var spec model.EnvironmentSpec if err := json.Unmarshal(r.Spec, &spec); err != nil { - return nil, errorc.With(errors.ErrCannotParseSpec, errorc.Error(keys.Cause, err)) + return nil, errorc.WithCause(errors.ErrCannotParseSpec, err) } return &model.Environment{ diff --git a/internal/resource/test.go b/internal/resource/test.go index 716421b..15365ba 100644 --- a/internal/resource/test.go +++ b/internal/resource/test.go @@ -180,7 +180,7 @@ func newTestFromRepository(r *repository.Test) (*model.Test, error) { var spec model.TestSpec if err := json.Unmarshal(r.Spec, &spec); err != nil { - return nil, errorc.With(errors.ErrCannotParseSpec, errorc.Error(keys.Cause, err)) + return nil, errorc.WithCause(errors.ErrCannotParseSpec, err) } return &model.Test{ diff --git a/internal/resource/testexecution.go b/internal/resource/testexecution.go index 9e5c84d..a6cc395 100644 --- a/internal/resource/testexecution.go +++ b/internal/resource/testexecution.go @@ -9,7 +9,6 @@ import ( "github.com/e2engine/core/model" "github.com/e2engine/core/pkg/errors" - "github.com/e2engine/core/pkg/keys" "github.com/e2engine/core/repository" ) @@ -156,7 +155,7 @@ func newTestExecutionFromRepository(r *repository.TestExecution) (*model.TestExe if len(r.Summary) > 0 { var summary model.TestExecutionSummary if err := json.Unmarshal(r.Summary, &summary); err != nil { - return nil, errorc.With(errors.ErrCannotParseTestExecutionSummary, errorc.Error(keys.Cause, err)) + return nil, errorc.WithCause(errors.ErrCannotParseTestExecutionSummary, err) } te.Summary = &summary } diff --git a/internal/resource/testsuite.go b/internal/resource/testsuite.go index e6dd07e..63c06c8 100644 --- a/internal/resource/testsuite.go +++ b/internal/resource/testsuite.go @@ -174,7 +174,7 @@ func newTestSuiteFromRepository(r *repository.TestSuite) (*model.TestSuite, erro var spec model.TestSuiteSpec if err := json.Unmarshal(r.Spec, &spec); err != nil { - return nil, errorc.With(errors.ErrCannotParseSpec, errorc.Error(keys.Cause, err)) + return nil, errorc.WithCause(errors.ErrCannotParseSpec, err) } return &model.TestSuite{ diff --git a/internal/resource/testsuiteexecution.go b/internal/resource/testsuiteexecution.go index 5d2966d..ef68a0c 100644 --- a/internal/resource/testsuiteexecution.go +++ b/internal/resource/testsuiteexecution.go @@ -9,7 +9,6 @@ import ( "github.com/e2engine/core/model" "github.com/e2engine/core/pkg/errors" - "github.com/e2engine/core/pkg/keys" "github.com/e2engine/core/repository" ) @@ -177,10 +176,7 @@ func newTestSuiteExecutionFromRepository(r *repository.TestSuiteExecution) (*mod if len(r.Summary) > 0 { var summary model.TestSuiteExecutionSummary if err := json.Unmarshal(r.Summary, &summary); err != nil { - return nil, errorc.With( - errors.ErrCannotParseTestSuiteExecutionSummary, - errorc.Error(keys.Cause, err), - ) + return nil, errorc.WithCause(errors.ErrCannotParseTestSuiteExecutionSummary, err) } tse.Summary = &summary } diff --git a/model/environment.go b/model/environment.go index f5847e2..7c4868c 100644 --- a/model/environment.go +++ b/model/environment.go @@ -28,7 +28,11 @@ func (e *Environment) Validate(ctx context.Context) error { } if errValidate := binding.Validate(ctx, e); errValidate != nil { - return handleValidationError(errValidate, EntityKindEnvironment) + return errorc.With( + errors.ErrInvalidSpec, + errorc.String(keys.SpecKind, string(EntityKindEnvironment)), + errorc.Error(keys.Validation, errValidate), + ) } if len(e.Spec.Services) == 0 { @@ -298,7 +302,7 @@ func (s *ServiceSpec) Validate(index int) error { keys.Fields, "Spec.Services["+strconv.Itoa(index)+"].Target", ), - errorc.String(keys.Validation, err.Error()), + errorc.Error(keys.Validation, err), ) } case ServiceKindGRPC: @@ -310,7 +314,7 @@ func (s *ServiceSpec) Validate(index int) error { keys.Fields, "Spec.Services["+strconv.Itoa(index)+"].Target", ), - errorc.String(keys.Validation, err.Error()), + errorc.Error(keys.Validation, err), ) } } @@ -355,7 +359,7 @@ func (s *ServiceSpec) Validate(index int) error { keys.Fields, "Spec.Services["+strconv.Itoa(index)+"].Fixtures["+strconv.Itoa(i)+"].When.HTTP", ), - errorc.String(keys.Validation, err.Error()), + errorc.Error(keys.Validation, err), ) } @@ -367,7 +371,7 @@ func (s *ServiceSpec) Validate(index int) error { keys.Fields, "Spec.Services["+strconv.Itoa(index)+"].Fixtures["+strconv.Itoa(i)+"].Then.HTTP", ), - errorc.String(keys.Validation, err.Error()), + errorc.Error(keys.Validation, err), ) } diff --git a/model/test.go b/model/test.go index dabcfe6..80abd81 100644 --- a/model/test.go +++ b/model/test.go @@ -23,7 +23,11 @@ func (t *Test) Validate(ctx context.Context) error { } if errValidate := binding.Validate(ctx, t); errValidate != nil { - return handleValidationError(errValidate, EntityKindTest) + return errorc.With( + errors.ErrInvalidSpec, + errorc.String(keys.SpecKind, string(EntityKindTest)), + errorc.Error(keys.Validation, errValidate), + ) } if index, errValidateTags := t.Spec.validateTags(); errValidateTags != nil { @@ -116,7 +120,7 @@ func invalidTestSpec(validation error, fields string) error { errors.ErrInvalidSpec, errorc.String(keys.SpecKind, string(EntityKindTest)), errorc.String(keys.Fields, fields), - errorc.String(keys.Validation, validation.Error()), + errorc.Error(keys.Validation, validation), ) } diff --git a/model/testsuite.go b/model/testsuite.go index b2d4a33..2856ff8 100644 --- a/model/testsuite.go +++ b/model/testsuite.go @@ -24,7 +24,11 @@ func (t *TestSuite) Validate(ctx context.Context) error { } if errValidate := binding.Validate(ctx, t); errValidate != nil { - return handleValidationError(errValidate, EntityKindTestSuite) + return errorc.With( + errors.ErrInvalidSpec, + errorc.String(keys.SpecKind, string(EntityKindTestSuite)), + errorc.Error(keys.Validation, errValidate), + ) } if fields, err := t.Spec.Selectors.Validate(); err != nil { @@ -43,7 +47,7 @@ func invalidTestSuiteSpec(validation error, fields string) error { errors.ErrInvalidSpec, errorc.String(keys.SpecKind, string(EntityKindTestSuite)), errorc.String(keys.Fields, fields), - errorc.String(keys.Validation, validation.Error()), + errorc.Error(keys.Validation, validation), ) } diff --git a/model/validation.go b/model/validation.go index f869ce0..cfab5ac 100644 --- a/model/validation.go +++ b/model/validation.go @@ -2,7 +2,6 @@ package model import ( "encoding/json" - nativeerrors "errors" "net" "net/url" "regexp" @@ -13,11 +12,8 @@ import ( "github.com/ygrebnov/errorc" modellib "github.com/ygrebnov/model" - "github.com/ygrebnov/model/validation" "github.com/e2engine/core/model/matcher" - "github.com/e2engine/core/pkg/errors" - "github.com/e2engine/core/pkg/keys" ) var ( @@ -178,32 +174,3 @@ func getIPRule() modellib.Rule { return nil }) } - -func handleValidationError(err error, specKind EntityKind) error { - var fields string - - if validationErr, ok := nativeerrors.AsType[*validation.Error](err); ok { - fields = strings.Join(validationErr.Fields(), ", ") - - validationErrs := make([]string, 0, validationErr.Len()) - for _, path := range validationErr.Fields() { - for _, fieldErr := range validationErr.ForField(path) { - validationErrs = append(validationErrs, fieldErr.Err.Error()) - } - } - combinedErr := strings.Join(validationErrs, ", ") - - return errorc.With( - errors.ErrInvalidSpec, - errorc.String(keys.SpecKind, string(specKind)), - errorc.String(keys.Fields, fields), - errorc.String(keys.Validation, combinedErr), - ) - } - - return errorc.With( - errors.ErrInvalidSpec, - errorc.String(keys.SpecKind, string(specKind)), - errorc.String(keys.Validation, err.Error()), - ) -} diff --git a/tests/environment_test.go b/tests/environment_test.go index 42f907d..ec2bc4b 100644 --- a/tests/environment_test.go +++ b/tests/environment_test.go @@ -5,9 +5,11 @@ import ( "encoding/json" "errors" "reflect" + "strings" "testing" "time" + "github.com/ygrebnov/model/validation" "go.uber.org/mock/gomock" core "github.com/e2engine/core/api" @@ -16,6 +18,7 @@ import ( "github.com/e2engine/core/model" pkgerrors "github.com/e2engine/core/pkg/errors" "github.com/e2engine/core/repository" + "github.com/e2engine/core/util/render" ) func TestEnvironment_Create(t *testing.T) { @@ -57,6 +60,13 @@ func TestEnvironment_Create(t *testing.T) { pkgerrors.ErrInvalidRequestParameters, ) } + var validationErr *validation.Error + if !errors.As(err, &validationErr) || len(validationErr.Issues()) == 0 { + t.Fatalf("CreateEnvironment() error = %v, want validation issues in chain", err) + } + if rendered := render.ValidationError(err); !strings.Contains(rendered, "fields: Name, validation:") { + t.Fatalf("rendered validation error = %q, want concise Name validation", rendered) + } }) t.Run("success", func(t *testing.T) { diff --git a/transport/socket/sender.go b/transport/socket/sender.go index 2336f18..c34516a 100644 --- a/transport/socket/sender.go +++ b/transport/socket/sender.go @@ -36,10 +36,7 @@ func (s *Sender) Send( // Serialize before establishing the connection. data, err := json.Marshal(message) if err != nil { - return errorc.With( - ErrCannotSendMessage, - errorc.Error(keys.Cause, err), - ) + return errorc.WithCause(ErrCannotSendMessage, err) } // Account for the newline appended by json.Encoder.Encode. diff --git a/util/file.go b/util/fs/file.go similarity index 99% rename from util/file.go rename to util/fs/file.go index 57b2c98..6fa0c3a 100644 --- a/util/file.go +++ b/util/fs/file.go @@ -1,4 +1,4 @@ -package util +package fs import ( "errors" diff --git a/util/file_fifo_unix_test.go b/util/fs/file_fifo_unix_test.go similarity index 98% rename from util/file_fifo_unix_test.go rename to util/fs/file_fifo_unix_test.go index 2bd3a7c..67ba3a4 100644 --- a/util/file_fifo_unix_test.go +++ b/util/fs/file_fifo_unix_test.go @@ -1,6 +1,6 @@ //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris -package util +package fs import ( "errors" diff --git a/util/file_test.go b/util/fs/file_test.go similarity index 99% rename from util/file_test.go rename to util/fs/file_test.go index 132e05b..1ea1de4 100644 --- a/util/file_test.go +++ b/util/fs/file_test.go @@ -1,4 +1,4 @@ -package util +package fs import ( "bytes" diff --git a/util/render/validation_error.go b/util/render/validation_error.go new file mode 100644 index 0000000..3d937df --- /dev/null +++ b/util/render/validation_error.go @@ -0,0 +1,56 @@ +package render + +import ( + "errors" + "strings" + + "github.com/ygrebnov/errorc" + "github.com/ygrebnov/model/validation" + + coreerrors "github.com/e2engine/core/pkg/errors" + "github.com/e2engine/core/pkg/keys" +) + +// ValidationError formats a model validation error as a concise invalid +// spec message. It accepts errors wrapped by callers and falls back to the +// original error text when no structured spec validation is available. +func ValidationError(err error) string { + if err == nil { + return "" + } + + var validationErr *validation.Error + if !errors.Is(err, coreerrors.ErrInvalidSpec) || + !errors.As(err, &validationErr) || validationErr == nil { + return err.Error() + } + + kind, ok := errorc.Lookup[string](err, keys.SpecKind) + if !ok { + return err.Error() + } + + paths := validationErr.Fields() + if len(paths) == 0 { + return err.Error() + } + + messages := make([]string, 0, validationErr.Len()) + for _, path := range paths { + for _, issue := range validationErr.ForField(path) { + if issue.Err != nil { + messages = append(messages, issue.Err.Error()) + } + } + } + if len(messages) == 0 { + return err.Error() + } + + return errorc.With( + coreerrors.ErrInvalidSpec, + errorc.String(keys.SpecKind, kind), + errorc.String(keys.Fields, strings.Join(paths, ", ")), + errorc.String(keys.Validation, strings.Join(messages, ", ")), + ).Error() +} diff --git a/util/render/validation_error_test.go b/util/render/validation_error_test.go new file mode 100644 index 0000000..7bad229 --- /dev/null +++ b/util/render/validation_error_test.go @@ -0,0 +1,61 @@ +package render + +import ( + "errors" + "testing" + + "github.com/ygrebnov/errorc" + "github.com/ygrebnov/model/validation" + + "github.com/e2engine/core/model" + coreerrors "github.com/e2engine/core/pkg/errors" + "github.com/e2engine/core/pkg/keys" +) + +func TestValidationError(t *testing.T) { + validationErr := &validation.Error{} + validationErr.Addf("Name", "min", errors.New("too short")) + validationErr.Addf("Port", "min", errors.New("too small")) + validationErr.Addf("Name", "max", errors.New("too long")) + + err := errorc.With( + coreerrors.ErrInvalidSpec, + errorc.String(keys.SpecKind, string(model.EntityKindEnvironment)), + errorc.Error(keys.Validation, validationErr), + ) + if !errors.Is(err, coreerrors.ErrInvalidSpec) { + t.Fatalf("error = %v, want ErrInvalidSpec", err) + } + var got *validation.Error + if !errors.As(err, &got) || got != validationErr { + t.Fatalf("error = %v, want original *validation.Error in chain", err) + } + if value, ok := errorc.Lookup[error](err, keys.Validation); !ok || value != validationErr { + t.Fatalf("validation field = %v, %v; want original error", value, ok) + } + if issues := got.Issues(); len(issues) != 3 || issues[0].Path != "Name" { + t.Fatalf("issues = %+v, want three issues beginning with Name", issues) + } + const want = "invalid spec, spec.kind: environment, fields: Name, Port, validation: too short, too long, too small" + if text := ValidationError(err); text != want { + t.Fatalf("rendered error = %q, want %q", text, want) + } + wrapped := errorc.WithCause(coreerrors.ErrInvalidRequestParameters, err) + if text := ValidationError(wrapped); text != want { + t.Fatalf("rendered wrapped error = %q, want %q", text, want) + } +} + +func TestValidationErrorFallback(t *testing.T) { + if text := ValidationError(nil); text != "" { + t.Fatalf("rendered nil error = %q, want empty string", text) + } + plain := errors.New("other error") + if text := ValidationError(plain); text != plain.Error() { + t.Fatalf("rendered other error = %q, want %q", text, plain.Error()) + } + invalid := errorc.With(coreerrors.ErrInvalidSpec, errorc.String(keys.Validation, "invalid kind")) + if text := ValidationError(invalid); text != invalid.Error() { + t.Fatalf("rendered unstructured error = %q, want %q", text, invalid.Error()) + } +} From 386a27f952c2ee512781ca6f753a50500a0bf064 Mon Sep 17 00:00:00 2001 From: Yaroslav Grebnov Date: Wed, 25 Nov 2026 14:35:25 +0100 Subject: [PATCH 4/6] Update CI --- .github/workflows/build.yml | 121 ++++++++++++++++++++++++++++++++++-- Makefile | 3 + 2 files changed, 118 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 571621f..f8c9341 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -10,7 +10,8 @@ permissions: contents: read jobs: - build: + dependencies: + name: Verify dependencies runs-on: ubuntu-latest steps: @@ -20,14 +21,122 @@ jobs: - name: Setup Go uses: actions/setup-go@v7 with: - go-version: "1.27.x" + go-version: '1.27.x' check-latest: true cache-dependency-path: | go.sum tools/go.sum - - name: Go version - run: go version + - name: Download modules + run: go mod download - - name: Verify - run: make verify \ No newline at end of file + - name: Verify modules + run: go mod verify + + generated: + name: Verify generated mocks + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v7 + + - name: Setup Go + uses: actions/setup-go@v7 + with: + go-version: '1.27.x' + check-latest: true + cache-dependency-path: | + go.sum + tools/go.sum + + - name: Verify generated mocks + run: make mockgen + + lint: + name: Lint + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v7 + + - name: Setup Go + uses: actions/setup-go@v7 + with: + go-version: '1.27.x' + check-latest: true + cache-dependency-path: | + go.sum + tools/go.sum + + - name: Lint + uses: golangci/golangci-lint-action@v9 + with: + version: v2.15.0 + args: --timeout=10m + + vulncheck: + name: Check vulnerabilities + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v7 + + - name: Setup Go + uses: actions/setup-go@v7 + with: + go-version: '1.27.x' + check-latest: true + cache-dependency-path: | + go.sum + tools/go.sum + + - name: Check vulnerabilities + run: make vulncheck + + test: + name: Test (${{ matrix.os }}) + runs-on: ${{ matrix.os }} + + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest, windows-latest] + + steps: + - name: Checkout + uses: actions/checkout@v7 + + - name: Setup Go + uses: actions/setup-go@v7 + with: + go-version: '1.27.x' + check-latest: true + cache-dependency-path: | + go.sum + tools/go.sum + + - name: Test + run: go test ./... -count=1 -timeout=600s + + race: + name: Race detector + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v7 + + - name: Setup Go + uses: actions/setup-go@v7 + with: + go-version: '1.27.x' + check-latest: true + cache-dependency-path: | + go.sum + tools/go.sum + + - name: Race tests + run: go test -race ./... -count=1 -timeout=600s diff --git a/Makefile b/Makefile index 5e580d1..8af3f31 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ ROOT_PATH := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) COVERAGE_PATH := $(ROOT_PATH).coverage/ +MOCK_GENERATED_PATHS := mock/ ':(glob)**/mock_*_test.go' # Use the locally installed Go toolchain. export GOTOOLCHAIN=local @@ -18,6 +19,8 @@ mockgen: install-mockgen $(MOCKGEN) -destination=mock/Runtime.go -package=mock github.com/e2engine/core/execute/runtime Service,ServiceProvider,Router,RouterProvider $(MOCKGEN) -destination=mock/Execute.go -package=mock github.com/e2engine/core/execute ProtocolExecutor go generate ./... + @git diff HEAD --exit-code -- $(MOCK_GENERATED_PATHS) + @test -z "$$(git ls-files --others --exclude-standard -- $(MOCK_GENERATED_PATHS))" .PHONY: lint lint: install-golangci-lint From 2ce4a27783dd13c4161ec5d038b97366430904a2 Mon Sep 17 00:00:00 2001 From: Yaroslav Grebnov Date: Wed, 25 Nov 2026 14:49:59 +0100 Subject: [PATCH 5/6] Fix mockgen target --- Makefile | 6 +++--- mock/Execute.go | 2 +- mock/Repository.go | 2 +- mock/Runtime.go | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 8af3f31..228f0cb 100644 --- a/Makefile +++ b/Makefile @@ -15,9 +15,9 @@ mockgen: install-mockgen @echo "Generating mocks..." @rm -rf mock @mkdir -p mock - $(MOCKGEN) -destination=mock/Repository.go -package=mock github.com/e2engine/core/repository EntityRepository,ResourceRepository,TestRepository,TestExecutionRepository,TestSuiteExecutionRepository - $(MOCKGEN) -destination=mock/Runtime.go -package=mock github.com/e2engine/core/execute/runtime Service,ServiceProvider,Router,RouterProvider - $(MOCKGEN) -destination=mock/Execute.go -package=mock github.com/e2engine/core/execute ProtocolExecutor + $(MOCKGEN_LINK) -destination=mock/Repository.go -package=mock github.com/e2engine/core/repository EntityRepository,ResourceRepository,TestRepository,TestExecutionRepository,TestSuiteExecutionRepository + $(MOCKGEN_LINK) -destination=mock/Runtime.go -package=mock github.com/e2engine/core/execute/runtime Service,ServiceProvider,Router,RouterProvider + $(MOCKGEN_LINK) -destination=mock/Execute.go -package=mock github.com/e2engine/core/execute ProtocolExecutor go generate ./... @git diff HEAD --exit-code -- $(MOCK_GENERATED_PATHS) @test -z "$$(git ls-files --others --exclude-standard -- $(MOCK_GENERATED_PATHS))" diff --git a/mock/Execute.go b/mock/Execute.go index d0d9e4f..091875f 100644 --- a/mock/Execute.go +++ b/mock/Execute.go @@ -3,7 +3,7 @@ // // Generated by this command: // -// mockgen-darwin-arm64-v0.6.0 -destination=mock/Execute.go -package=mock github.com/e2engine/core/execute ProtocolExecutor +// mockgen -destination=mock/Execute.go -package=mock github.com/e2engine/core/execute ProtocolExecutor // // Package mock is a generated GoMock package. diff --git a/mock/Repository.go b/mock/Repository.go index 5f0ff26..51aa754 100644 --- a/mock/Repository.go +++ b/mock/Repository.go @@ -3,7 +3,7 @@ // // Generated by this command: // -// mockgen-darwin-arm64-v0.6.0 -destination=mock/Repository.go -package=mock github.com/e2engine/core/repository EntityRepository,ResourceRepository,TestRepository,TestExecutionRepository,TestSuiteExecutionRepository +// mockgen -destination=mock/Repository.go -package=mock github.com/e2engine/core/repository EntityRepository,ResourceRepository,TestRepository,TestExecutionRepository,TestSuiteExecutionRepository // // Package mock is a generated GoMock package. diff --git a/mock/Runtime.go b/mock/Runtime.go index 1ae3ff4..a894b16 100644 --- a/mock/Runtime.go +++ b/mock/Runtime.go @@ -3,7 +3,7 @@ // // Generated by this command: // -// mockgen-darwin-arm64-v0.6.0 -destination=mock/Runtime.go -package=mock github.com/e2engine/core/execute/runtime Service,ServiceProvider,Router,RouterProvider +// mockgen -destination=mock/Runtime.go -package=mock github.com/e2engine/core/execute/runtime Service,ServiceProvider,Router,RouterProvider // // Package mock is a generated GoMock package. From a52210f318ea7c703f5b55db3e38e3d6d538436c Mon Sep 17 00:00:00 2001 From: Yaroslav Grebnov Date: Wed, 25 Nov 2026 14:58:19 +0100 Subject: [PATCH 6/6] Fix shutdown race in gRPC router Unmount --- execute/runtime/router/grpc/router.go | 8 +++++++- execute/runtime/router/grpc/router_test.go | 17 +++++++++++------ execute/runtime/router/http/router_test.go | 13 +++++++++++-- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/execute/runtime/router/grpc/router.go b/execute/runtime/router/grpc/router.go index 08cdccc..f17fb25 100644 --- a/execute/runtime/router/grpc/router.go +++ b/execute/runtime/router/grpc/router.go @@ -193,7 +193,7 @@ func (r *Router) Mount(ctx context.Context) error { go func() { err := server.Serve(listener) - if err != nil { + if err != nil && !nativeerrors.Is(err, grpcpkg.ErrServerStopped) { r.logger.Error( "Environment gRPC router listener stopped unexpectedly", log.String( @@ -423,6 +423,12 @@ func (r *Router) unmountMountedRoutes( case <-done: } + // Stop may run before Serve registers the listener. + if err := route.listener.Close(); err != nil && + !nativeerrors.Is(err, net.ErrClosed) { + accErr = nativeerrors.Join(accErr, err) + } + if err := route.connection.Close(); err != nil { r.logger.Error( "Failed to close gRPC router connection", diff --git a/execute/runtime/router/grpc/router_test.go b/execute/runtime/router/grpc/router_test.go index 89c36e6..28161f8 100644 --- a/execute/runtime/router/grpc/router_test.go +++ b/execute/runtime/router/grpc/router_test.go @@ -793,7 +793,7 @@ func TestRouterUnmountCancelledContext(t *testing.T) { t.Fatalf("Mount() error = %v", err) } - address := router.mounted[0].listener.Addr().String() + listener := router.mounted[0].listener ctx, cancel := context.WithCancel(context.Background()) cancel() @@ -808,15 +808,20 @@ func TestRouterUnmountCancelledContext(t *testing.T) { t.Fatal("expected mounted routes to be cleared") } - connection, err := net.DialTimeout( - "tcp", - address, - time.Second, - ) + if err := listener.(*net.TCPListener).SetDeadline( + time.Now().Add(time.Second), + ); err != nil && !errors.Is(err, net.ErrClosed) { + t.Fatalf("SetDeadline() error = %v", err) + } + + connection, err := listener.Accept() if err == nil { _ = connection.Close() t.Fatal("listener still accepts connections after unmount") } + if !errors.Is(err, net.ErrClosed) { + t.Fatalf("Accept() error = %v, want net.ErrClosed", err) + } } func TestRouterMultipleRoutes(t *testing.T) { diff --git a/execute/runtime/router/http/router_test.go b/execute/runtime/router/http/router_test.go index 48701d5..49e2104 100644 --- a/execute/runtime/router/http/router_test.go +++ b/execute/runtime/router/http/router_test.go @@ -474,7 +474,7 @@ func TestRouterUnmountCancelledContext(t *testing.T) { t.Fatalf("Mount() error = %v", err) } - address := router.mounted[0].listener.Addr().String() + listener := router.mounted[0].listener ctx, cancel := context.WithCancel(context.Background()) cancel() @@ -492,11 +492,20 @@ func TestRouterUnmountCancelledContext(t *testing.T) { t.Fatal("expected mounted routes to be cleared") } - conn, err := net.DialTimeout("tcp", address, time.Second) + if err := listener.(*net.TCPListener).SetDeadline( + time.Now().Add(time.Second), + ); err != nil && !errors.Is(err, net.ErrClosed) { + t.Fatalf("SetDeadline() error = %v", err) + } + + conn, err := listener.Accept() if err == nil { _ = conn.Close() t.Fatal("listener still accepts connections after unmount") } + if !errors.Is(err, net.ErrClosed) { + t.Fatalf("Accept() error = %v, want net.ErrClosed", err) + } } func TestRouterMultipleRoutes(t *testing.T) {