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

fix(cli): preprocess test and transaction files when running and applying #2879

Merged
merged 5 commits into from
Jul 6, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
131 changes: 0 additions & 131 deletions cli/actions/run_test_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,28 +245,8 @@ func (a runTestAction) applyTest(ctx context.Context, df defFile) (defFile, erro
return df, fmt.Errorf("cannot inject local env vars: %w", err)
}

var test openapi.TestResource
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this preprocessing is moved to preprocessor/test.go

err = yaml.Unmarshal(df.Contents(), &test)
if err != nil {
a.logger.Error("error parsing test", zap.String("content", string(df.Contents())), zap.Error(err))
return df, fmt.Errorf("could not unmarshal test yaml: %w", err)
}

test, err = a.consolidateGRPCFile(df, test)
if err != nil {
return df, fmt.Errorf("could not consolidate grpc file: %w", err)
}

marshalled, err := yaml.Marshal(test)
if err != nil {
return df, fmt.Errorf("could not marshal test yaml: %w", err)
}
df = defFile{fileutil.New(df.AbsPath(), marshalled)}

a.logger.Debug("applying test",
zap.String("absolutePath", df.AbsPath()),
zap.String("id", test.Spec.GetId()),
zap.String("marshalled", string(marshalled)),
)

updated, err := a.tests.Apply(ctx, df.File, a.yamlFormat)
Expand All @@ -276,74 +256,17 @@ func (a runTestAction) applyTest(ctx context.Context, df defFile) (defFile, erro

df = defFile{fileutil.New(df.AbsPath(), []byte(updated))}

err = yaml.Unmarshal(df.Contents(), &test)
if err != nil {
a.logger.Error("error parsing updated test", zap.String("content", string(df.Contents())), zap.Error(err))
return df, fmt.Errorf("could not unmarshal test yaml: %w", err)
}

a.logger.Debug("test applied",
zap.String("absolutePath", df.AbsPath()),
zap.String("id", test.Spec.GetId()),
)

return df, nil
}

func (a runTestAction) consolidateGRPCFile(df defFile, test openapi.TestResource) (openapi.TestResource, error) {
if test.Spec.Trigger.GetType() != "grpc" {
a.logger.Debug("test does not use grpc", zap.String("triggerType", test.Spec.Trigger.GetType()))
return test, nil
}

definedPBFile := test.Spec.Trigger.Grpc.GetProtobufFile()
if !fileutil.LooksLikeFilePath(definedPBFile) {
a.logger.Debug("protobuf file is not a file path", zap.String("protobufFile", definedPBFile))
return test, nil
}

pbFilePath := df.RelativeFile(definedPBFile)
a.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)
}
a.logger.Debug("protobuf file contents", zap.String("contents", string(pbFile.Contents())))

test.Spec.Trigger.Grpc.SetProtobufFile(string(pbFile.Contents()))

return test, nil
}

func (a runTestAction) applyTransaction(ctx context.Context, df defFile) (defFile, error) {
df, err := a.injectLocalEnvVars(ctx, df)
if err != nil {
return df, fmt.Errorf("cannot inject local env vars: %w", err)
}

var tran openapi.TransactionResource
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this preprocessing is moved to preprocessor/transaction.go

err = yaml.Unmarshal(df.Contents(), &tran)
if err != nil {
a.logger.Error("error parsing transaction", zap.String("content", string(df.Contents())), zap.Error(err))
return df, fmt.Errorf("could not unmarshal transaction yaml: %w", err)
}

tran, err = a.mapTransactionSteps(ctx, df, tran)
if err != nil {
return df, fmt.Errorf("could not map transaction steps: %w", err)
}

marshalled, err := yaml.Marshal(tran)
if err != nil {
return df, fmt.Errorf("could not marshal test yaml: %w", err)
}
df = defFile{fileutil.New(df.AbsPath(), marshalled)}

a.logger.Debug("applying transaction",
zap.String("absolutePath", df.AbsPath()),
zap.String("id", tran.Spec.GetId()),
zap.String("marshalled", string(marshalled)),
)

updated, err := a.transactions.Apply(ctx, df.File, a.yamlFormat)
Expand All @@ -352,63 +275,9 @@ func (a runTestAction) applyTransaction(ctx context.Context, df defFile) (defFil
}

df = defFile{fileutil.New(df.AbsPath(), []byte(updated))}

err = yaml.Unmarshal(df.Contents(), &tran)
if err != nil {
a.logger.Error("error parsing updated transaction", zap.String("content", updated), zap.Error(err))
return df, fmt.Errorf("could not unmarshal transaction yaml: %w", err)
}

a.logger.Debug("transaction applied",
zap.String("absolutePath", df.AbsPath()),
zap.String("updated id", tran.Spec.GetId()),
)

return df, nil
}

func (a runTestAction) mapTransactionSteps(ctx context.Context, df defFile, tran openapi.TransactionResource) (openapi.TransactionResource, error) {
for i, step := range tran.Spec.GetSteps() {
a.logger.Debug("mapping transaction step",
zap.Int("index", i),
zap.String("step", step),
)
if !fileutil.LooksLikeFilePath(step) {
a.logger.Debug("does not look like a file path",
zap.Int("index", i),
zap.String("step", step),
)
continue
}

f, err := fileutil.Read(df.RelativeFile(step))
if err != nil {
return openapi.TransactionResource{}, fmt.Errorf("cannot read test file: %w", err)
}

testDF, err := a.applyTest(ctx, defFile{f})
if err != nil {
return openapi.TransactionResource{}, fmt.Errorf("cannot apply test '%s': %w", step, err)
}

var test openapi.TestResource
err = yaml.Unmarshal(testDF.Contents(), &test)
if err != nil {
return openapi.TransactionResource{}, fmt.Errorf("cannot unmarshal updated test '%s': %w", step, err)
}

a.logger.Debug("mapped transaction step",
zap.Int("index", i),
zap.String("step", step),
zap.String("mapped step", test.Spec.GetId()),
)

tran.Spec.Steps[i] = test.Spec.GetId()
}

return tran, nil
}

func getTypeFromFile(df defFile) (string, error) {
var raw map[string]any
err := yaml.Unmarshal(df.Contents(), &raw)
Expand Down
5 changes: 2 additions & 3 deletions cli/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
)

var (
cliLogger = &zap.Logger{}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having a zeroed reference to a logger, in combination with replacing the pointer later in this file, allows to inject an unconfigured logger if needed on init, but have it configured by runtime.
Worst case scenario, clients get a zeroed, but valid, logger, instead of a nil pointer

cliConfig config.Config
cliLogger *zap.Logger
versionText string
isVersionMatch bool
)
Expand Down Expand Up @@ -140,8 +140,7 @@ func setupLogger(cmd *cobra.Command, args []string) {
zapcore.Lock(os.Stdout),
atom,
))

cliLogger = logger
*cliLogger = *logger
}

func teardownCommand(cmd *cobra.Command, args []string) {
Expand Down