Unify two very similar functions which query the ganeti cluster for job statuses during QA.
Signed-off-by: Thomas Thrainer <[email protected]> --- qa/qa_job.py | 25 +++++-------------------- qa/qa_job_utils.py | 18 ++++++++++++++++++ qa/qa_performance.py | 10 +++------- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/qa/qa_job.py b/qa/qa_job.py index ed2bf4a..95b5c98 100644 --- a/qa/qa_job.py +++ b/qa/qa_job.py @@ -23,17 +23,16 @@ """ -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): diff --git a/qa/qa_job_utils.py b/qa/qa_job_utils.py index 6d35ecb..d8ea4e5 100644 --- a/qa/qa_job_utils.py +++ b/qa/qa_job_utils.py @@ -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. diff --git a/qa/qa_performance.py b/qa/qa_performance.py index bc2c723..e666045 100644 --- a/qa/qa_performance.py +++ b/qa/qa_performance.py @@ -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 -- 1.9.1.423.g4596e3a
