Skip to content

Commit

Permalink
feat: cli polling profile (#3794)
Browse files Browse the repository at this point in the history
* add polling profile to test definition

* apply polling profile from test

* rename variable

* doc: add a brief reference to the `pollingProfile` field in the test def
  • Loading branch information
mathnogueira authored Apr 25, 2024
1 parent ad47266 commit cc4a66a
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 22 deletions.
3 changes: 3 additions & 0 deletions api/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ components:
skipTraceCollection:
type: boolean
description: If true, the test will not collect a trace
pollingProfile:
type: string
description: ID of the polling profile to be used for this test
specs:
type: array
items:
Expand Down
40 changes: 24 additions & 16 deletions cli/cmd/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,29 @@ var (
variableSetPreprocessor = preprocessor.VariableSet(cliLogger)
variableSetClient = GetVariableSetClient(httpClient, variableSetPreprocessor)

testPreprocessor = preprocessor.Test(cliLogger)
testClient = resourcemanager.NewClient(
pollingProfileClient = resourcemanager.NewClient(
httpClient, cliLogger,
"pollingprofile", "pollingprofiles",
resourcemanager.WithTableConfig(resourcemanager.TableConfig{
Cells: []resourcemanager.TableCellConfig{
{Header: "ID", Path: "spec.id"},
{Header: "NAME", Path: "spec.name"},
{Header: "STRATEGY", Path: "spec.strategy"},
},
}),
resourcemanager.WithResourceType("PollingProfile"),
)

testPreprocessor = preprocessor.Test(cliLogger, func(ctx context.Context, input fileutil.File) (fileutil.File, error) {
updated, err := pollingProfileClient.Apply(ctx, input, resourcemanager.Formats.Get(resourcemanager.FormatYAML))
if err != nil {
return input, fmt.Errorf("cannot apply polling profile: %w", err)
}

return fileutil.New(input.AbsPath(), []byte(updated)), nil
})

testClient = resourcemanager.NewClient(
httpClient, cliLogger,
"test", "tests",
resourcemanager.WithTableConfig(resourcemanager.TableConfig{
Expand Down Expand Up @@ -164,20 +185,7 @@ var (
}),
),
).
Register(
resourcemanager.NewClient(
httpClient, cliLogger,
"pollingprofile", "pollingprofiles",
resourcemanager.WithTableConfig(resourcemanager.TableConfig{
Cells: []resourcemanager.TableCellConfig{
{Header: "ID", Path: "spec.id"},
{Header: "NAME", Path: "spec.name"},
{Header: "STRATEGY", Path: "spec.strategy"},
},
}),
resourcemanager.WithResourceType("PollingProfile"),
),
).
Register(pollingProfileClient).
Register(
resourcemanager.NewClient(
httpClient, cliLogger,
Expand Down
37 changes: 37 additions & 0 deletions cli/openapi/model_test_.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 46 additions & 3 deletions cli/preprocessor/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import (
)

type test struct {
logger *zap.Logger
logger *zap.Logger
applyPollingProfileFunc applyResourceFunc
}

func Test(logger *zap.Logger) test {
func Test(logger *zap.Logger, applyPollingProfileFunc applyResourceFunc) test {
return test{
logger: cmdutil.GetLogger(),
logger: cmdutil.GetLogger(),
applyPollingProfileFunc: applyPollingProfileFunc,
}
}

Expand All @@ -29,6 +31,12 @@ func (t test) Preprocess(ctx context.Context, input fileutil.File) (fileutil.Fil
return input, fmt.Errorf("could not unmarshal test yaml: %w", err)
}

test, err = t.mapPollingProfiles(ctx, input, test)
if err != nil {
t.logger.Error("error mapping polling profiles from test", zap.String("content", string(input.Contents())), zap.Error(err))
return input, fmt.Errorf("could not map polling profiles referenced in test yaml: %w", err)
}

test, err = t.consolidateGRPCFile(input, test)
if err != nil {
return input, fmt.Errorf("could not consolidate grpc file: %w", err)
Expand All @@ -42,6 +50,41 @@ func (t test) Preprocess(ctx context.Context, input fileutil.File) (fileutil.Fil
return fileutil.New(input.AbsPath(), marshalled), nil
}

func (t test) mapPollingProfiles(ctx context.Context, input fileutil.File, test openapi.TestResource) (openapi.TestResource, error) {
if test.Spec.PollingProfile == nil {
return test, nil
}

pollingProfilePath := test.Spec.PollingProfile
if !fileutil.LooksLikeFilePath(*pollingProfilePath) {
t.logger.Debug("does not look like a file path",
zap.String("path", *pollingProfilePath),
)

return test, nil
}

f, err := fileutil.Read(input.RelativeFile(*pollingProfilePath))
if err != nil {
return openapi.TestResource{}, fmt.Errorf("cannot read polling profile file: %w", err)
}

pollingProfileFile, err := t.applyPollingProfileFunc(ctx, f)
if err != nil {
return openapi.TestResource{}, fmt.Errorf("cannot apply polling profile '%s': %w", *pollingProfilePath, err)
}

var pollingProfile openapi.PollingProfile
err = yaml.Unmarshal(pollingProfileFile.Contents(), &pollingProfile)
if err != nil {
return openapi.TestResource{}, fmt.Errorf("cannot unmarshal updated pollingProfile '%s': %w", *pollingProfilePath, err)
}

test.Spec.PollingProfile = &pollingProfile.Spec.Id

return test, nil
}

func (t test) consolidateGRPCFile(input fileutil.File, test openapi.TestResource) (openapi.TestResource, error) {
if test.Spec.Trigger.GetType() != "grpc" {
t.logger.Debug("test does not use grpc", zap.String("triggerType", test.Spec.Trigger.GetType()))
Expand Down
6 changes: 3 additions & 3 deletions cli/preprocessor/testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
"go.uber.org/zap"
)

type applyTestFunc func(context.Context, fileutil.File) (fileutil.File, error)
type applyResourceFunc func(context.Context, fileutil.File) (fileutil.File, error)

type testSuite struct {
logger *zap.Logger
applyTestFn applyTestFunc
applyTestFn applyResourceFunc
}

func TestSuite(logger *zap.Logger, applyTestFn applyTestFunc) testSuite {
func TestSuite(logger *zap.Logger, applyTestFn applyResourceFunc) testSuite {
return testSuite{
logger: cmdutil.GetLogger(),
applyTestFn: applyTestFn,
Expand Down
17 changes: 17 additions & 0 deletions docs/docs/cli/creating-tests.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,20 @@ Available functions:
:::tip
[Continue reading about Test Outputs, here.](/cli/creating-test-outputs)
:::

### Defining a polling profile

Some tests have different requirements than others, it means that some will take longer to run than other tests, so it's crucial to be able to
define the trace polling strategy at the test level:\

```yaml
type: Test
spec:
name: my test
pollingProfile: ./my-polling-profile.yaml
trigger: http
httpRequest:
...
```

This will make all runs from this test to use this specific test profile, and in case no value is defined, the test will use your default polling profile.
3 changes: 3 additions & 0 deletions server/openapi/model_test_.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type Test struct {
// If true, the test will not collect a trace
SkipTraceCollection bool `json:"skipTraceCollection,omitempty"`

// ID of the polling profile to be used for this test
PollingProfile string `json:"pollingProfile,omitempty"`

// specification of assertions that are going to be made
Specs []TestSpec `json:"specs,omitempty"`

Expand Down

0 comments on commit cc4a66a

Please sign in to comment.