From 52a924e7fd4b84801b8606436f287ff15c46f40a Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Mon, 19 Feb 2024 19:14:52 -0500 Subject: [PATCH] qa/tasks: change map_vips to raise exceptions instead of returning None None of the callers of map_vips ever checks for a None return. So instead of handling any error conditions it would always just blow up with a semi-obscure TypeError. Convert the function to always raise an exception (one that tries to breifly explain the condition) when something goes wrong. I also take the opportunity to make more clearer logging and reduce an indentation level. Signed-off-by: John Mulligan --- qa/tasks/vip.py | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/qa/tasks/vip.py b/qa/tasks/vip.py index 52114b1042285..8e5f44efd6d58 100644 --- a/qa/tasks/vip.py +++ b/qa/tasks/vip.py @@ -5,6 +5,7 @@ import re from teuthology import misc as teuthology from teuthology.config import config as teuth_config +from teuthology.exceptions import ConfigError log = logging.getLogger(__name__) @@ -69,23 +70,32 @@ def exec(ctx, config): def map_vips(mip, count): - for mapping in teuth_config.get('vip', []): + vip_entries = teuth_config.get('vip', []) + if not vip_entries: + raise ConfigError( + 'at least one item must be configured for "vip" config key' + ' to use the vip task' + ) + for mapping in vip_entries: mnet = ipaddress.ip_network(mapping['machine_subnet']) vnet = ipaddress.ip_network(mapping['virtual_subnet']) if vnet.prefixlen >= mnet.prefixlen: log.error(f"virtual_subnet {vnet} prefix >= machine_subnet {mnet} prefix") - return None - if mip in mnet: - pos = list(mnet.hosts()).index(mip) - log.info(f"{mip} in {mnet}, pos {pos}") - r = [] - for sub in vnet.subnets(new_prefix=mnet.prefixlen): - r += [list(sub.hosts())[pos]] - count -= 1 - if count == 0: - break - return vnet, r - return None + raise ConfigError('virtual subnet too small') + if mip not in mnet: + # not our machine subnet + log.info(f"machine ip {mip} not in machine subnet {mnet}") + continue + pos = list(mnet.hosts()).index(mip) + log.info(f"{mip} in {mnet}, pos {pos}") + r = [] + for sub in vnet.subnets(new_prefix=mnet.prefixlen): + r += [list(sub.hosts())[pos]] + count -= 1 + if count == 0: + break + return vnet, r + raise ConfigError(f"no matching machine subnet found for {mip}") @contextlib.contextmanager -- 2.39.5