[libvirt] [PATCH v2 13/15] test: Refactor testDomainSetVcpusFlags

Pavel Hrdina phrdina at redhat.com
Wed Jun 24 15:46:27 UTC 2015


On Wed, Jun 24, 2015 at 04:11:55PM +0200, Peter Krempa wrote:
> Remove the bogous flag check and refactor the code by using

s/bogous/bogus/

> virDomainObjGetDefs instead of virDomainObjGetPersistentDef.
> ---
>  src/test/test_driver.c | 68 +++++++++++++++++---------------------------------
>  1 file changed, 23 insertions(+), 45 deletions(-)
> 
> diff --git a/src/test/test_driver.c b/src/test/test_driver.c
> index 2c2f009..ae332ef 100644
> --- a/src/test/test_driver.c
> +++ b/src/test/test_driver.c
> @@ -2445,6 +2445,7 @@ testDomainSetVcpusFlags(virDomainPtr domain, unsigned int nrCpus,
>  {
>      testDriverPtr privconn = domain->conn->privateData;
>      virDomainObjPtr privdom = NULL;
> +    virDomainDefPtr def;
>      virDomainDefPtr persistentDef;
>      int ret = -1, maxvcpus;
> 
> @@ -2452,72 +2453,49 @@ testDomainSetVcpusFlags(virDomainPtr domain, unsigned int nrCpus,
>                    VIR_DOMAIN_AFFECT_CONFIG |
>                    VIR_DOMAIN_VCPU_MAXIMUM, -1);
> 
> -    /* At least one of LIVE or CONFIG must be set.  MAXIMUM cannot be
> -     * mixed with LIVE.  */
> -    if ((flags & (VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG)) == 0 ||
> -        (flags & (VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_AFFECT_LIVE)) ==
> -         (VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_AFFECT_LIVE)) {
> -        virReportError(VIR_ERR_INVALID_ARG,
> -                       _("invalid flag combination: (0x%x)"), flags);
> +    if ((maxvcpus = testConnectGetMaxVcpus(domain->conn, NULL)) == -1)
>          return -1;
> -    }
> -    if (!nrCpus || (maxvcpus = testConnectGetMaxVcpus(domain->conn, NULL)) < nrCpus) {
> +
> +    if (nrCpus > maxvcpus) {
>          virReportError(VIR_ERR_INVALID_ARG,
> -                       _("argument out of range: %d"), nrCpus);
> +                       _("requested cpu amount exceeds maximum supported amount "
> +                         "(%d > %d)"), nrCpus, maxvcpus);
>          return -1;
>      }
> 
>      if (!(privdom = testDomObjFromDomain(domain)))
>          return -1;
> 
> -    if (!virDomainObjIsActive(privdom) && (flags & VIR_DOMAIN_AFFECT_LIVE)) {
> -        virReportError(VIR_ERR_OPERATION_INVALID,
> -                       "%s", _("cannot hotplug vcpus for an inactive domain"));
> +    if (virDomainObjGetDefs(privdom, flags, &def, &persistentDef) < 0)
>          goto cleanup;
> -    }
> -
> -    /* We allow more cpus in guest than host, but not more than the
> -     * domain's starting limit.  */
> -    if (!(flags & (VIR_DOMAIN_VCPU_MAXIMUM)) &&
> -        privdom->def->maxvcpus < maxvcpus)
> -        maxvcpus = privdom->def->maxvcpus;
> 
> -    if (nrCpus > maxvcpus) {
> +    if ((def &&
> +         def->maxvcpus < nrCpus) ||
> +        (persistentDef &&
> +         !(flags & VIR_DOMAIN_VCPU_MAXIMUM) &&
> +         persistentDef->maxvcpus < nrCpus)) {
>          virReportError(VIR_ERR_INVALID_ARG,
>                         _("requested cpu amount exceeds maximum (%d > %d)"),
>                         nrCpus, maxvcpus);

As pointed in the previous review, you need to use (def|persistentDef)->maxvcpus
in the error message.

>          goto cleanup;
>      }
> 
> -    if (!(persistentDef = virDomainObjGetPersistentDef(privconn->caps,
> -                                                       privconn->xmlopt,
> -                                                       privdom)))
> +    if (def &&
> +        testDomainUpdateVCPUs(privconn, privdom, nrCpus, 0) < 0)
>          goto cleanup;
> 
> -    switch (flags) {
> -    case VIR_DOMAIN_VCPU_MAXIMUM | VIR_DOMAIN_AFFECT_CONFIG:
> -        persistentDef->maxvcpus = nrCpus;
> -        if (nrCpus < persistentDef->vcpus)
> -            persistentDef->vcpus = nrCpus;
> -        ret = 0;
> -        break;
> -
> -    case VIR_DOMAIN_AFFECT_CONFIG:
> -        persistentDef->vcpus = nrCpus;
> -        ret = 0;
> -        break;
> -
> -    case VIR_DOMAIN_AFFECT_LIVE:
> -        ret = testDomainUpdateVCPUs(privconn, privdom, nrCpus, 0);
> -        break;
> -
> -    case VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG:
> -        ret = testDomainUpdateVCPUs(privconn, privdom, nrCpus, 0);
> -        if (ret == 0)
> +    if (persistentDef) {
> +        if (flags & VIR_DOMAIN_VCPU_MAXIMUM) {
> +            persistentDef->maxvcpus = nrCpus;
> +            if (nrCpus < persistentDef->vcpus)
> +                persistentDef->vcpus = nrCpus;
> +        } else {
>              persistentDef->vcpus = nrCpus;
> -        break;
> +        }
>      }
> 
> +    ret = 0;
> +
>   cleanup:
>      virDomainObjEndAPI(&privdom);
>      return ret;
> -- 
> 2.4.1
> 
> --
> libvir-list mailing list
> libvir-list at redhat.com
> https://www.redhat.com/mailman/listinfo/libvir-list




More information about the libvir-list mailing list