diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3a145b96..b75dbcc1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,6 +18,8 @@ jobs: # below, but it's still much faster in the end than installing # golangci-lint manually in the `Run lint` step. - uses: golangci/golangci-lint-action@v2 + with: + args: --timeout 2m # Setup Go - name: Setup Go @@ -29,7 +31,7 @@ jobs: - name: Install dependencies run: | go version - go get -u golang.org/x/lint/golint + go install golang.org/x/lint/golint@latest sudo apt update sudo apt install -y make 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) {

headscale

- 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/go.sum b/go.sum index edae5615..e86e6041 100644 --- a/go.sum +++ b/go.sum @@ -296,7 +296,6 @@ github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaO github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= -github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/QA= github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus= github.com/mattn/go-sqlite3 v1.14.7 h1:fxWBnXkxfM6sRiuH3bqJ4CfzZojMOLVc0UTsTglEghA= github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= 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) + +}