Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions metricbeat/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package main
import (
"context"
"fmt"
"os"
"regexp"
"time"

Expand Down Expand Up @@ -93,6 +94,27 @@ func Update() error {
return sh.Run("make", "update")
}

// MockedTests runs the HTTP tests using the mocked data inside each {module}/{metricset}/testdata folder.
// Use MODULE={module_name} to run only mocked tests with a single module.
// Use GENERATE=true or GENERATE=1 to regenerate JSON files.
func MockedTests() error {
goTestDefaultArgs := mage.DefaultGoTestUnitArgs()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Once you mutate this it will no longer be the default args, so how about just calling it params.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point


goTestDefaultArgs.ExtraFlags = []string{"github.com/elastic/beats/metricbeat/mb/testing/data/."}

if module := os.Getenv("MODULE"); module != "" {
goTestDefaultArgs.ExtraFlags = append(goTestDefaultArgs.ExtraFlags, fmt.Sprintf("-module=%s", module))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For two strings I'd go with: "-module="+module

}

if generate := os.Getenv("GENERATE"); generate == "true" || generate == "1" {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For consistency, use the strconv.ParseBool to parse boolean arguments.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point!

goTestDefaultArgs.ExtraFlags = append(goTestDefaultArgs.ExtraFlags, "-generate")
}

goTestDefaultArgs.Packages = []string{}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No need to allocate an empty slice when nil will suffice.

Suggested change
goTestDefaultArgs.Packages = []string{}
goTestDefaultArgs.Packages = nil


return mage.GoTest(context.Background(), goTestDefaultArgs)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Define the function as func MockedTests(ctx context.Context) error. Then pass the context from Mage through here.

}

// Fields generates a fields.yml for the Beat.
func Fields() error {
return mage.GenerateFieldsYAML("module")
Expand Down