diff --git a/api.go b/api.go index 914a9382..b4f59cd1 100644 --- a/api.go +++ b/api.go @@ -40,7 +40,7 @@ func (h *Headscale) RegisterWebAPI(c *gin.Context) {
- Run the command below in the headscale server to add this machine to your network: + Run the command below on the headscale server to add this machine to your network:
diff --git a/machine_test.go b/machine_test.go new file mode 100644 index 00000000..d2117d16 --- /dev/null +++ b/machine_test.go @@ -0,0 +1,44 @@ +package headscale + +import ( + "gopkg.in/check.v1" +) + +var _ = check.Suite(&Suite{}) + +func (s *Suite) TestGetMachine(c *check.C) { + n, err := h.CreateNamespace("test") + c.Assert(err, check.IsNil) + + pak, err := h.CreatePreAuthKey(n.Name, false, nil) + c.Assert(err, check.IsNil) + + db, err := h.db() + if err != nil { + c.Fatal(err) + } + defer db.Close() + + _, err = h.GetMachine("test", "testmachine") + c.Assert(err, check.NotNil) + + m := Machine{ + ID: 0, + MachineKey: "foo", + NodeKey: "bar", + DiscoKey: "faa", + Name: "testmachine", + NamespaceID: n.ID, + Registered: true, + RegisterMethod: "authKey", + AuthKeyID: uint(pak.ID), + } + db.Save(&m) + + m1, err := h.GetMachine("test", "testmachine") + c.Assert(err, check.IsNil) + + _, err = m1.GetHostInfo() + c.Assert(err, check.IsNil) + +} diff --git a/namespaces_test.go b/namespaces_test.go index 42ab4879..23263685 100644 --- a/namespaces_test.go +++ b/namespaces_test.go @@ -1,8 +1,6 @@ package headscale import ( - //_ "github.com/jinzhu/gorm/dialects/sqlite" // sql driver - "gopkg.in/check.v1" ) diff --git a/routes_test.go b/routes_test.go new file mode 100644 index 00000000..b43e427d --- /dev/null +++ b/routes_test.go @@ -0,0 +1,63 @@ +package headscale + +import ( + "encoding/json" + + "github.com/jinzhu/gorm/dialects/postgres" + "gopkg.in/check.v1" + "inet.af/netaddr" + "tailscale.com/tailcfg" +) + +var _ = check.Suite(&Suite{}) + +func (s *Suite) TestGetRoutes(c *check.C) { + n, err := h.CreateNamespace("test") + c.Assert(err, check.IsNil) + + pak, err := h.CreatePreAuthKey(n.Name, false, nil) + c.Assert(err, check.IsNil) + + db, err := h.db() + if err != nil { + c.Fatal(err) + } + defer db.Close() + + _, 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), + HostInfo: postgres.Jsonb{RawMessage: json.RawMessage(hostinfo)}, + } + db.Save(&m) + + 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) + +}