On Thu, Apr 4, 2013 at 11:30 AM, Helga Velroyen <hel...@google.com> wrote:

> This patch is a preparation for later patches in QA in this series
> it refactors the instance QA to make it more flexible regarding
> creation of instances with different disk templates. Right now we
> only support creation of instances with disk templates 'drbd', 'plain',
> and 'diskless'. With our current plans to improve storage handling,
> we should make QA more flexible to also create instances of other
> templates. This patch restructures the code in a way that instances
> can be created more easily to be used in other QA scripts.
>
> Signed-off-by: Helga Velroyen <hel...@google.com>
> ---
>  qa/qa_instance.py | 88
> ++++++++++++++++++++++++++++++++++++++++++++++++++-----
>  1 file changed, 80 insertions(+), 8 deletions(-)
>
> diff --git a/qa/qa_instance.py b/qa/qa_instance.py
> index d3b2f25..298ff2f 100644
> --- a/qa/qa_instance.py
> +++ b/qa/qa_instance.py
> @@ -67,13 +67,25 @@ def _GetGenericAddParameters(inst, disk_template,
> force_mac=None):
>    return params
>
>
> -def _DiskTest(node, disk_template, fail=False):
> +def _CreateInstanceByDiskTemplateRaw(nodes_spec, disk_template,
> fail=False):
> +  """Creates an instance with the given disk template on the given
> nodes(s).
> +     Note that this function does not check if enough nodes are given for
> +     the respective disk template.
> +
> +  @type nodes_spec: string
> +  @param nodes_spec: string specification of one node (by node name) or
> several
> +                     nodes according to the requirements of the disk
> template
> +  @type disk_template: string
> +  @param disk_template: the disk template to be used by the instance
> +  @return: the created instance
> +
> +  """
>    instance = qa_config.AcquireInstance()
>    try:
>      cmd = (["gnt-instance", "add",
>              "--os-type=%s" % qa_config.get("os"),
>              "--disk-template=%s" % disk_template,
> -            "--node=%s" % node] +
> +            "--node=%s" % nodes] +
>

Shouldn't this be nodes_spec now?


>              _GetGenericAddParameters(instance, disk_template))
>      cmd.append(instance.name)
>
> @@ -94,6 +106,68 @@ def _DiskTest(node, disk_template, fail=False):
>    return None
>
>
> +def _CreateInstanceByDiskTemplateOneNode(nodes, disk_template,
> fail=False):
> +  """Creates an instance using the given disk template for disk templates
> +     for which one given node is sufficient. These templates are for
> example:
> +     plain, diskless, file, sharedfile, blockdev, rados.
> +
> +  @type nodes: list of nodes
> +  @param nodes: a list of nodes, whose first element is used to create the
> +                instance
> +  @type disk_template: string
> +  @param disk_template: the disk template to be used by the instance
> +  @return: the created instance
> +
> +  """
> +  assert len(nodes) > 0
> +  return _CreateInstanceByDiskTemplateRaw(nodes[0].primary, disk_template,
> +                                          fail=fail)
> +
> +
> +def _CreateInstanceDrbd8(nodes, fail=False):
> +  """Creates an instance using disk template 'drbd' on the given nodes.
> +
> +  @type nodes: list of nodes
> +  @param nodes: nodes to be used by the instance
> +  @return: the created instance
> +
> +  """
> +  assert len(nodes) > 1
> +  return _CreateInstanceByDiskTemplateRaw(
> +    ":".join(map(operator.attrgetter("primary"), nodes)),
> +    constants.DT_DRBD8, fail=fail)
> +
> +
> +def CreateInstanceByDiskTemplate(nodes, disk_template, fail=False):
> +  """Given a disk template, this function creates an instance using
> +     the template. It uses the required number of nodes depending on
> +     the disk template. This function is intended to be used by tests
> +     that don't care about the specifics of the instance other than
> +     that it uses the given disk template.
> +
> +     Note: If you use this function, make sure to call
> +     'TestInstanceRemove' at the end of your tests to avoid orphaned
> +     instances hanging around and interfering with the following tests.
> +
> +  @type nodes: list of nodes
> +  @param nodes: the list of the nodes on which the instance will be
> placed;
> +                it needs to have sufficiently many elements for the given
> +                disk template
> +  @type disk_template: string
> +  @param disk_template: the disk template to be used by the instance
> +  @return: the created instance
> +
> +  """
> +  if disk_template == constants.DT_DRBD8:
> +    return _CreateInstanceDrbd8(nodes, fail=fail)
> +  elif disk_template in [constants.DT_DISKLESS, constants.DT_PLAIN]:
> +    return _CreateInstanceByDiskTemplateOneNode(nodes, disk_template,
> fail=fail)
> +  else:
> +    #FIXME: Implement this for the remaining disk templates
> +    qa_error.Error("Instance creation not implemented for disk type
> '%s'." %
> +                   disk_template)
> +
> +
>  def _GetInstanceInfo(instance):
>    """Return information about the actual state of an instance.
>
> @@ -275,8 +349,8 @@ def IsDiskReplacingSupported(instance):
>
>  def TestInstanceAddWithPlainDisk(nodes, fail=False):
>    """gnt-instance add -t plain"""
> -  assert len(nodes) == 1
> -  instance = _DiskTest(nodes[0].primary, constants.DT_PLAIN, fail=fail)
> +  instance = _CreateInstanceByDiskTemplateOneNode(nodes,
> constants.DT_PLAIN,
> +                                                  fail=fail)
>    if not fail:
>      qa_utils.RunInstanceCheck(instance, True)
>    return instance
> @@ -285,9 +359,7 @@ def TestInstanceAddWithPlainDisk(nodes, fail=False):
>  @InstanceCheck(None, INST_UP, RETURN_VALUE)
>  def TestInstanceAddWithDrbdDisk(nodes):
>    """gnt-instance add -t drbd"""
> -  assert len(nodes) == 2
> -  return _DiskTest(":".join(map(operator.attrgetter("primary"), nodes)),
> -                   constants.DT_DRBD8)
> +  return _CreateInstanceDrbd8(nodes)
>
>
>  @InstanceCheck(None, INST_UP, RETURN_VALUE)
> @@ -301,7 +373,7 @@ def TestInstanceAddFile(nodes):
>  def TestInstanceAddDiskless(nodes):
>    """gnt-instance add -t diskless"""
>    assert len(nodes) == 1
> -  return _DiskTest(nodes[0].primary, constants.DT_DISKLESS)
> +  return _CreateInstanceByDiskTemplateOneNode(nodes,
> constants.DT_DISKLESS)
>
>
>  @InstanceCheck(None, INST_DOWN, FIRST_ARG)
> --
> 1.8.1.3
>
>
LGTM, thanks.
Michele

Reply via email to