Skip to content

Commit fe9cd16

Browse files
committed
WIP - Add Upgrader unit tests
1 parent 8118da9 commit fe9cd16

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

internal/pkg/agent/application/upgrade/upgrade.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ func init() {
7171
}
7272
}
7373

74+
// TODO substitute all the references to watcher with calls to the interface
75+
type WatcherHelper interface {
76+
InvokeWatcher(log *logger.Logger, agentExecutable string) (*exec.Cmd, error)
77+
SelectWatcherExecutable(topDir string, agentInstalls ...agentInstall) string
78+
WaitForWatcher(ctx context.Context, log *logger.Logger, markerFilePath string, waitTime time.Duration) error
79+
TakeOverWatcher(ctx context.Context, topDir string) (*filelock.AppLocker, error)
80+
}
81+
7482
// Upgrader performs an upgrade
7583
type Upgrader struct {
7684
log *logger.Logger
@@ -204,7 +212,7 @@ func checkUpgrade(log *logger.Logger, currentVersion, newVersion agentVersion, m
204212
func (u *Upgrader) Upgrade(ctx context.Context, version string, rollback bool, sourceURI string, action *fleetapi.ActionUpgrade, det *details.Details, skipVerifyOverride bool, skipDefaultPgp bool, pgpBytes ...string) (_ reexec.ShutdownCallbackFn, err error) {
205213

206214
if rollback {
207-
return u.forceRollbackToPreviousVersion(ctx, version, action, det)
215+
return u.forceRollbackToPreviousVersion(ctx, paths.Top(), version, action, det)
208216
}
209217

210218
u.log.Infow("Upgrading agent", "version", version, "source_uri", sourceURI)
@@ -388,19 +396,19 @@ func (u *Upgrader) Upgrade(ctx context.Context, version string, rollback bool, s
388396
return cb, nil
389397
}
390398

391-
func (u *Upgrader) forceRollbackToPreviousVersion(ctx context.Context, version string, action *fleetapi.ActionUpgrade, d *details.Details) (reexec.ShutdownCallbackFn, error) {
399+
func (u *Upgrader) forceRollbackToPreviousVersion(ctx context.Context, topDir string, version string, action *fleetapi.ActionUpgrade, d *details.Details) (reexec.ShutdownCallbackFn, error) {
392400
// Formal checks for verifying we can rollback properly:
393401
// 1. d.Metadata.RollbacksAvailable should contain the desired version with a valid TTL (it may need to be written by main agent process before starting watcher)
394402
// 2. there has been at least the first restart with the new agent (i.e. we are not still downloading/extracting/rotating)
395403
// 3. upgrade marker exists
396404
// these should be revalidated after taking over watcher
397-
err := u.PersistManualRollback(ctx)
405+
err := u.PersistManualRollback(ctx, topDir)
398406
if err != nil {
399407
return nil, err
400408
}
401409

402410
// Invoke watcher again
403-
_, err = InvokeWatcher(u.log, paths.BinaryPath(paths.VersionedHome(paths.Top()), agentName))
411+
_, err = InvokeWatcher(u.log, paths.BinaryPath(paths.VersionedHome(topDir), agentName))
404412
if err != nil {
405413
return nil, fmt.Errorf("invoking watcher: %w", err)
406414
}
@@ -409,8 +417,8 @@ func (u *Upgrader) forceRollbackToPreviousVersion(ctx context.Context, version s
409417

410418
}
411419

412-
func (u *Upgrader) PersistManualRollback(ctx context.Context) error {
413-
watcherApplock, err := u.takeOverWatcher(ctx)
420+
func (u *Upgrader) PersistManualRollback(ctx context.Context, topDir string) error {
421+
watcherApplock, err := u.takeOverWatcher(ctx, topDir)
414422
if err != nil {
415423
return fmt.Errorf("taking over watcher processes: %w", err)
416424
}
@@ -422,7 +430,7 @@ func (u *Upgrader) PersistManualRollback(ctx context.Context) error {
422430
}(watcherApplock)
423431

424432
// read the upgrade marker
425-
updateMarker, err := LoadMarker(paths.Data())
433+
updateMarker, err := LoadMarker(paths.DataFrom(topDir))
426434
if err != nil {
427435
return fmt.Errorf("loading marker: %w", err)
428436
}
@@ -435,7 +443,7 @@ func (u *Upgrader) PersistManualRollback(ctx context.Context) error {
435443
return nil
436444
}
437445

438-
func (u *Upgrader) takeOverWatcher(ctx context.Context) (*filelock.AppLocker, error) {
446+
func (u *Upgrader) takeOverWatcher(ctx context.Context, topDir string) (*filelock.AppLocker, error) {
439447

440448
takeoverCtx, takeoverCancel := context.WithTimeout(ctx, 30*time.Second)
441449
defer takeoverCancel()
@@ -479,7 +487,7 @@ func (u *Upgrader) takeOverWatcher(ctx context.Context) (*filelock.AppLocker, er
479487
case <-takeoverCtx.Done():
480488
return nil, fmt.Errorf("timed out taking over watcher applocker")
481489
case <-takeOverTicker.C:
482-
locker := filelock.NewAppLocker(paths.Top(), "watcher.lock")
490+
locker := filelock.NewAppLocker(topDir, "watcher.lock")
483491
err := locker.TryLock()
484492
if err != nil {
485493
u.log.Errorf("error locking watcher applocker: %s", err)

internal/pkg/agent/application/upgrade/upgrade_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"
2727
"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade/artifact"
2828
"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade/details"
29+
"github.com/elastic/elastic-agent/internal/pkg/agent/configuration"
2930
"github.com/elastic/elastic-agent/internal/pkg/agent/errors"
3031
"github.com/elastic/elastic-agent/internal/pkg/config"
3132
"github.com/elastic/elastic-agent/internal/pkg/fleetapi"
@@ -36,6 +37,7 @@ import (
3637
"github.com/elastic/elastic-agent/pkg/core/logger"
3738
"github.com/elastic/elastic-agent/pkg/core/logger/loggertest"
3839
agtversion "github.com/elastic/elastic-agent/pkg/version"
40+
infomocks "github.com/elastic/elastic-agent/testing/mocks/internal_/pkg/agent/application/info"
3941
ackermocks "github.com/elastic/elastic-agent/testing/mocks/internal_/pkg/fleetapi/acker"
4042
clientmocks "github.com/elastic/elastic-agent/testing/mocks/pkg/control/v2/client"
4143
)
@@ -316,7 +318,7 @@ func TestUpgraderAckAction(t *testing.T) {
316318
errAck := errors.New("ack error")
317319
mockAcker.EXPECT().Ack(mock.Anything, action).Return(errAck)
318320
// no expectation on Commit() since it shouldn't be called after an error during Ack()
319-
321+
320322
require.ErrorIs(t, u.AckAction(t.Context(), mockAcker, action), errAck)
321323
})
322324
}
@@ -1270,3 +1272,12 @@ func TestIsSameReleaseVersion(t *testing.T) {
12701272
})
12711273
}
12721274
}
1275+
1276+
func TestManualRollback(t *testing.T) {
1277+
log, _ := loggertest.New(t.Name())
1278+
mockAgentInfo := infomocks.NewAgent(t)
1279+
upgrader, err := NewUpgrader(log, &artifact.Config{}, &configuration.UpgradeConfig{}, mockAgentInfo)
1280+
require.NoError(t, err, "error creating upgrader")
1281+
1282+
upgrader.Upgrade(t.Context(), "", true, "", nil, nil, true, false)
1283+
}

0 commit comments

Comments
 (0)