Debian Codegraph @

testing/mozbase/mozpower/tests/conftest.py

raw
1 import os
2 import tempfile
3 import time
4 from unittest import mock
5
6 import pytest
7 from mozpower import MozPower
8 from mozpower.intel_power_gadget import IntelPowerGadget, IPGResultsHandler
9 from mozpower.macintelpower import MacIntelPower
10 from mozpower.powerbase import PowerBase
11
12
13 def os_side_effect(*args, **kwargs):
14 """Used as a side effect to os.path.exists when
15 checking if the Intel Power Gadget executable exists.
16 """
17 return True
18
19
20 def subprocess_side_effect(*args, **kwargs):
21 """Used as a side effect when running the Intel Power
22 Gadget tool.
23 """
24 time.sleep(1)
25
26
27 @pytest.fixture(scope="function")
28 def powermeasurer():
29 """Returns a testing subclass of the PowerBase class
30 for testing.
31 """
32
33 class PowerMeasurer(PowerBase):
34 pass
35
36 return PowerMeasurer()
37
38
39 @pytest.fixture(scope="function")
40 def ipg_obj():
41 """Returns an IntelPowerGadget object with the test
42 output file path.
43 """
44 return IntelPowerGadget(
45 "ipg-path",
46 output_file_path=os.path.abspath(os.path.dirname(__file__))
47 + "/files/raptor-tp6-amazon-firefox_powerlog",
48 )
49
50
51 @pytest.fixture(scope="function")
52 def ipg_rh_obj():
53 """Returns an IPGResultsHandler object set up with the
54 test files and cleans up the directory after the tests
55 are complete.
56 """
57 base_path = os.path.abspath(os.path.dirname(__file__)) + "/files/"
58 tmpdir = tempfile.mkdtemp()
59
60 # Return the results handler for the test
61 yield IPGResultsHandler(
62 [
63 base_path + "raptor-tp6-amazon-firefox_powerlog_1_.txt",
64 base_path + "raptor-tp6-amazon-firefox_powerlog_2_.txt",
65 base_path + "raptor-tp6-amazon-firefox_powerlog_3_.txt",
66 ],
67 tmpdir,
68 )
69
70
71 @pytest.fixture(scope="function")
72 def macintelpower_obj():
73 """Returns a MacIntelPower object with subprocess.check_output
74 and os.path.exists calls patched with side effects.
75 """
76 with mock.patch("subprocess.check_output") as subprocess_mock:
77 with mock.patch("os.path.exists") as os_mock:
78 subprocess_mock.side_effect = subprocess_side_effect
79 os_mock.side_effect = os_side_effect
80
81 yield MacIntelPower(ipg_measure_duration=2)
82
83
84 @pytest.fixture(scope="function")
85 def mozpower_obj():
86 """Returns a MozPower object with subprocess.check_output
87 and os.path.exists calls patched with side effects.
88 """
89 with mock.patch.object(
90 MozPower, "_get_os", return_value="Darwin"
91 ) as _, mock.patch.object(
92 MozPower, "_get_processor_info", return_value="GenuineIntel"
93 ) as _, mock.patch.object(
94 MacIntelPower, "get_ipg_path", return_value="/"
95 ) as _, mock.patch(
96 "subprocess.check_output"
97 ) as subprocess_mock, mock.patch(
98 "os.path.exists"
99 ) as os_mock:
100 subprocess_mock.side_effect = subprocess_side_effect
101 os_mock.side_effect = os_side_effect
102
103 yield MozPower(ipg_measure_duration=2)