[sheepdog] [PATCH v3 3/3] unittest: add unittest for sheep/vdi.c

Kai Zhang kyle at zelin.io
Sat Jun 8 12:17:23 CEST 2013


Signed-off-by: Kai Zhang <kyle at zelin.io>
---
 .gitignore                    |    1 +
 Makefile.am                   |    4 ++--
 configure.ac                  |    3 ++-
 unittest/Makefile.am          |    2 +-
 unittest/sheep/Makefile.am    |   21 +++++++++++++++++++++
 unittest/sheep/mock_request.c |   11 +++++++++++
 unittest/sheep/mock_sheep.c   |    3 +++
 unittest/sheep/mock_store.c   |   24 ++++++++++++++++++++++++
 unittest/sheep/test_vdi.c     |   38 ++++++++++++++++++++++++++++++++++++++
 9 files changed, 103 insertions(+), 4 deletions(-)
 create mode 100644 unittest/sheep/Makefile.am
 create mode 100644 unittest/sheep/mock_request.c
 create mode 100644 unittest/sheep/mock_sheep.c
 create mode 100644 unittest/sheep/mock_store.c
 create mode 100644 unittest/sheep/test_vdi.c

diff --git a/.gitignore b/.gitignore
index d569f4f..4fa2dfc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -36,6 +36,7 @@ sheep/sheep
 sheepfs/sheepfs
 shepherd/shepherd
 unittest/collie/test_common
+unittest/sheep/test_vdi
 
 # directories
 .deps
diff --git a/Makefile.am b/Makefile.am
index ebdaaee..29e2ead 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 unittest/collie ; do			\
+	@for dir in collie sheep unittest/collie unittest/sheep ; do	\
 		$(MAKE) -C $$dir coverage;				\
 	done
 
 	@lcov -a collie/collie.info -a sheep/sheep.info			\
-	-a unittest/collie/collie.info					\
+	-a unittest/collie/collie.info -a unittest/sheep/sheep.info	\
 	-o sheep.info &&						\
 	lcov -r sheep.info /usr/include/\* -o sheep.info &&		\
 	lcov -r sheep.info unittest/\* -o sheep.info &&			\
diff --git a/configure.ac b/configure.ac
index eb660ae..12b4655 100644
--- a/configure.ac
+++ b/configure.ac
@@ -136,7 +136,8 @@ AC_CONFIG_FILES([Makefile
 		man/Makefile
 		shepherd/Makefile
 		unittest/Makefile
-		unittest/collie/Makefile])
+		unittest/collie/Makefile
+		unittest/sheep/Makefile])
 
 ### Local business
 
diff --git a/unittest/Makefile.am b/unittest/Makefile.am
index fcf6858..eb0a50e 100644
--- a/unittest/Makefile.am
+++ b/unittest/Makefile.am
@@ -1,3 +1,3 @@
 MAINTAINERCLEANFILES	= Makefile.in
 
-SUBDIRS			= collie
+SUBDIRS			= collie sheep
diff --git a/unittest/sheep/Makefile.am b/unittest/sheep/Makefile.am
new file mode 100644
index 0000000..651470a
--- /dev/null
+++ b/unittest/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/unittest/sheep/mock_request.c b/unittest/sheep/mock_request.c
new file mode 100644
index 0000000..9d6495f
--- /dev/null
+++ b/unittest/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/unittest/sheep/mock_sheep.c b/unittest/sheep/mock_sheep.c
new file mode 100644
index 0000000..307c0d2
--- /dev/null
+++ b/unittest/sheep/mock_sheep.c
@@ -0,0 +1,3 @@
+#include "sheep_priv.h"
+
+struct cluster_info *sys;
diff --git a/unittest/sheep/mock_store.c b/unittest/sheep/mock_store.c
new file mode 100644
index 0000000..ec37d3c
--- /dev/null
+++ b/unittest/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/unittest/sheep/test_vdi.c b/unittest/sheep/test_vdi.c
new file mode 100644
index 0000000..2197d1f
--- /dev/null
+++ b/unittest/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.1




More information about the sheepdog mailing list