summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/lib/chunkio/tests/context.c
blob: 7b7d9ea4865bf560210dbca0cf94d6ec21eee0e8 (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Chunk I/O
 *  =========
 *  Copyright 2018 Eduardo Silva <eduardo@monkey.io>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#include <chunkio/chunkio.h>
#include <chunkio/cio_log.h>

#include "cio_tests_internal.h"

int log_check;

/* Logging callback, once called it just turn on the log_check flag */
static int log_cb(struct cio_ctx *ctx, int level, const char *file, int line,
                  char *str)
{
    (void) ctx;
    (void) file;
    (void) line;
    (void) str;

    log_check = 1;
    return 0;
}

/* Basic tests on context creation */
static void test_context()
{
    int flags;
    struct cio_ctx *ctx;
    struct cio_options cio_opts;

    flags = CIO_CHECKSUM;

    cio_options_init(&cio_opts);
    cio_opts.flags = flags;

    /* Invalid path */
    cio_opts.root_path = "";
    cio_opts.log_level = CIO_LOG_INFO;

    ctx = cio_create(&cio_opts);
    TEST_CHECK(ctx == NULL);

    /* Invalid debug level -1 */
    cio_opts.root_path = "/tmp/";
    cio_opts.log_level = -1;

    ctx = cio_create(&cio_opts);
    TEST_CHECK(ctx == NULL);

    /* Invalid debug level 6 */
    cio_opts.log_level = 6;

    ctx = cio_create(&cio_opts);
    TEST_CHECK(ctx == NULL);

    /* Valid context without callback */
    log_check = 0;
    cio_opts.log_level = CIO_LOG_INFO;

    ctx = cio_create(&cio_opts);
    TEST_CHECK(ctx != NULL);
    cio_log_info(ctx, "test");
    TEST_CHECK(log_check == 0);
    cio_destroy(ctx);

    /* Valid with context callback */
    log_check = 0;
    cio_opts.log_cb = log_cb;

    ctx = cio_create(&cio_opts);
    TEST_CHECK(ctx != NULL);
    cio_log_info(ctx, "test");
    TEST_CHECK(log_check == 1);
    cio_destroy(ctx);
}

static void test_log_level()
{
    struct cio_ctx *ctx;
    struct cio_options cio_opts;

    cio_options_init(&cio_opts);

    /* Logging with unset callback at creation, but set later */
    log_check = 0;
    cio_opts.root_path = "/tmp/";
    cio_opts.log_level = CIO_LOG_INFO;

    ctx = cio_create(&cio_opts);
    TEST_CHECK(ctx != NULL);
    cio_log_info(ctx, "test");
    TEST_CHECK(log_check == 0);

    /* Loggin callback enable */
    cio_set_log_callback(ctx, log_cb);
    cio_log_info(ctx, "test");
    TEST_CHECK(log_check == 1);

    /* Test: CIO_ERROR */
    cio_set_log_level(ctx, CIO_LOG_ERROR);
    log_check = 0;
    cio_log_warn(ctx, "test");
    TEST_CHECK(log_check == 0);
    cio_log_error(ctx, "test");
    TEST_CHECK(log_check == 1);

    /* Test: CIO_WARN */
    cio_set_log_level(ctx, CIO_LOG_WARN);
    log_check = 0;
    cio_log_info(ctx, "test");
    TEST_CHECK(log_check == 0);
    cio_log_warn(ctx, "test");
    TEST_CHECK(log_check == 1);

    /* Test: CIO_INFO */
    cio_set_log_level(ctx, CIO_LOG_INFO);
    log_check = 0;
    cio_log_debug(ctx, "test");
    TEST_CHECK(log_check == 0);
    cio_log_info(ctx, "test");
    TEST_CHECK(log_check == 1);

    /* Test: CIO_DEBUG */
    cio_set_log_level(ctx, CIO_LOG_DEBUG);
    log_check = 0;
    cio_log_trace(ctx, "test");
    TEST_CHECK(log_check == 0);
    cio_log_debug(ctx, "test");
    TEST_CHECK(log_check == 1);

    /* Test: CIO_TRACE */
    cio_set_log_level(ctx, CIO_LOG_TRACE);
    log_check = 0;
    cio_log_trace(ctx, "test");
    TEST_CHECK(log_check == 1);

    /* destroy context */
    cio_destroy(ctx);
}

static void test_open_flags()
{
    struct cio_ctx *ctx;
    struct cio_options cio_opts;

    cio_options_init(&cio_opts);
    TEST_CHECK(cio_opts.flags & CIO_OPEN_RW);

    /* reset flags */
    cio_opts.flags = 0;

    /* check that after context creation a default has been set */
    ctx = cio_create(&cio_opts);
    TEST_CHECK(ctx != NULL);
    TEST_CHECK(cio_opts.flags & CIO_OPEN_RW);
    
    /* destroy context */
    cio_destroy(ctx);
}

TEST_LIST = {
    {"context",     test_context},
    {"log_level",   test_log_level},
    {"open_flags",  test_open_flags},
    { 0 }
};