File: test_uv_bootstrap.c

package info (click to toggle)
raft 0.22.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,504 kB
  • sloc: ansic: 37,539; makefile: 264; sh: 77; python: 22
file content (98 lines) | stat: -rw-r--r-- 3,048 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "../lib/runner.h"
#include "../lib/uv.h"

/******************************************************************************
 *
 * Fixture with a libuv-based raft_io instance and an empty configuration.
 *
 *****************************************************************************/

struct fixture
{
    FIXTURE_UV_DEPS;
    FIXTURE_UV;
    struct raft_configuration conf;
};

/******************************************************************************
 *
 * Helper macros
 *
 *****************************************************************************/

/* Add a server to the fixture's configuration. */
#define CONFIGURATION_ADD(ID, ADDRESS)                                   \
    {                                                                    \
        int rv_;                                                         \
        rv_ = raft_configuration_add(&f->conf, ID, ADDRESS, RAFT_VOTER); \
        munit_assert_int(rv_, ==, 0);                                    \
    }

/* Invoke f->io->bootstrap() and assert that no error occurs. */
#define BOOTSTRAP                                \
    {                                            \
        int rv_;                                 \
        rv_ = f->io.bootstrap(&f->io, &f->conf); \
        munit_assert_int(rv_, ==, 0);            \
    }

/******************************************************************************
 *
 * Set up and tear down.
 *
 *****************************************************************************/

static void *setUp(const MunitParameter params[], void *user_data)
{
    struct fixture *f = munit_malloc(sizeof *f);
    SETUP_UV_DEPS;
    SETUP_UV;
    raft_configuration_init(&f->conf);
    return f;
}

static void tearDown(void *data)
{
    struct fixture *f = data;
    raft_configuration_close(&f->conf);
    TEAR_DOWN_UV;
    TEAR_DOWN_UV_DEPS;
    free(f);
}

/******************************************************************************
 *
 * raft_io->bootstrap()
 *
 *****************************************************************************/

SUITE(bootstrap)

/* Invoke f->io->bootstrap() and assert that it returns the given error code and
 * message. */
#define BOOTSTRAP_ERROR(RV, ERRMSG)                      \
    {                                                    \
        int rv_;                                         \
        rv_ = f->io.bootstrap(&f->io, &f->conf);         \
        munit_assert_int(rv_, ==, RV);                   \
        munit_assert_string_equal(f->io.errmsg, ERRMSG); \
    }

/* Bootstrap a pristine server. */
TEST(bootstrap, pristine, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    CONFIGURATION_ADD(1, "1");
    BOOTSTRAP;
    return MUNIT_OK;
}

/* The data directory already has metadata files with a non-zero term. */
TEST(bootstrap, termIsNonZero, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    CONFIGURATION_ADD(1, "1");
    BOOTSTRAP;
    BOOTSTRAP_ERROR(RAFT_CANTBOOTSTRAP, "metadata contains term 1");
    return MUNIT_OK;
}