[libvirt] [PATCH 7/9] vircgroupmock: rewrite cgroup fopen mocking

Fabiano Fidêncio fidencio at redhat.com
Wed Sep 19 11:36:53 UTC 2018


On Tue, Sep 18, 2018 at 2:17 PM, Pavel Hrdina <phrdina at redhat.com> wrote:

> Move all the cgroup data into separate files out of vircgroupmock.c
> and rework the fopen function to load data from files.  This will
> make it easier to add more test cases.
>
> Signed-off-by: Pavel Hrdina <phrdina at redhat.com>
>

Reviewed-by: Fabiano Fidêncio <fidencio at redhat.com>


> ---
>  tests/vircgroupdata/all-in-one.cgroups     |   7 ++
>  tests/vircgroupdata/all-in-one.mounts      |   2 +-
>  tests/vircgroupdata/all-in-one.parsed      |  12 +-
>  tests/vircgroupdata/all-in-one.self.cgroup |   1 +
>  tests/vircgroupdata/logind.cgroups         |  10 ++
>  tests/vircgroupdata/logind.mounts          |   2 +
>  tests/vircgroupdata/logind.self.cgroup     |   1 +
>  tests/vircgroupdata/systemd.cgroups        |   8 ++
>  tests/vircgroupdata/systemd.mounts         |  11 ++
>  tests/vircgroupdata/systemd.self.cgroup    |   6 +
>  tests/vircgroupmock.c                      | 133 ++++-----------------
>  tests/vircgrouptest.c                      |  10 +-
>  12 files changed, 79 insertions(+), 124 deletions(-)
>  create mode 100644 tests/vircgroupdata/all-in-one.cgroups
>  create mode 100644 tests/vircgroupdata/all-in-one.self.cgroup
>  create mode 100644 tests/vircgroupdata/logind.cgroups
>  create mode 100644 tests/vircgroupdata/logind.mounts
>  create mode 100644 tests/vircgroupdata/logind.self.cgroup
>  create mode 100644 tests/vircgroupdata/systemd.cgroups
>  create mode 100644 tests/vircgroupdata/systemd.mounts
>  create mode 100644 tests/vircgroupdata/systemd.self.cgroup
>
> diff --git a/tests/vircgroupdata/all-in-one.cgroups
> b/tests/vircgroupdata/all-in-one.cgroups
> new file mode 100644
> index 0000000000..7208e5a0b6
> --- /dev/null
> +++ b/tests/vircgroupdata/all-in-one.cgroups
> @@ -0,0 +1,7 @@
> +#subsys_name    hierarchy       num_cgroups     enabled
> +cpuset   6   1  1
> +cpu      6   1  1
> +cpuacct  6   1  1
> +memory   6   1  1
> +devices  6   1  1
> +blkio    6   1  1
> diff --git a/tests/vircgroupdata/all-in-one.mounts
> b/tests/vircgroupdata/all-in-one.mounts
> index 14093b961c..76c579ff69 100644
> --- a/tests/vircgroupdata/all-in-one.mounts
> +++ b/tests/vircgroupdata/all-in-one.mounts
> @@ -4,4 +4,4 @@ proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
>  udev /dev devtmpfs rw,relatime,size=16458560k,nr_inodes=4114640,mode=755
> 0 0
>  devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000
> 0 0
>  nfsd /proc/fs/nfsd nfsd rw,relatime 0 0
> -cgroup /sys/fs/cgroup cgroup rw,relatime,blkio,devices,memory,cpuacct,cpu,cpuset
> 0 0
> +cgroup /not/really/sys/fs/cgroup cgroup rw,relatime,blkio,devices,memory,cpuacct,cpu,cpuset
> 0 0
> diff --git a/tests/vircgroupdata/all-in-one.parsed
> b/tests/vircgroupdata/all-in-one.parsed
> index 2701778fea..d703d08fb9 100644
> --- a/tests/vircgroupdata/all-in-one.parsed
> +++ b/tests/vircgroupdata/all-in-one.parsed
> @@ -1,10 +1,10 @@
> -cpu          /sys/fs/cgroup
> -cpuacct      /sys/fs/cgroup
> -cpuset       /sys/fs/cgroup
> -memory       /sys/fs/cgroup
> -devices      /sys/fs/cgroup
> +cpu          /not/really/sys/fs/cgroup
> +cpuacct      /not/really/sys/fs/cgroup
> +cpuset       /not/really/sys/fs/cgroup
> +memory       /not/really/sys/fs/cgroup
> +devices      /not/really/sys/fs/cgroup
>  freezer      <null>
> -blkio        /sys/fs/cgroup
> +blkio        /not/really/sys/fs/cgroup
>  net_cls      <null>
>  perf_event   <null>
>  name=systemd <null>
> diff --git a/tests/vircgroupdata/all-in-one.self.cgroup
> b/tests/vircgroupdata/all-in-one.self.cgroup
> new file mode 100644
> index 0000000000..cf237502e9
> --- /dev/null
> +++ b/tests/vircgroupdata/all-in-one.self.cgroup
> @@ -0,0 +1 @@
> +6:blkio,devices,memory,cpuacct,cpu,cpuset:/
> diff --git a/tests/vircgroupdata/logind.cgroups
> b/tests/vircgroupdata/logind.cgroups
> new file mode 100644
> index 0000000000..9d46f130e0
> --- /dev/null
> +++ b/tests/vircgroupdata/logind.cgroups
> @@ -0,0 +1,10 @@
> +#subsys_name    hierarchy       num_cgroups     enabled
> +cpuset    0  1  1
> +cpu       0  1  1
> +cpuacct   0  1  1
> +memory    0  1  0
> +devices   0  1  1
> +freezer   0  1  1
> +net_cls   0  1  1
> +blkio     0  1  1
> +perf_event  0  1  1
> diff --git a/tests/vircgroupdata/logind.mounts
> b/tests/vircgroupdata/logind.mounts
> new file mode 100644
> index 0000000000..3ab908aee9
> --- /dev/null
> +++ b/tests/vircgroupdata/logind.mounts
> @@ -0,0 +1,2 @@
> +none /not/really/sys/fs/cgroup tmpfs rw,rootcontext=system_u:object
> _r:sysfs_t:s0,seclabel,relatime,size=4k,mode=755 0 0
> +systemd /not/really/sys/fs/cgroup/systemd cgroup
> rw,nosuid,nodev,noexec,relatime,name=systemd 0 0
> diff --git a/tests/vircgroupdata/logind.self.cgroup
> b/tests/vircgroupdata/logind.self.cgroup
> new file mode 100644
> index 0000000000..31e0cfe8eb
> --- /dev/null
> +++ b/tests/vircgroupdata/logind.self.cgroup
> @@ -0,0 +1 @@
> +0:name=systemd:/
> diff --git a/tests/vircgroupdata/systemd.cgroups
> b/tests/vircgroupdata/systemd.cgroups
> new file mode 100644
> index 0000000000..d32dfab222
> --- /dev/null
> +++ b/tests/vircgroupdata/systemd.cgroups
> @@ -0,0 +1,8 @@
> +#subsys_name    hierarchy       num_cgroups     enabled
> +cpuset  2       4       1
> +cpu     3       48      1
> +cpuacct 3       48      1
> +memory  4       4       1
> +devices 5       4       1
> +freezer 6       4       1
> +blkio   8       4       1
> diff --git a/tests/vircgroupdata/systemd.mounts
> b/tests/vircgroupdata/systemd.mounts
> new file mode 100644
> index 0000000000..75572c86f7
> --- /dev/null
> +++ b/tests/vircgroupdata/systemd.mounts
> @@ -0,0 +1,11 @@
> +rootfs / rootfs rw 0 0
> +tmpfs /run tmpfs rw,seclabel,nosuid,nodev,mode=755 0 0
> +tmpfs /not/really/sys/fs/cgroup tmpfs rw,seclabel,nosuid,nodev,noexec,mode=755
> 0 0
> +cgroup /not/really/sys/fs/cgroup/systemd cgroup
> rw,nosuid,nodev,noexec,relatime,release_agent=/usr/lib/
> systemd/systemd-cgroups-agent,name=systemd 0 0
> +cgroup /not/really/sys/fs/cgroup/cpuset cgroup
> rw,nosuid,nodev,noexec,relatime,cpuset 0 0
> +cgroup /not/really/sys/fs/cgroup/cpu,cpuacct cgroup
> rw,nosuid,nodev,noexec,relatime,cpuacct,cpu 0 0
> +cgroup /not/really/sys/fs/cgroup/freezer cgroup
> rw,nosuid,nodev,noexec,relatime,freezer 0 0
> +cgroup /not/really/sys/fs/cgroup/blkio cgroup
> rw,nosuid,nodev,noexec,relatime,blkio 0 0
> +cgroup /not/really/sys/fs/cgroup/memory cgroup
> rw,nosuid,nodev,noexec,relatime,memory 0 0
> +/dev/sda1 /boot ext4 rw,seclabel,relatime,data=ordered 0 0
> +tmpfs /tmp tmpfs rw,seclabel,relatime,size=1024000k 0 0
> diff --git a/tests/vircgroupdata/systemd.self.cgroup
> b/tests/vircgroupdata/systemd.self.cgroup
> new file mode 100644
> index 0000000000..2b95af79d2
> --- /dev/null
> +++ b/tests/vircgroupdata/systemd.self.cgroup
> @@ -0,0 +1,6 @@
> +115:memory:/
> +8:blkio:/
> +6:freezer:/
> +3:cpuacct,cpu:/system
> +2:cpuset:/
> +1:name=systemd:/user/berrange/123
> diff --git a/tests/vircgroupmock.c b/tests/vircgroupmock.c
> index e0024b2c63..3afe2fe192 100644
> --- a/tests/vircgroupmock.c
> +++ b/tests/vircgroupmock.c
> @@ -81,85 +81,6 @@ const char *fakedevicedir1 = FAKEDEVDIR1;
>   * of files beneath it
>   */
>
> -/*
> - * Intentionally missing the 'devices' mount.
> - * Co-mounting cpu & cpuacct controllers
> - * An anonymous controller for systemd
> - */
> -const char *procmounts =
> -    "rootfs / rootfs rw 0 0\n"
> -    "tmpfs /run tmpfs rw,seclabel,nosuid,nodev,mode=755 0 0\n"
> -    "tmpfs /not/really/sys/fs/cgroup tmpfs rw,seclabel,nosuid,nodev,noexec,mode=755
> 0 0\n"
> -    "cgroup /not/really/sys/fs/cgroup/systemd cgroup
> rw,nosuid,nodev,noexec,relatime,release_agent=/usr/lib/
> systemd/systemd-cgroups-agent,name=systemd 0 0\n"
> -    "cgroup /not/really/sys/fs/cgroup/cpuset cgroup
> rw,nosuid,nodev,noexec,relatime,cpuset 0 0\n"
> -    "cgroup /not/really/sys/fs/cgroup/cpu,cpuacct cgroup
> rw,nosuid,nodev,noexec,relatime,cpuacct,cpu 0 0\n"
> -    "cgroup /not/really/sys/fs/cgroup/freezer cgroup
> rw,nosuid,nodev,noexec,relatime,freezer 0 0\n"
> -    "cgroup /not/really/sys/fs/cgroup/blkio cgroup
> rw,nosuid,nodev,noexec,relatime,blkio 0 0\n"
> -    "cgroup /not/really/sys/fs/cgroup/memory cgroup
> rw,nosuid,nodev,noexec,relatime,memory 0 0\n"
> -    "/dev/sda1 /boot ext4 rw,seclabel,relatime,data=ordered 0 0\n"
> -    "tmpfs /tmp tmpfs rw,seclabel,relatime,size=1024000k 0 0\n";
> -
> -const char *procselfcgroups =
> -    "115:memory:/\n"
> -    "8:blkio:/\n"
> -    "6:freezer:/\n"
> -    "3:cpuacct,cpu:/system\n"
> -    "2:cpuset:/\n"
> -    "1:name=systemd:/user/berrange/123\n";
> -
> -const char *proccgroups =
> -    "#subsys_name    hierarchy       num_cgroups     enabled\n"
> -    "cpuset  2       4       1\n"
> -    "cpu     3       48      1\n"
> -    "cpuacct 3       48      1\n"
> -    "memory  4       4       1\n"
> -    "devices 5       4       1\n"
> -    "freezer 6       4       1\n"
> -    "blkio   8       4       1\n";
> -
> -
> -const char *procmountsallinone =
> -    "rootfs / rootfs rw 0 0\n"
> -    "sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0\n"
> -    "proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0\n"
> -    "udev /dev devtmpfs rw,relatime,size=16458560k,nr_inodes=4114640,mode=755
> 0 0\n"
> -    "devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000
> 0 0\n"
> -    "nfsd /proc/fs/nfsd nfsd rw,relatime 0 0\n"
> -    "cgroup /not/really/sys/fs/cgroup cgroup
> rw,relatime,blkio,devices,memory,cpuacct,cpu,cpuset 0 0\n";
> -
> -const char *procselfcgroupsallinone =
> -    "6:blkio,devices,memory,cpuacct,cpu,cpuset:/";
> -
> -const char *proccgroupsallinone =
> -    "#subsys_name    hierarchy       num_cgroups     enabled\n"
> -    "cpuset   6   1  1\n"
> -    "cpu      6   1  1\n"
> -    "cpuacct  6   1  1\n"
> -    "memory   6   1  1\n"
> -    "devices  6   1  1\n"
> -    "blkio    6   1  1\n";
> -
> -const char *procmountslogind =
> -    "none /not/really/sys/fs/cgroup tmpfs rw,rootcontext=system_u:object
> _r:sysfs_t:s0,seclabel,relatime,size=4k,mode=755 0 0\n"
> -    "systemd /not/really/sys/fs/cgroup/systemd cgroup
> rw,nosuid,nodev,noexec,relatime,name=systemd 0 0\n";
> -
> -const char *procselfcgroupslogind =
> -    "1:name=systemd:/\n";
> -
> -const char *proccgroupslogind =
> -    "#subsys_name    hierarchy       num_cgroups     enabled\n"
> -    "cpuset    0  1  1\n"
> -    "cpu       0  1  1\n"
> -    "cpuacct   0  1  1\n"
> -    "memory    0  1  0\n"
> -    "devices   0  1  1\n"
> -    "freezer   0  1  1\n"
> -    "net_cls   0  1  1\n"
> -    "blkio     0  1  1\n"
> -    "perf_event  0  1  1\n";
> -
> -
> -
>  static int make_file(const char *path,
>                       const char *name,
>                       const char *value)
> @@ -379,28 +300,16 @@ static void init_sysfs(void)
>
>  FILE *fopen(const char *path, const char *mode)
>  {
> -    const char *mock;
> -    bool allinone = false, logind = false;
> -    init_syms();
> +    char *filepath = NULL;
> +    const char *type = NULL;
> +    FILE *rc = NULL;
> +    const char *filename = getenv("VIR_CGROUP_MOCK_FILENAME");
>
> -    mock = getenv("VIR_CGROUP_MOCK_MODE");
> -    if (mock) {
> -        if (STREQ(mock, "allinone"))
> -            allinone = true;
> -        else if (STREQ(mock, "logind"))
> -            logind = true;
> -    }
> +    init_syms();
>
>      if (STREQ(path, "/proc/mounts")) {
>          if (STREQ(mode, "r")) {
> -            if (allinone)
> -                return fmemopen((void *)procmountsallinone,
> -                                strlen(procmountsallinone), mode);
> -            else if (logind)
> -                return fmemopen((void *)procmountslogind,
> -                                strlen(procmountslogind), mode);
> -            else
> -                return fmemopen((void *)procmounts, strlen(procmounts),
> mode);
> +            type = "mounts";
>          } else {
>              errno = EACCES;
>              return NULL;
> @@ -408,14 +317,7 @@ FILE *fopen(const char *path, const char *mode)
>      }
>      if (STREQ(path, "/proc/cgroups")) {
>          if (STREQ(mode, "r")) {
> -            if (allinone)
> -                return fmemopen((void *)proccgroupsallinone,
> -                                strlen(proccgroupsallinone), mode);
> -            else if (logind)
> -                return fmemopen((void *)proccgroupslogind,
> -                                strlen(proccgroupslogind), mode);
> -            else
> -                return fmemopen((void *)proccgroups, strlen(proccgroups),
> mode);
> +            type = "cgroups";
>          } else {
>              errno = EACCES;
>              return NULL;
> @@ -423,20 +325,25 @@ FILE *fopen(const char *path, const char *mode)
>      }
>      if (STREQ(path, "/proc/self/cgroup")) {
>          if (STREQ(mode, "r")) {
> -            if (allinone)
> -                return fmemopen((void *)procselfcgroupsallinone,
> -                                strlen(procselfcgroupsallinone), mode);
> -            else if (logind)
> -                return fmemopen((void *)procselfcgroupslogind,
> -                                strlen(procselfcgroupslogind), mode);
> -            else
> -                return fmemopen((void *)procselfcgroups,
> strlen(procselfcgroups), mode);
> +            type = "self.cgroup";
>          } else {
>              errno = EACCES;
>              return NULL;
>          }
>      }
>
> +    if (type) {
> +        if (!filename)
> +            abort();
> +        if (virAsprintfQuiet(&filepath, "%s/vircgroupdata/%s.%s",
> +                             abs_srcdir, filename, type) < 0) {
> +            abort();
> +        }
> +        rc = real_fopen(filepath, mode);
> +        free(filepath);
> +        return rc;
> +    }
> +
>      return real_fopen(path, mode);
>  }
>
> diff --git a/tests/vircgrouptest.c b/tests/vircgrouptest.c
> index be50f3e73c..7968903cad 100644
> --- a/tests/vircgrouptest.c
> +++ b/tests/vircgrouptest.c
> @@ -890,6 +890,7 @@ mymain(void)
>      DETECT_MOUNTS("no-cgroups");
>      DETECT_MOUNTS("kubevirt");
>
> +    setenv("VIR_CGROUP_MOCK_FILENAME", "systemd", 1);
>      if (virTestRun("New cgroup for self", testCgroupNewForSelf, NULL) < 0)
>          ret = -1;
>
> @@ -925,20 +926,21 @@ mymain(void)
>
>      if (virTestRun("virCgroupGetPercpuStats works",
> testCgroupGetPercpuStats, NULL) < 0)
>          ret = -1;
> +    unsetenv("VIR_CGROUP_MOCK_FILENAME");
>
> -    setenv("VIR_CGROUP_MOCK_MODE", "allinone", 1);
> +    setenv("VIR_CGROUP_MOCK_FILENAME", "all-in-one", 1);
>      if (virTestRun("New cgroup for self (allinone)",
> testCgroupNewForSelfAllInOne, NULL) < 0)
>          ret = -1;
>      if (virTestRun("Cgroup available", testCgroupAvailable, (void*)0x1) <
> 0)
>          ret = -1;
> -    unsetenv("VIR_CGROUP_MOCK_MODE");
> +    unsetenv("VIR_CGROUP_MOCK_FILENAME");
>
> -    setenv("VIR_CGROUP_MOCK_MODE", "logind", 1);
> +    setenv("VIR_CGROUP_MOCK_FILENAME", "logind", 1);
>      if (virTestRun("New cgroup for self (logind)",
> testCgroupNewForSelfLogind, NULL) < 0)
>          ret = -1;
>      if (virTestRun("Cgroup available", testCgroupAvailable, (void*)0x0) <
> 0)
>          ret = -1;
> -    unsetenv("VIR_CGROUP_MOCK_MODE");
> +    unsetenv("VIR_CGROUP_MOCK_FILENAME");
>
>      if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL)
>          virFileDeleteTree(fakerootdir);
> --
> 2.17.1
>
> --
> libvir-list mailing list
> libvir-list at redhat.com
> https://www.redhat.com/mailman/listinfo/libvir-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20180919/71037412/attachment-0001.htm>


More information about the libvir-list mailing list