c480ed
From d1c2f060ea724b4f430f9e3198676e18bf0656af Mon Sep 17 00:00:00 2001
c480ed
Message-Id: <d1c2f060ea724b4f430f9e3198676e18bf0656af@dist-git>
c480ed
From: Pavel Hrdina <phrdina@redhat.com>
c480ed
Date: Mon, 1 Jul 2019 17:07:56 +0200
c480ed
Subject: [PATCH] vircgrouptest: introduce initFakeFS and cleanupFakeFS helpers
c480ed
MIME-Version: 1.0
c480ed
Content-Type: text/plain; charset=UTF-8
c480ed
Content-Transfer-Encoding: 8bit
c480ed
c480ed
We need to configure multiple env variables for each set of tests so
c480ed
create helper functions to do that.
c480ed
c480ed
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c480ed
(cherry picked from commit 9b9c6528a2f867a3be79022b4f282116d2efd3cd)
c480ed
c480ed
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297
c480ed
c480ed
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c480ed
Message-Id: <b9c0307500ba7b4e95ed097a457590a54dcd0ddb.1561993100.git.phrdina@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
---
c480ed
 tests/vircgrouptest.c | 48 ++++++++++++++++++++++++++++++++-----------
c480ed
 1 file changed, 36 insertions(+), 12 deletions(-)
c480ed
c480ed
diff --git a/tests/vircgrouptest.c b/tests/vircgrouptest.c
c480ed
index c4bf987ca1..1837383e61 100644
c480ed
--- a/tests/vircgrouptest.c
c480ed
+++ b/tests/vircgrouptest.c
c480ed
@@ -831,10 +831,10 @@ static int testCgroupGetBlkioIoDeviceServiced(const void *args ATTRIBUTE_UNUSED)
c480ed
 
c480ed
 # define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX"
c480ed
 
c480ed
-static int
c480ed
-mymain(void)
c480ed
+static char *
c480ed
+initFakeFS(const char *mode,
c480ed
+           const char *filename)
c480ed
 {
c480ed
-    int ret = 0;
c480ed
     char *fakerootdir;
c480ed
 
c480ed
     if (VIR_STRDUP_QUIET(fakerootdir, FAKEROOTDIRTEMPLATE) < 0) {
c480ed
@@ -849,6 +849,33 @@ mymain(void)
c480ed
 
c480ed
     setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, 1);
c480ed
 
c480ed
+    if (mode)
c480ed
+        setenv("VIR_CGROUP_MOCK_MODE", mode, 1);
c480ed
+
c480ed
+    if (filename)
c480ed
+        setenv("VIR_CGROUP_MOCK_FILENAME", filename, 1);
c480ed
+
c480ed
+    return fakerootdir;
c480ed
+}
c480ed
+
c480ed
+static void
c480ed
+cleanupFakeFS(char *fakerootdir)
c480ed
+{
c480ed
+    if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL)
c480ed
+        virFileDeleteTree(fakerootdir);
c480ed
+
c480ed
+    VIR_FREE(fakerootdir);
c480ed
+    unsetenv("LIBVIRT_FAKE_ROOT_DIR");
c480ed
+    unsetenv("VIR_CGROUP_MOCK_MODE");
c480ed
+    unsetenv("VIR_CGROUP_MOCK_FILENAME");
c480ed
+}
c480ed
+
c480ed
+static int
c480ed
+mymain(void)
c480ed
+{
c480ed
+    int ret = 0;
c480ed
+    char *fakerootdir;
c480ed
+
c480ed
 # define DETECT_MOUNTS_FULL(file, fail) \
c480ed
     do { \
c480ed
         struct _detectMountsData data = { file, fail }; \
c480ed
@@ -872,7 +899,7 @@ mymain(void)
c480ed
     DETECT_MOUNTS_FAIL("no-cgroups");
c480ed
     DETECT_MOUNTS("kubevirt");
c480ed
 
c480ed
-    setenv("VIR_CGROUP_MOCK_FILENAME", "systemd", 1);
c480ed
+    fakerootdir = initFakeFS(NULL, "systemd");
c480ed
     if (virTestRun("New cgroup for self", testCgroupNewForSelf, NULL) < 0)
c480ed
         ret = -1;
c480ed
 
c480ed
@@ -908,26 +935,23 @@ mymain(void)
c480ed
 
c480ed
     if (virTestRun("virCgroupGetPercpuStats works", testCgroupGetPercpuStats, NULL) < 0)
c480ed
         ret = -1;
c480ed
-    unsetenv("VIR_CGROUP_MOCK_FILENAME");
c480ed
+    cleanupFakeFS(fakerootdir);
c480ed
 
c480ed
-    setenv("VIR_CGROUP_MOCK_FILENAME", "all-in-one", 1);
c480ed
+    fakerootdir = initFakeFS(NULL, "all-in-one");
c480ed
     if (virTestRun("New cgroup for self (allinone)", testCgroupNewForSelfAllInOne, NULL) < 0)
c480ed
         ret = -1;
c480ed
     if (virTestRun("Cgroup available", testCgroupAvailable, (void*)0x1) < 0)
c480ed
         ret = -1;
c480ed
-    unsetenv("VIR_CGROUP_MOCK_FILENAME");
c480ed
+    cleanupFakeFS(fakerootdir);
c480ed
 
c480ed
-    setenv("VIR_CGROUP_MOCK_FILENAME", "logind", 1);
c480ed
+    fakerootdir = initFakeFS(NULL, "logind");
c480ed
     if (virTestRun("New cgroup for self (logind)", testCgroupNewForSelfLogind, NULL) < 0)
c480ed
         ret = -1;
c480ed
     if (virTestRun("Cgroup available", testCgroupAvailable, (void*)0x0) < 0)
c480ed
         ret = -1;
c480ed
-    unsetenv("VIR_CGROUP_MOCK_FILENAME");
c480ed
+    cleanupFakeFS(fakerootdir);
c480ed
 
c480ed
-    if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL)
c480ed
-        virFileDeleteTree(fakerootdir);
c480ed
 
c480ed
-    VIR_FREE(fakerootdir);
c480ed
 
c480ed
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
c480ed
 }
c480ed
-- 
c480ed
2.22.0
c480ed