From d3349aa4d13fca1ad6ef28424657d8e6219b1aab Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Tue, 3 Aug 2021 09:26:28 +0100 Subject: [PATCH] Add test to ensure we can deal with empty ips from database --- utils_test.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/utils_test.go b/utils_test.go index 439fdc19..8fbe1375 100644 --- a/utils_test.go +++ b/utils_test.go @@ -117,3 +117,39 @@ func (s *Suite) TestGetMultiIp(c *check.C) { c.Assert(nextIP2.String(), check.Equals, expectedNextIP.String()) } + +func (s *Suite) TestGetAvailableIpMachineWithoutIP(c *check.C) { + ip, err := h.getAvailableIP() + c.Assert(err, check.IsNil) + + expected := netaddr.MustParseIP("10.27.0.0") + + c.Assert(ip.String(), check.Equals, expected.String()) + + n, err := h.CreateNamespace("test_ip") + c.Assert(err, check.IsNil) + + pak, err := h.CreatePreAuthKey(n.Name, false, false, nil) + c.Assert(err, check.IsNil) + + _, 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), + } + h.db.Save(&m) + + ip2, err := h.getAvailableIP() + c.Assert(err, check.IsNil) + + c.Assert(ip2.String(), check.Equals, expected.String()) +}