From dd1b2878744eed28c4711689cd62a87a9e96704a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Mon, 1 Feb 2021 18:02:02 +0100 Subject: [PATCH 1/9] feat: use local beats directory for elastic-agent installers --- e2e/_suites/fleet/services.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/e2e/_suites/fleet/services.go b/e2e/_suites/fleet/services.go index cb18ce1fbc..9bac5bb4fd 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,25 @@ 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) + } + + return fileName, path.Join(distributions, fileName), nil + } + handleDownload := func(URL string, fileName string) (string, string, error) { if val, ok := binariesCache[URL]; ok { log.WithFields(log.Fields{ From 465ac0d856fd6ab42fc4a78f60cbf0be8d5cc75f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Mon, 1 Feb 2021 18:05:17 +0100 Subject: [PATCH 2/9] docs: document BEATS_LOCAL_PATH variable --- e2e/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/e2e/README.md b/e2e/README.md index 3e5307f9e0..c678d8548f 100644 --- a/e2e/README.md +++ b/e2e/README.md @@ -135,6 +135,7 @@ 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_LOCAL_PATH`: Set this environment variable to the path to your local copy 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 `build/distributions` local directory. Default: empty. This variable is intented to be used by Beats developers, when testing locally the artifacts generated its own build. - `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. - `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`. From ea136403d380a7b6c6e4bf5eaa50a336bde078cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Mon, 1 Feb 2021 18:20:00 +0100 Subject: [PATCH 3/9] chore: check that the local file exists --- e2e/_suites/fleet/services.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/e2e/_suites/fleet/services.go b/e2e/_suites/fleet/services.go index 9bac5bb4fd..77570c5878 100644 --- a/e2e/_suites/fleet/services.go +++ b/e2e/_suites/fleet/services.go @@ -178,7 +178,16 @@ func downloadAgentBinary(artifact string, version string, OS string, arch string fileName = fmt.Sprintf("%s-%s-%s-%s.%s", artifact, version, OS, arch, extension) } - return fileName, path.Join(distributions, fileName), nil + fileNamePath := path.Join(distributions, fileName) + _, err := os.Stat(fileNamePath) + if err == nil { + return fileName, fileNamePath, nil + } + if os.IsNotExist(err) { + return fileName, fileNamePath, err + } + + return fileName, fileNamePath, err } handleDownload := func(URL string, fileName string) (string, string, error) { From 505ac24b13bf150b8a416b6349067774dc29a494 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 2 Feb 2021 09:58:08 +0100 Subject: [PATCH 4/9] chore: add unit tests for the BEATS_LOCAL_PATH variable --- 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 4 files changed, 62 insertions(+) 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/_suites/fleet/services_test.go b/e2e/_suites/fleet/services_test.go index 615e6f23c9..e4b6d477fd 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", 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 Docker image 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 From 8facf2ef0579cdaeb81f0a14c73a361bd8a52bb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 2 Feb 2021 09:59:13 +0100 Subject: [PATCH 5/9] chore: simplify logic after test coverage --- e2e/_suites/fleet/services.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/e2e/_suites/fleet/services.go b/e2e/_suites/fleet/services.go index 77570c5878..a4dd83afe8 100644 --- a/e2e/_suites/fleet/services.go +++ b/e2e/_suites/fleet/services.go @@ -180,10 +180,7 @@ func downloadAgentBinary(artifact string, version string, OS string, arch string fileNamePath := path.Join(distributions, fileName) _, err := os.Stat(fileNamePath) - if err == nil { - return fileName, fileNamePath, nil - } - if os.IsNotExist(err) { + if err != nil || os.IsNotExist(err) { return fileName, fileNamePath, err } From f45c453004ca21ebb5ac14990c7646452000b450 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 2 Feb 2021 09:59:50 +0100 Subject: [PATCH 6/9] chore: enrich test name --- e2e/_suites/fleet/services_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/_suites/fleet/services_test.go b/e2e/_suites/fleet/services_test.go index e4b6d477fd..b14d444e3d 100644 --- a/e2e/_suites/fleet/services_test.go +++ b/e2e/_suites/fleet/services_test.go @@ -17,7 +17,7 @@ func TestDownloadAgentBinary(t *testing.T) { OS := "linux" version := "8.0.0-SNAPSHOT" - t.Run("Fetching non-existent binary from local Beats dir", func(t *testing.T) { + 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) From 08e180d9fac6a9fdb5c546bf10711f550042ed72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 2 Feb 2021 10:00:40 +0100 Subject: [PATCH 7/9] chore: rename test --- e2e/_suites/fleet/services_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/_suites/fleet/services_test.go b/e2e/_suites/fleet/services_test.go index b14d444e3d..e48629157c 100644 --- a/e2e/_suites/fleet/services_test.go +++ b/e2e/_suites/fleet/services_test.go @@ -56,7 +56,7 @@ func TestDownloadAgentBinary(t *testing.T) { assert.Equal(t, downloadedFilePath, path.Join(distributionsDir, expectedFileName)) }) - t.Run("Fetching Docker image from local Beats dir", func(t *testing.T) { + 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) From 702af0047d9fc943f3b0ad343e9d5e80558f4188 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Tue, 2 Feb 2021 11:14:08 +0100 Subject: [PATCH 8/9] chore: move default values to the end of the descriptions --- e2e/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/e2e/README.md b/e2e/README.md index c678d8548f..2a58593a2a 100644 --- a/e2e/README.md +++ b/e2e/README.md @@ -135,8 +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_LOCAL_PATH`: Set this environment variable to the path to your local copy 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 `build/distributions` local directory. Default: empty. This variable is intented to be used by Beats developers, when testing locally the artifacts generated its own build. -- `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 path to your local copy 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 `build/distributions` local directory. This variable is intented 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. From 26521d0cebbcd278e5cd2953f9b9db5f0107d804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20de=20la=20Pe=C3=B1a?= Date: Wed, 3 Feb 2021 13:33:26 +0100 Subject: [PATCH 9/9] docs: clarify local path for Beats --- e2e/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/README.md b/e2e/README.md index 2a58593a2a..72108d04a8 100644 --- a/e2e/README.md +++ b/e2e/README.md @@ -135,7 +135,7 @@ 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_LOCAL_PATH`: Set this environment variable to the path to your local copy 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 `build/distributions` local directory. This variable is intented to be used by Beats developers, when testing locally the artifacts generated its own build. Default: empty. +- `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`.