Skip to content

Commit

Permalink
ZSTOR-3513 add mock for munit and increase code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
hongwei.wu committed Dec 6, 2022
1 parent ed4b017 commit d9b999c
Show file tree
Hide file tree
Showing 13 changed files with 684 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.trs
*.dirstamp
*.gcno
*.gcda
test/unit/core
test/unit/uv
test/fuzzy/core
Expand Down
11 changes: 8 additions & 3 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ libtest_la_SOURCES = \
test/lib/fsm.c \
test/lib/heap.c \
test/lib/munit.c \
test/lib/munit_mock.c \
test/lib/tracer.c \
test/lib/tcp.c

Expand All @@ -74,12 +75,13 @@ test_unit_core_SOURCES = \
test/unit/test_log.c \
test/unit/test_queue.c \
test/unit/test_request.c
test_unit_core_CFLAGS = $(AM_CFLAGS) -Wno-conversion
test_unit_core_CFLAGS = $(AM_CFLAGS) -Wno-conversion -std=gnu11
test_unit_core_LDADD = libtest.la ./.libs/libraft.a

if LZ4_AVAILABLE
test_unit_core_CFLAGS += -DLZ4_AVAILABLE
test_unit_core_LDFLAGS = $(LZ4_LIBS)
test_unit_core_LDFLAGS += -Wl,--wrap=LZ4F_createCompressionContext,--wrap=raft_malloc,--wrap=LZ4F_compressBegin
libraft_la_CFLAGS += -DLZ4_AVAILABLE
libraft_la_LDFLAGS += $(LZ4_LIBS)
endif # LZ4_AVAILABLE
Expand Down Expand Up @@ -121,11 +123,14 @@ test_integration_core_SOURCES = \
test/integration/test_paper.c \
test/integration/test_etcd_migrate_1.c \
test/integration/test_etcd_migrate_2.c \
test/integration/test_etcd_migrate_3.c
test/integration/test_etcd_migrate_3.c \
test/integration/mocks.c

test_integration_core_CFLAGS = $(AM_CFLAGS) -Wno-conversion
test_integration_core_CFLAGS = $(AM_CFLAGS) -Wno-conversion -std=gnu11
test_integration_core_LDFLAGS = -no-install
test_integration_core_LDADD = libtest.la ./.libs/libraft.a
test_integration_core_LDFLAGS += -Wl,--wrap=logAppendCommands,--wrap=requestRegEnqueue,--wrap=replicationTrigger
test_integration_core_LDFLAGS += -Wl,--wrap=logAppend,--wrap=configurationCopy,--wrap=logAcquire

test_fuzzy_core_SOURCES = \
test/fuzzy/main_core.c \
Expand Down
10 changes: 8 additions & 2 deletions src/replication.c
Original file line number Diff line number Diff line change
Expand Up @@ -633,13 +633,14 @@ static int appendLeader(struct raft *r, raft_index index)
request->n = n;
request->req.data = request;

r->nr_appending_requests += 1;
rv = r->io->append(r->io, &request->req, entries, n, appendLeaderCb);
if (rv != 0) {
r->nr_appending_requests -= 1;
evtErrf("raft(%llx) append failed %d", r->id, rv);
ErrMsgTransfer(r->io->errmsg, r->errmsg, "io");
goto err_after_request_alloc;
}
r->nr_appending_requests += 1;

return 0;

Expand All @@ -662,6 +663,9 @@ int replicationTrigger(struct raft *r, raft_index index)
return rv;
}

if (r->state != RAFT_LEADER)
return 0;

return triggerAll(r);
}

Expand Down Expand Up @@ -1279,15 +1283,16 @@ int replicationAppend(struct raft *r,
rv = RAFT_SHUTDOWN;
goto err_after_acquire_entries;
}
r->nr_appending_requests += 1;
request->req.data = request;
rv = r->io->append(r->io, &request->req, request->args.entries,
request->args.n_entries, appendFollowerCb);
if (rv != 0) {
r->nr_appending_requests -= 1;
ErrMsgTransfer(r->io->errmsg, r->errmsg, "io");
evtErrf("raft(%llx) append failed %d", r->id, rv);
goto err_after_acquire_entries;
}
r->nr_appending_requests += 1;

