From a9608488ccddde3316bad744294c864cb0127646 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Wed, 3 Feb 2021 17:00:36 +0100 Subject: [PATCH] feat: support using local Beats repository in the tests (#688) (#694) * feat: use local beats directory for elastic-agent installers * docs: document BEATS_LOCAL_PATH variable * chore: check that the local file exists * chore: add unit tests for the BEATS_LOCAL_PATH variable * chore: simplify logic after test coverage * chore: enrich test name * chore: rename test * chore: move default values to the end of the descriptions * docs: clarify local path for Beats --- e2e/README.md | 3 +- e2e/_suites/fleet/services.go | 21 +++++++ e2e/_suites/fleet/services_test.go | 62 +++++++++++++++++++ .../elastic-agent-8.0.0-SNAPSHOT-amd64.deb | 0 ...ic-agent-8.0.0-SNAPSHOT-linux-amd64.tar.gz | 0 .../elastic-agent-8.0.0-SNAPSHOT-x86_64.rpm | 0 6 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 e2e/_testresources/beats/x-pack/elastic-agent/build/distributions/elastic-agent-8.0.0-SNAPSHOT-amd64.deb create mode 100644 e2e/_testresources/beats/x-pack/elastic-agent/build/distributions/elastic-agent-8.0.0-SNAPSHOT-linux-amd64.tar.gz create mode 100644 e2e/_testresources/beats/x-pack/elastic-agent/build/distributions/elastic-agent-8.0.0-SNAPSHOT-x86_64.rpm diff --git a/e2e/README.md b/e2e/README.md index 3e5307f9e0..72108d04a8 100644 --- a/e2e/README.md +++ b/e2e/README.md @@ -135,7 +135,8 @@ We are going to enumerate the variables that will affect the product versions us The following environment variables affect how the tests are run in both the CI and a local machine. - `SKIP_SCENARIOS`: Set this environment variable to `false` if it's needed to include the scenarios annotated as `@skip` in the current test execution. Default value: `true`. -- `BEATS_USE_CI_SNAPSHOTS`: Set this environment variable to `true` if it's needed to use the binary snapshots produced by Beats CI instead of the official releases. The snapshots will be downloaded from a bucket in Google Cloud Storage. Default: `false`. This variable is used by the Beats repository, when testing the artifacts generated by the packaging job. +- `BEATS_LOCAL_PATH`: Set this environment variable to the base path to your local clone of Beats if it's needed to use the binary snapshots produced by your local build instead of the official releases. The snapshots will be fetched from the `${BEATS_LOCAL_PATH}/${THE_BEAT}/build/distributions` local directory. This variable is intended to be used by Beats developers, when testing locally the artifacts generated its own build. Default: empty. +- `BEATS_USE_CI_SNAPSHOTS`: Set this environment variable to `true` if it's needed to use the binary snapshots produced by Beats CI instead of the official releases. The snapshots will be downloaded from a bucket in Google Cloud Storage. This variable is used by the Beats repository, when testing the artifacts generated by the packaging job. Default: `false`. - `LOG_LEVEL`: Set this environment variable to `TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR` or `FATAL` to set the log level in the project. Default: `INFO`. - `DEVELOPER_MODE`: Set this environment variable to `true` to activate developer mode, which means not destroying the services provisioned by the test framework. Default: `false`. - `STACK_VERSION`. Set this environment variable to the proper version of the Elastic Stack (Elasticsearch and Kibana) to be used in the current execution. The default value depens on the branch you are targeting your work. diff --git a/e2e/_suites/fleet/services.go b/e2e/_suites/fleet/services.go index cb18ce1fbc..a4dd83afe8 100644 --- a/e2e/_suites/fleet/services.go +++ b/e2e/_suites/fleet/services.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "os" + "path" "strings" "time" @@ -161,11 +162,31 @@ func runElasticAgentCommand(profile string, image string, service string, proces // into the installer struct, to be used else where // If the environment variable ELASTIC_AGENT_DOWNLOAD_URL exists, then the artifact to be downloaded will // be defined by that value +// Else if the environment variable BEATS_LOCAL_PATH is set, then the artifact +// to be used will be defined by the local snapshot produced by the local build. // Else, if the environment variable BEATS_USE_CI_SNAPSHOTS is set, then the artifact // to be downloaded will be defined by the latest snapshot produced by the Beats CI. func downloadAgentBinary(artifact string, version string, OS string, arch string, extension string) (string, string, error) { fileName := fmt.Sprintf("%s-%s-%s.%s", artifact, version, arch, extension) + beatsLocalPath := shell.GetEnv("BEATS_LOCAL_PATH", "") + if beatsLocalPath != "" { + distributions := path.Join(beatsLocalPath, "x-pack", "elastic-agent", "build", "distributions") + log.Debugf("Using local snapshots for the Elastic Agent: %s", distributions) + + if extension == "tar.gz" { + fileName = fmt.Sprintf("%s-%s-%s-%s.%s", artifact, version, OS, arch, extension) + } + + fileNamePath := path.Join(distributions, fileName) + _, err := os.Stat(fileNamePath) + if err != nil || os.IsNotExist(err) { + return fileName, fileNamePath, err + } + + return fileName, fileNamePath, err + } + handleDownload := func(URL string, fileName string) (string, string, error) { if val, ok := binariesCache[URL]; ok { log.WithFields(log.Fields{ diff --git a/e2e/_suites/fleet/services_test.go b/e2e/_suites/fleet/services_test.go index 615e6f23c9..e48629157c 100644 --- a/e2e/_suites/fleet/services_test.go +++ b/e2e/_suites/fleet/services_test.go @@ -2,6 +2,7 @@ package main import ( "os" + "path" "testing" "github.com/stretchr/testify/assert" @@ -9,6 +10,67 @@ import ( var testVersion = agentVersionBase +func TestDownloadAgentBinary(t *testing.T) { + artifact := "elastic-agent" + beatsDir := path.Join("..", "..", "_testresources", "beats") + distributionsDir := path.Join(beatsDir, "x-pack", "elastic-agent", "build", "distributions") + OS := "linux" + version := "8.0.0-SNAPSHOT" + + t.Run("Fetching non-existent binary from local Beats dir throws an error", func(t *testing.T) { + defer os.Unsetenv("BEATS_LOCAL_PATH") + os.Setenv("BEATS_LOCAL_PATH", beatsDir) + + arch := "foo_arch" + extension := "foo_ext" + + _, _, err := downloadAgentBinary(artifact, version, OS, arch, extension) + assert.NotNil(t, err) + }) + + t.Run("Fetching RPM binary from local Beats dir", func(t *testing.T) { + defer os.Unsetenv("BEATS_LOCAL_PATH") + os.Setenv("BEATS_LOCAL_PATH", beatsDir) + + arch := "x86_64" + extension := "rpm" + expectedFileName := "elastic-agent-8.0.0-SNAPSHOT-x86_64.rpm" + + newFileName, downloadedFilePath, err := downloadAgentBinary(artifact, version, OS, arch, extension) + assert.Nil(t, err) + assert.Equal(t, newFileName, expectedFileName) + assert.Equal(t, downloadedFilePath, path.Join(distributionsDir, expectedFileName)) + }) + + t.Run("Fetching DEB binary from local Beats dir", func(t *testing.T) { + defer os.Unsetenv("BEATS_LOCAL_PATH") + os.Setenv("BEATS_LOCAL_PATH", beatsDir) + + arch := "amd64" + extension := "deb" + expectedFileName := "elastic-agent-8.0.0-SNAPSHOT-amd64.deb" + + newFileName, downloadedFilePath, err := downloadAgentBinary(artifact, version, OS, arch, extension) + assert.Nil(t, err) + assert.Equal(t, newFileName, expectedFileName) + assert.Equal(t, downloadedFilePath, path.Join(distributionsDir, expectedFileName)) + }) + + t.Run("Fetching TAR binary from local Beats dir", func(t *testing.T) { + defer os.Unsetenv("BEATS_LOCAL_PATH") + os.Setenv("BEATS_LOCAL_PATH", beatsDir) + + arch := "amd64" + extension := "tar.gz" + expectedFileName := "elastic-agent-8.0.0-SNAPSHOT-linux-amd64.tar.gz" + + newFileName, downloadedFilePath, err := downloadAgentBinary(artifact, version, OS, arch, extension) + assert.Nil(t, err) + assert.Equal(t, newFileName, expectedFileName) + assert.Equal(t, downloadedFilePath, path.Join(distributionsDir, expectedFileName)) + }) +} + func TestGetGCPBucketCoordinates_Commits(t *testing.T) { artifact := "elastic-agent" version := testVersion diff --git a/e2e/_testresources/beats/x-pack/elastic-agent/build/distributions/elastic-agent-8.0.0-SNAPSHOT-amd64.deb b/e2e/_testresources/beats/x-pack/elastic-agent/build/distributions/elastic-agent-8.0.0-SNAPSHOT-amd64.deb new file mode 100644 index 0000000000..e69de29bb2 diff --git a/e2e/_testresources/beats/x-pack/elastic-agent/build/distributions/elastic-agent-8.0.0-SNAPSHOT-linux-amd64.tar.gz b/e2e/_testresources/beats/x-pack/elastic-agent/build/distributions/elastic-agent-8.0.0-SNAPSHOT-linux-amd64.tar.gz new file mode 100644 index 0000000000..e69de29bb2 diff --git a/e2e/_testresources/beats/x-pack/elastic-agent/build/distributions/elastic-agent-8.0.0-SNAPSHOT-x86_64.rpm b/e2e/_testresources/beats/x-pack/elastic-agent/build/distributions/elastic-agent-8.0.0-SNAPSHOT-x86_64.rpm new file mode 100644 index 0000000000..e69de29bb2