From 6eb9df87cfb1d13d83ec6928647fab55714f9b08 Mon Sep 17 00:00:00 2001 From: bbedward Date: Tue, 6 Oct 2026 15:02:12 -0400 Subject: [PATCH] network: set routing preference only from settings fixes #3068 stable-1.6: core part only, the settings and network popout changes are master-only (cherry picked from commit 089dc98166e1a0636a097b1bd446dac63b968c3f) --- core/internal/server/network/priority.go | 23 +++++++++++-------- core/internal/server/network/priority_test.go | 12 ++++++---- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/core/internal/server/network/priority.go b/core/internal/server/network/priority.go index 612131f61..b53dc96a2 100644 --- a/core/internal/server/network/priority.go +++ b/core/internal/server/network/priority.go @@ -16,7 +16,8 @@ const ( metricPreferred = int64(100) metricNonPreferred = int64(300) - metricDefault = int64(100) + // -1 lets NetworkManager pick the metric by device type (nm-settings ipv4.route-metric) + metricDefault = int64(-1) ) func (m *Manager) SetConnectionPreference(pref ConnectionPreference) error { @@ -46,7 +47,7 @@ func (m *Manager) SetConnectionPreference(pref ConnectionPreference) error { case PreferenceCellular: return m.prioritizeCellular() case PreferenceAuto: - return m.balancePriorities() + return m.restoreDefaultPriorities() } return nil @@ -112,7 +113,7 @@ func (m *Manager) prioritizeCellular() error { return nil } -func (m *Manager) balancePriorities() error { +func (m *Manager) restoreDefaultPriorities() error { if err := m.setConnectionPriority("802-3-ethernet", priorityDefault, metricDefault); err != nil { log.Warnf("Failed to reset Ethernet priority: %v", err) } @@ -242,16 +243,20 @@ func (m *Manager) setConnectionPriority(connType string, autoconnectPriority int } func priorityMatches(variant dbus.Variant, expected int64) bool { - value, ok := variantInt64(variant) - return ok && value == expected + return settingInt64(variant, int64(priorityDefault)) == expected } func routeMetricMatches(section map[string]dbus.Variant, expected int64) bool { - if section == nil { - return false + return settingInt64(section["route-metric"], metricDefault) == expected +} + +// GetSettings omits properties at their default, so a missing key is the default value +func settingInt64(variant dbus.Variant, def int64) int64 { + value, ok := variantInt64(variant) + if !ok { + return def } - value, ok := variantInt64(section["route-metric"]) - return ok && value == expected + return value } func variantInt64(variant dbus.Variant) (int64, bool) { diff --git a/core/internal/server/network/priority_test.go b/core/internal/server/network/priority_test.go index 111da19f6..4a9d4626b 100644 --- a/core/internal/server/network/priority_test.go +++ b/core/internal/server/network/priority_test.go @@ -3,6 +3,7 @@ package network import ( "testing" + "github.com/godbus/dbus/v5" "github.com/stretchr/testify/assert" ) @@ -45,7 +46,10 @@ func TestManager_GetConnectionPreference(t *testing.T) { } } -// Note: Full testing of priority operations would require mocking NetworkManager -// D-Bus interfaces. The tests above cover the basic logic and error handling. -// Integration tests would be needed for complete coverage of network connection -// priority updates and reactivation. +func TestProfileAtDefaultsMatchesAutoTargets(t *testing.T) { + assert.True(t, priorityMatches(dbus.Variant{}, int64(priorityDefault))) + assert.True(t, routeMetricMatches(nil, metricDefault)) + assert.True(t, routeMetricMatches(map[string]dbus.Variant{"method": dbus.MakeVariant("auto")}, metricDefault)) + assert.False(t, routeMetricMatches(map[string]dbus.Variant{"route-metric": dbus.MakeVariant(int64(100))}, metricDefault)) + assert.False(t, priorityMatches(dbus.MakeVariant(int32(100)), int64(priorityDefault))) +}