1
0
mirror of https://github.com/juanfont/headscale.git synced 2024-12-20 19:09:07 +01:00
juanfont.headscale/routes_test.go

136 lines
3.5 KiB
Go
Raw Normal View History

2021-05-12 02:55:36 +02:00
package headscale
import (
2022-09-02 00:06:19 +02:00
"net/netip"
2021-05-12 02:55:36 +02:00
"gopkg.in/check.v1"
"tailscale.com/tailcfg"
)
func (s *Suite) TestGetRoutes(c *check.C) {
2021-11-15 17:16:04 +01:00
namespace, err := app.CreateNamespace("test")
2021-05-12 02:55:36 +02:00
c.Assert(err, check.IsNil)
2022-08-25 12:12:41 +02:00
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil)
2021-05-12 02:55:36 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
_, err = app.GetMachine("test", "test_get_route_machine")
2021-05-12 02:55:36 +02: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-12 02:55:36 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
hostInfo := tailcfg.Hostinfo{
2022-09-02 00:06:19 +02:00
RoutableIPs: []netip.Prefix{route},
2021-05-12 02:55:36 +02:00
}
2021-11-15 17:16:04 +01:00
machine := Machine{
2021-05-12 02:55:36 +02:00
ID: 0,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
2022-06-26 11:43:17 +02:00
Hostname: "test_get_route_machine",
2021-11-15 17:16:04 +01:00
NamespaceID: namespace.ID,
2021-11-18 09:49:55 +01:00
RegisterMethod: RegisterMethodAuthKey,
2021-05-12 02:55:36 +02:00
AuthKeyID: uint(pak.ID),
2022-03-01 17:34:35 +01:00
HostInfo: HostInfo(hostInfo),
2021-05-12 02:55:36 +02:00
}
2021-11-15 17:16:04 +01:00
app.db.Save(&machine)
2021-05-12 02:55:36 +02:00
2021-11-15 17:16:35 +01:00
advertisedRoutes, err := app.GetAdvertisedNodeRoutes(
"test",
"test_get_route_machine",
)
2021-05-12 02:55:36 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
c.Assert(len(*advertisedRoutes), check.Equals, 1)
2021-05-12 02:55:36 +02:00
2021-11-15 17:16:04 +01:00
err = app.EnableNodeRoute("test", "test_get_route_machine", "192.168.0.0/24")
2021-05-12 02:55:36 +02:00
c.Assert(err, check.NotNil)
2021-11-15 17:16:04 +01:00
err = app.EnableNodeRoute("test", "test_get_route_machine", "10.0.0.0/24")
c.Assert(err, check.IsNil)
}
func (s *Suite) TestGetEnableRoutes(c *check.C) {
2021-11-15 17:16:04 +01:00
namespace, err := app.CreateNamespace("test")
c.Assert(err, check.IsNil)
2022-08-25 12:12:41 +02:00
pak, err := app.CreatePreAuthKey(namespace.Name, false, false, nil, nil)
2021-05-12 02:55:36 +02:00
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
_, err = app.GetMachine("test", "test_enable_route_machine")
c.Assert(err, check.NotNil)
2022-09-02 00:06:19 +02:00
route, err := netip.ParsePrefix(
"10.0.0.0/24",
)
c.Assert(err, check.IsNil)
2022-09-02 00:06:19 +02:00
route2, err := netip.ParsePrefix(
"150.0.10.0/25",
)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
hostInfo := tailcfg.Hostinfo{
2022-09-02 00:06:19 +02:00
RoutableIPs: []netip.Prefix{route, route2},
}
2021-11-15 17:16:04 +01:00
machine := Machine{
ID: 0,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
2022-06-26 11:43:17 +02:00
Hostname: "test_enable_route_machine",
2021-11-15 17:16:04 +01:00
NamespaceID: namespace.ID,
2021-11-18 09:49:55 +01:00
RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
2022-03-01 17:34:35 +01:00
HostInfo: HostInfo(hostInfo),
}
2021-11-15 17:16:04 +01:00
app.db.Save(&machine)
2021-11-15 17:16:04 +01:00
availableRoutes, err := app.GetAdvertisedNodeRoutes(
2021-11-13 09:36:45 +01:00
"test",
"test_enable_route_machine",
)
c.Assert(err, check.IsNil)
c.Assert(len(*availableRoutes), check.Equals, 2)
2021-11-15 17:16:35 +01:00
noEnabledRoutes, err := app.GetEnabledNodeRoutes(
"test",
"test_enable_route_machine",
)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
c.Assert(len(noEnabledRoutes), check.Equals, 0)
2021-11-15 17:16:04 +01:00
err = app.EnableNodeRoute("test", "test_enable_route_machine", "192.168.0.0/24")
c.Assert(err, check.NotNil)
2021-11-15 17:16:04 +01:00
err = app.EnableNodeRoute("test", "test_enable_route_machine", "10.0.0.0/24")
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
enabledRoutes, err := app.GetEnabledNodeRoutes("test", "test_enable_route_machine")
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
c.Assert(len(enabledRoutes), check.Equals, 1)
// Adding it twice will just let it pass through
2021-11-15 17:16:04 +01:00
err = app.EnableNodeRoute("test", "test_enable_route_machine", "10.0.0.0/24")
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
enableRoutesAfterDoubleApply, err := app.GetEnabledNodeRoutes(
"test",
"test_enable_route_machine",
)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
c.Assert(len(enableRoutesAfterDoubleApply), check.Equals, 1)
2021-11-15 17:16:04 +01:00
err = app.EnableNodeRoute("test", "test_enable_route_machine", "150.0.10.0/25")
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
enabledRoutesWithAdditionalRoute, err := app.GetEnabledNodeRoutes(
"test",
"test_enable_route_machine",
)
c.Assert(err, check.IsNil)
2021-11-15 17:16:04 +01:00
c.Assert(len(enabledRoutesWithAdditionalRoute), check.Equals, 2)
2021-05-12 02:55:36 +02:00
}