Skip to content

Commit

Permalink
Add test scenario for elastic agent tags (elastic#2552)
Browse files Browse the repository at this point in the history
* add flags

* gitignore

* gitignore

* typo

* quotes

* remove quotes

* remove restart

* Update e2e/_suites/fleet/fleet.go

Co-authored-by: Manuel de la Peña <social.mdelapenya@gmail.com>

Co-authored-by: Manuel de la Peña <social.mdelapenya@gmail.com>
  • Loading branch information
narph and mdelapenya authored Jun 3, 2022
1 parent 16c9400 commit 5e682cf
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .ci/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ Once you have created and set up the remote machines with the above instructions

> When submitting the pull request with your user to enable the SSH access, please remember to add the right backport labels (ex. `backport-v8.2.0`) so that you will be able to SSH into the CI machines for all supported maintenance branches.
To SSH into the machines, please use the following commads:
To SSH into the machines, please use the following commands:

```shell
export SSH_KEY="PATH_TO_YOUR_SSH_KEY_WITH_ACCESS_TO_AWS"
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ dist/
.venv
hosts
*-sshhosts
.idea
6 changes: 6 additions & 0 deletions e2e/_suites/fleet/features/fleet_mode.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ Scenario Outline: Deploying the agent
Then the agent is listed in Fleet as "online"
And system package dashboards are listed in Fleet

@install-including-tags
Scenario Outline: Deploying the agent including command line --tag for tags
When an agent is deployed to Fleet with "tar" installer and "--tag=production,linux" flags
Then the agent is listed in Fleet as "online"
And the elastic agent index contains the tags

# @enroll
# Scenario Outline: Deploying the agent with enroll and then run on rpm and deb
# Given an agent is deployed to Fleet
Expand Down
74 changes: 67 additions & 7 deletions e2e/_suites/fleet/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ type FleetTestSuite struct {
AgentStoppedDate time.Time
RuntimeDependenciesStartDate time.Time
// instrumentation
currentContext context.Context
DefaultAPIKey string
currentContext context.Context
DefaultAPIKey string
ElasticAgentFlags string
}

func (fts *FleetTestSuite) getDeployer() deploy.Deployment {
Expand Down Expand Up @@ -148,6 +149,7 @@ func (fts *FleetTestSuite) afterScenario() {
fts.Image = ""
fts.StandAlone = false
fts.BeatsProcess = ""
fts.ElasticAgentFlags = ""
}

// beforeScenario creates the state needed by a scenario
Expand Down Expand Up @@ -255,6 +257,7 @@ func (fts *FleetTestSuite) contributeSteps(s *godog.ScenarioContext) {
s.Step(`^a "([^"]*)" agent is deployed to Fleet$`, fts.anAgentIsDeployedToFleet)
s.Step(`^an agent is deployed to Fleet on top of "([^"]*)"$`, fts.anAgentIsDeployedToFleetOnTopOfBeat)
s.Step(`^an agent is deployed to Fleet with "([^"]*)" installer$`, fts.anAgentIsDeployedToFleetWithInstaller)
s.Step(`^an agent is deployed to Fleet with "([^"]*)" installer and "([^"]*)" flags$`, fts.anAgentIsDeployedToFleetWithInstallerAndTags)
s.Step(`^a "([^"]*)" stale agent is deployed to Fleet with "([^"]*)" installer$`, fts.anStaleAgentIsDeployedToFleetWithInstaller)
s.Step(`^agent is in "([^"]*)" version$`, fts.agentInVersion)
s.Step(`^agent is upgraded to "([^"]*)" version$`, fts.anAgentIsUpgradedToVersion)
Expand All @@ -272,6 +275,9 @@ func (fts *FleetTestSuite) contributeSteps(s *godog.ScenarioContext) {
s.Step(`^a Linux data stream exists with some data$`, fts.checkDataStream)
s.Step(`^the agent is enrolled into "([^"]*)" policy$`, fts.agentRunPolicy)

//flags steps
s.Step(`^the elastic agent index contains the tags$`, fts.tagsAreInTheElasticAgentIndex)

// endpoint steps
s.Step(`^the "([^"]*)" integration is "([^"]*)" in the policy$`, fts.theIntegrationIsOperatedInThePolicy)
s.Step(`^the "([^"]*)" datasource is shown in the policy as added$`, fts.thePolicyShowsTheDatasourceAdded)
Expand Down Expand Up @@ -537,6 +543,60 @@ func (fts *FleetTestSuite) anAgentIsDeployedToFleetWithInstaller(installerType s
return fts.anAgentIsDeployedToFleetWithInstallerAndFleetServer(installerType)
}

// supported installers: tar, rpm, deb
func (fts *FleetTestSuite) anAgentIsDeployedToFleetWithInstallerAndTags(installerType string, flags string) error {
fts.BeatsProcess = ""

// FIXME: We need to cleanup the steps to support different operating systems
// for now we will force the zip installer type when the agent is running on windows
if runtime.GOOS == "windows" && common.Provider == "remote" {
installerType = "zip"
}
fts.ElasticAgentFlags = flags
return fts.anAgentIsDeployedToFleetWithInstallerAndFleetServer(installerType)
}

func (fts *FleetTestSuite) tagsAreInTheElasticAgentIndex() error {
var tagsArray []string
//ex of flags "--tag production,linux" or "--tag=production,linux"
if fts.ElasticAgentFlags != "" {
tags := strings.TrimPrefix(fts.ElasticAgentFlags, "--tag")
tags = strings.TrimPrefix(tags, "=")
tags = strings.ReplaceAll(tags, " ", "")
tagsArray = strings.Split(tags, ",")
}
if len(tagsArray) == 0 {
return errors.Errorf("no tags were found, ElasticAgentFlags value %s", fts.ElasticAgentFlags)
}

var tagTerms []map[string]interface{}
for _, tag := range tagsArray {
tagTerms = append(tagTerms, map[string]interface{}{
"term": map[string]interface{}{
"tags": tag,
},
})
}

query := map[string]interface{}{
"query": map[string]interface{}{
"bool": map[string]interface{}{
"must": tagTerms,
},
},
}

indexName := ".fleet-agents"

_, err := elasticsearch.WaitForNumberOfHits(context.Background(), indexName, query, 1, 3*time.Minute)
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Warn(elasticsearch.WaitForIndices())
}
return err
}

