]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
qa/tasks: change map_vips to raise exceptions instead of returning None
authorJohn Mulligan <jmulligan@redhat.com>
Tue, 20 Feb 2024 00:14:52 +0000 (19:14 -0500)
committerJohn Mulligan <jmulligan@redhat.com>
Thu, 29 Feb 2024 15:00:29 +0000 (10:00 -0500)
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 <jmulligan@redhat.com>
qa/tasks/vip.py

index 52114b1042285268782a05f932b112bf8e1df14c..8e5f44efd6d5866720e10f5b8e916d017e6d79d6 100644 (file)
@@ -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