On Tue, Apr 25, 2017 at 01:10:25PM +0200, Martin Kletzander wrote:
This mock (which is actually not mock at all, see later) can
redirect
all accesses to a path into another path. There is no need to
create mocks for particular directories, you just create a directory
with all the data a redirect the test there.
In the future, this should also be able to register callbacks for
calls/paths, e.g. when the test is going to write into anything under
"/sys/devices", call function fce(); Then in the open() call we would
add information about the fd into some structure and in write() we
would call fce() with parameters like @path to write to, @data to
be written and pointer to optional return value, so that fce() itself
could stop the call from happening or change its behaviour. But
that's an idea for a latter day.
This is not a mock because it will not be preloaded, but compiled in
the test itself. See future patches for usage.
Signed-off-by: Martin Kletzander <mkletzan(a)redhat.com>
---
cfg.mk | 4 +-
tests/Makefile.am | 2 +-
tests/virfilewrapper.c | 281 +++++++++++++++++++++++++++++++++++++++++++++++++
tests/virfilewrapper.h | 31 ++++++
4 files changed, 315 insertions(+), 3 deletions(-)
create mode 100644 tests/virfilewrapper.c
create mode 100644 tests/virfilewrapper.h
diff --git a/cfg.mk b/cfg.mk
index c04a9911eb78..ed4129e418bc 100644
--- a/cfg.mk
+++ b/cfg.mk
@@ -1129,7 +1129,7 @@ exclude_file_name_regexp--sc_copyright_usage = \
^COPYING(|\.LESSER)$$
exclude_file_name_regexp--sc_flags_usage = \
-
^(cfg\.mk|docs/|src/util/virnetdevtap\.c$$|tests/(vir(cgroup|pci|test|usb)|nss|qemuxml2argv)mock\.c$$)
+
^(cfg\.mk|docs/|src/util/virnetdevtap\.c$$|tests/((vir(cgroup|pci|test|usb)|nss|qemuxml2argv)mock|virfilewrapper)\.c$$)
exclude_file_name_regexp--sc_libvirt_unmarked_diagnostics = \
^(src/rpc/gendispatch\.pl$$|tests/)
@@ -1258,7 +1258,7 @@ exclude_file_name_regexp--sc_prohibit_always-defined_macros = \
^tests/virtestmock.c$$
exclude_file_name_regexp--sc_prohibit_readdir = \
- ^tests/.*mock\.c$$
+ ^tests/(.*mock|virfilewrapper)\.c$$
exclude_file_name_regexp--sc_prohibit_cross_inclusion = \
^(src/util/virclosecallbacks\.h|src/util/virhostdev\.h)$$
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 279e9b7da866..2685098f4343 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1156,7 +1156,7 @@ virnumamock_la_CFLAGS = $(AM_CFLAGS)
virnumamock_la_LDFLAGS = $(MOCKLIBS_LDFLAGS)
virnumamock_la_LIBADD = $(MOCKLIBS_LIBS)
else ! WITH_LINUX
-EXTRA_DIST += vircaps2xmltest.c virnumamock.c
+EXTRA_DIST += vircaps2xmltest.c virnumamock.c virfilewrapper.c virfilewrapper.h
endif ! WITH_LINUX
if WITH_NSS
diff --git a/tests/virfilewrapper.c b/tests/virfilewrapper.c
new file mode 100644
index 000000000000..81537a6baf61
--- /dev/null
+++ b/tests/virfilewrapper.c
@@ -0,0 +1,281 @@
+/*
+ * virfilewrapper.c: Wrapper for universal file access
+ *
+ * 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 "virmock.h"
+#include "virfilewrapper.h"
Move ^these under the system ones.
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
You don't need ^this one.
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
Neither ^these two
+#include <dirent.h>
And ^this one gets pulled in by
virfile.h
+
+#include "viralloc.h"
+#include "virstring.h"
+#include "virfile.h"
+
ACK with that.
Erik