summaryrefslogtreecommitdiffstats
path: root/fluent-bit/tests/include/aws_client_mock.c
blob: 84ee6a1eb5c131661c6dab55e503da0d16c50ddf (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#include "aws_client_mock.h"

#include <fluent-bit/flb_aws_util.h>
#include <fluent-bit/flb_http_client.h>

/* Vtable mocked methods */
static struct flb_http_client *flb_aws_client_mock_vtable_request(
    struct flb_aws_client *aws_client, int method, const char *uri, const char *body,
    size_t body_len, struct flb_aws_header *dynamic_headers, size_t dynamic_headers_len);

/* Protected structs */

/* flb_aws_client_mock pointer returned by mock_generator */
static struct flb_aws_client_mock *flb_aws_client_mock_instance = NULL;

/* Generator that returns clients with the test vtable */
static struct flb_aws_client_generator mock_generator = {
    .create = flb_aws_client_create_mock,
};

/* Test/mock flb_aws_client vtable */
static struct flb_aws_client_vtable mock_client_vtable = {
    .request = flb_aws_client_mock_vtable_request,
};

/*
 * Configure generator
 * Note: Automatically creates mock and wires to generator
 *       Destroys any existing mock in generator
 */
void flb_aws_client_mock_configure_generator(
    struct flb_aws_client_mock_request_chain *request_chain)
{
    flb_aws_client_mock_destroy_generator();
    flb_aws_client_mock_instance = flb_aws_client_mock_create(request_chain);
}

/*
 * Clean up generator's memory
 * Cleanup should be called on exiting generator
 */
void flb_aws_client_mock_destroy_generator()
{
    if (flb_aws_client_mock_instance != NULL) {
        flb_aws_client_mock_destroy(flb_aws_client_mock_instance);
    }
}

/* Create Mock of flb_aws_client */
struct flb_aws_client_mock *flb_aws_client_mock_create(
    struct flb_aws_client_mock_request_chain *request_chain)
{
    struct flb_aws_client_mock *mock = flb_calloc(1, sizeof(struct flb_aws_client_mock));

    /* Create a surrogate aws_client and copy to mock client */
    struct flb_aws_client *surrogate_aws_client = flb_aws_client_generator()->create();
    mock->super = *surrogate_aws_client;
    mock->surrogate = surrogate_aws_client;
    memset(mock->surrogate, 0, sizeof(struct flb_aws_client));

    /* Switch vtable to mock vtable */
    mock->super.client_vtable = &mock_client_vtable;
    mock->request_chain = request_chain;
    mock->next_request_index = 0;
    return mock;
}

/* Destroy flb_aws_client_mock */
void flb_aws_client_mock_destroy(struct flb_aws_client_mock *mock)
{
    /* Remove from generator registry if stored */
    if (flb_aws_client_mock_instance == mock) {
        flb_aws_client_mock_instance = NULL;
    }

    /* Resurrect surrogate, and destroy flb_aws_client */
    *mock->surrogate = mock->super;
    flb_aws_client_destroy(mock->surrogate);

    /* Destroy mock flb_aws_client */
    flb_free(mock);
}

/* Return a Mocked flb_aws_client, ready for injection */
struct flb_aws_client *flb_aws_client_mock_context(struct flb_aws_client_mock *mock)
{
    return (struct flb_aws_client *)mock;
}

/* Get the number of unused requests */
int flb_aws_client_mock_count_unused_requests(struct flb_aws_client_mock *mock)
{
    return mock->request_chain->length - mock->next_request_index;
}

/* Set flb_aws_client_mock_instance used in mock generator */
void flb_aws_client_mock_set_generator_instance(struct flb_aws_client_mock *mock)
{
    flb_aws_client_mock_instance = mock;
}

/* Set flb_aws_client_mock_instance used in mock generator */
struct flb_aws_client_mock *flb_aws_client_mock_get_generator_instance(
    struct flb_aws_client_mock *mock)
{
    return flb_aws_client_mock_instance = mock;
}

/* Get generator used in mock */
struct flb_aws_client_generator *flb_aws_client_get_mock_generator()
{
    return &mock_generator;
}

/* Get the number of unused requests */
int flb_aws_client_mock_generator_count_unused_requests()
{
    TEST_ASSERT(flb_aws_client_mock_instance != 0);
    return flb_aws_client_mock_count_unused_requests(flb_aws_client_mock_instance);
}

/* Return the mock instance */
struct flb_aws_client *flb_aws_client_create_mock()
{
    TEST_CHECK(flb_aws_client_mock_instance != NULL);
    TEST_MSG(
        "[aws_mock_client] Must initialize flb_aws_client_mock_instance before calling "
        "flb_aws_client_create_mock()");
    TEST_MSG(
        "[aws_mock_client] This ouccurs when the generator is called, before tests are "
        "initialized.");

    return flb_aws_client_mock_context(flb_aws_client_mock_instance);
}

/* Mock request used by flb_aws_client mock */
static struct flb_http_client *flb_aws_client_mock_vtable_request(
    struct flb_aws_client *aws_client, int method, const char *uri, const char *body,
    size_t body_len, struct flb_aws_header *dynamic_headers, size_t dynamic_headers_len)
{
    int h;
    int i;
    int ret;

    /* Get access to mock */
    struct flb_aws_client_mock *mock = (struct flb_aws_client_mock *)aws_client;

    /* Check that a response is left in the chain */
    ret = TEST_CHECK(mock->next_request_index < mock->request_chain->length);
    if (!ret) {
        TEST_MSG(
            "[flb_aws_client_mock] %d mock responses provided. Attempting to call %d "
            "times. Aborting.",
            (int)mock->request_chain->length, (int)mock->next_request_index + 1);
        return NULL;
    }
    struct flb_aws_client_mock_response *response =
        &(mock->request_chain->responses[mock->next_request_index]);
    struct flb_http_client *c = NULL;

    /* create an http client so that we can set the response */
    c = flb_calloc(1, sizeof(struct flb_http_client));
    if (!c) {
        flb_errno();
        return NULL;
    }
    mk_list_init(&c->headers);

    /* Response configuration */
    for (i = 0; i < response->length; ++i) {
        struct flb_aws_client_mock_response_config *response_config =
            &(response->config_parameters[i]);
        void *val1 = response_config->config_value;
        void *val2 = response_config->config_value_2;

        /* Expectations */
        if (response_config->config_parameter == FLB_AWS_CLIENT_MOCK_EXPECT_HEADER) {
            int header_found = FLB_FALSE;
            /* Search for header in request */
            for (h = 0; h < dynamic_headers_len; ++h) {
                ret = strncmp(dynamic_headers[h].key, (char *)val1,
                              dynamic_headers[h].key_len);
                if (ret == 0) {
                    /* Check header value */
                    ret = strncmp(dynamic_headers[h].val, (char *)val2,
                                  dynamic_headers[h].val_len + 1);
                    TEST_CHECK(ret == 0);
                    TEST_MSG("[aws_mock_client] Expected Header: (%s: %s)", (char *)val1,
                             (char *)val2);
                    TEST_MSG("[aws_mock_client] Received Header: (%s: %s)", (char *)val1,
                             dynamic_headers[h].val);

                    header_found = FLB_TRUE;
                }
            }
            TEST_CHECK(header_found);
            TEST_MSG("[aws_mock_client] Expected Header: (%s: %s)", (char *)val1,
                     (char *)val2);
            TEST_MSG("[aws_mock_client] Header not received");
        }
        else if (response_config->config_parameter == FLB_AWS_CLIENT_MOCK_EXPECT_METHOD) {
            char *flb_http_methods[] = {
                "FLB_HTTP_GET",  "FLB_HTTP_POST",    "FLB_HTTP_PUT",
                "FLB_HTTP_HEAD", "FLB_HTTP_CONNECT", "FLB_HTTP_PATCH",
            };

            /*
             * Check method is what is expected
             * Typecast config value from void * -> int
             */
            TEST_CHECK(method == (int)(uintptr_t)val1);
            TEST_MSG("[aws_mock_client] Expected HTTP Method: %s",
                     flb_http_methods[(int)(uintptr_t)val1]);
            TEST_MSG("[aws_mock_client] Received HTTP Method: %s",
                     flb_http_methods[method]);
        }
        else if (response_config->config_parameter ==
                 FLB_AWS_CLIENT_MOCK_EXPECT_HEADER_COUNT) {
            TEST_CHECK(dynamic_headers_len == (int)(uintptr_t)val1);
            TEST_MSG("[aws_mock_client] Expected %d Headers", (int)(uintptr_t)val1);
            TEST_MSG("[aws_mock_client] Received %d Headers",
                     (int)(uintptr_t)dynamic_headers_len);
        }
        else if (response_config->config_parameter == FLB_AWS_CLIENT_MOCK_EXPECT_URI) {
            ret = strncmp(uri, (char *)val1, strlen((char *)val1) + 1);
            TEST_CHECK(ret == 0);
            TEST_MSG("[aws_mock_client] Expected URI: %s", (char *)val1);
            TEST_MSG("[aws_mock_client] Received URI: %s", uri);
        }

        /* Replace response client */
        else if (response_config->config_parameter ==
                 FLB_AWS_CLIENT_MOCK_CONFIG_REPLACE) {
            flb_http_client_destroy(c);
            c = (struct flb_http_client *)val1;
        }

        /*
        * Response setters
        * Set client fields using XMacro definitions
        */
#define EXPAND_CLIENT_RESPONSE_PARAMETER(lower, UPPER, type)                           \
        else if (response_config->config_parameter == FLB_AWS_CLIENT_MOCK_SET_##UPPER) \
        {                                                                              \
            c->resp.lower = CONVERT_##type((char *)val1);                              \
        }
#include "aws_client_mock_client_resp.def"
#undef EXPAND_CLIENT_RESPONSE_PARAMETER
    }

    /* Increment request */
    ++mock->next_request_index;

    return c;
};