[sheepdog] [PATCH v4 4/4] unittest: add unittest for sheep/vdi.c
Kai Zhang
kyle at zelin.io
Mon Jun 17 15:32:45 CEST 2013
Signed-off-by: Kai Zhang <kyle at zelin.io>
---
.gitignore | 1 +
Makefile.am | 4 ++--
configure.ac | 3 ++-
tests/unit/Makefile.am | 2 +-
tests/unit/sheep/Makefile.am | 21 +++++++++++++++++++++
tests/unit/sheep/mock_request.c | 11 +++++++++++
tests/unit/sheep/mock_sheep.c | 3 +++
tests/unit/sheep/mock_store.c | 24 ++++++++++++++++++++++++
tests/unit/sheep/test_vdi.c | 38 ++++++++++++++++++++++++++++++++++++++
9 files changed, 103 insertions(+), 4 deletions(-)
create mode 100644 tests/unit/sheep/Makefile.am
create mode 100644 tests/unit/sheep/mock_request.c
create mode 100644 tests/unit/sheep/mock_sheep.c
create mode 100644 tests/unit/sheep/mock_store.c
create mode 100644 tests/unit/sheep/test_vdi.c
diff --git a/.gitignore b/.gitignore
index e241ec9..f333fd3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -36,6 +36,7 @@ sheep/sheep
sheepfs/sheepfs
shepherd/shepherd
tests/unit/collie/test_common
+tests/unit/sheep/test_vdi
# directories
.deps
diff --git a/Makefile.am b/Makefile.am
index a19f27d..bbd3296 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -92,12 +92,12 @@ if BUILD_COVERAGE
coverage: clean check
@rm -rf coverage
- @for dir in collie sheep tests/unit/collie ; do \
+ @for dir in collie sheep tests/unit/collie tests/unit/sheep ; do\
$(MAKE) -C $$dir coverage; \
done
@lcov -a collie/collie.info -a sheep/sheep.info \
- -a tests/unit/collie/collie.info \
+ -a tests/unit/collie/collie.info -a tests/unit/sheep/sheep.info \
-o sheep.info && \
lcov -r sheep.info /usr/include/\* -o sheep.info && \
lcov -r sheep.info tests/unit/\* -o sheep.info && \
diff --git a/configure.ac b/configure.ac
index f5ca156..6d9eaf9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -136,7 +136,8 @@ AC_CONFIG_FILES([Makefile
man/Makefile
shepherd/Makefile
tests/unit/Makefile
- tests/unit/collie/Makefile])
+ tests/unit/collie/Makefile
+ tests/unit/sheep/Makefile])
### Local business
diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am
index fcf6858..eb0a50e 100644
--- a/tests/unit/Makefile.am
+++ b/tests/unit/Makefile.am
@@ -1,3 +1,3 @@
MAINTAINERCLEANFILES = Makefile.in
-SUBDIRS = collie
+SUBDIRS = collie sheep
diff --git a/tests/unit/sheep/Makefile.am b/tests/unit/sheep/Makefile.am
new file mode 100644
index 0000000..651470a
--- /dev/null
+++ b/tests/unit/sheep/Makefile.am
@@ -0,0 +1,21 @@
+MAINTAINERCLEANFILES = Makefile.in
+
+TESTS = test_vdi
+
+check_PROGRAMS = ${TESTS}
+
+INCLUDES = -I$(top_srcdir)/include \
+ -I$(top_srcdir)/sheep \
+ @CHECK_CFLAGS@
+
+LIBS = $(top_srcdir)/lib/libsheepdog.a -lpthread \
+ @CHECK_LIBS@
+
+test_vdi_SOURCES = test_vdi.c mock_sheep.c mock_store.c \
+ mock_request.c $(top_srcdir)/sheep/vdi.c
+
+clean-local:
+ rm -f ${check_PROGRAMS} *.o
+
+coverage:
+ @lcov -d . -c -o sheep.info
diff --git a/tests/unit/sheep/mock_request.c b/tests/unit/sheep/mock_request.c
new file mode 100644
index 0000000..9d6495f
--- /dev/null
+++ b/tests/unit/sheep/mock_request.c
@@ -0,0 +1,11 @@
+#include "sheep_priv.h"
+
+int exec_local_req(struct sd_req *rq, void *data)
+{
+ return 0;
+}
+
+void put_request(struct request *req)
+{
+ return;
+}
diff --git a/tests/unit/sheep/mock_sheep.c b/tests/unit/sheep/mock_sheep.c
new file mode 100644
index 0000000..307c0d2
--- /dev/null
+++ b/tests/unit/sheep/mock_sheep.c
@@ -0,0 +1,3 @@
+#include "sheep_priv.h"
+
+struct cluster_info *sys;
diff --git a/tests/unit/sheep/mock_store.c b/tests/unit/sheep/mock_store.c
new file mode 100644
index 0000000..ec37d3c
--- /dev/null
+++ b/tests/unit/sheep/mock_store.c
@@ -0,0 +1,24 @@
+#include "sheep_priv.h"
+
+int read_object(uint64_t oid, char *data, unsigned int datalen,
+ uint64_t offset)
+{
+ return 0;
+}
+
+int write_object(uint64_t oid, char *data, unsigned int datalen,
+ uint64_t offset, bool create)
+{
+ return 0;
+}
+
+int read_backend_object(uint64_t oid, char *data, unsigned int datalen,
+ uint64_t offset)
+{
+ return 0;
+}
+
+int remove_object(uint64_t oid)
+{
+ return 0;
+}
diff --git a/tests/unit/sheep/test_vdi.c b/tests/unit/sheep/test_vdi.c
new file mode 100644
index 0000000..2197d1f
--- /dev/null
+++ b/tests/unit/sheep/test_vdi.c
@@ -0,0 +1,38 @@
+#include <check.h>
+
+#include "sheep_priv.h"
+
+START_TEST(test_vdi)
+{
+ add_vdi_state(1, 1, true);
+ add_vdi_state(2, 1, true);
+ add_vdi_state(3, 2, false);
+
+ ck_assert_int_eq(get_vdi_copy_number(1), 1);
+ ck_assert_int_eq(get_vdi_copy_number(2), 1);
+ ck_assert_int_eq(get_vdi_copy_number(3), 2);
+}
+END_TEST
+
+static Suite *test_suite(void)
+{
+ Suite *s = suite_create("test vdi");
+
+ TCase *tc_vdi = tcase_create("vdi");
+ tcase_add_test(tc_vdi, test_vdi);
+
+ suite_add_tcase(s, tc_vdi);
+
+ return s;
+}
+
+int main(void)
+{
+ int number_failed;
+ Suite *s = test_suite();
+ SRunner *sr = srunner_create(s);
+ srunner_run_all(sr, CK_NORMAL);
+ number_failed = srunner_ntests_failed(sr);
+ srunner_free(sr);
+ return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
+}
--
1.7.9.5
More information about the sheepdog
mailing list