Files
netbird/client/internal/updater/manager_mode_test.go
T
Zoltan PappandClaude Opus 5 a8a67f5cf1 [client] Keep the updater silent until management decides the mode (#7455)
* [client] Keep the updater silent until management decides the mode

The update manager started in download-only mode and every engine stop
reset it there again, kicking the update loop with the cached latest
version. On a managed peer this published a "New version available"
notification on each disconnect, session expiry or logout, and once more
in the window between daemon start and the first network map. Users under
enforced updates then installed the release by hand.

Replace the boolean with a three-state mode. The manager starts undecided
and publishes nothing, from the fetcher or from NotifyUI, until the network
map picks download-only or managed. A new connection lifecycle resets to
undecided; disconnects leave the last decision untouched. A missing
AutoUpdateSettings now means download-only instead of a no-op, and on
platforms without an installer SetVersion falls back to download-only so
they keep notifying.

* [client] Address review findings on the updater mode split

Route every mode transition through one locked helper so the staged
managed version cannot outlive the decision that created it. Both
download-only transitions kept pendingVersion, letting Install put on
the old enforced release after management disabled updates or the
platform fell back to download-only.

The helper also bumps a generation counter. handleUpdate and NotifyUI
copy the mode under the mutex but publish and install after releasing
it, so a reset landing in that window could still emit an enforced
notification. Both now recheck the generation right before the side
effect and drop the stale work.

Reset on every connection attempt instead of once per run: the backoff
loop reconnects without re-entering run, and since disconnects no longer
force download-only, a fetch in the disconnected gap republished the
enforced notification before the new network map arrived.

A missing AutoUpdateSettings now logs that it defaults to download-only
rather than claiming auto-update is disabled, which contradicted the
notifications that mode still emits.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* [client] Reset the updater mode on engine stop

Removing SetDownloadOnly from the engine teardown also dropped the reset of
the forced-update flag. After netbird down the manager kept the last
management directive, so a release published while the client was stopped
could still trigger a forced install that management no longer asked for.
The same window existed between an engine teardown and the next reconnect
attempt, where ResetMode only ran at the start of the attempt.

Reset the mode in stopLocked instead, so the updater stays silent from the
moment the engine goes down until the first network map of the next
connection decides the mode again.

* [client] Name the force-reset test for the behavior it proves

The silence after ResetMode follows from the undecided mode alone, and the
closing SetVersion overwrites forceUpdate from its argument, so the test
cannot observe whether the flag itself was cleared. Name it for the stale
directive staying silent, which is what it asserts.

* [client] Fall back to download-only on a malformed expected version

* [client] Compare the full auto-update directive before skipping it

* [client] Set the mock latest version under its lock in the updater tests

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-10-08 12:46:15 +02:00

218 lines
7.1 KiB
Go

package updater
import (
"context"
"path"
"testing"
"time"
v "github.com/hashicorp/go-version"
"github.com/netbirdio/netbird/client/internal/peer"
"github.com/netbirdio/netbird/client/internal/statemanager"
)
func Test_UndecidedMode_SuppressesNotification(t *testing.T) {
tmpFile := path.Join(t.TempDir(), "update-test-undecided.json")
recorder := peer.NewRecorder("")
sub := recorder.SubscribeToEvents()
defer recorder.UnsubscribeFromEvents(sub)
mockUpdate := &versionUpdateMock{latestVersion: v.Must(v.NewSemver("1.0.1"))}
m := NewManager(recorder, statemanager.New(tmpFile))
m.update = mockUpdate
m.currentVersion = "1.0.0"
m.Start(context.Background())
defer m.Stop()
mockUpdate.onUpdate()
if ver, _ := waitForUpdateEvent(sub, 300*time.Millisecond); ver != "" {
t.Fatalf("undecided mode must not publish, got %q", ver)
}
m.NotifyUI()
if ver, _ := waitForUpdateEvent(sub, 300*time.Millisecond); ver != "" {
t.Fatalf("NotifyUI in undecided mode must not publish, got %q", ver)
}
m.SetDownloadOnly()
ver, enforced := waitForUpdateEvent(sub, 500*time.Millisecond)
if ver != "1.0.1" {
t.Fatalf("expected download-only event for 1.0.1, got %q", ver)
}
if enforced {
t.Error("download-only event must not carry enforced metadata")
}
}
func Test_ResetMode_ReturnsToUndecided(t *testing.T) {
tmpFile := path.Join(t.TempDir(), "update-test-reset.json")
recorder := peer.NewRecorder("")
sub := recorder.SubscribeToEvents()
defer recorder.UnsubscribeFromEvents(sub)
mockUpdate := &versionUpdateMock{latestVersion: v.Must(v.NewSemver("1.0.1"))}
m := NewManager(recorder, statemanager.New(tmpFile))
m.update = mockUpdate
m.currentVersion = "1.0.0"
m.autoUpdateSupported = func() bool { return true }
m.Start(context.Background())
defer m.Stop()
m.SetVersion("1.0.1", false)
ver, enforced := waitForUpdateEvent(sub, 500*time.Millisecond)
if ver != "1.0.1" || !enforced {
t.Fatalf("expected enforced event for 1.0.1, got %q enforced=%v", ver, enforced)
}
m.ResetMode()
mockUpdate.onUpdate()
if ver, _ := waitForUpdateEvent(sub, 300*time.Millisecond); ver != "" {
t.Fatalf("reset mode must not publish on fetch, got %q", ver)
}
m.NotifyUI()
if ver, _ := waitForUpdateEvent(sub, 300*time.Millisecond); ver != "" {
t.Fatalf("NotifyUI after reset must not publish, got %q", ver)
}
if err := m.Install(context.Background()); err == nil {
t.Fatal("Install after reset must fail without a pending version")
}
m.SetVersion("1.0.1", false)
ver, enforced = waitForUpdateEvent(sub, 500*time.Millisecond)
if ver != "1.0.1" || !enforced {
t.Fatalf("expected enforced event again after reset, got %q enforced=%v", ver, enforced)
}
}
func Test_SetDownloadOnly_ClearsPendingVersion(t *testing.T) {
tmpFile := path.Join(t.TempDir(), "update-test-pending.json")
recorder := peer.NewRecorder("")
sub := recorder.SubscribeToEvents()
defer recorder.UnsubscribeFromEvents(sub)
m := NewManager(recorder, statemanager.New(tmpFile))
m.update = &versionUpdateMock{latestVersion: v.Must(v.NewSemver("1.0.1"))}
m.currentVersion = "1.0.0"
m.autoUpdateSupported = func() bool { return true }
m.Start(context.Background())
defer m.Stop()
m.SetVersion("1.0.1", false)
if ver, enforced := waitForUpdateEvent(sub, 500*time.Millisecond); ver != "1.0.1" || !enforced {
t.Fatalf("expected enforced event for 1.0.1, got %q enforced=%v", ver, enforced)
}
m.SetDownloadOnly()
if ver, enforced := waitForUpdateEvent(sub, 500*time.Millisecond); ver != "1.0.1" || enforced {
t.Fatalf("expected download-only event for 1.0.1, got %q enforced=%v", ver, enforced)
}
if err := m.Install(context.Background()); err == nil {
t.Fatal("Install in download-only mode must not install the staged managed version")
}
}
func Test_ResetMode_SilencesStaleForceDirective(t *testing.T) {
tmpFile := path.Join(t.TempDir(), "update-test-force-reset.json")
recorder := peer.NewRecorder("")
sub := recorder.SubscribeToEvents()
defer recorder.UnsubscribeFromEvents(sub)
mockUpdate := &versionUpdateMock{}
m := NewManager(recorder, statemanager.New(tmpFile))
m.update = mockUpdate
m.currentVersion = "1.0.0"
m.autoUpdateSupported = func() bool { return true }
m.Start(context.Background())
defer m.Stop()
// Management enforces "latest" before the fetcher has reported any version,
// so nothing can be installed while the engine is still up.
m.SetVersion(latestVersion, true)
if event := waitForAnyEvent(sub, 300*time.Millisecond); event != nil {
t.Fatalf("no event expected before the latest version is known, got %v", event)
}
// The engine stop resets the mode. A release published afterwards must not
// trigger the stale forced install or any notification.
m.ResetMode()
mockUpdate.setLatestVersion(v.Must(v.NewSemver("1.0.1")))
mockUpdate.onUpdate()
if event := waitForAnyEvent(sub, 300*time.Millisecond); event != nil {
t.Fatalf("stale force directive must stay silent after reset, got %v", event)
}
m.SetVersion("1.0.1", false)
ver, enforced := waitForUpdateEvent(sub, 500*time.Millisecond)
if ver != "1.0.1" || !enforced {
t.Fatalf("expected enforced event after a fresh directive, got %q enforced=%v", ver, enforced)
}
}
func Test_SetVersion_MalformedFallsBackToDownloadOnly(t *testing.T) {
tmpFile := path.Join(t.TempDir(), "update-test-malformed.json")
recorder := peer.NewRecorder("")
sub := recorder.SubscribeToEvents()
defer recorder.UnsubscribeFromEvents(sub)
m := NewManager(recorder, statemanager.New(tmpFile))
m.update = &versionUpdateMock{latestVersion: v.Must(v.NewSemver("1.0.1"))}
m.currentVersion = "1.0.0"
m.autoUpdateSupported = func() bool { return true }
m.Start(context.Background())
defer m.Stop()
m.SetVersion("not-a-version", false)
ver, enforced := waitForUpdateEvent(sub, 500*time.Millisecond)
if ver != "1.0.1" {
t.Fatalf("expected download-only event for 1.0.1 after malformed version, got %q", ver)
}
if enforced {
t.Error("malformed version fallback must not carry enforced metadata")
}
}
func Test_SetVersion_ForceChangeAppliesWithSameVersion(t *testing.T) {
m := NewManager(peer.NewRecorder(""), statemanager.New(path.Join(t.TempDir(), "update-test-force-change.json")))
m.update = &versionUpdateMock{}
m.autoUpdateSupported = func() bool { return true }
m.SetVersion("1.0.1", false)
m.SetVersion("1.0.1", true)
m.updateMutex.Lock()
defer m.updateMutex.Unlock()
if !m.forceUpdate {
t.Fatal("enabling force update without a version change must take effect")
}
if m.expectedVersion == nil || m.expectedVersion.String() != "1.0.1" {
t.Fatalf("expected version 1.0.1 to stay set, got %v", m.expectedVersion)
}
}
func Test_SetVersion_RepeatedDirectiveKeepsMode(t *testing.T) {
m := NewManager(peer.NewRecorder(""), statemanager.New(path.Join(t.TempDir(), "update-test-repeat.json")))
m.update = &versionUpdateMock{}
m.autoUpdateSupported = func() bool { return true }
for _, expected := range []string{"1.0.1", latestVersion} {
m.SetVersion(expected, false)
m.updateMutex.Lock()
gen := m.modeGen
m.updateMutex.Unlock()
m.SetVersion(expected, false)
m.updateMutex.Lock()
repeatedGen := m.modeGen
m.updateMutex.Unlock()
if repeatedGen != gen {
t.Errorf("repeating the %q directive must not reset the mode", expected)
}
}
}