Skip to content

Commit 7325b08

Browse files
committed
refactor: adapt tests to PowerAPI 2.0.1 refactoring
1 parent 8454b04 commit 7325b08

File tree

8 files changed

+3328
-3186
lines changed

8 files changed

+3328
-3186
lines changed

smartwatts/test_utils/reports.py

Lines changed: 0 additions & 3112 deletions
This file was deleted.

tests/acceptation/test_hwpc_report_with_mperf_to_zero_must_not_crash_the_system.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,10 @@
3838
The first hwpc report contains mperf value equals to 0
3939
Test if the system don't crash after receiving the first report and deal with the other report
4040
"""
41-
import time
42-
from datetime import datetime
4341
import pytest
4442

45-
import pymongo
46-
47-
from powerapi.test_utils.db.mongo import mongo_database
48-
from powerapi.test_utils.unit import shutdown_system
49-
50-
from smartwatts.test_utils.reports import smartwatts_timeline, smartwatts_timeline_with_mperf_0
51-
52-
from tests.acceptation.acceptation_test_utils import formula_config, check_db, AbstractAcceptationTest
43+
from tests.utils.acceptation import AbstractAcceptationTest
44+
from tests.utils.reports import smartwatts_timeline_with_mperf_0, smartwatts_timeline
5345

5446

5547
@pytest.fixture

tests/acceptation/test_normal_behaviour.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2828
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929

30-
# pylint: disable=redefined-outer-name,unused-argument,unused-import,no-self-use
30+
# pylint: disable=redefined-outer-name,unused-argument,unused-import
3131

3232
"""
3333
Run smartwatts on a mongodb database that contain 10 hwpc report per target :
@@ -41,20 +41,12 @@
4141
We test if smartwatts return 5 powerReport for rapl target
4242
"""
4343
import time
44-
from datetime import datetime
4544

4645
import pytest
4746

48-
import pymongo
49-
50-
from powerapi.test_utils.unit import shutdown_system
51-
from powerapi.test_utils.db.mongo import mongo_database
52-
53-
5447
from smartwatts.__main__ import run_smartwatts
55-
from smartwatts.test_utils.reports import smartwatts_timeline
56-
from tests.acceptation.acceptation_test_utils import formula_config, formula_config_real_time_enabled, check_db, \
57-
check_db_real_time, AbstractAcceptationTest
48+
from tests.utils.acceptation import check_db_real_time, AbstractAcceptationTest
49+
from tests.utils.reports import smartwatts_timeline
5850

5951

6052
@pytest.fixture

tests/utils/__init__.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright (c) 2023, INRIA
2+
# Copyright (c) 2023, University of Lille
3+
# All rights reserved.
4+
#
5+
# Redistribution and use in source and binary forms, with or without
6+
# modification, are permitted provided that the following conditions are met:
7+
#
8+
# * Redistributions of source code must retain the above copyright notice, this
9+
# list of conditions and the following disclaimer.
10+
#
11+
# * Redistributions in binary form must reproduce the above copyright notice,
12+
# this list of conditions and the following disclaimer in the documentation
13+
# and/or other materials provided with the distribution.
14+
#
15+
# * Neither the name of the copyright holder nor the names of its
16+
# contributors may be used to endorse or promote products derived from
17+
# this software without specific prior written permission.
18+
#
19+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

tests/acceptation/acceptation_test_utils.py renamed to tests/utils/acceptation.py

Lines changed: 72 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@
2727
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2828
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929

30-
# pylint: disable=redefined-outer-name,unused-argument,unused-import,no-self-use
30+
# pylint: disable=redefined-outer-name,unused-argument,unused-import
3131
import time
3232
from datetime import datetime
33+
from multiprocessing import active_children
3334

3435
import pymongo
3536
import pytest
3637

37-
from powerapi.test_utils.db.mongo import MONGO_URI, MONGO_DATABASE_NAME, MONGO_INPUT_COLLECTION_NAME, \
38-
MONGO_OUTPUT_COLLECTION_NAME
38+
from tests.utils.db.mongo import MONGO_URI, MONGO_DATABASE_NAME, MONGO_INPUT_COLLECTION_NAME, \
39+
MONGO_OUTPUT_COLLECTION_NAME, gen_base_db_test, clean_base_db_test
3940

4041
from smartwatts.__main__ import run_smartwatts
4142

@@ -44,54 +45,6 @@
4445
SENSOR_NAME = 'cpu'
4546

4647

47-
@pytest.fixture
48-
def formula_config():
49-
"""
50-
Return a formula config
51-
:return: The formula config
52-
"""
53-
return {'verbose': 0,
54-
'stream': False,
55-
'input': {'puller_mongodb': {'type': 'mongodb',
56-
'model': 'HWPCReport',
57-
'uri': MONGO_URI,
58-
'db': MONGO_DATABASE_NAME,
59-
'collection': MONGO_INPUT_COLLECTION_NAME}},
60-
'output': {'power_pusher': {'type': 'mongodb',
61-
'model': 'PowerReport',
62-
'uri': MONGO_URI,
63-
'db': MONGO_DATABASE_NAME,
64-
'collection': MONGO_OUTPUT_COLLECTION_NAME},
65-
'formula_pusher': {'type': 'mongodb',
66-
'model': 'FormulaReport',
67-
'uri': MONGO_URI,
68-
'db': MONGO_DATABASE_NAME,
69-
'collection': 'test_result_formula'}},
70-
'disable-cpu-formula': False,
71-
'disable-dram-formula': True,
72-
'cpu-rapl-ref-event': 'RAPL_ENERGY_PKG',
73-
'cpu-tdp': 125,
74-
'cpu-base-clock': 100,
75-
'cpu-frequency-min': 4,
76-
'cpu-frequency-base': 19,
77-
'cpu-frequency-max': 42,
78-
'cpu-error-threshold': 2.0,
79-
'sensor-report-sampling-interval': 1000,
80-
'learn-min-samples-required': 10,
81-
'learn-history-window-size': 60,
82-
'real-time-mode': False}
83-
84-
85-
@pytest.fixture
86-
def formula_config_real_time_enabled(formula_config):
87-
"""
88-
Return a formula config with real time mode = True
89-
:return: The formula config
90-
"""
91-
formula_config['real-time-mode'] = True
92-
return formula_config
93-
94-
9548
def check_db():
9649
"""
9750
Check that the output database has the generated reports
@@ -126,11 +79,77 @@ def check_db_real_time():
12679
assert c_output.count_documents(query) == 1
12780

