[sheepdog] [PATCH v3 4/4] tests/unit: add test_cluster_driver
Kai Zhang
kyle at zelin.io
Fri Jul 12 08:41:38 CEST 2013
Signed-off-by: Kai Zhang <kyle at zelin.io>
---
.gitignore | 1 +
tests/unit/sheep/Makefile.am | 14 +++-
tests/unit/sheep/mock_group.c | 32 ++++++++
tests/unit/sheep/mock_sheep.c | 1 +
tests/unit/sheep/test_cluster_driver.c | 130 ++++++++++++++++++++++++++++++++
5 files changed, 177 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..e27899a 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,18 @@ 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 \
+ $(top_srcdir)/sheep/cluster/local.c \
+ test_cluster_driver.c
+test_cluster_driver_CFLAGS =
+
+if BUILD_ZOOKEEPER
+test_cluster_driver_SOURCES += $(top_srcdir)/sheep/cluster/zookeeper.c
+test_cluster_driver_CFLAGS += -DBUILD_ZOOKEEPER
+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..7a47ca5
--- /dev/null
+++ b/tests/unit/sheep/mock_group.c
@@ -0,0 +1,32 @@
+/*
+ * 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)
+MOCK_VOID_METHOD(sd_update_node_handler, struct sd_node *node)
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..3ea6538
--- /dev/null
+++ b/tests/unit/sheep/test_cluster_driver.c
@@ -0,0 +1,130 @@
+/*
+ * 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>
+
+#ifdef BUILD_ZOOKEEPER
+#include <zookeeper/zookeeper.h>
+#endif
+
+#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 teardown(void)
+{
+ method_reset_all();
+}
+
+static void do_test(const char *arg)
+{
+ struct cluster_driver *driver;
+ struct sd_node node;
+ const char *option;
+ size_t len = 4;
+ void *msg;
+
+ driver = find_cdrv(arg);
+ if (!driver)
+ return;
+
+ option = get_cdrv_option(driver, arg);
+ msg = xmalloc(len);
+
+ 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_local)
+{
+ assert_ret(init_event(4096), 0);
+ do_test("local");
+}
+END_TEST
+
+START_TEST(test_zookeeper)
+{
+ assert_ret(init_event(4096), 0);
+#ifdef BUILD_ZOOKEEPER
+ zoo_set_debug_level(0);
+#endif
+ do_test("zookeeper:localhost:2181,timeout=1000");
+}
+END_TEST
+
+static Suite *test_suite(void)
+{
+ Suite *s = suite_create("test cluster driver");
+
+ /*
+ * If the program is configured with "--enable-zookeeper",
+ * tests succeed only when zookeeper is started externally.
+ * TODO: use JNI to call curator's TestingServer to remove this
+ * dependency
+ */
+ TCase *tc_local = tcase_create("local");
+ TCase *tc_zk = tcase_create("zookeeper");
+ tcase_add_test(tc_local, test_local);
+ tcase_add_test(tc_zk, test_zookeeper);
+
+ suite_add_tcase(s, tc_local);
+ suite_add_tcase(s, tc_zk);
+ tcase_add_checked_fixture(tc_local, NULL, teardown);
+ tcase_add_checked_fixture(tc_zk, NULL, teardown);
+
+ 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