]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
tests: cgroupmock: Use the temporary directory as fake root
authorAndrea Bolognani <abologna@redhat.com>
Fri, 4 Dec 2015 09:54:29 +0000 (10:54 +0100)
committerAndrea Bolognani <abologna@redhat.com>
Wed, 9 Dec 2015 14:22:58 +0000 (15:22 +0100)
We might need to mock files living outside SYSFS_PREFIX later on,
so it's better to treat the temporary directory we are passed via
the environment as the root of the fake filesystem and create
SYSFS_PREFIX inside it.

The environment variable name will be changed to reflect the new use
we're making of it in a later commit.

tests/vircgroupmock.c

index f2fee7d1bac9c926c900513ab5b8f65becb53b35..ead18b6935261ce6a2ca65a696f28a314e70bb22 100644 (file)
@@ -31,6 +31,8 @@
 # include <sys/stat.h>
 # include <stdarg.h>
 # include "testutilslxc.h"
+# include "virstring.h"
+# include "virfile.h"
 
 static int (*realopen)(const char *path, int flags, ...);
 static FILE *(*realfopen)(const char *path, const char *mode);
@@ -45,6 +47,7 @@ static int (*realmkdir)(const char *path, mode_t mode);
  * when passed as an arg to asprintf()
  * vircgroupmock.c:462:22: error: static variable 'fakesysfsdir' is used in an inline function with external linkage [-Werror,-Wstatic-in-inline]
  */
+char *fakerootdir;
 char *fakesysfsdir;
 const char *fakedevicedir0 = FAKEDEVDIR0;
 const char *fakedevicedir1 = FAKEDEVDIR1;
@@ -414,14 +417,23 @@ static void init_syms(void)
 
 static void init_sysfs(void)
 {
-    if (fakesysfsdir)
+    if (fakerootdir && fakesysfsdir)
         return;
 
-    if (!(fakesysfsdir = getenv("LIBVIRT_FAKE_SYSFS_DIR"))) {
+    if (!(fakerootdir = getenv("LIBVIRT_FAKE_SYSFS_DIR"))) {
         fprintf(stderr, "Missing LIBVIRT_FAKE_SYSFS_DIR env variable\n");
         abort();
     }
 
+    if (virAsprintfQuiet(&fakesysfsdir, "%s%s",
+                         fakerootdir, SYSFS_PREFIX) < 0)
+        abort();
+
+    if (virFileMakePath(fakesysfsdir) < 0) {
+        fprintf(stderr, "Cannot create %s\n", fakesysfsdir);
+        abort();
+    }
+
 # define MAKE_CONTROLLER(subpath)                               \
     do {                                                        \
         char *path;                                             \