vircgrouptest.c 39.1 KB
Newer Older
1
/*
2
 * Copyright (C) 2013-2015 Red Hat, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include "testutils.h"

#ifdef __linux__


26
# define LIBVIRT_VIRCGROUPPRIV_H_ALLOW
27
# include "vircgrouppriv.h"
28
# include "virstring.h"
29 30 31
# include "virerror.h"
# include "virlog.h"
# include "virfile.h"
32
# include "virbuffer.h"
33
# include "testutilslxc.h"
34
# include "virhostcpu.h"
35 36 37

# define VIR_FROM_THIS VIR_FROM_NONE

38 39
VIR_LOG_INIT("tests.cgrouptest");

40 41 42
static int validateCgroup(virCgroupPtr cgroup,
                          const char *expectPath,
                          const char **expectMountPoint,
43
                          const char **expectLinkPoint,
44 45 46 47
                          const char **expectPlacement,
                          const char *expectUnifiedMountPoint,
                          const char *expectUnifiedPlacement,
                          unsigned int expectUnifiedControllers)
48
{
49
    size_t i;
50 51 52 53 54 55 56

    if (STRNEQ(cgroup->path, expectPath)) {
        fprintf(stderr, "Wrong path '%s', expected '%s'\n",
                cgroup->path, expectPath);
        return -1;
    }

57
    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
58
        if (STRNEQ_NULLABLE(expectMountPoint[i],
59
                            cgroup->legacy[i].mountPoint)) {
60
            fprintf(stderr, "Wrong mount '%s', expected '%s' for '%s'\n",
61
                    cgroup->legacy[i].mountPoint,
62 63 64 65
                    expectMountPoint[i],
                    virCgroupControllerTypeToString(i));
            return -1;
        }
66
        if (STRNEQ_NULLABLE(expectLinkPoint[i],
67
                            cgroup->legacy[i].linkPoint)) {
68
            fprintf(stderr, "Wrong link '%s', expected '%s' for '%s'\n",
69
                    cgroup->legacy[i].linkPoint,
70 71 72 73
                    expectLinkPoint[i],
                    virCgroupControllerTypeToString(i));
            return -1;
        }
74
        if (STRNEQ_NULLABLE(expectPlacement[i],
75
                            cgroup->legacy[i].placement)) {
76
            fprintf(stderr, "Wrong placement '%s', expected '%s' for '%s'\n",
77
                    cgroup->legacy[i].placement,
78 79 80 81 82 83
                    expectPlacement[i],
                    virCgroupControllerTypeToString(i));
            return -1;
        }
    }

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    if (STRNEQ_NULLABLE(expectUnifiedMountPoint,
                        cgroup->unified.mountPoint)) {
        fprintf(stderr, "Wrong mount '%s', expected '%s' for 'unified'\n",
                cgroup->unified.mountPoint,
                expectUnifiedMountPoint);
        return -1;
    }
    if (STRNEQ_NULLABLE(expectUnifiedPlacement,
                        cgroup->unified.placement)) {
        fprintf(stderr, "Wrong placement '%s', expected '%s' for 'unified'\n",
                cgroup->unified.placement,
                expectUnifiedPlacement);
        return -1;
    }
    if (expectUnifiedControllers != cgroup->unified.controllers) {
        for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
            int type = 1 << i;
            if ((expectUnifiedControllers & type) != (cgroup->unified.controllers & type)) {
                const char *typeStr = virCgroupControllerTypeToString(i);
                if (expectUnifiedControllers & type) {
                    fprintf(stderr, "expected controller '%s' for 'unified', "
                            "but it's missing\n", typeStr);
                } else {
                    fprintf(stderr, "existing controller '%s' for 'unified', "
                            "but it's not expected\n", typeStr);
                }
            }

        }
        return -1;
    }

116 117 118 119 120 121 122 123 124 125 126
    return 0;
}

const char *mountsSmall[VIR_CGROUP_CONTROLLER_LAST] = {
    [VIR_CGROUP_CONTROLLER_CPU] = "/not/really/sys/fs/cgroup/cpu,cpuacct",
    [VIR_CGROUP_CONTROLLER_CPUACCT] = "/not/really/sys/fs/cgroup/cpu,cpuacct",
    [VIR_CGROUP_CONTROLLER_CPUSET] = NULL,
    [VIR_CGROUP_CONTROLLER_MEMORY] = "/not/really/sys/fs/cgroup/memory",
    [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
    [VIR_CGROUP_CONTROLLER_FREEZER] = NULL,
    [VIR_CGROUP_CONTROLLER_BLKIO] = NULL,
127
    [VIR_CGROUP_CONTROLLER_SYSTEMD] = NULL,
128 129 130 131 132 133 134 135 136
};
const char *mountsFull[VIR_CGROUP_CONTROLLER_LAST] = {
    [VIR_CGROUP_CONTROLLER_CPU] = "/not/really/sys/fs/cgroup/cpu,cpuacct",
    [VIR_CGROUP_CONTROLLER_CPUACCT] = "/not/really/sys/fs/cgroup/cpu,cpuacct",
    [VIR_CGROUP_CONTROLLER_CPUSET] = "/not/really/sys/fs/cgroup/cpuset",
    [VIR_CGROUP_CONTROLLER_MEMORY] = "/not/really/sys/fs/cgroup/memory",
    [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
    [VIR_CGROUP_CONTROLLER_FREEZER] = "/not/really/sys/fs/cgroup/freezer",
    [VIR_CGROUP_CONTROLLER_BLKIO] = "/not/really/sys/fs/cgroup/blkio",
137
    [VIR_CGROUP_CONTROLLER_SYSTEMD] = "/not/really/sys/fs/cgroup/systemd",
138
};
139 140 141 142 143 144 145 146 147 148
const char *mountsAllInOne[VIR_CGROUP_CONTROLLER_LAST] = {
    [VIR_CGROUP_CONTROLLER_CPU] = "/not/really/sys/fs/cgroup",
    [VIR_CGROUP_CONTROLLER_CPUACCT] = "/not/really/sys/fs/cgroup",
    [VIR_CGROUP_CONTROLLER_CPUSET] = "/not/really/sys/fs/cgroup",
    [VIR_CGROUP_CONTROLLER_MEMORY] = "/not/really/sys/fs/cgroup",
    [VIR_CGROUP_CONTROLLER_DEVICES] = "/not/really/sys/fs/cgroup",
    [VIR_CGROUP_CONTROLLER_FREEZER] = NULL,
    [VIR_CGROUP_CONTROLLER_BLKIO] = "/not/really/sys/fs/cgroup",
    [VIR_CGROUP_CONTROLLER_SYSTEMD] = NULL,
};
149

150 151 152 153 154 155 156 157
const char *links[VIR_CGROUP_CONTROLLER_LAST] = {
    [VIR_CGROUP_CONTROLLER_CPU] = "/not/really/sys/fs/cgroup/cpu",
    [VIR_CGROUP_CONTROLLER_CPUACCT] = "/not/really/sys/fs/cgroup/cpuacct",
    [VIR_CGROUP_CONTROLLER_CPUSET] = NULL,
    [VIR_CGROUP_CONTROLLER_MEMORY] = NULL,
    [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
    [VIR_CGROUP_CONTROLLER_FREEZER] = NULL,
    [VIR_CGROUP_CONTROLLER_BLKIO] = NULL,
158 159 160 161 162 163 164 165 166 167 168 169
    [VIR_CGROUP_CONTROLLER_SYSTEMD] = NULL,
};

const char *linksAllInOne[VIR_CGROUP_CONTROLLER_LAST] = {
    [VIR_CGROUP_CONTROLLER_CPU] = NULL,
    [VIR_CGROUP_CONTROLLER_CPUACCT] = NULL,
    [VIR_CGROUP_CONTROLLER_CPUSET] = NULL,
    [VIR_CGROUP_CONTROLLER_MEMORY] = NULL,
    [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
    [VIR_CGROUP_CONTROLLER_FREEZER] = NULL,
    [VIR_CGROUP_CONTROLLER_BLKIO] = NULL,
    [VIR_CGROUP_CONTROLLER_SYSTEMD] = NULL,
170 171 172
};


173 174 175 176 177 178
struct _detectMountsData {
    const char *file;
    bool fail;
};


179 180 181 182
static int
testCgroupDetectMounts(const void *args)
{
    int result = -1;
183
    const struct _detectMountsData *data = args;
184 185 186 187 188 189
    char *parsed = NULL;
    const char *actual;
    virCgroupPtr group = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    size_t i;

190
    setenv("VIR_CGROUP_MOCK_FILENAME", data->file, 1);
191

192 193
    if (virAsprintf(&parsed, "%s/vircgroupdata/%s.parsed",
                    abs_srcdir, data->file) < 0) {
194
        goto cleanup;
195
    }
196

197 198 199
    if (virCgroupNewSelf(&group) < 0) {
        if (data->fail)
            result = 0;
200
        goto cleanup;
201
    }
202

203
    if (data->fail)
204 205 206 207 208
        goto cleanup;

    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) {
        virBufferAsprintf(&buf, "%-12s %s\n",
                          virCgroupControllerTypeToString(i),
209
                          NULLSTR(group->legacy[i].mountPoint));
210
    }
211 212
    virBufferAsprintf(&buf, "%-12s %s\n",
                      "unified", NULLSTR(group->unified.mountPoint));
213 214 215 216
    if (virBufferCheckError(&buf) < 0)
        goto cleanup;

    actual = virBufferCurrentContent(&buf);
217
    if (virTestCompareToFile(actual, parsed) < 0)
218 219 220 221 222
        goto cleanup;

    result = 0;

 cleanup:
223
    unsetenv("VIR_CGROUP_MOCK_FILENAME");
224
    VIR_FREE(parsed);
225
    virCgroupFree(&group);
226 227 228 229 230
    virBufferFreeAndReset(&buf);
    return result;
}


231 232 233 234 235 236 237
static int testCgroupNewForSelf(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr cgroup = NULL;
    int ret = -1;
    const char *placement[VIR_CGROUP_CONTROLLER_LAST] = {
        [VIR_CGROUP_CONTROLLER_CPU] = "/system",
        [VIR_CGROUP_CONTROLLER_CPUACCT] = "/system",
238 239
        [VIR_CGROUP_CONTROLLER_CPUSET] = "/",
        [VIR_CGROUP_CONTROLLER_MEMORY] = "/",
240
        [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
241 242
        [VIR_CGROUP_CONTROLLER_FREEZER] = "/",
        [VIR_CGROUP_CONTROLLER_BLKIO] = "/",
243
        [VIR_CGROUP_CONTROLLER_SYSTEMD] = "/user/berrange/123",
244 245 246 247 248 249 250
    };

    if (virCgroupNewSelf(&cgroup) < 0) {
        fprintf(stderr, "Cannot create cgroup for self\n");
        goto cleanup;
    }

251
    ret = validateCgroup(cgroup, "", mountsFull, links, placement, NULL, NULL, 0);
252

253
 cleanup:
254
    virCgroupFree(&cgroup);
255 256 257 258
    return ret;
}


259 260 261 262 263 264 265
# define ENSURE_ERRNO(en) \
    do { \
    if (!virLastErrorIsSystemErrno(en)) { \
        virErrorPtr err = virGetLastError(); \
        fprintf(stderr, "Did not get " #en " error code: %d:%d\n", \
                err ? err->code : 0, err ? err->int1 : 0); \
        goto cleanup; \
266 267 268 269 270
    } } while (0)

    /* Asking for impossible combination since CPU is co-mounted */


