From: Aleksander Mistewicz Date: Thu, 8 Jun 2017 12:51:47 +0000 (+0200) Subject: Implement worker removal from the pool X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7d5188c948d29597adf13d780833d665673a96a8;p=tools%2Fboruta.git Implement worker removal from the pool When Worker will not be used anymore, it should be removed in order to free the resources. Administrator should first put Worker in the MAINTENANCE state, using SetState, and then remove it, using Deregister. Change-Id: Ic9bb5e705418fae664857f3f541e9543ca7da9e2 Signed-off-by: Aleksander Mistewicz Reviewed-on: https://mcdsrvbld02.digital.local/review/49058 Reviewed-by: Maciej Wereski Tested-by: Maciej Wereski --- diff --git a/workers/error.go b/workers/error.go index 1b0f7d6..bd51509 100644 --- a/workers/error.go +++ b/workers/error.go @@ -30,4 +30,7 @@ var ( ErrWorkerNotFound = errors.New("Worker is not present on the list") // ErrInMaintenance is returned when SetFail has been called for Worker in MAINTENANCE state. ErrInMaintenance = errors.New("It is forbidden to set FAIL state when Worker is in MAINTENANCE state") + // ErrNotInMaintenance is returned when Deregister is called for a worker not in MAINTENANCE state. + // Only workers in MAINTENANCE state can be removed from the list. + ErrNotInMaintenance = errors.New("Worker is not in MAINTENANCE state") ) diff --git a/workers/worker_list_test.go b/workers/worker_list_test.go index 562f452..2800cbf 100644 --- a/workers/worker_list_test.go +++ b/workers/worker_list_test.go @@ -173,5 +173,37 @@ var _ = Describe("WorkerList", func() { Expect(wl.workers[worker].State).To(Equal(MAINTENANCE)) }) }) + + Describe("Deregister", func() { + It("should fail to deregister nonexistent worker", func() { + uuid := randomUUID() + err := wl.Deregister(uuid) + Expect(err).To(Equal(ErrWorkerNotFound)) + }) + + It("should work to deregister", func() { + err := wl.Deregister(worker) + Expect(err).ToNot(HaveOccurred()) + Expect(wl.workers).ToNot(HaveKey(worker)) + }) + + It("should fail to deregister same worker twice", func() { + err := wl.Deregister(worker) + Expect(err).ToNot(HaveOccurred()) + Expect(wl.workers).ToNot(HaveKey(worker)) + + err = wl.Deregister(worker) + Expect(err).To(Equal(ErrWorkerNotFound)) + }) + + It("should fail to deregister worker not in MAINTENANCE state", func() { + for _, state := range []WorkerState{IDLE, RUN, FAIL} { + wl.workers[worker].State = state + err := wl.Deregister(worker) + Expect(err).To(Equal(ErrNotInMaintenance)) + Expect(wl.workers).To(HaveKey(worker)) + } + }) + }) }) }) diff --git a/workers/workers.go b/workers/workers.go index 3c27582..ff21f09 100644 --- a/workers/workers.go +++ b/workers/workers.go @@ -88,7 +88,15 @@ func (wl *WorkerList) SetGroups(uuid WorkerUUID, groups Groups) error { // Deregister is an implementation of Deregister from Workers interface. func (wl *WorkerList) Deregister(uuid WorkerUUID) error { - return ErrNotImplemented + worker, ok := wl.workers[uuid] + if !ok { + return ErrWorkerNotFound + } + if worker.State != MAINTENANCE { + return ErrNotInMaintenance + } + delete(wl.workers, uuid) + return nil } // ListWorkers is an implementation of ListWorkers from Workers interface. diff --git a/workers/workers_test.go b/workers/workers_test.go index c6cc3e6..dc2fed5 100644 --- a/workers/workers_test.go +++ b/workers/workers_test.go @@ -46,10 +46,6 @@ var _ = Describe("WorkerList", func() { err = wl.SetGroups(uuid, groups) Expect(err).To(Equal(ErrNotImplemented)) - By("Deregister") - err = wl.Deregister(uuid) - Expect(err).To(Equal(ErrNotImplemented)) - By("ListWorkers") _, err = wl.ListWorkers(groups, caps) Expect(err).To(Equal(ErrNotImplemented))