diff --git a/e2e/_suites/fleet/stand-alone.go b/e2e/_suites/fleet/stand-alone.go index 9f37039449..03f9163987 100644 --- a/e2e/_suites/fleet/stand-alone.go +++ b/e2e/_suites/fleet/stand-alone.go @@ -15,7 +15,6 @@ import ( "github.com/elastic/e2e-testing/internal/deploy" "github.com/elastic/e2e-testing/internal/installer" "github.com/elastic/e2e-testing/internal/kibana" - "github.com/elastic/e2e-testing/internal/shell" "github.com/elastic/e2e-testing/internal/utils" "github.com/elastic/e2e-testing/internal/elasticsearch" @@ -92,9 +91,8 @@ func (fts *FleetTestSuite) startStandAloneAgent(image string, flavour string, en } useCISnapshots := elasticversion.GithubCommitSha1 != "" - beatsLocalPath := shell.GetEnv("BEATS_LOCAL_PATH", "") - if useCISnapshots || beatsLocalPath != "" { + if useCISnapshots || elasticversion.BeatsLocalPath != "" { // load the docker images that were already: // a. downloaded from the GCP bucket // b. fetched from the local beats binaries diff --git a/e2e/_suites/kubernetes-autodiscover/autodiscover_test.go b/e2e/_suites/kubernetes-autodiscover/autodiscover_test.go index b35cd02bbd..c4d7379ad1 100644 --- a/e2e/_suites/kubernetes-autodiscover/autodiscover_test.go +++ b/e2e/_suites/kubernetes-autodiscover/autodiscover_test.go @@ -32,7 +32,6 @@ import ( "github.com/elastic/e2e-testing/internal/common" "github.com/elastic/e2e-testing/internal/deploy" "github.com/elastic/e2e-testing/internal/kubernetes" - "github.com/elastic/e2e-testing/internal/shell" "github.com/elastic/e2e-testing/internal/utils" ) @@ -136,8 +135,7 @@ func (m *podsManager) configureDockerImage(podName string) error { beatVersion := elasticversion.GetSnapshotVersion(common.BeatVersion) + "-amd64" useCISnapshots := elasticversion.GithubCommitSha1 != "" - beatsLocalPath := shell.GetEnv("BEATS_LOCAL_PATH", "") - if useCISnapshots || beatsLocalPath != "" { + if useCISnapshots || elasticversion.BeatsLocalPath != "" { log.Debugf("Configuring Docker image for %s", podName) _, imagePath, err := elasticversion.FetchElasticArtifact(m.ctx, podName, common.BeatVersion, "linux", "amd64", "tar.gz", true, true) diff --git a/internal/deploy/docker.go b/internal/deploy/docker.go index fb137ec729..91e97cd37f 100644 --- a/internal/deploy/docker.go +++ b/internal/deploy/docker.go @@ -278,9 +278,8 @@ func (c *dockerDeploymentManifest) Stop(ctx context.Context, service ServiceRequ // the images produced by local Beats build, or not. // If an error occurred reading the environment, will return the passed namespace as fallback func GetDockerNamespaceEnvVar(fallback string) string { - beatsLocalPath := shell.GetEnv("BEATS_LOCAL_PATH", "") useCISnapshots := elasticversion.GithubCommitSha1 != "" - if useCISnapshots || beatsLocalPath != "" { + if useCISnapshots || elasticversion.BeatsLocalPath != "" { return "observability-ci" } return fallback diff --git a/internal/versions.go b/internal/versions.go index f7dad4b7af..5611dea0f8 100644 --- a/internal/versions.go +++ b/internal/versions.go @@ -24,6 +24,10 @@ import ( log "github.com/sirupsen/logrus" ) +// BeatsLocalPath is the path to a local copy of the Beats git repository +// It can be overriden by BEATS_LOCAL_PATH env var. Using the empty string as a default. +var BeatsLocalPath = "" + // to avoid downloading the same artifacts, we are adding this map to cache the URL of the downloaded binaries, using as key // the URL of the artifact. If another installer is trying to download the same URL, it will return the location of the // already downloaded artifact. @@ -39,6 +43,11 @@ var GithubCommitSha1 string func init() { GithubCommitSha1 = shell.GetEnv("GITHUB_CHECK_SHA1", "") + + BeatsLocalPath = shell.GetEnv("BEATS_LOCAL_PATH", BeatsLocalPath) + if BeatsLocalPath != "" { + log.Infof(`Beats local path will be used for artifacts. Please make sure all binaries are properly built in their "build/distributions" folder: %s`, BeatsLocalPath) + } } // elasticVersion represents a version @@ -321,8 +330,7 @@ func buildArtifactName(artifact string, artifactVersion string, OS string, arch } } - beatsLocalPath := shell.GetEnv("BEATS_LOCAL_PATH", "") - if beatsLocalPath != "" && isDocker { + if BeatsLocalPath != "" && isDocker { dockerString = ".docker" return fmt.Sprintf("%s-%s-%s-%s%s.%s", artifact, artifactVersion, OS, arch, dockerString, lowerCaseExtension) } @@ -345,16 +353,15 @@ func buildArtifactName(artifact string, artifactVersion string, OS string, arch // Else, if the environment variable GITHUB_CHECK_SHA1 is set, then the artifact // to be downloaded will be defined by the snapshot produced by the Beats CI for that commit. func fetchBeatsBinary(ctx context.Context, artifactName string, artifact string, version string, timeoutFactor int, xpack bool) (string, error) { - beatsLocalPath := shell.GetEnv("BEATS_LOCAL_PATH", "") - if beatsLocalPath != "" { + if BeatsLocalPath != "" { span, _ := apm.StartSpanOptions(ctx, "Fetching Beats binary", "beats.local.fetch-binary", apm.SpanOptions{ Parent: apm.SpanFromContext(ctx).TraceContext(), }) defer span.End() - distributions := path.Join(beatsLocalPath, artifact, "build", "distributions") + distributions := path.Join(BeatsLocalPath, artifact, "build", "distributions") if xpack { - distributions = path.Join(beatsLocalPath, "x-pack", artifact, "build", "distributions") + distributions = path.Join(BeatsLocalPath, "x-pack", artifact, "build", "distributions") } log.Debugf("Using local snapshots for the %s: %s", artifact, distributions) diff --git a/internal/versions_test.go b/internal/versions_test.go index 47300192b9..52146b0d85 100644 --- a/internal/versions_test.go +++ b/internal/versions_test.go @@ -199,8 +199,8 @@ func TestBuildArtifactName(t *testing.T) { }) t.Run("For Docker from local repository (amd64)", func(t *testing.T) { - defer os.Unsetenv("BEATS_LOCAL_PATH") - os.Setenv("BEATS_LOCAL_PATH", "/tmp") + defer func() { BeatsLocalPath = "" }() + BeatsLocalPath = "/tmp" artifact = "elastic-agent" arch := "amd64" @@ -214,8 +214,8 @@ func TestBuildArtifactName(t *testing.T) { assert.Equal(t, expectedFileName, artifactName) }) t.Run("For Docker from local repository (arm64)", func(t *testing.T) { - defer os.Unsetenv("BEATS_LOCAL_PATH") - os.Setenv("BEATS_LOCAL_PATH", "/tmp") + defer func() { BeatsLocalPath = "" }() + BeatsLocalPath = "/tmp" artifact = "elastic-agent" arch := "arm64" @@ -230,8 +230,8 @@ func TestBuildArtifactName(t *testing.T) { }) t.Run("For Docker UBI8 from local repository (amd64)", func(t *testing.T) { - defer os.Unsetenv("BEATS_LOCAL_PATH") - os.Setenv("BEATS_LOCAL_PATH", "/tmp") + defer func() { BeatsLocalPath = "" }() + BeatsLocalPath = "/tmp" artifact = "elastic-agent-ubi8" arch := "amd64" @@ -245,8 +245,8 @@ func TestBuildArtifactName(t *testing.T) { assert.Equal(t, expectedFileName, artifactName) }) t.Run("For Docker UBI8 from local repository (arm64)", func(t *testing.T) { - defer os.Unsetenv("BEATS_LOCAL_PATH") - os.Setenv("BEATS_LOCAL_PATH", "/tmp") + defer func() { BeatsLocalPath = "" }() + BeatsLocalPath = "/tmp" artifact = "elastic-agent-ubi8" arch := "arm64" @@ -382,16 +382,16 @@ func TestFetchBeatsBinaryFromLocalPath(t *testing.T) { ctx := context.Background() 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) + defer func() { BeatsLocalPath = "" }() + BeatsLocalPath = beatsDir _, err := fetchBeatsBinary(ctx, "foo_fileName", artifact, version, utils.TimeoutFactor, true) assert.NotNil(t, err) }) t.Run("Fetching RPM binary (amd64) from local Beats dir", func(t *testing.T) { - defer os.Unsetenv("BEATS_LOCAL_PATH") - os.Setenv("BEATS_LOCAL_PATH", beatsDir) + defer func() { BeatsLocalPath = "" }() + BeatsLocalPath = beatsDir artifactName := versionPrefix + "-x86_64.rpm" expectedFilePath := path.Join(distributionsDir, artifactName) @@ -401,8 +401,8 @@ func TestFetchBeatsBinaryFromLocalPath(t *testing.T) { assert.Equal(t, downloadedFilePath, expectedFilePath) }) t.Run("Fetching RPM binary (arm64) from local Beats dir", func(t *testing.T) { - defer os.Unsetenv("BEATS_LOCAL_PATH") - os.Setenv("BEATS_LOCAL_PATH", beatsDir) + defer func() { BeatsLocalPath = "" }() + BeatsLocalPath = beatsDir artifactName := versionPrefix + "-aarch64.rpm" expectedFilePath := path.Join(distributionsDir, artifactName) @@ -413,8 +413,8 @@ func TestFetchBeatsBinaryFromLocalPath(t *testing.T) { }) t.Run("Fetching DEB binary (amd64) from local Beats dir", func(t *testing.T) { - defer os.Unsetenv("BEATS_LOCAL_PATH") - os.Setenv("BEATS_LOCAL_PATH", beatsDir) + defer func() { BeatsLocalPath = "" }() + BeatsLocalPath = beatsDir artifactName := versionPrefix + "-amd64.deb" expectedFilePath := path.Join(distributionsDir, artifactName) @@ -424,8 +424,8 @@ func TestFetchBeatsBinaryFromLocalPath(t *testing.T) { assert.Equal(t, downloadedFilePath, expectedFilePath) }) t.Run("Fetching DEB binary (arm64) from local Beats dir", func(t *testing.T) { - defer os.Unsetenv("BEATS_LOCAL_PATH") - os.Setenv("BEATS_LOCAL_PATH", beatsDir) + defer func() { BeatsLocalPath = "" }() + BeatsLocalPath = beatsDir artifactName := versionPrefix + "-arm64.deb" expectedFilePath := path.Join(distributionsDir, artifactName) @@ -436,8 +436,8 @@ func TestFetchBeatsBinaryFromLocalPath(t *testing.T) { }) t.Run("Fetching TAR binary (amd64) from local Beats dir", func(t *testing.T) { - defer os.Unsetenv("BEATS_LOCAL_PATH") - os.Setenv("BEATS_LOCAL_PATH", beatsDir) + defer func() { BeatsLocalPath = "" }() + BeatsLocalPath = beatsDir artifactName := versionPrefix + "-linux-amd64.tar.gz" expectedFilePath := path.Join(distributionsDir, artifactName) @@ -447,8 +447,8 @@ func TestFetchBeatsBinaryFromLocalPath(t *testing.T) { assert.Equal(t, downloadedFilePath, expectedFilePath) }) t.Run("Fetching TAR binary (x86_64) from local Beats dir", func(t *testing.T) { - defer os.Unsetenv("BEATS_LOCAL_PATH") - os.Setenv("BEATS_LOCAL_PATH", beatsDir) + defer func() { BeatsLocalPath = "" }() + BeatsLocalPath = beatsDir artifactName := versionPrefix + "-linux-x86_64.tar.gz" expectedFilePath := path.Join(distributionsDir, artifactName) @@ -458,8 +458,8 @@ func TestFetchBeatsBinaryFromLocalPath(t *testing.T) { assert.Equal(t, downloadedFilePath, expectedFilePath) }) t.Run("Fetching TAR binary (arm64) from local Beats dir", func(t *testing.T) { - defer os.Unsetenv("BEATS_LOCAL_PATH") - os.Setenv("BEATS_LOCAL_PATH", beatsDir) + defer func() { BeatsLocalPath = "" }() + BeatsLocalPath = beatsDir artifactName := versionPrefix + "-linux-arm64.tar.gz" expectedFilePath := path.Join(distributionsDir, artifactName) @@ -470,8 +470,8 @@ func TestFetchBeatsBinaryFromLocalPath(t *testing.T) { }) t.Run("Fetching Docker binary (amd64) from local Beats dir", func(t *testing.T) { - defer os.Unsetenv("BEATS_LOCAL_PATH") - os.Setenv("BEATS_LOCAL_PATH", beatsDir) + defer func() { BeatsLocalPath = "" }() + BeatsLocalPath = beatsDir artifactName := versionPrefix + "-linux-amd64.docker.tar.gz" expectedFilePath := path.Join(distributionsDir, artifactName) @@ -481,8 +481,8 @@ func TestFetchBeatsBinaryFromLocalPath(t *testing.T) { assert.Equal(t, downloadedFilePath, expectedFilePath) }) t.Run("Fetching Docker binary (arm64) from local Beats dir", func(t *testing.T) { - defer os.Unsetenv("BEATS_LOCAL_PATH") - os.Setenv("BEATS_LOCAL_PATH", beatsDir) + defer func() { BeatsLocalPath = "" }() + BeatsLocalPath = beatsDir artifactName := versionPrefix + "-linux-arm64.docker.tar.gz" expectedFilePath := path.Join(distributionsDir, artifactName) @@ -493,8 +493,8 @@ func TestFetchBeatsBinaryFromLocalPath(t *testing.T) { }) t.Run("Fetching ubi8 Docker binary (amd64) from local Beats dir", func(t *testing.T) { - defer os.Unsetenv("BEATS_LOCAL_PATH") - os.Setenv("BEATS_LOCAL_PATH", beatsDir) + defer func() { BeatsLocalPath = "" }() + BeatsLocalPath = beatsDir artifactName := ubi8VersionPrefix + "-linux-amd64.docker.tar.gz" expectedFilePath := path.Join(distributionsDir, artifactName) @@ -504,8 +504,8 @@ func TestFetchBeatsBinaryFromLocalPath(t *testing.T) { assert.Equal(t, downloadedFilePath, expectedFilePath) }) t.Run("Fetching ubi8 Docker binary (arm64) from local Beats dir", func(t *testing.T) { - defer os.Unsetenv("BEATS_LOCAL_PATH") - os.Setenv("BEATS_LOCAL_PATH", beatsDir) + defer func() { BeatsLocalPath = "" }() + BeatsLocalPath = beatsDir artifactName := ubi8VersionPrefix + "-linux-arm64.docker.tar.gz" expectedFilePath := path.Join(distributionsDir, artifactName)