]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
cephadm: split command_deploy_from into mockable parts
authorJohn Mulligan <jmulligan@redhat.com>
Sat, 20 May 2023 17:42:02 +0000 (13:42 -0400)
committerJohn Mulligan <jmulligan@redhat.com>
Thu, 15 Jun 2023 20:35:34 +0000 (16:35 -0400)
Split up command_deploy_from so that the part that reads the JSON
from an input source is moved into read_configuration_source and
the part that applied the configuration data to the ctx is moved
into apply_deploy_config_to_ctx. This will make these sections
reusable and, more importantly, make it easier to mock/monkeypatch
these functions in tests.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
src/cephadm/cephadm.py

index 847bb3f8e63b228d985a4642f0acda6fcf724766..c2d1e8f0e2f846098bc8486bf5a5766b08dc169f 100755 (executable)
@@ -6259,10 +6259,8 @@ def command_deploy(ctx):
     _common_deploy(ctx, daemon_type, daemon_id, daemon_ports, deployment_type)
 
 
-def command_deploy_from(ctx: CephadmContext) -> None:
-    """The deploy-from command is similar to deploy but sources nearly all
-    configuration parameters from an input JSON configuration file.
-    """
+def read_configuration_source(ctx: CephadmContext) -> Dict[str, Any]:
+    """Read a JSON configuration based on the `ctx.source` value."""
     source = '-'
     if 'source' in ctx and ctx.source:
         source = ctx.source
@@ -6272,9 +6270,16 @@ def command_deploy_from(ctx: CephadmContext) -> None:
         with open(source, 'rb') as fh:
             config_data = json.load(fh)
     logger.debug('Loaded deploy configuration: %r', config_data)
+    return config_data
 
-    # Bind properties taken from the json to our ctx, similar to how
-    # cli options on `deploy` are bound to the context.
+
+def apply_deploy_config_to_ctx(
+    config_data: Dict[str, Any],
+    ctx: CephadmContext,
+) -> None:
+    """Bind properties taken from the config_data dictionary to our ctx,
+    similar to how cli options on `deploy` are bound to the context.
+    """
     ctx.name = config_data['name']
     if 'image' in config_data:
         ctx.image = config_data['image']
@@ -6306,6 +6311,14 @@ def command_deploy_from(ctx: CephadmContext) -> None:
     update_default_image(ctx)
     logger.debug('Determined image: %r', ctx.image)
 
+
+def command_deploy_from(ctx: CephadmContext) -> None:
+    """The deploy-from command is similar to deploy but sources nearly all
+    configuration parameters from an input JSON configuration file.
+    """
+    config_data = read_configuration_source(ctx)
+    apply_deploy_config_to_ctx(config_data, ctx)
+
     daemon_type, daemon_id = ctx.name.split('.', 1)
 
     lock = FileLock(ctx, ctx.fsid)