Extract GetJobStatuses and use an unified version
authorThomas Thrainer <thomasth@google.com>
Mon, 12 May 2014 08:31:18 +0000 (10:31 +0200)
committerThomas Thrainer <thomasth@google.com>
Mon, 12 May 2014 09:25:29 +0000 (11:25 +0200)
Unify two very similar functions which query the ganeti cluster for job
statuses during QA.

Signed-off-by: Thomas Thrainer <thomasth@google.com>
Reviewed-by: Klaus Aehlig <aehlig@google.com>

qa/qa_job.py
qa/qa_job_utils.py
qa/qa_performance.py

index ed2bf4a..95b5c98 100644 (file)
 
 """
 
-from ganeti.utils import retry
-from ganeti import constants
-from ganeti import query
-
 import functools
 import re
 
+from ganeti.utils import retry
+from ganeti import constants
+from ganeti import query
 import qa_config
 import qa_error
+import qa_job_utils
 import qa_utils
-
 from qa_utils import AssertCommand, GetCommandOutput
 
 
@@ -48,20 +47,6 @@ def TestJobListFields():
   qa_utils.GenericQueryFieldsTest("gnt-job", query.JOB_FIELDS.keys())
 
 
-def _GetJobStatuses():
-  """ Invokes gnt-job list and extracts an id to status dictionary.
-
-  @rtype: dict of string to string
-  @return: A dictionary mapping job ids to matching statuses
-
-  """
-  master = qa_config.GetMasterNode()
-  list_output = GetCommandOutput(
-    master.primary, "gnt-job list --no-headers --output=id,status"
-  )
-  return dict(map(lambda s: s.split(), list_output.splitlines()))
-
-
 def _GetJobStatus(job_id):
   """ Retrieves the status of a job.
 
@@ -72,7 +57,7 @@ def _GetJobStatus(job_id):
   @return: The job status, or None if not present.
 
   """
-  return _GetJobStatuses().get(job_id, None)
+  return qa_job_utils.GetJobStatuses([job_id]).get(job_id, None)
 
 
 def _RetryingFetchJobStatus(retry_status, job_id):
index 6d35ecb..d8ea4e5 100644 (file)
@@ -82,6 +82,24 @@ def ExecuteJobProducingCommand(cmd):
   return int(possible_job_ids[0])
 
 
+def GetJobStatuses(job_ids=None):
+  """ Invokes gnt-job list and extracts an id to status dictionary.
+
+  @type job_ids: list
+  @param job_ids: list of job ids to query the status for; if C{None}, the
+                  status of all current jobs is returned
+  @rtype: dict of string to string
+  @return: A dictionary mapping job ids to matching statuses
+
+  """
+  cmd = ["gnt-job", "list", "--no-headers", "--output=id,status"]
+  if job_ids is not None:
+    cmd.extend(map(str, job_ids))
+
+  list_output = GetOutputFromMaster(cmd)
+  return dict(map(lambda s: s.split(), list_output.splitlines()))
+
+
 def _RetrieveTerminationInfo(job_id):
   """ Retrieves the termination info from a job caused by gnt-debug delay.
 
index bc2c723..e666045 100644 (file)
@@ -96,15 +96,11 @@ class _JobQueueDriver(object):
   def _FetchJobStatuses(self):
     """Retrieves status information of the given jobs.
 
-    @rtype: dict of string to list of L{_JobEntry)
-
     """
-    cmd = (["gnt-job", "list", "--no-headers", "-o", "id,status"])
-    cmd.extend(map(str, self._GetJobIds()))
-    job_statuses = [line.split() for line in
-                    qa_job_utils.GetOutputFromMaster(cmd).splitlines()]
+    job_statuses = qa_job_utils.GetJobStatuses(self._GetJobIds())
+
     new_statuses = {}
-    for job_id, status in job_statuses:
+    for job_id, status in job_statuses.items():
       new_statuses.setdefault(status, []).append(self._jobs[int(job_id)])
     self._jobs_per_status = new_statuses