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

Conversation

schoren
Copy link
Collaborator

@schoren schoren commented Jul 6, 2023

The new cli resources client introduced a bug:
When a test or transaction is run with tracetest test run ... command, the files gets preprocessed: tests files embed protobuf files if needed, and transactions map test file references to test IDs.

The original behaviour was to preprocess files both on test run and apply transaction (apply test wasn't implemented beofre).

This PR adds e2e tests to cover this behavior and fixes it.

In order to implement this in a generic way, we introdiuce a preprocessor concept to the resource manager client.

Resources can define an optional applyPreProcessorFn function when registering. This func can alter the file that gets submitted to the server before submitting it.

@schoren schoren requested a review from danielbdias July 6, 2023 15:50
@schoren schoren changed the title fix(cli-e2e): fix overwritten file reference on test fixtures fix(cli): preprocess test and transaction files when running and applying Jul 6, 2023
@@ -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

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

@@ -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

httpClient = &resourcemanager.HTTPClient{}

testPreprocessor = preprocessor.Test(cliLogger)
testClient = resourcemanager.NewClient(
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 client is needed by the transaction preprocessor, so its here now to avoid a ciruclar initialization loop

@@ -15,8 +15,7 @@ type Client struct {
client *HTTPClient
resourceName string
resourceNamePlural string
deleteSuccessMsg string
tableConfig TableConfig
options options
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

3 options seemed enough to move them into its own struct, so it can grow in the future and still havig the client struct readable

@@ -56,39 +55,24 @@ func (c HTTPClient) do(req *http.Request) (*http.Response, error) {
return c.client.Do(req)
}

type options func(c *Client)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

moved to its own file

}

func (t test) Preprocess(ctx context.Context, input fileutil.File) (fileutil.File, error) {
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 is the same code moved from the test_run_action.go file, with a few variables renamed

}

func (t transaction) Preprocess(ctx context.Context, input fileutil.File) (fileutil.File, error) {
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 is the same code moved from the test_run_action.go file, with a few variables renamed

assert.Equal("application/json", listTest.Spec.Trigger.HTTPRequest.Headers[0].Value)
})

t.Run("EmbeddingProtobufFile", func(t *testing.T) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

New test case to ensure proper protobuf file contents embed happens

@schoren schoren merged commit b38c37b into main Jul 6, 2023
25 checks passed
@schoren schoren deleted the fix-cli-e2e-transactions branch July 6, 2023 18:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants