forked from kubeshop/tracetest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): Implementing trigger preprocessor (kubeshop#3990)
* feat(cli): Implementing trigger preprocessor * feat(cli): Implementing trigger preprocessor * fixing logger bug
- Loading branch information
Showing
8 changed files
with
257 additions
and
75 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package trigger_preprocessor | ||
|
||
import ( | ||
"github.com/kubeshop/tracetest/agent/workers/trigger" | ||
"github.com/kubeshop/tracetest/cli/cmdutil" | ||
"github.com/kubeshop/tracetest/cli/openapi" | ||
"github.com/kubeshop/tracetest/cli/pkg/fileutil" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type graphql struct { | ||
logger *zap.Logger | ||
} | ||
|
||
func GRAPHQL(logger *zap.Logger) graphql { | ||
return graphql{logger: cmdutil.GetLogger()} | ||
} | ||
|
||
func (g graphql) Type() trigger.TriggerType { | ||
return trigger.TriggerTypeGraphql | ||
} | ||
|
||
func (g graphql) Preprocess(input fileutil.File, test openapi.TestResource) (openapi.TestResource, error) { | ||
// query can be defined separately in a file like: query: ./query.graphql | ||
rawQuery := test.Spec.Trigger.Graphql.Body.GetQuery() | ||
if isValidFilePath(rawQuery, input.AbsDir()) { | ||
queryFilePath := input.RelativeFile(rawQuery) | ||
g.logger.Debug("script file", zap.String("path", queryFilePath)) | ||
|
||
queryFile, err := fileutil.Read(queryFilePath) | ||
if err == nil { | ||
g.logger.Debug("script file contents", zap.String("contents", string(queryFile.Contents()))) | ||
test.Spec.Trigger.Graphql.Body.SetQuery(string(queryFile.Contents())) | ||
} | ||
} | ||
|
||
// schema can be defined separately in a file like: schema: ./schema.graphql | ||
rawSchema := test.Spec.Trigger.Graphql.GetSchema() | ||
if isValidFilePath(rawSchema, input.AbsDir()) { | ||
schemaFilePath := input.RelativeFile(rawSchema) | ||
g.logger.Debug("script file", zap.String("path", schemaFilePath)) | ||
|
||
schemaFile, err := fileutil.Read(schemaFilePath) | ||
if err == nil { | ||
g.logger.Debug("script file contents", zap.String("contents", string(schemaFile.Contents()))) | ||
test.Spec.Trigger.Graphql.SetSchema(string(schemaFile.Contents())) | ||
} | ||
} | ||
|
||
return test, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package trigger_preprocessor | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/kubeshop/tracetest/agent/workers/trigger" | ||
"github.com/kubeshop/tracetest/cli/cmdutil" | ||
"github.com/kubeshop/tracetest/cli/openapi" | ||
"github.com/kubeshop/tracetest/cli/pkg/fileutil" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type grpc struct { | ||
logger *zap.Logger | ||
} | ||
|
||
func GRPC(logger *zap.Logger) grpc { | ||
return grpc{logger: cmdutil.GetLogger()} | ||
} | ||
|
||
func (g grpc) Type() trigger.TriggerType { | ||
return trigger.TriggerTypeGRPC | ||
} | ||
|
||
func (g grpc) Preprocess(input fileutil.File, test openapi.TestResource) (openapi.TestResource, error) { | ||
// protobuf file can be defined separately in a file like: protobufFile: ./file.proto | ||
definedPBFile := test.Spec.Trigger.Grpc.GetProtobufFile() | ||
if !isValidFilePath(definedPBFile, input.AbsDir()) { | ||
g.logger.Debug("protobuf file is not a file path", zap.String("protobufFile", definedPBFile)) | ||
return test, nil | ||
} | ||
|
||
pbFilePath := input.RelativeFile(definedPBFile) | ||
g.logger.Debug("protobuf file", zap.String("path", pbFilePath)) | ||
|
||
pbFile, err := fileutil.Read(pbFilePath) | ||
if err != nil { | ||
return test, fmt.Errorf(`cannot read protobuf file: %w`, err) | ||
} | ||
g.logger.Debug("protobuf file contents", zap.String("contents", string(pbFile.Contents()))) | ||
|
||
test.Spec.Trigger.Grpc.SetProtobufFile(string(pbFile.Contents())) | ||
|
||
return test, nil | ||
} | ||
|
||
func isValidFilePath(filePath, testFile string) bool { | ||
if fileutil.LooksLikeRelativeFilePath(filePath) { | ||
// if looks like a relative file path, test if it exists | ||
return fileutil.IsFilePathToRelativeDir(filePath, testFile) | ||
} | ||
|
||
// it could be an absolute file path, test it | ||
return fileutil.IsFilePath(filePath) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package trigger_preprocessor | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/kubeshop/tracetest/agent/workers/trigger" | ||
"github.com/kubeshop/tracetest/cli/cmdutil" | ||
"github.com/kubeshop/tracetest/cli/openapi" | ||
"github.com/kubeshop/tracetest/cli/pkg/fileutil" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type http struct { | ||
logger *zap.Logger | ||
} | ||
|
||
func HTTP(logger *zap.Logger) http { | ||
return http{logger: cmdutil.GetLogger()} | ||
} | ||
|
||
func (g http) Type() trigger.TriggerType { | ||
return trigger.TriggerTypeHTTP | ||
} | ||
|
||
func (g http) Preprocess(input fileutil.File, test openapi.TestResource) (openapi.TestResource, error) { | ||
// body can be defined separately in a file like: body: ./body.json | ||
definedBodyFile := test.Spec.Trigger.HttpRequest.GetBody() | ||
if !isValidFilePath(definedBodyFile, input.AbsDir()) { | ||
g.logger.Debug("body file is not a file path", zap.String("protobufFile", definedBodyFile)) | ||
return test, nil | ||
} | ||
|
||
bodyFilePath := input.RelativeFile(definedBodyFile) | ||
g.logger.Debug("http body file", zap.String("path", bodyFilePath)) | ||
|
||
bodyFile, err := fileutil.Read(definedBodyFile) | ||
if err != nil { | ||
return test, fmt.Errorf(`cannot read protobuf file: %w`, err) | ||
} | ||
g.logger.Debug("http body file contents", zap.String("contents", string(bodyFile.Contents()))) | ||
|
||
test.Spec.Trigger.HttpRequest.SetBody(string(bodyFile.Contents())) | ||
|
||
return test, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package trigger_preprocessor | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/kubeshop/tracetest/agent/workers/trigger" | ||
"github.com/kubeshop/tracetest/cli/cmdutil" | ||
"github.com/kubeshop/tracetest/cli/openapi" | ||
"github.com/kubeshop/tracetest/cli/pkg/fileutil" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type playwrightengine struct { | ||
logger *zap.Logger | ||
} | ||
|
||
func PLAYWRIGHTENGINE(logger *zap.Logger) playwrightengine { | ||
return playwrightengine{logger: cmdutil.GetLogger()} | ||
} | ||
|
||
func (g playwrightengine) Type() trigger.TriggerType { | ||
return trigger.TriggerTypePlaywrightEngine | ||
} | ||
|
||
func (g playwrightengine) Preprocess(input fileutil.File, test openapi.TestResource) (openapi.TestResource, error) { | ||
// script can be defined separately in a file like: script: ./script.js | ||
definedScriptFile := test.Spec.Trigger.PlaywrightEngine.GetScript() | ||
if !isValidFilePath(definedScriptFile, input.AbsDir()) { | ||
g.logger.Debug("script file is not a file path", zap.String("protobufFile", definedScriptFile)) | ||
return test, nil | ||
} | ||
|
||
scriptFilePath := input.RelativeFile(definedScriptFile) | ||
g.logger.Debug("script file", zap.String("path", scriptFilePath)) | ||
|
||
scriptFile, err := fileutil.Read(scriptFilePath) | ||
if err != nil { | ||
return test, fmt.Errorf(`cannot read script file: %w`, err) | ||
} | ||
g.logger.Debug("script file contents", zap.String("contents", string(scriptFile.Contents()))) | ||
|
||
test.Spec.Trigger.PlaywrightEngine.SetScript(string(scriptFile.Contents())) | ||
|
||
return test, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package trigger_preprocessor | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/kubeshop/tracetest/agent/workers/trigger" | ||
"github.com/kubeshop/tracetest/cli/openapi" | ||
"github.com/kubeshop/tracetest/cli/pkg/fileutil" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type TriggerPreprocessor interface { | ||
Preprocess(input fileutil.File, test openapi.TestResource) (openapi.TestResource, error) | ||
Type() trigger.TriggerType | ||
} | ||
|
||
type Registry struct { | ||
processors map[trigger.TriggerType]TriggerPreprocessor | ||
} | ||
|
||
func NewRegistry(logger *zap.Logger) Registry { | ||
return Registry{ | ||
processors: map[trigger.TriggerType]TriggerPreprocessor{}, | ||
} | ||
} | ||
|
||
func (r Registry) Register(processor TriggerPreprocessor) Registry { | ||
r.processors[processor.Type()] = processor | ||
return r | ||
} | ||
|
||
var ErrNotFound = fmt.Errorf("preprocessor not found") | ||
|
||
func (r Registry) Preprocess(input fileutil.File, test openapi.TestResource) (openapi.TestResource, error) { | ||
triggerType := test.Spec.Trigger.GetType() | ||
|
||
processor, ok := r.processors[trigger.TriggerType(triggerType)] | ||
if ok { | ||
return processor.Preprocess(input, test) | ||
} | ||
|
||
return test, nil | ||
} |