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 089dc98166)
This commit is contained in:
bbedward committed 2026-10-07 13:03:41 -04:00
1 parent 0adbdfdde1
commit 6eb9df87cf
2 files changed
+22 -13

No files matched your search

+14 -9
View File
@@ -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) {
@@ -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)))
}