-
Notifications
You must be signed in to change notification settings - Fork 67
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
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) | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this preprocessing is moved to |
||
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) | ||
|
@@ -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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,8 +16,8 @@ import ( | |
) | ||
|
||
var ( | ||
cliLogger = &zap.Logger{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
cliConfig config.Config | ||
cliLogger *zap.Logger | ||
versionText string | ||
isVersionMatch bool | ||
) | ||
|
@@ -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) { | ||
|
There was a problem hiding this comment.
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