Skip to content

Commit

Permalink
Merge branch '174-tests-de-ressources-premiers-tests' into 'release'
Browse files Browse the repository at this point in the history
Resolve "Tests de ressources : Premiers tests"

See merge request 3d/PandoraBox/pandora2d!147
  • Loading branch information
lecontm committed Sep 10, 2024
2 parents eea304a + 6ac7f88 commit 507e8ca
Show file tree
Hide file tree
Showing 5 changed files with 276 additions and 109 deletions.
7 changes: 5 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@
Module with global test fixtures.
"""

# pylint: disable=redefined-outer-name
import json

# pylint: disable=redefined-outer-name
import pathlib
import re

import json
import numpy as np
import pytest
import rasterio
from pandora.common import write_data_array
import xarray as xr
from pandora.common import write_data_array

import pandora2d

import pandora2d

Expand Down
50 changes: 1 addition & 49 deletions tests/resource_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,62 +19,14 @@
"""
Module with global test fixtures.
"""
from typing import List
import sqlite3
import pytest


class Metrics:
"""
Metrics Class
"""

# pylint:disable=too-few-public-methods

_TOTAL_TIME_MAX = 120 # second
_CPU_USAGE_MAX = 50 # percent
_MEM_USAGE_MAX = 1024 # megabyte

def __init__(self, items: List) -> None:
self.test_name = items[0]
self.total_time = items[1]
self.cpu_usage = items[2]
self.mem_usage = items[3]
import pytest


def pytest_addoption(parser):
parser.addoption("--database", action="store", default=".pymon", required=False)


@pytest.fixture(name="database_path")
def database_path_fixture(request):
return request.config.getoption("--database")


@pytest.fixture
def output_result_path():
return "./tests/resource_tests/result"


@pytest.fixture(name="sqlite_select_query")
def sqlite_select_query_fixture():
return """SELECT ITEM, TOTAL_TIME, CPU_USAGE, MEM_USAGE FROM TEST_METRICS"""


@pytest.fixture()
def read_sqlite_table(database_path, sqlite_select_query):
"""
Read sqlite table from pytest-monitoring
"""
data = []
try:
sqlite_connection = sqlite3.connect(database_path)
cursor = sqlite_connection.cursor()

cursor.execute(sqlite_select_query)
records = cursor.fetchall()
data = [Metrics(record) for record in records]
sqlite_connection.close()
except sqlite3.Error as error:
print("Failed to read data from sqlite table", error)
return data
40 changes: 0 additions & 40 deletions tests/resource_tests/test_example.py

This file was deleted.

85 changes: 67 additions & 18 deletions tests/resource_tests/test_metrics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# pylint: disable=protected-access
#!/usr/bin/env python
#
# Copyright (c) 2024 Centre National d'Etudes Spatiales (CNES).
#
# This file is part of PANDORA2D
Expand All @@ -23,39 +20,91 @@
"""
This module is used to check the resources used for the tests in this directory.
"""

# pylint: disable=protected-access

import sqlite3
from typing import List

import pytest


class Metrics:
"""
Metrics Class
"""

# pylint:disable=too-few-public-methods

_TOTAL_TIME_MAX = 120 # second
_CPU_USAGE_MAX = 50 # percent
_MEM_USAGE_MAX = 1024 # megabyte

def __init__(self, items: List) -> None:
self.test_name = items[0]
self.test_variant = items[1]
self.total_time = items[2]
self.cpu_usage = items[3]
self.mem_usage = items[4]


def read_sqlite_table(database_path, sqlite_select_query):
"""
Read sqlite table from pytest-monitoring
"""
data = []
try:
sqlite_connection = sqlite3.connect(database_path)
cursor = sqlite_connection.cursor()

cursor.execute(sqlite_select_query)
records = cursor.fetchall()
data = [Metrics(record) for record in records]
sqlite_connection.close()
except sqlite3.Error as error:
print("Failed to read data from sqlite table", error)
return data


# Define the pytest_generate_tests hook to generate test cases
def pytest_generate_tests(metafunc):
"""Generate list of tests from pytest-monitoring database."""
query = "SELECT ITEM, ITEM_VARIANT, TOTAL_TIME, CPU_USAGE, MEM_USAGE FROM TEST_METRICS"
marks = [mark.name for mark in metafunc.cls.pytestmark]
if "metrics" in marks:
metrics = read_sqlite_table(metafunc.config.option.database, query)
if metrics:
# Generate test cases based on the metrics list
metafunc.parametrize("metric", metrics, ids=lambda x: x.test_variant)


@pytest.mark.metrics
@pytest.mark.monitor_skip_test
class TestResource:
"""
Test all tests are ok for CPU/MEM and time rule
"""

def test_total_time(self, read_sqlite_table):
def test_total_time(self, metric):
"""
Verify the time metrics for the test
"""
for metric in read_sqlite_table:
assert (
metric.total_time < metric._TOTAL_TIME_MAX
), f"Test {metric.test_name} does not respect max time : {metric._TOTAL_TIME_MAX} (seconds)"
assert (
metric.total_time < metric._TOTAL_TIME_MAX
), f"Test {metric.test_variant} does not respect max time : {metric._TOTAL_TIME_MAX} (seconds)"

def test_cpu_usage(self, read_sqlite_table):
def test_cpu_usage(self, metric):
"""
Verify the cpu metrics for the test
"""
for metric in read_sqlite_table:
assert (
metric.cpu_usage < metric._CPU_USAGE_MAX
), f"Test {metric.test_name} does not cpu usage max : {metric._CPU_USAGE_MAX} (%)"
assert (
metric.cpu_usage < metric._CPU_USAGE_MAX
), f"Test {metric.test_variant} does not cpu usage max : {metric._CPU_USAGE_MAX} (%)"

def test_mem_usage(self, read_sqlite_table):
def test_mem_usage(self, metric):
"""
Verify the memory metrics for the test
"""
for metric in read_sqlite_table:
assert (
metric.mem_usage < metric._MEM_USAGE_MAX
), f"Test {metric.test_name} does not respect memory usage max : {metric._MEM_USAGE_MAX} (megabyte)"
assert (
metric.mem_usage < metric._MEM_USAGE_MAX
), f"Test {metric.test_variant} does not respect memory usage max : {metric._MEM_USAGE_MAX} (megabyte)"
Loading

0 comments on commit 507e8ca

Please sign in to comment.