From c456369e7b3d4935b68f701b8a153936ef2392dc Mon Sep 17 00:00:00 2001 From: wmousa Date: Wed, 2 Sep 2026 13:18:05 +0200 Subject: [PATCH] fix(pool): set_pool crashed on an omitted size argument `sbctl pool set --pool-max 4TB` failed with Operation failed: '>' not supported between instances of 'NoneType' and 'int' The `set` subcommand declares no argparse default for --pool-max/--lvol-max (unlike `add`, which defaults both to '0'), so argparse supplies None for whichever the user omits. clibase.storage_pool__set forwards args.pool_max/args.lvol_max positionally and unconditionally, so that None reached set_pool and overrode its own `=0` default. None then fell through the `== 0` guard (None != 0) straight into `> 0` and raised. The reported command actually died on the omitted --lvol-max, not on --pool-max; passing --lvol-max alone hits the same crash in the pool_max branch. Fix by treating None as "not specified, leave unchanged" rather than coercing it to 0. Coercion would have been wrong: 0 is a meaningful value here -- it resets the limit to unlimited -- so `pool_max = pool_max or 0` would turn every partial update into a silent reset of the size the caller did not mention. That also fixes the same latent bug on the v1 API path, which only forwards the keys present in the request body. Note add_pool already normalises both sizes (`pool_max = pool_max or 0`); set_pool simply never got the equivalent treatment. Also fixes a field mix-up in the same block: the `lvol_max > 0` branch assigned to `pool.pool_max_size` instead of `pool.lvol_max_size`, so a non-zero --lvol-max silently overwrote the pool-wide limit and never set the per-lvol limit at all. The `== 0` branch three lines above already used the correct field, which is what makes the mismatch visible. Tests cover the cases nothing exercised before: each size omitted, both omitted, explicit zero still resetting, negatives still rejected, and lvol_max writing its own field without clobbering pool_max_size. They fail 9/10 against the previous code (including the exact reported TypeError) and pass with this change. --- .../controllers/pool_controller.py | 76 +++++---- tests/test_pool_set_size_args.py | 148 ++++++++++++++++++ 2 files changed, 197 insertions(+), 27 deletions(-) create mode 100644 tests/test_pool_set_size_args.py diff --git a/simplyblock_core/controllers/pool_controller.py b/simplyblock_core/controllers/pool_controller.py index 5074f28533..2a5618fce0 100644 --- a/simplyblock_core/controllers/pool_controller.py +++ b/simplyblock_core/controllers/pool_controller.py @@ -205,9 +205,19 @@ def qos_exists_on_child_lvol(db_controller: DBController, pool_uuid): return True return False -def set_pool(uuid, pool_max=0, lvol_max=0, max_rw_iops=0, +def set_pool(uuid, pool_max=None, lvol_max=None, max_rw_iops=0, max_rw_mbytes=0, max_r_mbytes=0, max_w_mbytes=0, name="", lvols_cr_name="", lvols_cr_namespace="", lvols_cr_plural=""): + """Update a pool. ``pool_max``/``lvol_max`` of None mean "not specified, + leave unchanged"; 0 means "explicitly reset to unlimited". + + They default to None rather than 0 because this is a partial update: the + CLI passes ``args.pool_max``/``args.lvol_max`` unconditionally and argparse + supplies None for a flag the user omitted (the `set` subcommand declares no + default, unlike `add`), and the v1 API only forwards the keys present in the + request body. With a 0 default, omitting one size silently reset that field + to unlimited instead of preserving it. + """ db_controller = DBController() try: pool = db_controller.get_pool_by_id(uuid) @@ -285,37 +295,49 @@ def set_pool(uuid, pool_max=0, lvol_max=0, max_rw_iops=0, if err: return False, err - if pool_max == 0: - pool.pool_max_size = 0 - elif pool_max > 0: - total_lvol_size = 0 - for lvol in db_controller.get_lvols_by_pool_id(uuid): - total_lvol_size += lvol.size - if total_lvol_size > pool_max: - msg = f"Pool max size can't be less than total provisioned size of lvols: {utils.humanbytes(total_lvol_size)}" + # None => the caller did not specify this size, so leave it alone. Without + # this guard None fell through `== 0` (False) into `> 0` and raised + # "TypeError: '>' not supported between instances of 'NoneType' and 'int'", + # which is what `pool set --pool-max 4TB` hit: --lvol-max was omitted, + # so args.lvol_max was None and got passed positionally regardless. + if pool_max is not None: + if pool_max == 0: + pool.pool_max_size = 0 + elif pool_max > 0: + total_lvol_size = 0 + for lvol in db_controller.get_lvols_by_pool_id(uuid): + total_lvol_size += lvol.size + if total_lvol_size > pool_max: + msg = f"Pool max size can't be less than total provisioned size of lvols: {utils.humanbytes(total_lvol_size)}" + logger.error(msg) + return False, msg + pool.pool_max_size = pool_max + else: + msg = "pool_max can not be negative" logger.error(msg) return False, msg - pool.pool_max_size = pool_max - else: - msg = "pool_max can not be negative" - logger.error(msg) - return False, msg - if lvol_max == 0: - pool.lvol_max_size = 0 - elif lvol_max > 0: - lvol_size_max = 0 - for lvol in db_controller.get_lvols_by_pool_id(uuid): - lvol_size_max = max(lvol_size_max, lvol.size) - if lvol_size_max > lvol_max: - msg = f"LVol max size can't be less than max provisioned size of lvols: {utils.humanbytes(lvol_size_max)}" + if lvol_max is not None: + if lvol_max == 0: + pool.lvol_max_size = 0 + elif lvol_max > 0: + lvol_size_max = 0 + for lvol in db_controller.get_lvols_by_pool_id(uuid): + lvol_size_max = max(lvol_size_max, lvol.size) + if lvol_size_max > lvol_max: + msg = f"LVol max size can't be less than max provisioned size of lvols: {utils.humanbytes(lvol_size_max)}" + logger.error(msg) + return False, msg + # Was `pool.pool_max_size = lvol_max`, which wrote the per-lvol + # limit into the whole-pool limit -- so a non-zero --lvol-max + # silently overwrote pool_max_size and never set lvol_max_size at + # all. The `== 0` branch above already used the right field, which + # is what makes the mismatch visible. + pool.lvol_max_size = lvol_max + else: + msg = "lvol_max can not be negative" logger.error(msg) return False, msg - pool.pool_max_size = lvol_max - else: - msg = "lvol_max can not be negative" - logger.error(msg) - return False, msg # Apply QoS settings via RPC for hostname in db_controller.get_hostnames_by_pool_id(uuid): diff --git a/tests/test_pool_set_size_args.py b/tests/test_pool_set_size_args.py new file mode 100644 index 0000000000..b79c0a85ed --- /dev/null +++ b/tests/test_pool_set_size_args.py @@ -0,0 +1,148 @@ +"""set_pool()'s size arguments: omitted vs explicitly zero vs positive. + +`pool set --pool-max 4TB` crashed with + + TypeError: '>' not supported between instances of 'NoneType' and 'int' + +because the `set` subcommand declares no argparse default for --pool-max / +--lvol-max (unlike `add`, which defaults both to '0'), while +clibase.storage_pool__set passes args.pool_max/args.lvol_max positionally and +unconditionally. Omitting one therefore forwarded None, which fell through the +`== 0` guard straight into `> 0`. + +None must mean "not specified, leave unchanged" rather than 0, because 0 is a +meaningful value here: it resets the limit to unlimited. Coercing None to 0 +would have turned every partial update into a silent reset of the other size. + +Also covers the field mix-up in the lvol_max branch, which assigned to +pool_max_size instead of lvol_max_size. +""" + +import unittest +from unittest.mock import MagicMock, patch + +from simplyblock_core.controllers import pool_controller +from simplyblock_core.models.pool import Pool + +POOL_ID = "8ab9ca19-38a0-4fb6-ad71-4cfbe058db71" +TB4 = 4 * 1000 ** 4 + + +def _pool(pool_max=1000, lvol_max=500): + p = Pool() + p.uuid = POOL_ID + p.pool_name = "testpool" + p.cluster_id = "cl-1" + p.status = Pool.STATUS_ACTIVE + p.pool_max_size = pool_max + p.lvol_max_size = lvol_max + return p + + +class _Ctx: + """Patch set_pool's collaborators; expose the pool object it mutates.""" + + def __enter__(self): + self.pool = _pool() + self._db = patch.object(pool_controller, "DBController") + db_cls = self._db.start() + db = db_cls.return_value + db.get_pool_by_id.return_value = self.pool + db.get_pools.return_value = [self.pool] + db.get_lvols_by_pool_id.return_value = [] # no lvols -> no size floor + db.get_hostnames_by_pool_id.return_value = [] # no QoS RPC fan-out + db.kv_store = MagicMock() + # pool_events -> events_controller keeps its OWN module-level + # DBController, whose kv_store is None under test. Worse, + # BaseModel.write_to_db calls exit(1) when the write fails, so an + # unstubbed event turns a failing assertion into SystemExit. + self._events = patch.object(pool_controller, "pool_events", MagicMock()) + self._events.start() + self._gate = patch.object( + pool_controller, "ops_gate", MagicMock(), create=True) + try: + self._gate.start() + except Exception: + self._gate = None + return self + + def __exit__(self, *a): + if self._gate is not None: + self._gate.stop() + self._events.stop() + self._db.stop() + return False + + +class TestOmittedSizesAreLeftAlone(unittest.TestCase): + """The reported crash, plus the reset-on-partial-update it was hiding.""" + + def test_pool_max_only_does_not_raise(self): + with _Ctx() as ctx: + ok, err = pool_controller.set_pool(POOL_ID, TB4, None) + self.assertTrue(ok, err) + self.assertEqual(ctx.pool.pool_max_size, TB4) + + def test_pool_max_only_preserves_lvol_max(self): + """Omitting --lvol-max must not reset it; 0 means unlimited, so + coercing None to 0 would silently wipe the existing limit.""" + with _Ctx() as ctx: + pool_controller.set_pool(POOL_ID, TB4, None) + self.assertEqual(ctx.pool.lvol_max_size, 500) + + def test_lvol_max_only_does_not_raise_and_preserves_pool_max(self): + with _Ctx() as ctx: + ok, err = pool_controller.set_pool(POOL_ID, None, 2000) + self.assertTrue(ok, err) + self.assertEqual(ctx.pool.pool_max_size, 1000, "pool_max must be untouched") + + def test_both_omitted_changes_neither(self): + with _Ctx() as ctx: + ok, _ = pool_controller.set_pool(POOL_ID, None, None) + self.assertTrue(ok) + self.assertEqual((ctx.pool.pool_max_size, ctx.pool.lvol_max_size), (1000, 500)) + + +class TestLvolMaxWritesItsOwnField(unittest.TestCase): + """The lvol_max branch assigned to pool_max_size.""" + + def test_lvol_max_sets_lvol_max_size(self): + with _Ctx() as ctx: + ok, err = pool_controller.set_pool(POOL_ID, None, 2000) + self.assertTrue(ok, err) + self.assertEqual(ctx.pool.lvol_max_size, 2000) + + def test_lvol_max_does_not_clobber_pool_max_size(self): + with _Ctx() as ctx: + pool_controller.set_pool(POOL_ID, None, 2000) + self.assertNotEqual(ctx.pool.pool_max_size, 2000, + "lvol limit leaked into the pool-wide limit") + + +class TestExplicitZeroStillResets(unittest.TestCase): + """0 is a real value -- 'unlimited' -- and must keep working.""" + + def test_zero_pool_max_resets(self): + with _Ctx() as ctx: + pool_controller.set_pool(POOL_ID, 0, None) + self.assertEqual(ctx.pool.pool_max_size, 0) + + def test_zero_lvol_max_resets(self): + with _Ctx() as ctx: + pool_controller.set_pool(POOL_ID, None, 0) + self.assertEqual(ctx.pool.lvol_max_size, 0) + + +class TestNegativeStillRejected(unittest.TestCase): + + def test_negative_pool_max(self): + with _Ctx(): + ok, err = pool_controller.set_pool(POOL_ID, -1, None) + self.assertFalse(ok) + self.assertIn("negative", err) + + def test_negative_lvol_max(self): + with _Ctx(): + ok, err = pool_controller.set_pool(POOL_ID, None, -1) + self.assertFalse(ok) + self.assertIn("negative", err)