Skip to content

Commit e7a626d

Browse files
authored
Merge pull request #26 from cure/more_tests
Add more tests
2 parents 0f933c1 + be83281 commit e7a626d

File tree

6 files changed

+111
-5
lines changed

6 files changed

+111
-5
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ jobs:
1818
# below, but it's still much faster in the end than installing
1919
# golangci-lint manually in the `Run lint` step.
2020
- uses: golangci/golangci-lint-action@v2
21+
with:
22+
args: --timeout 2m
2123

2224
# Setup Go
2325
- name: Setup Go
@@ -29,7 +31,7 @@ jobs:
2931
- name: Install dependencies
3032
run: |
3133
go version
32-
go get -u golang.org/x/lint/golint
34+
go install golang.org/x/lint/golint@latest
3335
sudo apt update
3436
sudo apt install -y make
3537

api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (h *Headscale) RegisterWebAPI(c *gin.Context) {
4040
<body>
4141
<h1>headscale</h1>
4242
<p>
43-
Run the command below in the headscale server to add this machine to your network:
43+
Run the command below on the headscale server to add this machine to your network:
4444
</p>
4545
4646
<p>

go.sum

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaO
296296
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
297297
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
298298
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
299-
github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/QA=
300299
github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus=
301300
github.com/mattn/go-sqlite3 v1.14.7 h1:fxWBnXkxfM6sRiuH3bqJ4CfzZojMOLVc0UTsTglEghA=
302301
github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=

machine_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package headscale
2+
3+
import (
4+
"gopkg.in/check.v1"
5+
)
6+
7+
var _ = check.Suite(&Suite{})
8+
9+
func (s *Suite) TestGetMachine(c *check.C) {
10+
n, err := h.CreateNamespace("test")
11+
c.Assert(err, check.IsNil)
12+
13+
pak, err := h.CreatePreAuthKey(n.Name, false, nil)
14+
c.Assert(err, check.IsNil)
15+
16+
db, err := h.db()
17+
if err != nil {
18+
c.Fatal(err)
19+
}
20+
defer db.Close()
21+
22+
_, err = h.GetMachine("test", "testmachine")
23+
c.Assert(err, check.NotNil)
24+
25+
m := Machine{
26+
ID: 0,
27+
MachineKey: "foo",
28+
NodeKey: "bar",
29+
DiscoKey: "faa",
30+
Name: "testmachine",
31+
NamespaceID: n.ID,
32+
Registered: true,
33+
RegisterMethod: "authKey",
34+
AuthKeyID: uint(pak.ID),
35+
}
36+
db.Save(&m)
37+
38+
m1, err := h.GetMachine("test", "testmachine")
39+
c.Assert(err, check.IsNil)
40+
41+
_, err = m1.GetHostInfo()
42+
c.Assert(err, check.IsNil)
43+
44+
}

namespaces_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package headscale
22

33
import (
4-
//_ "github.com/jinzhu/gorm/dialects/sqlite" // sql driver
5-
64
"gopkg.in/check.v1"
75
)
86

routes_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package headscale
2+
3+
import (
4+
"encoding/json"
5+
6+
"github.com/jinzhu/gorm/dialects/postgres"
7+
"gopkg.in/check.v1"
8+
"inet.af/netaddr"
9+
"tailscale.com/tailcfg"
10+
)
11+
12+
var _ = check.Suite(&Suite{})
13+
14+
func (s *Suite) TestGetRoutes(c *check.C) {
15+
n, err := h.CreateNamespace("test")
16+
c.Assert(err, check.IsNil)
17+
18+
pak, err := h.CreatePreAuthKey(n.Name, false, nil)
19+
c.Assert(err, check.IsNil)
20+
21+
db, err := h.db()
22+
if err != nil {
23+
c.Fatal(err)
24+
}
25+
defer db.Close()
26+
27+
_, err = h.GetMachine("test", "testmachine")
28+
c.Assert(err, check.NotNil)
29+
30+
route, err := netaddr.ParseIPPrefix("10.0.0.0/24")
31+
c.Assert(err, check.IsNil)
32+
33+
hi := tailcfg.Hostinfo{
34+
RoutableIPs: []netaddr.IPPrefix{route},
35+
}
36+
hostinfo, err := json.Marshal(hi)
37+
c.Assert(err, check.IsNil)
38+
39+
m := Machine{
40+
ID: 0,
41+
MachineKey: "foo",
42+
NodeKey: "bar",
43+
DiscoKey: "faa",
44+
Name: "testmachine",
45+
NamespaceID: n.ID,
46+
Registered: true,
47+
RegisterMethod: "authKey",
48+
AuthKeyID: uint(pak.ID),
49+
HostInfo: postgres.Jsonb{RawMessage: json.RawMessage(hostinfo)},
50+
}
51+
db.Save(&m)
52+
53+
r, err := h.GetNodeRoutes("test", "testmachine")
54+
c.Assert(err, check.IsNil)
55+
c.Assert(len(*r), check.Equals, 1)
56+
57+
_, err = h.EnableNodeRoute("test", "testmachine", "192.168.0.0/24")
58+
c.Assert(err, check.NotNil)
59+
60+
_, err = h.EnableNodeRoute("test", "testmachine", "10.0.0.0/24")
61+
c.Assert(err, check.IsNil)
62+
63+
}

0 commit comments

Comments
 (0)