Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

chore: refactor build artifacts methods (#689) backport for 6.8.x #712

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions cli/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ package docker

import (
"bytes"
"compress/gzip"
"context"
"os"
"path/filepath"
"strings"

"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -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()
Expand Down
16 changes: 7 additions & 9 deletions e2e/_suites/fleet/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,9 @@ func (fts *FleetTestSuite) anStaleAgentIsDeployedToFleetWithInstaller(image, ver
agentVersion = version

// prepare installer for stale version
if agentVersion != agentVersionBackup {
i := GetElasticAgentInstaller(image, installerType)
installerType = fmt.Sprintf("%s-%s", installerType, version)
fts.Installers[fmt.Sprintf("%s-%s", image, installerType)] = i
if fts.Version != agentVersionBackup {
i := GetElasticAgentInstaller(image, installerType, fts.Version)
fts.Installers[fmt.Sprintf("%s-%s-%s", image, installerType, version)] = i
}

return fts.anAgentIsDeployedToFleetWithInstaller(image, installerType)
Expand Down Expand Up @@ -371,11 +370,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" {
Expand Down
26 changes: 6 additions & 20 deletions e2e/_suites/fleet/ingest-manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"os"
"path"
"strings"
"time"

"github.com/cucumber/godog"
Expand Down Expand Up @@ -91,10 +90,12 @@ func setUpSuite() {
imts = IngestManagerTestSuite{
Fleet: &FleetTestSuite{
Installers: map[string]ElasticAgentInstaller{
"centos-systemd": GetElasticAgentInstaller("centos", "systemd"),
"centos-tar": GetElasticAgentInstaller("centos", "tar"),
"debian-systemd": GetElasticAgentInstaller("debian", "systemd"),
"debian-tar": GetElasticAgentInstaller("debian", "tar"),
"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{},
Expand Down Expand Up @@ -226,21 +227,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
Expand Down
114 changes: 101 additions & 13 deletions e2e/_suites/fleet/installers.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -214,20 +298,24 @@ func (i *TARPackage) Preinstall() error {
return err
}

version := checkElasticAgentVersion(i.version)

// simplify layout
cmds := []string{"mv", fmt.Sprintf("/%s-%s-%s-%s", i.artifact, version, i.OS, i.arch), "/elastic-agent"}
err = execCommandInService(i.profile, i.image, i.service, cmds, false)
if err != nil {
log.WithFields(log.Fields{
"command": cmds,
"error": err,
"image": i.image,
"service": i.service,
}).Error("Could not extract agent package in the box")

return err
cmds := [][]string{
[]string{"rm", "-fr", "/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)
if err != nil {
log.WithFields(log.Fields{
"command": cmd,
"error": err,
"image": i.image,
"service": i.service,
"version": i.version,
}).Error("Could not extract agent package in the box")

return err
}
}

return nil
Expand Down
Loading