Fix failover to disabled route #1706 (#1707)

* fix #1706 - failover should disregard disabled routes during failover

* fixe tests for failover; all current tests assume routes to be enabled

* add testcase for #1706 - failover to disabled route
This commit is contained in:
derelm 2024-02-03 15:30:15 +01:00 committed by GitHub
parent b4210e2c90
commit 4ea12f472a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 62 additions and 0 deletions

View File

@ -585,6 +585,10 @@ func (hsdb *HSDatabase) failoverRoute(r *types.Route) ([]key.MachinePublic, erro
continue continue
} }
if !route.Enabled {
continue
}
if hsdb.notifier.IsConnected(route.Node.MachineKey) { if hsdb.notifier.IsConnected(route.Node.MachineKey) {
newPrimary = &routes[idx] newPrimary = &routes[idx]
break break

View File

@ -371,6 +371,7 @@ func TestFailoverRoute(t *testing.T) {
MachineKey: machineKeys[0], MachineKey: machineKeys[0],
}, },
IsPrimary: true, IsPrimary: true,
Enabled: true,
}, },
routes: types.Routes{ routes: types.Routes{
types.Route{ types.Route{
@ -382,6 +383,7 @@ func TestFailoverRoute(t *testing.T) {
MachineKey: machineKeys[0], MachineKey: machineKeys[0],
}, },
IsPrimary: true, IsPrimary: true,
Enabled: true,
}, },
types.Route{ types.Route{
Model: gorm.Model{ Model: gorm.Model{
@ -392,6 +394,7 @@ func TestFailoverRoute(t *testing.T) {
MachineKey: machineKeys[1], MachineKey: machineKeys[1],
}, },
IsPrimary: false, IsPrimary: false,
Enabled: true,
}, },
}, },
want: []key.MachinePublic{ want: []key.MachinePublic{
@ -411,6 +414,7 @@ func TestFailoverRoute(t *testing.T) {
MachineKey: machineKeys[0], MachineKey: machineKeys[0],
}, },
IsPrimary: false, IsPrimary: false,
Enabled: true,
}, },
routes: types.Routes{ routes: types.Routes{
types.Route{ types.Route{
@ -422,6 +426,7 @@ func TestFailoverRoute(t *testing.T) {
MachineKey: machineKeys[0], MachineKey: machineKeys[0],
}, },
IsPrimary: true, IsPrimary: true,
Enabled: true,
}, },
types.Route{ types.Route{
Model: gorm.Model{ Model: gorm.Model{
@ -432,6 +437,7 @@ func TestFailoverRoute(t *testing.T) {
MachineKey: machineKeys[1], MachineKey: machineKeys[1],
}, },
IsPrimary: false, IsPrimary: false,
Enabled: true,
}, },
}, },
want: nil, want: nil,
@ -448,6 +454,7 @@ func TestFailoverRoute(t *testing.T) {
MachineKey: machineKeys[1], MachineKey: machineKeys[1],
}, },
IsPrimary: true, IsPrimary: true,
Enabled: true,
}, },
routes: types.Routes{ routes: types.Routes{
types.Route{ types.Route{
@ -459,6 +466,7 @@ func TestFailoverRoute(t *testing.T) {
MachineKey: machineKeys[0], MachineKey: machineKeys[0],
}, },
IsPrimary: false, IsPrimary: false,
Enabled: true,
}, },
types.Route{ types.Route{
Model: gorm.Model{ Model: gorm.Model{
@ -469,6 +477,7 @@ func TestFailoverRoute(t *testing.T) {
MachineKey: machineKeys[1], MachineKey: machineKeys[1],
}, },
IsPrimary: true, IsPrimary: true,
Enabled: true,
}, },
types.Route{ types.Route{
Model: gorm.Model{ Model: gorm.Model{
@ -479,6 +488,7 @@ func TestFailoverRoute(t *testing.T) {
MachineKey: machineKeys[2], MachineKey: machineKeys[2],
}, },
IsPrimary: false, IsPrimary: false,
Enabled: true,
}, },
}, },
want: []key.MachinePublic{ want: []key.MachinePublic{
@ -498,6 +508,7 @@ func TestFailoverRoute(t *testing.T) {
MachineKey: machineKeys[0], MachineKey: machineKeys[0],
}, },
IsPrimary: true, IsPrimary: true,
Enabled: true,
}, },
routes: types.Routes{ routes: types.Routes{
types.Route{ types.Route{
@ -509,6 +520,7 @@ func TestFailoverRoute(t *testing.T) {
MachineKey: machineKeys[0], MachineKey: machineKeys[0],
}, },
IsPrimary: true, IsPrimary: true,
Enabled: true,
}, },
// Offline // Offline
types.Route{ types.Route{
@ -520,6 +532,7 @@ func TestFailoverRoute(t *testing.T) {
MachineKey: machineKeys[3], MachineKey: machineKeys[3],
}, },
IsPrimary: false, IsPrimary: false,
Enabled: true,
}, },
}, },
want: nil, want: nil,
@ -536,6 +549,7 @@ func TestFailoverRoute(t *testing.T) {
MachineKey: machineKeys[0], MachineKey: machineKeys[0],
}, },
IsPrimary: true, IsPrimary: true,
Enabled: true,
}, },
routes: types.Routes{ routes: types.Routes{
types.Route{ types.Route{
@ -547,6 +561,7 @@ func TestFailoverRoute(t *testing.T) {
MachineKey: machineKeys[0], MachineKey: machineKeys[0],
}, },
IsPrimary: true, IsPrimary: true,
Enabled: true,
}, },
// Offline // Offline
types.Route{ types.Route{
@ -558,6 +573,7 @@ func TestFailoverRoute(t *testing.T) {
MachineKey: machineKeys[3], MachineKey: machineKeys[3],
}, },
IsPrimary: false, IsPrimary: false,
Enabled: true,
}, },
types.Route{ types.Route{
Model: gorm.Model{ Model: gorm.Model{
@ -568,6 +584,7 @@ func TestFailoverRoute(t *testing.T) {
MachineKey: machineKeys[1], MachineKey: machineKeys[1],
}, },
IsPrimary: true, IsPrimary: true,
Enabled: true,
}, },
}, },
want: []key.MachinePublic{ want: []key.MachinePublic{
@ -576,6 +593,47 @@ func TestFailoverRoute(t *testing.T) {
}, },
wantErr: false, wantErr: false,
}, },
{
name: "failover-primary-none-enabled",
failingRoute: types.Route{
Model: gorm.Model{
ID: 1,
},
Prefix: ipp("10.0.0.0/24"),
Node: types.Node{
MachineKey: machineKeys[0],
},
IsPrimary: true,
Enabled: true,
},
routes: types.Routes{
types.Route{
Model: gorm.Model{
ID: 1,
},
Prefix: ipp("10.0.0.0/24"),
Node: types.Node{
MachineKey: machineKeys[0],
},
IsPrimary: true,
Enabled: true,
},
// not enabled
types.Route{
Model: gorm.Model{
ID: 2,
},
Prefix: ipp("10.0.0.0/24"),
Node: types.Node{
MachineKey: machineKeys[1],
},
IsPrimary: false,
Enabled: false,
},
},
want: nil,
wantErr: false,
},
} }
for _, tt := range tests { for _, tt := range tests {