entryNonBatchDestroyPrefix(args->entries, args->n_entries, i);
raft_free(args->entries);
Expand Down Expand Up @@ -1736,6 +1741,7 @@ static int takeSnapshot(struct raft *r)
raft_configuration_close(&snapshot->configuration);
abort:
r->snapshot.pending.term = 0;
r->snapshot.put.data = NULL;
return rv;
}

Expand Down
68 changes: 68 additions & 0 deletions test/integration/mocks.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "../lib/munit_mock.h"
#include "../../src/log.h"
#include "../../src/request.h"
#include "../../src/replication.h"
#include "../../src/configuration.h"

extern int __real_logAppendCommands(struct raft_log *l,
const raft_term term,
const struct raft_buffer bufs[],
const unsigned n);

int __wrap_logAppendCommands(struct raft_log *l,
const raft_term term,
const struct raft_buffer bufs[],
const unsigned n)
{
return mock_type_args(int, logAppendCommands, l, term, bufs, n);
}

extern int __real_requestRegEnqueue(struct request_registry *reg,
struct request *req);

int __wrap_requestRegEnqueue(struct request_registry *reg, struct request *req)
{
return mock_type_args(int, requestRegEnqueue, reg, req);
}

extern int __real_replicationTrigger(struct raft *r, raft_index index);

int __wrap_replicationTrigger(struct raft *r, raft_index index)
{
return mock_type_args(int, replicationTrigger, r, index);
}

extern int __real_logAppend(struct raft_log *l,
const raft_term term,
const unsigned short type,
const struct raft_buffer *buf,
void *batch);

int __wrap_logAppend(struct raft_log *l,
const raft_term term,
const unsigned short type,
const struct raft_buffer *buf,
void *batch)
{
return mock_type_args(int, logAppend, l, term, type, buf, batch);
}


extern int __real_configurationCopy(const struct raft_configuration *src,
struct raft_configuration *dst);


int __wrap_configurationCopy(const struct raft_configuration *src,
struct raft_configuration *dst)
{
return mock_type_args(int, configurationCopy, src, dst);
}

extern int __real_logAcquire(struct raft_log *l, const raft_index index,
struct raft_entry *entries[], unsigned *n);

int __wrap_logAcquire(struct raft_log *l, const raft_index index,
struct raft_entry *entries[], unsigned *n)
{
return mock_type_args(int, logAcquire, l, index, entries, n);
}
84 changes: 84 additions & 0 deletions test/integration/test_apply.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "../lib/cluster.h"
#include "../lib/runner.h"
#include "../lib/munit_mock.h"

/******************************************************************************
*
Expand Down Expand Up @@ -95,6 +96,18 @@ static bool applyCbHasFired(struct raft_fixture *f, void *arg)
raft_free(_buf.base); \
} while (0)

/* Submit an apply request. */
#define APPLY_SUBMIT_ERROR(I, RV) \
struct raft_buffer _buf; \
struct raft_apply _req; \
struct result _result = {RV, false}; \
int _rv; \
FsmEncodeSetX(123, &_buf); \
_req.data = &_result; \
_rv = raft_apply(CLUSTER_RAFT(I), &_req, &_buf, 1, applyCbAssertResult); \
munit_assert_int(_rv, ==, 0);


