headscale/routes_test.go

129 lines
3.5 KiB
Go
Raw Permalink Normal View History

2021-05-11 20:55:36 -04:00
package headscale
import (
"encoding/json"
"gopkg.in/check.v1"
2021-05-15 00:05:41 +02:00
"gorm.io/datatypes"
2021-05-11 20:55:36 -04:00
"inet.af/netaddr"
"tailscale.com/tailcfg"
)
func (s *Suite) TestGetRoutes(c *check.C) {
n, err := h.CreateNamespace("test")
c.Assert(err, check.IsNil)
pak, err := h.CreatePreAuthKey(n.Name, false, false, nil)
2021-05-11 20:55:36 -04:00
c.Assert(err, check.IsNil)
2021-08-21 15:35:26 +01:00
_, err = h.GetMachine("test", "test_get_route_machine")
2021-05-11 20:55:36 -04:00
c.Assert(err, check.NotNil)
route, err := netaddr.ParseIPPrefix("10.0.0.0/24")
c.Assert(err, check.IsNil)
hi := tailcfg.Hostinfo{
RoutableIPs: []netaddr.IPPrefix{route},
}
hostinfo, err := json.Marshal(hi)
c.Assert(err, check.IsNil)
m := Machine{
ID: 0,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Name: "test_get_route_machine",
2021-05-11 20:55:36 -04:00
NamespaceID: n.ID,
Registered: true,
RegisterMethod: "authKey",
AuthKeyID: uint(pak.ID),
2021-05-15 00:05:41 +02:00
HostInfo: datatypes.JSON(hostinfo),
2021-05-11 20:55:36 -04:00
}
2021-07-04 21:40:46 +02:00
h.db.Save(&m)
2021-05-11 20:55:36 -04:00
2021-08-21 15:35:26 +01:00
r, err := h.GetAdvertisedNodeRoutes("test", "test_get_route_machine")
2021-05-11 20:55:36 -04:00
c.Assert(err, check.IsNil)
c.Assert(len(*r), check.Equals, 1)
2021-08-21 15:35:26 +01:00
err = h.EnableNodeRoute("test", "test_get_route_machine", "192.168.0.0/24")
2021-05-11 20:55:36 -04:00
c.Assert(err, check.NotNil)
2021-08-21 15:35:26 +01:00
err = h.EnableNodeRoute("test", "test_get_route_machine", "10.0.0.0/24")
c.Assert(err, check.IsNil)
}
func (s *Suite) TestGetEnableRoutes(c *check.C) {
n, err := h.CreateNamespace("test")
c.Assert(err, check.IsNil)
pak, err := h.CreatePreAuthKey(n.Name, false, false, nil)
2021-05-11 20:55:36 -04:00
c.Assert(err, check.IsNil)
2021-08-21 15:35:26 +01:00
_, err = h.GetMachine("test", "test_enable_route_machine")
c.Assert(err, check.NotNil)
route, err := netaddr.ParseIPPrefix(
"10.0.0.0/24",
)
c.Assert(err, check.IsNil)
route2, err := netaddr.ParseIPPrefix(
"150.0.10.0/25",
)
c.Assert(err, check.IsNil)
hi := tailcfg.Hostinfo{
RoutableIPs: []netaddr.IPPrefix{route, route2},
}
hostinfo, err := json.Marshal(hi)
c.Assert(err, check.IsNil)
m := Machine{
ID: 0,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Name: "test_enable_route_machine",
NamespaceID: n.ID,
Registered: true,
RegisterMethod: "authKey",
AuthKeyID: uint(pak.ID),
HostInfo: datatypes.JSON(hostinfo),
}
h.db.Save(&m)
2021-08-21 15:35:26 +01:00
availableRoutes, err := h.GetAdvertisedNodeRoutes("test", "test_enable_route_machine")
c.Assert(err, check.IsNil)
c.Assert(len(*availableRoutes), check.Equals, 2)
2021-08-21 15:35:26 +01:00
enabledRoutes, err := h.GetEnabledNodeRoutes("test", "test_enable_route_machine")
c.Assert(err, check.IsNil)
c.Assert(len(enabledRoutes), check.Equals, 0)
2021-08-21 15:35:26 +01:00
err = h.EnableNodeRoute("test", "test_enable_route_machine", "192.168.0.0/24")
c.Assert(err, check.NotNil)
2021-08-21 15:35:26 +01:00
err = h.EnableNodeRoute("test", "test_enable_route_machine", "10.0.0.0/24")
c.Assert(err, check.IsNil)
2021-08-21 15:35:26 +01:00
enabledRoutes1, err := h.GetEnabledNodeRoutes("test", "test_enable_route_machine")
c.Assert(err, check.IsNil)
c.Assert(len(enabledRoutes1), check.Equals, 1)
// Adding it twice will just let it pass through
2021-08-21 15:35:26 +01:00
err = h.EnableNodeRoute("test", "test_enable_route_machine", "10.0.0.0/24")
c.Assert(err, check.IsNil)
2021-08-21 15:35:26 +01:00
enabledRoutes2, err := h.GetEnabledNodeRoutes("test", "test_enable_route_machine")
c.Assert(err, check.IsNil)
c.Assert(len(enabledRoutes2), check.Equals, 1)
2021-08-21 15:35:26 +01:00
err = h.EnableNodeRoute("test", "test_enable_route_machine", "150.0.10.0/25")
c.Assert(err, check.IsNil)
2021-08-21 15:35:26 +01:00
enabledRoutes3, err := h.GetEnabledNodeRoutes("test", "test_enable_route_machine")
c.Assert(err, check.IsNil)
c.Assert(len(enabledRoutes3), check.Equals, 2)
2021-05-11 20:55:36 -04:00
}