[sheepdog] [PATCH v4 3/4] unittest: add unittest for collie/common.c
Kai Zhang
kyle at zelin.io
Mon Jun 17 15:32:44 CEST 2013
Signed-off-by: Kai Zhang <kyle at zelin.io>
---
.gitignore | 1 +
Makefile.am | 8 +++++++-
configure.ac | 4 +++-
tests/unit/Makefile.am | 3 +++
tests/unit/collie/Makefile.am | 22 ++++++++++++++++++++
tests/unit/collie/mock_collie.c | 18 ++++++++++++++++
tests/unit/collie/test_common.c | 43 +++++++++++++++++++++++++++++++++++++++
7 files changed, 97 insertions(+), 2 deletions(-)
create mode 100644 tests/unit/Makefile.am
create mode 100644 tests/unit/collie/Makefile.am
create mode 100644 tests/unit/collie/mock_collie.c
create mode 100644 tests/unit/collie/test_common.c
diff --git a/.gitignore b/.gitignore
index 1c40a20..e241ec9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -35,6 +35,7 @@ collie/collie
sheep/sheep
sheepfs/sheepfs
shepherd/shepherd
+tests/unit/collie/test_common
# directories
.deps
diff --git a/Makefile.am b/Makefile.am
index 2d2e158..a19f27d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -25,6 +25,10 @@ endif
SUBDIRS += man
+if BUILD_UNITTEST
+SUBDIRS += tests/unit
+endif
+
install-exec-local:
$(INSTALL) -d $(DESTDIR)/${localstatedir}/lib/sheepdog
@@ -88,12 +92,14 @@ if BUILD_COVERAGE
coverage: clean check
@rm -rf coverage
- @for dir in collie sheep ; do \
+ @for dir in collie sheep tests/unit/collie ; do \
$(MAKE) -C $$dir coverage; \
done
@lcov -a collie/collie.info -a sheep/sheep.info \
+ -a tests/unit/collie/collie.info \
-o sheep.info && \
lcov -r sheep.info /usr/include/\* -o sheep.info && \
+ lcov -r sheep.info tests/unit/\* -o sheep.info && \
genhtml sheep.info -o coverage
endif
diff --git a/configure.ac b/configure.ac
index 4ccadbd..f5ca156 100644
--- a/configure.ac
+++ b/configure.ac
@@ -134,7 +134,9 @@ AC_CONFIG_FILES([Makefile
script/Makefile
lib/Makefile
man/Makefile
- shepherd/Makefile])
+ shepherd/Makefile
+ tests/unit/Makefile
+ tests/unit/collie/Makefile])
### Local business
diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am
new file mode 100644
index 0000000..fcf6858
--- /dev/null
+++ b/tests/unit/Makefile.am
@@ -0,0 +1,3 @@
+MAINTAINERCLEANFILES = Makefile.in
+
+SUBDIRS = collie
diff --git a/tests/unit/collie/Makefile.am b/tests/unit/collie/Makefile.am
new file mode 100644
index 0000000..2471aa1
--- /dev/null
+++ b/tests/unit/collie/Makefile.am
@@ -0,0 +1,22 @@
+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 -lpthread \
+ @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/tests/unit/collie/mock_collie.c b/tests/unit/collie/mock_collie.c
new file mode 100644
index 0000000..b441b18
--- /dev/null
+++ b/tests/unit/collie/mock_collie.c
@@ -0,0 +1,18 @@
+#include "collie.h"
+
+/* collie mock */
+const char *sdhost = "127.0.0.1";
+int sdport = 7000, sd_vnodes_nr = 100;
+bool highlight = true;
+bool raw_output;
+struct sd_vnode sd_vnodes[SD_MAX_VNODES];
+
+int update_node_list(int max_nodes)
+{
+ return 0;
+}
+
+void subcommand_usage(char *cmd, char *subcmd, int status)
+{
+ return;
+}
diff --git a/tests/unit/collie/test_common.c b/tests/unit/collie/test_common.c
new file mode 100644
index 0000000..a180b5e
--- /dev/null
+++ b/tests/unit/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.9.5
More information about the sheepdog
mailing list