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_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_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_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_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_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_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_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_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_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_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_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_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
 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_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_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_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_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_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 test_set_on_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()
        h._switch_on()
        h._set_on()

        assert h.is_idle and h.pstate == p2 and h.state == HostState.IDLE
    def test_set_computation_state_when_computing_valid(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._allocate("job")
        h._start_computing()
        h._set_computation_pstate(2)
        assert h.pstate == p3 and h.power == p3.watt_full
 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_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_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_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_power_when_pstates_defined(self):
     p1 = PowerState(1, PowerStateType.COMPUTATION, 15, 100)
     p2 = PowerState(1, PowerStateType.COMPUTATION, 50, 100)
     res = [Host(0, "n", pstates=[p1]), Host(1, "n", pstates=[p2])]
     p = Platform(res)
     assert p.power == 15 + 50
 def test_pstates(self):
     p1 = PowerState(1, PowerStateType.COMPUTATION, 10, 100)
     h = Host(0, "n", pstates=[p1])
     assert h.pstates == [p1]
 def test_initial_state_must_be_idle(self):
     p1 = PowerState(1, PowerStateType.COMPUTATION, 10, 100)
     h = Host(0, "n", pstates=[p1])
     assert h.state == HostState.IDLE and h.is_idle
 def test_start_computing_valid(self):
     p1 = PowerState(1, PowerStateType.COMPUTATION, 10, 100)
     h = Host(0, "n", pstates=[p1])
     h._allocate("job")
     h._start_computing()
     assert h.is_computing and h.state == HostState.COMPUTING
 def test_allocate_valid(self):
     p1 = PowerState(1, PowerStateType.COMPUTATION, 10, 100)
     h = Host(0, "n", pstates=[p1])
     h._allocate("job")
     assert h.is_idle and h.jobs and h.jobs[0] == "job"