From fff3b704de338254217a47568dfc9884b6e373f2 Mon Sep 17 00:00:00 2001 From: Josh Durgin Date: Sun, 2 Feb 2020 16:29:13 -0500 Subject: [PATCH] mgr/pg_autoscaler: add unit tests for effective_target_ratio Signed-off-by: Josh Durgin --- src/pybind/mgr/pg_autoscaler/__init__.py | 2 +- .../pg_autoscaler/tests/test_autoscaler.py | 34 +++++++++++++++++++ src/pybind/mgr/requirements.txt | 3 +- src/pybind/mgr/tox.ini | 2 +- 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 src/pybind/mgr/pg_autoscaler/tests/test_autoscaler.py diff --git a/src/pybind/mgr/pg_autoscaler/__init__.py b/src/pybind/mgr/pg_autoscaler/__init__.py index e7c7b8fc01b..f0bffcdcd62 100644 --- a/src/pybind/mgr/pg_autoscaler/__init__.py +++ b/src/pybind/mgr/pg_autoscaler/__init__.py @@ -1 +1 @@ -from .module import PgAutoscaler +from .module import PgAutoscaler, effective_target_ratio diff --git a/src/pybind/mgr/pg_autoscaler/tests/test_autoscaler.py b/src/pybind/mgr/pg_autoscaler/tests/test_autoscaler.py new file mode 100644 index 00000000000..122d95274d0 --- /dev/null +++ b/src/pybind/mgr/pg_autoscaler/tests/test_autoscaler.py @@ -0,0 +1,34 @@ +from pg_autoscaler import effective_target_ratio +from pytest import approx + +def check_simple_ratio(target_ratio, tot_ratio): + etr = effective_target_ratio(target_ratio, tot_ratio, 0, 0) + assert (target_ratio / tot_ratio) == approx(etr) + return etr + +def test_simple(): + etr1 = check_simple_ratio(0.2, 0.9) + etr2 = check_simple_ratio(2, 9) + etr3 = check_simple_ratio(20, 90) + assert etr1 == approx(etr2) + assert etr1 == approx(etr3) + + etr = check_simple_ratio(0.9, 0.9) + assert etr == approx(1.0) + etr1 = check_simple_ratio(1, 2) + etr2 = check_simple_ratio(0.5, 1.0) + assert etr1 == approx(etr2) + +def test_total_bytes(): + etr = effective_target_ratio(1, 10, 5, 10) + assert etr == approx(0.05) + etr = effective_target_ratio(0.1, 1, 5, 10) + assert etr == approx(0.05) + etr = effective_target_ratio(1, 1, 5, 10) + assert etr == approx(0.5) + etr = effective_target_ratio(1, 1, 0, 10) + assert etr == approx(1.0) + etr = effective_target_ratio(0, 1, 5, 10) + assert etr == approx(0.0) + etr = effective_target_ratio(1, 1, 10, 10) + assert etr == approx(0.0) diff --git a/src/pybind/mgr/requirements.txt b/src/pybind/mgr/requirements.txt index 21a5b35af45..f14efb8cd80 100644 --- a/src/pybind/mgr/requirements.txt +++ b/src/pybind/mgr/requirements.txt @@ -4,4 +4,5 @@ ipaddress; python_version < '3.3' ../../python-common kubernetes requests-mock -pyyaml \ No newline at end of file +pyyaml +prettytable diff --git a/src/pybind/mgr/tox.ini b/src/pybind/mgr/tox.ini index ac3424e87cb..d9ea1ef5fd0 100644 --- a/src/pybind/mgr/tox.ini +++ b/src/pybind/mgr/tox.ini @@ -5,7 +5,7 @@ skipsdist = true [testenv] setenv = UNITTEST = true deps = -r requirements.txt -commands = pytest -v --cov --cov-append --cov-report=term --doctest-modules {posargs:mgr_util.py tests/ cephadm/ progress/} +commands = pytest -v --cov --cov-append --cov-report=term --doctest-modules {posargs:mgr_util.py tests/ cephadm/ pg_autoscaler/ progress/} [testenv:mypy] basepython = python3 -- 2.47.3