Skip to content

Commit

Permalink
feat: partial backport for (elastic#1628)
Browse files Browse the repository at this point in the history
* chore: define variable for BeatsLocalPath

* fix: update path initialisation in unit tests

* chore: pass inline env vars to commands

Co-authored-by: Victor Martinez <[email protected]>

Co-authored-by: Victor Martinez <[email protected]>
  • Loading branch information
mdelapenya and v1v committed Feb 28, 2022
1 parent 0037c9a commit 24e88e4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 46 deletions.
4 changes: 1 addition & 3 deletions e2e/_suites/fleet/stand-alone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions e2e/_suites/kubernetes-autodiscover/autodiscover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions internal/deploy/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 13 additions & 6 deletions internal/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down
64 changes: 32 additions & 32 deletions internal/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 24e88e4

Please sign in to comment.