]> git-server-git.apps.pok.os.sepia.ceph.com Git - teuthology.git/commitdiff
Make *_if_vm() more unit-testable
authorZack Cerza <zack@redhat.com>
Thu, 9 Apr 2015 16:30:45 +0000 (10:30 -0600)
committerZack Cerza <zack@redhat.com>
Thu, 9 Apr 2015 19:39:32 +0000 (13:39 -0600)
Allow passing in a custom Downburst instance. Also clean up several
linter issues.

Signed-off-by: Zack Cerza <zack@redhat.com>
teuthology/provision.py

index abcea824f0f775ce77084a0a636f14e581ff7ac6..b283efb36986e4640ef80ebf5be78aab5554e6e8 100644 (file)
@@ -172,11 +172,16 @@ class Downburst(object):
         self.remove_config()
 
 
-def create_if_vm(ctx, machine_name):
+def create_if_vm(ctx, machine_name, _downburst=None):
     """
     Use downburst to create a virtual machine
+
+    :param _downburst: Only used for unit testing.
     """
-    status_info = get_status(machine_name)
+    if _downburst:
+        status_info = _downburst.status
+    else:
+        status_info = get_status(machine_name)
     if not status_info.get('is_vm', False):
         return False
     os_type = get_distro(ctx)
@@ -188,30 +193,41 @@ def create_if_vm(ctx, machine_name):
             'Usage of a custom downburst config has been deprecated.'
         )
 
-    dbrst = Downburst(name=machine_name, os_type=os_type,
-                      os_version=os_version, status=status_info)
+    dbrst = _downburst or Downburst(name=machine_name, os_type=os_type,
+                                    os_version=os_version, status=status_info)
     return dbrst.create()
 
 
-def destroy_if_vm(ctx, machine_name, user=None, description=None):
+def destroy_if_vm(ctx, machine_name, user=None, description=None,
+                  _downburst=None):
     """
     Use downburst to destroy a virtual machine
 
     Return False only on vm downburst failures.
+
+    :param _downburst: Only used for unit testing.
     """
-    status_info = get_status(machine_name)
+    if _downburst:
+        status_info = _downburst.status
+    else:
+        status_info = get_status(machine_name)
     os_type = get_distro(ctx)
     os_version = get_distro_version(ctx)
     if not status_info or not status_info.get('is_vm', False):
         return True
     if user is not None and user != status_info['locked_by']:
-        log.error("Tried to destroy {node} as {as_user} but it is locked by {locked_by}".format(
-            node=machine_name, as_user=user, locked_by=status_info['locked_by']))
+        msg = "Tried to destroy {node} as {as_user} but it is locked " + \
+            "by {locked_by}"
+        log.error(msg.format(node=machine_name, as_user=user,
+                             locked_by=status_info['locked_by']))
         return False
-    if description is not None and description != status_info['description']:
-        log.error("Tried to destroy {node} with description {desc_arg} but it is locked with description {desc_lock}".format(
-            node=machine_name, desc_arg=description, desc_lock=status_info['description']))
+    if (description is not None and description !=
+            status_info['description']):
+        msg = "Tried to destroy {node} with description {desc_arg} " + \
+            "but it is locked with description {desc_lock}"
+        log.error(msg.format(node=machine_name, desc_arg=description,
+                             desc_lock=status_info['description']))
         return False
-    dbrst = Downburst(name=machine_name, os_type=os_type,
-                      os_version=os_version, status=status_info)
+    dbrst = _downburst or Downburst(name=machine_name, os_type=os_type,
+                                    os_version=os_version, status=status_info)
     return dbrst.destroy()