Skip to content

Commit

Permalink
refactor: adapt tests to PowerAPI 2.0.1 refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
roda82 committed Mar 24, 2023
1 parent 8454b04 commit 7325b08
Show file tree
Hide file tree
Showing 8 changed files with 3,328 additions and 3,186 deletions.
3,112 changes: 0 additions & 3,112 deletions smartwatts/test_utils/reports.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,10 @@
The first hwpc report contains mperf value equals to 0
Test if the system don't crash after receiving the first report and deal with the other report
"""
import time
from datetime import datetime
import pytest

import pymongo

from powerapi.test_utils.db.mongo import mongo_database
from powerapi.test_utils.unit import shutdown_system

from smartwatts.test_utils.reports import smartwatts_timeline, smartwatts_timeline_with_mperf_0

from tests.acceptation.acceptation_test_utils import formula_config, check_db, AbstractAcceptationTest
from tests.utils.acceptation import AbstractAcceptationTest
from tests.utils.reports import smartwatts_timeline_with_mperf_0, smartwatts_timeline


@pytest.fixture
Expand Down
14 changes: 3 additions & 11 deletions tests/acceptation/test_normal_behaviour.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

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

"""
Run smartwatts on a mongodb database that contain 10 hwpc report per target :
Expand All @@ -41,20 +41,12 @@
We test if smartwatts return 5 powerReport for rapl target
"""
import time
from datetime import datetime

import pytest

import pymongo

from powerapi.test_utils.unit import shutdown_system
from powerapi.test_utils.db.mongo import mongo_database


from smartwatts.__main__ import run_smartwatts
from smartwatts.test_utils.reports import smartwatts_timeline
from tests.acceptation.acceptation_test_utils import formula_config, formula_config_real_time_enabled, check_db, \
check_db_real_time, AbstractAcceptationTest
from tests.utils.acceptation import check_db_real_time, AbstractAcceptationTest
from tests.utils.reports import smartwatts_timeline


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

# pylint: disable=redefined-outer-name,unused-argument,unused-import,no-self-use
# pylint: disable=redefined-outer-name,unused-argument,unused-import
import time
from datetime import datetime
from multiprocessing import active_children

import pymongo
import pytest

from powerapi.test_utils.db.mongo import MONGO_URI, MONGO_DATABASE_NAME, MONGO_INPUT_COLLECTION_NAME, \
MONGO_OUTPUT_COLLECTION_NAME
from tests.utils.db.mongo import MONGO_URI, MONGO_DATABASE_NAME, MONGO_INPUT_COLLECTION_NAME, \
MONGO_OUTPUT_COLLECTION_NAME, gen_base_db_test, clean_base_db_test

from smartwatts.__main__ import run_smartwatts

Expand All @@ -44,54 +45,6 @@
SENSOR_NAME = 'cpu'


@pytest.fixture
def formula_config():
"""
Return a formula config
:return: The formula config
"""
return {'verbose': 0,
'stream': False,
'input': {'puller_mongodb': {'type': 'mongodb',
'model': 'HWPCReport',
'uri': MONGO_URI,
'db': MONGO_DATABASE_NAME,
'collection': MONGO_INPUT_COLLECTION_NAME}},
'output': {'power_pusher': {'type': 'mongodb',
'model': 'PowerReport',
'uri': MONGO_URI,
'db': MONGO_DATABASE_NAME,
'collection': MONGO_OUTPUT_COLLECTION_NAME},
'formula_pusher': {'type': 'mongodb',
'model': 'FormulaReport',
'uri': MONGO_URI,
'db': MONGO_DATABASE_NAME,
'collection': 'test_result_formula'}},
'disable-cpu-formula': False,
'disable-dram-formula': True,
'cpu-rapl-ref-event': 'RAPL_ENERGY_PKG',
'cpu-tdp': 125,
'cpu-base-clock': 100,
'cpu-frequency-min': 4,
'cpu-frequency-base': 19,
'cpu-frequency-max': 42,
'cpu-error-threshold': 2.0,
'sensor-report-sampling-interval': 1000,
'learn-min-samples-required': 10,
'learn-history-window-size': 60,
'real-time-mode': False}


@pytest.fixture
def formula_config_real_time_enabled(formula_config):
"""
Return a formula config with real time mode = True
:return: The formula config
"""
formula_config['real-time-mode'] = True
return formula_config


def check_db():
"""
Check that the output database has the generated reports
Expand Down Expand Up @@ -126,11 +79,77 @@ def check_db_real_time():
assert c_output.count_documents(query) == 1


