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

Commit d5689c7

Browse files
Update installer code to support deployer abstraction (#1163) (#1198)
* Installer refactor * More work abstracting out installer bits * Update internal/deploy/docker.go * Update installer to support attaching a deployment * cleanup execing into services * fix e2e tests * print logs of container, up timeout factor * revert jenkinsfile * remove dead code Signed-off-by: Adam Stokes <51892+adam-stokes@users.noreply.github.com> Co-authored-by: Manuel de la Peña <mdelapenya@gmail.com> (cherry picked from commit 584769a) Co-authored-by: Adam Stokes <51892+adam-stokes@users.noreply.github.com>
1 parent 68543cc commit d5689c7

17 files changed

Lines changed: 787 additions & 829 deletions

e2e/_suites/fleet/fleet.go

Lines changed: 114 additions & 240 deletions
Large diffs are not rendered by default.

e2e/_suites/fleet/stand-alone.go

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ func (fts *FleetTestSuite) thereIsNewDataInTheIndexFromAgent() error {
5151
maxTimeout := time.Duration(utils.TimeoutFactor) * time.Minute * 2
5252
minimumHitsCount := 50
5353

54-
result, err := searchAgentData(fts.Hostname, fts.RuntimeDependenciesStartDate, minimumHitsCount, maxTimeout)
54+
agentService := deploy.NewServiceRequest(common.ElasticAgentServiceName).WithFlavour(fts.Image)
55+
56+
manifest, _ := fts.deployer.Inspect(agentService)
57+
result, err := searchAgentData(manifest.Hostname, fts.RuntimeDependenciesStartDate, minimumHitsCount, maxTimeout)
5558
if err != nil {
5659
return err
5760
}
@@ -62,11 +65,8 @@ func (fts *FleetTestSuite) thereIsNewDataInTheIndexFromAgent() error {
6265
}
6366

6467
func (fts *FleetTestSuite) theDockerContainerIsStopped(serviceName string) error {
65-
services := []deploy.ServiceRequest{
66-
deploy.NewServiceRequest(common.FleetProfileName),
67-
deploy.NewServiceRequest(serviceName),
68-
}
69-
err := fts.deployer.Remove(services, common.ProfileEnv)
68+
agentService := deploy.NewServiceRequest(serviceName)
69+
err := fts.deployer.Stop(agentService)
7070
if err != nil {
7171
return err
7272
}
@@ -79,7 +79,9 @@ func (fts *FleetTestSuite) thereIsNoNewDataInTheIndexAfterAgentShutsDown() error
7979
maxTimeout := time.Duration(30) * time.Second
8080
minimumHitsCount := 1
8181

82-
result, err := searchAgentData(fts.Hostname, fts.AgentStoppedDate, minimumHitsCount, maxTimeout)
82+
agentService := deploy.NewServiceRequest(common.ElasticAgentServiceName)
83+
manifest, _ := fts.deployer.Inspect(agentService)
84+
result, err := searchAgentData(manifest.Hostname, fts.AgentStoppedDate, minimumHitsCount, maxTimeout)
8385
if err != nil {
8486
if strings.Contains(err.Error(), "type:index_not_found_exception") {
8587
return err
@@ -140,14 +142,7 @@ func (fts *FleetTestSuite) startStandAloneAgent(image string, flavour string, en
140142
return err
141143
}
142144

143-
// get container hostname once
144-
hostname, err := deploy.GetContainerHostname(containerName)
145-
if err != nil {
146-
return err
147-
}
148-
149145
fts.Image = image
150-
fts.Hostname = hostname
151146

152147
err = fts.installTestTools(containerName)
153148
if err != nil {
@@ -208,7 +203,7 @@ func (fts *FleetTestSuite) installTestTools(containerName string) error {
208203
"containerName": containerName,
209204
}).Trace("Installing test tools ")
210205

211-
_, err := deploy.ExecCommandIntoContainer(context.Background(), deploy.NewServiceRequest(containerName), "root", cmd)
206+
_, err := deploy.ExecCommandIntoContainer(context.Background(), containerName, "root", cmd)
212207
if err != nil {
213208
log.WithFields(log.Fields{
214209
"command": cmd,

e2e/_suites/fleet/world.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ type IngestManagerTestSuite struct {
2323
}
2424

2525
func (imts *IngestManagerTestSuite) processStateOnTheHost(process string, state string) error {
26-
return imts.thereAreInstancesOfTheProcessInTheState("1", process, state)
26+
ocurrences := "1"
27+
if state == "uninstalled" || state == "stopped" {
28+
ocurrences = "0"
29+
}
30+
return imts.thereAreInstancesOfTheProcessInTheState(ocurrences, process, state)
2731
}
2832

2933
func (imts *IngestManagerTestSuite) thereAreInstancesOfTheProcessInTheState(ocurrences string, process string, state string) error {
@@ -34,8 +38,9 @@ func (imts *IngestManagerTestSuite) thereAreInstancesOfTheProcessInTheState(ocur
3438
if imts.Fleet.StandAlone {
3539
containerName = fmt.Sprintf("%s_%s_%d", profile, common.ElasticAgentServiceName, 1)
3640
} else {
37-
agentInstaller := imts.Fleet.getInstaller()
38-
containerName = imts.Fleet.getContainerName(agentInstaller)
41+
agentService := deploy.NewServiceRequest(common.ElasticAgentServiceName)
42+
manifest, _ := imts.Fleet.deployer.Inspect(agentService)
43+
containerName = manifest.Name
3944
}
4045

4146
count, err := strconv.Atoi(ocurrences)
@@ -100,6 +105,22 @@ func waitForProcess(deployer deploy.Deployment, service string, process string,
100105
cmds := []string{"pgrep", "-d", ",", process}
101106
output, err := deployer.ExecIn(serviceRequest, cmds)
102107
if err != nil {
108+
109+
if !mustBePresent && ocurrences == 0 {
110+
log.WithFields(log.Fields{
111+
"cmds": cmds,
112+
"desiredState": desiredState,
113+
"elapsedTime": exp.GetElapsedTime(),
114+
"error": err,
115+
"service": service,
116+
"mustBePresent": mustBePresent,
117+
"ocurrences": ocurrences,
118+
"process": process,
119+
"retry": retryCount,
120+
}).Warn("Process is not present and number of occurences is 0")
121+
return nil
122+
}
123+
103124
log.WithFields(log.Fields{
104125
"cmds": cmds,
105126
"desiredState": desiredState,

internal/deploy/base.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,47 @@ import (
1212
// required for testing
1313
type Deployment interface {
1414
Add(services []ServiceRequest, env map[string]string) error // adds a service to deployment
15+
AddFiles(service ServiceRequest, files []string) error // adds files to a service
1516
Bootstrap(waitCB func() error) error // will bootstrap or reuse existing cluster if kubernetes is selected
1617
Destroy() error // Teardown deployment
1718
ExecIn(service ServiceRequest, cmd []string) (string, error) // Execute arbitrary commands in service
1819
Inspect(service ServiceRequest) (*ServiceManifest, error) // inspects service
20+
Logs(service ServiceRequest) error // prints logs of deployed service
1921
Remove(services []ServiceRequest, env map[string]string) error // Removes services from deployment
22+
Start(service ServiceRequest) error // Starts a service or container depending on Deployment
23+
Stop(service ServiceRequest) error // Stop a service or container depending on deployment
24+
}
25+
26+
// ServiceOperator represents the operations that can be performed by a service
27+
type ServiceOperator interface {
28+
AddFiles(files []string) error // adds files to service environment
29+
Enroll(token string) error // handle any enrollment/registering of service
30+
Exec(args []string) (string, error) // exec arbitrary commands in service environment
31+
Inspect() (ServiceOperatorManifest, error) // returns manifest for package
32+
Install() error
33+
InstallCerts() error
34+
Logs() error
35+
Postinstall() error
36+
Preinstall() error
37+
Start() error // will start a service
38+
Stop() error // will stop a service
39+
Uninstall() error
40+
}
41+
42+
// ServiceOperatorManifest is state information for each service operator
43+
type ServiceOperatorManifest struct {
44+
CommitFile string
45+
WorkDir string
2046
}
2147

2248
// ServiceManifest information about a service in a deployment
2349
type ServiceManifest struct {
2450
ID string
2551
Name string
2652
Connection string // a string representing how to connect to service
53+
Alias string // container network aliases
2754
Hostname string
55+
Platform string // running in linux, macos, windows
2856
}
2957

3058
// ServiceRequest represents the service to be created using the provider

internal/deploy/docker.go

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ package deploy
66

77
import (
88
"context"
9+
"path/filepath"
910
"strings"
1011

1112
"github.com/elastic/e2e-testing/internal/common"
13+
"github.com/elastic/e2e-testing/internal/shell"
1214
"github.com/elastic/e2e-testing/internal/utils"
1315
log "github.com/sirupsen/logrus"
1416
)
@@ -58,6 +60,23 @@ func (c *dockerDeploymentManifest) Bootstrap(waitCB func() error) error {
5860
return nil
5961
}
6062

63+
// AddFiles - add files to service
64+
func (c *dockerDeploymentManifest) AddFiles(service ServiceRequest, files []string) error {
65+
container, _ := c.Inspect(service)
66+
for _, file := range files {
67+
isTar := true
68+
fileExt := filepath.Ext(file)
69+
if fileExt == ".rpm" || fileExt == ".deb" {
70+
isTar = false
71+
}
72+
err := CopyFileToContainer(c.Context, container.Name, file, "/", isTar)
73+
if err != nil {
74+
log.WithField("error", err).Fatal("Unable to copy file to service")
75+
}
76+
}
77+
return nil
78+
}
79+
6180
// Destroy teardown docker environment
6281
func (c *dockerDeploymentManifest) Destroy() error {
6382
serviceManager := NewServiceManager()
@@ -73,7 +92,12 @@ func (c *dockerDeploymentManifest) Destroy() error {
7392

7493
// ExecIn execute command in service
7594
func (c *dockerDeploymentManifest) ExecIn(service ServiceRequest, cmd []string) (string, error) {
76-
output, err := ExecCommandIntoContainer(c.Context, service, "root", cmd)
95+
inspect, _ := c.Inspect(service)
96+
args := []string{"exec", "-u", "root", "-i", inspect.Name}
97+
for _, cmdArg := range cmd {
98+
args = append(args, cmdArg)
99+
}
100+
output, err := shell.Execute(c.Context, ".", "docker", args...)
77101
if err != nil {
78102
return "", err
79103
}
@@ -86,17 +110,54 @@ func (c *dockerDeploymentManifest) Inspect(service ServiceRequest) (*ServiceMani
86110
if err != nil {
87111
return &ServiceManifest{}, err
88112
}
113+
89114
return &ServiceManifest{
90115
ID: inspect.ID,
91116
Name: strings.TrimPrefix(inspect.Name, "/"),
92117
Connection: service.Name,
93-
Hostname: inspect.NetworkSettings.Networks["fleet_default"].Aliases[0],
118+
Alias: inspect.NetworkSettings.Networks["fleet_default"].Aliases[0],
119+
Hostname: inspect.Config.Hostname,
120+
Platform: inspect.Platform,
94121
}, nil
95122
}
96123

124+
// Logs print logs of service
125+
func (c *dockerDeploymentManifest) Logs(service ServiceRequest) error {
126+
manifest, _ := c.Inspect(service)
127+
_, err := shell.Execute(c.Context, ".", "docker", "logs", manifest.Name)
128+
if err != nil {
129+
log.WithFields(log.Fields{
130+
"error": err,
131+
"service": service.Name,
132+
}).Error("Could not retrieve Elastic Agent logs")
133+
134+
return err
135+
}
136+
return nil
137+
}
138+
97139
// Remove remove services from deployment
98140
func (c *dockerDeploymentManifest) Remove(services []ServiceRequest, env map[string]string) error {
99-
serviceManager := NewServiceManager()
141+
for _, service := range services[1:] {
142+
manifest, _ := c.Inspect(service)
143+
_, err := shell.Execute(c.Context, ".", "docker", "rm", "-fv", manifest.Name)
144+
if err != nil {
145+
return err
146+
}
147+
}
148+
return nil
149+
}
150+
151+
// Start a container
152+
func (c *dockerDeploymentManifest) Start(service ServiceRequest) error {
153+
manifest, _ := c.Inspect(service)
154+
_, err := shell.Execute(c.Context, ".", "docker", "start", manifest.Name)
155+
return err
156+
}
100157

101-
return serviceManager.RemoveServicesFromCompose(c.Context, services[0], services[1:], env)
158+
// Stop a container
159+
func (c *dockerDeploymentManifest) Stop(service ServiceRequest) error {
160+
manifest, _ := c.Inspect(service)
161+
_, err := shell.Execute(c.Context, ".", "docker", "stop", manifest.Name)
162+
return err
102163
}

internal/deploy/docker_client.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,18 @@ func CopyFileToContainer(ctx context.Context, containerName string, srcPath stri
135135
}
136136

137137
// ExecCommandIntoContainer executes a command, as a user, into a container
138-
func ExecCommandIntoContainer(ctx context.Context, container ServiceRequest, user string, cmd []string) (string, error) {
138+
func ExecCommandIntoContainer(ctx context.Context, container string, user string, cmd []string) (string, error) {
139139
return ExecCommandIntoContainerWithEnv(ctx, container, user, cmd, []string{})
140140
}
141141

142142
// ExecCommandIntoContainerWithEnv executes a command, as a user, with env, into a container
143-
func ExecCommandIntoContainerWithEnv(ctx context.Context, container ServiceRequest, user string, cmd []string, env []string) (string, error) {
143+
func ExecCommandIntoContainerWithEnv(ctx context.Context, container string, user string, cmd []string, env []string) (string, error) {
144144
dockerClient := getDockerClient()
145145

146146
detach := false
147147
tty := false
148148

149-
containerName := container.Name
149+
containerName := container
150150

151151
log.WithFields(log.Fields{
152152
"container": containerName,
@@ -192,6 +192,8 @@ func ExecCommandIntoContainerWithEnv(ctx context.Context, container ServiceReque
192192
Detach: detach,
193193
Tty: tty,
194194
})
195+
defer resp.Close()
196+
195197
if err != nil {
196198
log.WithFields(log.Fields{
197199
"container": containerName,
@@ -203,7 +205,6 @@ func ExecCommandIntoContainerWithEnv(ctx context.Context, container ServiceReque
203205
}).Error("Could not execute command in container")
204206
return "", err
205207
}
206-
defer resp.Close()
207208

208209
// see https://stackoverflow.com/a/57132902
209210
var execRes execResult
@@ -268,7 +269,7 @@ func GetContainerHostname(containerName string) (string, error) {
268269
"containerName": containerName,
269270
}).Trace("Retrieving container name from the Docker client")
270271

271-
hostname, err := ExecCommandIntoContainer(context.Background(), NewServiceRequest(containerName), "root", []string{"cat", "/etc/hostname"})
272+
hostname, err := ExecCommandIntoContainer(context.Background(), containerName, "root", []string{"cat", "/etc/hostname"})
272273
if err != nil {
273274
log.WithFields(log.Fields{
274275
"containerName": containerName,

internal/deploy/docker_client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func Test_CopyFile(t *testing.T) {
4040
err = CopyFileToContainer(ctx, containerName, src, target, false)
4141
assert.Nil(t, err)
4242

43-
output, err := ExecCommandIntoContainer(ctx, NewServiceRequest(containerName), "root", []string{"cat", "/tmp/dockerCopy.txt"})
43+
output, err := ExecCommandIntoContainer(ctx, containerName, "root", []string{"cat", "/tmp/dockerCopy.txt"})
4444
assert.Nil(t, err)
4545
assert.True(t, strings.HasSuffix(output, "OK!"), "File contains the 'OK!' string")
4646
})
@@ -76,7 +76,7 @@ func Test_CopyFile(t *testing.T) {
7676
err = CopyFileToContainer(ctx, containerName, src, target, true)
7777
assert.Nil(t, err)
7878

79-
output, err := ExecCommandIntoContainer(ctx, NewServiceRequest(containerName), "root", []string{"ls", "/project/txtr/kermit.jpg"})
79+
output, err := ExecCommandIntoContainer(ctx, containerName, "root", []string{"ls", "/project/txtr/kermit.jpg"})
8080
assert.Nil(t, err)
8181
assert.True(t, strings.Contains(output, "/project/txtr/kermit.jpg"), "File '/project/txtr/kermit.jpg' should be present")
8282
})

0 commit comments

Comments
 (0)