Ejemplo n.º 1
0
    def test_on_batsim_notify_machine_available(self, mocker):
        s = SimulatorHandler()
        s.start("p", "w")

        # Setup
        s.platform.get(0)._set_unavailable()
        s.platform.get(1)._set_unavailable()
        s.platform.get(2)._set_unavailable()
        e = BatsimEventAPI.get_notify_machine_available(10, [0, 1, 2])
        msg = BatsimMessage(10, [NotifyBatsimEvent(10, e['data'])])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()

        assert not s.platform.get(0).is_unavailable
        assert not s.platform.get(1).is_unavailable
        assert not s.platform.get(2).is_unavailable
Ejemplo n.º 2
0
 def test_switch_ps_not_running_must_raise(self):
     s = SimulatorHandler()
     with pytest.raises(RuntimeError) as excinfo:
         s.switch_power_state(0, 0)
     assert 'running' in str(excinfo.value)
Ejemplo n.º 3
0
 def test_switch_off_not_running_must_raise(self):
     s = SimulatorHandler()
     with pytest.raises(RuntimeError) as excinfo:
         s.switch_off([0])
     assert 'running' in str(excinfo.value)
Ejemplo n.º 4
0
 def test_batsim_not_found_must_raise(self, mocker):
     mocker.patch("batsim_py.simulator.which", return_value=None)
     with pytest.raises(ImportError) as excinfo:
         SimulatorHandler()
     assert 'Batsim' in str(excinfo.value)
Ejemplo n.º 5
0
    def test_allocate_start_must_dispatch_events(self, mocker):
        def foo_j(j: Job):
            self.__j_called, self.__j_id = True, j.id

        def foo_h(h: Host):
            self.__h_called, self.__h_id = True, h.id

        self.__j_called = self.__h_called = False
        self.__j_id = self.__h_id = -1
        s = SimulatorHandler()
        s.start("p", "w")
        s.subscribe(JobEvent.STARTED, foo_j)
        s.subscribe(HostEvent.STATE_CHANGED, foo_h)

        e = BatsimEventAPI.get_job_submitted(res=1)
        msg = BatsimMessage(150, [JobSubmittedBatsimEvent(150, e['data'])])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()

        assert s.queue

        job = s.jobs[0]
        s.allocate(job.id, [0])
        assert self.__j_called and self.__j_id == job.id
        assert self.__h_called and self.__h_id == 0
Ejemplo n.º 6
0
    def test_agenda_with_job_not_running(self, mocker):
        s = SimulatorHandler()
        s.start("p", "w")

        s.switch_off([h.id for h in s.platform.hosts])

        e = BatsimEventAPI.get_job_submitted(res=1, walltime=100)
        e = JobSubmittedBatsimEvent(0, e['data'])
        msg = BatsimMessage(0, [e])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()

        s.allocate(e.job.id, [0])

        msg = BatsimMessage(10, [RequestedCallBatsimEvent(10)])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()

        agenda = list(s.agenda)
        assert s.current_time == 10
        assert agenda[0].host.id == 0 and agenda[0].release_time == e.job.walltime
Ejemplo n.º 7
0
 def test_agenda_without_platform(self, mocker):
     s = SimulatorHandler()
     assert not list(s.agenda)
Ejemplo n.º 8
0
 def test_start_already_running_must_raise(self):
     s = SimulatorHandler()
     s.start("p", "w")
     with pytest.raises(RuntimeError) as excinfo:
         s.start("p2", "w2")
     assert "running" in str(excinfo.value)
Ejemplo n.º 9
0
 def test_close_call_network_close(self, mocker):
     s = SimulatorHandler()
     mocker.patch("batsim_py.protocol.NetworkHandler.close")
     s.start("p", "w")
     s.close()
     protocol.NetworkHandler.close.assert_called_once()
Ejemplo n.º 10
0
 def test_close_not_running_must_not_raise(self):
     s = SimulatorHandler()
     try:
         s.close()
     except:
         raise pytest.fail("Close raised an exception.")  # type: ignore
Ejemplo n.º 11
0
 def test_close_valid(self):
     s = SimulatorHandler()
     s.start("p", "w")
     s.close()
     assert not s.is_running
Ejemplo n.º 12
0
 def test_start_with_simulation_time_must_setup_call_request(self, mocker):
     mocker.patch("batsim_py.simulator.CallMeLaterBatsimRequest")
     s = SimulatorHandler()
     s.start("p", "w", simulation_time=100)
     batsim_py.simulator.CallMeLaterBatsimRequest.assert_called_once_with(  # type: ignore
         0, 100+0.09)
Ejemplo n.º 13
0
    def test_start_with_simulation_time_equal_to_zero_must_raise(self):
        s = SimulatorHandler()
        with pytest.raises(ValueError) as excinfo:
            s.start("p2", "w2", simulation_time=0)

        assert "simulation_time" in str(excinfo.value)
Ejemplo n.º 14
0
 def test_start_verbosity_invalid_value_must_raise(self):
     s = SimulatorHandler()
     with pytest.raises(ValueError) as excinfo:
         s.start("p", "w", verbosity="l")  # type: ignore
     assert "verbosity" in str(excinfo.value)
Ejemplo n.º 15
0
 def test_switch_ps_not_found_must_raise(self):
     s = SimulatorHandler()
     s.start("p", "w")
     with pytest.raises(LookupError) as excinfo:
         s.switch_power_state(10, 0)
     assert 'resources' in str(excinfo.value)
Ejemplo n.º 16
0
    def test_on_batsim_job_completed_must_dispatch_event(self, mocker):
        def foo_j(j: Job):
            self.__j_called, self.__j_id = True, j.id

        def foo_h(h: Host):
            self.__h_called, self.__h_id = True, h.id

        self.__j_called = self.__h_called = False
        self.__j_id = self.__h_id = -1

        s = SimulatorHandler()
        s.start("p", "w")
        s.subscribe(HostEvent.STATE_CHANGED, foo_h)
        s.subscribe(JobEvent.COMPLETED, foo_j)

        # Setup Allocate
        e = BatsimEventAPI.get_job_submitted(res=1)
        job_id, job_alloc = e['data']['job_id'], [0]
        msg = BatsimMessage(150, [JobSubmittedBatsimEvent(150, e['data'])])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()
        s.allocate(job_id, job_alloc)

        # Setup Completed
        mocker.patch.object(batsim_py.jobs.Job, '_terminate')
        e = BatsimEventAPI.get_job_completted(100, job_id, alloc=job_alloc)
        msg = BatsimMessage(150, [JobCompletedBatsimEvent(150, e['data'])])
        mocker.patch.object(protocol.NetworkHandler, 'recv', return_value=msg)
        s.proceed_time()

        assert self.__j_called and self.__j_id == job_id
        assert self.__h_called and self.__h_id == job_alloc[0]
Ejemplo n.º 17
0
    def test_proceed_time_not_running_must_raise(self, mocker):
        s = SimulatorHandler()

        with pytest.raises(RuntimeError) as excinfo:
            s.proceed_time()
        assert "running" in str(excinfo.value)