Skip to content

Commit e662c54

Browse files
mergify[bot]pchila
andauthored
[9.2] (backport #11324) Restart elastic-agent matching version in TestStandaloneUpgradeRollbackOnRestarts (#11326)
* Restart elastic-agent matching version in TestStandaloneUpgradeRollbackOnRestarts (#11324) Replace the fixed amount of restarts with restarting based on elastic-agent version. This has been already successfully implemented in fixing TestFleetManagedUpgradeRollbackOnRestarts and it's now being extended to the standalone tests. This removes the +-1 restarts needed based on elastic-agent watcher timing, test timing, platform differences etc. * goimports --------- Co-authored-by: Paolo Chilà <[email protected]>
1 parent 65a6cd9 commit e662c54

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

testing/integration/ess/upgrade_rollback_test.go

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ agent.upgrade.watcher:
4444
const fastWatcherCfgWithRollbackWindow = `
4545
agent.upgrade:
4646
watcher:
47-
grace_period: 2m
47+
grace_period: 1m
4848
error_check.interval: 5s
4949
rollback:
5050
window: 10m
@@ -341,6 +341,8 @@ func TestFleetManagedUpgradeRollbackOnRestarts(t *testing.T) {
341341
}
342342
}
343343

344+
type rollbackTriggerFunc func(ctx context.Context, t *testing.T, client client.Client, startFixture, endFixture *atesting.Fixture)
345+
344346
// TestStandaloneUpgradeManualRollback tests the scenario where, after upgrading to a new version
345347
// of Agent, a manual rollback is triggered. It checks that the Agent is rolled back to the previous version.
346348
func TestStandaloneUpgradeManualRollback(t *testing.T) {
@@ -351,9 +353,12 @@ func TestStandaloneUpgradeManualRollback(t *testing.T) {
351353
})
352354

353355
type fixturesSetupFunc func(t *testing.T) (from *atesting.Fixture, to *atesting.Fixture)
356+
354357
testcases := []struct {
355-
name string
356-
fixturesSetup fixturesSetupFunc
358+
name string
359+
fixturesSetup fixturesSetupFunc
360+
agentConfig string
361+
rollbackTrigger rollbackTriggerFunc
357362
}{
358363
{
359364
name: "upgrade to a repackaged agent built from the same commit",
@@ -392,23 +397,26 @@ func TestStandaloneUpgradeManualRollback(t *testing.T) {
392397

393398
return fromFixture, toFixture
394399
},
400+
rollbackTrigger: func(ctx context.Context, t *testing.T, client client.Client, startFixture, endFixture *atesting.Fixture) {
401+
t.Logf("sending version=%s rollback=%v upgrade to agent", startFixture.Version(), true)
402+
retVal, err := client.Upgrade(ctx, startFixture.Version(), true, "", false, false)
403+
require.NoError(t, err, "error triggering manual rollback to version %s", startFixture.Version())
404+
t.Logf("received output %s from upgrade command", retVal)
405+
},
395406
},
396407
}
397408

398-
// set up start ficture with a rollback window of 1h
399-
rollbackWindowConfig := `
400-
agent.upgrade.rollback.window: 1h
401-
`
402-
403409
for _, tc := range testcases {
404410
t.Run(tc.name, func(t *testing.T) {
405411
ctx, cancel := testcontext.WithDeadline(t, t.Context(), time.Now().Add(10*time.Minute))
406412
defer cancel()
407413
from, to := tc.fixturesSetup(t)
408414

409-
err := from.Configure(ctx, []byte(rollbackWindowConfig))
410-
require.NoError(t, err, "error setting up rollback window")
411-
standaloneManualRollbackTest(ctx, t, from, to)
415+
err := from.Configure(ctx, []byte(tc.agentConfig))
416+
require.NoError(t, err, "error configuring starting fixture")
417+
standaloneRollbackTest(
418+
ctx, t, from, to, fastWatcherCfgWithRollbackWindow, fmt.Sprintf(details.ReasonManualRollbackPattern, from.Version()),
419+
tc.rollbackTrigger)
412420
})
413421
}
414422

@@ -497,23 +505,18 @@ func managedRollbackRestartTest(ctx context.Context, t *testing.T, info *define.
497505

498506
func standaloneRollbackRestartTest(ctx context.Context, t *testing.T, startFixture *atesting.Fixture, endFixture *atesting.Fixture) {
499507
standaloneRollbackTest(ctx, t, startFixture, endFixture, reallyFastWatcherCfg, details.ReasonWatchFailed,
500-
func(t *testing.T, _ client.Client) {
501-
restartAgentNTimes(t, 3, 10*time.Second)
508+
func(ctx context.Context, t *testing.T, _ client.Client, from *atesting.Fixture, to *atesting.Fixture) {
509+
installedAgentClient := from.NewClient()
510+
targetVersion, err := to.ExecVersion(ctx)
511+
require.NoError(t, err, "failed to get target version")
512+
restartContext, cancel := context.WithTimeout(t.Context(), 1*time.Minute)
513+
defer cancel()
514+
// restart the agent only if it matches the (upgraded) target version
515+
restartAgentVersion(restartContext, t, installedAgentClient, targetVersion.Binary, 10*time.Second)
502516
})
503517
}
504518

505-
func standaloneManualRollbackTest(ctx context.Context, t *testing.T, startFixture *atesting.Fixture, endFixture *atesting.Fixture) {
506-
standaloneRollbackTest(ctx, t, startFixture, endFixture, fastWatcherCfgWithRollbackWindow, fmt.Sprintf(details.ReasonManualRollbackPattern, startFixture.Version()),
507-
func(t *testing.T, client client.Client) {
508-
t.Logf("sending version=%s rollback=%v upgrade to agent", startFixture.Version(), true)
509-
retVal, err := client.Upgrade(ctx, startFixture.Version(), true, "", false, false)
510-
require.NoError(t, err, "error triggering manual rollback to version %s", startFixture.Version())
511-
t.Logf("received output %s from upgrade command", retVal)
512-
},
513-
)
514-
}
515-
516-
func standaloneRollbackTest(ctx context.Context, t *testing.T, startFixture *atesting.Fixture, endFixture *atesting.Fixture, customConfig string, rollbackReason string, rollbackTrigger func(t *testing.T, client client.Client)) {
519+
func standaloneRollbackTest(ctx context.Context, t *testing.T, startFixture *atesting.Fixture, endFixture *atesting.Fixture, customConfig string, rollbackReason string, rollbackTrigger rollbackTriggerFunc) {
517520

518521
startVersionInfo, err := startFixture.ExecVersion(ctx)
519522
require.NoError(t, err, "failed to get start agent build version info")
@@ -546,7 +549,7 @@ func standaloneRollbackTest(ctx context.Context, t *testing.T, startFixture *ate
546549
defer elasticAgentClient.Disconnect()
547550

548551
// A few seconds after the upgrade, trigger a rollback using the passed trigger
549-
rollbackTrigger(t, elasticAgentClient)
552+
rollbackTrigger(ctx, t, elasticAgentClient, startFixture, endFixture)
550553

551554
// wait for the agent to be healthy and back at the start version
552555
err = upgradetest.WaitHealthyAndVersion(ctx, startFixture, startVersionInfo.Binary, 2*time.Minute, 10*time.Second, t)

0 commit comments

Comments
 (0)