2023-05-21 19:37:59 +03:00
|
|
|
package db
|
2021-05-11 20:55:36 -04:00
|
|
|
|
|
|
|
import (
|
2022-09-02 00:06:19 +02:00
|
|
|
"net/netip"
|
2023-12-09 18:09:24 +01:00
|
|
|
"os"
|
|
|
|
"testing"
|
2022-11-24 22:41:11 +00:00
|
|
|
"time"
|
2022-09-02 00:06:19 +02:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2023-05-21 19:37:59 +03:00
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
2023-05-11 09:09:18 +02:00
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
2023-12-09 18:09:24 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-05-11 20:55:36 -04:00
|
|
|
"gopkg.in/check.v1"
|
2023-12-09 18:09:24 +01:00
|
|
|
"gorm.io/gorm"
|
2021-05-11 20:55:36 -04:00
|
|
|
"tailscale.com/tailcfg"
|
2023-12-09 18:09:24 +01:00
|
|
|
"tailscale.com/types/key"
|
2021-05-11 20:55:36 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Suite) TestGetRoutes(c *check.C) {
|
2023-05-21 19:37:59 +03:00
|
|
|
user, err := db.CreateUser("test")
|
2021-05-11 20:55:36 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 19:37:59 +03:00
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2021-05-11 20:55:36 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2024-02-08 17:28:19 +01:00
|
|
|
_, err = db.getNode("test", "test_get_route_node")
|
2021-05-11 20:55:36 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2022-09-02 00:06:19 +02:00
|
|
|
route, err := netip.ParsePrefix("10.0.0.0/24")
|
2021-05-11 20:55:36 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
hostInfo := tailcfg.Hostinfo{
|
2022-09-02 00:06:19 +02:00
|
|
|
RoutableIPs: []netip.Prefix{route},
|
2021-05-11 20:55:36 -04:00
|
|
|
}
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
node := types.Node{
|
2021-05-11 20:55:36 -04:00
|
|
|
ID: 0,
|
2023-09-24 13:42:05 +02:00
|
|
|
Hostname: "test_get_route_node",
|
2023-01-17 20:36:46 +01:00
|
|
|
UserID: user.ID,
|
2023-05-21 19:37:59 +03:00
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2021-05-11 20:55:36 -04:00
|
|
|
AuthKeyID: uint(pak.ID),
|
2023-11-21 18:20:06 +01:00
|
|
|
Hostinfo: &hostInfo,
|
2021-05-11 20:55:36 -04:00
|
|
|
}
|
2024-02-08 17:28:19 +01:00
|
|
|
db.DB.Save(&node)
|
2021-05-11 20:55:36 -04:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
su, err := db.SaveNodeRoutes(&node)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-12-09 18:09:24 +01:00
|
|
|
c.Assert(su, check.Equals, false)
|
2022-11-24 16:00:40 +00:00
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
advertisedRoutes, err := db.GetAdvertisedRoutes(&node)
|
2021-05-11 20:55:36 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(len(advertisedRoutes), check.Equals, 1)
|
2021-05-11 20:55:36 -04:00
|
|
|
|
2024-02-08 17:28:19 +01:00
|
|
|
// TODO(kradalby): check state update
|
|
|
|
_, err = db.enableRoutes(&node, "192.168.0.0/24")
|
2021-05-11 20:55:36 -04:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2024-02-08 17:28:19 +01:00
|
|
|
_, err = db.enableRoutes(&node, "10.0.0.0/24")
|
2021-08-21 14:49:46 +01:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Suite) TestGetEnableRoutes(c *check.C) {
|
2023-05-21 19:37:59 +03:00
|
|
|
user, err := db.CreateUser("test")
|
2021-08-21 14:49:46 +01:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 19:37:59 +03:00
|
|
|
pak, err := db.CreatePreAuthKey(user.Name, false, false, nil, nil)
|
2021-05-11 20:55:36 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2024-02-08 17:28:19 +01:00
|
|
|
_, err = db.getNode("test", "test_enable_route_node")
|
2021-08-21 14:49:46 +01:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2022-09-02 00:06:19 +02:00
|
|
|
route, err := netip.ParsePrefix(
|
2021-08-21 14:49:46 +01:00
|
|
|
"10.0.0.0/24",
|
|
|
|
)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2022-09-02 00:06:19 +02:00
|
|
|
route2, err := netip.ParsePrefix(
|
2021-08-21 14:49:46 +01:00
|
|
|
"150.0.10.0/25",
|
|
|
|
)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2021-11-15 16:16:04 +00:00
|
|
|
hostInfo := tailcfg.Hostinfo{
|
2022-09-02 00:06:19 +02:00
|
|
|
RoutableIPs: []netip.Prefix{route, route2},
|
2021-08-21 14:49:46 +01:00
|
|
|
}
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
node := types.Node{
|
2021-08-21 14:49:46 +01:00
|
|
|
ID: 0,
|
2023-09-24 13:42:05 +02:00
|
|
|
Hostname: "test_enable_route_node",
|
2023-01-17 20:36:46 +01:00
|
|
|
UserID: user.ID,
|
2023-05-21 19:37:59 +03:00
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2021-08-21 14:49:46 +01:00
|
|
|
AuthKeyID: uint(pak.ID),
|
2023-11-21 18:20:06 +01:00
|
|
|
Hostinfo: &hostInfo,
|
2021-08-21 14:49:46 +01:00
|
|
|
}
|
2024-02-08 17:28:19 +01:00
|
|
|
db.DB.Save(&node)
|
2021-08-21 14:49:46 +01:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
sendUpdate, err := db.SaveNodeRoutes(&node)
|
2021-08-21 14:49:46 +01:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-12-09 18:09:24 +01:00
|
|
|
c.Assert(sendUpdate, check.Equals, false)
|
2021-08-21 14:49:46 +01:00
|
|
|
|
2023-09-24 13:42:05 +02: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 13:42:05 +02:00
|
|
|
noEnabledRoutes, err := db.GetEnabledRoutes(&node)
|
2021-08-21 14:49:46 +01:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 16:16:04 +00:00
|
|
|
c.Assert(len(noEnabledRoutes), check.Equals, 0)
|
2021-08-21 14:49:46 +01:00
|
|
|
|
2024-02-08 17:28:19 +01:00
|
|
|
_, err = db.enableRoutes(&node, "192.168.0.0/24")
|
2021-08-21 14:49:46 +01:00
|
|
|
c.Assert(err, check.NotNil)
|
|
|
|
|
2024-02-08 17:28:19 +01:00
|
|
|
_, err = db.enableRoutes(&node, "10.0.0.0/24")
|
2021-08-21 14:49:46 +01:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
enabledRoutes, err := db.GetEnabledRoutes(&node)
|
2021-08-21 14:49:46 +01:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 16:16:04 +00:00
|
|
|
c.Assert(len(enabledRoutes), check.Equals, 1)
|
2021-08-21 14:49:46 +01:00
|
|
|
|
|
|
|
// Adding it twice will just let it pass through
|
2024-02-08 17:28:19 +01:00
|
|
|
_, err = db.enableRoutes(&node, "10.0.0.0/24")
|
2021-08-21 14:49:46 +01:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
enableRoutesAfterDoubleApply, err := db.GetEnabledRoutes(&node)
|
2021-08-21 14:49:46 +01:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 16:16:04 +00:00
|
|
|
c.Assert(len(enableRoutesAfterDoubleApply), check.Equals, 1)
|
2021-08-21 14:49:46 +01:00
|
|
|
|
2024-02-08 17:28:19 +01:00
|
|
|
_, err = db.enableRoutes(&node, "150.0.10.0/25")
|
2021-08-21 14:49:46 +01:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
enabledRoutesWithAdditionalRoute, err := db.GetEnabledRoutes(&node)
|
2021-08-21 14:49:46 +01:00
|
|
|
c.Assert(err, check.IsNil)
|
2021-11-15 16:16:04 +00:00
|
|
|
c.Assert(len(enabledRoutesWithAdditionalRoute), check.Equals, 2)
|
2021-05-11 20:55:36 -04:00
|
|
|
}
|
2022-11-24 16:00:40 +00:00
|
|
|
|
|
|
|
func (s *Suite) TestIsUniquePrefix(c *check.C) {
|
2023-05-21 19:37:59 +03:00
|
|
|
user, err := db.CreateUser("test")
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 19:37:59 +03: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 17:28:19 +01: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 13:42:05 +02:00
|
|
|
node1 := types.Node{
|
2022-11-24 22:41:11 +00:00
|
|
|
ID: 1,
|
2023-09-24 13:42:05 +02:00
|
|
|
Hostname: "test_enable_route_node",
|
2023-01-17 20:36:46 +01:00
|
|
|
UserID: user.ID,
|
2023-05-21 19:37:59 +03:00
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2022-11-24 16:00:40 +00:00
|
|
|
AuthKeyID: uint(pak.ID),
|
2023-11-21 18:20:06 +01:00
|
|
|
Hostinfo: &hostInfo1,
|
2022-11-24 16:00:40 +00:00
|
|
|
}
|
2024-02-08 17:28:19 +01:00
|
|
|
db.DB.Save(&node1)
|
2022-11-24 16:00:40 +00:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
sendUpdate, err := db.SaveNodeRoutes(&node1)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-12-09 18:09:24 +01:00
|
|
|
c.Assert(sendUpdate, check.Equals, false)
|
2022-11-24 16:00:40 +00:00
|
|
|
|
2024-02-08 17:28:19 +01:00
|
|
|
_, err = db.enableRoutes(&node1, route.String())
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2024-02-08 17:28:19 +01:00
|
|
|
_, err = db.enableRoutes(&node1, route2.String())
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
hostInfo2 := tailcfg.Hostinfo{
|
|
|
|
RoutableIPs: []netip.Prefix{route2},
|
|
|
|
}
|
2023-09-24 13:42:05 +02:00
|
|
|
node2 := types.Node{
|
2022-11-24 22:41:11 +00:00
|
|
|
ID: 2,
|
2023-09-24 13:42:05 +02:00
|
|
|
Hostname: "test_enable_route_node",
|
2023-01-17 20:36:46 +01:00
|
|
|
UserID: user.ID,
|
2023-05-21 19:37:59 +03:00
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2022-11-24 16:00:40 +00:00
|
|
|
AuthKeyID: uint(pak.ID),
|
2023-11-21 18:20:06 +01:00
|
|
|
Hostinfo: &hostInfo2,
|
2022-11-24 16:00:40 +00:00
|
|
|
}
|
2024-02-08 17:28:19 +01:00
|
|
|
db.DB.Save(&node2)
|
2022-11-24 16:00:40 +00:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
sendUpdate, err = db.SaveNodeRoutes(&node2)
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-12-09 18:09:24 +01:00
|
|
|
c.Assert(sendUpdate, check.Equals, false)
|
2022-11-24 16:00:40 +00:00
|
|
|
|
2024-02-08 17:28:19 +01:00
|
|
|
_, err = db.enableRoutes(&node2, route2.String())
|
2022-11-24 16:00:40 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-09-24 13:42:05 +02: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 13:42:05 +02: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 13:42:05 +02: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 13:42:05 +02: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 18:09:24 +01:00
|
|
|
func (s *Suite) TestDeleteRoutes(c *check.C) {
|
2023-05-21 19:37:59 +03:00
|
|
|
user, err := db.CreateUser("test")
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-05-21 19:37:59 +03: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 17:28:19 +01: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 13:42:05 +02:00
|
|
|
node1 := types.Node{
|
2022-11-24 22:41:11 +00:00
|
|
|
ID: 1,
|
2023-09-24 13:42:05 +02:00
|
|
|
Hostname: "test_enable_route_node",
|
2023-01-17 20:36:46 +01:00
|
|
|
UserID: user.ID,
|
2023-05-21 19:37:59 +03:00
|
|
|
RegisterMethod: util.RegisterMethodAuthKey,
|
2022-11-24 22:41:11 +00:00
|
|
|
AuthKeyID: uint(pak.ID),
|
2023-11-21 18:20:06 +01:00
|
|
|
Hostinfo: &hostInfo1,
|
2022-11-24 22:41:11 +00:00
|
|
|
LastSeen: &now,
|
|
|
|
}
|
2024-02-08 17:28:19 +01:00
|
|
|
db.DB.Save(&node1)
|
2022-11-24 22:41:11 +00:00
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
sendUpdate, err := db.SaveNodeRoutes(&node1)
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-12-09 18:09:24 +01:00
|
|
|
c.Assert(sendUpdate, check.Equals, false)
|
2022-11-24 22:41:11 +00:00
|
|
|
|
2024-02-08 17:28:19 +01:00
|
|
|
_, err = db.enableRoutes(&node1, prefix.String())
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2024-02-08 17:28:19 +01:00
|
|
|
_, err = db.enableRoutes(&node1, prefix2.String())
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
routes, err := db.GetNodeRoutes(&node1)
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2024-02-08 17:28:19 +01:00
|
|
|
// TODO(kradalby): check stateupdate
|
|
|
|
_, err = db.DeleteRoute(uint64(routes[0].ID), map[key.MachinePublic]bool{})
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
enabledRoutes1, err := db.GetEnabledRoutes(&node1)
|
2022-11-24 22:41:11 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2023-12-09 18:09:24 +01: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-02-08 17:28:19 +01:00
|
|
|
var ipp = func(s string) types.IPPrefix { return types.IPPrefix(netip.MustParsePrefix(s)) }
|
2023-03-06 09:05:40 +01:00
|
|
|
|
2024-02-08 17:28:19 +01:00
|
|
|
func TestFailoverRoute(t *testing.T) {
|
2023-12-09 18:09:24 +01:00
|
|
|
machineKeys := []key.MachinePublic{
|
|
|
|
key.NewMachine().Public(),
|
|
|
|
key.NewMachine().Public(),
|
|
|
|
key.NewMachine().Public(),
|
|
|
|
key.NewMachine().Public(),
|
2023-03-06 09:05:40 +01:00
|
|
|
}
|
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
failingRoute types.Route
|
|
|
|
routes types.Routes
|
2024-02-08 17:28:19 +01:00
|
|
|
isConnected map[key.MachinePublic]bool
|
2023-12-09 18:09:24 +01:00
|
|
|
want []key.MachinePublic
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
|
|
|
MachineKey: machineKeys[0],
|
|
|
|
},
|
|
|
|
IsPrimary: false,
|
|
|
|
},
|
|
|
|
routes: types.Routes{},
|
|
|
|
want: nil,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "exit-node",
|
|
|
|
failingRoute: types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 1,
|
|
|
|
},
|
|
|
|
Prefix: ipp("0.0.0.0/0"),
|
|
|
|
Node: types.Node{
|
|
|
|
MachineKey: machineKeys[0],
|
|
|
|
},
|
|
|
|
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{
|
|
|
|
MachineKey: machineKeys[0],
|
|
|
|
},
|
|
|
|
IsPrimary: 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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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{
|
|
|
|
MachineKey: machineKeys[0],
|
|
|
|
},
|
|
|
|
IsPrimary: true,
|
2024-02-03 15:30:15 +01:00
|
|
|
Enabled: true,
|
2023-12-09 18:09:24 +01:00
|
|
|
},
|
|
|
|
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,
|
2024-02-03 15:30:15 +01:00
|
|
|
Enabled: true,
|
2023-12-09 18:09:24 +01:00
|
|
|
},
|
|
|
|
types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 2,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
|
|
|
MachineKey: machineKeys[1],
|
|
|
|
},
|
|
|
|
IsPrimary: false,
|
2024-02-03 15:30:15 +01:00
|
|
|
Enabled: true,
|
2023-12-09 18:09:24 +01:00
|
|
|
},
|
|
|
|
},
|
2024-02-08 17:28:19 +01:00
|
|
|
isConnected: map[key.MachinePublic]bool{
|
|
|
|
machineKeys[0]: false,
|
|
|
|
machineKeys[1]: true,
|
|
|
|
},
|
2023-12-09 18:09:24 +01:00
|
|
|
want: []key.MachinePublic{
|
|
|
|
machineKeys[0],
|
|
|
|
machineKeys[1],
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "failover-none-primary",
|
|
|
|
failingRoute: types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 1,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
|
|
|
MachineKey: machineKeys[0],
|
|
|
|
},
|
|
|
|
IsPrimary: false,
|
2024-02-03 15:30:15 +01:00
|
|
|
Enabled: true,
|
2023-12-09 18:09:24 +01:00
|
|
|
},
|
|
|
|
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,
|
2024-02-03 15:30:15 +01:00
|
|
|
Enabled: true,
|
2023-12-09 18:09:24 +01:00
|
|
|
},
|
|
|
|
types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 2,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
|
|
|
MachineKey: machineKeys[1],
|
|
|
|
},
|
|
|
|
IsPrimary: false,
|
2024-02-03 15:30:15 +01:00
|
|
|
Enabled: true,
|
2023-12-09 18:09:24 +01: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{
|
|
|
|
MachineKey: machineKeys[1],
|
|
|
|
},
|
|
|
|
IsPrimary: true,
|
2024-02-03 15:30:15 +01:00
|
|
|
Enabled: true,
|
2023-12-09 18:09:24 +01:00
|
|
|
},
|
|
|
|
routes: types.Routes{
|
|
|
|
types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 1,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
|
|
|
MachineKey: machineKeys[0],
|
|
|
|
},
|
|
|
|
IsPrimary: false,
|
2024-02-03 15:30:15 +01:00
|
|
|
Enabled: true,
|
2023-12-09 18:09:24 +01:00
|
|
|
},
|
|
|
|
types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 2,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
|
|
|
MachineKey: machineKeys[1],
|
|
|
|
},
|
|
|
|
IsPrimary: true,
|
2024-02-03 15:30:15 +01:00
|
|
|
Enabled: true,
|
2023-12-09 18:09:24 +01:00
|
|
|
},
|
|
|
|
types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 3,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
|
|
|
MachineKey: machineKeys[2],
|
|
|
|
},
|
|
|
|
IsPrimary: false,
|
2024-02-03 15:30:15 +01:00
|
|
|
Enabled: true,
|
2023-12-09 18:09:24 +01:00
|
|
|
},
|
|
|
|
},
|
2024-02-08 17:28:19 +01:00
|
|
|
isConnected: map[key.MachinePublic]bool{
|
|
|
|
machineKeys[0]: true,
|
|
|
|
machineKeys[1]: true,
|
|
|
|
machineKeys[2]: true,
|
|
|
|
},
|
2023-12-09 18:09:24 +01:00
|
|
|
want: []key.MachinePublic{
|
|
|
|
machineKeys[1],
|
|
|
|
machineKeys[0],
|
|
|
|
},
|
|
|
|
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{
|
|
|
|
MachineKey: machineKeys[0],
|
|
|
|
},
|
|
|
|
IsPrimary: true,
|
2024-02-03 15:30:15 +01:00
|
|
|
Enabled: true,
|
2023-12-09 18:09:24 +01:00
|
|
|
},
|
|
|
|
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,
|
2024-02-03 15:30:15 +01:00
|
|
|
Enabled: true,
|
2023-12-09 18:09:24 +01:00
|
|
|
},
|
|
|
|
// Offline
|
|
|
|
types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 2,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
|
|
|
MachineKey: machineKeys[3],
|
|
|
|
},
|
|
|
|
IsPrimary: false,
|
2024-02-03 15:30:15 +01:00
|
|
|
Enabled: true,
|
2023-12-09 18:09:24 +01:00
|
|
|
},
|
|
|
|
},
|
2024-02-08 17:28:19 +01:00
|
|
|
isConnected: map[key.MachinePublic]bool{
|
|
|
|
machineKeys[0]: true,
|
|
|
|
machineKeys[3]: false,
|
|
|
|
},
|
2023-12-09 18:09:24 +01: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{
|
|
|
|
MachineKey: machineKeys[0],
|
|
|
|
},
|
|
|
|
IsPrimary: true,
|
2024-02-03 15:30:15 +01:00
|
|
|
Enabled: true,
|
2023-12-09 18:09:24 +01:00
|
|
|
},
|
|
|
|
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,
|
2024-02-03 15:30:15 +01:00
|
|
|
Enabled: true,
|
2023-12-09 18:09:24 +01:00
|
|
|
},
|
|
|
|
// Offline
|
|
|
|
types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 2,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
|
|
|
MachineKey: machineKeys[3],
|
|
|
|
},
|
|
|
|
IsPrimary: false,
|
2024-02-03 15:30:15 +01:00
|
|
|
Enabled: true,
|
2023-12-09 18:09:24 +01:00
|
|
|
},
|
|
|
|
types.Route{
|
|
|
|
Model: gorm.Model{
|
|
|
|
ID: 3,
|
|
|
|
},
|
|
|
|
Prefix: ipp("10.0.0.0/24"),
|
|
|
|
Node: types.Node{
|
|
|
|
MachineKey: machineKeys[1],
|
|
|
|
},
|
|
|
|
IsPrimary: true,
|
2024-02-03 15:30:15 +01:00
|
|
|
Enabled: true,
|
2023-12-09 18:09:24 +01:00
|
|
|
},
|
|
|
|
},
|
2024-02-08 17:28:19 +01:00
|
|
|
isConnected: map[key.MachinePublic]bool{
|
|
|
|
machineKeys[0]: false,
|
|
|
|
machineKeys[1]: true,
|
|
|
|
machineKeys[3]: false,
|
|
|
|
},
|
2023-12-09 18:09:24 +01:00
|
|
|
want: []key.MachinePublic{
|
|
|
|
machineKeys[0],
|
|
|
|
machineKeys[1],
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
2024-02-03 15:30:15 +01:00
|
|
|
{
|
|
|
|
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,
|
|
|
|
},
|
2023-03-06 09:05:40 +01:00
|
|
|
}
|
|
|
|
|
2023-12-09 18:09:24 +01:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
tmpDir, err := os.MkdirTemp("", "failover-db-test")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
db, err = NewHeadscaleDatabase(
|
2024-02-09 07:27:00 +01:00
|
|
|
types.DatabaseConfig{
|
|
|
|
Type: "sqlite3",
|
|
|
|
Sqlite: types.SqliteConfig{
|
|
|
|
Path: tmpDir + "/headscale_test.db",
|
|
|
|
},
|
|
|
|
},
|
2023-12-09 18:09:24 +01:00
|
|
|
"",
|
|
|
|
)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
for _, route := range tt.routes {
|
2024-02-08 17:28:19 +01:00
|
|
|
if err := db.DB.Save(&route).Error; err != nil {
|
2023-12-09 18:09:24 +01:00
|
|
|
t.Fatalf("failed to create route: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-08 17:28:19 +01:00
|
|
|
got, err := Write(db.DB, func(tx *gorm.DB) ([]key.MachinePublic, error) {
|
|
|
|
return failoverRoute(tx, tt.isConnected, &tt.failingRoute)
|
|
|
|
})
|
2023-12-09 18:09:24 +01: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 09:05:40 +01:00
|
|
|
}
|
2024-02-08 17:28:19 +01:00
|
|
|
|
|
|
|
// func TestDisableRouteFailover(t *testing.T) {
|
|
|
|
// machineKeys := []key.MachinePublic{
|
|
|
|
// key.NewMachine().Public(),
|
|
|
|
// key.NewMachine().Public(),
|
|
|
|
// key.NewMachine().Public(),
|
|
|
|
// key.NewMachine().Public(),
|
|
|
|
// }
|
|
|
|
|
|
|
|
// tests := []struct {
|
|
|
|
// name string
|
|
|
|
// nodes types.Nodes
|
|
|
|
|
|
|
|
// routeID uint64
|
|
|
|
// isConnected map[key.MachinePublic]bool
|
|
|
|
|
|
|
|
// wantMachineKey key.MachinePublic
|
|
|
|
// wantErr string
|
|
|
|
// }{
|
|
|
|
// {
|
|
|
|
// name: "single-route",
|
|
|
|
// nodes: types.Nodes{
|
|
|
|
// &types.Node{
|
|
|
|
// ID: 0,
|
|
|
|
// MachineKey: machineKeys[0],
|
|
|
|
// Routes: []types.Route{
|
|
|
|
// {
|
|
|
|
// Model: gorm.Model{
|
|
|
|
// ID: 1,
|
|
|
|
// },
|
|
|
|
// Prefix: ipp("10.0.0.0/24"),
|
|
|
|
// Node: types.Node{
|
|
|
|
// MachineKey: machineKeys[0],
|
|
|
|
// },
|
|
|
|
// IsPrimary: true,
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// Hostinfo: &tailcfg.Hostinfo{
|
|
|
|
// RoutableIPs: []netip.Prefix{
|
|
|
|
// netip.MustParsePrefix("10.0.0.0/24"),
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// routeID: 1,
|
|
|
|
// wantMachineKey: machineKeys[0],
|
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// name: "failover-simple",
|
|
|
|
// nodes: types.Nodes{
|
|
|
|
// &types.Node{
|
|
|
|
// ID: 0,
|
|
|
|
// MachineKey: machineKeys[0],
|
|
|
|
// Routes: []types.Route{
|
|
|
|
// {
|
|
|
|
// Model: gorm.Model{
|
|
|
|
// ID: 1,
|
|
|
|
// },
|
|
|
|
// Prefix: ipp("10.0.0.0/24"),
|
|
|
|
// IsPrimary: true,
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// Hostinfo: &tailcfg.Hostinfo{
|
|
|
|
// RoutableIPs: []netip.Prefix{
|
|
|
|
// netip.MustParsePrefix("10.0.0.0/24"),
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// &types.Node{
|
|
|
|
// ID: 1,
|
|
|
|
// MachineKey: machineKeys[1],
|
|
|
|
// Routes: []types.Route{
|
|
|
|
// {
|
|
|
|
// Model: gorm.Model{
|
|
|
|
// ID: 2,
|
|
|
|
// },
|
|
|
|
// Prefix: ipp("10.0.0.0/24"),
|
|
|
|
// IsPrimary: false,
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// Hostinfo: &tailcfg.Hostinfo{
|
|
|
|
// RoutableIPs: []netip.Prefix{
|
|
|
|
// netip.MustParsePrefix("10.0.0.0/24"),
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// routeID: 1,
|
|
|
|
// wantMachineKey: machineKeys[1],
|
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// name: "no-failover-offline",
|
|
|
|
// nodes: types.Nodes{
|
|
|
|
// &types.Node{
|
|
|
|
// ID: 0,
|
|
|
|
// MachineKey: machineKeys[0],
|
|
|
|
// Routes: []types.Route{
|
|
|
|
// {
|
|
|
|
// Model: gorm.Model{
|
|
|
|
// ID: 1,
|
|
|
|
// },
|
|
|
|
// Prefix: ipp("10.0.0.0/24"),
|
|
|
|
// IsPrimary: true,
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// Hostinfo: &tailcfg.Hostinfo{
|
|
|
|
// RoutableIPs: []netip.Prefix{
|
|
|
|
// netip.MustParsePrefix("10.0.0.0/24"),
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// &types.Node{
|
|
|
|
// ID: 1,
|
|
|
|
// MachineKey: machineKeys[1],
|
|
|
|
// Routes: []types.Route{
|
|
|
|
// {
|
|
|
|
// Model: gorm.Model{
|
|
|
|
// ID: 2,
|
|
|
|
// },
|
|
|
|
// Prefix: ipp("10.0.0.0/24"),
|
|
|
|
// IsPrimary: false,
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// Hostinfo: &tailcfg.Hostinfo{
|
|
|
|
// RoutableIPs: []netip.Prefix{
|
|
|
|
// netip.MustParsePrefix("10.0.0.0/24"),
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// isConnected: map[key.MachinePublic]bool{
|
|
|
|
// machineKeys[0]: true,
|
|
|
|
// machineKeys[1]: false,
|
|
|
|
// },
|
|
|
|
// routeID: 1,
|
|
|
|
// wantMachineKey: machineKeys[1],
|
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// name: "failover-to-online",
|
|
|
|
// nodes: types.Nodes{
|
|
|
|
// &types.Node{
|
|
|
|
// ID: 0,
|
|
|
|
// MachineKey: machineKeys[0],
|
|
|
|
// Routes: []types.Route{
|
|
|
|
// {
|
|
|
|
// Model: gorm.Model{
|
|
|
|
// ID: 1,
|
|
|
|
// },
|
|
|
|
// Prefix: ipp("10.0.0.0/24"),
|
|
|
|
// IsPrimary: true,
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// Hostinfo: &tailcfg.Hostinfo{
|
|
|
|
// RoutableIPs: []netip.Prefix{
|
|
|
|
// netip.MustParsePrefix("10.0.0.0/24"),
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// &types.Node{
|
|
|
|
// ID: 1,
|
|
|
|
// MachineKey: machineKeys[1],
|
|
|
|
// Routes: []types.Route{
|
|
|
|
// {
|
|
|
|
// Model: gorm.Model{
|
|
|
|
// ID: 2,
|
|
|
|
// },
|
|
|
|
// Prefix: ipp("10.0.0.0/24"),
|
|
|
|
// IsPrimary: false,
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// Hostinfo: &tailcfg.Hostinfo{
|
|
|
|
// RoutableIPs: []netip.Prefix{
|
|
|
|
// netip.MustParsePrefix("10.0.0.0/24"),
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// },
|
|
|
|
// isConnected: map[key.MachinePublic]bool{
|
|
|
|
// machineKeys[0]: true,
|
|
|
|
// machineKeys[1]: true,
|
|
|
|
// },
|
|
|
|
// routeID: 1,
|
|
|
|
// wantMachineKey: machineKeys[1],
|
|
|
|
// },
|
|
|
|
// }
|
|
|
|
|
|
|
|
// for _, tt := range tests {
|
|
|
|
// t.Run(tt.name, func(t *testing.T) {
|
|
|
|
// datab, err := NewHeadscaleDatabase("sqlite3", ":memory:", false, []netip.Prefix{}, "")
|
|
|
|
// assert.NoError(t, err)
|
|
|
|
|
|
|
|
// // bootstrap db
|
|
|
|
// datab.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
|
// for _, node := range tt.nodes {
|
|
|
|
// err := tx.Save(node).Error
|
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
|
|
|
|
// _, err = SaveNodeRoutes(tx, node)
|
|
|
|
// if err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return nil
|
|
|
|
// })
|
|
|
|
|
|
|
|
// got, err := Write(datab.DB, func(tx *gorm.DB) (*types.StateUpdate, error) {
|
|
|
|
// return DisableRoute(tx, tt.routeID, tt.isConnected)
|
|
|
|
// })
|
|
|
|
|
|
|
|
// // if (err.Error() != "") != tt.wantErr {
|
|
|
|
// // t.Errorf("failoverRoute() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
|
|
|
|
// // return
|
|
|
|
// // }
|
|
|
|
|
|
|
|
// if len(got.ChangeNodes) != 1 {
|
|
|
|
// t.Errorf("expected update with one machine, got %d", len(got.ChangeNodes))
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if diff := cmp.Diff(tt.wantMachineKey, got.ChangeNodes[0].MachineKey, util.Comparers...); diff != "" {
|
|
|
|
// t.Errorf("DisableRoute() unexpected result (-want +got):\n%s", diff)
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
// }
|