[libvirt] [RFC PATCH v1 7/7] tests for virCgroup.

Daniel P. Berrange berrange at redhat.com
Wed Jan 16 10:15:17 UTC 2013


On Wed, Jan 16, 2013 at 10:53:09AM +0800, Hu Tao wrote:
> ---
>  tests/Makefile.am     |   5 +++
>  tests/vircgrouptest.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 108 insertions(+)
>  create mode 100644 tests/vircgrouptest.c
> 
> diff --git a/tests/Makefile.am b/tests/Makefile.am
> index 61b0a0c..b2ccdc1 100644
> --- a/tests/Makefile.am
> +++ b/tests/Makefile.am
> @@ -99,6 +99,7 @@ test_programs = virshtest sockettest \
>  	virlockspacetest \
>  	virstringtest \
>  	sysinfotest \
> +	vircgrouptest \
>  	$(NULL)
>  
>  if WITH_GNUTLS
> @@ -640,6 +641,10 @@ utiltest_SOURCES = \
>  	utiltest.c testutils.h testutils.c
>  utiltest_LDADD = $(LDADDS)
>  
> +vircgrouptest_SOURCES = \
> +	vircgrouptest.c testutils.h testutils.c
> +vircgrouptest_LDADD = $(LDADDS)
> +
>  if WITH_DRIVER_MODULES
>  virdrivermoduletest_SOURCES = \
>  	virdrivermoduletest.c testutils.h testutils.c
> diff --git a/tests/vircgrouptest.c b/tests/vircgrouptest.c
> new file mode 100644
> index 0000000..8d0387b
> --- /dev/null
> +++ b/tests/vircgrouptest.c
> @@ -0,0 +1,103 @@
> +/*
> + * Copyright (C) 2012 Fujitsu.
> + *
> + * 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"
> +
> +#include "vircgroup.h"
> +
> +static int test_cgroup(const void *data ATTRIBUTE_UNUSED)
> +{
> +    virCgroupItemPtr cpuset;
> +
> +    cpuset = virCgroupItemNew(VIR_CGROUP_CONTROLLER_CPUSET,
> +                              "test_cgroup",
> +                              virCgroupGetAppRoot(VIR_CGROUP_CONTROLLER_CPUSET, false));
> +    if (!cpuset)
> +        return -1;
> +
> +    if (virCgroupSetCpusetCpus(cpuset, "1") < 0) {
> +        virCgroupItemFree(cpuset);
> +        return -1;
> +    }
> +
> +    virCgroupItemFree(cpuset);
> +
> +    return 0;
> +}
> +
> +static int test_child_cgroup(const void *data ATTRIBUTE_UNUSED)
> +{
> +    int ret = -1;
> +    virCgroupItemPtr item = NULL, item1 = NULL, item2 = NULL;
> +    char *cpus;
> +
> +    item = virCgroupItemNew(VIR_CGROUP_CONTROLLER_CPUSET,
> +                            "test_child_cgroup",
> +                            virCgroupGetAppRoot(VIR_CGROUP_CONTROLLER_CPUSET, false));
> +    if (!item)
> +        goto out;
> +
> +    item1 = virCgroupItemNew(VIR_CGROUP_CONTROLLER_CPUSET, "child1", item);
> +    item2 = virCgroupItemNew(VIR_CGROUP_CONTROLLER_CPUSET, "child2", item1);
> +
> +    if (virCgroupGetCpusetCpus(item2, &cpus) < 0)
> +        goto out;
> +
> +    VIR_FREE(cpus);
> +
> +    if (virCgroupSetCpusetCpus(item2, "0") < 0)
> +        goto out;
> +
> +    if (virCgroupGetCpusetCpus(item2, &cpus) < 0)
> +        goto out;
> +
> +    VIR_FREE(cpus);
> +
> +    ret = 0;
> +out:
> +    if (item)
> +        virCgroupItemFree(item);
> +    if (item1)
> +        virCgroupItemFree(item1);
> +    if (item2)
> +        virCgroupItemFree(item2);
> +    return ret;
> +}
> +
> +static int
> +mymain(void)
> +{
> +    int ret = 0;
> +
> +    if (virCgroupInit() < 0)
> +        return -1;
> +
> +    if (virtTestRun("test_cgroup", 1, test_cgroup, NULL) < 0)
> +        ret = -1;
> +    if (virtTestRun("test_child_cgroup", 1, test_child_cgroup, NULL) < 0)
> +        ret = -1;
> +
> +    virCgroupUninit();
> +
> +    return ret;
> +}
> +
> +VIRT_TEST_MAIN(mymain)

This test appears to be interacting with host system state which is not
something we can safely do from unit tests.

If you want to test out cgroups, then the only option is to make use of
an LD_PRELOAD hack to override the various system calls that the
virCgroup APIs do - eg see what we did with tests/securityselinuxhelper.c
to fake some system calls. This might be quite alot of hard work for
cgroups though.


Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|




More information about the libvir-list mailing list