Skip to content

WIP: filebeat: auto-build test binary in Go integration tests via TestMain#49583

Draft
AndersonQ wants to merge 5 commits intoelastic:mainfrom
AndersonQ:filebeat-integ-test-build-binary
Draft

WIP: filebeat: auto-build test binary in Go integration tests via TestMain#49583
AndersonQ wants to merge 5 commits intoelastic:mainfrom
AndersonQ:filebeat-integ-test-build-binary

Conversation

@AndersonQ
Copy link
Member

Proposed commit message

filebeat: auto-build test binary in Go integration tests via TestMain

Move the filebeat.test binary build step from the mage targets into TestMain so that `go test -tags integration` works without requiring a prior `mage buildSystemTestBinary`. This removes a manual prerequisite that tripped up developers running integration tests locally.

  - Add BuildSystemTestBinary helper to libbeat/tests/integration/framework.go
  - Add TestMain in filebeat and x-pack/filebeat integration test packages
  - Remove BuildSystemTestBinary from mage GoIntegTest/GoFIPSOnlyIntegTest
  - Python integration tests are unchanged (they still use devtools directly)

Assisted by Claude

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works. Where relevant, I have used the stresstest.sh script to run them under stress conditions and race detector to verify their stability.
  • I have added an entry in ./changelog/fragments using the changelog tool.

Disruptive User Impact

Author's Checklist

  • [ ]

How to test this PR locally

Related issues

Use cases

Screenshots

Logs

@AndersonQ AndersonQ self-assigned this Mar 20, 2026
@AndersonQ AndersonQ added Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team backport-active-all Automated backport with mergify to all the active branches skip-changelog labels Mar 20, 2026
@botelastic botelastic bot added needs_team Indicates that the issue/PR needs a Team:* label and removed needs_team Indicates that the issue/PR needs a Team:* label labels Mar 20, 2026
@github-actions
Copy link
Contributor

🤖 GitHub comments

Just comment with:

  • run docs-build : Re-trigger the docs validation. (use unformatted text in the comment!)

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to make Filebeat Go integration tests runnable via go test -tags integration without requiring a prior mage buildSystemTestBinary, by moving the filebeat.test build step into TestMain and removing the explicit mage dependency.

Changes:

  • Added a BuildSystemTestBinary(binPath, packagePath) helper to the Go integration test framework.
  • Added TestMain in filebeat/tests/integration and x-pack/filebeat/tests/integration to auto-build filebeat.test.
  • Removed system test binary build steps from Filebeat mage Go integration test targets.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
libbeat/tests/integration/framework.go Adds a helper to build the system test binary via go test -c.
filebeat/tests/integration/integration_test.go Adds TestMain to build filebeat.test before running tests in this package.
x-pack/filebeat/tests/integration/integration_test.go Adds TestMain to build the x-pack filebeat.test before running tests in this package.
filebeat/magefile.go Removes BuildSystemTestBinary wrapper and drops binary build dependency from Go integ mage targets.
x-pack/filebeat/magefile.go Drops binary build step from Go integ mage targets.
Comments suppressed due to low confidence (1)

x-pack/filebeat/magefile.go:172

  • GoIntegTest/GoFIPSOnlyIntegTest no longer build the system test binary before running Go integration tests. Because go test runs packages in parallel and multiple x-pack/filebeat integration packages require ../../filebeat.test to exist, this will likely fail unless binary creation is coordinated elsewhere (e.g. a shared EnsureSystemTestBinary with locking). Consider restoring the build dependency here or ensuring the new TestMain-based build is reliable across all packages.
