From a9932fc292198197ea1d9289bfb95a78f895e6c6 Mon Sep 17 00:00:00 2001 From: Pavel Hrdina Date: Tue, 22 Sep 2020 12:52:26 +0200 Subject: [PATCH] tests: use g_autoptr for virCgroup Signed-off-by: Pavel Hrdina Reviewed-by: Jonathon Jongsma --- tests/vircgrouptest.c | 228 +++++++++++++++--------------------------- 1 file changed, 80 insertions(+), 148 deletions(-) diff --git a/tests/vircgrouptest.c b/tests/vircgrouptest.c index 632d33efcd..6c1a766fa2 100644 --- a/tests/vircgrouptest.c +++ b/tests/vircgrouptest.c @@ -183,7 +183,7 @@ testCgroupDetectMounts(const void *args) const struct _detectMountsData *data = args; char *parsed = NULL; const char *actual; - virCgroupPtr group = NULL; + g_autoptr(virCgroup) group = NULL; g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; size_t i; @@ -217,15 +217,13 @@ testCgroupDetectMounts(const void *args) cleanup: g_unsetenv("VIR_CGROUP_MOCK_FILENAME"); VIR_FREE(parsed); - virCgroupFree(group); return result; } static int testCgroupNewForSelf(const void *args G_GNUC_UNUSED) { - virCgroupPtr cgroup = NULL; - int ret = -1; + g_autoptr(virCgroup) cgroup = NULL; const char *placement[VIR_CGROUP_CONTROLLER_LAST] = { [VIR_CGROUP_CONTROLLER_CPU] = "/system", [VIR_CGROUP_CONTROLLER_CPUACCT] = "/system", @@ -239,14 +237,10 @@ static int testCgroupNewForSelf(const void *args G_GNUC_UNUSED) if (virCgroupNewSelf(&cgroup) < 0) { fprintf(stderr, "Cannot create cgroup for self\n"); - goto cleanup; + return -1; } - ret = validateCgroup(cgroup, "", mountsFull, links, placement, NULL, NULL, 0); - - cleanup: - virCgroupFree(cgroup); - return ret; + return validateCgroup(cgroup, "", mountsFull, links, placement, NULL, NULL, 0); } @@ -256,7 +250,7 @@ static int testCgroupNewForSelf(const void *args G_GNUC_UNUSED) virErrorPtr err = virGetLastError(); \ fprintf(stderr, "Did not get " #en " error code: %d:%d\n", \ err ? err->code : 0, err ? err->int1 : 0); \ - goto cleanup; \ + return -1; \ } } while (0) /* Asking for impossible combination since CPU is co-mounted */ @@ -264,8 +258,7 @@ static int testCgroupNewForSelf(const void *args G_GNUC_UNUSED) static int testCgroupNewForPartition(const void *args G_GNUC_UNUSED) { - virCgroupPtr cgroup = NULL; - int ret = -1; + g_autoptr(virCgroup) cgroup = NULL; int rv; const char *placementSmall[VIR_CGROUP_CONTROLLER_LAST] = { [VIR_CGROUP_CONTROLLER_CPU] = "/virtualmachines.partition", @@ -290,7 +283,7 @@ static int testCgroupNewForPartition(const void *args G_GNUC_UNUSED) if ((rv = virCgroupNewPartition("/virtualmachines", false, -1, &cgroup)) != -1) { fprintf(stderr, "Unexpected found /virtualmachines cgroup: %d\n", -rv); - goto cleanup; + return -1; } ENSURE_ERRNO(ENOENT); @@ -299,7 +292,7 @@ static int testCgroupNewForPartition(const void *args G_GNUC_UNUSED) (1 << VIR_CGROUP_CONTROLLER_CPU), &cgroup)) != -1) { fprintf(stderr, "Should not have created /virtualmachines cgroup: %d\n", -rv); - goto cleanup; + return -1; } ENSURE_ERRNO(EINVAL); @@ -308,7 +301,7 @@ static int testCgroupNewForPartition(const void *args G_GNUC_UNUSED) (1 << VIR_CGROUP_CONTROLLER_DEVICES), &cgroup)) != -1) { fprintf(stderr, "Should not have created /virtualmachines cgroup: %d\n", -rv); - goto cleanup; + return -1; } ENSURE_ERRNO(ENXIO); @@ -319,27 +312,22 @@ static int testCgroupNewForPartition(const void *args G_GNUC_UNUSED) (1 << VIR_CGROUP_CONTROLLER_MEMORY), &cgroup)) != 0) { fprintf(stderr, "Cannot create /virtualmachines cgroup: %d\n", -rv); - goto cleanup; + return -1; } - ret = validateCgroup(cgroup, "/virtualmachines.partition", mountsSmall, links, placementSmall, NULL, NULL, 0); + rv = validateCgroup(cgroup, "/virtualmachines.partition", mountsSmall, links, placementSmall, NULL, NULL, 0); virCgroupFree(cgroup); if ((rv = virCgroupNewPartition("/virtualmachines", true, -1, &cgroup)) != 0) { fprintf(stderr, "Cannot create /virtualmachines cgroup: %d\n", -rv); - goto cleanup; + return -1; } - ret = validateCgroup(cgroup, "/virtualmachines.partition", mountsFull, links, placementFull, NULL, NULL, 0); - - cleanup: - virCgroupFree(cgroup); - return ret; + return validateCgroup(cgroup, "/virtualmachines.partition", mountsFull, links, placementFull, NULL, NULL, 0); } static int testCgroupNewForPartitionNested(const void *args G_GNUC_UNUSED) { - virCgroupPtr cgroup = NULL; - int ret = -1; + g_autoptr(virCgroup) cgroup = NULL; int rv; const char *placementFull[VIR_CGROUP_CONTROLLER_LAST] = { [VIR_CGROUP_CONTROLLER_CPU] = "/deployment.partition/production.partition", @@ -354,42 +342,37 @@ static int testCgroupNewForPartitionNested(const void *args G_GNUC_UNUSED) if ((rv = virCgroupNewPartition("/deployment/production", false, -1, &cgroup)) != -1) { fprintf(stderr, "Unexpected found /deployment/production cgroup: %d\n", -rv); - goto cleanup; + return -1; } ENSURE_ERRNO(ENOENT); /* Should not work, since we require /deployment to be pre-created */ if ((rv = virCgroupNewPartition("/deployment/production", true, -1, &cgroup)) != -1) { fprintf(stderr, "Unexpected created /deployment/production cgroup: %d\n", -rv); - goto cleanup; + return -1; } ENSURE_ERRNO(ENOENT); if ((rv = virCgroupNewPartition("/deployment", true, -1, &cgroup)) != 0) { fprintf(stderr, "Failed to create /deployment cgroup: %d\n", -rv); - goto cleanup; + return -1; } /* Should now work */ virCgroupFree(cgroup); if ((rv = virCgroupNewPartition("/deployment/production", true, -1, &cgroup)) != 0) { fprintf(stderr, "Failed to create /deployment/production cgroup: %d\n", -rv); - goto cleanup; + return -1; } - ret = validateCgroup(cgroup, "/deployment.partition/production.partition", - mountsFull, links, placementFull, NULL, NULL, 0); - - cleanup: - virCgroupFree(cgroup); - return ret; + return validateCgroup(cgroup, "/deployment.partition/production.partition", + mountsFull, links, placementFull, NULL, NULL, 0); } static int testCgroupNewForPartitionNestedDeep(const void *args G_GNUC_UNUSED) { - virCgroupPtr cgroup = NULL; - int ret = -1; + g_autoptr(virCgroup) cgroup = NULL; int rv; const char *placementFull[VIR_CGROUP_CONTROLLER_LAST] = { [VIR_CGROUP_CONTROLLER_CPU] = "/user/berrange.user/production.partition", @@ -404,50 +387,45 @@ static int testCgroupNewForPartitionNestedDeep(const void *args G_GNUC_UNUSED) if ((rv = virCgroupNewPartition("/user/berrange.user/production", false, -1, &cgroup)) != -1) { fprintf(stderr, "Unexpected found /user/berrange.user/production cgroup: %d\n", -rv); - goto cleanup; + return -1; } ENSURE_ERRNO(ENOENT); /* Should not work, since we require /user/berrange.user to be pre-created */ if ((rv = virCgroupNewPartition("/user/berrange.user/production", true, -1, &cgroup)) != -1) { fprintf(stderr, "Unexpected created /user/berrange.user/production cgroup: %d\n", -rv); - goto cleanup; + return -1; } ENSURE_ERRNO(ENOENT); if ((rv = virCgroupNewPartition("/user", true, -1, &cgroup)) != 0) { fprintf(stderr, "Failed to create /user/berrange.user cgroup: %d\n", -rv); - goto cleanup; + return -1; } virCgroupFree(cgroup); if ((rv = virCgroupNewPartition("/user/berrange.user", true, -1, &cgroup)) != 0) { fprintf(stderr, "Failed to create /user/berrange.user cgroup: %d\n", -rv); - goto cleanup; + return -1; } /* Should now work */ virCgroupFree(cgroup); if ((rv = virCgroupNewPartition("/user/berrange.user/production", true, -1, &cgroup)) != 0) { fprintf(stderr, "Failed to create /user/berrange.user/production cgroup: %d\n", -rv); - goto cleanup; + return -1; } - ret = validateCgroup(cgroup, "/user/berrange.user/production.partition", - mountsFull, links, placementFull, NULL, NULL, 0); - - cleanup: - virCgroupFree(cgroup); - return ret; + return validateCgroup(cgroup, "/user/berrange.user/production.partition", + mountsFull, links, placementFull, NULL, NULL, 0); } static int testCgroupNewForPartitionDomain(const void *args G_GNUC_UNUSED) { - virCgroupPtr partitioncgroup = NULL; - virCgroupPtr domaincgroup = NULL; - int ret = -1; + g_autoptr(virCgroup) partitioncgroup = NULL; + g_autoptr(virCgroup) domaincgroup = NULL; int rv; const char *placement[VIR_CGROUP_CONTROLLER_LAST] = { [VIR_CGROUP_CONTROLLER_CPU] = "/production.partition/foo.libvirt-lxc", @@ -462,29 +440,23 @@ static int testCgroupNewForPartitionDomain(const void *args G_GNUC_UNUSED) if ((rv = virCgroupNewPartition("/production", true, -1, &partitioncgroup)) != 0) { fprintf(stderr, "Failed to create /production cgroup: %d\n", -rv); - goto cleanup; + return -1; } if ((rv = virCgroupNewDomainPartition(partitioncgroup, "lxc", "foo", true, &domaincgroup)) != 0) { fprintf(stderr, "Cannot create LXC cgroup: %d\n", -rv); - goto cleanup; + return -1; } - ret = validateCgroup(domaincgroup, "/production.partition/foo.libvirt-lxc", mountsFull, links, placement, NULL, NULL, 0); - - cleanup: - virCgroupFree(partitioncgroup); - virCgroupFree(domaincgroup); - return ret; + return validateCgroup(domaincgroup, "/production.partition/foo.libvirt-lxc", mountsFull, links, placement, NULL, NULL, 0); } static int testCgroupNewForPartitionDomainEscaped(const void *args G_GNUC_UNUSED) { - virCgroupPtr partitioncgroup1 = NULL; - virCgroupPtr partitioncgroup2 = NULL; - virCgroupPtr partitioncgroup3 = NULL; - virCgroupPtr domaincgroup = NULL; - int ret = -1; + g_autoptr(virCgroup) partitioncgroup1 = NULL; + g_autoptr(virCgroup) partitioncgroup2 = NULL; + g_autoptr(virCgroup) partitioncgroup3 = NULL; + g_autoptr(virCgroup) domaincgroup = NULL; int rv; const char *placement[VIR_CGROUP_CONTROLLER_LAST] = { [VIR_CGROUP_CONTROLLER_CPU] = "/_cgroup.evil/net_cls.evil/__evil.evil/_cpu.foo.libvirt-lxc", @@ -499,42 +471,34 @@ static int testCgroupNewForPartitionDomainEscaped(const void *args G_GNUC_UNUSED if ((rv = virCgroupNewPartition("/cgroup.evil", true, -1, &partitioncgroup1)) != 0) { fprintf(stderr, "Failed to create /cgroup.evil cgroup: %d\n", -rv); - goto cleanup; + return -1; } if ((rv = virCgroupNewPartition("/cgroup.evil/net_cls.evil", true, -1, &partitioncgroup2)) != 0) { fprintf(stderr, "Failed to create /cgroup.evil/cpu.evil cgroup: %d\n", -rv); - goto cleanup; + return -1; } if ((rv = virCgroupNewPartition("/cgroup.evil/net_cls.evil/_evil.evil", true, -1, &partitioncgroup3)) != 0) { fprintf(stderr, "Failed to create /cgroup.evil cgroup: %d\n", -rv); - goto cleanup; + return -1; } if ((rv = virCgroupNewDomainPartition(partitioncgroup3, "lxc", "cpu.foo", true, &domaincgroup)) != 0) { fprintf(stderr, "Cannot create LXC cgroup: %d\n", -rv); - goto cleanup; + return -1; } /* NB we're not expecting 'net_cls.evil' to be escaped, * since our fake /proc/cgroups pretends this controller * isn't compiled into the kernel */ - ret = validateCgroup(domaincgroup, "/_cgroup.evil/net_cls.evil/__evil.evil/_cpu.foo.libvirt-lxc", mountsFull, links, placement, NULL, NULL, 0); - - cleanup: - virCgroupFree(partitioncgroup3); - virCgroupFree(partitioncgroup2); - virCgroupFree(partitioncgroup1); - virCgroupFree(domaincgroup); - return ret; + return validateCgroup(domaincgroup, "/_cgroup.evil/net_cls.evil/__evil.evil/_cpu.foo.libvirt-lxc", mountsFull, links, placement, NULL, NULL, 0); } static int testCgroupNewForSelfAllInOne(const void *args G_GNUC_UNUSED) { - virCgroupPtr cgroup = NULL; - int ret = -1; + g_autoptr(virCgroup) cgroup = NULL; const char *placement[VIR_CGROUP_CONTROLLER_LAST] = { [VIR_CGROUP_CONTROLLER_CPU] = "/", [VIR_CGROUP_CONTROLLER_CPUACCT] = "/", @@ -547,24 +511,19 @@ static int testCgroupNewForSelfAllInOne(const void *args G_GNUC_UNUSED) if (virCgroupNewSelf(&cgroup) < 0) { fprintf(stderr, "Cannot create cgroup for self\n"); - goto cleanup; + return -1; } - ret = validateCgroup(cgroup, "", mountsAllInOne, linksAllInOne, placement, NULL, NULL, 0); - - cleanup: - virCgroupFree(cgroup); - return ret; + return validateCgroup(cgroup, "", mountsAllInOne, linksAllInOne, placement, NULL, NULL, 0); } static int testCgroupNewForSelfLogind(const void *args G_GNUC_UNUSED) { - virCgroupPtr cgroup = NULL; + g_autoptr(virCgroup) cgroup = NULL; if (virCgroupNewSelf(&cgroup) >= 0) { fprintf(stderr, "Expected to fail, only systemd cgroup available.\n"); - virCgroupFree(cgroup); return -1; } @@ -574,8 +533,7 @@ static int testCgroupNewForSelfLogind(const void *args G_GNUC_UNUSED) static int testCgroupNewForSelfUnified(const void *args G_GNUC_UNUSED) { - virCgroupPtr cgroup = NULL; - int ret = -1; + g_autoptr(virCgroup) cgroup = NULL; const char *empty[VIR_CGROUP_CONTROLLER_LAST] = { 0 }; unsigned int controllers = (1 << VIR_CGROUP_CONTROLLER_CPU) | @@ -586,21 +544,17 @@ static int testCgroupNewForSelfUnified(const void *args G_GNUC_UNUSED) if (virCgroupNewSelf(&cgroup) < 0) { fprintf(stderr, "Cannot create cgroup for self\n"); - goto cleanup; + return -1; } - ret = validateCgroup(cgroup, "", empty, empty, empty, - "/not/really/sys/fs/cgroup", "/", controllers); - cleanup: - virCgroupFree(cgroup); - return ret; + return validateCgroup(cgroup, "", empty, empty, empty, + "/not/really/sys/fs/cgroup", "/", controllers); } static int testCgroupNewForSelfHybrid(const void *args G_GNUC_UNUSED) { - virCgroupPtr cgroup = NULL; - int ret = -1; + g_autoptr(virCgroup) cgroup = NULL; const char *empty[VIR_CGROUP_CONTROLLER_LAST] = { 0 }; const char *mounts[VIR_CGROUP_CONTROLLER_LAST] = { [VIR_CGROUP_CONTROLLER_CPUSET] = "/not/really/sys/fs/cgroup/cpuset", @@ -623,15 +577,11 @@ static int testCgroupNewForSelfHybrid(const void *args G_GNUC_UNUSED) if (virCgroupNewSelf(&cgroup) < 0) { fprintf(stderr, "Cannot create cgroup for self\n"); - goto cleanup; + return -1; } - ret = validateCgroup(cgroup, "", mounts, empty, placement, - "/not/really/sys/fs/cgroup/unified", "/", controllers); - - cleanup: - virCgroupFree(cgroup); - return ret; + return validateCgroup(cgroup, "", mounts, empty, placement, + "/not/really/sys/fs/cgroup/unified", "/", controllers); } @@ -679,7 +629,7 @@ static int testCgroupControllerAvailable(const void *args G_GNUC_UNUSED) static int testCgroupGetPercpuStats(const void *args G_GNUC_UNUSED) { - virCgroupPtr cgroup = NULL; + g_autoptr(virCgroup) cgroup = NULL; size_t i; int rv, ret = -1; virTypedParameterPtr params = NULL; @@ -756,50 +706,44 @@ static int testCgroupGetPercpuStats(const void *args G_GNUC_UNUSED) ret = 0; cleanup: - virCgroupFree(cgroup); VIR_FREE(params); return ret; } static int testCgroupGetMemoryUsage(const void *args G_GNUC_UNUSED) { - virCgroupPtr cgroup = NULL; - int rv, ret = -1; + g_autoptr(virCgroup) cgroup = NULL; + int rv; unsigned long kb; if ((rv = virCgroupNewPartition("/virtualmachines", true, (1 << VIR_CGROUP_CONTROLLER_MEMORY), &cgroup)) < 0) { fprintf(stderr, "Could not create /virtualmachines cgroup: %d\n", -rv); - goto cleanup; + return -1; } if ((rv = virCgroupGetMemoryUsage(cgroup, &kb)) < 0) { fprintf(stderr, "Could not retrieve GetMemoryUsage for /virtualmachines cgroup: %d\n", -rv); - goto cleanup; + return -1; } if (kb != 1421212UL) { fprintf(stderr, "Wrong value from virCgroupGetMemoryUsage (expected %ld)\n", 1421212UL); - goto cleanup; + return -1; } - ret = 0; - - cleanup: - virCgroupFree(cgroup); - return ret; + return 0; } static int testCgroupGetMemoryStat(const void *args G_GNUC_UNUSED) { - virCgroupPtr cgroup = NULL; + g_autoptr(virCgroup) cgroup = NULL; int rv; - int ret = -1; size_t i; const unsigned long long expected_values[] = { @@ -824,7 +768,7 @@ testCgroupGetMemoryStat(const void *args G_GNUC_UNUSED) (1 << VIR_CGROUP_CONTROLLER_MEMORY), &cgroup)) < 0) { fprintf(stderr, "Could not create /virtualmachines cgroup: %d\n", -rv); - goto cleanup; + return -1; } if ((rv = virCgroupGetMemoryStat(cgroup, &values[0], @@ -832,7 +776,7 @@ testCgroupGetMemoryStat(const void *args G_GNUC_UNUSED) &values[3], &values[4], &values[5])) < 0) { fprintf(stderr, "Could not retrieve GetMemoryStat for /virtualmachines cgroup: %d\n", -rv); - goto cleanup; + return -1; } for (i = 0; i < G_N_ELEMENTS(expected_values); i++) { @@ -842,23 +786,19 @@ testCgroupGetMemoryStat(const void *args G_GNUC_UNUSED) "Wrong value (%llu) for %s from virCgroupGetMemoryStat " "(expected %llu)\n", values[i], names[i], (expected_values[i] >> 10)); - goto cleanup; + return -1; } } - ret = 0; - - cleanup: - virCgroupFree(cgroup); - return ret; + return 0; } static int testCgroupGetBlkioIoServiced(const void *args G_GNUC_UNUSED) { - virCgroupPtr cgroup = NULL; + g_autoptr(virCgroup) cgroup = NULL; size_t i; - int rv, ret = -1; + int rv; const long long expected_values[] = { 119084214273ULL, @@ -878,14 +818,14 @@ static int testCgroupGetBlkioIoServiced(const void *args G_GNUC_UNUSED) (1 << VIR_CGROUP_CONTROLLER_BLKIO), &cgroup)) < 0) { fprintf(stderr, "Could not create /virtualmachines cgroup: %d\n", -rv); - goto cleanup; + return -1; } if ((rv = virCgroupGetBlkioIoServiced(cgroup, values, &values[1], &values[2], &values[3])) < 0) { fprintf(stderr, "Could not retrieve BlkioIoServiced for /virtualmachines cgroup: %d\n", -rv); - goto cleanup; + return -1; } for (i = 0; i < G_N_ELEMENTS(expected_values); i++) { @@ -893,22 +833,18 @@ static int testCgroupGetBlkioIoServiced(const void *args G_GNUC_UNUSED) fprintf(stderr, "Wrong value for %s from virCgroupBlkioIoServiced (expected %lld)\n", names[i], expected_values[i]); - goto cleanup; + return -1; } } - ret = 0; - - cleanup: - virCgroupFree(cgroup); - return ret; + return 0; } static int testCgroupGetBlkioIoDeviceServiced(const void *args G_GNUC_UNUSED) { - virCgroupPtr cgroup = NULL; + g_autoptr(virCgroup) cgroup = NULL; size_t i; - int rv, ret = -1; + int rv; const long long expected_values0[] = { 59542107136ULL, 411440480256ULL, @@ -933,7 +869,7 @@ static int testCgroupGetBlkioIoDeviceServiced(const void *args G_GNUC_UNUSED) (1 << VIR_CGROUP_CONTROLLER_BLKIO), &cgroup)) < 0) { fprintf(stderr, "Could not create /virtualmachines cgroup: %d\n", -rv); - goto cleanup; + return -1; } if ((rv = virCgroupGetBlkioIoDeviceServiced(cgroup, @@ -941,7 +877,7 @@ static int testCgroupGetBlkioIoDeviceServiced(const void *args G_GNUC_UNUSED) values, &values[1], &values[2], &values[3])) < 0) { fprintf(stderr, "Could not retrieve BlkioIoDeviceServiced for /virtualmachines cgroup: %d\n", -rv); - goto cleanup; + return -1; } for (i = 0; i < G_N_ELEMENTS(expected_values0); i++) { @@ -949,7 +885,7 @@ static int testCgroupGetBlkioIoDeviceServiced(const void *args G_GNUC_UNUSED) fprintf(stderr, "Wrong value for %s from virCgroupGetBlkioIoDeviceServiced (expected %lld)\n", names[i], expected_values0[i]); - goto cleanup; + return -1; } } @@ -958,7 +894,7 @@ static int testCgroupGetBlkioIoDeviceServiced(const void *args G_GNUC_UNUSED) values, &values[1], &values[2], &values[3])) < 0) { fprintf(stderr, "Could not retrieve BlkioIoDeviceServiced for /virtualmachines cgroup: %d\n", -rv); - goto cleanup; + return -1; } for (i = 0; i < G_N_ELEMENTS(expected_values1); i++) { @@ -966,15 +902,11 @@ static int testCgroupGetBlkioIoDeviceServiced(const void *args G_GNUC_UNUSED) fprintf(stderr, "Wrong value for %s from virCgroupGetBlkioIoDeviceServiced (expected %lld)\n", names[i], expected_values1[i]); - goto cleanup; + return -1; } } - ret = 0; - - cleanup: - virCgroupFree(cgroup); - return ret; + return 0; } # define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX"