From 0f9ec32097f860ef93d61e96361c16513131851c Mon Sep 17 00:00:00 2001 From: Albert Chu Date: Wed, 24 Jan 2018 15:08:15 -0800 Subject: [PATCH] modules/kvs: Add kvsroot unit tests --- src/modules/kvs/Makefile.am | 15 ++- src/modules/kvs/test/kvsroot.c | 227 +++++++++++++++++++++++++++++++++ 2 files changed, 241 insertions(+), 1 deletion(-) create mode 100644 src/modules/kvs/test/kvsroot.c diff --git a/src/modules/kvs/Makefile.am b/src/modules/kvs/Makefile.am index f92740d2c96d..19497164552f 100644 --- a/src/modules/kvs/Makefile.am +++ b/src/modules/kvs/Makefile.am @@ -43,7 +43,8 @@ TESTS = \ test_fence.t \ test_commit.t \ test_kvs_util.t \ - test_msg_cb_handler.t + test_msg_cb_handler.t \ + test_kvsroot.t test_ldadd = \ $(top_builddir)/src/common/libkvs/libkvs.la \ @@ -119,3 +120,15 @@ test_msg_cb_handler_t_CPPFLAGS = $(test_cppflags) test_msg_cb_handler_t_LDADD = \ $(top_builddir)/src/modules/kvs/msg_cb_handler.o \ $(test_ldadd) + +test_kvsroot_t_SOURCES = test/kvsroot.c +test_kvsroot_t_CPPFLAGS = $(test_cppflags) +test_kvsroot_t_LDADD = \ + $(top_builddir)/src/modules/kvs/kvsroot.o \ + $(top_builddir)/src/modules/kvs/waitqueue.o \ + $(top_builddir)/src/modules/kvs/commit.o \ + $(top_builddir)/src/modules/kvs/cache.o \ + $(top_builddir)/src/modules/kvs/fence.o \ + $(top_builddir)/src/modules/kvs/msg_cb_handler.o \ + $(top_builddir)/src/modules/kvs/kvs_util.o \ + $(test_ldadd) diff --git a/src/modules/kvs/test/kvsroot.c b/src/modules/kvs/test/kvsroot.c new file mode 100644 index 000000000000..14f0218490d1 --- /dev/null +++ b/src/modules/kvs/test/kvsroot.c @@ -0,0 +1,227 @@ +#if HAVE_CONFIG_H +#include "config.h" +#endif +#include +#include + +#include "src/common/libtap/tap.h" +#include "src/common/libkvs/kvs.h" +#include "src/modules/kvs/kvsroot.h" +#include "src/modules/kvs/fence.h" + +int global = 0; + +void basic_api_tests (void) +{ + kvsroot_mgr_t *km; + struct cache *cache; + struct kvsroot *root; + struct kvsroot *tmproot; + + cache = cache_create (); + + ok ((km = kvsroot_mgr_create (NULL, &global)) != NULL, + "kvsroot_mgr_create works"); + + ok (kvsroot_mgr_root_count (km) == 0, + "kvsroot_mgr_root_count returns correct count of roots"); + + ok ((root = kvsroot_mgr_create_root (km, + cache, + "sha1", + KVS_PRIMARY_NAMESPACE, + 0)) != NULL, + "kvsroot_mgr_create_root works"); + + ok (kvsroot_mgr_root_count (km) == 1, + "kvsroot_mgr_root_count returns correct count of roots"); + + ok ((tmproot = kvsroot_mgr_lookup_root (km, KVS_PRIMARY_NAMESPACE)) != NULL, + "kvsroot_mgr_lookup_root works"); + + ok (tmproot == root, + "kvsroot_mgr_lookup_root returns correct root"); + + ok ((tmproot = kvsroot_mgr_lookup_root_safe (km, KVS_PRIMARY_NAMESPACE)) != NULL, + "kvsroot_mgr_lookup_root_safe works"); + + ok (tmproot == root, + "kvsroot_mgr_lookup_root_safe returns correct root"); + + root->remove = true; + + ok ((tmproot = kvsroot_mgr_lookup_root (km, KVS_PRIMARY_NAMESPACE)) != NULL, + "kvsroot_mgr_lookup_root works"); + + ok (tmproot == root, + "kvsroot_mgr_lookup_root returns correct root"); + + ok (kvsroot_mgr_lookup_root_safe (km, KVS_PRIMARY_NAMESPACE) == NULL, + "kvsroot_mgr_lookup_root_safe returns NULL on root marked removed"); + + ok (kvsroot_mgr_remove_root (km, KVS_PRIMARY_NAMESPACE) == 0, + "kvsroot_mgr_remove_root works"); + + ok (kvsroot_mgr_lookup_root (km, KVS_PRIMARY_NAMESPACE) == NULL, + "kvsroot_mgr_lookup_root returns NULL after namespace removed"); + + ok (kvsroot_mgr_lookup_root_safe (km, KVS_PRIMARY_NAMESPACE) == NULL, + "kvsroot_mgr_lookup_root_safe returns NULL after namespace removed"); + + kvsroot_mgr_destroy (km); + + /* destroy works with NULL */ + kvsroot_mgr_destroy (NULL); + + cache_destroy (cache); +} + +int count_roots_cb (struct kvsroot *root, void *arg) +{ + int *count = arg; + (*count)++; + return 0; +} + +int count_roots_early_exit_cb (struct kvsroot *root, void *arg) +{ + int *count = arg; + (*count)++; + return 1; +} + +int roots_error_cb (struct kvsroot *root, void *arg) +{ + return -1; +} + + +int roots_remove_cb (struct kvsroot *root, void *arg) +{ + kvsroot_mgr_t *km = arg; + kvsroot_mgr_remove_root (km, root->namespace); + return 1; +} + +void basic_iter_tests (void) +{ + kvsroot_mgr_t *km; + struct cache *cache; + struct kvsroot *root; + int count; + + cache = cache_create (); + + ok ((km = kvsroot_mgr_create (NULL, &global)) != NULL, + "kvsroot_mgr_create works"); + + ok ((root = kvsroot_mgr_create_root (km, + cache, + "sha1", + "foo", + 0)) != NULL, + "kvsroot_mgr_create_root works"); + + ok ((root = kvsroot_mgr_create_root (km, + cache, + "sha1", + "bar", + 0)) != NULL, + "kvsroot_mgr_create_root works"); + + ok (kvsroot_mgr_root_count (km) == 2, + "kvsroot_mgr_root_count returns correct count of roots"); + + count = 0; + ok (kvsroot_mgr_iter_roots (km, count_roots_cb, &count) == 0, + "kvsroot_mgr_iter_roots works"); + + ok (count == 2, + "kvsroot_mgr_iter_roots called callback correct number of times"); + + count = 0; + ok (kvsroot_mgr_iter_roots (km, count_roots_early_exit_cb, &count) == 0, + "kvsroot_mgr_iter_roots works if exitting midway"); + + ok (count == 1, + "kvsroot_mgr_iter_roots called callback correct number of times"); + + ok (kvsroot_mgr_iter_roots (km, roots_error_cb, NULL) < 0, + "kvsroot_mgr_iter_roots errors on error in callback"); + + ok (kvsroot_mgr_iter_roots (km, roots_remove_cb, km) == 0, + "kvsroot_mgr_iter_roots works on remove callback"); + + ok (kvsroot_mgr_root_count (km) == 1, + "kvsroot_mgr_root_count returns correct count of roots after a removal"); + + kvsroot_mgr_destroy (km); + cache_destroy (cache); +} + +void basic_commit_mgr_tests (void) +{ + kvsroot_mgr_t *km; + struct cache *cache; + struct kvsroot *root; + commit_t *c; + fence_t *f; + json_t *ops = NULL; + void *tmpaux; + + cache = cache_create (); + + f = fence_create ("foo", 1, 0); + ops = json_array (); + /* not a real operation */ + json_array_append_new (ops, json_string ("foo")); + + fence_add_request_data (f, ops); + + json_decref (ops); + + ok ((km = kvsroot_mgr_create (NULL, &global)) != NULL, + "kvsroot_mgr_create works"); + + ok ((root = kvsroot_mgr_create_root (km, + cache, + "sha1", + KVS_PRIMARY_NAMESPACE, + 0)) != NULL, + "kvsroot_mgr_create_root works"); + + ok (commit_mgr_add_fence (root->cm, f) == 0, + "commit_mgr_add_fence works"); + + ok (commit_mgr_process_fence_request (root->cm, "foo") == 0, + "commit_mgr_process_fence_request works"); + + ok ((c = commit_mgr_get_ready_commit (root->cm)) != NULL, + "commit_mgr_get_ready_commit returns ready commit"); + + ok ((tmpaux = commit_get_aux (c)) != NULL, + "commit_get_aux returns non-NULL aux"); + + ok (tmpaux == &global, + "commit_get_aux returns correct aux value"); + + kvsroot_mgr_destroy (km); + cache_destroy (cache); +} + +int main (int argc, char *argv[]) +{ + plan (NO_PLAN); + + basic_api_tests (); + basic_iter_tests (); + basic_commit_mgr_tests (); + + done_testing (); + return (0); +} + + +/* + * vi:tabstop=4 shiftwidth=4 expandtab + */