headscale/routes_test.go

56 lines
1.2 KiB
Go
Raw Permalink Normal View History

2021-05-12 00:55:36 +00:00
package headscale
import (
"encoding/json"
"gopkg.in/check.v1"
2021-05-14 22:05:41 +00:00
"gorm.io/datatypes"
2021-05-12 00:55:36 +00: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-12 00:55:36 +00:00
c.Assert(err, check.IsNil)
_, err = h.GetMachine("test", "testmachine")
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: "testmachine",
NamespaceID: n.ID,
Registered: true,
RegisterMethod: "authKey",
AuthKeyID: uint(pak.ID),
2021-05-14 22:05:41 +00:00
HostInfo: datatypes.JSON(hostinfo),
2021-05-12 00:55:36 +00:00
}
2021-07-04 19:40:46 +00:00
h.db.Save(&m)
2021-05-12 00:55:36 +00:00
r, err := h.GetNodeRoutes("test", "testmachine")
c.Assert(err, check.IsNil)
c.Assert(len(*r), check.Equals, 1)
_, err = h.EnableNodeRoute("test", "testmachine", "192.168.0.0/24")
c.Assert(err, check.NotNil)
_, err = h.EnableNodeRoute("test", "testmachine", "10.0.0.0/24")
c.Assert(err, check.IsNil)
}