From 4e686f8b77b70c94a67e3487db4f9b39eeb94ea9 Mon Sep 17 00:00:00 2001 From: Igor Perepilitsyn Date: Sun, 1 May 2022 21:40:18 +0400 Subject: [PATCH] add unit test --- namespaces_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/namespaces_test.go b/namespaces_test.go index 5d873bd6..6baf0d91 100644 --- a/namespaces_test.go +++ b/namespaces_test.go @@ -372,3 +372,38 @@ func TestCheckForFQDNRules(t *testing.T) { }) } } + +func (s *Suite) TestSetMachineNamespace(c *check.C) { + oldNamespace, err := app.CreateNamespace("old") + c.Assert(err, check.IsNil) + + newNamespace, err := app.CreateNamespace("new") + c.Assert(err, check.IsNil) + + pak, err := app.CreatePreAuthKey(oldNamespace.Name, false, false, nil) + c.Assert(err, check.IsNil) + + machine := Machine{ + ID: 0, + MachineKey: "foo", + NodeKey: "bar", + DiscoKey: "faa", + Name: "testmachine", + NamespaceID: oldNamespace.ID, + RegisterMethod: RegisterMethodAuthKey, + AuthKeyID: uint(pak.ID), + } + app.db.Save(&machine) + c.Assert(machine.NamespaceID, check.Equals, oldNamespace.ID) + + err = app.SetMachineNamespace(&machine, newNamespace.Name) + c.Assert(err, check.IsNil) + c.Assert(machine.NamespaceID, check.Equals, newNamespace.ID) + + err = app.SetMachineNamespace(&machine, "non-existing-namespace") + c.Assert(err, check.Equals, errNamespaceNotFound) + + err = app.SetMachineNamespace(&machine, newNamespace.Name) + c.Assert(err, check.IsNil) + c.Assert(machine.NamespaceID, check.Equals, newNamespace.ID) +}