[sheepdog] [PATCH 2/3] unittest: add unittest for collie/common.c

Kai Zhang kyle at zelin.io
Mon Jun 3 13:32:28 CEST 2013


Signed-off-by: Kai Zhang <kyle at zelin.io>
---
 .gitignore                    |    1 +
 Makefile.am                   |    8 ++++--
 configure.ac                  |    4 ++-
 unittest/Makefile.am          |    3 ++
 unittest/collie/Makefile.am   |   21 ++++++++++++++++++++
 unittest/collie/mock_collie.c |   17 ++++++++++++++++
 unittest/collie/test_common.c |   43 +++++++++++++++++++++++++++++++++++++++++
 7 files changed, 93 insertions(+), 4 deletions(-)
 create mode 100644 unittest/Makefile.am
 create mode 100644 unittest/collie/Makefile.am
 create mode 100644 unittest/collie/mock_collie.c
 create mode 100644 unittest/collie/test_common.c

diff --git a/.gitignore b/.gitignore
index 1c40a20..d569f4f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -35,6 +35,7 @@ collie/collie
 sheep/sheep
 sheepfs/sheepfs
 shepherd/shepherd
+unittest/collie/test_common
 
 # directories
 .deps
diff --git a/Makefile.am b/Makefile.am
index a1bdf67..be63175 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -17,7 +17,7 @@ sheepdogsysconfdir	= ${SHEEPDOGCONFDIR}
 
 sheepdogsysconf_DATA	= 
 
-SUBDIRS			= lib collie sheep include script shepherd
+SUBDIRS			= lib collie sheep include script shepherd unittest
 
 if BUILD_SHEEPFS
 SUBDIRS			+= sheepfs
@@ -88,12 +88,14 @@ coverage: clean
 	@rm -rf coverage
 	$(MAKE) AM_CFLAGS=--coverage && $(MAKE) check AM_CFLAGS=--coverage
 
-	@for dir in collie sheep ; do		 			\
-		$(MAKE) -C $$dir coverage; 				\
+	@for dir in collie sheep unittest/collie ; do			\
+		$(MAKE) -C $$dir coverage;				\
 	done
 
 	lcov -a collie/collie.info -a sheep/sheep.info	 		\
+	-a unittest/collie/collie.info					\
 	-o sheep.info && 						\
 	lcov -r sheep.info /usr/include/\* -o sheep.info && 		\
+	lcov -r sheep.info unittest/\* -o sheep.info &&			\
 	genhtml sheep.info -o coverage
 
diff --git a/configure.ac b/configure.ac
index ef47672..550e462 100644
--- a/configure.ac
+++ b/configure.ac
@@ -135,7 +135,9 @@ AC_CONFIG_FILES([Makefile
 		script/Makefile
 		lib/Makefile
 		man/Makefile
-		shepherd/Makefile])
+		shepherd/Makefile
+		unittest/Makefile
+		unittest/collie/Makefile])
 
 ### Local business
 
diff --git a/unittest/Makefile.am b/unittest/Makefile.am
new file mode 100644
index 0000000..fcf6858
--- /dev/null
+++ b/unittest/Makefile.am
@@ -0,0 +1,3 @@
+MAINTAINERCLEANFILES	= Makefile.in
+
+SUBDIRS			= collie
diff --git a/unittest/collie/Makefile.am b/unittest/collie/Makefile.am
new file mode 100644
index 0000000..8c21446
--- /dev/null
+++ b/unittest/collie/Makefile.am
@@ -0,0 +1,21 @@
+MAINTAINERCLEANFILES	= Makefile.in
+
+TESTS			= test_common
+
+check_PROGRAMS		= ${TESTS}
+
+INCLUDES		= -I$(top_srcdir)/include			\
+			  -I$(top_srcdir)/collie			\
+			  -I$(top_srcdir)/collie/farm			\
+			  @CHECK_CFLAGS@
+
+LIBS			= $(top_srcdir)/lib/libsheepdog.a @CHECK_LIBS@
+
+test_common_SOURCES	= test_common.c mock_collie.c			\
+			  $(top_srcdir)/collie/common.c
+
+clean-local:
+	rm -f ${check_PROGRAMS} *.o
+
+coverage:
+	@lcov -d . -c -o collie.info
diff --git a/unittest/collie/mock_collie.c b/unittest/collie/mock_collie.c
new file mode 100644
index 0000000..d170a17
--- /dev/null
+++ b/unittest/collie/mock_collie.c
@@ -0,0 +1,17 @@
+#include "collie.h"
+
+/* collie mock */
+const char *sdhost = "127.0.0.1";
+int sdport = 7000;
+bool highlight = true;
+bool raw_output;
+
+int update_node_list(int max_nodes)
+{
+	return 0;
+}
+
+void subcommand_usage(char *cmd, char *subcmd, int status)
+{
+	return;
+}
diff --git a/unittest/collie/test_common.c b/unittest/collie/test_common.c
new file mode 100644
index 0000000..a180b5e
--- /dev/null
+++ b/unittest/collie/test_common.c
@@ -0,0 +1,43 @@
+#include <check.h>
+
+#include "collie.h"
+
+/* test */
+START_TEST(test_common)
+{
+	char str[100];
+
+	raw_output = true;
+	ck_assert_str_eq(size_to_str(10, str, 100), "10");
+	ck_assert_str_eq(size_to_str(10000, str, 100), "10000");
+	ck_assert_str_eq(size_to_str(100000000, str, 100), "100000000");
+
+	raw_output = false;
+	ck_assert_str_eq(size_to_str(10, str, 100), "0.0 MB");
+	ck_assert_str_eq(size_to_str(10000, str, 100), "0.0 MB");
+	ck_assert_str_eq(size_to_str(100000000, str, 100), "95 MB");
+}
+END_TEST
+
+static Suite *test_suite(void)
+{
+	Suite *s = suite_create("test common");
+
+	TCase *tc_common = tcase_create("common");
+	tcase_add_test(tc_common, test_common);
+
+	suite_add_tcase(s, tc_common);
+
+	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