| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
from unittest import mock |
| 4 |
|
| 5 |
import mozunit |
| 6 |
import pytest |
| 7 |
from mozpower.powerbase import ( |
| 8 |
IPGExecutableMissingError, |
| 9 |
PlatformUnsupportedError, |
| 10 |
PowerBase, |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
def test_powerbase_intialization(): |
| 15 |
"""Tests that the PowerBase class correctly raises |
| 16 |
a NotImplementedError when attempting to instantiate |
| 17 |
it directly. |
| 18 |
""" |
| 19 |
with pytest.raises(NotImplementedError): |
| 20 |
PowerBase() |
| 21 |
|
| 22 |
|
| 23 |
def test_powerbase_missing_methods(powermeasurer): |
| 24 |
"""Tests that trying to call PowerBase methods |
| 25 |
without an implementation in the subclass raises |
| 26 |
the NotImplementedError. |
| 27 |
""" |
| 28 |
with pytest.raises(NotImplementedError): |
| 29 |
powermeasurer.initialize_power_measurements() |
| 30 |
|
| 31 |
with pytest.raises(NotImplementedError): |
| 32 |
powermeasurer.finalize_power_measurements() |
| 33 |
|
| 34 |
with pytest.raises(NotImplementedError): |
| 35 |
powermeasurer.get_perfherder_data() |
| 36 |
|
| 37 |
|
| 38 |
def test_powerbase_get_ipg_path_mac(powermeasurer): |
| 39 |
"""Tests that getting the IPG path returns the expected results.""" |
| 40 |
powermeasurer._os = "darwin" |
| 41 |
powermeasurer._cpu = "not-intel" |
| 42 |
|
| 43 |
def os_side_effect(arg): |
| 44 |
"""Used to get around the os.path.exists check in |
| 45 |
get_ipg_path which raises an IPGExecutableMissingError |
| 46 |
otherwise. |
| 47 |
""" |
| 48 |
return True |
| 49 |
|
| 50 |
with mock.patch("os.path.exists", return_value=True) as m: |
| 51 |
m.side_effect = os_side_effect |
| 52 |
|
| 53 |
# None is returned when a non-intel based machine is |
| 54 |
# tested. |
| 55 |
ipg_path = powermeasurer.get_ipg_path() |
| 56 |
assert ipg_path is None |
| 57 |
|
| 58 |
# Check the path returned for mac intel-based machines. |
| 59 |
powermeasurer._cpu = "intel" |
| 60 |
ipg_path = powermeasurer.get_ipg_path() |
| 61 |
assert ipg_path == "/Applications/Intel Power Gadget/PowerLog" |
| 62 |
|
| 63 |
|
| 64 |
def test_powerbase_get_ipg_path_errors(powermeasurer): |
| 65 |
"""Tests that the appropriate error is output when calling |
| 66 |
get_ipg_path with invalid/unsupported _os and _cpu entries. |
| 67 |
""" |
| 68 |
|
| 69 |
powermeasurer._cpu = "intel" |
| 70 |
powermeasurer._os = "Not-An-OS" |
| 71 |
|
| 72 |
def os_side_effect(arg): |
| 73 |
"""Used to force the error IPGExecutableMissingError to occur |
| 74 |
(in case a machine running these tests is a mac, and has intel |
| 75 |
power gadget installed). |
| 76 |
""" |
| 77 |
return False |
| 78 |
|
| 79 |
with pytest.raises(PlatformUnsupportedError): |
| 80 |
powermeasurer.get_ipg_path() |
| 81 |
|
| 82 |
with mock.patch("os.path.exists", return_value=False) as m: |
| 83 |
m.side_effect = os_side_effect |
| 84 |
|
| 85 |
powermeasurer._os = "darwin" |
| 86 |
with pytest.raises(IPGExecutableMissingError): |
| 87 |
powermeasurer.get_ipg_path() |
| 88 |
|
| 89 |
|
| 90 |
if __name__ == "__main__": |
| 91 |
mozunit.main() |