class AbstractAcceptationTest():
class AbstractAcceptationTest:
"""
Basic acceptation tests for Smartwatts Formula
Basic acceptation tests for SmartWatts Formula
"""

@pytest.fixture
def shutdown_system(self):
"""
Shutdown the actor system, i.e., all actors are killed
"""
yield None
active = active_children()
for child in active:
child.kill()

@pytest.fixture
def mongo_database(self, mongodb_content):
"""
connect to a local mongo database (localhost:27017) and store data contained in the list influxdb_content
after test end, delete the data
"""
gen_base_db_test(MONGO_URI, mongodb_content)
yield None
clean_base_db_test(MONGO_URI)

@pytest.fixture
def formula_config(self):
"""
Return a formula config
:return: The formula config
"""
return {'verbose': 0,
'stream': False,
'input': {'puller_mongodb': {'type': 'mongodb',
'model': 'HWPCReport',
'uri': MONGO_URI,
'db': MONGO_DATABASE_NAME,
'collection': MONGO_INPUT_COLLECTION_NAME}},
'output': {'power_pusher': {'type': 'mongodb',
'model': 'PowerReport',
'uri': MONGO_URI,
'db': MONGO_DATABASE_NAME,
'collection': MONGO_OUTPUT_COLLECTION_NAME},
'formula_pusher': {'type': 'mongodb',
'model': 'FormulaReport',
'uri': MONGO_URI,
'db': MONGO_DATABASE_NAME,
'collection': 'test_result_formula'}},
'disable-cpu-formula': False,
'disable-dram-formula': True,
'cpu-rapl-ref-event': 'RAPL_ENERGY_PKG',
'cpu-tdp': 125,
'cpu-base-clock': 100,
'cpu-frequency-min': 4,
'cpu-frequency-base': 19,
'cpu-frequency-max': 42,
'cpu-error-threshold': 2.0,
'sensor-report-sampling-interval': 1000,
'learn-min-samples-required': 10,
'learn-history-window-size': 60,
'real-time-mode': False}

@pytest.fixture
def formula_config_real_time_enabled(self, formula_config):
"""
Return a formula config with real time mode = True
:return: The formula config
"""
formula_config['real-time-mode'] = True
return formula_config

def test_normal_behaviour(self, mongo_database, formula_config, shutdown_system):
"""
Test that the formula generate the expected Power reports
Expand Down
28 changes: 28 additions & 0 deletions tests/utils/db/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright (c) 2023, INRIA
# Copyright (c) 2023, University of Lille
# All rights reserved.

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:

# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.

# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.

# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
67 changes: 67 additions & 0 deletions tests/utils/db/mongo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright (c) 2021, INRIA
# Copyright (c) 2021, University of Lille
# All rights reserved.

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:

# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.

# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.

# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import pymongo


MONGO_URI = "mongodb://127.0.0.1:27017/"
MONGO_INPUT_COLLECTION_NAME = 'test_input_smartwatts'
MONGO_OUTPUT_COLLECTION_NAME = 'test_output_smartwatts'
MONGO_DATABASE_NAME = 'MongoDB1'


def gen_base_db_test(uri, content):
"""
Create the input database with the provided content. The output database is deleted
:param uri: The mongo connection URI
:param content: The database content
"""
mongo = pymongo.MongoClient(uri)
db = mongo[MONGO_DATABASE_NAME]

# delete collection if it already exist
db[MONGO_INPUT_COLLECTION_NAME].drop()
db.create_collection(MONGO_INPUT_COLLECTION_NAME)
for item in content:
db[MONGO_INPUT_COLLECTION_NAME].insert_one(item)

# delete output collection
db[MONGO_OUTPUT_COLLECTION_NAME].drop()
mongo.close()


def clean_base_db_test(uri):
"""
drop test_input_smartwatts and test_output_smartwatts collections
"""
mongo = pymongo.MongoClient(uri)
db = mongo[MONGO_DATABASE_NAME]
db[MONGO_INPUT_COLLECTION_NAME].drop()
db[MONGO_OUTPUT_COLLECTION_NAME].drop()
mongo.close()
Loading

0 comments on commit 7325b08

Please sign in to comment.