diff --git a/cli/docker/docker.go b/cli/docker/docker.go index c9463323a8..b7e5a0d40b 100644 --- a/cli/docker/docker.go +++ b/cli/docker/docker.go @@ -6,7 +6,10 @@ package docker import ( "bytes" + "compress/gzip" "context" + "os" + "path/filepath" "strings" "github.com/docker/docker/api/types" @@ -172,6 +175,37 @@ func RemoveContainer(containerName string) error { return nil } +// LoadImage loads a TAR file in the local docker engine +func LoadImage(imagePath string) error { + fileNamePath, err := filepath.Abs(imagePath) + if err != nil { + return err + } + + _, err = os.Stat(fileNamePath) + if err != nil || os.IsNotExist(err) { + return err + } + + dockerClient := getDockerClient() + file, err := os.Open(imagePath) + + input, err := gzip.NewReader(file) + imageLoadResponse, err := dockerClient.ImageLoad(context.Background(), input, false) + if err != nil { + log.WithFields(log.Fields{ + "error": err, + "image": fileNamePath, + }).Error("Could not load the Docker image.") + return err + } + + log.WithFields(log.Fields{ + "response": imageLoadResponse, + }).Debug("Docker image loaded successfully") + return nil +} + // RemoveDevNetwork removes the developer network func RemoveDevNetwork() error { dockerClient := getDockerClient() diff --git a/e2e/_suites/fleet/fleet.go b/e2e/_suites/fleet/fleet.go index a92c8e2518..68e246989a 100644 --- a/e2e/_suites/fleet/fleet.go +++ b/e2e/_suites/fleet/fleet.go @@ -167,7 +167,7 @@ func (fts *FleetTestSuite) anStaleAgentIsDeployedToFleetWithInstaller(image, ver // prepare installer for stale version if fts.Version != agentVersionBackup { - i := GetElasticAgentInstaller(image, installerType, fts.Version, true) + i := GetElasticAgentInstaller(image, installerType, fts.Version) fts.Installers[fmt.Sprintf("%s-%s-%s", image, installerType, version)] = i } @@ -379,11 +379,10 @@ func (fts *FleetTestSuite) theAgentIsListedInFleetWithStatus(desiredStatus strin // the agent is not listed in Fleet if desiredStatus == "offline" || desiredStatus == "inactive" { log.WithFields(log.Fields{ - "isAgentInStatus": isAgentInStatus, - "elapsedTime": exp.GetElapsedTime(), - "hostname": fts.Hostname, - "retries": retryCount, - "status": desiredStatus, + "elapsedTime": exp.GetElapsedTime(), + "hostname": fts.Hostname, + "retries": retryCount, + "status": desiredStatus, }).Info("The Agent is not present in Fleet, as expected") return nil } else if desiredStatus == "online" { diff --git a/e2e/_suites/fleet/ingest-manager_test.go b/e2e/_suites/fleet/ingest-manager_test.go index fe61fa2341..89735cc08b 100644 --- a/e2e/_suites/fleet/ingest-manager_test.go +++ b/e2e/_suites/fleet/ingest-manager_test.go @@ -96,10 +96,12 @@ func setUpSuite() { imts = IngestManagerTestSuite{ Fleet: &FleetTestSuite{ Installers: map[string]ElasticAgentInstaller{ - "centos-systemd-" + agentVersion: GetElasticAgentInstaller("centos", "systemd", agentVersion, false), - "centos-tar-" + agentVersion: GetElasticAgentInstaller("centos", "tar", agentVersion, false), - "debian-systemd-" + agentVersion: GetElasticAgentInstaller("debian", "systemd", agentVersion, false), - "debian-tar-" + agentVersion: GetElasticAgentInstaller("debian", "tar", agentVersion, false), + "centos-systemd-" + agentVersion: GetElasticAgentInstaller("centos", "systemd", agentVersion), + "centos-tar-" + agentVersion: GetElasticAgentInstaller("centos", "tar", agentVersion), + "debian-systemd-" + agentVersion: GetElasticAgentInstaller("debian", "systemd", agentVersion), + "debian-tar-" + agentVersion: GetElasticAgentInstaller("debian", "tar", agentVersion), + "docker-default-" + agentVersion: GetElasticAgentInstaller("docker", "default", agentVersion), + "docker-ubi8-" + agentVersion: GetElasticAgentInstaller("docker", "ubi8", agentVersion), }, }, StandAlone: &StandAloneTestSuite{}, @@ -231,21 +233,6 @@ func (imts *IngestManagerTestSuite) processStateOnTheHost(process string, state return checkProcessStateOnTheHost(containerName, process, state) } -// checkElasticAgentVersion returns a fallback version (agentVersionBase) if the version set by the environment is empty -func checkElasticAgentVersion(version string) string { - environmentVersion := os.Getenv("ELASTIC_AGENT_VERSION") - - if environmentVersion == "" { - return agentVersionBase - } - - if strings.HasPrefix(strings.ToLower(environmentVersion), "pr-") { - return agentVersionBase - } - - return version -} - // name of the container for the service: // we are using the Docker client instead of docker-compose // because it does not support returning the output of a diff --git a/e2e/_suites/fleet/installers.go b/e2e/_suites/fleet/installers.go index 75589ea2e5..40e810b02c 100644 --- a/e2e/_suites/fleet/installers.go +++ b/e2e/_suites/fleet/installers.go @@ -1,8 +1,13 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + package main import ( "fmt" + "github.com/elastic/e2e-testing/cli/docker" log "github.com/sirupsen/logrus" ) @@ -98,6 +103,85 @@ func (i *DEBPackage) Uninstall() error { return nil } +// DockerPackage implements operations for a DEB installer +type DockerPackage struct { + BasePackage + installerPath string + ubi8 bool + // optional fields + arch string + artifact string + OS string + version string +} + +// NewDockerPackage creates an instance for the Docker installer +func NewDockerPackage(binaryName string, profile string, image string, service string, installerPath string, ubi8 bool) *DockerPackage { + return &DockerPackage{ + BasePackage: BasePackage{ + binaryName: binaryName, + image: image, + profile: profile, + service: service, + }, + installerPath: installerPath, + ubi8: ubi8, + } +} + +// Install installs a Docker package +func (i *DockerPackage) Install(containerName string, token string) error { + log.Trace("No install commands for Docker packages") + return nil +} + +// InstallCerts installs the certificates for a Docker package +func (i *DockerPackage) InstallCerts() error { + log.Trace("No install certs commands for Docker packages") + return nil +} + +// Preinstall executes operations before installing a Docker package +func (i *DockerPackage) Preinstall() error { + return docker.LoadImage(i.installerPath) +} + +// Postinstall executes operations after installing a Docker package +func (i *DockerPackage) Postinstall() error { + log.Trace("No postinstall commands for Docker packages") + return nil +} + +// Uninstall uninstalls a Docker package +func (i *DockerPackage) Uninstall() error { + log.Trace("No uninstall commands for Docker packages") + return nil +} + +// WithArch sets the architecture +func (i *DockerPackage) WithArch(arch string) *DockerPackage { + i.arch = arch + return i +} + +// WithArtifact sets the artifact +func (i *DockerPackage) WithArtifact(artifact string) *DockerPackage { + i.artifact = artifact + return i +} + +// WithOS sets the OS +func (i *DockerPackage) WithOS(OS string) *DockerPackage { + i.OS = OS + return i +} + +// WithVersion sets the version +func (i *DockerPackage) WithVersion(version string) *DockerPackage { + i.version = version + return i +} + // RPMPackage implements operations for a RPM installer type RPMPackage struct { BasePackage @@ -157,7 +241,6 @@ type TARPackage struct { arch string artifact string OS string - stale bool version string } @@ -215,15 +298,10 @@ func (i *TARPackage) Preinstall() error { return err } - version := i.version - if !i.stale { - version = checkElasticAgentVersion(i.version) - } - // simplify layout cmds := [][]string{ []string{"rm", "-fr", "/elastic-agent"}, - []string{"mv", fmt.Sprintf("/%s-%s-%s-%s", i.artifact, version, i.OS, i.arch), "/elastic-agent"}, + []string{"mv", fmt.Sprintf("/%s-%s-%s-%s", i.artifact, i.version, i.OS, i.arch), "/elastic-agent"}, } for _, cmd := range cmds { err = execCommandInService(i.profile, i.image, i.service, cmd, false) @@ -233,7 +311,7 @@ func (i *TARPackage) Preinstall() error { "error": err, "image": i.image, "service": i.service, - "version": version, + "version": i.version, }).Error("Could not extract agent package in the box") return err @@ -243,12 +321,6 @@ func (i *TARPackage) Preinstall() error { return nil } -// Stale sets the stale state -func (i *TARPackage) Stale(stale bool) *TARPackage { - i.stale = stale - return i -} - // Uninstall uninstalls a TAR package func (i *TARPackage) Uninstall() error { args := []string{"-f"} diff --git a/e2e/_suites/fleet/services.go b/e2e/_suites/fleet/services.go index d9b5b2eecf..abb35479fa 100644 --- a/e2e/_suites/fleet/services.go +++ b/e2e/_suites/fleet/services.go @@ -1,24 +1,19 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + package main import ( "context" "fmt" - "os" - "path" "strings" - "time" "github.com/elastic/e2e-testing/cli/docker" - "github.com/elastic/e2e-testing/cli/shell" "github.com/elastic/e2e-testing/e2e" log "github.com/sirupsen/logrus" ) -// 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. -var binariesCache = map[string]string{} - // ElasticAgentInstaller represents how to install an agent, depending of the box type type ElasticAgentInstaller struct { artifactArch string // architecture of the artifact @@ -166,84 +161,17 @@ func runElasticAgentCommand(profile string, image string, service string, proces // 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, stale bool) (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{ - "URL": URL, - "path": val, - }).Debug("Retrieving binary from local cache") - return fileName, val, nil - } - - filePath, err := e2e.DownloadFile(URL) - if err != nil { - return fileName, filePath, err - } - - binariesCache[URL] = filePath - - return fileName, filePath, nil - } - - if downloadURL, exists := os.LookupEnv("ELASTIC_AGENT_DOWNLOAD_URL"); exists { - return handleDownload(downloadURL, fileName) - } - - var downloadURL string - var err error - - useCISnapshots := shell.GetEnvBool("BEATS_USE_CI_SNAPSHOTS") - if useCISnapshots { - log.Debug("Using CI snapshots for the Elastic Agent") - - bucketFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, stale) - - maxTimeout := time.Duration(timeoutFactor) * time.Minute - - downloadURL, err = e2e.GetObjectURLFromBucket(bucket, prefix, object, maxTimeout) - if err != nil { - return "", "", err - } - - return handleDownload(downloadURL, bucketFileName) - } - - downloadVersion := version - if !stale { - downloadVersion = checkElasticAgentVersion(version) - } - - downloadURL, err = e2e.GetElasticArtifactURL(artifact, downloadVersion, OS, arch, extension) +func downloadAgentBinary(artifactName string, artifact string, version string) (string, error) { + imagePath, err := e2e.FetchBeatsBinary(artifactName, artifact, version, agentVersionBase, timeoutFactor, true) if err != nil { - return "", "", err + return "", err } - return handleDownload(downloadURL, fileName) + return imagePath, nil } // GetElasticAgentInstaller returns an installer from a docker image -func GetElasticAgentInstaller(image string, installerType string, version string, stale bool) ElasticAgentInstaller { +func GetElasticAgentInstaller(image string, installerType string, version string) ElasticAgentInstaller { log.WithFields(log.Fields{ "image": image, "installer": installerType, @@ -252,13 +180,17 @@ func GetElasticAgentInstaller(image string, installerType string, version string var installer ElasticAgentInstaller var err error if "centos" == image && "tar" == installerType { - installer, err = newTarInstaller("centos", "latest", version, stale) + installer, err = newTarInstaller("centos", "latest", version) } else if "centos" == image && "systemd" == installerType { - installer, err = newCentosInstaller("centos", "latest", version, stale) + installer, err = newCentosInstaller("centos", "latest", version) } else if "debian" == image && "tar" == installerType { - installer, err = newTarInstaller("debian", "stretch", version, stale) + installer, err = newTarInstaller("debian", "stretch", version) } else if "debian" == image && "systemd" == installerType { - installer, err = newDebianInstaller("debian", "stretch", version, stale) + installer, err = newDebianInstaller("debian", "stretch", version) + } else if "docker" == image && "default" == installerType { + installer, err = newDockerInstaller(false, version) + } else if "docker" == image && "ubi8" == installerType { + installer, err = newDockerInstaller(true, version) } else { log.WithFields(log.Fields{ "image": image, @@ -277,55 +209,12 @@ func GetElasticAgentInstaller(image string, installerType string, version string return installer } -// getGCPBucketCoordinates it calculates the bucket path in GCP -func getGCPBucketCoordinates(fileName string, artifact string, version string, OS string, arch string, extension string, stale bool) (string, string, string, string) { - if extension == "tar.gz" { - fileName = fmt.Sprintf("%s-%s-%s-%s.%s", artifact, version, OS, arch, extension) - } - - bucket := "beats-ci-artifacts" - prefix := fmt.Sprintf("snapshots/%s", artifact) - object := fileName - newFileName := fileName - - // the commit SHA will identify univocally the artifact in the GCP storage bucket - commitSHA := shell.GetEnv("GITHUB_CHECK_SHA1", "") - if commitSHA != "" { - prefix = fmt.Sprintf("commits/%s", commitSHA) - object = artifact + "/" + fileName - } - - // we are setting a version from a pull request: the version of the artifact will be kept as the base one - // i.e. /pull-requests/pr-21100/elastic-agent/elastic-agent-8.0.0-SNAPSHOT-x86_64.rpm - // i.e. /pull-requests/pr-21100/elastic-agent/elastic-agent-8.0.0-SNAPSHOT-amd64.deb - // i.e. /pull-requests/pr-21100/elastic-agent/elastic-agent-8.0.0-SNAPSHOT-linux-x86_64.tar.gz - if strings.HasPrefix(strings.ToLower(version), "pr-") { - newFileName = fmt.Sprintf("%s-%s-%s.%s", artifact, agentVersionBase, arch, extension) - if extension == "tar.gz" { - newFileName = fmt.Sprintf("%s-%s-%s-%s.%s", artifact, agentVersionBase, OS, arch, extension) - } - log.WithFields(log.Fields{ - "agentVersion": agentVersionBase, - "PR": version, - }).Debug("Using CI snapshots for a pull request") - prefix = fmt.Sprintf("pull-requests/%s", version) - object = fmt.Sprintf("%s/%s", artifact, newFileName) - } - - if stale { - prefix = fmt.Sprintf("snapshots/%s", artifact) - object = newFileName - } - - return newFileName, bucket, prefix, object -} - func isSystemdBased(image string) bool { return strings.HasSuffix(image, "-systemd") } // newCentosInstaller returns an instance of the Centos installer for a specific version -func newCentosInstaller(image string, tag string, version string, stale bool) (ElasticAgentInstaller, error) { +func newCentosInstaller(image string, tag string, version string) (ElasticAgentInstaller, error) { image = image + "-systemd" // we want to consume systemd boxes service := image profile := FleetProfileName @@ -336,7 +225,8 @@ func newCentosInstaller(image string, tag string, version string, stale bool) (E arch := "x86_64" extension := "rpm" - binaryName, binaryPath, err := downloadAgentBinary(artifact, version, os, arch, extension, stale) + binaryName := e2e.BuildArtifactName(artifact, version, agentVersionBase, os, arch, extension, false) + binaryPath, err := downloadAgentBinary(binaryName, artifact, version) if err != nil { log.WithFields(log.Fields{ "artifact": artifact, @@ -389,7 +279,7 @@ func newCentosInstaller(image string, tag string, version string, stale bool) (E } // newDebianInstaller returns an instance of the Debian installer for a specific version -func newDebianInstaller(image string, tag string, version string, stale bool) (ElasticAgentInstaller, error) { +func newDebianInstaller(image string, tag string, version string) (ElasticAgentInstaller, error) { image = image + "-systemd" // we want to consume systemd boxes service := image profile := FleetProfileName @@ -400,7 +290,8 @@ func newDebianInstaller(image string, tag string, version string, stale bool) (E arch := "amd64" extension := "deb" - binaryName, binaryPath, err := downloadAgentBinary(artifact, version, os, arch, extension, stale) + binaryName := e2e.BuildArtifactName(artifact, version, agentVersionBase, os, arch, extension, false) + binaryPath, err := downloadAgentBinary(binaryName, artifact, version) if err != nil { log.WithFields(log.Fields{ "artifact": artifact, @@ -452,8 +343,83 @@ func newDebianInstaller(image string, tag string, version string, stale bool) (E }, nil } +// newDockerInstaller returns an instance of the Docker installer +func newDockerInstaller(ubi8 bool, version string) (ElasticAgentInstaller, error) { + image := "elastic-agent" + service := image + profile := FleetProfileName + + // extract the agent in the box, as it's mounted as a volume + artifact := "elastic-agent" + + artifactName := artifact + if ubi8 { + artifactName = "elastic-agent-ubi8" + } + + os := "linux" + arch := "amd64" + extension := "tar.gz" + + binaryName := e2e.BuildArtifactName(artifactName, version, agentVersionBase, os, arch, extension, true) + binaryPath, err := downloadAgentBinary(binaryName, artifact, version) + if err != nil { + log.WithFields(log.Fields{ + "artifact": artifact, + "version": version, + "os": os, + "arch": arch, + "extension": extension, + "error": err, + }).Error("Could not download the binary for the agent") + return ElasticAgentInstaller{}, err + } + + commitFile := ".elastic-agent.active.commit" + homeDir := "/usr/share/elastic-agent" + binDir := "/usr/share/elastic-agent/data/elastic-agent-%s/" + + enrollFn := func(token string) error { + return nil + } + + installerPackage := NewDockerPackage(binaryName, profile, image, service, binaryPath, ubi8). + WithArch(arch). + WithArtifact(artifact). + WithOS(os). + WithVersion(e2e.CheckPRVersion(version, agentVersionBase)) // sanitize version + + return ElasticAgentInstaller{ + artifactArch: arch, + artifactExtension: extension, + artifactName: artifact, + artifactOS: os, + artifactVersion: version, + binDir: binDir, + commitFile: commitFile, + EnrollFn: enrollFn, + homeDir: homeDir, + image: image, + InstallFn: installerPackage.Install, + InstallCertsFn: installerPackage.InstallCerts, + installerType: "docker", + logFile: "elastic-agent-json.log", + logsDir: binDir + "logs/", + name: binaryName, + path: binaryPath, + PostInstallFn: installerPackage.Postinstall, + PreInstallFn: installerPackage.Preinstall, + processName: ElasticAgentProcessName, + profile: profile, + service: service, + tag: version, + UninstallFn: installerPackage.Uninstall, + workingDir: "/usr/share/elastic-agent/", + }, nil +} + // newTarInstaller returns an instance of the Debian installer for a specific version -func newTarInstaller(image string, tag string, version string, stale bool) (ElasticAgentInstaller, error) { +func newTarInstaller(image string, tag string, version string) (ElasticAgentInstaller, error) { image = image + "-systemd" // we want to consume systemd boxes service := image profile := FleetProfileName @@ -464,7 +430,8 @@ func newTarInstaller(image string, tag string, version string, stale bool) (Elas arch := "x86_64" extension := "tar.gz" - tarFile, binaryPath, err := downloadAgentBinary(artifact, version, os, arch, extension, stale) + binaryName := e2e.BuildArtifactName(artifact, version, agentVersionBase, os, arch, extension, false) + binaryPath, err := downloadAgentBinary(binaryName, artifact, version) if err != nil { log.WithFields(log.Fields{ "artifact": artifact, @@ -488,12 +455,11 @@ func newTarInstaller(image string, tag string, version string, stale bool) (Elas } // - installerPackage := NewTARPackage(tarFile, profile, image, service). - Stale(stale). + installerPackage := NewTARPackage(binaryName, profile, image, service). WithArch(arch). WithArtifact(artifact). WithOS(os). - WithVersion(version) + WithVersion(e2e.CheckPRVersion(version, agentVersionBase)) // sanitize version return ElasticAgentInstaller{ artifactArch: arch, @@ -511,7 +477,7 @@ func newTarInstaller(image string, tag string, version string, stale bool) (Elas installerType: "tar", logFile: "elastic-agent.log", logsDir: "/opt/Elastic/Agent/", - name: tarFile, + name: binaryName, path: binaryPath, PostInstallFn: installerPackage.Postinstall, PreInstallFn: installerPackage.Preinstall, diff --git a/e2e/_suites/fleet/services_test.go b/e2e/_suites/fleet/services_test.go index 7250297e59..12e4d87f43 100644 --- a/e2e/_suites/fleet/services_test.go +++ b/e2e/_suites/fleet/services_test.go @@ -1,30 +1,29 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + package main import ( "os" "path" + "path/filepath" "testing" "github.com/stretchr/testify/assert" ) -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" + distributionsDir, _ := filepath.Abs(path.Join(beatsDir, "x-pack", "elastic-agent", "build", "distributions")) 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, false) + _, err := downloadAgentBinary("foo_fileName", artifact, version) assert.NotNil(t, err) }) @@ -32,377 +31,59 @@ func TestDownloadAgentBinary(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" + artifactName := "elastic-agent-8.0.0-SNAPSHOT-x86_64.rpm" + expectedFilePath := path.Join(distributionsDir, artifactName) - newFileName, downloadedFilePath, err := downloadAgentBinary(artifact, version, OS, arch, extension, false) + downloadedFilePath, err := downloadAgentBinary(artifactName, artifact, version) assert.Nil(t, err) - assert.Equal(t, newFileName, expectedFileName) - assert.Equal(t, downloadedFilePath, path.Join(distributionsDir, expectedFileName)) + assert.Equal(t, downloadedFilePath, expectedFilePath) }) 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" + artifactName := "elastic-agent-8.0.0-SNAPSHOT-amd64.deb" + expectedFilePath := path.Join(distributionsDir, artifactName) - newFileName, downloadedFilePath, err := downloadAgentBinary(artifact, version, OS, arch, extension, false) + downloadedFilePath, err := downloadAgentBinary(artifactName, artifact, version) assert.Nil(t, err) - assert.Equal(t, newFileName, expectedFileName) - assert.Equal(t, downloadedFilePath, path.Join(distributionsDir, expectedFileName)) + assert.Equal(t, downloadedFilePath, expectedFilePath) }) 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" + artifactName := "elastic-agent-8.0.0-SNAPSHOT-linux-amd64.tar.gz" + expectedFilePath := path.Join(distributionsDir, artifactName) - newFileName, downloadedFilePath, err := downloadAgentBinary(artifact, version, OS, arch, extension, false) + downloadedFilePath, err := downloadAgentBinary(artifactName, artifact, version) 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 - OS := "linux" - - t.Run("Fetching commits bucket for RPM package", func(t *testing.T) { - defer os.Unsetenv("GITHUB_CHECK_SHA1") - os.Setenv("GITHUB_CHECK_SHA1", "0123456789") - - arch := "x86_64" - extension := "rpm" - fileName := "elastic-agent-" + testVersion + "-x86_64.rpm" - - newFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, false) - assert.Equal(t, bucket, "beats-ci-artifacts") - assert.Equal(t, prefix, "commits/0123456789") - assert.Equal(t, newFileName, "elastic-agent-"+testVersion+"-x86_64.rpm") - assert.Equal(t, object, "elastic-agent/elastic-agent-"+testVersion+"-x86_64.rpm") - }) - t.Run("Fetching commits bucket for stale RPM package in the snapshots folder", func(t *testing.T) { - defer os.Unsetenv("GITHUB_CHECK_SHA1") - os.Setenv("GITHUB_CHECK_SHA1", "0123456789") - - arch := "x86_64" - extension := "rpm" - fileName := "elastic-agent-" + testVersion + "-x86_64.rpm" - - newFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, true) - assert.Equal(t, bucket, "beats-ci-artifacts") - assert.Equal(t, prefix, "snapshots/elastic-agent") - assert.Equal(t, newFileName, "elastic-agent-"+testVersion+"-x86_64.rpm") - assert.Equal(t, object, "elastic-agent-"+testVersion+"-x86_64.rpm") - }) - - t.Run("Fetching commits bucket for DEB package", func(t *testing.T) { - defer os.Unsetenv("GITHUB_CHECK_SHA1") - os.Setenv("GITHUB_CHECK_SHA1", "0123456789") - - arch := "amd64" - extension := "deb" - fileName := "elastic-agent-" + testVersion + "-amd64.deb" - - newFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, false) - assert.Equal(t, bucket, "beats-ci-artifacts") - assert.Equal(t, prefix, "commits/0123456789") - assert.Equal(t, newFileName, "elastic-agent-"+testVersion+"-amd64.deb") - assert.Equal(t, object, "elastic-agent/elastic-agent-"+testVersion+"-amd64.deb") + assert.Equal(t, downloadedFilePath, expectedFilePath) }) - t.Run("Fetching commits bucket for stale DEB package in the snapshots folder", func(t *testing.T) { - defer os.Unsetenv("GITHUB_CHECK_SHA1") - os.Setenv("GITHUB_CHECK_SHA1", "0123456789") - - arch := "amd64" - extension := "deb" - fileName := "elastic-agent-" + testVersion + "-amd64.deb" - - newFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, true) - assert.Equal(t, bucket, "beats-ci-artifacts") - assert.Equal(t, prefix, "snapshots/elastic-agent") - assert.Equal(t, newFileName, "elastic-agent-"+testVersion+"-amd64.deb") - assert.Equal(t, object, "elastic-agent-"+testVersion+"-amd64.deb") - }) - - t.Run("Fetching commits bucket for TAR package adds OS to fileName and object", func(t *testing.T) { - defer os.Unsetenv("GITHUB_CHECK_SHA1") - os.Setenv("GITHUB_CHECK_SHA1", "0123456789") - - arch := "x86_64" - extension := "tar.gz" - fileName := "elastic-agent-" + testVersion + "-linux-x86_64.tar.gz" - newFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, false) - assert.Equal(t, bucket, "beats-ci-artifacts") - assert.Equal(t, prefix, "commits/0123456789") - assert.Equal(t, newFileName, "elastic-agent-"+testVersion+"-linux-x86_64.tar.gz") - assert.Equal(t, object, "elastic-agent/elastic-agent-"+testVersion+"-linux-x86_64.tar.gz") - }) - t.Run("Fetching commits bucket for stale TAR package adds OS to fileName and object in the snapshots folder", func(t *testing.T) { - defer os.Unsetenv("GITHUB_CHECK_SHA1") - os.Setenv("GITHUB_CHECK_SHA1", "0123456789") - - arch := "x86_64" - extension := "tar.gz" - fileName := "elastic-agent-" + testVersion + "-linux-x86_64.tar.gz" - - newFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, true) - assert.Equal(t, bucket, "beats-ci-artifacts") - assert.Equal(t, prefix, "snapshots/elastic-agent") - assert.Equal(t, newFileName, "elastic-agent-"+testVersion+"-linux-x86_64.tar.gz") - assert.Equal(t, object, "elastic-agent-"+testVersion+"-linux-x86_64.tar.gz") - }) -} - -func TestGetGCPBucketCoordinates_CommitsForAPullRequest(t *testing.T) { - artifact := "elastic-agent" - version := "pr-23456" - OS := "linux" - - t.Run("Fetching commits bucket for RPM package", func(t *testing.T) { - defer os.Unsetenv("GITHUB_CHECK_SHA1") - os.Setenv("GITHUB_CHECK_SHA1", "0123456789") - - arch := "x86_64" - extension := "rpm" - fileName := "elastic-agent-" + testVersion + "-x86_64.rpm" - - newFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, false) - assert.Equal(t, bucket, "beats-ci-artifacts") - assert.Equal(t, prefix, "pull-requests/pr-23456") - assert.Equal(t, newFileName, "elastic-agent-"+testVersion+"-x86_64.rpm") - assert.Equal(t, object, "elastic-agent/elastic-agent-"+testVersion+"-x86_64.rpm") - }) - t.Run("Fetching commits bucket for stale RPM package in the snapshots folder", func(t *testing.T) { - defer os.Unsetenv("GITHUB_CHECK_SHA1") - os.Setenv("GITHUB_CHECK_SHA1", "0123456789") - - arch := "x86_64" - extension := "rpm" - fileName := "elastic-agent-" + testVersion + "-x86_64.rpm" - - newFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, true) - assert.Equal(t, bucket, "beats-ci-artifacts") - assert.Equal(t, prefix, "snapshots/elastic-agent") - assert.Equal(t, newFileName, "elastic-agent-"+testVersion+"-x86_64.rpm") - assert.Equal(t, object, "elastic-agent-"+testVersion+"-x86_64.rpm") - }) - - t.Run("Fetching commits bucket for DEB package", func(t *testing.T) { - defer os.Unsetenv("GITHUB_CHECK_SHA1") - os.Setenv("GITHUB_CHECK_SHA1", "0123456789") - - arch := "amd64" - extension := "deb" - fileName := "elastic-agent-" + testVersion + "-amd64.deb" - - newFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, false) - assert.Equal(t, bucket, "beats-ci-artifacts") - assert.Equal(t, prefix, "pull-requests/pr-23456") - assert.Equal(t, newFileName, "elastic-agent-"+testVersion+"-amd64.deb") - assert.Equal(t, object, "elastic-agent/elastic-agent-"+testVersion+"-amd64.deb") - }) - t.Run("Fetching commits bucket for stale DEB package in the snapshots folder", func(t *testing.T) { - defer os.Unsetenv("GITHUB_CHECK_SHA1") - os.Setenv("GITHUB_CHECK_SHA1", "0123456789") - - arch := "amd64" - extension := "deb" - fileName := "elastic-agent-" + testVersion + "-amd64.deb" - - newFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, true) - assert.Equal(t, bucket, "beats-ci-artifacts") - assert.Equal(t, prefix, "snapshots/elastic-agent") - assert.Equal(t, newFileName, "elastic-agent-"+testVersion+"-amd64.deb") - assert.Equal(t, object, "elastic-agent-"+testVersion+"-amd64.deb") - }) - - t.Run("Fetching commits bucket for TAR package adds OS to fileName and object", func(t *testing.T) { - defer os.Unsetenv("GITHUB_CHECK_SHA1") - os.Setenv("GITHUB_CHECK_SHA1", "0123456789") - - arch := "x86_64" - extension := "tar.gz" - fileName := "elastic-agent-" + testVersion + "-linux-x86_64.tar.gz" - - newFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, false) - assert.Equal(t, bucket, "beats-ci-artifacts") - assert.Equal(t, prefix, "pull-requests/pr-23456") - assert.Equal(t, newFileName, "elastic-agent-"+testVersion+"-linux-x86_64.tar.gz") - assert.Equal(t, object, "elastic-agent/elastic-agent-"+testVersion+"-linux-x86_64.tar.gz") - }) - t.Run("Fetching commits bucket for stale TAR package adds OS to fileName and object in the snapshots folder", func(t *testing.T) { - defer os.Unsetenv("GITHUB_CHECK_SHA1") - os.Setenv("GITHUB_CHECK_SHA1", "0123456789") - - arch := "x86_64" - extension := "tar.gz" - fileName := "elastic-agent-" + testVersion + "-linux-x86_64.tar.gz" - - newFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, true) - assert.Equal(t, bucket, "beats-ci-artifacts") - assert.Equal(t, prefix, "snapshots/elastic-agent") - assert.Equal(t, newFileName, "elastic-agent-"+testVersion+"-linux-x86_64.tar.gz") - assert.Equal(t, object, "elastic-agent-"+testVersion+"-linux-x86_64.tar.gz") - }) -} - -func TestGetGCPBucketCoordinates_PullRequests(t *testing.T) { - artifact := "elastic-agent" - version := "pr-23456" - OS := "linux" - - t.Run("Fetching commits bucket for RPM package", func(t *testing.T) { - arch := "x86_64" - extension := "rpm" - fileName := "elastic-agent-" + testVersion + "-x86_64.rpm" - - newFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, false) - assert.Equal(t, newFileName, "elastic-agent-"+testVersion+"-x86_64.rpm") - assert.Equal(t, bucket, "beats-ci-artifacts") - assert.Equal(t, prefix, "pull-requests/pr-23456") - assert.Equal(t, object, "elastic-agent/elastic-agent-"+testVersion+"-x86_64.rpm") - }) - t.Run("Fetching commits bucket for stale RPM package in the snapshots folder", func(t *testing.T) { - arch := "x86_64" - extension := "rpm" - fileName := "elastic-agent-" + testVersion + "-x86_64.rpm" - - newFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, true) - assert.Equal(t, newFileName, "elastic-agent-"+testVersion+"-x86_64.rpm") - assert.Equal(t, bucket, "beats-ci-artifacts") - assert.Equal(t, prefix, "snapshots/elastic-agent") - assert.Equal(t, object, "elastic-agent-"+testVersion+"-x86_64.rpm") - }) - - t.Run("Fetching commits bucket for DEB package", func(t *testing.T) { - arch := "amd64" - extension := "deb" - fileName := "elastic-agent-" + testVersion + "-amd64.deb" - - newFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, false) - assert.Equal(t, newFileName, "elastic-agent-"+testVersion+"-amd64.deb") - assert.Equal(t, bucket, "beats-ci-artifacts") - assert.Equal(t, prefix, "pull-requests/pr-23456") - assert.Equal(t, object, "elastic-agent/elastic-agent-"+testVersion+"-amd64.deb") - }) - t.Run("Fetching commits bucket for stale DEB package in the snapshots folder", func(t *testing.T) { - arch := "amd64" - extension := "deb" - fileName := "elastic-agent-" + testVersion + "-amd64.deb" - - newFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, true) - assert.Equal(t, newFileName, "elastic-agent-"+testVersion+"-amd64.deb") - assert.Equal(t, bucket, "beats-ci-artifacts") - assert.Equal(t, prefix, "snapshots/elastic-agent") - assert.Equal(t, object, "elastic-agent-"+testVersion+"-amd64.deb") - }) - - t.Run("Fetching commits bucket for TAR package adds OS to fileName and object", func(t *testing.T) { - arch := "x86_64" - extension := "tar.gz" - fileName := "elastic-agent-" + testVersion + "-linux-x86_64.tar.gz" - - newFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, false) - assert.Equal(t, newFileName, "elastic-agent-"+testVersion+"-linux-x86_64.tar.gz") - assert.Equal(t, bucket, "beats-ci-artifacts") - assert.Equal(t, prefix, "pull-requests/pr-23456") - assert.Equal(t, object, "elastic-agent/elastic-agent-"+testVersion+"-linux-x86_64.tar.gz") - }) - t.Run("Fetching commits bucket for stale TAR package adds OS to fileName and object in the snapshots folder", func(t *testing.T) { - arch := "x86_64" - extension := "tar.gz" - fileName := "elastic-agent-" + testVersion + "-linux-x86_64.tar.gz" - - newFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, true) - assert.Equal(t, newFileName, "elastic-agent-"+testVersion+"-linux-x86_64.tar.gz") - assert.Equal(t, bucket, "beats-ci-artifacts") - assert.Equal(t, prefix, "snapshots/elastic-agent") - assert.Equal(t, object, "elastic-agent-"+testVersion+"-linux-x86_64.tar.gz") - }) -} - -func TestGetGCPBucketCoordinates_Snapshots(t *testing.T) { - artifact := "elastic-agent" - version := testVersion - OS := "linux" - - t.Run("Fetching commits bucket for RPM package", func(t *testing.T) { - arch := "x86_64" - extension := "rpm" - fileName := "elastic-agent-" + testVersion + "-x86_64.rpm" - - newFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, false) - assert.Equal(t, bucket, "beats-ci-artifacts") - assert.Equal(t, prefix, "snapshots/elastic-agent") - assert.Equal(t, newFileName, "elastic-agent-"+testVersion+"-x86_64.rpm") - assert.Equal(t, object, "elastic-agent-"+testVersion+"-x86_64.rpm") - }) - t.Run("Fetching commits bucket for stale RPM package", func(t *testing.T) { - arch := "x86_64" - extension := "rpm" - fileName := "elastic-agent-" + testVersion + "-x86_64.rpm" - - newFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, true) - assert.Equal(t, bucket, "beats-ci-artifacts") - assert.Equal(t, prefix, "snapshots/elastic-agent") - assert.Equal(t, newFileName, "elastic-agent-"+testVersion+"-x86_64.rpm") - assert.Equal(t, object, "elastic-agent-"+testVersion+"-x86_64.rpm") - }) - - t.Run("Fetching commits bucket for DEB package", func(t *testing.T) { - arch := "amd64" - extension := "deb" - fileName := "elastic-agent-" + testVersion + "-amd64.deb" + t.Run("Fetching Docker binary from local Beats dir", func(t *testing.T) { + defer os.Unsetenv("BEATS_LOCAL_PATH") + os.Setenv("BEATS_LOCAL_PATH", beatsDir) - newFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, false) - assert.Equal(t, bucket, "beats-ci-artifacts") - assert.Equal(t, prefix, "snapshots/elastic-agent") - assert.Equal(t, newFileName, "elastic-agent-"+testVersion+"-amd64.deb") - assert.Equal(t, object, "elastic-agent-"+testVersion+"-amd64.deb") - }) - t.Run("Fetching commits bucket for stale DEB package", func(t *testing.T) { - arch := "amd64" - extension := "deb" - fileName := "elastic-agent-" + testVersion + "-amd64.deb" + artifactName := "elastic-agent-8.0.0-SNAPSHOT-linux-amd64.docker.tar.gz" + expectedFilePath := path.Join(distributionsDir, artifactName) - newFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, true) - assert.Equal(t, bucket, "beats-ci-artifacts") - assert.Equal(t, prefix, "snapshots/elastic-agent") - assert.Equal(t, newFileName, "elastic-agent-"+testVersion+"-amd64.deb") - assert.Equal(t, object, "elastic-agent-"+testVersion+"-amd64.deb") + downloadedFilePath, err := downloadAgentBinary(artifactName, artifact, version) + assert.Nil(t, err) + assert.Equal(t, downloadedFilePath, expectedFilePath) }) - t.Run("Fetching commits bucket for TAR package adds OS to fileName and object", func(t *testing.T) { - arch := "x86_64" - extension := "tar.gz" - fileName := "elastic-agent-" + testVersion + "-linux-x86_64.tar.gz" + t.Run("Fetching ubi8 Docker binary from local Beats dir", func(t *testing.T) { + defer os.Unsetenv("BEATS_LOCAL_PATH") + os.Setenv("BEATS_LOCAL_PATH", beatsDir) - newFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, false) - assert.Equal(t, bucket, "beats-ci-artifacts") - assert.Equal(t, prefix, "snapshots/elastic-agent") - assert.Equal(t, newFileName, "elastic-agent-"+testVersion+"-linux-x86_64.tar.gz") - assert.Equal(t, object, "elastic-agent-"+testVersion+"-linux-x86_64.tar.gz") - }) - t.Run("Fetching commits bucket for stale TAR package adds OS to fileName and object", func(t *testing.T) { - arch := "x86_64" - extension := "tar.gz" - fileName := "elastic-agent-" + testVersion + "-linux-x86_64.tar.gz" + artifactName := "elastic-agent-ubi8-8.0.0-SNAPSHOT-linux-amd64.docker.tar.gz" + expectedFilePath := path.Join(distributionsDir, artifactName) - newFileName, bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, OS, arch, extension, true) - assert.Equal(t, bucket, "beats-ci-artifacts") - assert.Equal(t, prefix, "snapshots/elastic-agent") - assert.Equal(t, newFileName, "elastic-agent-"+testVersion+"-linux-x86_64.tar.gz") - assert.Equal(t, object, "elastic-agent-"+testVersion+"-linux-x86_64.tar.gz") + downloadedFilePath, err := downloadAgentBinary(artifactName, artifact, version) + assert.Nil(t, err) + assert.Equal(t, downloadedFilePath, expectedFilePath) }) } diff --git a/e2e/_suites/fleet/stand-alone.go b/e2e/_suites/fleet/stand-alone.go index 42dbfc43bf..08475eb336 100644 --- a/e2e/_suites/fleet/stand-alone.go +++ b/e2e/_suites/fleet/stand-alone.go @@ -14,6 +14,7 @@ import ( "github.com/cucumber/godog" "github.com/elastic/e2e-testing/cli/docker" "github.com/elastic/e2e-testing/cli/services" + shell "github.com/elastic/e2e-testing/cli/shell" "github.com/elastic/e2e-testing/e2e" log "github.com/sirupsen/logrus" ) @@ -62,6 +63,17 @@ func (sats *StandAloneTestSuite) contributeSteps(s *godog.ScenarioContext) { func (sats *StandAloneTestSuite) aStandaloneAgentIsDeployed(image string) error { log.Trace("Deploying an agent to Fleet") + useCISnapshots := shell.GetEnvBool("BEATS_USE_CI_SNAPSHOTS") + beatsLocalPath := shell.GetEnv("BEATS_LOCAL_PATH", "") + if useCISnapshots || beatsLocalPath != "" { + // load the docker images that were already: + // a. downloaded from the GCP bucket + // b. fetched from the local beats binaries + dockerInstaller := GetElasticAgentInstaller("docker", image, agentVersion) + + dockerInstaller.PreInstallFn() + } + serviceManager := services.NewServiceManager() profileEnv["elasticAgentDockerImageSuffix"] = "" diff --git a/e2e/_suites/metricbeat/metricbeat_test.go b/e2e/_suites/metricbeat/metricbeat_test.go index cb3cc77258..ee8b6d20cf 100644 --- a/e2e/_suites/metricbeat/metricbeat_test.go +++ b/e2e/_suites/metricbeat/metricbeat_test.go @@ -15,6 +15,7 @@ import ( "github.com/cucumber/godog" messages "github.com/cucumber/messages-go/v10" "github.com/elastic/e2e-testing/cli/config" + "github.com/elastic/e2e-testing/cli/docker" "github.com/elastic/e2e-testing/cli/services" "github.com/elastic/e2e-testing/cli/shell" "github.com/elastic/e2e-testing/e2e" @@ -281,9 +282,7 @@ func (mts *MetricbeatTestSuite) installedUsingConfiguration(configuration string mts.Version = metricbeatVersion mts.setIndexName() - if strings.HasPrefix(metricbeatVersion, "pr-") { - metricbeatVersion = metricbeatVersionBase - } + metricbeatVersion = e2e.CheckPRVersion(metricbeatVersion, metricbeatVersionBase) // use master branch for snapshots tag := "v" + metricbeatVersion @@ -313,6 +312,22 @@ func (mts *MetricbeatTestSuite) installedUsingConfiguration(configuration string // runMetricbeatService runs a metricbeat service entity for a service to monitor it func (mts *MetricbeatTestSuite) runMetricbeatService() error { + useCISnapshots := shell.GetEnvBool("BEATS_USE_CI_SNAPSHOTS") + beatsLocalPath := shell.GetEnv("BEATS_LOCAL_PATH", "") + if useCISnapshots || beatsLocalPath != "" { + artifactName := e2e.BuildArtifactName("metricbeat", mts.Version, metricbeatVersionBase, "linux", "amd64", "tar.gz", true) + + imagePath, err := e2e.FetchBeatsBinary(artifactName, "metricbeat", mts.Version, metricbeatVersionBase, timeoutFactor, false) + if err != nil { + return err + } + + err = docker.LoadImage(imagePath) + if err != nil { + return err + } + } + // this is needed because, in general, the target service (apache, mysql, redis) does not have a healthcheck waitForService := time.Duration(timeoutFactor) * 10 * time.Second if mts.ServiceName == "ceph" { diff --git a/e2e/_testresources/beats/x-pack/elastic-agent/build/distributions/elastic-agent-8.0.0-SNAPSHOT-linux-amd64.docker.tar.gz b/e2e/_testresources/beats/x-pack/elastic-agent/build/distributions/elastic-agent-8.0.0-SNAPSHOT-linux-amd64.docker.tar.gz new file mode 100644 index 0000000000..e69de29bb2 diff --git a/e2e/_testresources/beats/x-pack/elastic-agent/build/distributions/elastic-agent-ubi8-8.0.0-SNAPSHOT-linux-amd64.docker.tar.gz b/e2e/_testresources/beats/x-pack/elastic-agent/build/distributions/elastic-agent-ubi8-8.0.0-SNAPSHOT-linux-amd64.docker.tar.gz new file mode 100644 index 0000000000..e69de29bb2 diff --git a/e2e/utils.go b/e2e/utils.go index 350cd708fb..001aa4163e 100644 --- a/e2e/utils.go +++ b/e2e/utils.go @@ -13,6 +13,7 @@ import ( "net/http" "os" "path" + "path/filepath" "strings" "time" @@ -24,12 +25,158 @@ import ( log "github.com/sirupsen/logrus" ) +// 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. +var binariesCache = map[string]string{} + //nolint:unused const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" //nolint:unused var seededRand = rand.New(rand.NewSource(time.Now().UnixNano())) +// BuildArtifactName builds the artifact name from the different coordinates for the artifact +func BuildArtifactName(artifact string, version string, fallbackVersion string, OS string, arch string, extension string, isDocker bool) string { + dockerString := "" + if isDocker { + dockerString = ".docker" + } + + artifactVersion := CheckPRVersion(version, fallbackVersion) + + lowerCaseExtension := strings.ToLower(extension) + + artifactName := fmt.Sprintf("%s-%s-%s-%s%s.%s", artifact, artifactVersion, OS, arch, dockerString, lowerCaseExtension) + if lowerCaseExtension == "deb" || lowerCaseExtension == "rpm" { + artifactName = fmt.Sprintf("%s-%s-%s%s.%s", artifact, artifactVersion, arch, dockerString, lowerCaseExtension) + } + + beatsLocalPath := shell.GetEnv("BEATS_LOCAL_PATH", "") + if beatsLocalPath != "" && isDocker { + return fmt.Sprintf("%s-%s-%s-%s%s.%s", artifact, artifactVersion, OS, arch, dockerString, lowerCaseExtension) + } + + useCISnapshots := shell.GetEnvBool("BEATS_USE_CI_SNAPSHOTS") + // we detected that the docker name on CI is using a different structure + // CI snapshots on GCP: elastic-agent-$VERSION-linux-amd64.docker.tar.gz + // Elastic's snapshots: elastic-agent-$VERSION-docker-image-linux-amd64.tar.gz + if !useCISnapshots && isDocker { + dockerString = "docker-image" + artifactName = fmt.Sprintf("%s-%s-%s-%s-%s.%s", artifact, artifactVersion, dockerString, OS, arch, lowerCaseExtension) + } + + return artifactName +} + +// CheckPRVersion returns a fallback version if the version comes from a Pull Request (PR) +func CheckPRVersion(version string, fallbackVersion string) string { + if strings.HasPrefix(strings.ToLower(version), "pr-") { + return fallbackVersion + } + + return version +} + +// FetchBeatsBinary it downloads the binary and returns the location of the downloaded file +// 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 FetchBeatsBinary(artifactName string, artifact string, version string, fallbackVersion string, timeoutFactor int, xpack bool) (string, error) { + beatsLocalPath := shell.GetEnv("BEATS_LOCAL_PATH", "") + if beatsLocalPath != "" { + distributions := path.Join(beatsLocalPath, artifact, "build", "distributions") + if xpack { + distributions = path.Join(beatsLocalPath, "x-pack", artifact, "build", "distributions") + } + + log.Debugf("Using local snapshots for the %s: %s", artifact, distributions) + + fileNamePath, _ := filepath.Abs(path.Join(distributions, artifactName)) + _, err := os.Stat(fileNamePath) + if err != nil || os.IsNotExist(err) { + return fileNamePath, err + } + + return fileNamePath, err + } + + handleDownload := func(URL string) (string, error) { + if val, ok := binariesCache[URL]; ok { + log.WithFields(log.Fields{ + "URL": URL, + "path": val, + }).Debug("Retrieving binary from local cache") + return val, nil + } + + filePath, err := DownloadFile(URL) + if err != nil { + return filePath, err + } + + binariesCache[URL] = filePath + + return filePath, nil + } + + var downloadURL string + var err error + + useCISnapshots := shell.GetEnvBool("BEATS_USE_CI_SNAPSHOTS") + if useCISnapshots { + log.Debugf("Using CI snapshots for %s", artifact) + + bucket, prefix, object := getGCPBucketCoordinates(artifactName, artifact, version, fallbackVersion) + + maxTimeout := time.Duration(timeoutFactor) * time.Minute + + downloadURL, err = GetObjectURLFromBucket(bucket, prefix, object, maxTimeout) + if err != nil { + return "", err + } + + return handleDownload(downloadURL) + } + + downloadURL, err = GetElasticArtifactURL(artifactName, artifact, version) + if err != nil { + return "", err + } + + return handleDownload(downloadURL) +} + +// getGCPBucketCoordinates it calculates the bucket path in GCP +func getGCPBucketCoordinates(fileName string, artifact string, version string, fallbackVersion string) (string, string, string) { + bucket := "beats-ci-artifacts" + prefix := fmt.Sprintf("snapshots/%s", artifact) + object := fileName + + // the commit SHA will identify univocally the artifact in the GCP storage bucket + commitSHA := shell.GetEnv("GITHUB_CHECK_SHA1", "") + if commitSHA != "" { + prefix = fmt.Sprintf("commits/%s", commitSHA) + object = artifact + "/" + fileName + } + + // we are setting a version from a pull request: the version of the artifact will be kept as the base one + // i.e. /pull-requests/pr-21100/$THE_BEAT/$THE_BEAT-$VERSION-x86_64.rpm + // i.e. /pull-requests/pr-21100/$THE_BEAT/$THE_BEAT-$VERSION-amd64.deb + // i.e. /pull-requests/pr-21100/$THE_BEAT/$THE_BEAT-$VERSION-linux-x86_64.tar.gz + if strings.HasPrefix(strings.ToLower(version), "pr-") { + log.WithFields(log.Fields{ + "version": fallbackVersion, + "PR": version, + }).Debug("Using CI snapshots for a pull request") + prefix = fmt.Sprintf("pull-requests/%s", version) + object = fmt.Sprintf("%s/%s", artifact, fileName) + } + + return bucket, prefix, object +} + // GetExponentialBackOff returns a preconfigured exponential backoff instance func GetExponentialBackOff(elapsedTime time.Duration) *backoff.ExponentialBackOff { var ( @@ -53,7 +200,7 @@ func GetExponentialBackOff(elapsedTime time.Duration) *backoff.ExponentialBackOf // GetElasticArtifactVersion returns the current version: // 1. Elastic's artifact repository, building the JSON path query based // If the version is a PR, then it will return the version without checking the artifacts API -// i.e. GetElasticArtifactVersion("8.0.0-SNAPSHOT") +// i.e. GetElasticArtifactVersion("$VERSION") // i.e. GetElasticArtifactVersion("pr-22000") func GetElasticArtifactVersion(version string) string { if strings.HasPrefix(strings.ToLower(version), "pr-") { @@ -127,14 +274,12 @@ func GetElasticArtifactVersion(version string) string { return latestVersion } -// GetElasticArtifactURL returns the URL of a released artifact from two possible sources -// on the desired OS, architecture and file extension: -// 1. Observability CI Storage bucket -// 2. Elastic's artifact repository, building the JSON path query based -// i.e. GetElasticArtifactURL("elastic-agent", "8.0.0-SNAPSHOT", "linux", "x86_64", "tar.gz") -// i.e. GetElasticArtifactURL("elastic-agent", "8.0.0-SNAPSHOT", "x86_64", "rpm") -// i.e. GetElasticArtifactURL("elastic-agent", "8.0.0-SNAPSHOT", "amd64", "deb") -func GetElasticArtifactURL(artifact string, version string, operativeSystem string, arch string, extension string) (string, error) { +// GetElasticArtifactURL returns the URL of a released artifact, which its full name is defined in the first argument, +// from Elastic's artifact repository, building the JSON path query based on the full name +// i.e. GetElasticArtifactURL("elastic-agent-$VERSION-amd64.deb", "elastic-agent", "$VERSION") +// i.e. GetElasticArtifactURL("elastic-agent-$VERSION-x86_64.rpm", "elastic-agent","$VERSION") +// i.e. GetElasticArtifactURL("elastic-agent-$VERSION-linux-amd64.tar.gz", "elastic-agent","$VERSION") +func GetElasticArtifactURL(artifactName string, artifact string, version string) (string, error) { exp := GetExponentialBackOff(time.Minute) retryCount := 1 @@ -150,10 +295,8 @@ func GetElasticArtifactURL(artifact string, version string, operativeSystem stri if err != nil { log.WithFields(log.Fields{ "artifact": artifact, + "artifactName": artifactName, "version": version, - "os": operativeSystem, - "arch": arch, - "extension": extension, "error": err, "retry": retryCount, "statusEndpoint": r.URL, @@ -183,26 +326,16 @@ func GetElasticArtifactURL(artifact string, version string, operativeSystem stri jsonParsed, err := gabs.ParseJSON([]byte(body)) if err != nil { log.WithFields(log.Fields{ - "artifact": artifact, - "version": version, - "os": operativeSystem, - "arch": arch, - "extension": extension, + "artifact": artifact, + "artifactName": artifactName, + "version": version, }).Error("Could not parse the response body for the artifact") return "", err } - // elastic-agent-8.0.0-SNAPSHOT-linux-x86_64.tar.gz - artifactPath := fmt.Sprintf("%s-%s-%s-%s.%s", artifact, version, operativeSystem, arch, extension) - if extension == "deb" || extension == "rpm" { - // elastic-agent-8.0.0-SNAPSHOT-x86_64.rpm - // elastic-agent-8.0.0-SNAPSHOT-amd64.deb - artifactPath = fmt.Sprintf("%s-%s-%s.%s", artifact, version, arch, extension) - } - packagesObject := jsonParsed.Path("packages") // we need to get keys with dots using Search instead of Path - downloadObject := packagesObject.Search(artifactPath) + downloadObject := packagesObject.Search(artifactName) downloadURL := downloadObject.Path("url").Data().(string) return downloadURL, nil diff --git a/e2e/utils_test.go b/e2e/utils_test.go index ec2521149d..0f6ae28a58 100644 --- a/e2e/utils_test.go +++ b/e2e/utils_test.go @@ -10,6 +10,8 @@ import ( gabs "github.com/Jeffail/gabs/v2" ) +var testVersion = "8.0.0-SNAPSHOT" + const bucket = "beats-ci-artifacts" const pullRequests = "pull-requests" const snapshots = "snapshots" @@ -38,6 +40,176 @@ func init() { snapshotsJSON, _ = gabs.ParseJSON([]byte(snapshotsContent)) } +func TestBuildArtifactName(t *testing.T) { + artifact := "elastic-agent" + OS := "linux" + version := "8.0.0-SNAPSHOT" + + t.Run("For RPM", func(t *testing.T) { + arch := "x86_64" + extension := "rpm" + expectedFileName := "elastic-agent-8.0.0-SNAPSHOT-x86_64.rpm" + + artifactName := BuildArtifactName(artifact, version, version, OS, arch, extension, false) + assert.Equal(t, expectedFileName, artifactName) + + artifactName = BuildArtifactName(artifact, version, version, OS, arch, "RPM", false) + assert.Equal(t, expectedFileName, artifactName) + }) + + t.Run("For DEB", func(t *testing.T) { + arch := "amd64" + extension := "deb" + expectedFileName := "elastic-agent-8.0.0-SNAPSHOT-amd64.deb" + + artifactName := BuildArtifactName(artifact, version, version, OS, arch, extension, false) + assert.Equal(t, expectedFileName, artifactName) + + artifactName = BuildArtifactName(artifact, version, version, OS, arch, "DEB", false) + assert.Equal(t, expectedFileName, artifactName) + }) + + t.Run("For TAR", func(t *testing.T) { + arch := "amd64" + extension := "tar.gz" + expectedFileName := "elastic-agent-8.0.0-SNAPSHOT-linux-amd64.tar.gz" + + artifactName := BuildArtifactName(artifact, version, version, OS, arch, extension, false) + assert.Equal(t, expectedFileName, artifactName) + + artifactName = BuildArtifactName(artifact, version, version, OS, arch, "TAR.GZ", false) + assert.Equal(t, expectedFileName, artifactName) + }) + + t.Run("For Docker from Elastic's repository", func(t *testing.T) { + defer os.Unsetenv("BEATS_USE_CI_SNAPSHOTS") + os.Setenv("BEATS_USE_CI_SNAPSHOTS", "false") + + artifact = "elastic-agent" + arch := "amd64" + extension := "tar.gz" + expectedFileName := "elastic-agent-8.0.0-SNAPSHOT-docker-image-linux-amd64.tar.gz" + + artifactName := BuildArtifactName(artifact, version, version, OS, arch, extension, true) + assert.Equal(t, expectedFileName, artifactName) + + artifactName = BuildArtifactName(artifact, version, version, OS, arch, "TAR.GZ", true) + assert.Equal(t, expectedFileName, artifactName) + }) + + t.Run("For Docker UBI8 from Elastic's repository", func(t *testing.T) { + defer os.Unsetenv("BEATS_USE_CI_SNAPSHOTS") + os.Setenv("BEATS_USE_CI_SNAPSHOTS", "false") + + artifact = "elastic-agent-ubi8" + arch := "amd64" + extension := "tar.gz" + expectedFileName := "elastic-agent-ubi8-8.0.0-SNAPSHOT-docker-image-linux-amd64.tar.gz" + + artifactName := BuildArtifactName(artifact, version, version, OS, arch, extension, true) + assert.Equal(t, expectedFileName, artifactName) + + artifactName = BuildArtifactName(artifact, version, version, OS, arch, "TAR.GZ", true) + assert.Equal(t, expectedFileName, artifactName) + }) + + t.Run("For Docker from local repository", func(t *testing.T) { + defer os.Unsetenv("BEATS_LOCAL_PATH") + os.Setenv("BEATS_LOCAL_PATH", "/tmp") + + artifact = "elastic-agent" + arch := "amd64" + extension := "tar.gz" + expectedFileName := "elastic-agent-8.0.0-SNAPSHOT-linux-amd64.docker.tar.gz" + + artifactName := BuildArtifactName(artifact, version, version, OS, arch, extension, true) + assert.Equal(t, expectedFileName, artifactName) + + artifactName = BuildArtifactName(artifact, version, version, OS, arch, "TAR.GZ", true) + assert.Equal(t, expectedFileName, artifactName) + }) + + t.Run("For Docker UBI8 from local repository", func(t *testing.T) { + defer os.Unsetenv("BEATS_LOCAL_PATH") + os.Setenv("BEATS_LOCAL_PATH", "/tmp") + + artifact = "elastic-agent-ubi8" + arch := "amd64" + extension := "tar.gz" + expectedFileName := "elastic-agent-ubi8-8.0.0-SNAPSHOT-linux-amd64.docker.tar.gz" + + artifactName := BuildArtifactName(artifact, version, version, OS, arch, extension, true) + assert.Equal(t, expectedFileName, artifactName) + + artifactName = BuildArtifactName(artifact, version, version, OS, arch, "TAR.GZ", true) + assert.Equal(t, expectedFileName, artifactName) + }) + + t.Run("For Docker from GCP", func(t *testing.T) { + defer os.Unsetenv("BEATS_USE_CI_SNAPSHOTS") + os.Setenv("BEATS_USE_CI_SNAPSHOTS", "true") + + artifact = "elastic-agent" + arch := "amd64" + extension := "tar.gz" + expectedFileName := "elastic-agent-8.0.0-SNAPSHOT-linux-amd64.docker.tar.gz" + + artifactName := BuildArtifactName(artifact, version, version, OS, arch, extension, true) + assert.Equal(t, expectedFileName, artifactName) + + artifactName = BuildArtifactName(artifact, version, version, OS, arch, "TAR.GZ", true) + assert.Equal(t, expectedFileName, artifactName) + }) + + t.Run("For Docker UBI8 from GCP", func(t *testing.T) { + defer os.Unsetenv("BEATS_USE_CI_SNAPSHOTS") + os.Setenv("BEATS_USE_CI_SNAPSHOTS", "true") + + artifact = "elastic-agent-ubi8" + arch := "amd64" + extension := "tar.gz" + expectedFileName := "elastic-agent-ubi8-8.0.0-SNAPSHOT-linux-amd64.docker.tar.gz" + + artifactName := BuildArtifactName(artifact, version, version, OS, arch, extension, true) + assert.Equal(t, expectedFileName, artifactName) + + artifactName = BuildArtifactName(artifact, version, version, OS, arch, "TAR.GZ", true) + assert.Equal(t, expectedFileName, artifactName) + }) + + t.Run("For Docker for a Pull Request", func(t *testing.T) { + defer os.Unsetenv("ELASTIC_AGENT_VERSION") + os.Setenv("ELASTIC_AGENT_VERSION", "pr-12345") + + artifact = "elastic-agent" + arch := "amd64" + extension := "tar.gz" + fallbackVersion := "8.0.0-SNAPSHOT" + expectedFileName := "elastic-agent-8.0.0-SNAPSHOT-docker-image-linux-amd64.tar.gz" + + artifactName := BuildArtifactName(artifact, version, fallbackVersion, OS, arch, extension, true) + assert.Equal(t, expectedFileName, artifactName) + + artifactName = BuildArtifactName(artifact, version, fallbackVersion, OS, arch, "TAR.GZ", true) + assert.Equal(t, expectedFileName, artifactName) + }) +} + +func TestCheckPRVersion(t *testing.T) { + t.Run("Checking a version should return the version", func(t *testing.T) { + v := CheckPRVersion(testVersion, testVersion) + + assert.Equal(t, testVersion, v) + }) + + t.Run("A PR should return base version", func(t *testing.T) { + prVersion := "pr-12345" + v := CheckPRVersion(prVersion, testVersion) + + assert.Equal(t, testVersion, v) + }) +} + func TestGetBucketSearchNextPageParam_HasMorePages(t *testing.T) { expectedParam := "&pageToken=foo" @@ -51,6 +223,152 @@ func TestGetBucketSearchNextPageParam_HasNoMorePages(t *testing.T) { assert.True(t, param == "") } +func TestGetGCPBucketCoordinates_Commits(t *testing.T) { + artifact := "elastic-agent" + version := testVersion + + t.Run("Fetching commits bucket for RPM package", func(t *testing.T) { + defer os.Unsetenv("GITHUB_CHECK_SHA1") + os.Setenv("GITHUB_CHECK_SHA1", "0123456789") + + fileName := "elastic-agent-" + testVersion + "-x86_64.rpm" + + bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, testVersion) + assert.Equal(t, bucket, "beats-ci-artifacts") + assert.Equal(t, prefix, "commits/0123456789") + assert.Equal(t, object, "elastic-agent/elastic-agent-"+testVersion+"-x86_64.rpm") + }) + + t.Run("Fetching commits bucket for DEB package", func(t *testing.T) { + defer os.Unsetenv("GITHUB_CHECK_SHA1") + os.Setenv("GITHUB_CHECK_SHA1", "0123456789") + + fileName := "elastic-agent-" + testVersion + "-amd64.deb" + + bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, testVersion) + assert.Equal(t, bucket, "beats-ci-artifacts") + assert.Equal(t, prefix, "commits/0123456789") + assert.Equal(t, object, "elastic-agent/elastic-agent-"+testVersion+"-amd64.deb") + }) + + t.Run("Fetching commits bucket for TAR package adds OS to fileName and object", func(t *testing.T) { + defer os.Unsetenv("GITHUB_CHECK_SHA1") + os.Setenv("GITHUB_CHECK_SHA1", "0123456789") + + fileName := "elastic-agent-" + testVersion + "-linux-x86_64.tar.gz" + + bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, testVersion) + assert.Equal(t, bucket, "beats-ci-artifacts") + assert.Equal(t, prefix, "commits/0123456789") + assert.Equal(t, object, "elastic-agent/elastic-agent-"+testVersion+"-linux-x86_64.tar.gz") + }) +} + +func TestGetGCPBucketCoordinates_CommitsForAPullRequest(t *testing.T) { + artifact := "elastic-agent" + version := "pr-23456" + + t.Run("Fetching pull request bucket for RPM package", func(t *testing.T) { + defer os.Unsetenv("GITHUB_CHECK_SHA1") + os.Setenv("GITHUB_CHECK_SHA1", "0123456789") + + fileName := "elastic-agent-" + testVersion + "-x86_64.rpm" + + bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, testVersion) + assert.Equal(t, bucket, "beats-ci-artifacts") + assert.Equal(t, prefix, "pull-requests/pr-23456") + assert.Equal(t, object, "elastic-agent/elastic-agent-"+testVersion+"-x86_64.rpm") + }) + + t.Run("Fetching pull request bucket for DEB package", func(t *testing.T) { + defer os.Unsetenv("GITHUB_CHECK_SHA1") + os.Setenv("GITHUB_CHECK_SHA1", "0123456789") + + fileName := "elastic-agent-" + testVersion + "-amd64.deb" + + bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, testVersion) + assert.Equal(t, bucket, "beats-ci-artifacts") + assert.Equal(t, prefix, "pull-requests/pr-23456") + assert.Equal(t, object, "elastic-agent/elastic-agent-"+testVersion+"-amd64.deb") + }) + + t.Run("Fetching pull request bucket for TAR package adds OS to fileName and object", func(t *testing.T) { + defer os.Unsetenv("GITHUB_CHECK_SHA1") + os.Setenv("GITHUB_CHECK_SHA1", "0123456789") + + fileName := "elastic-agent-" + testVersion + "-linux-x86_64.tar.gz" + + bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, testVersion) + assert.Equal(t, bucket, "beats-ci-artifacts") + assert.Equal(t, prefix, "pull-requests/pr-23456") + assert.Equal(t, object, "elastic-agent/elastic-agent-"+testVersion+"-linux-x86_64.tar.gz") + }) +} + +func TestGetGCPBucketCoordinates_PullRequests(t *testing.T) { + artifact := "elastic-agent" + version := "pr-23456" + + t.Run("Fetching pull request bucket for RPM package", func(t *testing.T) { + fileName := "elastic-agent-" + testVersion + "-x86_64.rpm" + + bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, testVersion) + assert.Equal(t, bucket, "beats-ci-artifacts") + assert.Equal(t, prefix, "pull-requests/pr-23456") + assert.Equal(t, object, "elastic-agent/elastic-agent-"+testVersion+"-x86_64.rpm") + }) + + t.Run("Fetching pull request bucket for DEB package", func(t *testing.T) { + fileName := "elastic-agent-" + testVersion + "-amd64.deb" + + bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, testVersion) + assert.Equal(t, bucket, "beats-ci-artifacts") + assert.Equal(t, prefix, "pull-requests/pr-23456") + assert.Equal(t, object, "elastic-agent/elastic-agent-"+testVersion+"-amd64.deb") + }) + + t.Run("Fetching pull request bucket for TAR package adds OS to fileName and object", func(t *testing.T) { + fileName := "elastic-agent-" + testVersion + "-linux-x86_64.tar.gz" + + bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, testVersion) + assert.Equal(t, bucket, "beats-ci-artifacts") + assert.Equal(t, prefix, "pull-requests/pr-23456") + assert.Equal(t, object, "elastic-agent/elastic-agent-"+testVersion+"-linux-x86_64.tar.gz") + }) +} + +func TestGetGCPBucketCoordinates_Snapshots(t *testing.T) { + artifact := "elastic-agent" + version := testVersion + + t.Run("Fetching snapshots bucket for RPM package", func(t *testing.T) { + fileName := "elastic-agent-" + testVersion + "-x86_64.rpm" + + bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, testVersion) + assert.Equal(t, bucket, "beats-ci-artifacts") + assert.Equal(t, prefix, "snapshots/elastic-agent") + assert.Equal(t, object, "elastic-agent-"+testVersion+"-x86_64.rpm") + }) + + t.Run("Fetching snapshots bucket for DEB package", func(t *testing.T) { + fileName := "elastic-agent-" + testVersion + "-amd64.deb" + + bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, testVersion) + assert.Equal(t, bucket, "beats-ci-artifacts") + assert.Equal(t, prefix, "snapshots/elastic-agent") + assert.Equal(t, object, "elastic-agent-"+testVersion+"-amd64.deb") + }) + + t.Run("Fetching snapshots bucket for TAR package adds OS to fileName and object", func(t *testing.T) { + fileName := "elastic-agent-" + testVersion + "-linux-x86_64.tar.gz" + + bucket, prefix, object := getGCPBucketCoordinates(fileName, artifact, version, testVersion) + assert.Equal(t, bucket, "beats-ci-artifacts") + assert.Equal(t, prefix, "snapshots/elastic-agent") + assert.Equal(t, object, "elastic-agent-"+testVersion+"-linux-x86_64.tar.gz") + }) +} + func TestProcessBucketSearchPage_PullRequestsFound(t *testing.T) { // retrieving last element in pull-requests.json object := "pr-22495/filebeat/filebeat-8.0.0-SNAPSHOT-linux-amd64.docker.tar.gz"