exporting_fixtures.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "test_exporting_engine.h"
  3. int setup_configured_engine(void **state)
  4. {
  5. struct engine *engine = __mock_read_exporting_config();
  6. engine->instance_root->data_is_ready = 1;
  7. *state = engine;
  8. return 0;
  9. }
  10. int teardown_configured_engine(void **state)
  11. {
  12. struct engine *engine = *state;
  13. struct instance *instance = engine->instance_root;
  14. free((void *)instance->config.destination);
  15. free((void *)instance->config.username);
  16. free((void *)instance->config.password);
  17. free((void *)instance->config.name);
  18. free((void *)instance->config.prefix);
  19. free((void *)instance->config.hostname);
  20. simple_pattern_free(instance->config.charts_pattern);
  21. simple_pattern_free(instance->config.hosts_pattern);
  22. free(instance);
  23. free((void *)engine->config.hostname);
  24. free(engine);
  25. return 0;
  26. }
  27. static void rrddim_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrddim, void *st) {
  28. RRDDIM *rd = rrddim;
  29. rd->id = string_strdupz("dimension_id");
  30. rd->name = string_strdupz("dimension_name");
  31. rd->rrdset = (RRDSET *)st;
  32. rd->last_collected_value = 123000321;
  33. rd->last_collected_time.tv_sec = 15051;
  34. rd->collections_counter++;
  35. rd->next = NULL;
  36. rd->tiers[0] = calloc(1, sizeof(struct rrddim_tier));
  37. rd->tiers[0]->query_ops.oldest_time = __mock_rrddim_query_oldest_time;
  38. rd->tiers[0]->query_ops.latest_time = __mock_rrddim_query_latest_time;
  39. rd->tiers[0]->query_ops.init = __mock_rrddim_query_init;
  40. rd->tiers[0]->query_ops.is_finished = __mock_rrddim_query_is_finished;
  41. rd->tiers[0]->query_ops.next_metric = __mock_rrddim_query_next_metric;
  42. rd->tiers[0]->query_ops.finalize = __mock_rrddim_query_finalize;
  43. }
  44. static void rrdset_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdset, void *constructor_data __maybe_unused) {
  45. RRDHOST *host = localhost;
  46. RRDSET *st = rrdset;
  47. // const char *chart_full_id = dictionary_acquired_item_name(item);
  48. st->id = string_strdupz("chart_id");
  49. st->name = string_strdupz("chart_name");
  50. st->update_every = 1;
  51. st->rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
  52. st->rrdhost = host;
  53. st->rrddim_root_index = dictionary_create(DICT_OPTION_DONT_OVERWRITE_VALUE);
  54. dictionary_register_insert_callback(st->rrddim_root_index, rrddim_insert_callback, NULL);
  55. }
  56. int setup_rrdhost()
  57. {
  58. localhost = calloc(1, sizeof(RRDHOST));
  59. localhost->rrd_update_every = 1;
  60. localhost->tags = string_strdupz("TAG1=VALUE1 TAG2=VALUE2");
  61. localhost->rrdlabels = rrdlabels_create();
  62. rrdlabels_add(localhost->rrdlabels, "key1", "value1", RRDLABEL_SRC_CONFIG);
  63. rrdlabels_add(localhost->rrdlabels, "key2", "value2", RRDLABEL_SRC_CONFIG);
  64. localhost->rrdset_root_index = dictionary_create(DICT_OPTION_DONT_OVERWRITE_VALUE);
  65. dictionary_register_insert_callback(localhost->rrdset_root_index, rrdset_insert_callback, NULL);
  66. RRDSET *st = dictionary_set_advanced(localhost->rrdset_root_index, "chart_id", -1, NULL, sizeof(RRDSET), NULL);
  67. st->rrddim_root_index = dictionary_create(DICT_OPTION_DONT_OVERWRITE_VALUE);
  68. dictionary_register_insert_callback(st->rrddim_root_index, rrddim_insert_callback, NULL);
  69. st->dimensions = dictionary_set_advanced(st->rrddim_root_index, "dimension_id", -1, NULL, sizeof(RRDDIM), st);
  70. return 0;
  71. }
  72. int teardown_rrdhost()
  73. {
  74. RRDSET *st;
  75. rrdset_foreach_read(st, localhost);
  76. break;
  77. rrdset_foreach_done(st);
  78. RRDDIM *rd;
  79. rrddim_foreach_read(rd, st);
  80. break;
  81. rrddim_foreach_done(rd);
  82. string_freez(rd->id);
  83. string_freez(rd->name);
  84. free(rd->tiers[0]);
  85. string_freez(st->id);
  86. string_freez(st->name);
  87. dictionary_destroy(st->rrddim_root_index);
  88. rrdlabels_destroy(localhost->rrdlabels);
  89. string_freez(localhost->tags);
  90. dictionary_destroy(localhost->rrdset_root_index);
  91. free(localhost);
  92. return 0;
  93. }
  94. int setup_initialized_engine(void **state)
  95. {
  96. setup_configured_engine(state);
  97. struct engine *engine = *state;
  98. init_connectors_in_tests(engine);
  99. setup_rrdhost();
  100. return 0;
  101. }
  102. int teardown_initialized_engine(void **state)
  103. {
  104. struct engine *engine = *state;
  105. teardown_rrdhost();
  106. buffer_free(engine->instance_root->labels_buffer);
  107. buffer_free(engine->instance_root->buffer);
  108. teardown_configured_engine(state);
  109. return 0;
  110. }
  111. int setup_prometheus(void **state)
  112. {
  113. (void)state;
  114. prometheus_exporter_instance = calloc(1, sizeof(struct instance));
  115. setup_rrdhost();
  116. prometheus_exporter_instance->config.update_every = 10;
  117. prometheus_exporter_instance->config.options |=
  118. EXPORTING_OPTION_SEND_NAMES | EXPORTING_OPTION_SEND_CONFIGURED_LABELS | EXPORTING_OPTION_SEND_AUTOMATIC_LABELS;
  119. prometheus_exporter_instance->config.charts_pattern = simple_pattern_create("*", NULL, SIMPLE_PATTERN_EXACT);
  120. prometheus_exporter_instance->config.hosts_pattern = simple_pattern_create("*", NULL, SIMPLE_PATTERN_EXACT);
  121. prometheus_exporter_instance->config.initialized = 1;
  122. return 0;
  123. }
  124. int teardown_prometheus(void **state)
  125. {
  126. (void)state;
  127. teardown_rrdhost();
  128. simple_pattern_free(prometheus_exporter_instance->config.charts_pattern);
  129. simple_pattern_free(prometheus_exporter_instance->config.hosts_pattern);
  130. free(prometheus_exporter_instance);
  131. return 0;
  132. }