|
| 1 | +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | + |
| 3 | +/* Fluent Bit |
| 4 | + * ========== |
| 5 | + * Copyright (C) 2015-2026 The Fluent Bit Authors |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include <fluent-bit/flb_info.h> |
| 21 | +#include <fluent-bit/flb_mem.h> |
| 22 | +#include <fluent-bit/flb_plugin.h> |
| 23 | +#include <fluent-bit/flb_plugin_alias.h> |
| 24 | +#include <fluent-bit/flb_network.h> |
| 25 | +#include <fluent-bit/flb_config.h> |
| 26 | +#include <fluent-bit/flb_output.h> |
| 27 | + |
| 28 | +#include <string.h> |
| 29 | + |
| 30 | +#include "flb_tests_internal.h" |
| 31 | + |
| 32 | +void plugin_alias_lookup_test() |
| 33 | +{ |
| 34 | + const char *alias_target; |
| 35 | + |
| 36 | + alias_target = flb_plugin_alias_get(FLB_PLUGIN_OUTPUT, "elasticsearch", |
| 37 | + strlen("elasticsearch")); |
| 38 | + if (!TEST_CHECK(alias_target != NULL)) { |
| 39 | + TEST_MSG("output plugin alias was not resolved"); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + if (!TEST_CHECK(strcmp(alias_target, "es") == 0)) { |
| 44 | + TEST_MSG("unexpected alias target: %s", alias_target); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +void plugin_alias_rewrite_test() |
| 49 | +{ |
| 50 | + char *rewritten_name; |
| 51 | + |
| 52 | + rewritten_name = flb_plugin_alias_rewrite(FLB_PLUGIN_OUTPUT, |
| 53 | + "elasticsearch://127.0.0.1:9200"); |
| 54 | + if (!TEST_CHECK(rewritten_name != FLB_PLUGIN_ALIAS_ERR)) { |
| 55 | + TEST_MSG("error while rewriting output plugin alias"); |
| 56 | + return; |
| 57 | + } |
| 58 | + if (!TEST_CHECK(rewritten_name != NULL)) { |
| 59 | + TEST_MSG("could not rewrite output plugin alias"); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + if (!TEST_CHECK(strcmp(rewritten_name, "es://127.0.0.1:9200") == 0)) { |
| 64 | + TEST_MSG("unexpected rewritten output plugin name: %s", rewritten_name); |
| 65 | + } |
| 66 | + |
| 67 | + flb_free(rewritten_name); |
| 68 | +} |
| 69 | + |
| 70 | +void network_alias_address_parse_test() |
| 71 | +{ |
| 72 | + int ret; |
| 73 | + struct flb_net_host host; |
| 74 | + |
| 75 | + ret = flb_net_host_set("es", &host, "elasticsearch://127.0.0.1:9200/path"); |
| 76 | + if (!TEST_CHECK(ret == 0)) { |
| 77 | + TEST_MSG("could not parse alias output address"); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + if (!TEST_CHECK(strcmp(host.name, "127.0.0.1") == 0)) { |
| 82 | + TEST_MSG("unexpected host name parsed from alias output address: %s", |
| 83 | + host.name); |
| 84 | + } |
| 85 | + |
| 86 | + if (!TEST_CHECK(host.port == 9200)) { |
| 87 | + TEST_MSG("unexpected host port parsed from alias output address: %d", |
| 88 | + host.port); |
| 89 | + } |
| 90 | + |
| 91 | + flb_sds_destroy(host.name); |
| 92 | + flb_sds_destroy(host.listen); |
| 93 | + flb_uri_destroy(host.uri); |
| 94 | + flb_sds_destroy(host.address); |
| 95 | +} |
| 96 | + |
| 97 | +void output_alias_instantiation_test() |
| 98 | +{ |
| 99 | + struct flb_config *config; |
| 100 | + struct flb_output_instance *instance; |
| 101 | + |
| 102 | + config = flb_config_init(); |
| 103 | + if (!TEST_CHECK(config != NULL)) { |
| 104 | + TEST_MSG("could not initialize config context"); |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + instance = flb_output_new(config, "elasticsearch", NULL, FLB_TRUE); |
| 109 | + if (!TEST_CHECK(instance != NULL)) { |
| 110 | + TEST_MSG("could not instantiate aliased output plugin"); |
| 111 | + flb_config_exit(config); |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + if (!TEST_CHECK(strcmp(instance->p->name, "es") == 0)) { |
| 116 | + TEST_MSG("unexpected output plugin instantiated for alias: %s", |
| 117 | + instance->p->name); |
| 118 | + } |
| 119 | + |
| 120 | + flb_output_instance_destroy(instance); |
| 121 | + flb_config_exit(config); |
| 122 | +} |
| 123 | + |
| 124 | +TEST_LIST = { |
| 125 | + { "plugin_alias_lookup_test", plugin_alias_lookup_test }, |
| 126 | + { "plugin_alias_rewrite_test", plugin_alias_rewrite_test }, |
| 127 | + { "network_alias_address_parse_test", network_alias_address_parse_test }, |
| 128 | + { "output_alias_instantiation_test", output_alias_instantiation_test }, |
| 129 | + { 0 } |
| 130 | +}; |
0 commit comments