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

Introduce tests for elastic-agent upgrade procedure #506

Merged
merged 27 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
22986b0
upgrade
michalpristas Dec 2, 2020
ab90f06
conflicts
michalpristas Dec 2, 2020
28b7b36
may the force
michalpristas Dec 2, 2020
276810c
working yay
michalpristas Dec 2, 2020
56595fd
jenkins var
michalpristas Dec 2, 2020
08bbe75
revert json logs
michalpristas Dec 2, 2020
7695b23
uninstall revert
michalpristas Dec 2, 2020
a82cec1
looogs
michalpristas Dec 2, 2020
0a3ade8
added wait
michalpristas Dec 2, 2020
ce0c704
then and
michalpristas Dec 2, 2020
df9e936
no online check
michalpristas Dec 2, 2020
ce50ecd
Merge branch 'master' of github.com:elastic/e2e-testing into upgrade-…
michalpristas Dec 2, 2020
9d38c55
updated
michalpristas Dec 2, 2020
7fed11a
Merge branch 'master' into upgrade-test
mdelapenya Dec 2, 2020
6002c19
rephrased
michalpristas Dec 3, 2020
c2156f2
Merge branch 'master' of github.com:elastic/e2e-testing into upgrade-…
michalpristas Dec 3, 2020
2e4f6f8
Merge branch 'upgrade-test' of github.com:michalpristas/e2e-testing i…
michalpristas Dec 3, 2020
c5b4dd6
tar installer
michalpristas Dec 3, 2020
539c88f
install certs
michalpristas Dec 3, 2020
9c1749e
be declarative
michalpristas Dec 3, 2020
e5aaf00
conflicts
michalpristas Dec 4, 2020
9de13af
conflicts
michalpristas Dec 4, 2020
8492472
Merge branch 'master' of github.com:elastic/e2e-testing into upgrade-…
michalpristas Dec 8, 2020
07a2219
make it nice
michalpristas Dec 8, 2020
1df2679
reverse order
michalpristas Dec 8, 2020
44b1e62
reverse order
michalpristas Dec 8, 2020
6c69884
removed wait
michalpristas Dec 8, 2020
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
10 changes: 10 additions & 0 deletions e2e/_suites/fleet/features/fleet_mode_agent.feature
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ Examples:
| centos | tar |
| debian | tar |

@upgrade-agent
Scenario Outline: Upgrading the installed <os> agent
EricDavisX marked this conversation as resolved.
Show resolved Hide resolved
Given a "<os>" agent "stale" is deployed to Fleet with "<installer>" installer
When agent is upgraded to version "latest"
Then agent is in version "stack"
And the agent is listed in Fleet as "online"
Examples:
| os | installer |
| debian | tar |

