Commit e44b72bb authored by Petr Pudlak's avatar Petr Pudlak Committed by Petr Pudlak
Browse files

Allow QA node acquisition and group creation using `with`

For acquiring nodes use `with AcquireManyNodes(num): ...`. The nodes
will be released automatically.
For creating a new group use `with NewGroupCtx() as group: ...`. The
group will be removed automatically.
Signed-off-by: default avatarPetr Pudlak <>
Reviewed-by: default avatarHrvoje Ribicic <>
parent 70e6a07e
......@@ -757,7 +757,25 @@ def AcquireNode(exclude=None, _cfg=None):
return sorted(nodes, key=_NodeSortKey)[0].Use()
def AcquireManyNodes(num, exclude=None):
class AcquireManyNodesCtx(object):
"""Returns the least used nodes for use with a `with` block
def __init__(self, num, exclude=None, cfg=None):
self._num = num
self._exclude = exclude
self._cfg = cfg
def __enter__(self):
self._nodes = AcquireManyNodes(self._num, exclude=self._exclude,
return self._nodes
def __exit__(self, exc_type, exc_value, exc_tb):
def AcquireManyNodes(num, exclude=None, cfg=None):
"""Return the least used nodes.
@type num: int
......@@ -779,7 +797,7 @@ def AcquireManyNodes(num, exclude=None):
for _ in range(0, num):
n = AcquireNode(exclude=exclude)
n = AcquireNode(exclude=exclude, _cfg=cfg)
except qa_error.OutOfNodesError:
......@@ -127,6 +127,18 @@ def TestGroupAddWithOptions():
AssertCommand(["gnt-group", "remove", group1])
class NewGroupCtx(object):
"""Creates a new group and disposes afterwards."""
def __enter__(self):
(self._group, ) = qa_utils.GetNonexistentGroups(1)
AssertCommand(["gnt-group", "add", self._group])
return self._group
def __exit__(self, exc_type, exc_val, exc_tb):
AssertCommand(["gnt-group", "remove", self._group])
def _GetGroupIPolicy(groupname):
"""Return the run-time values of the cluster-level instance policy.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment