2023-05-21 16:37:59 +00:00
|
|
|
package db
|
2021-05-12 00:55:36 +00:00
|
|
|
|
|
|
|
import (
|
2022-09-01 22:06:19 +00:00
|
|
|
"net/netip"
|
2023-12-09 17:09:24 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
2022-11-24 22:41:11 +00:00
|
|
|
"time"
|
2022-09-01 22:06:19 +00:00
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2024-04-10 13:35:09 +00:00
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2023-05-21 16:37:59 +00:00
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
2023-05-11 07:09:18 +00:00
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
2024-04-21 16:28:17 +00:00
|
|
|
"github.com/puzpuzpuz/xsync/v3"
|
2021-05-12 00:55:36 +00:00
|
|
|
"gopkg.in/check.v1"
|
2023-12-09 17:09:24 +00:00
|
|
|
"gorm.io/gorm"
|
2021-05-12 00:55:36 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2024-07-18 08:01:59 +00:00
|
|
|
"tailscale.com/types/ptr"
|
2021-05-12 00:55:36 +00:00
|
|
|
)
|
|
|
|
|
2024-04-21 16:28:17 +00:00
|
|
|
var smap = func(m map[types.NodeID]bool) *xsync.MapOf[types.NodeID, bool] {
|
|
|
|
s := xsync.NewMapOf[types.NodeID, bool]()
|
|
|
|
|
|
|
|
for k, v := range m {
|
|
|
|
s.Store(k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2024-10-02 07:06:09 +00:00
|
|
|
var mp = func(p string) netip.Prefix {
|
|
|
|
return netip.MustParsePrefix(p)
|
|
|
|
}
|
|
|
|
|
2021-05-12 00:55:36 +00:00
|
|
|
func (s *Suite) TestGetRoutes(c *check.C) {
|
2023-05-21 16:37:59 +00:00
|
|
|
user, err := db.CreateUser("test")
|
2021-05-12 00:55:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2021-05-12 00:55:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
_, err = db.getNode("test", "test_get_route_node")
|
2021-05-12 00:55:36 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2022-09-01 22:06:19 +00:00
|
|
|
route, err := netip.ParsePrefix("10.0.0.0/24")
|
2021-05-12 00:55:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
hostInfo := tailcfg.Hostinfo{
|
2022-09-01 22:06:19 +00:00
|
|
|
RoutableIPs: []netip.Prefix{route},
|
2021-05-12 00:55:36 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
node := types.Node{
|
2021-05-12 00:55:36 +00:00
|
|
|
ID: 0,
|
2023-09-24 11:42:05 +00:00
|
|
|
Hostname: "test_get_route_node",
|
2023-01-17 19:36:46 +00:00
|
|
|
UserID: user.ID,
|
2023-05-21 16:37:59 +00:00
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2024-07-18 08:01:59 +00:00
|
|
|
AuthKeyID: ptr.To(pak.ID),
|
2023-11-21 17:20:06 +00:00
|
|
|
Hostinfo: &hostInfo,
|
2021-05-12 00:55:36 +00:00
|
|
|
}
|
2024-05-16 00:40:14 +00:00
|
|
|
trx := db.DB.Save(&node)
|
|
|
|
c.Assert(trx.Error, check.IsNil)
|
2021-05-12 00:55:36 +00:00
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
su, err := db.SaveNodeRoutes(&node)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-12-09 17:09:24 +00:00
|
|
|
c.Assert(su, check.Equals, false)
|
2022-11-24 16:00:40 +00:00
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
advertisedRoutes, err := db.GetAdvertisedRoutes(&node)
|
2021-05-12 00:55:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(len(advertisedRoutes), check.Equals, 1)
|
2021-05-12 00:55:36 +00:00
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
// TODO(kradalby): check state update
|
2024-10-02 07:06:09 +00:00
|
|
|
_, err = db.enableRoutes(&node, mp("192.168.0.0/24"))
|
2021-05-12 00:55:36 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2024-10-02 07:06:09 +00:00
|
|
|
_, err = db.enableRoutes(&node, mp("10.0.0.0/24"))
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestGetEnableRoutes(c *check.C) {
|
2023-05-21 16:37:59 +00:00
|
|
|
user, err := db.CreateUser("test")
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2021-05-12 00:55:36 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
_, err = db.getNode("test", "test_enable_route_node")
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2022-09-01 22:06:19 +00:00
|
|
|
route, err := netip.ParsePrefix(
|
2021-08-21 13:49:46 +00:00
|
|
|
"10.0.0.0/24",
|
|
|
|
)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-09-01 22:06:19 +00:00
|
|
|
route2, err := netip.ParsePrefix(
|
2021-08-21 13:49:46 +00:00
|
|
|
"150.0.10.0/25",
|
|
|
|
)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
hostInfo := tailcfg.Hostinfo{
|
2022-09-01 22:06:19 +00:00
|
|
|
RoutableIPs: []netip.Prefix{route, route2},
|
2021-08-21 13:49:46 +00:00
|
|
|
}
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
node := types.Node{
|
2021-08-21 13:49:46 +00:00
|
|
|
ID: 0,
|
2023-09-24 11:42:05 +00:00
|
|
|
Hostname: "test_enable_route_node",
|
2023-01-17 19:36:46 +00:00
|
|
|
UserID: user.ID,
|
2023-05-21 16:37:59 +00:00
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2024-07-18 08:01:59 +00:00
|
|
|
AuthKeyID: ptr.To(pak.ID),
|
2023-11-21 17:20:06 +00:00
|
|
|
Hostinfo: &hostInfo,
|
2021-08-21 13:49:46 +00:00
|
|
|
}
|
2024-05-16 00:40:14 +00:00
|
|
|
trx := db.DB.Save(&node)
|
|
|
|
c.Assert(trx.Error, check.IsNil)
|
2021-08-21 13:49:46 +00:00
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
sendUpdate, err := db.SaveNodeRoutes(&node)
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-12-09 17:09:24 +00:00
|
|
|
c.Assert(sendUpdate, check.Equals, false)
|
2021-08-21 13:49:46 +00:00
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
availableRoutes, err := db.GetAdvertisedRoutes(&node)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(availableRoutes), check.Equals, 2)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
noEnabledRoutes, err := db.GetEnabledRoutes(&node)
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 16:16:04 +00:00
|
|
|
c.Assert(len(noEnabledRoutes), check.Equals, 0)
|
2021-08-21 13:49:46 +00:00
|
|
|
|
2024-10-02 07:06:09 +00:00
|
|
|
_, err = db.enableRoutes(&node, mp("192.168.0.0/24"))
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2024-10-02 07:06:09 +00:00
|
|
|
_, err = db.enableRoutes(&node, mp("10.0.0.0/24"))
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
enabledRoutes, err := db.GetEnabledRoutes(&node)
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 16:16:04 +00:00
|
|
|
c.Assert(len(enabledRoutes), check.Equals, 1)
|
2021-08-21 13:49:46 +00:00
|
|
|
|
|
|
|
// Adding it twice will just let it pass through
|
2024-10-02 07:06:09 +00:00
|
|
|
_, err = db.enableRoutes(&node, mp("10.0.0.0/24"))
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
enableRoutesAfterDoubleApply, err := db.GetEnabledRoutes(&node)
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 16:16:04 +00:00
|
|
|
c.Assert(len(enableRoutesAfterDoubleApply), check.Equals, 1)
|
2021-08-21 13:49:46 +00:00
|
|
|
|
2024-10-02 07:06:09 +00:00
|
|
|
_, err = db.enableRoutes(&node, mp("150.0.10.0/25"))
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
enabledRoutesWithAdditionalRoute, err := db.GetEnabledRoutes(&node)
|
2021-08-21 13:49:46 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 16:16:04 +00:00
|
|
|
c.Assert(len(enabledRoutesWithAdditionalRoute), check.Equals, 2)
|
2021-05-12 00:55:36 +00:00
|
|
|
}
|
2022-11-24 16:00:40 +00:00
|
|
|
|
|
|
|
func (s *Suite) TestIsUniquePrefix(c *check.C) {
|
2023-05-21 16:37:59 +00:00
|
|
|
user, err := db.CreateUser("test")
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
_, err = db.getNode("test", "test_enable_route_node")
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
|
|
|
route, err := netip.ParsePrefix(
|
|
|
|
"10.0.0.0/24",
|
|
|
|
)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
route2, err := netip.ParsePrefix(
|
|
|
|
"150.0.10.0/25",
|
|
|
|
)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
hostInfo1 := tailcfg.Hostinfo{
|
|
|
|
RoutableIPs: []netip.Prefix{route, route2},
|
|
|
|
}
|
2023-09-24 11:42:05 +00:00
|
|
|
node1 := types.Node{
|
2022-11-24 22:41:11 +00:00
|
|
|
ID: 1,
|
2023-09-24 11:42:05 +00:00
|
|
|
Hostname: "test_enable_route_node",
|
2023-01-17 19:36:46 +00:00
|
|
|
UserID: user.ID,
|
2023-05-21 16:37:59 +00:00
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2024-07-18 08:01:59 +00:00
|
|
|
AuthKeyID: ptr.To(pak.ID),
|
2023-11-21 17:20:06 +00:00
|
|
|
Hostinfo: &hostInfo1,
|
2022-11-24 16:00:40 +00:00
|
|
|
}
|
2024-05-16 00:40:14 +00:00
|
|
|
trx := db.DB.Save(&node1)
|
|
|
|
c.Assert(trx.Error, check.IsNil)
|
2022-11-24 16:00:40 +00:00
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
sendUpdate, err := db.SaveNodeRoutes(&node1)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-12-09 17:09:24 +00:00
|
|
|
c.Assert(sendUpdate, check.Equals, false)
|
2022-11-24 16:00:40 +00:00
|
|
|
|
2024-10-02 07:06:09 +00:00
|
|
|
_, err = db.enableRoutes(&node1, route)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2024-10-02 07:06:09 +00:00
|
|
|
_, err = db.enableRoutes(&node1, route2)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
hostInfo2 := tailcfg.Hostinfo{
|
|
|
|
RoutableIPs: []netip.Prefix{route2},
|
|
|
|
}
|
2023-09-24 11:42:05 +00:00
|
|
|
node2 := types.Node{
|
2022-11-24 22:41:11 +00:00
|
|
|
ID: 2,
|
2023-09-24 11:42:05 +00:00
|
|
|
Hostname: "test_enable_route_node",
|
2023-01-17 19:36:46 +00:00
|
|
|
UserID: user.ID,
|
2023-05-21 16:37:59 +00:00
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2024-07-18 08:01:59 +00:00
|
|
|
AuthKeyID: ptr.To(pak.ID),
|
2023-11-21 17:20:06 +00:00
|
|
|
Hostinfo: &hostInfo2,
|
2022-11-24 16:00:40 +00:00
|
|
|
}
|
2024-02-08 16:28:19 +00:00
|
|
|
db.DB.Save(&node2)
|
2022-11-24 16:00:40 +00:00
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
sendUpdate, err = db.SaveNodeRoutes(&node2)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-12-09 17:09:24 +00:00
|
|
|
c.Assert(sendUpdate, check.Equals, false)
|
2022-11-24 16:00:40 +00:00
|
|
|
|
2024-10-02 07:06:09 +00:00
|
|
|
_, err = db.enableRoutes(&node2, route2)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
enabledRoutes1, err := db.GetEnabledRoutes(&node1)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(enabledRoutes1), check.Equals, 2)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
enabledRoutes2, err := db.GetEnabledRoutes(&node2)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(enabledRoutes2), check.Equals, 1)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
routes, err := db.GetNodePrimaryRoutes(&node1)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(routes), check.Equals, 2)
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
routes, err = db.GetNodePrimaryRoutes(&node2)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(len(routes), check.Equals, 0)
|
|
|
|
}
|
2022-11-24 22:41:11 +00:00
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
func (s *Suite) TestDeleteRoutes(c *check.C) {
|
2023-05-21 16:37:59 +00:00
|
|
|
user, err := db.CreateUser("test")
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
_, err = db.getNode("test", "test_enable_route_node")
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
|
|
|
prefix, err := netip.ParsePrefix(
|
|
|
|
"10.0.0.0/24",
|
|
|
|
)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
prefix2, err := netip.ParsePrefix(
|
|
|
|
"150.0.10.0/25",
|
|
|
|
)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
hostInfo1 := tailcfg.Hostinfo{
|
|
|
|
RoutableIPs: []netip.Prefix{prefix, prefix2},
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
2023-09-24 11:42:05 +00:00
|
|
|
node1 := types.Node{
|
2022-11-24 22:41:11 +00:00
|
|
|
ID: 1,
|
2023-09-24 11:42:05 +00:00
|
|
|
Hostname: "test_enable_route_node",
|
2023-01-17 19:36:46 +00:00
|
|
|
UserID: user.ID,
|
2023-05-21 16:37:59 +00:00
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2024-07-18 08:01:59 +00:00
|
|
|
AuthKeyID: ptr.To(pak.ID),
|
2023-11-21 17:20:06 +00:00
|
|
|
Hostinfo: &hostInfo1,
|
2022-11-24 22:41:11 +00:00
|
|
|
LastSeen: &now,
|
|
|
|
}
|
2024-05-16 00:40:14 +00:00
|
|
|
trx := db.DB.Save(&node1)
|
|
|
|
c.Assert(trx.Error, check.IsNil)
|
2022-11-24 22:41:11 +00:00
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
sendUpdate, err := db.SaveNodeRoutes(&node1)
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-12-09 17:09:24 +00:00
|
|
|
c.Assert(sendUpdate, check.Equals, false)
|
2022-11-24 22:41:11 +00:00
|
|
|
|
2024-10-02 07:06:09 +00:00
|
|
|
_, err = db.enableRoutes(&node1, prefix)
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2024-10-02 07:06:09 +00:00
|
|
|
_, err = db.enableRoutes(&node1, prefix2)
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
routes, err := db.GetNodeRoutes(&node1)
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
// TODO(kradalby): check stateupdate
|
2024-02-23 09:59:24 +00:00
|
|
|
_, err = db.DeleteRoute(uint64(routes[0].ID), nil)
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
enabledRoutes1, err := db.GetEnabledRoutes(&node1)
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-12-09 17:09:24 +00:00
|
|
|
c.Assert(len(enabledRoutes1), check.Equals, 1)
|
2022-11-24 22:41:11 +00:00
|
|
|
}
|
2023-01-01 11:53:49 +00:00
|
|
|
|
2024-07-22 06:56:00 +00:00
|
|
|
var (
|
2024-10-02 09:41:58 +00:00
|
|
|
ipp = func(s string) netip.Prefix { return netip.MustParsePrefix(s) }
|
2024-07-22 06:56:00 +00:00
|
|
|
mkNode = func(nid types.NodeID) types.Node {
|
|
|
|
return types.Node{ID: nid}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-04-10 13:35:09 +00:00
|
|
|
var np = func(nid types.NodeID) *types.Node {
|
2024-07-22 06:56:00 +00:00
|
|
|
no := mkNode(nid)
|
2024-04-10 13:35:09 +00:00
|
|
|
return &no
|
|
|
|
}
|
2024-07-22 06:56:00 +00:00
|
|
|
|
2024-10-02 09:41:58 +00:00
|
|
|
var r = func(id uint, nid types.NodeID, prefix netip.Prefix, enabled, primary bool) types.Route {
|
2024-04-10 13:35:09 +00:00
|
|
|
return types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: id,
|
|
|
|
},
|
2024-07-22 06:56:00 +00:00
|
|
|
Node: mkNode(nid),
|
2024-04-10 13:35:09 +00:00
|
|
|
Prefix: prefix,
|
|
|
|
Enabled: enabled,
|
|
|
|
IsPrimary: primary,
|
|
|
|
}
|
|
|
|
}
|
2024-07-22 06:56:00 +00:00
|
|
|
|
2024-10-02 09:41:58 +00:00
|
|
|
var rp = func(id uint, nid types.NodeID, prefix netip.Prefix, enabled, primary bool) *types.Route {
|
2024-04-10 13:35:09 +00:00
|
|
|
ro := r(id, nid, prefix, enabled, primary)
|
|
|
|
return &ro
|
|
|
|
}
|
|
|
|
|
|
|
|
func dbForTest(t *testing.T, testName string) *HSDatabase {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
tmpDir, err := os.MkdirTemp("", testName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("creating tempdir: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dbPath := tmpDir + "/headscale_test.db"
|
|
|
|
|
|
|
|
db, err = NewHeadscaleDatabase(
|
|
|
|
types.DatabaseConfig{
|
|
|
|
Type: "sqlite3",
|
|
|
|
Sqlite: types.SqliteConfig{
|
|
|
|
Path: dbPath,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("setting up database: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("database set up at: %s", dbPath)
|
|
|
|
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFailoverNodeRoutesIfNeccessary(t *testing.T) {
|
|
|
|
su := func(nids ...types.NodeID) *types.StateUpdate {
|
|
|
|
return &types.StateUpdate{
|
|
|
|
ChangeNodes: nids,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
nodes types.Nodes
|
|
|
|
routes types.Routes
|
2024-04-21 16:28:17 +00:00
|
|
|
isConnected []map[types.NodeID]bool
|
2024-04-10 13:35:09 +00:00
|
|
|
want []*types.StateUpdate
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "n1-down-n2-down-n1-up",
|
|
|
|
nodes: types.Nodes{
|
|
|
|
np(1),
|
|
|
|
np(2),
|
|
|
|
np(1),
|
|
|
|
},
|
|
|
|
routes: types.Routes{
|
|
|
|
r(1, 1, ipp("10.0.0.0/24"), true, true),
|
|
|
|
r(2, 2, ipp("10.0.0.0/24"), true, false),
|
|
|
|
},
|
2024-04-21 16:28:17 +00:00
|
|
|
isConnected: []map[types.NodeID]bool{
|
2024-04-10 13:35:09 +00:00
|
|
|
// n1 goes down
|
|
|
|
{
|
|
|
|
1: false,
|
|
|
|
2: true,
|
|
|
|
},
|
|
|
|
// n2 goes down
|
|
|
|
{
|
|
|
|
1: false,
|
|
|
|
2: false,
|
|
|
|
},
|
|
|
|
// n1 comes up
|
|
|
|
{
|
|
|
|
1: true,
|
|
|
|
2: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []*types.StateUpdate{
|
|
|
|
// route changes from 1 -> 2
|
|
|
|
su(1, 2),
|
|
|
|
// both down, no change
|
|
|
|
nil,
|
|
|
|
// route changes from 2 -> 1
|
|
|
|
su(1, 2),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "n1-recon-n2-down-n1-recon-n2-up",
|
|
|
|
nodes: types.Nodes{
|
|
|
|
np(1),
|
|
|
|
np(2),
|
|
|
|
np(1),
|
|
|
|
np(2),
|
|
|
|
},
|
|
|
|
routes: types.Routes{
|
|
|
|
r(1, 1, ipp("10.0.0.0/24"), true, true),
|
|
|
|
r(2, 2, ipp("10.0.0.0/24"), true, false),
|
|
|
|
},
|
2024-04-21 16:28:17 +00:00
|
|
|
isConnected: []map[types.NodeID]bool{
|
2024-04-10 13:35:09 +00:00
|
|
|
// n1 up recon = noop
|
|
|
|
{
|
|
|
|
1: true,
|
|
|
|
2: true,
|
|
|
|
},
|
|
|
|
// n2 goes down
|
|
|
|
{
|
|
|
|
1: true,
|
|
|
|
2: false,
|
|
|
|
},
|
|
|
|
// n1 up recon = noop
|
|
|
|
{
|
|
|
|
1: true,
|
|
|
|
2: false,
|
|
|
|
},
|
|
|
|
// n2 comes back up
|
|
|
|
{
|
|
|
|
1: true,
|
|
|
|
2: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []*types.StateUpdate{
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "n1-recon-n2-down-n1-recon-n2-up",
|
|
|
|
nodes: types.Nodes{
|
|
|
|
np(1),
|
|
|
|
np(1),
|
|
|
|
np(3),
|
|
|
|
np(3),
|
|
|
|
np(2),
|
|
|
|
np(1),
|
|
|
|
},
|
|
|
|
routes: types.Routes{
|
|
|
|
r(1, 1, ipp("10.0.0.0/24"), true, true),
|
|
|
|
r(2, 2, ipp("10.0.0.0/24"), true, false),
|
|
|
|
r(3, 3, ipp("10.0.0.0/24"), true, false),
|
|
|
|
},
|
2024-04-21 16:28:17 +00:00
|
|
|
isConnected: []map[types.NodeID]bool{
|
2024-04-10 13:35:09 +00:00
|
|
|
// n1 goes down
|
|
|
|
{
|
|
|
|
1: false,
|
|
|
|
2: false,
|
|
|
|
3: true,
|
|
|
|
},
|
|
|
|
// n1 comes up
|
|
|
|
{
|
|
|
|
1: true,
|
|
|
|
2: false,
|
|
|
|
3: true,
|
|
|
|
},
|
|
|
|
// n3 goes down
|
|
|
|
{
|
|
|
|
1: true,
|
|
|
|
2: false,
|
|
|
|
3: false,
|
|
|
|
},
|
|
|
|
// n3 comes up
|
|
|
|
{
|
|
|
|
1: true,
|
|
|
|
2: false,
|
|
|
|
3: true,
|
|
|
|
},
|
|
|
|
// n2 comes up
|
|
|
|
{
|
|
|
|
1: true,
|
|
|
|
2: true,
|
|
|
|
3: true,
|
|
|
|
},
|
|
|
|
// n1 goes down
|
|
|
|
{
|
|
|
|
1: false,
|
|
|
|
2: true,
|
|
|
|
3: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []*types.StateUpdate{
|
|
|
|
su(1, 3), // n1 -> n3
|
|
|
|
nil,
|
|
|
|
su(1, 3), // n3 -> n1
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
su(1, 2), // n1 -> n2
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "n1-recon-n2-dis-n3-take",
|
|
|
|
nodes: types.Nodes{
|
|
|
|
np(1),
|
|
|
|
np(3),
|
|
|
|
},
|
|
|
|
routes: types.Routes{
|
|
|
|
r(1, 1, ipp("10.0.0.0/24"), true, true),
|
|
|
|
r(2, 2, ipp("10.0.0.0/24"), false, false),
|
|
|
|
r(3, 3, ipp("10.0.0.0/24"), true, false),
|
|
|
|
},
|
2024-04-21 16:28:17 +00:00
|
|
|
isConnected: []map[types.NodeID]bool{
|
2024-04-10 13:35:09 +00:00
|
|
|
// n1 goes down
|
|
|
|
{
|
|
|
|
1: false,
|
|
|
|
2: true,
|
|
|
|
3: true,
|
|
|
|
},
|
|
|
|
// n3 goes down
|
|
|
|
{
|
|
|
|
1: false,
|
|
|
|
2: true,
|
|
|
|
3: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []*types.StateUpdate{
|
|
|
|
su(1, 3), // n1 -> n3
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multi-n1-oneforeach-n2-n3",
|
|
|
|
nodes: types.Nodes{
|
|
|
|
np(1),
|
|
|
|
},
|
|
|
|
routes: types.Routes{
|
|
|
|
r(1, 1, ipp("10.0.0.0/24"), true, true),
|
|
|
|
r(4, 1, ipp("10.1.0.0/24"), true, true),
|
|
|
|
r(2, 2, ipp("10.0.0.0/24"), true, false),
|
|
|
|
r(3, 3, ipp("10.1.0.0/24"), true, false),
|
|
|
|
},
|
2024-04-21 16:28:17 +00:00
|
|
|
isConnected: []map[types.NodeID]bool{
|
2024-04-10 13:35:09 +00:00
|
|
|
// n1 goes down
|
|
|
|
{
|
|
|
|
1: false,
|
|
|
|
2: true,
|
|
|
|
3: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []*types.StateUpdate{
|
|
|
|
su(1, 2, 3), // n1 -> n2,n3
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multi-n1-onefor-n2-disabled-n3",
|
|
|
|
nodes: types.Nodes{
|
|
|
|
np(1),
|
|
|
|
},
|
|
|
|
routes: types.Routes{
|
|
|
|
r(1, 1, ipp("10.0.0.0/24"), true, true),
|
|
|
|
r(4, 1, ipp("10.1.0.0/24"), true, true),
|
|
|
|
r(2, 2, ipp("10.0.0.0/24"), true, false),
|
|
|
|
r(3, 3, ipp("10.1.0.0/24"), false, false),
|
|
|
|
},
|
2024-04-21 16:28:17 +00:00
|
|
|
isConnected: []map[types.NodeID]bool{
|
2024-04-10 13:35:09 +00:00
|
|
|
// n1 goes down
|
|
|
|
{
|
|
|
|
1: false,
|
|
|
|
2: true,
|
|
|
|
3: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []*types.StateUpdate{
|
|
|
|
su(1, 2), // n1 -> n2, n3 is not enabled
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multi-n1-onefor-n2-offline-n3",
|
|
|
|
nodes: types.Nodes{
|
|
|
|
np(1),
|
|
|
|
},
|
|
|
|
routes: types.Routes{
|
|
|
|
r(1, 1, ipp("10.0.0.0/24"), true, true),
|
|
|
|
r(4, 1, ipp("10.1.0.0/24"), true, true),
|
|
|
|
r(2, 2, ipp("10.0.0.0/24"), true, false),
|
|
|
|
r(3, 3, ipp("10.1.0.0/24"), true, false),
|
|
|
|
},
|
2024-04-21 16:28:17 +00:00
|
|
|
isConnected: []map[types.NodeID]bool{
|
2024-04-10 13:35:09 +00:00
|
|
|
// n1 goes down
|
|
|
|
{
|
|
|
|
1: false,
|
|
|
|
2: true,
|
|
|
|
3: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []*types.StateUpdate{
|
|
|
|
su(1, 2), // n1 -> n2, n3 is offline
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multi-n2-back-to-multi-n1",
|
|
|
|
nodes: types.Nodes{
|
|
|
|
np(1),
|
|
|
|
},
|
|
|
|
routes: types.Routes{
|
|
|
|
r(1, 1, ipp("10.0.0.0/24"), true, false),
|
|
|
|
r(4, 1, ipp("10.1.0.0/24"), true, true),
|
|
|
|
r(2, 2, ipp("10.0.0.0/24"), true, true),
|
|
|
|
r(3, 3, ipp("10.1.0.0/24"), true, false),
|
|
|
|
},
|
2024-04-21 16:28:17 +00:00
|
|
|
isConnected: []map[types.NodeID]bool{
|
2024-04-10 13:35:09 +00:00
|
|
|
// n1 goes down
|
|
|
|
{
|
|
|
|
1: true,
|
|
|
|
2: false,
|
|
|
|
3: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []*types.StateUpdate{
|
|
|
|
su(1, 2), // n2 -> n1
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if (len(tt.isConnected) != len(tt.want)) && len(tt.want) != len(tt.nodes) {
|
|
|
|
t.Fatalf("nodes (%d), isConnected updates (%d), wants (%d) must be equal", len(tt.nodes), len(tt.isConnected), len(tt.want))
|
|
|
|
}
|
|
|
|
|
|
|
|
db := dbForTest(t, tt.name)
|
|
|
|
|
2024-05-16 00:40:14 +00:00
|
|
|
user := types.User{Name: tt.name}
|
|
|
|
if err := db.DB.Save(&user).Error; err != nil {
|
|
|
|
t.Fatalf("failed to create user: %s", err)
|
|
|
|
}
|
|
|
|
|
2024-04-10 13:35:09 +00:00
|
|
|
for _, route := range tt.routes {
|
2024-05-16 00:40:14 +00:00
|
|
|
route.Node.User = user
|
|
|
|
if err := db.DB.Save(&route.Node).Error; err != nil {
|
|
|
|
t.Fatalf("failed to create node: %s", err)
|
|
|
|
}
|
2024-04-10 13:35:09 +00:00
|
|
|
if err := db.DB.Save(&route).Error; err != nil {
|
|
|
|
t.Fatalf("failed to create route: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for step := range len(tt.isConnected) {
|
|
|
|
node := tt.nodes[step]
|
|
|
|
isConnected := tt.isConnected[step]
|
|
|
|
want := tt.want[step]
|
|
|
|
|
|
|
|
got, err := Write(db.DB, func(tx *gorm.DB) (*types.StateUpdate, error) {
|
2024-04-21 16:28:17 +00:00
|
|
|
return FailoverNodeRoutesIfNeccessary(tx, smap(isConnected), node)
|
2024-04-10 13:35:09 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("failoverRoute() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := cmp.Diff(want, got, cmpopts.IgnoreFields(types.StateUpdate{}, "Type", "Message")); diff != "" {
|
|
|
|
t.Errorf("failoverRoute() unexpected result (-want +got):\n%s", diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2023-03-06 08:05:40 +00:00
|
|
|
|
2024-02-23 09:59:24 +00:00
|
|
|
func TestFailoverRouteTx(t *testing.T) {
|
2023-12-09 17:09:24 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
failingRoute types.Route
|
|
|
|
routes types.Routes
|
2024-04-21 16:28:17 +00:00
|
|
|
isConnected map[types.NodeID]bool
|
2024-02-23 09:59:24 +00:00
|
|
|
want []types.NodeID
|
2023-12-09 17:09:24 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no-route",
|
|
|
|
failingRoute: types.Route{},
|
|
|
|
routes: types.Routes{},
|
|
|
|
want: nil,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no-prime",
|
|
|
|
failingRoute: types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 1,
|
|
|
|
},
|
2024-02-23 09:59:24 +00:00
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{},
|
2023-12-09 17:09:24 +00:00
|
|
|
IsPrimary: false,
|
|
|
|
},
|
|
|
|
routes: types.Routes{},
|
|
|
|
want: nil,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "exit-node",
|
|
|
|
failingRoute: types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 1,
|
|
|
|
},
|
2024-02-23 09:59:24 +00:00
|
|
|
Prefix: ipp("0.0.0.0/0"),
|
|
|
|
Node: types.Node{},
|
2023-12-09 17:09:24 +00:00
|
|
|
IsPrimary: true,
|
|
|
|
},
|
|
|
|
routes: types.Routes{},
|
|
|
|
want: nil,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no-failover-single-route",
|
|
|
|
failingRoute: types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 1,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
2024-02-23 09:59:24 +00:00
|
|
|
ID: 1,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
IsPrimary: true,
|
|
|
|
},
|
|
|
|
routes: types.Routes{
|
|
|
|
types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 1,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
2024-02-23 09:59:24 +00:00
|
|
|
ID: 1,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
IsPrimary: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "failover-primary",
|
|
|
|
failingRoute: types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 1,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
2024-02-23 09:59:24 +00:00
|
|
|
ID: 1,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
IsPrimary: true,
|
2024-02-03 14:30:15 +00:00
|
|
|
Enabled: true,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
routes: types.Routes{
|
|
|
|
types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 1,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
2024-02-23 09:59:24 +00:00
|
|
|
ID: 1,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
IsPrimary: true,
|
2024-02-03 14:30:15 +00:00
|
|
|
Enabled: true,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 2,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
2024-02-23 09:59:24 +00:00
|
|
|
ID: 2,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
IsPrimary: false,
|
2024-02-03 14:30:15 +00:00
|
|
|
Enabled: true,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
},
|
2024-04-21 16:28:17 +00:00
|
|
|
isConnected: map[types.NodeID]bool{
|
2024-02-23 09:59:24 +00:00
|
|
|
1: false,
|
|
|
|
2: true,
|
2024-02-08 16:28:19 +00:00
|
|
|
},
|
2024-02-23 09:59:24 +00:00
|
|
|
want: []types.NodeID{
|
|
|
|
1,
|
|
|
|
2,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "failover-none-primary",
|
|
|
|
failingRoute: types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 1,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
2024-02-23 09:59:24 +00:00
|
|
|
ID: 1,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
IsPrimary: false,
|
2024-02-03 14:30:15 +00:00
|
|
|
Enabled: true,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
routes: types.Routes{
|
|
|
|
types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 1,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
2024-02-23 09:59:24 +00:00
|
|
|
ID: 1,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
IsPrimary: true,
|
2024-02-03 14:30:15 +00:00
|
|
|
Enabled: true,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 2,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
2024-02-23 09:59:24 +00:00
|
|
|
ID: 2,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
IsPrimary: false,
|
2024-02-03 14:30:15 +00:00
|
|
|
Enabled: true,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "failover-primary-multi-route",
|
|
|
|
failingRoute: types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 2,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
2024-02-23 09:59:24 +00:00
|
|
|
ID: 2,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
IsPrimary: true,
|
2024-02-03 14:30:15 +00:00
|
|
|
Enabled: true,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
routes: types.Routes{
|
|
|
|
types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 1,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
2024-02-23 09:59:24 +00:00
|
|
|
ID: 1,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
IsPrimary: false,
|
2024-02-03 14:30:15 +00:00
|
|
|
Enabled: true,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 2,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
2024-02-23 09:59:24 +00:00
|
|
|
ID: 2,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
IsPrimary: true,
|
2024-02-03 14:30:15 +00:00
|
|
|
Enabled: true,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 3,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
2024-02-23 09:59:24 +00:00
|
|
|
ID: 3,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
IsPrimary: false,
|
2024-02-03 14:30:15 +00:00
|
|
|
Enabled: true,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
},
|
2024-04-21 16:28:17 +00:00
|
|
|
isConnected: map[types.NodeID]bool{
|
2024-02-23 09:59:24 +00:00
|
|
|
1: true,
|
|
|
|
2: true,
|
|
|
|
3: true,
|
2024-02-08 16:28:19 +00:00
|
|
|
},
|
2024-02-23 09:59:24 +00:00
|
|
|
want: []types.NodeID{
|
|
|
|
2, 1,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "failover-primary-no-online",
|
|
|
|
failingRoute: types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 1,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
2024-02-23 09:59:24 +00:00
|
|
|
ID: 1,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
IsPrimary: true,
|
2024-02-03 14:30:15 +00:00
|
|
|
Enabled: true,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
routes: types.Routes{
|
|
|
|
types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 1,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
2024-02-23 09:59:24 +00:00
|
|
|
ID: 1,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
IsPrimary: true,
|
2024-02-03 14:30:15 +00:00
|
|
|
Enabled: true,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
// Offline
|
|
|
|
types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 2,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
2024-02-23 09:59:24 +00:00
|
|
|
ID: 4,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
IsPrimary: false,
|
2024-02-03 14:30:15 +00:00
|
|
|
Enabled: true,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
},
|
2024-04-21 16:28:17 +00:00
|
|
|
isConnected: map[types.NodeID]bool{
|
2024-02-23 09:59:24 +00:00
|
|
|
1: true,
|
|
|
|
4: false,
|
2024-02-08 16:28:19 +00:00
|
|
|
},
|
2023-12-09 17:09:24 +00:00
|
|
|
want: nil,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "failover-primary-one-not-online",
|
|
|
|
failingRoute: types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 1,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
2024-02-23 09:59:24 +00:00
|
|
|
ID: 1,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
IsPrimary: true,
|
2024-02-03 14:30:15 +00:00
|
|
|
Enabled: true,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
routes: types.Routes{
|
|
|
|
types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 1,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
2024-02-23 09:59:24 +00:00
|
|
|
ID: 1,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
IsPrimary: true,
|
2024-02-03 14:30:15 +00:00
|
|
|
Enabled: true,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
// Offline
|
|
|
|
types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 2,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
2024-02-23 09:59:24 +00:00
|
|
|
ID: 4,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
IsPrimary: false,
|
2024-02-03 14:30:15 +00:00
|
|
|
Enabled: true,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 3,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
2024-02-23 09:59:24 +00:00
|
|
|
ID: 2,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
IsPrimary: true,
|
2024-02-03 14:30:15 +00:00
|
|
|
Enabled: true,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
},
|
2024-04-21 16:28:17 +00:00
|
|
|
isConnected: map[types.NodeID]bool{
|
2024-02-23 09:59:24 +00:00
|
|
|
1: false,
|
|
|
|
2: true,
|
|
|
|
4: false,
|
2024-02-08 16:28:19 +00:00
|
|
|
},
|
2024-02-23 09:59:24 +00:00
|
|
|
want: []types.NodeID{
|
|
|
|
1,
|
|
|
|
2,
|
2023-12-09 17:09:24 +00:00
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
2024-02-03 14:30:15 +00:00
|
|
|
{
|
|
|
|
name: "failover-primary-none-enabled",
|
|
|
|
failingRoute: types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 1,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
2024-02-23 09:59:24 +00:00
|
|
|
ID: 1,
|
2024-02-03 14:30:15 +00:00
|
|
|
},
|
|
|
|
IsPrimary: true,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
routes: types.Routes{
|
|
|
|
types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 1,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
2024-02-23 09:59:24 +00:00
|
|
|
ID: 1,
|
2024-02-03 14:30:15 +00:00
|
|
|
},
|
|
|
|
IsPrimary: true,
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
// not enabled
|
|
|
|
types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 2,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
2024-02-23 09:59:24 +00:00
|
|
|
ID: 2,
|
2024-02-03 14:30:15 +00:00
|
|
|
},
|
|
|
|
IsPrimary: false,
|
|
|
|
Enabled: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
2023-03-06 08:05:40 +00:00
|
|
|
}
|
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-04-10 13:35:09 +00:00
|
|
|
db := dbForTest(t, tt.name)
|
2024-05-16 00:40:14 +00:00
|
|
|
user := types.User{Name: "test"}
|
|
|
|
if err := db.DB.Save(&user).Error; err != nil {
|
|
|
|
t.Fatalf("failed to create user: %s", err)
|
|
|
|
}
|
2023-12-09 17:09:24 +00:00
|
|
|
|
|
|
|
for _, route := range tt.routes {
|
2024-05-16 00:40:14 +00:00
|
|
|
route.Node.User = user
|
|
|
|
if err := db.DB.Save(&route.Node).Error; err != nil {
|
|
|
|
t.Fatalf("failed to create node: %s", err)
|
|
|
|
}
|
2024-02-08 16:28:19 +00:00
|
|
|
if err := db.DB.Save(&route).Error; err != nil {
|
2023-12-09 17:09:24 +00:00
|
|
|
t.Fatalf("failed to create route: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-23 09:59:24 +00:00
|
|
|
got, err := Write(db.DB, func(tx *gorm.DB) ([]types.NodeID, error) {
|
2024-04-21 16:28:17 +00:00
|
|
|
return failoverRouteTx(tx, smap(tt.isConnected), &tt.failingRoute)
|
2024-02-08 16:28:19 +00:00
|
|
|
})
|
2023-12-09 17:09:24 +00:00
|
|
|
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("failoverRoute() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := cmp.Diff(tt.want, got, util.Comparers...); diff != "" {
|
|
|
|
t.Errorf("failoverRoute() unexpected result (-want +got):\n%s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2023-03-06 08:05:40 +00:00
|
|
|
}
|
2024-02-08 16:28:19 +00:00
|
|
|
|
2024-02-23 09:59:24 +00:00
|
|
|
func TestFailoverRoute(t *testing.T) {
|
2024-10-02 09:41:58 +00:00
|
|
|
r := func(id uint, nid types.NodeID, prefix netip.Prefix, enabled, primary bool) types.Route {
|
2024-02-23 09:59:24 +00:00
|
|
|
return types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: id,
|
|
|
|
},
|
|
|
|
Node: types.Node{
|
|
|
|
ID: nid,
|
|
|
|
},
|
|
|
|
Prefix: prefix,
|
|
|
|
Enabled: enabled,
|
|
|
|
IsPrimary: primary,
|
|
|
|
}
|
|
|
|
}
|
2024-10-02 09:41:58 +00:00
|
|
|
rp := func(id uint, nid types.NodeID, prefix netip.Prefix, enabled, primary bool) *types.Route {
|
2024-02-23 09:59:24 +00:00
|
|
|
ro := r(id, nid, prefix, enabled, primary)
|
|
|
|
return &ro
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
failingRoute types.Route
|
|
|
|
routes types.Routes
|
2024-04-21 16:28:17 +00:00
|
|
|
isConnected map[types.NodeID]bool
|
2024-02-23 09:59:24 +00:00
|
|
|
want *failover
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no-route",
|
|
|
|
failingRoute: types.Route{},
|
|
|
|
routes: types.Routes{},
|
|
|
|
want: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no-prime",
|
|
|
|
failingRoute: r(1, 1, ipp("10.0.0.0/24"), false, false),
|
|
|
|
|
|
|
|
routes: types.Routes{},
|
|
|
|
want: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "exit-node",
|
|
|
|
failingRoute: r(1, 1, ipp("0.0.0.0/0"), false, true),
|
|
|
|
routes: types.Routes{},
|
|
|
|
want: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no-failover-single-route",
|
|
|
|
failingRoute: r(1, 1, ipp("10.0.0.0/24"), false, true),
|
|
|
|
routes: types.Routes{
|
|
|
|
r(1, 1, ipp("10.0.0.0/24"), false, true),
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "failover-primary",
|
|
|
|
failingRoute: r(1, 1, ipp("10.0.0.0/24"), true, true),
|
|
|
|
routes: types.Routes{
|
|
|
|
r(1, 1, ipp("10.0.0.0/24"), true, true),
|
|
|
|
r(2, 2, ipp("10.0.0.0/24"), true, false),
|
|
|
|
},
|
2024-04-21 16:28:17 +00:00
|
|
|
isConnected: map[types.NodeID]bool{
|
2024-02-23 09:59:24 +00:00
|
|
|
1: false,
|
|
|
|
2: true,
|
|
|
|
},
|
|
|
|
want: &failover{
|
|
|
|
old: rp(1, 1, ipp("10.0.0.0/24"), true, false),
|
|
|
|
new: rp(2, 2, ipp("10.0.0.0/24"), true, true),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "failover-none-primary",
|
|
|
|
failingRoute: r(1, 1, ipp("10.0.0.0/24"), true, false),
|
|
|
|
routes: types.Routes{
|
|
|
|
r(1, 1, ipp("10.0.0.0/24"), true, true),
|
|
|
|
r(2, 2, ipp("10.0.0.0/24"), true, false),
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "failover-primary-multi-route",
|
|
|
|
failingRoute: r(2, 2, ipp("10.0.0.0/24"), true, true),
|
|
|
|
routes: types.Routes{
|
|
|
|
r(1, 1, ipp("10.0.0.0/24"), true, false),
|
|
|
|
r(2, 2, ipp("10.0.0.0/24"), true, true),
|
|
|
|
r(3, 3, ipp("10.0.0.0/24"), true, false),
|
|
|
|
},
|
2024-04-21 16:28:17 +00:00
|
|
|
isConnected: map[types.NodeID]bool{
|
2024-02-23 09:59:24 +00:00
|
|
|
1: true,
|
|
|
|
2: true,
|
|
|
|
3: true,
|
|
|
|
},
|
|
|
|
want: &failover{
|
|
|
|
old: rp(2, 2, ipp("10.0.0.0/24"), true, false),
|
|
|
|
new: rp(1, 1, ipp("10.0.0.0/24"), true, true),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "failover-primary-no-online",
|
|
|
|
failingRoute: r(1, 1, ipp("10.0.0.0/24"), true, true),
|
|
|
|
routes: types.Routes{
|
|
|
|
r(1, 1, ipp("10.0.0.0/24"), true, true),
|
|
|
|
r(2, 4, ipp("10.0.0.0/24"), true, false),
|
|
|
|
},
|
2024-04-21 16:28:17 +00:00
|
|
|
isConnected: map[types.NodeID]bool{
|
2024-02-23 09:59:24 +00:00
|
|
|
1: true,
|
|
|
|
4: false,
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "failover-primary-one-not-online",
|
|
|
|
failingRoute: r(1, 1, ipp("10.0.0.0/24"), true, true),
|
|
|
|
routes: types.Routes{
|
|
|
|
r(1, 1, ipp("10.0.0.0/24"), true, true),
|
|
|
|
r(2, 4, ipp("10.0.0.0/24"), true, false),
|
|
|
|
r(3, 2, ipp("10.0.0.0/24"), true, false),
|
|
|
|
},
|
2024-04-21 16:28:17 +00:00
|
|
|
isConnected: map[types.NodeID]bool{
|
2024-02-23 09:59:24 +00:00
|
|
|
1: false,
|
|
|
|
2: true,
|
|
|
|
4: false,
|
|
|
|
},
|
|
|
|
want: &failover{
|
|
|
|
old: rp(1, 1, ipp("10.0.0.0/24"), true, false),
|
|
|
|
new: rp(3, 2, ipp("10.0.0.0/24"), true, true),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "failover-primary-none-enabled",
|
|
|
|
failingRoute: r(1, 1, ipp("10.0.0.0/24"), true, true),
|
|
|
|
routes: types.Routes{
|
|
|
|
r(1, 1, ipp("10.0.0.0/24"), true, false),
|
|
|
|
r(2, 2, ipp("10.0.0.0/24"), false, true),
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2024-04-21 16:28:17 +00:00
|
|
|
gotf := failoverRoute(smap(tt.isConnected), &tt.failingRoute, tt.routes)
|
2024-02-23 09:59:24 +00:00
|
|
|
|
|
|
|
if tt.want == nil && gotf != nil {
|
|
|
|
t.Fatalf("expected nil, got %+v", gotf)
|
|
|
|
}
|
|
|
|
|
|
|
|
if gotf == nil && tt.want != nil {
|
|
|
|
t.Fatalf("expected %+v, got nil", tt.want)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tt.want != nil && gotf != nil {
|
|
|
|
want := map[string]*types.Route{
|
|
|
|
"new": tt.want.new,
|
|
|
|
"old": tt.want.old,
|
|
|
|
}
|
|
|
|
|
|
|
|
got := map[string]*types.Route{
|
|
|
|
"new": gotf.new,
|
|
|
|
"old": gotf.old,
|
|
|
|
}
|
|
|
|
|
2024-10-02 09:41:58 +00:00
|
|
|
if diff := cmp.Diff(want, got, util.Comparers...); diff != "" {
|
2024-02-23 09:59:24 +00:00
|
|
|
t.Fatalf("failoverRoute unexpected result (-want +got):\n%s", diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|