@restart-agent
Scenario Outline: Restarting the installed <os> agent
Given a "<os>" agent is deployed to Fleet with "<installer>" installer
Expand Down
107 changes: 107 additions & 0 deletions e2e/_suites/fleet/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import (
curl "github.com/elastic/e2e-testing/cli/shell"
"github.com/elastic/e2e-testing/e2e"
"github.com/google/uuid"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

const fleetAgentsURL = kibanaBaseURL + "/api/fleet/agents"
const fleetAgentEventsURL = kibanaBaseURL + "/api/fleet/agents/%s/events"
const fleetAgentsUnEnrollURL = kibanaBaseURL + "/api/fleet/agents/%s/unenroll"
const fleetAgentUpgradeURL = kibanaBaseURL + "/api/fleet/agents/%s/upgrade"
const fleetEnrollmentTokenURL = kibanaBaseURL + "/api/fleet/enrollment-api-keys"
const fleetSetupURL = kibanaBaseURL + "/api/fleet/agents/setup"
const ingestManagerAgentPoliciesURL = kibanaBaseURL + "/api/fleet/agent_policies"
Expand Down Expand Up @@ -117,6 +119,9 @@ func (fts *FleetTestSuite) beforeScenario() {

func (fts *FleetTestSuite) contributeSteps(s *godog.Suite) {
s.Step(`^a "([^"]*)" agent is deployed to Fleet with "([^"]*)" installer$`, fts.anAgentIsDeployedToFleetWithInstaller)
s.Step(`^a "([^"]*)" 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.anAgentIsUpgraded)
s.Step(`^the agent is listed in Fleet as "([^"]*)"$`, fts.theAgentIsListedInFleetWithStatus)
s.Step(`^the host is restarted$`, fts.theHostIsRestarted)
s.Step(`^system package dashboards are listed in Fleet$`, fts.systemPackageDashboardsAreListedInFleet)
Expand All @@ -138,6 +143,84 @@ func (fts *FleetTestSuite) contributeSteps(s *godog.Suite) {
s.Step(`^the policy will reflect the change in the Security App$`, fts.thePolicyWillReflectTheChangeInTheSecurityApp)
}

func (fts *FleetTestSuite) anStaleAgentIsDeployedToFleetWithInstaller(image, version, installerType string) error {
agentVersionBackup := agentVersion
defer func() { agentVersion = agentVersionBackup }()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I liked this post-condition style!


switch version {
case "stale":
version = agentStaleVersion
case "latest":
version = agentVersion
default:
version = agentStaleVersion
}

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
}

return fts.anAgentIsDeployedToFleetWithInstaller(image, installerType)
}

func (fts *FleetTestSuite) anAgentIsUpgraded(desiredVersion string) error {
switch desiredVersion {
case "stale":
desiredVersion = agentStaleVersion
case "latest":
desiredVersion = agentVersion
default:
desiredVersion = agentVersion
}

return fts.upgradeAgent(desiredVersion)
}

func (fts *FleetTestSuite) agentInVersion(version string) error {
switch version {
case "stale":
version = agentStaleVersion
case "latest":
version = agentVersion
case "stack":
version = stackVersion
}

agentID, err := getAgentID(fts.Hostname)
if err != nil {
return err
}

r := createDefaultHTTPRequest(fleetAgentsURL + "/" + agentID)
body, err := curl.Get(r)
if err != nil {
log.WithFields(log.Fields{
"body": body,
"error": err,
"url": r.GetURL(),
}).Error("Could not get agent in Fleet")
return err
}

jsonResponse, err := gabs.ParseJSON([]byte(body))

retrievedVersion := jsonResponse.Path("item.local_metadata.elastic.agent.version").Data().(string)
if isSnapshot := jsonResponse.Path("item.local_metadata.elastic.agent.version").Data().(string); isSnapshot == "true" {
retrievedVersion += "-SNAPSHOT"
}

if retrievedVersion != version {
return fmt.Errorf("version mismatch required '%s' desired '%s'", version, retrievedVersion)
}

return nil
}

// supported installers: tar, systemd
func (fts *FleetTestSuite) anAgentIsDeployedToFleetWithInstaller(image string, installerType string) error {
log.WithFields(log.Fields{
Expand Down Expand Up @@ -951,6 +1034,30 @@ func (fts *FleetTestSuite) unenrollHostname(force bool) error {
return nil
}

func (fts *FleetTestSuite) upgradeAgent(version string) error {
agentID, err := getAgentID(fts.Hostname)
if err != nil {
return err
}

upgradeReq := curl.HTTPRequest{
BasicAuthUser: "elastic",
BasicAuthPassword: "changeme",
Headers: map[string]string{
"Content-Type": "application/json",
"kbn-xsrf": "true",
},
URL: fmt.Sprintf(fleetAgentUpgradeURL, agentID),
Payload: `{"version":"` + version + `", "force": true}`,
}

if content, err := curl.Post(upgradeReq); err != nil {
return errors.Wrap(err, content)
}

return nil
}

// checkFleetConfiguration checks that Fleet configuration is not missing
// any requirements and is read. To achieve it, a GET request is executed
func checkFleetConfiguration() error {
Expand Down
5 changes: 5 additions & 0 deletions e2e/_suites/fleet/ingest-manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ var agentVersionBase = "8.0.0-SNAPSHOT"
// It can be overriden by ELASTIC_AGENT_VERSION env var
var agentVersion = agentVersionBase

// agentStaleVersion is the version of the agent to use as a base during upgrade
// It can be overriden by ELASTIC_AGENT_STALE_VERSION env var. Using latest GA as a default.
var agentStaleVersion = "7.10.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add it to the Jenkinsfile as an input parameter? This way we will allow manual tests on CI simply setting that input parameter in Jenkins UI

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding it to Jenkins as an optional param is even better... where it uses the value if passed... but if not, we can do some parse and math logic to get the current-minus-1 version (from the stack deployed or other e2e-testing variable) and use that instead. It is more future proof and will require less maintenance, right?

It is worth some discussion, open to what ever you want to merge, and if not tackled now, we can just drop a ticket in for the reminder of the work.


// stackVersion is the version of the stack to use
// It can be overriden by STACK_VERSION env var
var stackVersion = agentVersionBase
Expand Down Expand Up @@ -72,6 +76,7 @@ func init() {

timeoutFactor = shell.GetEnvInteger("TIMEOUT_FACTOR", timeoutFactor)
agentVersion = shell.GetEnv("ELASTIC_AGENT_VERSION", agentVersionBase)
agentStaleVersion = shell.GetEnv("ELASTIC_AGENT_STALE_VERSION", agentStaleVersion)
stackVersion = shell.GetEnv("STACK_VERSION", stackVersion)
}

Expand Down
5 changes: 3 additions & 2 deletions e2e/_suites/fleet/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ func newTarInstaller(image string, tag string) (ElasticAgentInstaller, error) {
return nil
}
unInstallFn := func() error {
return nil
args := []string{"-f"}

return runElasticAgentCommand(profile, image, service, ElasticAgentProcessName, "uninstall", args)
Expand All @@ -497,8 +498,8 @@ func newTarInstaller(image string, tag string) (ElasticAgentInstaller, error) {
image: image,
InstallFn: installFn,
installerType: "tar",
logFile: "elastic-agent.log",
logsDir: "/opt/Elastic/Agent/data/elastic-agent-%s/",
logFile: "elastic-agent-json.log",
logsDir: "/opt/Elastic/Agent/data/elastic-agent-%s/logs/",
EricDavisX marked this conversation as resolved.
Show resolved Hide resolved
name: tarFile,
path: binaryPath,
PostInstallFn: postInstallFn,
Expand Down