func (fts *FleetTestSuite) anAgentIsDeployedToFleetWithInstallerAndFleetServer(installerType string) error {
log.WithFields(log.Fields{
"installer": installerType,
Expand Down Expand Up @@ -564,7 +624,7 @@ func (fts *FleetTestSuite) anAgentIsDeployedToFleetWithInstallerAndFleetServer(i
}

agentInstaller, _ := installer.Attach(fts.currentContext, fts.getDeployer(), agentService, installerType)
err = deployAgentToFleet(fts.currentContext, agentInstaller, fts.CurrentToken)
err = deployAgentToFleet(fts.currentContext, agentInstaller, fts.CurrentToken, fts.ElasticAgentFlags)
if err != nil {
return err
}
Expand Down Expand Up @@ -1063,7 +1123,7 @@ func (fts *FleetTestSuite) theAgentIsReenrolledOnTheHost() error {
agentService := deploy.NewServiceRequest(common.ElasticAgentServiceName)
agentInstaller, _ := installer.Attach(fts.currentContext, fts.getDeployer(), agentService, fts.InstallerType)

err := agentInstaller.Enroll(fts.currentContext, fts.CurrentToken)
err := agentInstaller.Enroll(fts.currentContext, fts.CurrentToken, fts.ElasticAgentFlags)
if err != nil {
return err
}
Expand Down Expand Up @@ -1415,7 +1475,7 @@ func (fts *FleetTestSuite) anAttemptToEnrollANewAgentFails() error {
}

agentInstaller, _ := installer.Attach(fts.currentContext, fts.getDeployer(), agentService, fts.InstallerType)
err = deployAgentToFleet(fts.currentContext, agentInstaller, fts.CurrentToken)
err = deployAgentToFleet(fts.currentContext, agentInstaller, fts.CurrentToken, fts.ElasticAgentFlags)

if err == nil {
err = fmt.Errorf("the agent was enrolled although the token was previously revoked")
Expand Down Expand Up @@ -1546,7 +1606,7 @@ func (fts *FleetTestSuite) checkDataStream() error {
return err
}

func deployAgentToFleet(ctx context.Context, agentInstaller deploy.ServiceOperator, token string) error {
func deployAgentToFleet(ctx context.Context, agentInstaller deploy.ServiceOperator, token string, flags string) error {
err := agentInstaller.Preinstall(ctx)
if err != nil {
return err
Expand All @@ -1557,7 +1617,7 @@ func deployAgentToFleet(ctx context.Context, agentInstaller deploy.ServiceOperat
return err
}

err = agentInstaller.Enroll(ctx, token)
err = agentInstaller.Enroll(ctx, token, flags)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions internal/deploy/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ type Deployment interface {

// ServiceOperator represents the operations that can be performed by a service
type ServiceOperator interface {
AddFiles(ctx context.Context, files []string) error // adds files to service environment
Enroll(ctx context.Context, token string) error // handle any enrollment/registering of service
Exec(ctx context.Context, args []string) (string, error) // exec arbitrary commands in service environment
Inspect() (ServiceOperatorManifest, error) // returns manifest for package
AddFiles(ctx context.Context, files []string) error // adds files to service environment
Enroll(ctx context.Context, token string, extraFlags string) error // handle any enrollment/registering of service
Exec(ctx context.Context, args []string) (string, error) // exec arbitrary commands in service environment
Inspect() (ServiceOperatorManifest, error) // returns manifest for package
Install(ctx context.Context) error
InstallCerts(ctx context.Context) error
Logs(ctx context.Context) error
Expand Down
5 changes: 4 additions & 1 deletion internal/installer/elasticagent_deb.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (i *elasticAgentDEBPackage) Exec(ctx context.Context, args []string) (strin
}

// Enroll will enroll the agent into fleet
func (i *elasticAgentDEBPackage) Enroll(ctx context.Context, token string) error {
func (i *elasticAgentDEBPackage) Enroll(ctx context.Context, token string, extraFlags string) error {
cmds := []string{"elastic-agent", "enroll"}
span, _ := apm.StartSpanOptions(ctx, "Enrolling Elastic Agent with token", "elastic-agent.debian.enroll", apm.SpanOptions{
Parent: apm.SpanFromContext(ctx).TraceContext(),
Expand All @@ -90,6 +90,9 @@ func (i *elasticAgentDEBPackage) Enroll(ctx context.Context, token string) error

cfg, _ := kibana.NewFleetConfig(token)
cmds = append(cmds, cfg.Flags()...)
if extraFlags != "" {
cmds = append(cmds, extraFlags)
}

output, err := i.Exec(ctx, cmds)
log.Trace(output)
Expand Down
2 changes: 1 addition & 1 deletion internal/installer/elasticagent_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (i *elasticAgentDockerPackage) Exec(ctx context.Context, args []string) (st
}

// Enroll will enroll the agent into fleet
func (i *elasticAgentDockerPackage) Enroll(ctx context.Context, token string) error {
func (i *elasticAgentDockerPackage) Enroll(ctx context.Context, token string, extraFlags string) error {
return nil
}

Expand Down
5 changes: 4 additions & 1 deletion internal/installer/elasticagent_rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (i *elasticAgentRPMPackage) Exec(ctx context.Context, args []string) (strin
}

// Enroll will enroll the agent into fleet
func (i *elasticAgentRPMPackage) Enroll(ctx context.Context, token string) error {
func (i *elasticAgentRPMPackage) Enroll(ctx context.Context, token string, extraFlags string) error {
cmds := []string{"elastic-agent", "enroll"}
span, _ := apm.StartSpanOptions(ctx, "Enrolling Elastic Agent with token", "elastic-agent.rpm.enroll", apm.SpanOptions{
Parent: apm.SpanFromContext(ctx).TraceContext(),
Expand All @@ -95,6 +95,9 @@ func (i *elasticAgentRPMPackage) Enroll(ctx context.Context, token string) error

cfg, _ := kibana.NewFleetConfig(token)
cmds = append(cmds, cfg.Flags()...)
if extraFlags != "" {
cmds = append(cmds, extraFlags)
}

output, err := i.Exec(ctx, cmds)
log.Trace(output)
Expand Down
5 changes: 4 additions & 1 deletion internal/installer/elasticagent_tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (i *elasticAgentTARPackage) Exec(ctx context.Context, args []string) (strin
}

// Enroll will enroll the agent into fleet
func (i *elasticAgentTARPackage) Enroll(ctx context.Context, token string) error {
func (i *elasticAgentTARPackage) Enroll(ctx context.Context, token string, extraFlags string) error {
cmds := []string{"./elastic-agent/elastic-agent", "install"}
span, _ := apm.StartSpanOptions(ctx, "Enrolling Elastic Agent with token", "elastic-agent.tar.enroll", apm.SpanOptions{
Parent: apm.SpanFromContext(ctx).TraceContext(),
Expand All @@ -97,6 +97,9 @@ func (i *elasticAgentTARPackage) Enroll(ctx context.Context, token string) error

cfg, _ := kibana.NewFleetConfig(token)
cmds = append(cmds, cfg.Flags()...)
if extraFlags != "" {
cmds = append(cmds, extraFlags)
}

_, err := i.Exec(ctx, cmds)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion internal/installer/elasticagent_tar_macos.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (i *elasticAgentTARDarwinPackage) Exec(ctx context.Context, args []string)
}

// Enroll will enroll the agent into fleet
func (i *elasticAgentTARDarwinPackage) Enroll(ctx context.Context, token string) error {
func (i *elasticAgentTARDarwinPackage) Enroll(ctx context.Context, token string, extraFlags string) error {
cmds := []string{"/elastic-agent/elastic-agent", "install"}
span, _ := apm.StartSpanOptions(ctx, "Enrolling Elastic Agent with token", "elastic-agent.tar.enroll", apm.SpanOptions{
Parent: apm.SpanFromContext(ctx).TraceContext(),
Expand All @@ -91,6 +91,9 @@ func (i *elasticAgentTARDarwinPackage) Enroll(ctx context.Context, token string)

cfg, _ := kibana.NewFleetConfig(token)
cmds = append(cmds, cfg.Flags()...)
if extraFlags != "" {
cmds = append(cmds, extraFlags)
}

_, err := i.Exec(ctx, cmds)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion internal/installer/elasticagent_zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (i *elasticAgentZIPPackage) Exec(ctx context.Context, args []string) (strin
}

// Enroll will enroll the agent into fleet
func (i *elasticAgentZIPPackage) Enroll(ctx context.Context, token string) error {
func (i *elasticAgentZIPPackage) Enroll(ctx context.Context, token string, extraFlags string) error {
cmds := []string{"C:\\elastic-agent\\elastic-agent.exe", "install"}
span, _ := apm.StartSpanOptions(ctx, "Enrolling Elastic Agent with token", "elastic-agent.zip.enroll", apm.SpanOptions{
Parent: apm.SpanFromContext(ctx).TraceContext(),
Expand All @@ -83,6 +83,9 @@ func (i *elasticAgentZIPPackage) Enroll(ctx context.Context, token string) error

cfg, _ := kibana.NewFleetConfig(token)
cmds = append(cmds, cfg.Flags()...)
if extraFlags != "" {
cmds = append(cmds, extraFlags)
}

_, err := i.Exec(ctx, cmds)
if err != nil {
Expand Down

0 comments on commit 5e682cf

Please sign in to comment.