/******************************************************************************
*
* Success scenarios
Expand Down Expand Up @@ -137,3 +150,74 @@ TEST(raft_apply, leadershipLost, setUp, tearDown, 0, NULL)
APPLY_WAIT;
return MUNIT_OK;
}

TEST(raft_apply, logAppendCommandsFail, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;

will_return(logAppendCommands, RAFT_NOMEM);
APPLY_ERROR(0, RAFT_NOMEM, "");
return MUNIT_OK;
}

TEST(raft_apply, requestRegEnqueueFail, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;

will_return(requestRegEnqueue, RAFT_NOMEM);
APPLY_ERROR(0, RAFT_NOMEM, "");
return MUNIT_OK;
}

TEST(raft_apply, replicationTriggerFail, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;

will_return(replicationTrigger, RAFT_NOMEM);
APPLY_ERROR(0, RAFT_NOMEM, "");
return MUNIT_OK;
}

static int fakeAppend(struct raft_io *io, struct raft_io_append *req,
const struct raft_entry entries[], unsigned n,
raft_io_append_cb cb)
{
(void)io;
(void)req;
(void)entries;
(void)n;
(void)cb;

return RAFT_NOMEM;
}

static int fakeAppendCb(struct raft_io *io, struct raft_io_append *req,
const struct raft_entry entries[], unsigned n,
raft_io_append_cb cb)
{
(void)io;
(void)entries;
(void)n;

cb(req, RAFT_NOMEM);
return 0;
}

TEST(raft_apply, appendLeaderFail, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;
typeof(fakeAppend)* append;

CLUSTER_RAFT(0)->prev_append_status = RAFT_NOMEM;
APPLY_ERROR(0, RAFT_NOMEM, "");
CLUSTER_RAFT(0)->prev_append_status = 0;
will_return(logAcquire, RAFT_NOMEM);
APPLY_ERROR(0, RAFT_NOMEM, "");
append = CLUSTER_RAFT(0)->io->append;
CLUSTER_RAFT(0)->io->append = fakeAppend;
APPLY_ERROR(0, RAFT_NOMEM, "io: ");
CLUSTER_RAFT(0)->io->append = fakeAppendCb;
APPLY_SUBMIT_ERROR(0, RAFT_NOMEM);
CLUSTER_RAFT(0)->io->append = append;
return MUNIT_OK;
}
40 changes: 40 additions & 0 deletions test/integration/test_barrier.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "../lib/cluster.h"
#include "../lib/runner.h"
#include "../lib/munit_mock.h"

/******************************************************************************
*
Expand Down Expand Up @@ -78,6 +79,18 @@ static bool barrierCbHasFired(struct raft_fixture *f, void *arg)
BARRIER_WAIT; \
} while (0)

#define BARRIER_ERROR(I, RV, ERRMSG) \
do { \
struct raft_barrier _req; \
struct result _result = {0, false}; \
int _rv; \
_req.data = &_result; \
_rv = raft_barrier(CLUSTER_RAFT(I), &_req, barrierCbAssertResult); \
munit_assert_int(_rv, ==, RV); \
munit_assert_string_equal(CLUSTER_ERRMSG(I), ERRMSG); \
} while (0)


/******************************************************************************
*
* Success scenarios
Expand All @@ -92,3 +105,30 @@ TEST(raft_barrier, cb, setUp, tearDown, 0, NULL)
BARRIER(0);
return MUNIT_OK;
}

TEST(raft_barrier, logAppendFail, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;

will_return(logAppend, RAFT_NOMEM);
BARRIER_ERROR(0, RAFT_NOMEM, "");
return MUNIT_OK;
}

TEST(raft_barrier, requestRegEnqueueFail, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;

will_return(requestRegEnqueue, RAFT_NOMEM);
BARRIER_ERROR(0, RAFT_NOMEM, "");
return MUNIT_OK;
}

TEST(raft_barrier, replicationTriggerFail, setUp, tearDown, 0, NULL)
{
struct fixture *f = data;

will_return(replicationTrigger, RAFT_NOMEM);
BARRIER_ERROR(0, RAFT_NOMEM, "");
return MUNIT_OK;
}
11 changes: 10 additions & 1 deletion test/integration/test_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ TEST(raft_heap, aligned_alloc, NULL, NULL, 0, NULL)
p = raft_aligned_alloc(1024, 2048);
munit_assert_ptr_not_null(p);
munit_assert_int((uintptr_t)p % 1024, ==, 0);
raft_free(p);
raft_aligned_free(1024, p);
return MUNIT_OK;
}

TEST(raft_heap, entry_malloc, NULL, NULL, 0, NULL)
{
void *p;
p = raft_entry_malloc(2048);
munit_assert_ptr_not_null(p);
raft_entry_free(p);
return MUNIT_OK;
}
Loading

0 comments on commit d9b999c

Please sign in to comment.