diff options
-rw-r--r-- | workers/error.go | 3 | ||||
-rw-r--r-- | workers/worker_list_test.go | 32 | ||||
-rw-r--r-- | workers/workers.go | 10 | ||||
-rw-r--r-- | workers/workers_test.go | 4 |
4 files changed, 44 insertions, 5 deletions
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)) |