Refactor unit tests

This commit is contained in:
Juan Font
2023-05-01 14:53:23 +00:00
parent 851da9d674
commit 9f7c25e853
8 changed files with 483 additions and 483 deletions

View File

@@ -28,21 +28,21 @@ func (s *Suite) TestGetUsedIps(c *check.C) {
pak, err := app.CreatePreAuthKey(user.Name, false, false, nil, nil)
c.Assert(err, check.IsNil)
_, err = app.GetMachine("test", "testmachine")
_, err = app.GetNode("test", "testnode")
c.Assert(err, check.NotNil)
machine := Machine{
node := Node{
ID: 0,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Hostname: "testmachine",
Hostname: "testnode",
UserID: user.ID,
RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
IPAddresses: ips,
}
app.db.Save(&machine)
app.db.Save(&node)
usedIps, err := app.getUsedIPs()
@@ -56,11 +56,11 @@ func (s *Suite) TestGetUsedIps(c *check.C) {
c.Assert(usedIps.Equal(expectedIPSet), check.Equals, true)
c.Assert(usedIps.Contains(expected), check.Equals, true)
machine1, err := app.GetMachineByID(0)
node1, err := app.GetNodeByID(0)
c.Assert(err, check.IsNil)
c.Assert(len(machine1.IPAddresses), check.Equals, 1)
c.Assert(machine1.IPAddresses[0], check.Equals, expected)
c.Assert(len(node1.IPAddresses), check.Equals, 1)
c.Assert(node1.IPAddresses[0], check.Equals, expected)
}
func (s *Suite) TestGetMultiIp(c *check.C) {
@@ -76,21 +76,21 @@ func (s *Suite) TestGetMultiIp(c *check.C) {
pak, err := app.CreatePreAuthKey(user.Name, false, false, nil, nil)
c.Assert(err, check.IsNil)
_, err = app.GetMachine("test", "testmachine")
_, err = app.GetNode("test", "testnode")
c.Assert(err, check.NotNil)
machine := Machine{
node := Node{
ID: uint64(index),
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Hostname: "testmachine",
Hostname: "testnode",
UserID: user.ID,
RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
IPAddresses: ips,
}
app.db.Save(&machine)
app.db.Save(&node)
app.ipAllocationMutex.Unlock()
}
@@ -117,20 +117,20 @@ func (s *Suite) TestGetMultiIp(c *check.C) {
c.Assert(usedIps.Contains(expected300), check.Equals, true)
// Check that we can read back the IPs
machine1, err := app.GetMachineByID(1)
node1, err := app.GetNodeByID(1)
c.Assert(err, check.IsNil)
c.Assert(len(machine1.IPAddresses), check.Equals, 1)
c.Assert(len(node1.IPAddresses), check.Equals, 1)
c.Assert(
machine1.IPAddresses[0],
node1.IPAddresses[0],
check.Equals,
netip.MustParseAddr("10.27.0.1"),
)
machine50, err := app.GetMachineByID(50)
node50, err := app.GetNodeByID(50)
c.Assert(err, check.IsNil)
c.Assert(len(machine50.IPAddresses), check.Equals, 1)
c.Assert(len(node50.IPAddresses), check.Equals, 1)
c.Assert(
machine50.IPAddresses[0],
node50.IPAddresses[0],
check.Equals,
netip.MustParseAddr("10.27.0.50"),
)
@@ -151,7 +151,7 @@ func (s *Suite) TestGetMultiIp(c *check.C) {
c.Assert(nextIP2[0].String(), check.Equals, expectedNextIP.String())
}
func (s *Suite) TestGetAvailableIpMachineWithoutIP(c *check.C) {
func (s *Suite) TestGetAvailableIpNodeWithoutIP(c *check.C) {
ips, err := app.getAvailableIPs()
c.Assert(err, check.IsNil)
@@ -166,20 +166,20 @@ func (s *Suite) TestGetAvailableIpMachineWithoutIP(c *check.C) {
pak, err := app.CreatePreAuthKey(user.Name, false, false, nil, nil)
c.Assert(err, check.IsNil)
_, err = app.GetMachine("test", "testmachine")
_, err = app.GetNode("test", "testnode")
c.Assert(err, check.NotNil)
machine := Machine{
node := Node{
ID: 0,
MachineKey: "foo",
NodeKey: "bar",
DiscoKey: "faa",
Hostname: "testmachine",
Hostname: "testnode",
UserID: user.ID,
RegisterMethod: RegisterMethodAuthKey,
AuthKeyID: uint(pak.ID),
}
app.db.Save(&machine)
app.db.Save(&node)
ips2, err := app.getAvailableIPs()
c.Assert(err, check.IsNil)