Remove superfluous test support code. Fix bug in node list cli command.

Add tests.
This commit is contained in:
Ward Vandewege 2021-05-23 09:55:15 -04:00
parent 6a3b171e99
commit d1c3faae5f
4 changed files with 42 additions and 7 deletions

5
app.go
View File

@ -100,11 +100,6 @@ func (h *Headscale) redirect(w http.ResponseWriter, req *http.Request) {
// ExpireEphemeralNodes deletes ephemeral machine records that have not been
// seen for longer than h.cfg.EphemeralNodeInactivityTimeout
func (h *Headscale) ExpireEphemeralNodes(milliSeconds int64) {
if milliSeconds == 0 {
// For testing
h.expireEphemeralNodesWorker()
return
}
ticker := time.NewTicker(time.Duration(milliSeconds) * time.Millisecond)
for range ticker.C {
h.expireEphemeralNodesWorker()

36
cli_test.go Normal file
View File

@ -0,0 +1,36 @@
package headscale
import (
"gopkg.in/check.v1"
)
func (s *Suite) TestRegisterMachine(c *check.C) {
n, err := h.CreateNamespace("test")
c.Assert(err, check.IsNil)
db, err := h.db()
if err != nil {
c.Fatal(err)
}
defer db.Close()
m := Machine{
ID: 0,
MachineKey: "8ce002a935f8c394e55e78fbbb410576575ff8ec5cfa2e627e4b807f1be15b0e",
NodeKey: "bar",
DiscoKey: "faa",
Name: "testmachine",
NamespaceID: n.ID,
}
db.Save(&m)
_, err = h.GetMachine("test", "testmachine")
c.Assert(err, check.IsNil)
m2, err := h.RegisterMachine("8ce002a935f8c394e55e78fbbb410576575ff8ec5cfa2e627e4b807f1be15b0e", n.Name)
c.Assert(err, check.IsNil)
c.Assert(m2.Registered, check.Equals, true)
_, err = m2.GetHostInfo()
c.Assert(err, check.IsNil)
}

View File

@ -67,7 +67,11 @@ var ListNodesCmd = &cobra.Command{
fmt.Printf("name\t\tlast seen\t\tephemeral\n")
for _, m := range *machines {
fmt.Printf("%s\t%s\t%t\n", m.Name, m.LastSeen.Format("2006-01-02 15:04:05"), m.AuthKey.Ephemeral)
var ephemeral bool
if m.AuthKey != nil && m.AuthKey.Ephemeral {
ephemeral = true
}
fmt.Printf("%s\t%s\t%t\n", m.Name, m.LastSeen.Format("2006-01-02 15:04:05"), ephemeral)
}
},

View File

@ -172,7 +172,7 @@ func (*Suite) TestEphemeralKey(c *check.C) {
_, err = h.GetMachine("test7", "testest")
c.Assert(err, check.IsNil)
h.ExpireEphemeralNodes(0)
h.expireEphemeralNodesWorker()
// The machine record should have been deleted
_, err = h.GetMachine("test7", "testest")