271 272 273 274 275 276
static int testCgroupNewForPartition(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr cgroup = NULL;
    int ret = -1;
    int rv;
    const char *placementSmall[VIR_CGROUP_CONTROLLER_LAST] = {
277 278
        [VIR_CGROUP_CONTROLLER_CPU] = "/virtualmachines.partition",
        [VIR_CGROUP_CONTROLLER_CPUACCT] = "/virtualmachines.partition",
279
        [VIR_CGROUP_CONTROLLER_CPUSET] = NULL,
280
        [VIR_CGROUP_CONTROLLER_MEMORY] = "/virtualmachines.partition",
281 282 283
        [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
        [VIR_CGROUP_CONTROLLER_FREEZER] = NULL,
        [VIR_CGROUP_CONTROLLER_BLKIO] = NULL,
284
        [VIR_CGROUP_CONTROLLER_SYSTEMD] = NULL,
285 286
    };
    const char *placementFull[VIR_CGROUP_CONTROLLER_LAST] = {
287 288 289 290
        [VIR_CGROUP_CONTROLLER_CPU] = "/virtualmachines.partition",
        [VIR_CGROUP_CONTROLLER_CPUACCT] = "/virtualmachines.partition",
        [VIR_CGROUP_CONTROLLER_CPUSET] = "/virtualmachines.partition",
        [VIR_CGROUP_CONTROLLER_MEMORY] = "/virtualmachines.partition",
291
        [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
292 293
        [VIR_CGROUP_CONTROLLER_FREEZER] = "/virtualmachines.partition",
        [VIR_CGROUP_CONTROLLER_BLKIO] = "/virtualmachines.partition",
294
        [VIR_CGROUP_CONTROLLER_SYSTEMD] = "/user/berrange/123",
295 296
    };

297
    if ((rv = virCgroupNewPartition("/virtualmachines", false, -1, &cgroup)) != -1) {
298 299 300
        fprintf(stderr, "Unexpected found /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }
301
    ENSURE_ERRNO(ENOENT);
302 303 304 305

    /* Asking for impossible combination since CPU is co-mounted */
    if ((rv = virCgroupNewPartition("/virtualmachines", true,
                                    (1 << VIR_CGROUP_CONTROLLER_CPU),
306
                                    &cgroup)) != -1) {
307 308 309
        fprintf(stderr, "Should not have created /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }
310
    ENSURE_ERRNO(EINVAL);
311 312 313 314

    /* Asking for impossible combination since devices is not mounted */
    if ((rv = virCgroupNewPartition("/virtualmachines", true,
                                    (1 << VIR_CGROUP_CONTROLLER_DEVICES),
315
                                    &cgroup)) != -1) {
316 317 318
        fprintf(stderr, "Should not have created /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }
319
    ENSURE_ERRNO(ENXIO);
320 321 322 323 324 325 326 327 328 329

    /* Asking for small combination since devices is not mounted */
    if ((rv = virCgroupNewPartition("/virtualmachines", true,
                                    (1 << VIR_CGROUP_CONTROLLER_CPU) |
                                    (1 << VIR_CGROUP_CONTROLLER_CPUACCT) |
                                    (1 << VIR_CGROUP_CONTROLLER_MEMORY),
                                    &cgroup)) != 0) {
        fprintf(stderr, "Cannot create /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }
330
    ret = validateCgroup(cgroup, "/virtualmachines.partition", mountsSmall, links, placementSmall, NULL, NULL, 0);
331
    virCgroupFree(&cgroup);
332 333 334 335 336

    if ((rv = virCgroupNewPartition("/virtualmachines", true, -1, &cgroup)) != 0) {
        fprintf(stderr, "Cannot create /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }
337
    ret = validateCgroup(cgroup, "/virtualmachines.partition", mountsFull, links, placementFull, NULL, NULL, 0);
338

339
 cleanup:
340
    virCgroupFree(&cgroup);
341 342 343
    return ret;
}

344

345 346 347 348 349 350
static int testCgroupNewForPartitionNested(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr cgroup = NULL;
    int ret = -1;
    int rv;
    const char *placementFull[VIR_CGROUP_CONTROLLER_LAST] = {
351 352 353 354
        [VIR_CGROUP_CONTROLLER_CPU] = "/deployment.partition/production.partition",
        [VIR_CGROUP_CONTROLLER_CPUACCT] = "/deployment.partition/production.partition",
        [VIR_CGROUP_CONTROLLER_CPUSET] = "/deployment.partition/production.partition",
        [VIR_CGROUP_CONTROLLER_MEMORY] = "/deployment.partition/production.partition",
355
        [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
356 357
        [VIR_CGROUP_CONTROLLER_FREEZER] = "/deployment.partition/production.partition",
        [VIR_CGROUP_CONTROLLER_BLKIO] = "/deployment.partition/production.partition",
358
        [VIR_CGROUP_CONTROLLER_SYSTEMD] = "/user/berrange/123",
359
    };
360

361
    if ((rv = virCgroupNewPartition("/deployment/production", false, -1, &cgroup)) != -1) {
362
        fprintf(stderr, "Unexpected found /deployment/production cgroup: %d\n", -rv);
363 364
        goto cleanup;
    }
365
    ENSURE_ERRNO(ENOENT);
366

367
    /* Should not work, since we require /deployment to be pre-created */
368
    if ((rv = virCgroupNewPartition("/deployment/production", true, -1, &cgroup)) != -1) {
369
        fprintf(stderr, "Unexpected created /deployment/production cgroup: %d\n", -rv);
370 371
        goto cleanup;
    }
372
    ENSURE_ERRNO(ENOENT);
373

374 375
    if ((rv = virCgroupNewPartition("/deployment", true, -1, &cgroup)) != 0) {
        fprintf(stderr, "Failed to create /deployment cgroup: %d\n", -rv);
376 377 378 379
        goto cleanup;
    }

    /* Should now work */
380
    virCgroupFree(&cgroup);
381 382
    if ((rv = virCgroupNewPartition("/deployment/production", true, -1, &cgroup)) != 0) {
        fprintf(stderr, "Failed to create /deployment/production cgroup: %d\n", -rv);
383 384 385
        goto cleanup;
    }

386
    ret = validateCgroup(cgroup, "/deployment.partition/production.partition",
387
                         mountsFull, links, placementFull, NULL, NULL, 0);
388

389
 cleanup:
390
    virCgroupFree(&cgroup);
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
    return ret;
}


static int testCgroupNewForPartitionNestedDeep(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr cgroup = NULL;
    int ret = -1;
    int rv;
    const char *placementFull[VIR_CGROUP_CONTROLLER_LAST] = {
        [VIR_CGROUP_CONTROLLER_CPU] = "/user/berrange.user/production.partition",
        [VIR_CGROUP_CONTROLLER_CPUACCT] = "/user/berrange.user/production.partition",
        [VIR_CGROUP_CONTROLLER_CPUSET] = "/user/berrange.user/production.partition",
        [VIR_CGROUP_CONTROLLER_MEMORY] = "/user/berrange.user/production.partition",
        [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
        [VIR_CGROUP_CONTROLLER_FREEZER] = "/user/berrange.user/production.partition",
        [VIR_CGROUP_CONTROLLER_BLKIO] = "/user/berrange.user/production.partition",
408
        [VIR_CGROUP_CONTROLLER_SYSTEMD] = "/user/berrange/123",
409 410
    };

411
    if ((rv = virCgroupNewPartition("/user/berrange.user/production", false, -1, &cgroup)) != -1) {
412 413 414
        fprintf(stderr, "Unexpected found /user/berrange.user/production cgroup: %d\n", -rv);
        goto cleanup;
    }
415
    ENSURE_ERRNO(ENOENT);
416 417

    /* Should not work, since we require /user/berrange.user to be pre-created */
418
    if ((rv = virCgroupNewPartition("/user/berrange.user/production", true, -1, &cgroup)) != -1) {
419 420 421
        fprintf(stderr, "Unexpected created /user/berrange.user/production cgroup: %d\n", -rv);
        goto cleanup;
    }
422
    ENSURE_ERRNO(ENOENT);
423 424 425 426 427 428

    if ((rv = virCgroupNewPartition("/user", true, -1, &cgroup)) != 0) {
        fprintf(stderr, "Failed to create /user/berrange.user cgroup: %d\n", -rv);
        goto cleanup;
    }

429
    virCgroupFree(&cgroup);
430 431 432 433 434 435
    if ((rv = virCgroupNewPartition("/user/berrange.user", true, -1, &cgroup)) != 0) {
        fprintf(stderr, "Failed to create /user/berrange.user cgroup: %d\n", -rv);
        goto cleanup;
    }

    /* Should now work */
436
    virCgroupFree(&cgroup);
437 438 439 440 441 442
    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;
    }

    ret = validateCgroup(cgroup, "/user/berrange.user/production.partition",
443
                         mountsFull, links, placementFull, NULL, NULL, 0);
444

445
 cleanup:
446
    virCgroupFree(&cgroup);
447 448 449 450 451 452 453 454 455 456 457 458
    return ret;
}



static int testCgroupNewForPartitionDomain(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr partitioncgroup = NULL;
    virCgroupPtr domaincgroup = NULL;
    int ret = -1;
    int rv;
    const char *placement[VIR_CGROUP_CONTROLLER_LAST] = {
459 460 461 462
        [VIR_CGROUP_CONTROLLER_CPU] = "/production.partition/foo.libvirt-lxc",
        [VIR_CGROUP_CONTROLLER_CPUACCT] = "/production.partition/foo.libvirt-lxc",
        [VIR_CGROUP_CONTROLLER_CPUSET] = "/production.partition/foo.libvirt-lxc",
        [VIR_CGROUP_CONTROLLER_MEMORY] = "/production.partition/foo.libvirt-lxc",
463
        [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
464 465
        [VIR_CGROUP_CONTROLLER_FREEZER] = "/production.partition/foo.libvirt-lxc",
        [VIR_CGROUP_CONTROLLER_BLKIO] = "/production.partition/foo.libvirt-lxc",
466
        [VIR_CGROUP_CONTROLLER_SYSTEMD] = "/user/berrange/123",
467 468 469 470 471 472 473 474 475 476 477 478
    };

    if ((rv = virCgroupNewPartition("/production", true, -1, &partitioncgroup)) != 0) {
        fprintf(stderr, "Failed to create /production cgroup: %d\n", -rv);
        goto cleanup;
    }

    if ((rv = virCgroupNewDomainPartition(partitioncgroup, "lxc", "foo", true, &domaincgroup)) != 0) {
        fprintf(stderr, "Cannot create LXC cgroup: %d\n", -rv);
        goto cleanup;
    }

479
    ret = validateCgroup(domaincgroup, "/production.partition/foo.libvirt-lxc", mountsFull, links, placement, NULL, NULL, 0);
480

481
 cleanup:
482 483
    virCgroupFree(&partitioncgroup);
    virCgroupFree(&domaincgroup);
484 485 486
    return ret;
}

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
static int testCgroupNewForPartitionDomainEscaped(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr partitioncgroup1 = NULL;
    virCgroupPtr partitioncgroup2 = NULL;
    virCgroupPtr partitioncgroup3 = NULL;
    virCgroupPtr domaincgroup = NULL;
    int ret = -1;
    int rv;
    const char *placement[VIR_CGROUP_CONTROLLER_LAST] = {
        [VIR_CGROUP_CONTROLLER_CPU] = "/_cgroup.evil/net_cls.evil/__evil.evil/_cpu.foo.libvirt-lxc",
        [VIR_CGROUP_CONTROLLER_CPUACCT] = "/_cgroup.evil/net_cls.evil/__evil.evil/_cpu.foo.libvirt-lxc",
        [VIR_CGROUP_CONTROLLER_CPUSET] = "/_cgroup.evil/net_cls.evil/__evil.evil/_cpu.foo.libvirt-lxc",
        [VIR_CGROUP_CONTROLLER_MEMORY] = "/_cgroup.evil/net_cls.evil/__evil.evil/_cpu.foo.libvirt-lxc",
        [VIR_CGROUP_CONTROLLER_DEVICES] = NULL,
        [VIR_CGROUP_CONTROLLER_FREEZER] = "/_cgroup.evil/net_cls.evil/__evil.evil/_cpu.foo.libvirt-lxc",
        [VIR_CGROUP_CONTROLLER_BLKIO] = "/_cgroup.evil/net_cls.evil/__evil.evil/_cpu.foo.libvirt-lxc",
503
        [VIR_CGROUP_CONTROLLER_SYSTEMD] = "/user/berrange/123",
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
    };

    if ((rv = virCgroupNewPartition("/cgroup.evil", true, -1, &partitioncgroup1)) != 0) {
        fprintf(stderr, "Failed to create /cgroup.evil cgroup: %d\n", -rv);
        goto cleanup;
    }

    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;
    }

    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;
    }

    if ((rv = virCgroupNewDomainPartition(partitioncgroup3, "lxc", "cpu.foo", true, &domaincgroup)) != 0) {
        fprintf(stderr, "Cannot create LXC cgroup: %d\n", -rv);
        goto cleanup;
    }

    /* 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
     */
530
    ret = validateCgroup(domaincgroup, "/_cgroup.evil/net_cls.evil/__evil.evil/_cpu.foo.libvirt-lxc", mountsFull, links, placement, NULL, NULL, 0);
531

532
 cleanup:
533 534 535 536
    virCgroupFree(&partitioncgroup3);
    virCgroupFree(&partitioncgroup2);
    virCgroupFree(&partitioncgroup1);
    virCgroupFree(&domaincgroup);
537 538 539
    return ret;
}

540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
static int testCgroupNewForSelfAllInOne(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr cgroup = NULL;
    int ret = -1;
    const char *placement[VIR_CGROUP_CONTROLLER_LAST] = {
        [VIR_CGROUP_CONTROLLER_CPU] = "/",
        [VIR_CGROUP_CONTROLLER_CPUACCT] = "/",
        [VIR_CGROUP_CONTROLLER_CPUSET] = "/",
        [VIR_CGROUP_CONTROLLER_MEMORY] = "/",
        [VIR_CGROUP_CONTROLLER_DEVICES] = "/",
        [VIR_CGROUP_CONTROLLER_FREEZER] = NULL,
        [VIR_CGROUP_CONTROLLER_BLKIO] = "/",
    };

    if (virCgroupNewSelf(&cgroup) < 0) {
        fprintf(stderr, "Cannot create cgroup for self\n");
        goto cleanup;
    }

559
    ret = validateCgroup(cgroup, "", mountsAllInOne, linksAllInOne, placement, NULL, NULL, 0);
560

561
 cleanup:
562
    virCgroupFree(&cgroup);
563 564 565 566
    return ret;
}


567
static int testCgroupNewForSelfLogind(const void *args ATTRIBUTE_UNUSED)
P
Pavel Hrdina 已提交
568 569 570 571 572 573 574 575 576 577 578 579 580 581
{
    virCgroupPtr cgroup = NULL;

    if (virCgroupNewSelf(&cgroup) >= 0) {
        fprintf(stderr, "Expected to fail, only systemd cgroup available.\n");
        virCgroupFree(&cgroup);
        return -1;
    }

    return 0;
}


static int testCgroupNewForSelfUnified(const void *args ATTRIBUTE_UNUSED)
582 583 584
{
    virCgroupPtr cgroup = NULL;
    int ret = -1;
P
Pavel Hrdina 已提交
585 586 587 588 589 590
    const char *empty[VIR_CGROUP_CONTROLLER_LAST] = { 0 };
    unsigned int controllers =
        (1 << VIR_CGROUP_CONTROLLER_CPU) |
        (1 << VIR_CGROUP_CONTROLLER_CPUACCT) |
        (1 << VIR_CGROUP_CONTROLLER_MEMORY) |
        (1 << VIR_CGROUP_CONTROLLER_BLKIO);
591

P
Pavel Hrdina 已提交
592 593
    if (virCgroupNewSelf(&cgroup) < 0) {
        fprintf(stderr, "Cannot create cgroup for self\n");
594 595 596
        goto cleanup;
    }

P
Pavel Hrdina 已提交
597 598
    ret = validateCgroup(cgroup, "", empty, empty, empty,
                         "/not/really/sys/fs/cgroup", "/", controllers);
599
 cleanup:
600
    virCgroupFree(&cgroup);
601 602 603 604
    return ret;
}


P
Pavel Hrdina 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
static int testCgroupNewForSelfHybrid(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr cgroup = NULL;
    int ret = -1;
    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",
        [VIR_CGROUP_CONTROLLER_DEVICES] = "/not/really/sys/fs/cgroup/devices",
        [VIR_CGROUP_CONTROLLER_FREEZER] = "/not/really/sys/fs/cgroup/freezer",
        [VIR_CGROUP_CONTROLLER_NET_CLS] = "/not/really/sys/fs/cgroup/net_cls",
        [VIR_CGROUP_CONTROLLER_PERF_EVENT] = "/not/really/sys/fs/cgroup/perf_event",
    };
    const char *placement[VIR_CGROUP_CONTROLLER_LAST] = {
        [VIR_CGROUP_CONTROLLER_CPUSET] = "/",
        [VIR_CGROUP_CONTROLLER_DEVICES] = "/",
        [VIR_CGROUP_CONTROLLER_FREEZER] = "/",
        [VIR_CGROUP_CONTROLLER_NET_CLS] = "/",
        [VIR_CGROUP_CONTROLLER_PERF_EVENT] = "/",
    };
    unsigned int controllers =
        (1 << VIR_CGROUP_CONTROLLER_CPU) |
        (1 << VIR_CGROUP_CONTROLLER_CPUACCT) |
        (1 << VIR_CGROUP_CONTROLLER_MEMORY) |
        (1 << VIR_CGROUP_CONTROLLER_BLKIO);

    if (virCgroupNewSelf(&cgroup) < 0) {
        fprintf(stderr, "Cannot create cgroup for self\n");
        goto cleanup;
    }

    ret = validateCgroup(cgroup, "", mounts, empty, placement,
                         "/not/really/sys/fs/cgroup/unified", "/", controllers);

 cleanup:
    virCgroupFree(&cgroup);
    return ret;
}


644 645 646 647 648 649 650 651 652 653 654 655 656 657
static int testCgroupAvailable(const void *args)
{
    bool got = virCgroupAvailable();
    bool want = args == (void*)0x1;

    if (got != want) {
        fprintf(stderr, "Expected cgroup %savailable, but state was wrong\n",
                want ? "" : "not ");
        return -1;
    }

    return 0;
}

658 659 660 661
static int testCgroupControllerAvailable(const void *args ATTRIBUTE_UNUSED)
{
    int ret = 0;

662 663 664 665 666 667 668
# define CHECK_CONTROLLER(c, present) \
    if ((present && !virCgroupControllerAvailable(c)) || \
        (!present && virCgroupControllerAvailable(c))) { \
        fprintf(stderr, present ? \
                "Expected controller %s not available\n" : \
                "Unexpected controller %s available\n", #c); \
        ret = -1; \
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
    }

    CHECK_CONTROLLER(VIR_CGROUP_CONTROLLER_CPU, true)
    CHECK_CONTROLLER(VIR_CGROUP_CONTROLLER_CPUACCT, true)
    CHECK_CONTROLLER(VIR_CGROUP_CONTROLLER_CPUSET, true)
    CHECK_CONTROLLER(VIR_CGROUP_CONTROLLER_MEMORY, true)
    CHECK_CONTROLLER(VIR_CGROUP_CONTROLLER_DEVICES, false)
    CHECK_CONTROLLER(VIR_CGROUP_CONTROLLER_FREEZER, true)
    CHECK_CONTROLLER(VIR_CGROUP_CONTROLLER_BLKIO, true)
    CHECK_CONTROLLER(VIR_CGROUP_CONTROLLER_NET_CLS, false)
    CHECK_CONTROLLER(VIR_CGROUP_CONTROLLER_PERF_EVENT, false)
    CHECK_CONTROLLER(VIR_CGROUP_CONTROLLER_SYSTEMD, true)

# undef CHECK_CONTROLLER
    return ret;
}

686 687 688 689 690
static int testCgroupGetPercpuStats(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr cgroup = NULL;
    size_t i;
    int rv, ret = -1;
691 692
    virTypedParameterPtr params = NULL;
# define EXPECTED_NCPUS 160
693

J
Ján Tomko 已提交
694
    unsigned long long expected[EXPECTED_NCPUS] = {
695
        0, 0, 0, 0, 0, 0, 0, 0,
696 697
        7059492996ULL, 0, 0, 0, 0, 0, 0, 0,
        4180532496ULL, 0, 0, 0, 0, 0, 0, 0,
698 699 700
        0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0,
701 702 703 704 705 706 707 708 709 710 711 712 713 714
        1957541268ULL, 0, 0, 0, 0, 0, 0, 0,
        2065932204ULL, 0, 0, 0, 0, 0, 0, 0,
        18228689414ULL, 0, 0, 0, 0, 0, 0, 0,
        4245525148ULL, 0, 0, 0, 0, 0, 0, 0,
        2911161568ULL, 0, 0, 0, 0, 0, 0, 0,
        1407758136ULL, 0, 0, 0, 0, 0, 0, 0,
        1836807700ULL, 0, 0, 0, 0, 0, 0, 0,
        1065296618ULL, 0, 0, 0, 0, 0, 0, 0,
        2046213266ULL, 0, 0, 0, 0, 0, 0, 0,
        747889778ULL, 0, 0, 0, 0, 0, 0, 0,
        709566900ULL, 0, 0, 0, 0, 0, 0, 0,
        444777342ULL, 0, 0, 0, 0, 0, 0, 0,
        5683512916ULL, 0, 0, 0, 0, 0, 0, 0,
        635751356ULL, 0, 0, 0, 0, 0, 0, 0,
715
    };
716 717 718

    if (VIR_ALLOC_N(params, EXPECTED_NCPUS) < 0)
        goto cleanup;
719 720 721 722 723 724 725 726 727

    if ((rv = virCgroupNewPartition("/virtualmachines", true,
                                    (1 << VIR_CGROUP_CONTROLLER_CPU) |
                                    (1 << VIR_CGROUP_CONTROLLER_CPUACCT),
                                    &cgroup)) < 0) {
        fprintf(stderr, "Could not create /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }

728 729
    if (virHostCPUGetCount() != EXPECTED_NCPUS) {
        fprintf(stderr, "Unexpected: virHostCPUGetCount() yields: %d\n", virHostCPUGetCount());
730 731 732 733 734
        goto cleanup;
    }

    if ((rv = virCgroupGetPercpuStats(cgroup,
                                      params,
735
                                      1, 0, EXPECTED_NCPUS, NULL)) < 0) {
736 737 738 739
        fprintf(stderr, "Failed call to virCgroupGetPercpuStats for /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }

740
    for (i = 0; i < EXPECTED_NCPUS; i++) {
741
        if (STRNEQ(params[i].field, VIR_DOMAIN_CPU_STATS_CPUTIME)) {
742
            fprintf(stderr,
743 744
                    "Wrong parameter name value from virCgroupGetPercpuStats at %zu (is: %s)\n",
                    i, params[i].field);
745 746 747 748 749
            goto cleanup;
        }

        if (params[i].type != VIR_TYPED_PARAM_ULLONG) {
            fprintf(stderr,
750 751
                    "Wrong parameter value type from virCgroupGetPercpuStats at %zu (is: %d)\n",
                    i, params[i].type);
752 753 754 755 756
            goto cleanup;
        }

        if (params[i].value.ul != expected[i]) {
            fprintf(stderr,
757
                    "Wrong value from virCgroupGetPercpuStats at %zu (expected %llu)\n",
758
                    i, params[i].value.ul);
759 760 761 762 763 764
            goto cleanup;
        }
    }

    ret = 0;

765
 cleanup:
766
    virCgroupFree(&cgroup);
767
    VIR_FREE(params);
768 769 770
    return ret;
}

771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
static int testCgroupGetMemoryUsage(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr cgroup = NULL;
    int rv, ret = -1;
    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;
    }

    if ((rv = virCgroupGetMemoryUsage(cgroup, &kb)) < 0) {
        fprintf(stderr, "Could not retrieve GetMemoryUsage for /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }

    if (kb != 1421212UL) {
        fprintf(stderr,
                "Wrong value from virCgroupGetMemoryUsage (expected %ld)\n",
                1421212UL);
        goto cleanup;
    }

    ret = 0;

798
 cleanup:
799
    virCgroupFree(&cgroup);
800 801 802
    return ret;
}

803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863

static int
testCgroupGetMemoryStat(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr cgroup = NULL;
    int rv;
    int ret = -1;
    size_t i;

    const unsigned long long expected_values[] = {
        1336619008ULL,
        67100672ULL,
        145887232ULL,
        661872640ULL,
        627400704UL,
        3690496ULL
    };
    const char* names[] = {
        "cache",
        "active_anon",
        "inactive_anon",
        "active_file",
        "inactive_file",
        "unevictable"
    };
    unsigned long long values[ARRAY_CARDINALITY(expected_values)];

    if ((rv = virCgroupNewPartition("/virtualmachines", true,
                                    (1 << VIR_CGROUP_CONTROLLER_MEMORY),
                                    &cgroup)) < 0) {
        fprintf(stderr, "Could not create /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }

    if ((rv = virCgroupGetMemoryStat(cgroup, &values[0],
                                     &values[1], &values[2],
                                     &values[3], &values[4],
                                     &values[5])) < 0) {
        fprintf(stderr, "Could not retrieve GetMemoryStat for /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }

    for (i = 0; i < ARRAY_CARDINALITY(expected_values); i++) {
        /* NB: virCgroupGetMemoryStat returns a KiB scaled value */
        if ((expected_values[i] >> 10) != values[i]) {
            fprintf(stderr,
                    "Wrong value (%llu) for %s from virCgroupGetMemoryStat "
                    "(expected %llu)\n",
                    values[i], names[i], (expected_values[i] >> 10));
            goto cleanup;
        }
    }

    ret = 0;

 cleanup:
    virCgroupFree(&cgroup);
    return ret;
}


864 865 866 867 868 869 870
static int testCgroupGetBlkioIoServiced(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr cgroup = NULL;
    size_t i;
    int rv, ret = -1;

    const long long expected_values[] = {
E
Eric Blake 已提交
871 872
        119084214273ULL,
        822880960513ULL,
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
        9665167,
        73283807
    };
    const char* names[] = {
        "bytes read",
        "bytes written",
        "requests read",
        "requests written"
    };
    long long values[ARRAY_CARDINALITY(expected_values)];

    if ((rv = virCgroupNewPartition("/virtualmachines", true,
                                    (1 << VIR_CGROUP_CONTROLLER_BLKIO),
                                    &cgroup)) < 0) {
        fprintf(stderr, "Could not create /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }

    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;
    }

    for (i = 0; i < ARRAY_CARDINALITY(expected_values); i++) {
        if (expected_values[i] != values[i]) {
            fprintf(stderr,
                    "Wrong value for %s from virCgroupBlkioIoServiced (expected %lld)\n",
                    names[i], expected_values[i]);
            goto cleanup;
        }
    }

    ret = 0;

909
 cleanup:
910
    virCgroupFree(&cgroup);
911 912 913 914 915 916 917 918 919
    return ret;
}

static int testCgroupGetBlkioIoDeviceServiced(const void *args ATTRIBUTE_UNUSED)
{
    virCgroupPtr cgroup = NULL;
    size_t i;
    int rv, ret = -1;
    const long long expected_values0[] = {
E
Eric Blake 已提交
920 921
        59542107136ULL,
        411440480256ULL,
922 923 924 925
        4832583,
        36641903
    };
    const long long expected_values1[] = {
E
Eric Blake 已提交
926 927
        59542107137ULL,
        411440480257ULL,
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
        4832584,
        36641904
    };
    const char* names[] = {
        "bytes read",
        "bytes written",
        "requests read",
        "requests written"
    };
    long long values[ARRAY_CARDINALITY(expected_values0)];

    if ((rv = virCgroupNewPartition("/virtualmachines", true,
                                    (1 << VIR_CGROUP_CONTROLLER_BLKIO),
                                    &cgroup)) < 0) {
        fprintf(stderr, "Could not create /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }

    if ((rv = virCgroupGetBlkioIoDeviceServiced(cgroup,
                                                FAKEDEVDIR0,
                                                values, &values[1],
                                                &values[2], &values[3])) < 0) {
        fprintf(stderr, "Could not retrieve BlkioIoDeviceServiced for /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }

    for (i = 0; i < ARRAY_CARDINALITY(expected_values0); i++) {
        if (expected_values0[i] != values[i]) {
            fprintf(stderr,
                    "Wrong value for %s from virCgroupGetBlkioIoDeviceServiced (expected %lld)\n",
                    names[i], expected_values0[i]);
            goto cleanup;
        }
    }

    if ((rv = virCgroupGetBlkioIoDeviceServiced(cgroup,
                                                FAKEDEVDIR1,
                                                values, &values[1],
                                                &values[2], &values[3])) < 0) {
        fprintf(stderr, "Could not retrieve BlkioIoDeviceServiced for /virtualmachines cgroup: %d\n", -rv);
        goto cleanup;
    }

    for (i = 0; i < ARRAY_CARDINALITY(expected_values1); i++) {
        if (expected_values1[i] != values[i]) {
            fprintf(stderr,
                    "Wrong value for %s from virCgroupGetBlkioIoDeviceServiced (expected %lld)\n",
                    names[i], expected_values1[i]);
            goto cleanup;
        }
    }

    ret = 0;

982
 cleanup:
983
    virCgroupFree(&cgroup);
984 985
    return ret;
}
986

987
# define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX"
988

989 990 991
static char *
initFakeFS(const char *mode,
           const char *filename)
992
{
993
    char *fakerootdir;
994

995
    if (VIR_STRDUP_QUIET(fakerootdir, FAKEROOTDIRTEMPLATE) < 0) {
996 997 998 999
        fprintf(stderr, "Out of memory\n");
        abort();
    }

1000 1001
    if (!mkdtemp(fakerootdir)) {
        fprintf(stderr, "Cannot create fakerootdir");
1002 1003 1004
        abort();
    }

1005
    setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, 1);
1006

1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
    if (mode)
        setenv("VIR_CGROUP_MOCK_MODE", mode, 1);

    if (filename)
        setenv("VIR_CGROUP_MOCK_FILENAME", filename, 1);

    return fakerootdir;
}

static void
cleanupFakeFS(char *fakerootdir)
{
    if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL)
        virFileDeleteTree(fakerootdir);

    VIR_FREE(fakerootdir);
    unsetenv("LIBVIRT_FAKE_ROOT_DIR");
    unsetenv("VIR_CGROUP_MOCK_MODE");
    unsetenv("VIR_CGROUP_MOCK_FILENAME");
}

static int
mymain(void)
{
    int ret = 0;
    char *fakerootdir;

1034
# define DETECT_MOUNTS_FULL(file, fail) \
1035
    do { \
1036
        struct _detectMountsData data = { file, fail }; \
1037 1038
        if (virTestRun("Detect cgroup mounts for " file, \
                       testCgroupDetectMounts, \
1039
                       &data) < 0) \
1040
            ret = -1; \
1041
    } while (0)
1042 1043
# define DETECT_MOUNTS(file) DETECT_MOUNTS_FULL(file, false);
# define DETECT_MOUNTS_FAIL(file) DETECT_MOUNTS_FULL(file, true);
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053

    DETECT_MOUNTS("ovirt-node-6.6");
    DETECT_MOUNTS("ovirt-node-7.1");
    DETECT_MOUNTS("fedora-18");
    DETECT_MOUNTS("fedora-21");
    DETECT_MOUNTS("rhel-7.1");
    DETECT_MOUNTS("cgroups1");
    DETECT_MOUNTS("cgroups2");
    DETECT_MOUNTS("cgroups3");
    DETECT_MOUNTS("all-in-one");
1054
    DETECT_MOUNTS_FAIL("no-cgroups");
1055
    DETECT_MOUNTS("kubevirt");
1056 1057 1058
    fakerootdir = initFakeFS("unified", NULL);
    DETECT_MOUNTS("unified");
    cleanupFakeFS(fakerootdir);
1059 1060 1061
    fakerootdir = initFakeFS("hybrid", NULL);
    DETECT_MOUNTS("hybrid");
    cleanupFakeFS(fakerootdir);
1062

1063
    fakerootdir = initFakeFS(NULL, "systemd");
1064
    if (virTestRun("New cgroup for self", testCgroupNewForSelf, NULL) < 0)
1065 1066
        ret = -1;

1067
    if (virTestRun("New cgroup for partition", testCgroupNewForPartition, NULL) < 0)
1068 1069
        ret = -1;

1070
    if (virTestRun("New cgroup for partition nested", testCgroupNewForPartitionNested, NULL) < 0)
1071 1072
        ret = -1;

1073
    if (virTestRun("New cgroup for partition nested deeply", testCgroupNewForPartitionNestedDeep, NULL) < 0)
1074 1075
        ret = -1;

1076
    if (virTestRun("New cgroup for domain partition", testCgroupNewForPartitionDomain, NULL) < 0)
1077 1078
        ret = -1;

1079
    if (virTestRun("New cgroup for domain partition escaped", testCgroupNewForPartitionDomainEscaped, NULL) < 0)
1080
        ret = -1;
1081

1082
    if (virTestRun("Cgroup available", testCgroupAvailable, (void*)0x1) < 0)
1083 1084
        ret = -1;

1085
    if (virTestRun("Cgroup controller available", testCgroupControllerAvailable, NULL) < 0)
1086 1087
        ret = -1;

1088
    if (virTestRun("virCgroupGetBlkioIoServiced works", testCgroupGetBlkioIoServiced, NULL) < 0)
1089 1090
        ret = -1;

1091
    if (virTestRun("virCgroupGetBlkioIoDeviceServiced works", testCgroupGetBlkioIoDeviceServiced, NULL) < 0)
1092 1093
        ret = -1;

1094
    if (virTestRun("virCgroupGetMemoryUsage works", testCgroupGetMemoryUsage, NULL) < 0)
1095 1096
        ret = -1;

1097 1098 1099
    if (virTestRun("virCgroupGetMemoryStat works", testCgroupGetMemoryStat, NULL) < 0)
        ret = -1;

1100
    if (virTestRun("virCgroupGetPercpuStats works", testCgroupGetPercpuStats, NULL) < 0)
1101
        ret = -1;
1102
    cleanupFakeFS(fakerootdir);
1103

1104
    fakerootdir = initFakeFS(NULL, "all-in-one");
1105
    if (virTestRun("New cgroup for self (allinone)", testCgroupNewForSelfAllInOne, NULL) < 0)
1106
        ret = -1;
1107
    if (virTestRun("Cgroup available", testCgroupAvailable, (void*)0x1) < 0)
1108
        ret = -1;
1109
    cleanupFakeFS(fakerootdir);
1110

1111
    fakerootdir = initFakeFS(NULL, "logind");
1112
    if (virTestRun("New cgroup for self (logind)", testCgroupNewForSelfLogind, NULL) < 0)
1113
        ret = -1;
1114
    if (virTestRun("Cgroup available", testCgroupAvailable, (void*)0x0) < 0)
1115
        ret = -1;
1116
    cleanupFakeFS(fakerootdir);
1117

P
Pavel Hrdina 已提交
1118
    /* cgroup unified */
1119

P
Pavel Hrdina 已提交
1120 1121 1122 1123 1124 1125
    fakerootdir = initFakeFS("unified", "unified");
    if (virTestRun("New cgroup for self (unified)", testCgroupNewForSelfUnified, NULL) < 0)
        ret = -1;
    if (virTestRun("Cgroup available (unified)", testCgroupAvailable, (void*)0x1) < 0)
        ret = -1;
    cleanupFakeFS(fakerootdir);
1126

P
Pavel Hrdina 已提交
1127 1128 1129 1130 1131 1132 1133 1134 1135
    /* cgroup hybrid */

    fakerootdir = initFakeFS("hybrid", "hybrid");
    if (virTestRun("New cgroup for self (hybrid)", testCgroupNewForSelfHybrid, NULL) < 0)
        ret = -1;
    if (virTestRun("Cgroup available (hybrid)", testCgroupAvailable, (void*)0x1) < 0)
        ret = -1;
    cleanupFakeFS(fakerootdir);

1136
    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
1137 1138
}

1139
VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("vircgroup"))
1140 1141 1142 1143 1144 1145 1146 1147

#else
int
main(void)
{
    return EXIT_AM_SKIP;
}
#endif