From 6998aefe24f4782f836497724a49931486aa863b Mon Sep 17 00:00:00 2001 From: Michael Hanselmann <hansmi@google.com> Date: Tue, 5 Feb 2013 16:32:30 +0100 Subject: [PATCH] QA: Drop dictionary support from qa_utils._GetName This is no longer needed now that instance/node objects in QA have attributes. Use a helper function instead to retrieve an entity's identifying attribute. Signed-off-by: Michael Hanselmann <hansmi@google.com> Reviewed-by: Bernardo Dal Seno <bdalseno@google.com> --- qa/qa_utils.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/qa/qa_utils.py b/qa/qa_utils.py index 614941472..68656730b 100644 --- a/qa/qa_utils.py +++ b/qa/qa_utils.py @@ -29,6 +29,7 @@ import sys import subprocess import random import tempfile +import operator try: import functools @@ -129,21 +130,17 @@ def AssertMatch(string, pattern): raise qa_error.Error("%r doesn't match /%r/" % (string, pattern)) -def _GetName(entity, key): +def _GetName(entity, fn): """Tries to get name of an entity. @type entity: string or dict - @type key: string - @param key: Dictionary key containing name + @param fn: Function retrieving name from entity """ if isinstance(entity, basestring): result = entity - elif isinstance(entity, dict) or hasattr(entity, "__getitem__"): - result = entity[key] else: - raise qa_error.Error("Expected string or dictionary, got %s: %s" % - (type(entity), entity)) + result = fn(entity) if not ht.TNonEmptyString(result): raise Exception("Invalid name '%s'" % result) @@ -182,7 +179,7 @@ def AssertCommand(cmd, fail=False, node=None, log_cmd=True): if node is None: node = qa_config.GetMasterNode() - nodename = _GetName(node, "primary") + nodename = _GetName(node, operator.attrgetter("primary")) if isinstance(cmd, basestring): cmdstr = cmd @@ -614,7 +611,7 @@ def RunInstanceCheck(instance, running): """Check if instance is running or not. """ - instance_name = _GetName(instance, "name") + instance_name = _GetName(instance, operator.attrgetter("name")) script = qa_config.GetInstanceCheckScript() if not script: -- GitLab