diff options
author | Vladimir Kobal <vlad@prokk.net> | 2022-09-26 10:24:36 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-26 10:24:36 +0200 |
commit | a1758307f9924a2c8a942aae258ff26a77329cae (patch) | |
tree | 15986ebba5144ac68f89bd7669a22b8b330e5cf1 /exporting | |
parent | 8ad75bbdfd6ec9a69206133c3f7e138236acb463 (diff) |
Update exporting unit tests (#13706)
Diffstat (limited to 'exporting')
-rw-r--r-- | exporting/tests/exporting_fixtures.c | 85 | ||||
-rw-r--r-- | exporting/tests/netdata_doubles.c | 17 | ||||
-rw-r--r-- | exporting/tests/test_exporting_engine.c | 171 |
3 files changed, 207 insertions, 66 deletions
diff --git a/exporting/tests/exporting_fixtures.c b/exporting/tests/exporting_fixtures.c index 906c7cf892..c9fc9458ca 100644 --- a/exporting/tests/exporting_fixtures.c +++ b/exporting/tests/exporting_fixtures.c @@ -33,31 +33,13 @@ int teardown_configured_engine(void **state) return 0; } -int setup_rrdhost() -{ - localhost = calloc(1, sizeof(RRDHOST)); - - localhost->rrd_update_every = 1; - - localhost->tags = string_strdupz("TAG1=VALUE1 TAG2=VALUE2"); - - localhost->rrdlabels = rrdlabels_create(); - rrdlabels_add(localhost->rrdlabels, "key1", "value1", RRDLABEL_SRC_CONFIG); - rrdlabels_add(localhost->rrdlabels, "key2", "value2", RRDLABEL_SRC_CONFIG); +static void rrddim_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrddim, void *st) { + RRDDIM *rd = rrddim; - localhost->rrdset_root = calloc(1, sizeof(RRDSET)); - RRDSET *st = localhost->rrdset_root; - st->rrdhost = localhost; - st->id = string_strdupz("chart_id"); - st->name = string_strdupz("chart_name"); - st->rrd_memory_mode |= RRD_MEMORY_MODE_SAVE; - st->update_every = 1; - - localhost->rrdset_root->dimensions = calloc(1, sizeof(RRDDIM)); - RRDDIM *rd = localhost->rrdset_root->dimensions; - rd->rrdset = st; rd->id = string_strdupz("dimension_id"); rd->name = string_strdupz("dimension_name"); + + rd->rrdset = (RRDSET *)st; rd->last_collected_value = 123000321; rd->last_collected_time.tv_sec = 15051; rd->collections_counter++; @@ -70,25 +52,74 @@ int setup_rrdhost() rd->tiers[0]->query_ops.is_finished = __mock_rrddim_query_is_finished; rd->tiers[0]->query_ops.next_metric = __mock_rrddim_query_next_metric; rd->tiers[0]->query_ops.finalize = __mock_rrddim_query_finalize; +} + +static void rrdset_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdset, void *constructor_data __maybe_unused) { + RRDHOST *host = localhost; + RRDSET *st = rrdset; + + // const char *chart_full_id = dictionary_acquired_item_name(item); + + st->id = string_strdupz("chart_id"); + st->name = string_strdupz("chart_name"); + + st->update_every = 1; + st->rrd_memory_mode = RRD_MEMORY_MODE_SAVE; + + st->rrdhost = host; + + st->rrddim_root_index = dictionary_create(DICT_OPTION_DONT_OVERWRITE_VALUE); + + dictionary_register_insert_callback(st->rrddim_root_index, rrddim_insert_callback, NULL); +} + +int setup_rrdhost() +{ + localhost = calloc(1, sizeof(RRDHOST)); + + localhost->rrd_update_every = 1; + + localhost->tags = string_strdupz("TAG1=VALUE1 TAG2=VALUE2"); + + localhost->rrdlabels = rrdlabels_create(); + rrdlabels_add(localhost->rrdlabels, "key1", "value1", RRDLABEL_SRC_CONFIG); + rrdlabels_add(localhost->rrdlabels, "key2", "value2", RRDLABEL_SRC_CONFIG); + + localhost->rrdset_root_index = dictionary_create(DICT_OPTION_DONT_OVERWRITE_VALUE); + dictionary_register_insert_callback(localhost->rrdset_root_index, rrdset_insert_callback, NULL); + RRDSET *st = dictionary_set_advanced(localhost->rrdset_root_index, "chart_id", -1, NULL, sizeof(RRDSET), NULL); + + st->rrddim_root_index = dictionary_create(DICT_OPTION_DONT_OVERWRITE_VALUE); + dictionary_register_insert_callback(st->rrddim_root_index, rrddim_insert_callback, NULL); + st->dimensions = dictionary_set_advanced(st->rrddim_root_index, "dimension_id", -1, NULL, sizeof(RRDDIM), st); return 0; } int teardown_rrdhost() { - RRDDIM *rd = localhost->rrdset_root->dimensions; - string_freez(rd->name); + RRDSET *st; + rrdset_foreach_read(st, localhost); + break; + rrdset_foreach_done(st); + + RRDDIM *rd; + rrddim_foreach_read(rd, st); + break; + rrddim_foreach_done(rd); + string_freez(rd->id); + string_freez(rd->name); free(rd->tiers[0]); - free(rd); - RRDSET *st = localhost->rrdset_root; + string_freez(st->id); string_freez(st->name); - free(st); + dictionary_destroy(st->rrddim_root_index); rrdlabels_destroy(localhost->rrdlabels); string_freez(localhost->tags); + dictionary_destroy(localhost->rrdset_root_index); free(localhost); return 0; diff --git a/exporting/tests/netdata_doubles.c b/exporting/tests/netdata_doubles.c index f83a474f9b..abc6f77bba 100644 --- a/exporting/tests/netdata_doubles.c +++ b/exporting/tests/netdata_doubles.c @@ -177,14 +177,6 @@ const char *rrd_memory_mode_name(RRD_MEMORY_MODE id) return RRD_MEMORY_MODE_NONE_NAME; } -int foreach_host_variable_callback(RRDHOST *host, int (*callback)(RRDVAR *rv, void *data), void *data) -{ - (void)host; - (void)callback; - (void)data; - return 0; -} - void rrdset_update_heterogeneous_flag(RRDSET *st) { (void)st; @@ -247,13 +239,18 @@ void rrdcalc_update_rrdlabels(RRDSET *st) (void)st; } -void rrdpush_sender_send_this_host_variable_now(RRDHOST *host, RRDVAR *rv) +void rrdpush_sender_send_this_host_variable_now(RRDHOST *host, const RRDVAR_ACQUIRED *rva) { (void)host; - (void)rv; + (void)rva; } void db_execute(const char *cmd) { (void)cmd; } + +DICTIONARY *rrdfamily_rrdvars_dict(const RRDFAMILY_ACQUIRED *rfa) { + (void)rfa; + return NULL; +} diff --git a/exporting/tests/test_exporting_engine.c b/exporting/tests/test_exporting_engine.c index 2eee049a81..cabd0a2f7d 100644 --- a/exporting/tests/test_exporting_engine.c +++ b/exporting/tests/test_exporting_engine.c @@ -257,7 +257,10 @@ static void test_rrdset_is_exportable(void **state) { struct engine *engine = *state; struct instance *instance = engine->instance_root; - RRDSET *st = localhost->rrdset_root; + RRDSET *st; + rrdset_foreach_read(st, localhost); + break; + rrdset_foreach_done(st); assert_ptr_equal(st->exporting_flags, NULL); @@ -271,7 +274,10 @@ static void test_false_rrdset_is_exportable(void **state) { struct engine *engine = *state; struct instance *instance = engine->instance_root; - RRDSET *st = localhost->rrdset_root; + RRDSET *st; + rrdset_foreach_read(st, localhost); + break; + rrdset_foreach_done(st); simple_pattern_free(instance->config.charts_pattern); instance->config.charts_pattern = simple_pattern_create("!*", NULL, SIMPLE_PATTERN_EXACT); @@ -288,7 +294,17 @@ static void test_exporting_calculate_value_from_stored_data(void **state) { struct engine *engine = *state; struct instance *instance = engine->instance_root; - RRDDIM *rd = localhost->rrdset_root->dimensions; + + RRDSET *st; + rrdset_foreach_read(st, localhost); + break; + rrdset_foreach_done(st); + + RRDDIM *rd; + rrddim_foreach_read(rd, st); + break; + rrddim_foreach_done(rd); + time_t timestamp; instance->after = 3; @@ -348,7 +364,11 @@ static void test_prepare_buffers(void **state) expect_value(__mock_start_host_formatting, host, localhost); will_return(__mock_start_host_formatting, 0); - RRDSET *st = localhost->rrdset_root; + RRDSET *st; + rrdset_foreach_read(st, localhost); + break; + rrdset_foreach_done(st); + expect_function_call(__wrap_rrdset_is_exportable); expect_value(__wrap_rrdset_is_exportable, instance, instance); expect_value(__wrap_rrdset_is_exportable, st, st); @@ -359,7 +379,10 @@ static void test_prepare_buffers(void **state) expect_value(__mock_start_chart_formatting, st, st); will_return(__mock_start_chart_formatting, 0); - RRDDIM *rd = localhost->rrdset_root->dimensions; + RRDDIM *rd; + rrddim_foreach_read(rd, st); + break; + rrddim_foreach_done(rd); expect_function_call(__mock_metric_formatting); expect_value(__mock_metric_formatting, instance, instance); expect_value(__mock_metric_formatting, rd, rd); @@ -412,7 +435,15 @@ static void test_format_dimension_collected_graphite_plaintext(void **state) { struct engine *engine = *state; - RRDDIM *rd = localhost->rrdset_root->dimensions; + RRDSET *st; + rrdset_foreach_read(st, localhost); + break; + rrdset_foreach_done(st); + + RRDDIM *rd; + rrddim_foreach_read(rd, st); + break; + rrddim_foreach_done(rd); assert_int_equal(format_dimension_collected_graphite_plaintext(engine->instance_root, rd), 0); assert_string_equal( buffer_tostring(engine->instance_root->buffer), @@ -426,7 +457,15 @@ static void test_format_dimension_stored_graphite_plaintext(void **state) expect_function_call(__wrap_exporting_calculate_value_from_stored_data); will_return(__wrap_exporting_calculate_value_from_stored_data, pack_storage_number(27, SN_DEFAULT_FLAGS)); - RRDDIM *rd = localhost->rrdset_root->dimensions; + RRDSET *st; + rrdset_foreach_read(st, localhost); + break; + rrdset_foreach_done(st); + + RRDDIM *rd; + rrddim_foreach_read(rd, st); + break; + rrddim_foreach_done(rd); assert_int_equal(format_dimension_stored_graphite_plaintext(engine->instance_root, rd), 0); assert_string_equal( buffer_tostring(engine->instance_root->buffer), @@ -437,7 +476,15 @@ static void test_format_dimension_collected_json_plaintext(void **state) { struct engine *engine = *state; - RRDDIM *rd = localhost->rrdset_root->dimensions; + RRDSET *st; + rrdset_foreach_read(st, localhost); + break; + rrdset_foreach_done(st); + + RRDDIM *rd; + rrddim_foreach_read(rd, st); + break; + rrddim_foreach_done(rd); assert_int_equal(format_dimension_collected_json_plaintext(engine->instance_root, rd), 0); assert_string_equal( buffer_tostring(engine->instance_root->buffer), @@ -454,7 +501,15 @@ static void test_format_dimension_stored_json_plaintext(void **state) expect_function_call(__wrap_exporting_calculate_value_from_stored_data); will_return(__wrap_exporting_calculate_value_from_stored_data, pack_storage_number(27, SN_DEFAULT_FLAGS)); - RRDDIM *rd = localhost->rrdset_root->dimensions; + RRDSET *st; + rrdset_foreach_read(st, localhost); + break; + rrdset_foreach_done(st); + + RRDDIM *rd; + rrddim_foreach_read(rd, st); + break; + rrddim_foreach_done(rd); assert_int_equal(format_dimension_stored_json_plaintext(engine->instance_root, rd), 0); assert_string_equal( buffer_tostring(engine->instance_root->buffer), @@ -468,7 +523,15 @@ static void test_format_dimension_collected_opentsdb_telnet(void **state) { struct engine *engine = *state; - RRDDIM *rd = localhost->rrdset_root->dimensions; + RRDSET *st; + rrdset_foreach_read(st, localhost); + break; + rrdset_foreach_done(st); + + RRDDIM *rd; + rrddim_foreach_read(rd, st); + break; + rrddim_foreach_done(rd); assert_int_equal(format_dimension_collected_opentsdb_telnet(engine->instance_root, rd), 0); assert_string_equal( buffer_tostring(engine->instance_root->buffer), @@ -482,7 +545,15 @@ static void test_format_dimension_stored_opentsdb_telnet(void **state) expect_function_call(__wrap_exporting_calculate_value_from_stored_data); will_return(__wrap_exporting_calculate_value_from_stored_data, pack_storage_number(27, SN_DEFAULT_FLAGS)); - RRDDIM *rd = localhost->rrdset_root->dimensions; + RRDSET *st; + rrdset_foreach_read(st, localhost); + break; + rrdset_foreach_done(st); + + RRDDIM *rd; + rrddim_foreach_read(rd, st); + break; + rrddim_foreach_done(rd); assert_int_equal(format_dimension_stored_opentsdb_telnet(engine->instance_root, rd), 0); assert_string_equal( buffer_tostring(engine->instance_root->buffer), @@ -493,7 +564,15 @@ static void test_format_dimension_collected_opentsdb_http(void **state) { struct engine *engine = *state; - RRDDIM *rd = localhost->rrdset_root->dimensions; + RRDSET *st; + rrdset_foreach_read(st, localhost); + break; + rrdset_foreach_done(st); + + RRDDIM *rd; + rrddim_foreach_read(rd, st); + break; + rrddim_foreach_done(rd); assert_int_equal(format_dimension_collected_opentsdb_http(engine->instance_root, rd), 0); assert_string_equal( buffer_tostring(engine->instance_root->buffer), @@ -510,7 +589,15 @@ static void test_format_dimension_stored_opentsdb_http(void **state) expect_function_call(__wrap_exporting_calculate_value_from_stored_data); will_return(__wrap_exporting_calculate_value_from_stored_data, pack_storage_number(27, SN_DEFAULT_FLAGS)); - RRDDIM *rd = localhost->rrdset_root->dimensions; + RRDSET *st; + rrdset_foreach_read(st, localhost); + break; + rrdset_foreach_done(st); + + RRDDIM *rd; + rrddim_foreach_read(rd, st); + break; + rrddim_foreach_done(rd); assert_int_equal(format_dimension_stored_opentsdb_http(engine->instance_root, rd), 0); assert_string_equal( buffer_tostring(engine->instance_root->buffer), @@ -986,21 +1073,26 @@ static void test_can_send_rrdset(void **state) { (void)*state; - assert_int_equal(can_send_rrdset(prometheus_exporter_instance, localhost->rrdset_root, NULL), 1); + RRDSET *st; + rrdset_foreach_read(st, localhost); + break; + rrdset_foreach_done(st); + + assert_int_equal(can_send_rrdset(prometheus_exporter_instance, st, NULL), 1); - rrdset_flag_set(localhost->rrdset_root, RRDSET_FLAG_EXPORTING_IGNORE); - assert_int_equal(can_send_rrdset(prometheus_exporter_instance, localhost->rrdset_root, NULL), 0); - rrdset_flag_clear(localhost->rrdset_root, RRDSET_FLAG_EXPORTING_IGNORE); + rrdset_flag_set(st, RRDSET_FLAG_EXPORTING_IGNORE); + assert_int_equal(can_send_rrdset(prometheus_exporter_instance, st, NULL), 0); + rrdset_flag_clear(st, RRDSET_FLAG_EXPORTING_IGNORE); // TODO: test with a denying simple pattern - rrdset_flag_set(localhost->rrdset_root, RRDSET_FLAG_OBSOLETE); - assert_int_equal(can_send_rrdset(prometheus_exporter_instance, localhost->rrdset_root, NULL), 0); - rrdset_flag_clear(localhost->rrdset_root, RRDSET_FLAG_OBSOLETE); + rrdset_flag_set(st, RRDSET_FLAG_OBSOLETE); + assert_int_equal(can_send_rrdset(prometheus_exporter_instance, st, NULL), 0); + rrdset_flag_clear(st, RRDSET_FLAG_OBSOLETE); - localhost->rrdset_root->rrd_memory_mode = RRD_MEMORY_MODE_NONE; + st->rrd_memory_mode = RRD_MEMORY_MODE_NONE; prometheus_exporter_instance->config.options |= EXPORTING_SOURCE_DATA_AVERAGE; - assert_int_equal(can_send_rrdset(prometheus_exporter_instance, localhost->rrdset_root, NULL), 0); + assert_int_equal(can_send_rrdset(prometheus_exporter_instance, st, NULL), 0); } static void test_prometheus_name_copy(void **state) @@ -1055,9 +1147,14 @@ static void rrd_stats_api_v1_charts_allmetrics_prometheus(void **state) BUFFER *buffer = buffer_create(0); + RRDSET *st; + rrdset_foreach_read(st, localhost); + break; + rrdset_foreach_done(st); + localhost->hostname = string_strdupz("test_hostname"); - localhost->rrdset_root->family = string_strdupz("test_family"); - localhost->rrdset_root->context = string_strdupz("test_context"); + st->family = string_strdupz("test_family"); + st->context = string_strdupz("test_context"); expect_function_call(__wrap_now_realtime_sec); will_return(__wrap_now_realtime_sec, 2); @@ -1104,8 +1201,8 @@ static void rrd_stats_api_v1_charts_allmetrics_prometheus(void **state) "netdata_info{instance=\"test_hostname\",application=\"\",version=\"\",key1=\"value1\",key2=\"value2\"} 1\n" "test_prefix_test_context{chart=\"chart_id\",family=\"test_family\",dimension=\"dimension_id\",instance=\"test_hostname\"} 690565856.0000000\n"); - free(localhost->rrdset_root->context); - free(localhost->rrdset_root->family); + free(st->context); + free(st->family); free(localhost->hostname); buffer_free(buffer); } @@ -1249,7 +1346,15 @@ static void test_format_dimension_prometheus_remote_write(void **state) simple_connector_data->connector_specific_data = (void *)connector_specific_data; connector_specific_data->write_request = (void *)0xff; - RRDDIM *rd = localhost->rrdset_root->dimensions; + RRDSET *st; + rrdset_foreach_read(st, localhost); + break; + rrdset_foreach_done(st); + + RRDDIM *rd; + rrddim_foreach_read(rd, st); + break; + rrddim_foreach_done(rd); expect_function_call(__wrap_exporting_calculate_value_from_stored_data); will_return(__wrap_exporting_calculate_value_from_stored_data, pack_storage_number(27, SN_DEFAULT_FLAGS)); @@ -1428,7 +1533,11 @@ static void test_aws_kinesis_connector_worker(void **state) expect_value(__wrap_rrdhost_is_exportable, host, localhost); will_return(__wrap_rrdhost_is_exportable, 1); - RRDSET *st = localhost->rrdset_root; + RRDSET *st; + rrdset_foreach_read(st, localhost); + break; + rrdset_foreach_done(st); + expect_function_call(__wrap_rrdset_is_exportable); expect_value(__wrap_rrdset_is_exportable, instance, instance); expect_value(__wrap_rrdset_is_exportable, st, st); @@ -1563,7 +1672,11 @@ static void test_pubsub_connector_worker(void **state) expect_value(__wrap_rrdhost_is_exportable, host, localhost); will_return(__wrap_rrdhost_is_exportable, 1); - RRDSET *st = localhost->rrdset_root; + RRDSET *st; + rrdset_foreach_read(st, localhost); + break; + rrdset_foreach_done(st); + expect_function_call(__wrap_rrdset_is_exportable); expect_value(__wrap_rrdset_is_exportable, instance, instance); expect_value(__wrap_rrdset_is_exportable, st, st); |