
On Mon, May 13, 2019 at 2:38 PM Michal Privoznik <mprivozn@redhat.com> wrote:
On 5/13/19 1:26 AM, Ilias Stamatis wrote:
Return the number of disks present in the configuration of the test domain when called with @errors as NULL and @maxerrors as 0.
Otherwise report an error for every second disk, assigning available error codes in a cyclic order.
Signed-off-by: Ilias Stamatis <stamatis.iliass@gmail.com> --- src/test/test_driver.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+)
diff --git a/src/test/test_driver.c b/src/test/test_driver.c index a06d1fc402..527c2f5d3b 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -3046,6 +3046,47 @@ static int testDomainSetAutostart(virDomainPtr domain, return 0; }
+static int testDomainGetDiskErrors(virDomainPtr dom, + virDomainDiskErrorPtr errors, + unsigned int maxerrors, + unsigned int flags) +{ + virDomainObjPtr vm = NULL; + int ret = -1; + size_t i; + int n = 0; + int codes[] = {VIR_DOMAIN_DISK_ERROR_UNSPEC, VIR_DOMAIN_DISK_ERROR_NO_SPACE}; + size_t ncodes = sizeof(codes) / sizeof(codes[0]);
We have ARRAY_CARDINALITY() for this. Also, there's no need to create @codes array as we'll see later.
Aah, okay, I wasn't aware of this macro!
+ + virCheckFlags(0, -1); + + if (!(vm = testDomObjFromDomain(dom))) + goto cleanup; + + if (virDomainObjCheckActive(vm) < 0) + goto cleanup; + + if (!errors) { + ret = vm->def->ndisks;
Tiny nitpick, we tend to put the longer body first, which in this case is the else branch.
Hmm, I would normally do it like that but I noticed this in the "Curly braces" section of the Contributor guidelines [1]: """Also, if the else block is much shorter than the if block, consider negating the if-condition and swapping the bodies, putting the short block first and making the longer, multi-line block be the else block.""" So what is the case?
+ } else { + for (i = 1; i < vm->def->ndisks && n < maxerrors; i += 2) { + if (VIR_STRDUP(errors[n].disk, vm->def->disks[i]->dst) < 0) + goto cleanup; + errors[n].error = codes[n % ncodes];
This can be simplified. This enum in question is guaranteed to have continuous items and _LAST to be the last item. Hence, this can be rewritten as:
errors[i].error = (i % (VIR_DOMAIN_DISK_ERROR_LAST - 1)) + 1;
I am aware of this but I wasn't sure if it would be very clean if I started adding -1s and +1s in places.
But if we want to simplify it a little bit more (so that VIR_DOMAIN_DISK_ERROR_NONE is possible too) we can do it like this:
errors[i].error = i % VIR_DOMAIN_DISK_ERROR_LAST;
Hmm, in the documentation for this function [2] it is stated that: """Disks with no error will not be returned in the @errors array.""" So it seems to me that VIR_DOMAIN_DISK_ERROR_NONE shouldn't be included. What do you think?
This immediatelly drops @codes, @ncodes and @n variables. Also, there's MIN() macro ;-)
Cool, but where exactly do you suggest using MIN() here? Or you're just mentioning it?
Yeah, libvirt has a lot of marcos and helpers and it's not obvious at the first glance, but I guess that's like so with every bigger project, isn't it?
Yes, and I find these wrappers very useful / handy ;)
+ n++; + } + ret = n; + } + + cleanup: + virDomainObjEndAPI(&vm); + if (ret < 0) { + for (i = 0; i < n; i++) + VIR_FREE(errors[i].disk); + } + return ret; +} + static char *testDomainGetSchedulerType(virDomainPtr domain ATTRIBUTE_UNUSED, int *nparams) { @@ -6832,6 +6873,7 @@ static virHypervisorDriver testHypervisorDriver = { .domainUndefineFlags = testDomainUndefineFlags, /* 0.9.4 */ .domainGetAutostart = testDomainGetAutostart, /* 0.3.2 */ .domainSetAutostart = testDomainSetAutostart, /* 0.3.2 */ + .domainGetDiskErrors = testDomainGetDiskErrors, /* 5.4.0 */ .domainGetSchedulerType = testDomainGetSchedulerType, /* 0.3.2 */ .domainGetSchedulerParameters = testDomainGetSchedulerParameters, /* 0.3.2 */ .domainGetSchedulerParametersFlags = testDomainGetSchedulerParametersFlags, /* 0.9.2 */
I'm cleaning the patch as outlined, ACKing and pushing.
Sure, or I could fix it if you wanted. Thank you very much! Ilias [1] https://libvirt.org/hacking.html#curly_braces [2] https://libvirt.org/html/libvirt-libvirt-domain.html#virDomainGetDiskErrors
Michal