[sheepdog] [PATCH 4/4] tests/unit: add test_cluster_driver
Kai Zhang
kyle at zelin.io
Wed Jul 10 12:23:17 CEST 2013
Signed-off-by: Kai Zhang <kyle at zelin.io>
---
.gitignore | 1 +
tests/unit/sheep/Makefile.am | 11 +++-
tests/unit/sheep/mock_group.c | 30 ++++++++++
tests/unit/sheep/mock_sheep.c | 1 +
tests/unit/sheep/test_cluster_driver.c | 103 ++++++++++++++++++++++++++++++++
5 files changed, 145 insertions(+), 1 deletion(-)
create mode 100644 tests/unit/sheep/mock_group.c
create mode 100644 tests/unit/sheep/test_cluster_driver.c
diff --git a/.gitignore b/.gitignore
index d8643a2..197ad95 100644
--- a/.gitignore
+++ b/.gitignore
@@ -38,6 +38,7 @@ shepherd/shepherd
tools/kill_zk_session
tests/unit/collie/test_common
tests/unit/sheep/test_vdi
+tests/unit/sheep/test_cluster_driver
# directories
.deps
diff --git a/tests/unit/sheep/Makefile.am b/tests/unit/sheep/Makefile.am
index 1cc3988..9744c4c 100644
--- a/tests/unit/sheep/Makefile.am
+++ b/tests/unit/sheep/Makefile.am
@@ -1,6 +1,6 @@
MAINTAINERCLEANFILES = Makefile.in
-TESTS = test_vdi
+TESTS = test_vdi test_cluster_driver
check_PROGRAMS = ${TESTS}
@@ -16,6 +16,15 @@ LIBS = $(top_srcdir)/lib/libsheepdog.a \
test_vdi_SOURCES = test_vdi.c mock_sheep.c mock_store.c \
mock_request.c $(top_srcdir)/sheep/vdi.c
+test_cluster_driver_SOURCES = mock_sheep.c mock_group.c \
+ test_cluster_driver.c
+
+if BUILD_ZOOKEEPER
+test_cluster_driver_SOURCES += $(top_srcdir)/sheep/cluster/zookeeper.c
+LIBS += -lzookeeper_mt
+endif
+
+
clean-local:
rm -f ${check_PROGRAMS} *.o
diff --git a/tests/unit/sheep/mock_group.c b/tests/unit/sheep/mock_group.c
new file mode 100644
index 0000000..63d0ac4
--- /dev/null
+++ b/tests/unit/sheep/mock_group.c
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2013 Zelin.io
+ *
+ * Kai Zhang <kyle at zelin.io>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 as published by the Free Software Foundation.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "mock.h"
+
+#include "cluster.h"
+
+MOCK_VOID_METHOD(sd_join_handler, const struct sd_node *joined,
+ const struct sd_node *members,
+ size_t nr_members, enum cluster_join_result result,
+ const void *opaque)
+MOCK_VOID_METHOD(sd_leave_handler, const struct sd_node *left, const struct sd_node *members,
+ size_t nr_members)
+MOCK_VOID_METHOD(sd_notify_handler, const struct sd_node *sender, void *msg, size_t msg_len)
+MOCK_METHOD(sd_block_handler, bool, true, const struct sd_node *sender)
+MOCK_METHOD(sd_reconnect_handler, int, 0)
+MOCK_METHOD(sd_check_join_cb, enum cluster_join_result, CJ_RES_SUCCESS,
+ const struct sd_node *joining,
+ const struct sd_node *nodes,
+ size_t nr_nodes, void *opaque)
diff --git a/tests/unit/sheep/mock_sheep.c b/tests/unit/sheep/mock_sheep.c
index a5d97cb..5454735 100644
--- a/tests/unit/sheep/mock_sheep.c
+++ b/tests/unit/sheep/mock_sheep.c
@@ -14,3 +14,4 @@
#include "sheep_priv.h"
struct system_info *sys;
+LIST_HEAD(cluster_drivers);
diff --git a/tests/unit/sheep/test_cluster_driver.c b/tests/unit/sheep/test_cluster_driver.c
new file mode 100644
index 0000000..4047f4e
--- /dev/null
+++ b/tests/unit/sheep/test_cluster_driver.c
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2013 Zelin.io
+ *
+ * Kai Zhang <kyle at zelin.io>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 as published by the Free Software Foundation.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <check.h>
+#include <zookeeper/zookeeper.h>
+
+#include "cluster.h"
+#include "event.h"
+#include "mock.h"
+
+#define LOOP_WHEN(expr) \
+ while (expr) \
+ event_loop(-1)
+
+#define assert_ret(call, expect) \
+ do { \
+ int __ret = call; \
+ ck_assert_int_eq(__ret, expect); \
+ } while(0)
+
+static void do_test(const char *arg)
+{
+ struct cluster_driver *driver = find_cdrv(arg);
+ struct sd_node node;
+ const char *option = get_cdrv_option(driver, arg);
+ size_t len = 4;
+ void *msg = xmalloc(len);
+ int ret;
+
+ zoo_set_debug_level(0);
+
+ assert_ret(driver->init(option), 0);
+ assert_ret(driver->join(&node, msg, len), 0);
+
+ LOOP_WHEN(METHOD_NR_CALL(sd_check_join_cb) == 0);
+ LOOP_WHEN(METHOD_NR_CALL(sd_join_handler) == 0);
+ ck_assert_int_eq(METHOD_NR_CALL(sd_join_handler), 1);
+
+ assert_ret(driver->block(), 0);
+ assert_ret(driver->block(), 0);
+
+ LOOP_WHEN(METHOD_NR_CALL(sd_block_handler) == 0);
+ ck_assert_int_eq(METHOD_NR_CALL(sd_block_handler), 1);
+
+ assert_ret(driver->unblock(msg, len), 0);
+ LOOP_WHEN(METHOD_NR_CALL(sd_block_handler) == 1);
+
+ ck_assert_int_eq(METHOD_NR_CALL(sd_block_handler), 2);
+ ck_assert_int_eq(METHOD_NR_CALL(sd_notify_handler), 1);
+
+ assert_ret(driver->unblock(msg, len), 0);
+ LOOP_WHEN(METHOD_NR_CALL(sd_notify_handler) == 1);
+
+ ck_assert_int_eq(METHOD_NR_CALL(sd_notify_handler), 2);
+
+ free(msg);
+}
+
+START_TEST(test_zookeeper)
+{
+ assert_ret(init_event(4096), 0);
+ do_test("zookeeper:localhost:2181,timeout=1000");
+}
+END_TEST
+
+static Suite *test_suite(void)
+{
+ Suite *s = suite_create("test cluster driver");
+
+ /*
+ * Current tests succeed only when zookeeper is started externally.
+ * TODO: use JNI to call curator's TestingServer to remove this
+ * dependency
+ */
+ TCase *tc_zk = tcase_create("zookeeper");
+ tcase_add_test(tc_zk, test_zookeeper);
+
+ suite_add_tcase(s, tc_zk);
+
+ return s;
+}
+
+int main(void)
+{
+ int number_failed;
+ Suite *s = test_suite();
+ SRunner *sr = srunner_create(s);
+ srunner_set_fork_status(sr, CK_NOFORK);
+ 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