def test_get_not_allocated_hosts(self): h1 = Host(0, "n") h2 = Host(1, "n") p = Platform([h1, h2]) h1._allocate("j1") assert p.get_not_allocated_hosts() == [h2]
def test_state(self): p1 = PowerState(0, PowerStateType.SLEEP, 10, 10) p2 = PowerState(1, PowerStateType.COMPUTATION, 10, 100) p3 = PowerState(2, PowerStateType.COMPUTATION, 15, 150) p4 = PowerState(3, PowerStateType.SWITCHING_OFF, 50, 50) p5 = PowerState(4, PowerStateType.SWITCHING_ON, 75, 75) h1 = Host(0, "n", pstates=[p1, p3, p2, p4, p5]) h2 = Host(1, "n") p = Platform([h1, h2]) h1._switch_off() assert p.state == [HostState.SWITCHING_OFF, HostState.IDLE]
def monitor(self, mock_simulator): monitor = ConsumedEnergyMonitor(mock_simulator) mock_simulator.current_time = 0 watt_on = [(100, 200), (200, 300)] pstates = BatsimPlatformAPI.get_resource_properties(watt_on=watt_on) pstates = Converters.json_to_power_states(pstates) hosts = [ Host(0, "0", pstates=pstates), Host(1, "1", pstates=pstates), ] mock_simulator.platform = Platform(hosts) monitor.on_simulation_begins(mock_simulator) return monitor
def test_allocate_multiple_jobs_when_not_shareable_must_raise(self): p1 = PowerState(1, PowerStateType.COMPUTATION, 10, 100) h = Host(0, "n", pstates=[p1], allow_sharing=False) h._allocate("j1") with pytest.raises(RuntimeError) as excinfo: h._allocate("j2") assert "multiple jobs" in str(excinfo.value)
def test_pstates_must_have_computation(self): p1 = PowerState(0, PowerStateType.SLEEP, 10, 10) p2 = PowerState(1, PowerStateType.SWITCHING_ON, 10, 10) p4 = PowerState(3, PowerStateType.SWITCHING_OFF, 50, 50) with pytest.raises(ValueError) as excinfo: Host(0, "n", pstates=[p1, p2, p4]) assert "at least one COMPUTATION" in str(excinfo.value)
def test_set_computation_state_not_found_must_raise(self): p1 = PowerState(0, PowerStateType.COMPUTATION, 10, 100) p2 = PowerState(1, PowerStateType.COMPUTATION, 15, 150) h = Host(0, "n", pstates=[p1, p2]) with pytest.raises(LookupError) as excinfo: h._set_computation_pstate(2) assert "not be found" in str(excinfo.value)
def test_allocate_when_unavailable_must_raise(self): h = Host(0, "n") h._set_unavailable() with pytest.raises(RuntimeError) as excinfo: h._allocate("job") assert "unavailable" in str(excinfo.value)
def test_valid_instance(self): p = PowerState(0, PowerStateType.COMPUTATION, 10, 10) i, n, r, m = 0, "n", [p], {'u': 1} h = Host(i, n, r, True, m) assert h.id == i and h.name == n assert h.pstate == p and h.metadata == m and not h.jobs
def test_transition_pstate_without_sleep_must_raise(self): p1 = PowerState(0, PowerStateType.SWITCHING_OFF, 10, 10) p2 = PowerState(1, PowerStateType.COMPUTATION, 10, 100) p4 = PowerState(3, PowerStateType.SWITCHING_ON, 50, 50) with pytest.raises(ValueError) as excinfo: Host(0, "n", pstates=[p1, p2, p4]) assert "sleeping" in str(excinfo.value)
def test_set_computation_state_valid(self): p1 = PowerState(0, PowerStateType.COMPUTATION, 10, 100) p2 = PowerState(1, PowerStateType.COMPUTATION, 15, 150) h = Host(0, "n", pstates=[p1, p2]) h._set_computation_pstate(1) assert h.pstate == p2
def test_pstates_must_have_unique_id(self): p1 = PowerState(1, PowerStateType.COMPUTATION, 10, 10) p2 = PowerState(1, PowerStateType.COMPUTATION, 100, 100) with pytest.raises(ValueError) as excinfo: Host(0, "n", pstates=[p1, p2]) assert "unique ids" in str(excinfo.value)
def test_allocate_multiple_jobs_valid(self): p1 = PowerState(1, PowerStateType.COMPUTATION, 10, 100) h = Host(0, "n", pstates=[p1], allow_sharing=True) h._allocate("j1") h._allocate("j2") assert h.is_idle and h.jobs and len(h.jobs) == 2 assert "j1" in h.jobs assert "j2" in h.jobs
def test_set_unavailable_must_return_to_last_state(self): h = Host(0, "n") h._allocate("job") h._start_computing() h._set_unavailable() assert h.is_unavailable and not h.is_computing h._set_available() assert h.is_computing and not h.is_unavailable
def test_release_when_multiple_jobs_must_remain_computing(self): h = Host(0, "n", allow_sharing=True) h._allocate("j1") h._allocate("j2") h._start_computing() h._release("j1") assert h.is_computing and h.state == HostState.COMPUTING assert h.jobs and len(h.jobs) == 1 and h.jobs[0] == "j2"
def test_get_pstate_by_type_not_found_must_raise(self): p2 = PowerState(1, PowerStateType.COMPUTATION, 10, 100) p3 = PowerState(2, PowerStateType.COMPUTATION, 1000, 10000) h = Host(0, "n", pstates=[p3, p2]) with pytest.raises(LookupError) as excinfo: h.get_pstate_by_type(PowerStateType.SWITCHING_OFF) assert "not be found" in str(excinfo.value)
def test_start_computing_when_unavailable_must_raise(self): p1 = PowerState(1, PowerStateType.COMPUTATION, 10, 100) h = Host(0, "n", pstates=[p1]) h._allocate("job") h._set_unavailable() with pytest.raises(SystemError) as excinfo: h._start_computing() assert "Unavailable" in str(excinfo.value)
def test_initial_pstate_must_be_the_first_computation_one(self): p1 = PowerState(0, PowerStateType.SLEEP, 10, 10) p2 = PowerState(1, PowerStateType.COMPUTATION, 10, 100) p3 = PowerState(2, PowerStateType.COMPUTATION, 1000, 10000) p4 = PowerState(3, PowerStateType.SWITCHING_OFF, 50, 50) p5 = PowerState(4, PowerStateType.SWITCHING_ON, 25, 25) h = Host(0, "n", pstates=[p1, p3, p2, p4, p5]) assert h.pstate == p2
def test_get_not_found_must_raise(self): h1 = Host(0, "n") s1 = Storage(1, "n") p = Platform([h1, s1]) with pytest.raises(LookupError) as excinfo: p.get(2) assert "no resources" in str(excinfo.value)
def test_get_default_pstate_valid(self): p1 = PowerState(0, PowerStateType.SLEEP, 10, 10) p2 = PowerState(1, PowerStateType.COMPUTATION, 10, 100) p3 = PowerState(2, PowerStateType.COMPUTATION, 1000, 10000) p4 = PowerState(3, PowerStateType.SWITCHING_OFF, 50, 50) p5 = PowerState(4, PowerStateType.SWITCHING_ON, 25, 25) h = Host(0, "n", pstates=[p1, p3, p2, p4, p5]) assert h.get_default_pstate() == p2
def test_get_pstate_by_type_must_return_all_matches(self): p1 = PowerState(0, PowerStateType.SLEEP, 10, 10) p2 = PowerState(1, PowerStateType.COMPUTATION, 10, 100) p3 = PowerState(2, PowerStateType.COMPUTATION, 1000, 10000) p4 = PowerState(3, PowerStateType.SWITCHING_OFF, 50, 50) p5 = PowerState(4, PowerStateType.SWITCHING_ON, 25, 25) h = Host(0, "n", pstates=[p1, p3, p2, p4, p5]) pstates = h.get_pstate_by_type(PowerStateType.COMPUTATION) assert pstates == [p2, p3]
def test_set_on_when_idle_must_raise(self): p1 = PowerState(0, PowerStateType.SLEEP, 10, 10) p2 = PowerState(1, PowerStateType.COMPUTATION, 10, 100) p3 = PowerState(2, PowerStateType.SWITCHING_OFF, 50, 50) p4 = PowerState(3, PowerStateType.SWITCHING_ON, 75, 75) h = Host(0, "n", pstates=[p1, p3, p2, p4]) with pytest.raises(SystemError) as excinfo: h._set_on() assert "switching on" in str(excinfo.value)
def test_sleep_pstate_must_be_unique(self): p1 = PowerState(0, PowerStateType.SLEEP, 10, 10) p2 = PowerState(1, PowerStateType.SWITCHING_OFF, 10, 10) p3 = PowerState(2, PowerStateType.COMPUTATION, 10, 100) p4 = PowerState(3, PowerStateType.SWITCHING_ON, 50, 50) p5 = PowerState(4, PowerStateType.SLEEP, 10, 10) with pytest.raises(ValueError) as excinfo: Host(0, "n", pstates=[p1, p2, p3, p4, p5]) assert "sleeping" in str(excinfo.value)
def test_get_storage_with_host_id_must_raise(self): h1 = Host(0, "n") s1 = Storage(1, "n") p = Platform([h1, s1]) with pytest.raises(LookupError) as excinfo: p.get_storage(0) assert "storage" in str(excinfo.value)
def test_set_computation_state_not_correct_type_must_raise(self): p1 = PowerState(0, PowerStateType.SLEEP, 10, 10) p2 = PowerState(1, PowerStateType.COMPUTATION, 10, 100) p3 = PowerState(2, PowerStateType.COMPUTATION, 15, 150) p4 = PowerState(3, PowerStateType.SWITCHING_OFF, 50, 50) p5 = PowerState(4, PowerStateType.SWITCHING_ON, 75, 75) h = Host(0, "n", pstates=[p1, p3, p2, p4, p5]) with pytest.raises(RuntimeError) as excinfo: h._set_computation_pstate(0) assert "computation" in str(excinfo.value)
def test_set_off_when_computing_must_raise(self): p1 = PowerState(0, PowerStateType.SLEEP, 10, 10) p2 = PowerState(1, PowerStateType.COMPUTATION, 10, 100) p3 = PowerState(3, PowerStateType.SWITCHING_OFF, 50, 50) p4 = PowerState(4, PowerStateType.SWITCHING_ON, 75, 75) h = Host(0, "n", pstates=[p1, p3, p2, p4]) h._allocate("job") h._start_computing() with pytest.raises(SystemError) as excinfo: h._set_off() assert "switching off" in str(excinfo.value)
def test_switch_off_valid(self): p1 = PowerState(0, PowerStateType.SLEEP, 10, 10) p2 = PowerState(1, PowerStateType.COMPUTATION, 10, 100) p3 = PowerState(2, PowerStateType.COMPUTATION, 1000, 10000) p4 = PowerState(3, PowerStateType.SWITCHING_OFF, 50, 50) p5 = PowerState(4, PowerStateType.SWITCHING_ON, 25, 25) h = Host(0, "n", pstates=[p1, p3, p2, p4, p5]) h._switch_off() assert h.is_switching_off and h.power == p4.watt_idle assert h.pstate == p4 and h.state == HostState.SWITCHING_OFF
def test_switch_off_allocated_must_raise(self): p1 = PowerState(0, PowerStateType.SLEEP, 10, 10) p2 = PowerState(1, PowerStateType.COMPUTATION, 10, 100) p3 = PowerState(2, PowerStateType.COMPUTATION, 1000, 10000) p4 = PowerState(3, PowerStateType.SWITCHING_OFF, 50, 50) p5 = PowerState(4, PowerStateType.SWITCHING_ON, 25, 25) h = Host(0, "n", pstates=[p1, p3, p2, p4, p5]) h._allocate("job") with pytest.raises(RuntimeError) as excinfo: h._switch_off() assert "idle and free " in str(excinfo.value)
def test_switch_on_switching_off_must_raises(self): p1 = PowerState(0, PowerStateType.SLEEP, 10, 10) p2 = PowerState(1, PowerStateType.COMPUTATION, 10, 100) p3 = PowerState(2, PowerStateType.SWITCHING_OFF, 50, 50) p4 = PowerState(3, PowerStateType.SWITCHING_ON, 75, 75) h = Host(0, "n", pstates=[p1, p3, p2, p4]) h._switch_off() with pytest.raises(RuntimeError) as excinfo: h._switch_on() assert "be sleeping" in str(excinfo.value)
def test_set_computation_state_when_switching_off_must_raise(self): p1 = PowerState(0, PowerStateType.SLEEP, 10, 10) p2 = PowerState(1, PowerStateType.COMPUTATION, 10, 100) p3 = PowerState(2, PowerStateType.COMPUTATION, 15, 150) p4 = PowerState(3, PowerStateType.SWITCHING_OFF, 50, 50) p5 = PowerState(4, PowerStateType.SWITCHING_ON, 75, 75) h = Host(0, "n", pstates=[p1, p3, p2, p4, p5]) h._switch_off() with pytest.raises(RuntimeError) as excinfo: h._set_computation_pstate(2) assert "idle or computing" in str(excinfo.value)
def test_set_off_valid(self): p1 = PowerState(0, PowerStateType.SLEEP, 10, 10) p2 = PowerState(1, PowerStateType.COMPUTATION, 10, 100) p3 = PowerState(3, PowerStateType.SWITCHING_OFF, 50, 50) p4 = PowerState(4, PowerStateType.SWITCHING_ON, 75, 75) h = Host(0, "n", pstates=[p1, p3, p2, p4]) h._switch_off() h._set_off() assert h.is_sleeping and h.power == p1.watt_full assert h.state == HostState.SLEEPING and h.pstate == p1