def add_vm_to_pool(self, vm_ip, vm_name, group): """ Adds newly spawned VM into the pool of available builders :param str vm_ip: IP :param str vm_name: VM name :param group: builder group :type group: int :rtype: VmDescriptor """ # print("\n ADD VM TO POOL") if self.rc.sismember(KEY_VM_POOL.format(group=group), vm_name): raise VmError( "Can't add VM `{}` to the pool, such name already used".format( vm_name)) vmd = VmDescriptor(vm_ip, vm_name, group, VmStates.GOT_IP) # print("VMD: {}".format(vmd)) pipe = self.rc.pipeline() pipe.sadd(KEY_VM_POOL.format(group=group), vm_name) pipe.hmset(KEY_VM_INSTANCE.format(vm_name=vm_name), vmd.to_dict()) pipe.execute() self.log.info("registered new VM: {} {}".format( vmd.vm_name, vmd.vm_ip)) return vmd
def remove_vm_from_pool(self, vm_name): """ Backend forgets about VM after this method :raises VmError: if VM has wrong state """ vmd = self.get_vm_by_name(vm_name) if vmd.get_field(self.rc, "state") != VmStates.TERMINATING: raise VmError("VM should have `terminating` state to be removable") pipe = self.rc.pipeline() pipe.srem(KEY_VM_POOL.format(group=vmd.group), vm_name) pipe.delete(KEY_VM_INSTANCE.format(vm_name=vm_name)) pipe.execute() self.log.info("removed vm `{}` from pool".format(vm_name))
def test_dummy_run(self, mc_time, mc_setproctitle): mc_do_cycle = MagicMock() mc_do_cycle.side_effect = [VmError("FooBar"), None] self.vm_master.do_cycle = types.MethodType(mc_do_cycle, self.vm_master) self.vmm.mark_server_start = MagicMock() self.stage = 0 def on_sleep(*args, **kwargs): self.stage += 1 if self.stage == 1: pass elif self.stage >= 2: self.vm_master.kill_received = True mc_time.sleep.side_effect = on_sleep self.vm_master.run()
def test_run_cycle(self, init_worker, mc_time): self.worker.update_process_title = MagicMock() self.worker.obtain_job = MagicMock() self.worker.do_job = MagicMock() self.worker.notify_job_grab_about_task_end = MagicMock() self.worker.obtain_job.return_value = None self.worker.run_cycle() assert self.worker.obtain_job.called assert mc_time.sleep.called assert not mc_time.time.called vmd = VmDescriptor(self.vm_ip, self.vm_name, 0, "ready") vmd.vm_ip = self.vm_ip vmd.vm_name = self.vm_name self.worker.obtain_job.return_value = self.job self.worker.vmm.acquire_vm.side_effect = [ IOError(), None, NoVmAvailable("foobar"), vmd, ] self.worker.run_cycle() assert not self.worker.do_job.called assert self.worker.notify_job_grab_about_task_end.called_once assert self.worker.notify_job_grab_about_task_end.call_args[1]["do_reschedule"] self.worker.notify_job_grab_about_task_end.reset_mock() ### normal work def on_release_vm(*args, **kwargs): assert self.worker.vm_ip == self.vm_ip assert self.worker.vm_name == self.vm_name self.worker.vmm.release_vm.side_effect = on_release_vm self.worker.run_cycle() assert self.worker.do_job.called_once assert self.worker.notify_job_grab_about_task_end.called_once assert not self.worker.notify_job_grab_about_task_end.call_args[1].get("do_reschedule") assert self.worker.vmm.release_vm.called self.worker.vmm.acquire_vm = MagicMock() self.worker.vmm.acquire_vm.return_value = vmd ### handle VmError self.worker.notify_job_grab_about_task_end.reset_mock() self.worker.vmm.release_vm.reset_mock() self.worker.do_job.side_effect = VmError("foobar") self.worker.run_cycle() assert self.worker.notify_job_grab_about_task_end.call_args[1]["do_reschedule"] assert self.worker.vmm.release_vm.called ### handle other errors self.worker.notify_job_grab_about_task_end.reset_mock() self.worker.vmm.release_vm.reset_mock() self.worker.do_job.side_effect = IOError() self.worker.run_cycle() assert self.worker.notify_job_grab_about_task_end.call_args[1]["do_reschedule"] assert self.worker.vmm.release_vm.called