12881

129-
class AbstractAcceptationTest():
82+
class AbstractAcceptationTest:
13083
"""
131-
Basic acceptation tests for Smartwatts Formula
84+
Basic acceptation tests for SmartWatts Formula
13285
"""
13386

87+
@pytest.fixture
88+
def shutdown_system(self):
89+
"""
90+
Shutdown the actor system, i.e., all actors are killed
91+
"""
92+
yield None
93+
active = active_children()
94+
for child in active:
95+
child.kill()
96+
97+
@pytest.fixture
98+
def mongo_database(self, mongodb_content):
99+
"""
100+
connect to a local mongo database (localhost:27017) and store data contained in the list influxdb_content
101+
after test end, delete the data
102+
"""
103+
gen_base_db_test(MONGO_URI, mongodb_content)
104+
yield None
105+
clean_base_db_test(MONGO_URI)
106+
107+
@pytest.fixture
108+
def formula_config(self):
109+
"""
110+
Return a formula config
111+
:return: The formula config
112+
"""
113+
return {'verbose': 0,
114+
'stream': False,
115+
'input': {'puller_mongodb': {'type': 'mongodb',
116+
'model': 'HWPCReport',
117+
'uri': MONGO_URI,
118+
'db': MONGO_DATABASE_NAME,
119+
'collection': MONGO_INPUT_COLLECTION_NAME}},
120+
'output': {'power_pusher': {'type': 'mongodb',
121+
'model': 'PowerReport',
122+
'uri': MONGO_URI,
123+
'db': MONGO_DATABASE_NAME,
124+
'collection': MONGO_OUTPUT_COLLECTION_NAME},
125+
'formula_pusher': {'type': 'mongodb',
126+
'model': 'FormulaReport',
127+
'uri': MONGO_URI,
128+
'db': MONGO_DATABASE_NAME,
129+
'collection': 'test_result_formula'}},
130+
'disable-cpu-formula': False,
131+
'disable-dram-formula': True,
132+
'cpu-rapl-ref-event': 'RAPL_ENERGY_PKG',
133+
'cpu-tdp': 125,
134+
'cpu-base-clock': 100,
135+
'cpu-frequency-min': 4,
136+
'cpu-frequency-base': 19,
137+
'cpu-frequency-max': 42,
138+
'cpu-error-threshold': 2.0,
139+
'sensor-report-sampling-interval': 1000,
140+
'learn-min-samples-required': 10,
141+
'learn-history-window-size': 60,
142+
'real-time-mode': False}
143+
144+
@pytest.fixture
145+
def formula_config_real_time_enabled(self, formula_config):
146+
"""
147+
Return a formula config with real time mode = True
148+
:return: The formula config
149+
"""
150+
formula_config['real-time-mode'] = True
151+
return formula_config
152+
134153
def test_normal_behaviour(self, mongo_database, formula_config, shutdown_system):
135154
"""
136155
Test that the formula generate the expected Power reports

tests/utils/db/__init__.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright (c) 2023, INRIA
2+
# Copyright (c) 2023, University of Lille
3+
# All rights reserved.
4+
5+
# Redistribution and use in source and binary forms, with or without
6+
# modification, are permitted provided that the following conditions are met:
7+
8+
# * Redistributions of source code must retain the above copyright notice, this
9+
# list of conditions and the following disclaimer.
10+
11+
# * Redistributions in binary form must reproduce the above copyright notice,
12+
# this list of conditions and the following disclaimer in the documentation
13+
# and/or other materials provided with the distribution.
14+
15+
# * Neither the name of the copyright holder nor the names of its
16+
# contributors may be used to endorse or promote products derived from
17+
# this software without specific prior written permission.
18+
19+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE

tests/utils/db/mongo.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Copyright (c) 2021, INRIA
2+
# Copyright (c) 2021, University of Lille
3+
# All rights reserved.
4+
5+
# Redistribution and use in source and binary forms, with or without
6+
# modification, are permitted provided that the following conditions are met:
7+
8+
# * Redistributions of source code must retain the above copyright notice, this
9+
# list of conditions and the following disclaimer.
10+
11+
# * Redistributions in binary form must reproduce the above copyright notice,
12+
# this list of conditions and the following disclaimer in the documentation
13+
# and/or other materials provided with the distribution.
14+
15+
# * Neither the name of the copyright holder nor the names of its
16+
# contributors may be used to endorse or promote products derived from
17+
# this software without specific prior written permission.
18+
19+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
30+
import pymongo
31+
32+
33+
MONGO_URI = "mongodb://127.0.0.1:27017/"
34+
MONGO_INPUT_COLLECTION_NAME = 'test_input_smartwatts'
35+
MONGO_OUTPUT_COLLECTION_NAME = 'test_output_smartwatts'
36+
MONGO_DATABASE_NAME = 'MongoDB1'
37+
38+
39+
def gen_base_db_test(uri, content):
40+
"""
41+
Create the input database with the provided content. The output database is deleted
42+
:param uri: The mongo connection URI
43+
:param content: The database content
44+
"""
45+
mongo = pymongo.MongoClient(uri)
46+
db = mongo[MONGO_DATABASE_NAME]
47+
48+
# delete collection if it already exist
49+
db[MONGO_INPUT_COLLECTION_NAME].drop()
50+
db.create_collection(MONGO_INPUT_COLLECTION_NAME)
51+
for item in content:
52+
db[MONGO_INPUT_COLLECTION_NAME].insert_one(item)
53+
54+
# delete output collection
55+
db[MONGO_OUTPUT_COLLECTION_NAME].drop()
56+
mongo.close()
57+
58+
59+
def clean_base_db_test(uri):
60+
"""
61+
drop test_input_smartwatts and test_output_smartwatts collections
62+
"""
63+
mongo = pymongo.MongoClient(uri)
64+
db = mongo[MONGO_DATABASE_NAME]
65+
db[MONGO_INPUT_COLLECTION_NAME].drop()
66+
db[MONGO_OUTPUT_COLLECTION_NAME].drop()
67+
mongo.close()

0 commit comments

Comments
 (0)