diff --git a/.buildkite/auditbeat/auditbeat-pipeline.yml b/.buildkite/auditbeat/auditbeat-pipeline.yml index 9849109e8e4..19ce9e996a5 100644 --- a/.buildkite/auditbeat/auditbeat-pipeline.yml +++ b/.buildkite/auditbeat/auditbeat-pipeline.yml @@ -26,6 +26,9 @@ env: RACE_DETECTOR: "true" TEST_COVERAGE: "true" + # Module Tests + BEAT_PATH: "auditbeat" + steps: - group: "Check/Update" key: "auditbeat-check-update" @@ -101,10 +104,6 @@ steps: env: STACK_ENVIRONMENT: "next-major" command: | - set -euo pipefail - source .buildkite/scripts/changesets.sh - defineModuleFromTheChangeSet auditbeat - echo "~~~ Running tests" cd auditbeat mage pythonIntegTest retry: diff --git a/.buildkite/filebeat/filebeat-pipeline.yml b/.buildkite/filebeat/filebeat-pipeline.yml index 1820f575aad..c2ab8e13d64 100644 --- a/.buildkite/filebeat/filebeat-pipeline.yml +++ b/.buildkite/filebeat/filebeat-pipeline.yml @@ -23,6 +23,9 @@ env: RACE_DETECTOR: "true" TEST_COVERAGE: "true" + # Module Tests + BEAT_PATH: "filebeat" + steps: - group: "Check/Update" key: "filebeat-check-update" @@ -134,10 +137,6 @@ steps: TESTING_FILEBEAT_SKIP_DIFF: 1 PYTEST_ADDOPTS: "-k test_modules" command: | - set -euo pipefail - source .buildkite/scripts/changesets.sh - defineModuleFromTheChangeSet filebeat - echo "~~~ Running tests" cd filebeat mage pythonIntegTest retry: @@ -160,10 +159,6 @@ steps: STACK_ENVIRONMENT: "next-major" TESTING_FILEBEAT_SKIP_DIFF: 1 command: | - set -euo pipefail - source .buildkite/scripts/changesets.sh - defineModuleFromTheChangeSet filebeat - echo "~~~ Running tests" cd filebeat mage pythonIntegTest retry: diff --git a/.buildkite/heartbeat/heartbeat-pipeline.yml b/.buildkite/heartbeat/heartbeat-pipeline.yml index ea73af181e3..560d5240211 100644 --- a/.buildkite/heartbeat/heartbeat-pipeline.yml +++ b/.buildkite/heartbeat/heartbeat-pipeline.yml @@ -113,17 +113,9 @@ steps: - github_commit_status: context: "heartbeat: Win 2019 Unit Tests" - - label: ":ubuntu: Heartbeat: Go (Module) Integration Tests" + # Heartbeat has no modules yet + - label: ":ubuntu: Heartbeat: Go Integration Tests" command: | - set -euo pipefail - echo "~~~ Installing @elastic/synthetics" - npm install -g @elastic/synthetics - - # defines the MODULE env var based on what's changed in a PR - source .buildkite/scripts/changesets.sh - defineModuleFromTheChangeSet heartbeat - - echo "~~~ Running tests" cd heartbeat mage -v goIntegTest retry: @@ -140,14 +132,9 @@ steps: - github_commit_status: context: "heartbeat: Go Integration Tests" - - label: ":ubuntu: Heartbeat: Python (Module) Integration Tests" + # Heartbeat has no modules yet + - label: ":ubuntu: Heartbeat: Python Integration Tests" command: | - set -euo pipefail - # defines the MODULE env var based on what's changed in a PR - source .buildkite/scripts/changesets.sh - defineModuleFromTheChangeSet heartbeat - - echo "~~~ Running tests" cd heartbeat mage pythonIntegTest retry: diff --git a/.buildkite/libbeat/pipeline.libbeat.yml b/.buildkite/libbeat/pipeline.libbeat.yml index f7a86459a99..a5d0bfc4088 100644 --- a/.buildkite/libbeat/pipeline.libbeat.yml +++ b/.buildkite/libbeat/pipeline.libbeat.yml @@ -108,15 +108,11 @@ steps: - github_commit_status: context: "libbeat: Python Integration Tests" - - label: ":ubuntu: Libbeat: Module compat tests: next major" + - label: ":ubuntu: Libbeat: next major" # Run module integration tests under next major of Elastic stack. env: STACK_ENVIRONMENT: "next-major" command: | - set -euo pipefail - source .buildkite/scripts/changesets.sh - defineModuleFromTheChangeSet libbeat - echo "~~~ Running tests" cd libbeat mage pythonIntegTest retry: @@ -131,7 +127,7 @@ steps: - "libbeat/build/*.json" notify: - github_commit_status: - context: "libbeat: Module compat tests: next major" + context: "libbeat: next major" - label: ":ubuntu: Libbeat: Crosscompile" key: "mandatory-cross-compile" diff --git a/.buildkite/metricbeat/pipeline.yml b/.buildkite/metricbeat/pipeline.yml index 9be0612101a..71bd8d2080a 100644 --- a/.buildkite/metricbeat/pipeline.yml +++ b/.buildkite/metricbeat/pipeline.yml @@ -20,6 +20,9 @@ env: K8S_VERSION: "v1.21.1" ASDF_KIND_VERSION: "0.20.0" + # Module Tests + BEAT_PATH: "metricbeat" + # Other deps ASDF_MAGE_VERSION: 1.15.0 @@ -79,14 +82,10 @@ steps: - github_commit_status: context: "metricbeat: Ubuntu x86_64 Unit Tests" - - label: ":ubuntu: Metricbeat: Go Integration Tests" + - label: ":ubuntu: Metricbeat: Go Integration Tests (Module)" key: "mandatory-int-test" command: | set -euo pipefail - # defines the MODULE env var based on what's changed in a PR - source .buildkite/scripts/changesets.sh - defineModuleFromTheChangeSet metricbeat - echo "~~~ Running tests" export KUBECONFIG="$$PWD/kubecfg" cd metricbeat @@ -103,16 +102,12 @@ steps: - "metricbeat/build/*.json" notify: - github_commit_status: - context: "metricbeat: Go Integration Tests" + context: "metricbeat: Go Integration Tests (Module)" - - label: ":ubuntu: Metricbeat: Python Integration Tests" + - label: ":ubuntu: Metricbeat: Python Integration Tests (Module)" key: "mandatory-python-int-test" command: | set -euo pipefail - # defines the MODULE env var based on what's changed in a PR - source .buildkite/scripts/changesets.sh - defineModuleFromTheChangeSet metricbeat - echo "~~~ Running tests" export KUBECONFIG="$$PWD/kubecfg" cd metricbeat diff --git a/.buildkite/scripts/changesets.psm1 b/.buildkite/scripts/changesets.psm1 deleted file mode 100644 index 10e4d31a8b6..00000000000 --- a/.buildkite/scripts/changesets.psm1 +++ /dev/null @@ -1,64 +0,0 @@ -function ArePathsChanged($patterns) { - $changedlist = @() - foreach ($pattern in $patterns) { - $changedFiles = & git diff --name-only "HEAD@{1}" HEAD | Select-String -Pattern $pattern -SimpleMatch - if ($changedFiles) { - $changedlist += $changedFiles - } - } - if ($changedlist) { - Write-Host "--- Files changed: $changedlist" - return $true - } - else { - Write-Host "--- No files changed within specified changeset: $patterns" - return $false - } -} - -function AreChangedOnlyPaths($patterns) { - $changedFiles = & git diff --name-only "HEAD@{1}" HEAD - Write-Host "--- Git Diff result:" - Write-Host "$changedFiles" - - $matchedFiles = @() - foreach ($pattern in $patterns) { - $matched = $changedFiles | Select-String -Pattern $pattern -SimpleMatch - if ($matched) { - $matchedFiles += $matched - } - } - if (($matchedFiles.Count -eq $changedFiles.Count) -or ($changedFiles.Count -eq 0)) { - return $true - } - return $false -} - -# This function sets a `MODULE` env var, required by IT tests, containing a comma separated list of modules for a given beats project (specified via the first argument). -# The list is built depending on directories that have changed under `modules/` excluding anything else such as asciidoc and png files. -# `MODULE` will empty if no changes apply. -function DefineModuleFromTheChangeSet($projectPath) { - $projectPathTransformed = $projectPath -replace '/', '\\' - $projectPathExclusion = "((?!^$projectPathTransformed\\\/).)*\$" - $exclude = @("^($projectPathExclusion|((?!\\/module\\/).)*\$|.*\\.asciidoc|.*\\.png)") - - $changedModules = '' - - $moduleDirs = Get-ChildItem -Directory "$projectPath\module" - foreach($moduleDir in $moduleDirs) { - if((ArePathsChanged($moduleDir)) -and !(AreChangedOnlyPaths($exclude))) { - if(!$changedModules) { - $changedModules = $moduleDir.Name - } - else { - $changedModules += ',' + $moduleDir.Name - } - } - } - - if ($changedModules) { - $env:MODULE = $changedModules - Write-Output "~~~ Set env var MODULE to [$env:MODULE]" - Write-Output "~~~ Resuming commands" - } -} diff --git a/.buildkite/scripts/changesets.sh b/.buildkite/scripts/changesets.sh deleted file mode 100644 index 1ab80edc585..00000000000 --- a/.buildkite/scripts/changesets.sh +++ /dev/null @@ -1,66 +0,0 @@ -#!/usr/bin/env bash -# This script contains helper functions related to what should be run depending on Git changes - -set -euo pipefail - -OSS_MODULE_PATTERN="^[a-z0-9]+beat\\/module\\/([^\\/]+)\\/.*" -XPACK_MODULE_PATTERN="^x-pack\\/[a-z0-9]+beat\\/module\\/([^\\/]+)\\/.*" - -definePattern() { - pattern="${OSS_MODULE_PATTERN}" - - if [[ "$beatPath" == *"x-pack/"* ]]; then - pattern="${XPACK_MODULE_PATTERN}" - fi -} - -defineExclusions() { - exclude="^$beatPath\/module\/(.*(? 0 { + modules[matches[1]] = struct{}{} + } + } + } + + keys := make([]string, len(modules)) + i := 0 + for k := range modules { + keys[i] = k + i++ + } + + moduleVar := strings.Join(keys, ",") + + if moduleVar != "" { + err = os.Setenv("MODULE", moduleVar) + if err != nil { + return + } + + log.Printf("Detected changes in module(s): %s\n", moduleVar) + } else { + log.Printf("No changed modules found") + } +} + +func shouldIgnore(file string) bool { + ignoreList := []string{".asciidoc", ".png"} + for ext := range ignoreList { + if strings.HasSuffix(file, ignoreList[ext]) { + return true + } + } + return false +} + +func getDiff() []string { + commitRange := getCommitRange() + var output, err = sh.Output("git", "diff", "--name-only", commitRange) + if err != nil { + log.Fatal("git Diff failed: %w", err) + } + + printWhenVerbose("Git Diff result: %s\n", output) + + return strings.Split(output, "\n") +} + +func getFromCommit() string { + if baseBranch := os.Getenv("BUILDKITE_PULL_REQUEST_BASE_BRANCH"); baseBranch != "" { + printWhenVerbose("PR branch: %s\n", baseBranch) + + return getBranchName(baseBranch) + } + + if previousCommit := getPreviousCommit(); previousCommit != "" { + printWhenVerbose("Git from commit: %s\n", previousCommit) + + return previousCommit + } else { + commit, err := getBuildkiteCommit() + if err != nil { + log.Fatal(err) + } + printWhenVerbose("Git from commit: %s\n", commit) + + return commit + } +} + +func getPreviousCommit() string { + var output, _ = sh.Output("git", "rev-parse", "HEAD^") + printWhenVerbose("Git previous commit: %s\n", output) + + return strings.TrimSpace(output) +} + +func getCommitRange() string { + commit, err := getBuildkiteCommit() + if err != nil { + log.Fatal(err) + } + + return fmt.Sprintf("%s...%s", getFromCommit(), commit) +} + +func getBranchName(branch string) string { + return fmt.Sprintf("origin/%s", branch) +} + +func printWhenVerbose(template string, parameter string) { + if mg.Verbose() { + fmt.Printf(template, parameter) + } +} + +func getBuildkiteCommit() (string, error) { + commit := os.Getenv("BUILDKITE_COMMIT") + if commit == "" { + log.Fatal("BUILDKITE_COMMIT is not defined") + } + + return commit, nil +} diff --git a/filebeat/magefile.go b/filebeat/magefile.go index 16ae678e594..6130fafa91a 100644 --- a/filebeat/magefile.go +++ b/filebeat/magefile.go @@ -23,6 +23,7 @@ package main import ( "context" "fmt" + "os" "time" "github.com/magefile/mage/mg" @@ -203,6 +204,10 @@ func GoIntegTest(ctx context.Context) error { // PythonIntegTest executes the python system tests in the integration environment (Docker). func PythonIntegTest(ctx context.Context) error { + if os.Getenv("CI") == "true" && (os.Getenv("STACK_ENVIRONMENT") == "prev-minor" || os.Getenv("STACK_ENVIRONMENT") == "next-major") { + mg.Deps(devtools.DefineModules) + } + if !devtools.IsInIntegTestEnv() { mg.Deps(Fields, Dashboards) } diff --git a/metricbeat/magefile.go b/metricbeat/magefile.go index 86245ec67f2..0cd72c8a424 100644 --- a/metricbeat/magefile.go +++ b/metricbeat/magefile.go @@ -201,6 +201,10 @@ func IntegTest() { // Use TEST_TAGS=tag1,tag2 to add additional build tags. // Use MODULE=module to run only tests for `module`. func GoIntegTest(ctx context.Context) error { + if os.Getenv("CI") == "true" { + mg.Deps(devtools.DefineModules) + } + if !devtools.IsInIntegTestEnv() { mg.SerialDeps(Fields, Dashboards) } @@ -213,6 +217,10 @@ func GoIntegTest(ctx context.Context) error { // Use PYTEST_ADDOPTS="-k pattern" to only run tests matching the specified pattern. // Use any other PYTEST_* environment variable to influence the behavior of pytest. func PythonIntegTest(ctx context.Context) error { + if os.Getenv("CI") == "true" { + mg.Deps(devtools.DefineModules) + } + if !devtools.IsInIntegTestEnv() { mg.SerialDeps(Fields, Dashboards) } diff --git a/x-pack/auditbeat/magefile.go b/x-pack/auditbeat/magefile.go index c2700e99620..e6620ecc015 100644 --- a/x-pack/auditbeat/magefile.go +++ b/x-pack/auditbeat/magefile.go @@ -9,12 +9,15 @@ package main import ( "fmt" + "os" "time" "github.com/magefile/mage/mg" "github.com/magefile/mage/sh" "github.com/pkg/errors" + "github.com/elastic/beats/v7/dev-tools/mage/target/test" + auditbeat "github.com/elastic/beats/v7/auditbeat/scripts/mage" devtools "github.com/elastic/beats/v7/dev-tools/mage" @@ -24,8 +27,6 @@ import ( "github.com/elastic/beats/v7/dev-tools/mage/target/unittest" // mage:import _ "github.com/elastic/beats/v7/dev-tools/mage/target/integtest" - // mage:import - _ "github.com/elastic/beats/v7/dev-tools/mage/target/test" ) func init() { @@ -231,3 +232,12 @@ func installDependencies(arch string, pkgs ...string) error { }, pkgs...) return sh.Run("apt-get", args...) } + +// Test runs all available tests (unitTest + integTest) +func Test() { + if os.Getenv("CI") == "true" { + mg.Deps(devtools.DefineModules) + } + + test.Test() +} diff --git a/x-pack/filebeat/magefile.go b/x-pack/filebeat/magefile.go index bb0908af571..b95d69d5572 100644 --- a/x-pack/filebeat/magefile.go +++ b/x-pack/filebeat/magefile.go @@ -10,10 +10,13 @@ package main import ( "context" "fmt" + "os" "time" "github.com/magefile/mage/mg" + "github.com/elastic/beats/v7/dev-tools/mage/target/test" + devtools "github.com/elastic/beats/v7/dev-tools/mage" filebeat "github.com/elastic/beats/v7/filebeat/scripts/mage" @@ -25,8 +28,6 @@ import ( _ "github.com/elastic/beats/v7/dev-tools/mage/target/compose" // mage:import _ "github.com/elastic/beats/v7/dev-tools/mage/target/unittest" - // mage:import - "github.com/elastic/beats/v7/dev-tools/mage/target/test" ) func init() { @@ -172,6 +173,10 @@ func GoIntegTest(ctx context.Context) error { // PythonIntegTest executes the python system tests in the integration environment (Docker). func PythonIntegTest(ctx context.Context) error { + if os.Getenv("CI") == "true" && os.Getenv("STACK_ENVIRONMENT") == "prev-minor" { + mg.Deps(devtools.DefineModules) + } + if !devtools.IsInIntegTestEnv() { mg.Deps(Fields, Dashboards) } @@ -189,3 +194,12 @@ func PythonIntegTest(ctx context.Context) error { return devtools.PythonTest(args) }) } + +// Test runs all available tests (unitTest + integTest). +func Test() { + if os.Getenv("CI") == "true" { + mg.Deps(devtools.DefineModules) + } + + test.Test() +} diff --git a/x-pack/metricbeat/magefile.go b/x-pack/metricbeat/magefile.go index c74e5876ccb..6f2054cc99b 100644 --- a/x-pack/metricbeat/magefile.go +++ b/x-pack/metricbeat/magefile.go @@ -11,6 +11,7 @@ import ( "context" "fmt" "log" + "os" "runtime" "strings" "time" @@ -216,6 +217,10 @@ func IntegTest() { // Use TEST_TAGS=tag1,tag2 to add additional build tags. // Use MODULE=module to run only tests for `module`. func GoIntegTest(ctx context.Context) error { + if os.Getenv("CI") == "true" { + mg.Deps(devtools.DefineModules) + } + if !devtools.IsInIntegTestEnv() { mg.SerialDeps(Fields, Dashboards) } @@ -228,6 +233,10 @@ func GoIntegTest(ctx context.Context) error { // Use PYTEST_ADDOPTS="-k pattern" to only run tests matching the specified pattern. // Use any other PYTEST_* environment variable to influence the behavior of pytest. func PythonIntegTest(ctx context.Context) error { + if os.Getenv("CI") == "true" { + mg.Deps(devtools.DefineModules) + } + if !devtools.IsInIntegTestEnv() { mg.SerialDeps(Fields, Dashboards) }