// GoIntegTest starts the docker containers and executes the Go integration tests.
func GoIntegTest(ctx context.Context) error {
	args := devtools.DefaultGoTestIntegrationFromHostArgs(ctx)
	// ES_USER must be admin in order for the Go Integration tests to function because they require
	// indices:data/read/search
	args.Env["ES_USER"] = args.Env["ES_SUPERUSER_USER"]
	args.Env["ES_PASS"] = args.Env["ES_SUPERUSER_PASS"]
	return devtools.GoIntegTestFromHost(ctx, args)
}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +31 to +45
func TestMain(m *testing.M) {
binPath, err := filepath.Abs("../../filebeat.test")
if err != nil {
fmt.Fprintf(os.Stderr, "failed to resolve binary path: %s\n", err)
os.Exit(1)
}
packagePath, err := filepath.Abs("../../")
if err != nil {
fmt.Fprintf(os.Stderr, "failed to resolve package path: %s\n", err)
os.Exit(1)
}
if err := integration.BuildSystemTestBinary(binPath, packagePath); err != nil {
fmt.Fprintf(os.Stderr, "failed to build filebeat test binary: %s\n", err)
os.Exit(1)
}
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

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

Building ../../filebeat.test in TestMain for this single package doesn’t guarantee the binary exists before other integration-tagged packages run. go test -tags integration ./... executes packages in parallel, and several other packages (e.g. filebeat/input/filestream, filebeat/tests/integration/* is not the only user) call integration.NewBeat with "../../filebeat.test" and currently fail fast if it’s missing. Consider moving the auto-build into a shared helper (e.g. integration.NewBeat or a dedicated EnsureSystemTestBinary with cross-process locking) so every package that needs the binary can reliably trigger/await the build, or keep a pre-step in the mage GoIntegTest targets.

Copilot uses AI. Check for mistakes.
Comment on lines +19 to +32
binPath, err := filepath.Abs("../../filebeat.test")
if err != nil {
fmt.Fprintf(os.Stderr, "failed to resolve binary path: %s\n", err)
os.Exit(1)
}
packagePath, err := filepath.Abs("../../")
if err != nil {
fmt.Fprintf(os.Stderr, "failed to resolve package path: %s\n", err)
os.Exit(1)
}
if err := integration.BuildSystemTestBinary(binPath, packagePath); err != nil {
fmt.Fprintf(os.Stderr, "failed to build filebeat test binary: %s\n", err)
os.Exit(1)
}
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

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

Same concurrency issue as in OSS filebeat: building ../../filebeat.test only from this package’s TestMain is racy under go test -tags integration ./... because other integration packages (e.g. x-pack/filebeat/input/* integration tests) may start in parallel and require the binary to already exist. Prefer a shared EnsureSystemTestBinary that can be called by all packages (or from integration.NewBeat) and coordinates via a file lock / sync.Once.

Suggested change
binPath, err := filepath.Abs("../../filebeat.test")
if err != nil {
fmt.Fprintf(os.Stderr, "failed to resolve binary path: %s\n", err)
os.Exit(1)
}
packagePath, err := filepath.Abs("../../")
if err != nil {
fmt.Fprintf(os.Stderr, "failed to resolve package path: %s\n", err)
os.Exit(1)
}
if err := integration.BuildSystemTestBinary(binPath, packagePath); err != nil {
fmt.Fprintf(os.Stderr, "failed to build filebeat test binary: %s\n", err)
os.Exit(1)
}
if err := integration.EnsureSystemTestBinary(); err != nil {
fmt.Fprintf(os.Stderr, "failed to ensure filebeat test binary: %s\n", err)
os.Exit(1)
}

Copilot uses AI. Check for mistakes.
Comment on lines +1303 to +1305
args = append(args, packagePath)

cmd := exec.Command("go", args...)
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

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

When TEST_COVERAGE=true, -coverpkg ./... is evaluated relative to the current working directory. Since this helper can be invoked from nested test package directories, coverage instrumentation will likely target the wrong package set compared to the mage build (which runs from the beat root). Set cmd.Dir (e.g. to packagePath or the module root) and/or make -coverpkg refer to the intended module path so coverage results are consistent.

Suggested change
args = append(args, packagePath)
cmd := exec.Command("go", args...)
// Ensure that coverage patterns like ./... are evaluated relative to packagePath.
absPackagePath, err := filepath.Abs(packagePath)
if err != nil {
return err
}
// Run "go test" in the target package directory, addressing it as ".".
args = append(args, ".")
cmd := exec.Command("go", args...)
cmd.Dir = absPackagePath

Copilot uses AI. Check for mistakes.
Comment on lines 192 to 195
// GoIntegTest starts the docker containers and executes the Go integration tests.
func GoIntegTest(ctx context.Context) error {
mg.Deps(BuildSystemTestBinary)
return devtools.GoIntegTestFromHost(ctx, devtools.DefaultGoTestIntegrationFromHostArgs(ctx))
}
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

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

GoIntegTest no longer ensures the system test binary exists before running go test -tags integration ./.... Given packages run in parallel and many integration tests require ../../filebeat.test to exist (integration.NewBeat requires it), this can make mage GoIntegTest flaky/fail unless the binary is built elsewhere in a coordinated way. Either keep a pre-build dependency here or introduce a robust shared auto-build mechanism used by all packages before starting tests.

Copilot uses AI. Check for mistakes.
Move the filebeat.test binary build step from the mage targets into
TestMain so that `go test -tags integration` works without requiring
a prior `mage buildSystemTestBinary`. This removes a manual prerequisite
that tripped up developers running integration tests locally.

  - Add BuildSystemTestBinary helper to libbeat/tests/integration/framework.go
  - Add TestMain in filebeat and x-pack/filebeat integration test packages
  - Remove BuildSystemTestBinary from mage GoIntegTest/GoFIPSOnlyIntegTest
  - Python integration tests are unchanged (they still use devtools directly)

Assisted by Claude
@AndersonQ AndersonQ force-pushed the filebeat-integ-test-build-binary branch from 70a227f to 4a597bb Compare March 23, 2026 15:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-active-all Automated backport with mergify to all the active branches skip-changelog Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants