From d1c3faae5f515dcdf53ee4eefe3b58183a833257 Mon Sep 17 00:00:00 2001 From: Ward Vandewege Date: Sun, 23 May 2021 09:55:15 -0400 Subject: [PATCH] Remove superfluous test support code. Fix bug in node list cli command. Add tests. --- app.go | 5 ----- cli_test.go | 36 ++++++++++++++++++++++++++++++++++++ cmd/headscale/cli/nodes.go | 6 +++++- preauth_keys_test.go | 2 +- 4 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 cli_test.go diff --git a/app.go b/app.go index 680ecc9a..947980d3 100644 --- a/app.go +++ b/app.go @@ -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() diff --git a/cli_test.go b/cli_test.go new file mode 100644 index 00000000..d15e60fa --- /dev/null +++ b/cli_test.go @@ -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) +} diff --git a/cmd/headscale/cli/nodes.go b/cmd/headscale/cli/nodes.go index fff292cc..e6f5876a 100644 --- a/cmd/headscale/cli/nodes.go +++ b/cmd/headscale/cli/nodes.go @@ -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) } }, diff --git a/preauth_keys_test.go b/preauth_keys_test.go index e2569432..ebf7ea4e 100644 --- a/preauth_keys_test.go +++ b/preauth_keys_test.go @@ -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")