test_exporting_engine.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "test_exporting_engine.h"
  3. #include "libnetdata/required_dummies.h"
  4. RRDHOST *localhost;
  5. netdata_rwlock_t rrd_rwlock;
  6. // global variables needed by read_exporting_config()
  7. struct config netdata_config;
  8. char *netdata_configured_user_config_dir = ".";
  9. char *netdata_configured_stock_config_dir = ".";
  10. char *netdata_configured_hostname = "test_global_host";
  11. char log_line[MAX_LOG_LINE + 1];
  12. void init_connectors_in_tests(struct engine *engine)
  13. {
  14. expect_function_call(__wrap_now_realtime_sec);
  15. will_return(__wrap_now_realtime_sec, 2);
  16. expect_function_call(__wrap_uv_thread_create);
  17. expect_value(__wrap_uv_thread_create, thread, &engine->instance_root->thread);
  18. expect_value(__wrap_uv_thread_create, worker, simple_connector_worker);
  19. expect_value(__wrap_uv_thread_create, arg, engine->instance_root);
  20. expect_function_call(__wrap_uv_thread_set_name_np);
  21. assert_int_equal(__real_init_connectors(engine), 0);
  22. assert_int_equal(engine->now, 2);
  23. assert_int_equal(engine->instance_root->after, 2);
  24. }
  25. static void test_exporting_engine(void **state)
  26. {
  27. struct engine *engine = *state;
  28. expect_function_call(__wrap_read_exporting_config);
  29. will_return(__wrap_read_exporting_config, engine);
  30. expect_function_call(__wrap_init_connectors);
  31. expect_memory(__wrap_init_connectors, engine, engine, sizeof(struct engine));
  32. will_return(__wrap_init_connectors, 0);
  33. expect_function_call(__wrap_create_main_rusage_chart);
  34. expect_not_value(__wrap_create_main_rusage_chart, st_rusage, NULL);
  35. expect_not_value(__wrap_create_main_rusage_chart, rd_user, NULL);
  36. expect_not_value(__wrap_create_main_rusage_chart, rd_system, NULL);
  37. expect_function_call(__wrap_now_realtime_sec);
  38. will_return(__wrap_now_realtime_sec, 2);
  39. expect_function_call(__wrap_mark_scheduled_instances);
  40. expect_memory(__wrap_mark_scheduled_instances, engine, engine, sizeof(struct engine));
  41. will_return(__wrap_mark_scheduled_instances, 1);
  42. expect_function_call(__wrap_prepare_buffers);
  43. expect_memory(__wrap_prepare_buffers, engine, engine, sizeof(struct engine));
  44. will_return(__wrap_prepare_buffers, 0);
  45. expect_function_call(__wrap_send_main_rusage);
  46. expect_value(__wrap_send_main_rusage, st_rusage, NULL);
  47. expect_value(__wrap_send_main_rusage, rd_user, NULL);
  48. expect_value(__wrap_send_main_rusage, rd_system, NULL);
  49. void *ptr = malloc(sizeof(struct netdata_static_thread));
  50. assert_ptr_equal(exporting_main(ptr), NULL);
  51. assert_int_equal(engine->now, 2);
  52. free(ptr);
  53. }
  54. static void test_read_exporting_config(void **state)
  55. {
  56. struct engine *engine = __mock_read_exporting_config(); // TODO: use real read_exporting_config() function
  57. *state = engine;
  58. assert_ptr_not_equal(engine, NULL);
  59. assert_string_equal(engine->config.hostname, "test_engine_host");
  60. assert_int_equal(engine->config.update_every, 3);
  61. assert_int_equal(engine->instance_num, 0);
  62. struct instance *instance = engine->instance_root;
  63. assert_ptr_not_equal(instance, NULL);
  64. assert_ptr_equal(instance->next, NULL);
  65. assert_ptr_equal(instance->engine, engine);
  66. assert_int_equal(instance->config.type, EXPORTING_CONNECTOR_TYPE_GRAPHITE);
  67. assert_string_equal(instance->config.destination, "localhost");
  68. assert_string_equal(instance->config.prefix, "netdata");
  69. assert_int_equal(instance->config.update_every, 1);
  70. assert_int_equal(instance->config.buffer_on_failures, 10);
  71. assert_int_equal(instance->config.timeoutms, 10000);
  72. assert_true(simple_pattern_matches(instance->config.charts_pattern, "any_chart"));
  73. assert_true(simple_pattern_matches(instance->config.hosts_pattern, "anyt_host"));
  74. assert_int_equal(instance->config.options, EXPORTING_SOURCE_DATA_AS_COLLECTED | EXPORTING_OPTION_SEND_NAMES);
  75. teardown_configured_engine(state);
  76. }
  77. static void test_init_connectors(void **state)
  78. {
  79. struct engine *engine = *state;
  80. init_connectors_in_tests(engine);
  81. assert_int_equal(engine->instance_num, 1);
  82. struct instance *instance = engine->instance_root;
  83. assert_ptr_equal(instance->next, NULL);
  84. assert_int_equal(instance->index, 0);
  85. struct simple_connector_config *connector_specific_config = instance->config.connector_specific_config;
  86. assert_int_equal(connector_specific_config->default_port, 2003);
  87. assert_ptr_equal(instance->worker, simple_connector_worker);
  88. assert_ptr_equal(instance->start_batch_formatting, NULL);
  89. assert_ptr_equal(instance->start_host_formatting, format_host_labels_graphite_plaintext);
  90. assert_ptr_equal(instance->start_chart_formatting, NULL);
  91. assert_ptr_equal(instance->metric_formatting, format_dimension_collected_graphite_plaintext);
  92. assert_ptr_equal(instance->end_chart_formatting, NULL);
  93. assert_ptr_equal(instance->end_host_formatting, flush_host_labels);
  94. BUFFER *buffer = instance->buffer;
  95. assert_ptr_not_equal(buffer, NULL);
  96. buffer_sprintf(buffer, "%s", "graphite test");
  97. assert_string_equal(buffer_tostring(buffer), "graphite test");
  98. }
  99. static void test_init_graphite_instance(void **state)
  100. {
  101. struct engine *engine = *state;
  102. struct instance *instance = engine->instance_root;
  103. instance->config.options = EXPORTING_SOURCE_DATA_AS_COLLECTED | EXPORTING_OPTION_SEND_NAMES;
  104. assert_int_equal(init_graphite_instance(instance), 0);
  105. assert_int_equal(
  106. ((struct simple_connector_config *)(instance->config.connector_specific_config))->default_port, 2003);
  107. freez(instance->config.connector_specific_config);
  108. assert_ptr_equal(instance->metric_formatting, format_dimension_collected_graphite_plaintext);
  109. assert_ptr_not_equal(instance->buffer, NULL);
  110. buffer_free(instance->buffer);
  111. instance->config.options = EXPORTING_SOURCE_DATA_AVERAGE | EXPORTING_OPTION_SEND_NAMES;
  112. assert_int_equal(init_graphite_instance(instance), 0);
  113. assert_ptr_equal(instance->metric_formatting, format_dimension_stored_graphite_plaintext);
  114. }
  115. static void test_init_json_instance(void **state)
  116. {
  117. struct engine *engine = *state;
  118. struct instance *instance = engine->instance_root;
  119. instance->config.options = EXPORTING_SOURCE_DATA_AS_COLLECTED | EXPORTING_OPTION_SEND_NAMES;
  120. assert_int_equal(init_json_instance(instance), 0);
  121. assert_int_equal(
  122. ((struct simple_connector_config *)(instance->config.connector_specific_config))->default_port, 5448);
  123. freez(instance->config.connector_specific_config);
  124. assert_ptr_equal(instance->metric_formatting, format_dimension_collected_json_plaintext);
  125. assert_ptr_not_equal(instance->buffer, NULL);
  126. buffer_free(instance->buffer);
  127. instance->config.options = EXPORTING_SOURCE_DATA_AVERAGE | EXPORTING_OPTION_SEND_NAMES;
  128. assert_int_equal(init_json_instance(instance), 0);
  129. assert_ptr_equal(instance->metric_formatting, format_dimension_stored_json_plaintext);
  130. }
  131. static void test_init_opentsdb_telnet_instance(void **state)
  132. {
  133. struct engine *engine = *state;
  134. struct instance *instance = engine->instance_root;
  135. instance->config.options = EXPORTING_SOURCE_DATA_AS_COLLECTED | EXPORTING_OPTION_SEND_NAMES;
  136. assert_int_equal(init_opentsdb_telnet_instance(instance), 0);
  137. assert_int_equal(
  138. ((struct simple_connector_config *)(instance->config.connector_specific_config))->default_port, 4242);
  139. freez(instance->config.connector_specific_config);
  140. assert_ptr_equal(instance->metric_formatting, format_dimension_collected_opentsdb_telnet);
  141. assert_ptr_not_equal(instance->buffer, NULL);
  142. buffer_free(instance->buffer);
  143. instance->config.options = EXPORTING_SOURCE_DATA_AVERAGE | EXPORTING_OPTION_SEND_NAMES;
  144. assert_int_equal(init_opentsdb_telnet_instance(instance), 0);
  145. assert_ptr_equal(instance->metric_formatting, format_dimension_stored_opentsdb_telnet);
  146. }
  147. static void test_init_opentsdb_http_instance(void **state)
  148. {
  149. struct engine *engine = *state;
  150. struct instance *instance = engine->instance_root;
  151. instance->config.options = EXPORTING_SOURCE_DATA_AS_COLLECTED | EXPORTING_OPTION_SEND_NAMES;
  152. assert_int_equal(init_opentsdb_http_instance(instance), 0);
  153. assert_int_equal(
  154. ((struct simple_connector_config *)(instance->config.connector_specific_config))->default_port, 4242);
  155. freez(instance->config.connector_specific_config);
  156. assert_ptr_equal(instance->metric_formatting, format_dimension_collected_opentsdb_http);
  157. assert_ptr_not_equal(instance->buffer, NULL);
  158. buffer_free(instance->buffer);
  159. instance->config.options = EXPORTING_SOURCE_DATA_AVERAGE | EXPORTING_OPTION_SEND_NAMES;
  160. assert_int_equal(init_opentsdb_http_instance(instance), 0);
  161. assert_ptr_equal(instance->metric_formatting, format_dimension_stored_opentsdb_http);
  162. }
  163. static void test_mark_scheduled_instances(void **state)
  164. {
  165. struct engine *engine = *state;
  166. assert_int_equal(__real_mark_scheduled_instances(engine), 1);
  167. struct instance *instance = engine->instance_root;
  168. assert_int_equal(instance->scheduled, 1);
  169. assert_int_equal(instance->before, 2);
  170. }
  171. static void test_rrdhost_is_exportable(void **state)
  172. {
  173. struct engine *engine = *state;
  174. struct instance *instance = engine->instance_root;
  175. expect_function_call(__wrap_info_int);
  176. assert_ptr_equal(localhost->exporting_flags, NULL);
  177. assert_int_equal(__real_rrdhost_is_exportable(instance, localhost), 1);
  178. assert_string_equal(log_line, "enabled exporting of host 'localhost' for instance 'instance_name'");
  179. assert_ptr_not_equal(localhost->exporting_flags, NULL);
  180. assert_int_equal(localhost->exporting_flags[0], RRDHOST_FLAG_EXPORTING_SEND);
  181. }
  182. static void test_false_rrdhost_is_exportable(void **state)
  183. {
  184. struct engine *engine = *state;
  185. struct instance *instance = engine->instance_root;
  186. simple_pattern_free(instance->config.hosts_pattern);
  187. instance->config.hosts_pattern = simple_pattern_create("!*", NULL, SIMPLE_PATTERN_EXACT);
  188. expect_function_call(__wrap_info_int);
  189. assert_ptr_equal(localhost->exporting_flags, NULL);
  190. assert_int_equal(__real_rrdhost_is_exportable(instance, localhost), 0);
  191. assert_string_equal(log_line, "disabled exporting of host 'localhost' for instance 'instance_name'");
  192. assert_ptr_not_equal(localhost->exporting_flags, NULL);
  193. assert_int_equal(localhost->exporting_flags[0], RRDHOST_FLAG_EXPORTING_DONT_SEND);
  194. }
  195. static void test_rrdset_is_exportable(void **state)
  196. {
  197. struct engine *engine = *state;
  198. struct instance *instance = engine->instance_root;
  199. RRDSET *st = localhost->rrdset_root;
  200. assert_ptr_equal(st->exporting_flags, NULL);
  201. assert_int_equal(__real_rrdset_is_exportable(instance, st), 1);
  202. assert_ptr_not_equal(st->exporting_flags, NULL);
  203. assert_int_equal(st->exporting_flags[0], RRDSET_FLAG_EXPORTING_SEND);
  204. }
  205. static void test_false_rrdset_is_exportable(void **state)
  206. {
  207. struct engine *engine = *state;
  208. struct instance *instance = engine->instance_root;
  209. RRDSET *st = localhost->rrdset_root;
  210. simple_pattern_free(instance->config.charts_pattern);
  211. instance->config.charts_pattern = simple_pattern_create("!*", NULL, SIMPLE_PATTERN_EXACT);
  212. assert_ptr_equal(st->exporting_flags, NULL);
  213. assert_int_equal(__real_rrdset_is_exportable(instance, st), 0);
  214. assert_ptr_not_equal(st->exporting_flags, NULL);
  215. assert_int_equal(st->exporting_flags[0], RRDSET_FLAG_EXPORTING_IGNORE);
  216. }
  217. static void test_exporting_calculate_value_from_stored_data(void **state)
  218. {
  219. struct engine *engine = *state;
  220. struct instance *instance = engine->instance_root;
  221. RRDDIM *rd = localhost->rrdset_root->dimensions;
  222. time_t timestamp;
  223. instance->after = 3;
  224. instance->before = 10;
  225. expect_function_call(__mock_rrddim_query_oldest_time);
  226. will_return(__mock_rrddim_query_oldest_time, 1);
  227. expect_function_call(__mock_rrddim_query_latest_time);
  228. will_return(__mock_rrddim_query_latest_time, 2);
  229. expect_function_call(__mock_rrddim_query_init);
  230. expect_value(__mock_rrddim_query_init, start_time, 1);
  231. expect_value(__mock_rrddim_query_init, end_time, 2);
  232. expect_function_call(__mock_rrddim_query_is_finished);
  233. will_return(__mock_rrddim_query_is_finished, 0);
  234. expect_function_call(__mock_rrddim_query_next_metric);
  235. will_return(__mock_rrddim_query_next_metric, pack_storage_number(27, SN_DEFAULT_FLAGS));
  236. expect_function_call(__mock_rrddim_query_is_finished);
  237. will_return(__mock_rrddim_query_is_finished, 0);
  238. expect_function_call(__mock_rrddim_query_next_metric);
  239. will_return(__mock_rrddim_query_next_metric, pack_storage_number(45, SN_DEFAULT_FLAGS));
  240. expect_function_call(__mock_rrddim_query_is_finished);
  241. will_return(__mock_rrddim_query_is_finished, 1);
  242. expect_function_call(__mock_rrddim_query_finalize);
  243. assert_int_equal(__real_exporting_calculate_value_from_stored_data(instance, rd, &timestamp), 36);
  244. }
  245. static void test_prepare_buffers(void **state)
  246. {
  247. struct engine *engine = *state;
  248. struct instance *instance = engine->instance_root;
  249. instance->start_batch_formatting = __mock_start_batch_formatting;
  250. instance->start_host_formatting = __mock_start_host_formatting;
  251. instance->start_chart_formatting = __mock_start_chart_formatting;
  252. instance->metric_formatting = __mock_metric_formatting;
  253. instance->end_chart_formatting = __mock_end_chart_formatting;
  254. instance->end_host_formatting = __mock_end_host_formatting;
  255. instance->end_batch_formatting = __mock_end_batch_formatting;
  256. __real_mark_scheduled_instances(engine);
  257. expect_function_call(__mock_start_batch_formatting);
  258. expect_value(__mock_start_batch_formatting, instance, instance);
  259. will_return(__mock_start_batch_formatting, 0);
  260. expect_function_call(__wrap_rrdhost_is_exportable);
  261. expect_value(__wrap_rrdhost_is_exportable, instance, instance);
  262. expect_value(__wrap_rrdhost_is_exportable, host, localhost);
  263. will_return(__wrap_rrdhost_is_exportable, 1);
  264. expect_function_call(__mock_start_host_formatting);
  265. expect_value(__mock_start_host_formatting, instance, instance);
  266. expect_value(__mock_start_host_formatting, host, localhost);
  267. will_return(__mock_start_host_formatting, 0);
  268. RRDSET *st = localhost->rrdset_root;
  269. expect_function_call(__wrap_rrdset_is_exportable);
  270. expect_value(__wrap_rrdset_is_exportable, instance, instance);
  271. expect_value(__wrap_rrdset_is_exportable, st, st);
  272. will_return(__wrap_rrdset_is_exportable, 1);
  273. expect_function_call(__mock_start_chart_formatting);
  274. expect_value(__mock_start_chart_formatting, instance, instance);
  275. expect_value(__mock_start_chart_formatting, st, st);
  276. will_return(__mock_start_chart_formatting, 0);
  277. RRDDIM *rd = localhost->rrdset_root->dimensions;
  278. expect_function_call(__mock_metric_formatting);
  279. expect_value(__mock_metric_formatting, instance, instance);
  280. expect_value(__mock_metric_formatting, rd, rd);
  281. will_return(__mock_metric_formatting, 0);
  282. expect_function_call(__mock_end_chart_formatting);
  283. expect_value(__mock_end_chart_formatting, instance, instance);
  284. expect_value(__mock_end_chart_formatting, st, st);
  285. will_return(__mock_end_chart_formatting, 0);
  286. expect_function_call(__mock_end_host_formatting);
  287. expect_value(__mock_end_host_formatting, instance, instance);
  288. expect_value(__mock_end_host_formatting, host, localhost);
  289. will_return(__mock_end_host_formatting, 0);
  290. expect_function_call(__mock_end_batch_formatting);
  291. expect_value(__mock_end_batch_formatting, instance, instance);
  292. will_return(__mock_end_batch_formatting, 0);
  293. assert_int_equal(__real_prepare_buffers(engine), 0);
  294. assert_int_equal(instance->stats.buffered_metrics, 1);
  295. // check with NULL functions
  296. instance->start_batch_formatting = NULL;
  297. instance->start_host_formatting = NULL;
  298. instance->start_chart_formatting = NULL;
  299. instance->metric_formatting = NULL;
  300. instance->end_chart_formatting = NULL;
  301. instance->end_host_formatting = NULL;
  302. instance->end_batch_formatting = NULL;
  303. assert_int_equal(__real_prepare_buffers(engine), 0);
  304. assert_int_equal(instance->scheduled, 0);
  305. assert_int_equal(instance->after, 2);
  306. }
  307. static void test_exporting_name_copy(void **state)
  308. {
  309. (void)state;
  310. char *source_name = "test.name-with/special#characters_";
  311. char destination_name[RRD_ID_LENGTH_MAX + 1];
  312. assert_int_equal(exporting_name_copy(destination_name, source_name, RRD_ID_LENGTH_MAX), 34);
  313. assert_string_equal(destination_name, "test.name_with_special_characters_");
  314. }
  315. static void test_format_dimension_collected_graphite_plaintext(void **state)
  316. {
  317. struct engine *engine = *state;
  318. RRDDIM *rd = localhost->rrdset_root->dimensions;
  319. assert_int_equal(format_dimension_collected_graphite_plaintext(engine->instance_root, rd), 0);
  320. assert_string_equal(
  321. buffer_tostring(engine->instance_root->buffer),
  322. "netdata.test-host.chart_name.dimension_name;TAG1=VALUE1 TAG2=VALUE2 123000321 15051\n");
  323. }
  324. static void test_format_dimension_stored_graphite_plaintext(void **state)
  325. {
  326. struct engine *engine = *state;
  327. expect_function_call(__wrap_exporting_calculate_value_from_stored_data);
  328. will_return(__wrap_exporting_calculate_value_from_stored_data, pack_storage_number(27, SN_DEFAULT_FLAGS));
  329. RRDDIM *rd = localhost->rrdset_root->dimensions;
  330. assert_int_equal(format_dimension_stored_graphite_plaintext(engine->instance_root, rd), 0);
  331. assert_string_equal(
  332. buffer_tostring(engine->instance_root->buffer),
  333. "netdata.test-host.chart_name.dimension_name;TAG1=VALUE1 TAG2=VALUE2 690565856.0000000 15052\n");
  334. }
  335. static void test_format_dimension_collected_json_plaintext(void **state)
  336. {
  337. struct engine *engine = *state;
  338. RRDDIM *rd = localhost->rrdset_root->dimensions;
  339. assert_int_equal(format_dimension_collected_json_plaintext(engine->instance_root, rd), 0);
  340. assert_string_equal(
  341. buffer_tostring(engine->instance_root->buffer),
  342. "{\"prefix\":\"netdata\",\"hostname\":\"test-host\",\"host_tags\":\"TAG1=VALUE1 TAG2=VALUE2\","
  343. "\"chart_id\":\"chart_id\",\"chart_name\":\"chart_name\",\"chart_family\":\"(null)\","
  344. "\"chart_context\":\"(null)\",\"chart_type\":\"(null)\",\"units\":\"(null)\",\"id\":\"dimension_id\","
  345. "\"name\":\"dimension_name\",\"value\":123000321,\"timestamp\":15051}\n");
  346. }
  347. static void test_format_dimension_stored_json_plaintext(void **state)
  348. {
  349. struct engine *engine = *state;
  350. expect_function_call(__wrap_exporting_calculate_value_from_stored_data);
  351. will_return(__wrap_exporting_calculate_value_from_stored_data, pack_storage_number(27, SN_DEFAULT_FLAGS));
  352. RRDDIM *rd = localhost->rrdset_root->dimensions;
  353. assert_int_equal(format_dimension_stored_json_plaintext(engine->instance_root, rd), 0);
  354. assert_string_equal(
  355. buffer_tostring(engine->instance_root->buffer),
  356. "{\"prefix\":\"netdata\",\"hostname\":\"test-host\",\"host_tags\":\"TAG1=VALUE1 TAG2=VALUE2\","
  357. "\"chart_id\":\"chart_id\",\"chart_name\":\"chart_name\",\"chart_family\":\"(null)\"," \
  358. "\"chart_context\": \"(null)\",\"chart_type\":\"(null)\",\"units\": \"(null)\",\"id\":\"dimension_id\","
  359. "\"name\":\"dimension_name\",\"value\":690565856.0000000,\"timestamp\": 15052}\n");
  360. }
  361. static void test_format_dimension_collected_opentsdb_telnet(void **state)
  362. {
  363. struct engine *engine = *state;
  364. RRDDIM *rd = localhost->rrdset_root->dimensions;
  365. assert_int_equal(format_dimension_collected_opentsdb_telnet(engine->instance_root, rd), 0);
  366. assert_string_equal(
  367. buffer_tostring(engine->instance_root->buffer),
  368. "put netdata.chart_name.dimension_name 15051 123000321 host=test-host TAG1=VALUE1 TAG2=VALUE2\n");
  369. }
  370. static void test_format_dimension_stored_opentsdb_telnet(void **state)
  371. {
  372. struct engine *engine = *state;
  373. expect_function_call(__wrap_exporting_calculate_value_from_stored_data);
  374. will_return(__wrap_exporting_calculate_value_from_stored_data, pack_storage_number(27, SN_DEFAULT_FLAGS));
  375. RRDDIM *rd = localhost->rrdset_root->dimensions;
  376. assert_int_equal(format_dimension_stored_opentsdb_telnet(engine->instance_root, rd), 0);
  377. assert_string_equal(
  378. buffer_tostring(engine->instance_root->buffer),
  379. "put netdata.chart_name.dimension_name 15052 690565856.0000000 host=test-host TAG1=VALUE1 TAG2=VALUE2\n");
  380. }
  381. static void test_format_dimension_collected_opentsdb_http(void **state)
  382. {
  383. struct engine *engine = *state;
  384. RRDDIM *rd = localhost->rrdset_root->dimensions;
  385. assert_int_equal(format_dimension_collected_opentsdb_http(engine->instance_root, rd), 0);
  386. assert_string_equal(
  387. buffer_tostring(engine->instance_root->buffer),
  388. "{\"metric\":\"netdata.chart_name.dimension_name\","
  389. "\"timestamp\":15051,"
  390. "\"value\":123000321,"
  391. "\"tags\":{\"host\":\"test-host TAG1=VALUE1 TAG2=VALUE2\"}}");
  392. }
  393. static void test_format_dimension_stored_opentsdb_http(void **state)
  394. {
  395. struct engine *engine = *state;
  396. expect_function_call(__wrap_exporting_calculate_value_from_stored_data);
  397. will_return(__wrap_exporting_calculate_value_from_stored_data, pack_storage_number(27, SN_DEFAULT_FLAGS));
  398. RRDDIM *rd = localhost->rrdset_root->dimensions;
  399. assert_int_equal(format_dimension_stored_opentsdb_http(engine->instance_root, rd), 0);
  400. assert_string_equal(
  401. buffer_tostring(engine->instance_root->buffer),
  402. "{\"metric\":\"netdata.chart_name.dimension_name\","
  403. "\"timestamp\":15052,"
  404. "\"value\":690565856.0000000,"
  405. "\"tags\":{\"host\":\"test-host TAG1=VALUE1 TAG2=VALUE2\"}}");
  406. }
  407. static void test_exporting_discard_response(void **state)
  408. {
  409. struct engine *engine = *state;
  410. BUFFER *response = buffer_create(0);
  411. buffer_sprintf(response, "Test response");
  412. assert_int_equal(exporting_discard_response(response, engine->instance_root), 0);
  413. assert_int_equal(buffer_strlen(response), 0);
  414. buffer_free(response);
  415. }
  416. static void test_simple_connector_receive_response(void **state)
  417. {
  418. struct engine *engine = *state;
  419. struct instance *instance = engine->instance_root;
  420. struct stats *stats = &instance->stats;
  421. int sock = 1;
  422. expect_function_call(__wrap_recv);
  423. expect_value(__wrap_recv, sockfd, 1);
  424. expect_not_value(__wrap_recv, buf, 0);
  425. expect_value(__wrap_recv, len, 4096);
  426. expect_value(__wrap_recv, flags, MSG_DONTWAIT);
  427. simple_connector_receive_response(&sock, instance);
  428. assert_int_equal(stats->received_bytes, 9);
  429. assert_int_equal(stats->receptions, 1);
  430. assert_int_equal(sock, 1);
  431. }
  432. static void test_simple_connector_send_buffer(void **state)
  433. {
  434. struct engine *engine = *state;
  435. struct instance *instance = engine->instance_root;
  436. struct stats *stats = &instance->stats;
  437. int sock = 1;
  438. int failures = 3;
  439. size_t buffered_metrics = 1;
  440. BUFFER *header = buffer_create(0);
  441. BUFFER *buffer = buffer_create(0);
  442. buffer_strcat(header, "test header\n");
  443. buffer_strcat(buffer, "test buffer\n");
  444. expect_function_call(__wrap_send);
  445. expect_value(__wrap_send, sockfd, 1);
  446. expect_value(__wrap_send, buf, buffer_tostring(header));
  447. expect_string(__wrap_send, buf, "test header\n");
  448. expect_value(__wrap_send, len, 12);
  449. expect_value(__wrap_send, flags, MSG_NOSIGNAL);
  450. expect_function_call(__wrap_send);
  451. expect_value(__wrap_send, sockfd, 1);
  452. expect_value(__wrap_send, buf, buffer_tostring(buffer));
  453. expect_string(__wrap_send, buf, "test buffer\n");
  454. expect_value(__wrap_send, len, 12);
  455. expect_value(__wrap_send, flags, MSG_NOSIGNAL);
  456. simple_connector_send_buffer(&sock, &failures, instance, header, buffer, buffered_metrics);
  457. assert_int_equal(failures, 0);
  458. assert_int_equal(stats->transmission_successes, 1);
  459. assert_int_equal(stats->sent_bytes, 12);
  460. assert_int_equal(stats->sent_metrics, 1);
  461. assert_int_equal(stats->transmission_failures, 0);
  462. assert_int_equal(buffer_strlen(buffer), 0);
  463. assert_int_equal(sock, 1);
  464. }
  465. static void test_simple_connector_worker(void **state)
  466. {
  467. struct engine *engine = *state;
  468. struct instance *instance = engine->instance_root;
  469. struct stats *stats = &instance->stats;
  470. __real_mark_scheduled_instances(engine);
  471. struct simple_connector_data *simple_connector_data = callocz(1, sizeof(struct simple_connector_data));
  472. instance->connector_specific_data = simple_connector_data;
  473. simple_connector_data->last_buffer = callocz(1, sizeof(struct simple_connector_buffer));
  474. simple_connector_data->first_buffer = simple_connector_data->last_buffer;
  475. simple_connector_data->header = buffer_create(0);
  476. simple_connector_data->buffer = buffer_create(0);
  477. simple_connector_data->last_buffer->header = buffer_create(0);
  478. simple_connector_data->last_buffer->buffer = buffer_create(0);
  479. strcpy(simple_connector_data->connected_to, "localhost");
  480. buffer_sprintf(simple_connector_data->last_buffer->header, "test header");
  481. buffer_sprintf(simple_connector_data->last_buffer->buffer, "test buffer");
  482. expect_function_call(__wrap_connect_to_one_of);
  483. expect_string(__wrap_connect_to_one_of, destination, "localhost");
  484. expect_value(__wrap_connect_to_one_of, default_port, 2003);
  485. expect_not_value(__wrap_connect_to_one_of, reconnects_counter, 0);
  486. expect_string(__wrap_connect_to_one_of, connected_to, "localhost");
  487. expect_value(__wrap_connect_to_one_of, connected_to_size, CONNECTED_TO_MAX);
  488. will_return(__wrap_connect_to_one_of, 2);
  489. expect_function_call(__wrap_send);
  490. expect_value(__wrap_send, sockfd, 2);
  491. expect_not_value(__wrap_send, buf, buffer_tostring(simple_connector_data->last_buffer->buffer));
  492. expect_string(__wrap_send, buf, "test header");
  493. expect_value(__wrap_send, len, 11);
  494. expect_value(__wrap_send, flags, MSG_NOSIGNAL);
  495. expect_function_call(__wrap_send);
  496. expect_value(__wrap_send, sockfd, 2);
  497. expect_value(__wrap_send, buf, buffer_tostring(simple_connector_data->last_buffer->buffer));
  498. expect_string(__wrap_send, buf, "test buffer");
  499. expect_value(__wrap_send, len, 11);
  500. expect_value(__wrap_send, flags, MSG_NOSIGNAL);
  501. expect_function_call(__wrap_send_internal_metrics);
  502. expect_value(__wrap_send_internal_metrics, instance, instance);
  503. will_return(__wrap_send_internal_metrics, 0);
  504. simple_connector_worker(instance);
  505. assert_int_equal(stats->buffered_metrics, 0);
  506. assert_int_equal(stats->buffered_bytes, 0);
  507. assert_int_equal(stats->received_bytes, 0);
  508. assert_int_equal(stats->sent_bytes, 0);
  509. assert_int_equal(stats->sent_metrics, 0);
  510. assert_int_equal(stats->lost_metrics, 0);
  511. assert_int_equal(stats->receptions, 0);
  512. assert_int_equal(stats->transmission_successes, 0);
  513. assert_int_equal(stats->transmission_failures, 0);
  514. assert_int_equal(stats->data_lost_events, 0);
  515. assert_int_equal(stats->lost_bytes, 0);
  516. assert_int_equal(stats->reconnects, 0);
  517. }
  518. static void test_sanitize_json_string(void **state)
  519. {
  520. (void)state;
  521. char *src = "check \t\\\" string";
  522. char dst[19 + 1];
  523. sanitize_json_string(dst, src, 19);
  524. assert_string_equal(dst, "check _\\\\\\\" string");
  525. }
  526. static void test_sanitize_graphite_label_value(void **state)
  527. {
  528. (void)state;
  529. char *src = "check ;~ string";
  530. char dst[15 + 1];
  531. sanitize_graphite_label_value(dst, src, 15);
  532. assert_string_equal(dst, "check____string");
  533. }
  534. static void test_sanitize_opentsdb_label_value(void **state)
  535. {
  536. (void)state;
  537. char *src = "check \t\\\" #&$? -_./ string";
  538. char dst[26 + 1];
  539. sanitize_opentsdb_label_value(dst, src, 26);
  540. assert_string_equal(dst, "check__________-_./_string");
  541. }
  542. static void test_format_host_labels_json_plaintext(void **state)
  543. {
  544. struct engine *engine = *state;
  545. struct instance *instance = engine->instance_root;
  546. instance->config.options |= EXPORTING_OPTION_SEND_CONFIGURED_LABELS;
  547. instance->config.options |= EXPORTING_OPTION_SEND_AUTOMATIC_LABELS;
  548. assert_int_equal(format_host_labels_json_plaintext(instance, localhost), 0);
  549. assert_string_equal(buffer_tostring(instance->labels), "\"labels\":{\"key1\":\"value1\",\"key2\":\"value2\"},");
  550. }
  551. static void test_format_host_labels_graphite_plaintext(void **state)
  552. {
  553. struct engine *engine = *state;
  554. struct instance *instance = engine->instance_root;
  555. instance->config.options |= EXPORTING_OPTION_SEND_CONFIGURED_LABELS;
  556. instance->config.options |= EXPORTING_OPTION_SEND_AUTOMATIC_LABELS;
  557. assert_int_equal(format_host_labels_graphite_plaintext(instance, localhost), 0);
  558. assert_string_equal(buffer_tostring(instance->labels), ";key1=value1;key2=value2");
  559. }
  560. static void test_format_host_labels_opentsdb_telnet(void **state)
  561. {
  562. struct engine *engine = *state;
  563. struct instance *instance = engine->instance_root;
  564. instance->config.options |= EXPORTING_OPTION_SEND_CONFIGURED_LABELS;
  565. instance->config.options |= EXPORTING_OPTION_SEND_AUTOMATIC_LABELS;
  566. assert_int_equal(format_host_labels_opentsdb_telnet(instance, localhost), 0);
  567. assert_string_equal(buffer_tostring(instance->labels), " key1=value1 key2=value2");
  568. }
  569. static void test_format_host_labels_opentsdb_http(void **state)
  570. {
  571. struct engine *engine = *state;
  572. struct instance *instance = engine->instance_root;
  573. instance->config.options |= EXPORTING_OPTION_SEND_CONFIGURED_LABELS;
  574. instance->config.options |= EXPORTING_OPTION_SEND_AUTOMATIC_LABELS;
  575. assert_int_equal(format_host_labels_opentsdb_http(instance, localhost), 0);
  576. assert_string_equal(buffer_tostring(instance->labels), ",\"key1\":\"value1\",\"key2\":\"value2\"");
  577. }
  578. static void test_flush_host_labels(void **state)
  579. {
  580. struct engine *engine = *state;
  581. struct instance *instance = engine->instance_root;
  582. instance->labels = buffer_create(12);
  583. buffer_strcat(instance->labels, "check string");
  584. assert_int_equal(buffer_strlen(instance->labels), 12);
  585. assert_int_equal(flush_host_labels(instance, localhost), 0);
  586. assert_int_equal(buffer_strlen(instance->labels), 0);
  587. }
  588. static void test_create_main_rusage_chart(void **state)
  589. {
  590. UNUSED(state);
  591. RRDSET *st_rusage = calloc(1, sizeof(RRDSET));
  592. RRDDIM *rd_user = NULL;
  593. RRDDIM *rd_system = NULL;
  594. expect_function_call(rrdset_create_custom);
  595. expect_value(rrdset_create_custom, host, localhost);
  596. expect_string(rrdset_create_custom, type, "netdata");
  597. expect_string(rrdset_create_custom, id, "exporting_main_thread_cpu");
  598. expect_value(rrdset_create_custom, name, NULL);
  599. expect_string(rrdset_create_custom, family, "exporting");
  600. expect_string(rrdset_create_custom, context, "exporting_cpu_usage");
  601. expect_string(rrdset_create_custom, units, "milliseconds/s");
  602. expect_string(rrdset_create_custom, plugin, "exporting");
  603. expect_value(rrdset_create_custom, module, NULL);
  604. expect_value(rrdset_create_custom, priority, 130600);
  605. expect_value(rrdset_create_custom, update_every, localhost->rrd_update_every);
  606. expect_value(rrdset_create_custom, chart_type, RRDSET_TYPE_STACKED);
  607. will_return(rrdset_create_custom, st_rusage);
  608. expect_function_calls(rrddim_add_custom, 2);
  609. expect_value_count(rrddim_add_custom, st, st_rusage, 2);
  610. expect_value_count(rrddim_add_custom, name, NULL, 2);
  611. expect_value_count(rrddim_add_custom, multiplier, 1, 2);
  612. expect_value_count(rrddim_add_custom, divisor, 1000, 2);
  613. expect_value_count(rrddim_add_custom, algorithm, RRD_ALGORITHM_INCREMENTAL, 2);
  614. __real_create_main_rusage_chart(&st_rusage, &rd_user, &rd_system);
  615. free(st_rusage);
  616. }
  617. static void test_send_main_rusage(void **state)
  618. {
  619. UNUSED(state);
  620. RRDSET *st_rusage = calloc(1, sizeof(RRDSET));
  621. st_rusage->counter_done = 1;
  622. expect_function_call(rrdset_next_usec);
  623. expect_value(rrdset_next_usec, st, st_rusage);
  624. expect_function_calls(rrddim_set_by_pointer, 2);
  625. expect_value_count(rrddim_set_by_pointer, st, st_rusage, 2);
  626. expect_function_call(rrdset_done);
  627. expect_value(rrdset_done, st, st_rusage);
  628. __real_send_main_rusage(st_rusage, NULL, NULL);
  629. free(st_rusage);
  630. }
  631. static void test_send_internal_metrics(void **state)
  632. {
  633. UNUSED(state);
  634. struct instance *instance = calloc(1, sizeof(struct instance));
  635. instance->config.name = (const char *)strdupz("test_instance");
  636. instance->config.update_every = 2;
  637. struct stats *stats = &instance->stats;
  638. stats->st_metrics = calloc(1, sizeof(RRDSET));
  639. stats->st_metrics->counter_done = 1;
  640. stats->st_bytes = calloc(1, sizeof(RRDSET));
  641. stats->st_bytes->counter_done = 1;
  642. stats->st_ops = calloc(1, sizeof(RRDSET));
  643. stats->st_ops->counter_done = 1;
  644. stats->st_rusage = calloc(1, sizeof(RRDSET));
  645. stats->st_rusage->counter_done = 1;
  646. // ------------------------------------------------------------------------
  647. expect_function_call(rrdset_create_custom);
  648. expect_value(rrdset_create_custom, host, localhost);
  649. expect_string(rrdset_create_custom, type, "netdata");
  650. expect_string(rrdset_create_custom, id, "exporting_test_instance_metrics");
  651. expect_value(rrdset_create_custom, name, NULL);
  652. expect_string(rrdset_create_custom, family, "exporting_test_instance");
  653. expect_string(rrdset_create_custom, context, "exporting_buffer");
  654. expect_string(rrdset_create_custom, units, "metrics");
  655. expect_string(rrdset_create_custom, plugin, "exporting");
  656. expect_value(rrdset_create_custom, module, NULL);
  657. expect_value(rrdset_create_custom, priority, 130610);
  658. expect_value(rrdset_create_custom, update_every, 2);
  659. expect_value(rrdset_create_custom, chart_type, RRDSET_TYPE_LINE);
  660. will_return(rrdset_create_custom, stats->st_metrics);
  661. expect_function_calls(rrddim_add_custom, 3);
  662. expect_value_count(rrddim_add_custom, st, stats->st_metrics, 3);
  663. expect_value_count(rrddim_add_custom, name, NULL, 3);
  664. expect_value_count(rrddim_add_custom, multiplier, 1, 3);
  665. expect_value_count(rrddim_add_custom, divisor, 1, 3);
  666. expect_value_count(rrddim_add_custom, algorithm, RRD_ALGORITHM_ABSOLUTE, 3);
  667. // ------------------------------------------------------------------------
  668. expect_function_call(rrdset_create_custom);
  669. expect_value(rrdset_create_custom, host, localhost);
  670. expect_string(rrdset_create_custom, type, "netdata");
  671. expect_string(rrdset_create_custom, id, "exporting_test_instance_bytes");
  672. expect_value(rrdset_create_custom, name, NULL);
  673. expect_string(rrdset_create_custom, family, "exporting_test_instance");
  674. expect_string(rrdset_create_custom, context, "exporting_data_size");
  675. expect_string(rrdset_create_custom, units, "KiB");
  676. expect_string(rrdset_create_custom, plugin, "exporting");
  677. expect_value(rrdset_create_custom, module, NULL);
  678. expect_value(rrdset_create_custom, priority, 130620);
  679. expect_value(rrdset_create_custom, update_every, 2);
  680. expect_value(rrdset_create_custom, chart_type, RRDSET_TYPE_AREA);
  681. will_return(rrdset_create_custom, stats->st_bytes);
  682. expect_function_calls(rrddim_add_custom, 4);
  683. expect_value_count(rrddim_add_custom, st, stats->st_bytes, 4);
  684. expect_value_count(rrddim_add_custom, name, NULL, 4);
  685. expect_value_count(rrddim_add_custom, multiplier, 1, 4);
  686. expect_value_count(rrddim_add_custom, divisor, 1024, 4);
  687. expect_value_count(rrddim_add_custom, algorithm, RRD_ALGORITHM_ABSOLUTE, 4);
  688. // ------------------------------------------------------------------------
  689. expect_function_call(rrdset_create_custom);
  690. expect_value(rrdset_create_custom, host, localhost);
  691. expect_string(rrdset_create_custom, type, "netdata");
  692. expect_string(rrdset_create_custom, id, "exporting_test_instance_ops");
  693. expect_value(rrdset_create_custom, name, NULL);
  694. expect_string(rrdset_create_custom, family, "exporting_test_instance");
  695. expect_string(rrdset_create_custom, context, "exporting_operations");
  696. expect_string(rrdset_create_custom, units, "operations");
  697. expect_string(rrdset_create_custom, plugin, "exporting");
  698. expect_value(rrdset_create_custom, module, NULL);
  699. expect_value(rrdset_create_custom, priority, 130630);
  700. expect_value(rrdset_create_custom, update_every, 2);
  701. expect_value(rrdset_create_custom, chart_type, RRDSET_TYPE_LINE);
  702. will_return(rrdset_create_custom, stats->st_ops);
  703. expect_function_calls(rrddim_add_custom, 5);
  704. expect_value_count(rrddim_add_custom, st, stats->st_ops, 5);
  705. expect_value_count(rrddim_add_custom, name, NULL, 5);
  706. expect_value_count(rrddim_add_custom, multiplier, 1, 5);
  707. expect_value_count(rrddim_add_custom, divisor, 1, 5);
  708. expect_value_count(rrddim_add_custom, algorithm, RRD_ALGORITHM_ABSOLUTE, 5);
  709. // ------------------------------------------------------------------------
  710. expect_function_call(rrdset_create_custom);
  711. expect_value(rrdset_create_custom, host, localhost);
  712. expect_string(rrdset_create_custom, type, "netdata");
  713. expect_string(rrdset_create_custom, id, "exporting_test_instance_thread_cpu");
  714. expect_value(rrdset_create_custom, name, NULL);
  715. expect_string(rrdset_create_custom, family, "exporting_test_instance");
  716. expect_string(rrdset_create_custom, context, "exporting_instance");
  717. expect_string(rrdset_create_custom, units, "milliseconds/s");
  718. expect_string(rrdset_create_custom, plugin, "exporting");
  719. expect_value(rrdset_create_custom, module, NULL);
  720. expect_value(rrdset_create_custom, priority, 130640);
  721. expect_value(rrdset_create_custom, update_every, 2);
  722. expect_value(rrdset_create_custom, chart_type, RRDSET_TYPE_STACKED);
  723. will_return(rrdset_create_custom, stats->st_rusage);
  724. expect_function_calls(rrddim_add_custom, 2);
  725. expect_value_count(rrddim_add_custom, st, stats->st_rusage, 2);
  726. expect_value_count(rrddim_add_custom, name, NULL, 2);
  727. expect_value_count(rrddim_add_custom, multiplier, 1, 2);
  728. expect_value_count(rrddim_add_custom, divisor, 1000, 2);
  729. expect_value_count(rrddim_add_custom, algorithm, RRD_ALGORITHM_INCREMENTAL, 2);
  730. // ------------------------------------------------------------------------
  731. expect_function_call(rrdset_next_usec);
  732. expect_value(rrdset_next_usec, st, stats->st_metrics);
  733. expect_function_calls(rrddim_set_by_pointer, 3);
  734. expect_value_count(rrddim_set_by_pointer, st, stats->st_metrics, 3);
  735. expect_function_call(rrdset_done);
  736. expect_value(rrdset_done, st, stats->st_metrics);
  737. // ------------------------------------------------------------------------
  738. expect_function_call(rrdset_next_usec);
  739. expect_value(rrdset_next_usec, st, stats->st_bytes);
  740. expect_function_calls(rrddim_set_by_pointer, 4);
  741. expect_value_count(rrddim_set_by_pointer, st, stats->st_bytes, 4);
  742. expect_function_call(rrdset_done);
  743. expect_value(rrdset_done, st, stats->st_bytes);
  744. // ------------------------------------------------------------------------
  745. expect_function_call(rrdset_next_usec);
  746. expect_value(rrdset_next_usec, st, stats->st_ops);
  747. expect_function_calls(rrddim_set_by_pointer, 5);
  748. expect_value_count(rrddim_set_by_pointer, st, stats->st_ops, 5);
  749. expect_function_call(rrdset_done);
  750. expect_value(rrdset_done, st, stats->st_ops);
  751. // ------------------------------------------------------------------------
  752. expect_function_call(rrdset_next_usec);
  753. expect_value(rrdset_next_usec, st, stats->st_rusage);
  754. expect_function_calls(rrddim_set_by_pointer, 2);
  755. expect_value_count(rrddim_set_by_pointer, st, stats->st_rusage, 2);
  756. expect_function_call(rrdset_done);
  757. expect_value(rrdset_done, st, stats->st_rusage);
  758. // ------------------------------------------------------------------------
  759. __real_send_internal_metrics(instance);
  760. free(stats->st_metrics);
  761. free(stats->st_bytes);
  762. free(stats->st_ops);
  763. free(stats->st_rusage);
  764. free((void *)instance->config.name);
  765. free(instance);
  766. }
  767. static void test_can_send_rrdset(void **state)
  768. {
  769. (void)*state;
  770. assert_int_equal(can_send_rrdset(prometheus_exporter_instance, localhost->rrdset_root), 1);
  771. rrdset_flag_set(localhost->rrdset_root, RRDSET_FLAG_EXPORTING_IGNORE);
  772. assert_int_equal(can_send_rrdset(prometheus_exporter_instance, localhost->rrdset_root), 0);
  773. rrdset_flag_clear(localhost->rrdset_root, RRDSET_FLAG_EXPORTING_IGNORE);
  774. // TODO: test with a denying simple pattern
  775. rrdset_flag_set(localhost->rrdset_root, RRDSET_FLAG_OBSOLETE);
  776. assert_int_equal(can_send_rrdset(prometheus_exporter_instance, localhost->rrdset_root), 0);
  777. rrdset_flag_clear(localhost->rrdset_root, RRDSET_FLAG_OBSOLETE);
  778. localhost->rrdset_root->rrd_memory_mode = RRD_MEMORY_MODE_NONE;
  779. prometheus_exporter_instance->config.options |= EXPORTING_SOURCE_DATA_AVERAGE;
  780. assert_int_equal(can_send_rrdset(prometheus_exporter_instance, localhost->rrdset_root), 0);
  781. }
  782. static void test_prometheus_name_copy(void **state)
  783. {
  784. (void)*state;
  785. char destination_name[PROMETHEUS_ELEMENT_MAX + 1];
  786. assert_int_equal(prometheus_name_copy(destination_name, "test-name", PROMETHEUS_ELEMENT_MAX), 9);
  787. assert_string_equal(destination_name, "test_name");
  788. }
  789. static void test_prometheus_label_copy(void **state)
  790. {
  791. (void)*state;
  792. char destination_name[PROMETHEUS_ELEMENT_MAX + 1];
  793. assert_int_equal(prometheus_label_copy(destination_name, "test\"\\\nlabel", PROMETHEUS_ELEMENT_MAX), 15);
  794. assert_string_equal(destination_name, "test\\\"\\\\\\\nlabel");
  795. }
  796. static void test_prometheus_units_copy(void **state)
  797. {
  798. (void)*state;
  799. char destination_name[PROMETHEUS_ELEMENT_MAX + 1];
  800. assert_string_equal(prometheus_units_copy(destination_name, "test-units", PROMETHEUS_ELEMENT_MAX, 0), "_test_units");
  801. assert_string_equal(destination_name, "_test_units");
  802. assert_string_equal(prometheus_units_copy(destination_name, "%", PROMETHEUS_ELEMENT_MAX, 0), "_percent");
  803. assert_string_equal(prometheus_units_copy(destination_name, "test-units/s", PROMETHEUS_ELEMENT_MAX, 0), "_test_units_persec");
  804. assert_string_equal(prometheus_units_copy(destination_name, "KiB", PROMETHEUS_ELEMENT_MAX, 1), "_KB");
  805. }
  806. static void test_format_host_labels_prometheus(void **state)
  807. {
  808. struct engine *engine = *state;
  809. struct instance *instance = engine->instance_root;
  810. instance->config.options |= EXPORTING_OPTION_SEND_CONFIGURED_LABELS;
  811. instance->config.options |= EXPORTING_OPTION_SEND_AUTOMATIC_LABELS;
  812. format_host_labels_prometheus(instance, localhost);
  813. assert_string_equal(buffer_tostring(instance->labels), "key1=\"value1\",key2=\"value2\"");
  814. }
  815. static void rrd_stats_api_v1_charts_allmetrics_prometheus(void **state)
  816. {
  817. (void)state;
  818. BUFFER *buffer = buffer_create(0);
  819. localhost->hostname = strdupz("test_hostname");
  820. localhost->rrdset_root->family = strdupz("test_family");
  821. localhost->rrdset_root->context = strdupz("test_context");
  822. expect_function_call(__wrap_now_realtime_sec);
  823. will_return(__wrap_now_realtime_sec, 2);
  824. expect_function_call(__wrap_exporting_calculate_value_from_stored_data);
  825. will_return(__wrap_exporting_calculate_value_from_stored_data, pack_storage_number(27, SN_DEFAULT_FLAGS));
  826. rrd_stats_api_v1_charts_allmetrics_prometheus_single_host(localhost, buffer, "test_server", "test_prefix", 0, 0);
  827. assert_string_equal(
  828. buffer_tostring(buffer),
  829. "netdata_info{instance=\"test_hostname\",application=\"(null)\",version=\"(null)\"} 1\n"
  830. "netdata_host_tags_info{key1=\"value1\",key2=\"value2\"} 1\n"
  831. "netdata_host_tags{key1=\"value1\",key2=\"value2\"} 1\n"
  832. "test_prefix_test_context{chart=\"chart_id\",family=\"test_family\",dimension=\"dimension_id\"} 690565856.0000000\n");
  833. buffer_flush(buffer);
  834. expect_function_call(__wrap_now_realtime_sec);
  835. will_return(__wrap_now_realtime_sec, 2);
  836. expect_function_call(__wrap_exporting_calculate_value_from_stored_data);
  837. will_return(__wrap_exporting_calculate_value_from_stored_data, pack_storage_number(27, SN_DEFAULT_FLAGS));
  838. rrd_stats_api_v1_charts_allmetrics_prometheus_single_host(
  839. localhost, buffer, "test_server", "test_prefix", 0, PROMETHEUS_OUTPUT_NAMES | PROMETHEUS_OUTPUT_TYPES);
  840. assert_string_equal(
  841. buffer_tostring(buffer),
  842. "netdata_info{instance=\"test_hostname\",application=\"(null)\",version=\"(null)\"} 1\n"
  843. "netdata_host_tags_info{key1=\"value1\",key2=\"value2\"} 1\n"
  844. "netdata_host_tags{key1=\"value1\",key2=\"value2\"} 1\n"
  845. "# TYPE test_prefix_test_context gauge\n"
  846. "test_prefix_test_context{chart=\"chart_name\",family=\"test_family\",dimension=\"dimension_name\"} 690565856.0000000\n");
  847. buffer_flush(buffer);
  848. expect_function_call(__wrap_now_realtime_sec);
  849. will_return(__wrap_now_realtime_sec, 2);
  850. expect_function_call(__wrap_exporting_calculate_value_from_stored_data);
  851. will_return(__wrap_exporting_calculate_value_from_stored_data, pack_storage_number(27, SN_DEFAULT_FLAGS));
  852. rrd_stats_api_v1_charts_allmetrics_prometheus_all_hosts(localhost, buffer, "test_server", "test_prefix", 0, 0);
  853. assert_string_equal(
  854. buffer_tostring(buffer),
  855. "netdata_info{instance=\"test_hostname\",application=\"(null)\",version=\"(null)\"} 1\n"
  856. "netdata_host_tags_info{instance=\"test_hostname\",key1=\"value1\",key2=\"value2\"} 1\n"
  857. "netdata_host_tags{instance=\"test_hostname\",key1=\"value1\",key2=\"value2\"} 1\n"
  858. "test_prefix_test_context{chart=\"chart_id\",family=\"test_family\",dimension=\"dimension_id\",instance=\"test_hostname\"} 690565856.0000000\n");
  859. free(localhost->rrdset_root->context);
  860. free(localhost->rrdset_root->family);
  861. free(localhost->hostname);
  862. buffer_free(buffer);
  863. }
  864. #if ENABLE_PROMETHEUS_REMOTE_WRITE
  865. static void test_init_prometheus_remote_write_instance(void **state)
  866. {
  867. struct engine *engine = *state;
  868. struct instance *instance = engine->instance_root;
  869. expect_function_call(__wrap_init_write_request);
  870. will_return(__wrap_init_write_request, 0xff);
  871. assert_int_equal(init_prometheus_remote_write_instance(instance), 0);
  872. assert_ptr_equal(instance->worker, simple_connector_worker);
  873. assert_ptr_equal(instance->start_batch_formatting, NULL);
  874. assert_ptr_equal(instance->start_host_formatting, format_host_prometheus_remote_write);
  875. assert_ptr_equal(instance->start_chart_formatting, format_chart_prometheus_remote_write);
  876. assert_ptr_equal(instance->metric_formatting, format_dimension_prometheus_remote_write);
  877. assert_ptr_equal(instance->end_chart_formatting, NULL);
  878. assert_ptr_equal(instance->end_host_formatting, NULL);
  879. assert_ptr_equal(instance->end_batch_formatting, format_batch_prometheus_remote_write);
  880. assert_ptr_equal(instance->prepare_header, prometheus_remote_write_prepare_header);
  881. assert_ptr_equal(instance->check_response, process_prometheus_remote_write_response);
  882. assert_ptr_not_equal(instance->buffer, NULL);
  883. buffer_free(instance->buffer);
  884. struct prometheus_remote_write_specific_data *connector_specific_data =
  885. (struct prometheus_remote_write_specific_data *)instance->connector_specific_data;
  886. assert_ptr_not_equal(instance->connector_specific_data, NULL);
  887. assert_ptr_not_equal(connector_specific_data->write_request, NULL);
  888. freez(instance->connector_specific_data);
  889. }
  890. static void test_prometheus_remote_write_prepare_header(void **state)
  891. {
  892. struct engine *engine = *state;
  893. struct instance *instance = engine->instance_root;
  894. struct prometheus_remote_write_specific_config *connector_specific_config =
  895. callocz(1, sizeof(struct prometheus_remote_write_specific_config));
  896. instance->config.connector_specific_config = connector_specific_config;
  897. connector_specific_config->remote_write_path = strdupz("/receive");
  898. struct simple_connector_data *simple_connector_data = callocz(1, sizeof(struct simple_connector_data));
  899. instance->connector_specific_data = simple_connector_data;
  900. simple_connector_data->last_buffer = callocz(1, sizeof(struct simple_connector_buffer));
  901. simple_connector_data->last_buffer->header = buffer_create(0);
  902. simple_connector_data->last_buffer->buffer = buffer_create(0);
  903. strcpy(simple_connector_data->connected_to, "localhost");
  904. buffer_sprintf(simple_connector_data->last_buffer->buffer, "test buffer");
  905. prometheus_remote_write_prepare_header(instance);
  906. assert_string_equal(
  907. buffer_tostring(simple_connector_data->last_buffer->header),
  908. "POST /receive HTTP/1.1\r\n"
  909. "Host: localhost\r\n"
  910. "Accept: */*\r\n"
  911. "Content-Encoding: snappy\r\n"
  912. "Content-Type: application/x-protobuf\r\n"
  913. "X-Prometheus-Remote-Write-Version: 0.1.0\r\n"
  914. "Content-Length: 11\r\n"
  915. "\r\n");
  916. free(connector_specific_config->remote_write_path);
  917. buffer_free(simple_connector_data->last_buffer->header);
  918. buffer_free(simple_connector_data->last_buffer->buffer);
  919. }
  920. static void test_process_prometheus_remote_write_response(void **state)
  921. {
  922. (void)state;
  923. BUFFER *buffer = buffer_create(0);
  924. buffer_sprintf(buffer, "HTTP/1.1 200 OK\r\n");
  925. assert_int_equal(process_prometheus_remote_write_response(buffer, NULL), 0);
  926. buffer_free(buffer);
  927. }
  928. static void test_format_host_prometheus_remote_write(void **state)
  929. {
  930. struct engine *engine = *state;
  931. struct instance *instance = engine->instance_root;
  932. instance->config.options |= EXPORTING_OPTION_SEND_CONFIGURED_LABELS;
  933. instance->config.options |= EXPORTING_OPTION_SEND_AUTOMATIC_LABELS;
  934. struct simple_connector_data *simple_connector_data = mallocz(sizeof(struct simple_connector_data *));
  935. instance->connector_specific_data = simple_connector_data;
  936. struct prometheus_remote_write_specific_data *connector_specific_data =
  937. mallocz(sizeof(struct prometheus_remote_write_specific_data *));
  938. simple_connector_data->connector_specific_data = (void *)connector_specific_data;
  939. connector_specific_data->write_request = (void *)0xff;
  940. localhost->program_name = strdupz("test_program");
  941. localhost->program_version = strdupz("test_version");
  942. expect_function_call(__wrap_add_host_info);
  943. expect_value(__wrap_add_host_info, write_request_p, 0xff);
  944. expect_string(__wrap_add_host_info, name, "netdata_info");
  945. expect_string(__wrap_add_host_info, instance, "test-host");
  946. expect_string(__wrap_add_host_info, application, "test_program");
  947. expect_string(__wrap_add_host_info, version, "test_version");
  948. expect_in_range(
  949. __wrap_add_host_info, timestamp, now_realtime_usec() / USEC_PER_MS - 1000, now_realtime_usec() / USEC_PER_MS);
  950. expect_function_call(__wrap_add_label);
  951. expect_value(__wrap_add_label, write_request_p, 0xff);
  952. expect_string(__wrap_add_label, key, "key1");
  953. expect_string(__wrap_add_label, value, "value1");
  954. expect_function_call(__wrap_add_label);
  955. expect_value(__wrap_add_label, write_request_p, 0xff);
  956. expect_string(__wrap_add_label, key, "key2");
  957. expect_string(__wrap_add_label, value, "value2");
  958. assert_int_equal(format_host_prometheus_remote_write(instance, localhost), 0);
  959. freez(connector_specific_data);
  960. freez(simple_connector_data);
  961. free(localhost->program_name);
  962. free(localhost->program_version);
  963. }
  964. static void test_format_dimension_prometheus_remote_write(void **state)
  965. {
  966. struct engine *engine = *state;
  967. struct instance *instance = engine->instance_root;
  968. struct simple_connector_data *simple_connector_data = mallocz(sizeof(struct simple_connector_data *));
  969. instance->connector_specific_data = simple_connector_data;
  970. struct prometheus_remote_write_specific_data *connector_specific_data =
  971. mallocz(sizeof(struct prometheus_remote_write_specific_data *));
  972. simple_connector_data->connector_specific_data = (void *)connector_specific_data;
  973. connector_specific_data->write_request = (void *)0xff;
  974. RRDDIM *rd = localhost->rrdset_root->dimensions;
  975. expect_function_call(__wrap_exporting_calculate_value_from_stored_data);
  976. will_return(__wrap_exporting_calculate_value_from_stored_data, pack_storage_number(27, SN_DEFAULT_FLAGS));
  977. expect_function_call(__wrap_add_metric);
  978. expect_value(__wrap_add_metric, write_request_p, 0xff);
  979. expect_string(__wrap_add_metric, name, "netdata_");
  980. expect_string(__wrap_add_metric, chart, "");
  981. expect_string(__wrap_add_metric, family, "");
  982. expect_string(__wrap_add_metric, dimension, "dimension_name");
  983. expect_string(__wrap_add_metric, instance, "test-host");
  984. expect_value(__wrap_add_metric, value, 0x292932e0);
  985. expect_value(__wrap_add_metric, timestamp, 15052 * MSEC_PER_SEC);
  986. assert_int_equal(format_dimension_prometheus_remote_write(instance, rd), 0);
  987. }
  988. static void test_format_batch_prometheus_remote_write(void **state)
  989. {
  990. struct engine *engine = *state;
  991. struct instance *instance = engine->instance_root;
  992. struct simple_connector_data *simple_connector_data = mallocz(sizeof(struct simple_connector_data *));
  993. instance->connector_specific_data = simple_connector_data;
  994. struct prometheus_remote_write_specific_data *connector_specific_data =
  995. mallocz(sizeof(struct prometheus_remote_write_specific_data *));
  996. simple_connector_data->connector_specific_data = (void *)connector_specific_data;
  997. connector_specific_data->write_request = __real_init_write_request();
  998. expect_function_call(__wrap_simple_connector_end_batch);
  999. expect_value(__wrap_simple_connector_end_batch, instance, instance);
  1000. will_return(__wrap_simple_connector_end_batch, 0);
  1001. __real_add_host_info(
  1002. connector_specific_data->write_request,
  1003. "test_name", "test_instance", "test_application", "test_version", 15051);
  1004. __real_add_label(connector_specific_data->write_request, "test_key", "test_value");
  1005. __real_add_metric(
  1006. connector_specific_data->write_request,
  1007. "test_name", "test chart", "test_family", "test_dimension", "test_instance",
  1008. 123000321, 15052);
  1009. assert_int_equal(format_batch_prometheus_remote_write(instance), 0);
  1010. BUFFER *buffer = instance->buffer;
  1011. char *write_request_string = calloc(1, 1000);
  1012. convert_write_request_to_string(buffer_tostring(buffer), buffer_strlen(buffer), write_request_string, 999);
  1013. assert_int_equal(strlen(write_request_string), 753);
  1014. assert_string_equal(
  1015. write_request_string,
  1016. "timeseries {\n"
  1017. " labels {\n"
  1018. " name: \"__name__\"\n"
  1019. " value: \"test_name\"\n"
  1020. " }\n"
  1021. " labels {\n"
  1022. " name: \"instance\"\n"
  1023. " value: \"test_instance\"\n"
  1024. " }\n"
  1025. " labels {\n"
  1026. " name: \"application\"\n"
  1027. " value: \"test_application\"\n"
  1028. " }\n"
  1029. " labels {\n"
  1030. " name: \"version\"\n"
  1031. " value: \"test_version\"\n"
  1032. " }\n"
  1033. " labels {\n"
  1034. " name: \"test_key\"\n"
  1035. " value: \"test_value\"\n"
  1036. " }\n"
  1037. " samples {\n"
  1038. " value: 1\n"
  1039. " timestamp: 15051\n"
  1040. " }\n"
  1041. "}\n"
  1042. "timeseries {\n"
  1043. " labels {\n"
  1044. " name: \"__name__\"\n"
  1045. " value: \"test_name\"\n"
  1046. " }\n"
  1047. " labels {\n"
  1048. " name: \"chart\"\n"
  1049. " value: \"test chart\"\n"
  1050. " }\n"
  1051. " labels {\n"
  1052. " name: \"family\"\n"
  1053. " value: \"test_family\"\n"
  1054. " }\n"
  1055. " labels {\n"
  1056. " name: \"dimension\"\n"
  1057. " value: \"test_dimension\"\n"
  1058. " }\n"
  1059. " labels {\n"
  1060. " name: \"instance\"\n"
  1061. " value: \"test_instance\"\n"
  1062. " }\n"
  1063. " samples {\n"
  1064. " value: 123000321\n"
  1065. " timestamp: 15052\n"
  1066. " }\n"
  1067. "}\n");
  1068. free(write_request_string);
  1069. protocol_buffers_shutdown();
  1070. }
  1071. #endif // ENABLE_PROMETHEUS_REMOTE_WRITE
  1072. #if HAVE_KINESIS
  1073. static void test_init_aws_kinesis_instance(void **state)
  1074. {
  1075. struct engine *engine = *state;
  1076. struct instance *instance = engine->instance_root;
  1077. instance->config.options = EXPORTING_SOURCE_DATA_AS_COLLECTED | EXPORTING_OPTION_SEND_NAMES;
  1078. struct aws_kinesis_specific_config *connector_specific_config =
  1079. callocz(1, sizeof(struct aws_kinesis_specific_config));
  1080. instance->config.connector_specific_config = connector_specific_config;
  1081. connector_specific_config->stream_name = strdupz("test_stream");
  1082. connector_specific_config->auth_key_id = strdupz("test_auth_key_id");
  1083. connector_specific_config->secure_key = strdupz("test_secure_key");
  1084. expect_function_call(__wrap_aws_sdk_init);
  1085. expect_function_call(__wrap_kinesis_init);
  1086. expect_not_value(__wrap_kinesis_init, kinesis_specific_data_p, NULL);
  1087. expect_string(__wrap_kinesis_init, region, "localhost");
  1088. expect_string(__wrap_kinesis_init, access_key_id, "test_auth_key_id");
  1089. expect_string(__wrap_kinesis_init, secret_key, "test_secure_key");
  1090. expect_value(__wrap_kinesis_init, timeout, 10000);
  1091. assert_int_equal(init_aws_kinesis_instance(instance), 0);
  1092. assert_ptr_equal(instance->worker, aws_kinesis_connector_worker);
  1093. assert_ptr_equal(instance->start_batch_formatting, NULL);
  1094. assert_ptr_equal(instance->start_host_formatting, format_host_labels_json_plaintext);
  1095. assert_ptr_equal(instance->start_chart_formatting, NULL);
  1096. assert_ptr_equal(instance->metric_formatting, format_dimension_collected_json_plaintext);
  1097. assert_ptr_equal(instance->end_chart_formatting, NULL);
  1098. assert_ptr_equal(instance->end_host_formatting, flush_host_labels);
  1099. assert_ptr_equal(instance->end_batch_formatting, NULL);
  1100. assert_ptr_not_equal(instance->buffer, NULL);
  1101. buffer_free(instance->buffer);
  1102. assert_ptr_not_equal(instance->connector_specific_data, NULL);
  1103. freez(instance->connector_specific_data);
  1104. instance->config.options = EXPORTING_SOURCE_DATA_AVERAGE | EXPORTING_OPTION_SEND_NAMES;
  1105. expect_function_call(__wrap_kinesis_init);
  1106. expect_not_value(__wrap_kinesis_init, kinesis_specific_data_p, NULL);
  1107. expect_string(__wrap_kinesis_init, region, "localhost");
  1108. expect_string(__wrap_kinesis_init, access_key_id, "test_auth_key_id");
  1109. expect_string(__wrap_kinesis_init, secret_key, "test_secure_key");
  1110. expect_value(__wrap_kinesis_init, timeout, 10000);
  1111. assert_int_equal(init_aws_kinesis_instance(instance), 0);
  1112. assert_ptr_equal(instance->metric_formatting, format_dimension_stored_json_plaintext);
  1113. free(connector_specific_config->stream_name);
  1114. free(connector_specific_config->auth_key_id);
  1115. free(connector_specific_config->secure_key);
  1116. }
  1117. static void test_aws_kinesis_connector_worker(void **state)
  1118. {
  1119. struct engine *engine = *state;
  1120. struct instance *instance = engine->instance_root;
  1121. struct stats *stats = &instance->stats;
  1122. BUFFER *buffer = instance->buffer;
  1123. __real_mark_scheduled_instances(engine);
  1124. expect_function_call(__wrap_rrdhost_is_exportable);
  1125. expect_value(__wrap_rrdhost_is_exportable, instance, instance);
  1126. expect_value(__wrap_rrdhost_is_exportable, host, localhost);
  1127. will_return(__wrap_rrdhost_is_exportable, 1);
  1128. RRDSET *st = localhost->rrdset_root;
  1129. expect_function_call(__wrap_rrdset_is_exportable);
  1130. expect_value(__wrap_rrdset_is_exportable, instance, instance);
  1131. expect_value(__wrap_rrdset_is_exportable, st, st);
  1132. will_return(__wrap_rrdset_is_exportable, 1);
  1133. expect_function_call(__wrap_simple_connector_end_batch);
  1134. expect_value(__wrap_simple_connector_end_batch, instance, instance);
  1135. will_return(__wrap_simple_connector_end_batch, 0);
  1136. __real_prepare_buffers(engine);
  1137. struct aws_kinesis_specific_config *connector_specific_config =
  1138. callocz(1, sizeof(struct aws_kinesis_specific_config));
  1139. instance->config.connector_specific_config = connector_specific_config;
  1140. connector_specific_config->stream_name = strdupz("test_stream");
  1141. connector_specific_config->auth_key_id = strdupz("test_auth_key_id");
  1142. connector_specific_config->secure_key = strdupz("test_secure_key");
  1143. struct aws_kinesis_specific_data *connector_specific_data = callocz(1, sizeof(struct aws_kinesis_specific_data));
  1144. instance->connector_specific_data = (void *)connector_specific_data;
  1145. expect_function_call(__wrap_kinesis_put_record);
  1146. expect_not_value(__wrap_kinesis_put_record, kinesis_specific_data_p, NULL);
  1147. expect_string(__wrap_kinesis_put_record, stream_name, "test_stream");
  1148. expect_string(__wrap_kinesis_put_record, partition_key, "netdata_0");
  1149. expect_value(__wrap_kinesis_put_record, data, buffer_tostring(buffer));
  1150. // The buffer is prepared by Graphite exporting connector
  1151. expect_string(
  1152. __wrap_kinesis_put_record, data,
  1153. "netdata.test-host.chart_name.dimension_name;TAG1=VALUE1 TAG2=VALUE2 123000321 15051\n");
  1154. expect_value(__wrap_kinesis_put_record, data_len, 84);
  1155. expect_function_call(__wrap_kinesis_get_result);
  1156. expect_value(__wrap_kinesis_get_result, request_outcomes_p, NULL);
  1157. expect_not_value(__wrap_kinesis_get_result, error_message, NULL);
  1158. expect_not_value(__wrap_kinesis_get_result, sent_bytes, NULL);
  1159. expect_not_value(__wrap_kinesis_get_result, lost_bytes, NULL);
  1160. will_return(__wrap_kinesis_get_result, 0);
  1161. expect_function_call(__wrap_send_internal_metrics);
  1162. expect_value(__wrap_send_internal_metrics, instance, instance);
  1163. will_return(__wrap_send_internal_metrics, 0);
  1164. aws_kinesis_connector_worker(instance);
  1165. assert_int_equal(stats->buffered_metrics, 0);
  1166. assert_int_equal(stats->buffered_bytes, 84);
  1167. assert_int_equal(stats->received_bytes, 0);
  1168. assert_int_equal(stats->sent_bytes, 84);
  1169. assert_int_equal(stats->sent_metrics, 1);
  1170. assert_int_equal(stats->lost_metrics, 0);
  1171. assert_int_equal(stats->receptions, 1);
  1172. assert_int_equal(stats->transmission_successes, 1);
  1173. assert_int_equal(stats->transmission_failures, 0);
  1174. assert_int_equal(stats->data_lost_events, 0);
  1175. assert_int_equal(stats->lost_bytes, 0);
  1176. assert_int_equal(stats->reconnects, 0);
  1177. free(connector_specific_config->stream_name);
  1178. free(connector_specific_config->auth_key_id);
  1179. free(connector_specific_config->secure_key);
  1180. }
  1181. #endif // HAVE_KINESIS
  1182. #if ENABLE_EXPORTING_PUBSUB
  1183. static void test_init_pubsub_instance(void **state)
  1184. {
  1185. struct engine *engine = *state;
  1186. struct instance *instance = engine->instance_root;
  1187. instance->config.options = EXPORTING_SOURCE_DATA_AS_COLLECTED | EXPORTING_OPTION_SEND_NAMES;
  1188. struct pubsub_specific_config *connector_specific_config =
  1189. callocz(1, sizeof(struct pubsub_specific_config));
  1190. instance->config.connector_specific_config = connector_specific_config;
  1191. connector_specific_config->credentials_file = strdupz("/test/credentials/file");
  1192. connector_specific_config->project_id = strdupz("test_project_id");
  1193. connector_specific_config->topic_id = strdupz("test_topic_id");
  1194. expect_function_call(__wrap_pubsub_init);
  1195. expect_not_value(__wrap_pubsub_init, pubsub_specific_data_p, NULL);
  1196. expect_string(__wrap_pubsub_init, destination, "localhost");
  1197. expect_string(__wrap_pubsub_init, error_message, "");
  1198. expect_string(__wrap_pubsub_init, credentials_file, "/test/credentials/file");
  1199. expect_string(__wrap_pubsub_init, project_id, "test_project_id");
  1200. expect_string(__wrap_pubsub_init, topic_id, "test_topic_id");
  1201. will_return(__wrap_pubsub_init, 0);
  1202. assert_int_equal(init_pubsub_instance(instance), 0);
  1203. assert_ptr_equal(instance->worker, pubsub_connector_worker);
  1204. assert_ptr_equal(instance->start_batch_formatting, NULL);
  1205. assert_ptr_equal(instance->start_host_formatting, format_host_labels_json_plaintext);
  1206. assert_ptr_equal(instance->start_chart_formatting, NULL);
  1207. assert_ptr_equal(instance->metric_formatting, format_dimension_collected_json_plaintext);
  1208. assert_ptr_equal(instance->end_chart_formatting, NULL);
  1209. assert_ptr_equal(instance->end_host_formatting, flush_host_labels);
  1210. assert_ptr_equal(instance->end_batch_formatting, NULL);
  1211. assert_ptr_not_equal(instance->buffer, NULL);
  1212. buffer_free(instance->buffer);
  1213. assert_ptr_not_equal(instance->connector_specific_data, NULL);
  1214. freez(instance->connector_specific_data);
  1215. instance->config.options = EXPORTING_SOURCE_DATA_AVERAGE | EXPORTING_OPTION_SEND_NAMES;
  1216. expect_function_call(__wrap_pubsub_init);
  1217. expect_not_value(__wrap_pubsub_init, pubsub_specific_data_p, NULL);
  1218. expect_string(__wrap_pubsub_init, destination, "localhost");
  1219. expect_string(__wrap_pubsub_init, error_message, "");
  1220. expect_string(__wrap_pubsub_init, credentials_file, "/test/credentials/file");
  1221. expect_string(__wrap_pubsub_init, project_id, "test_project_id");
  1222. expect_string(__wrap_pubsub_init, topic_id, "test_topic_id");
  1223. will_return(__wrap_pubsub_init, 0);
  1224. assert_int_equal(init_pubsub_instance(instance), 0);
  1225. assert_ptr_equal(instance->metric_formatting, format_dimension_stored_json_plaintext);
  1226. free(connector_specific_config->credentials_file);
  1227. free(connector_specific_config->project_id);
  1228. free(connector_specific_config->topic_id);
  1229. }
  1230. static void test_pubsub_connector_worker(void **state)
  1231. {
  1232. struct engine *engine = *state;
  1233. struct instance *instance = engine->instance_root;
  1234. struct stats *stats = &instance->stats;
  1235. __real_mark_scheduled_instances(engine);
  1236. expect_function_call(__wrap_rrdhost_is_exportable);
  1237. expect_value(__wrap_rrdhost_is_exportable, instance, instance);
  1238. expect_value(__wrap_rrdhost_is_exportable, host, localhost);
  1239. will_return(__wrap_rrdhost_is_exportable, 1);
  1240. RRDSET *st = localhost->rrdset_root;
  1241. expect_function_call(__wrap_rrdset_is_exportable);
  1242. expect_value(__wrap_rrdset_is_exportable, instance, instance);
  1243. expect_value(__wrap_rrdset_is_exportable, st, st);
  1244. will_return(__wrap_rrdset_is_exportable, 1);
  1245. expect_function_call(__wrap_simple_connector_end_batch);
  1246. expect_value(__wrap_simple_connector_end_batch, instance, instance);
  1247. will_return(__wrap_simple_connector_end_batch, 0);
  1248. __real_prepare_buffers(engine);
  1249. struct pubsub_specific_config *connector_specific_config =
  1250. callocz(1, sizeof(struct pubsub_specific_config));
  1251. instance->config.connector_specific_config = connector_specific_config;
  1252. connector_specific_config->credentials_file = strdupz("/test/credentials/file");
  1253. connector_specific_config->project_id = strdupz("test_project_id");
  1254. connector_specific_config->topic_id = strdupz("test_topic_id");
  1255. struct pubsub_specific_data *connector_specific_data = callocz(1, sizeof(struct pubsub_specific_data));
  1256. instance->connector_specific_data = (void *)connector_specific_data;
  1257. expect_function_call(__wrap_pubsub_add_message);
  1258. expect_not_value(__wrap_pubsub_add_message, pubsub_specific_data_p, NULL);
  1259. // The buffer is prepared by Graphite exporting connector
  1260. expect_string(
  1261. __wrap_pubsub_add_message, data,
  1262. "netdata.test-host.chart_name.dimension_name;TAG1=VALUE1 TAG2=VALUE2 123000321 15051\n");
  1263. will_return(__wrap_pubsub_add_message, 0);
  1264. expect_function_call(__wrap_pubsub_publish);
  1265. expect_not_value(__wrap_pubsub_publish, pubsub_specific_data_p, NULL);
  1266. expect_string(__wrap_pubsub_publish, error_message, "");
  1267. expect_value(__wrap_pubsub_publish, buffered_metrics, 1);
  1268. expect_value(__wrap_pubsub_publish, buffered_bytes, 84);
  1269. will_return(__wrap_pubsub_publish, 0);
  1270. expect_function_call(__wrap_pubsub_get_result);
  1271. expect_not_value(__wrap_pubsub_get_result, pubsub_specific_data_p, NULL);
  1272. expect_not_value(__wrap_pubsub_get_result, error_message, NULL);
  1273. expect_not_value(__wrap_pubsub_get_result, sent_metrics, NULL);
  1274. expect_not_value(__wrap_pubsub_get_result, sent_bytes, NULL);
  1275. expect_not_value(__wrap_pubsub_get_result, lost_metrics, NULL);
  1276. expect_not_value(__wrap_pubsub_get_result, lost_bytes, NULL);
  1277. will_return(__wrap_pubsub_get_result, 0);
  1278. expect_function_call(__wrap_send_internal_metrics);
  1279. expect_value(__wrap_send_internal_metrics, instance, instance);
  1280. will_return(__wrap_send_internal_metrics, 0);
  1281. pubsub_connector_worker(instance);
  1282. assert_int_equal(stats->buffered_metrics, 0);
  1283. assert_int_equal(stats->buffered_bytes, 84);
  1284. assert_int_equal(stats->received_bytes, 0);
  1285. assert_int_equal(stats->sent_bytes, 84);
  1286. assert_int_equal(stats->sent_metrics, 0);
  1287. assert_int_equal(stats->lost_metrics, 0);
  1288. assert_int_equal(stats->receptions, 1);
  1289. assert_int_equal(stats->transmission_successes, 1);
  1290. assert_int_equal(stats->transmission_failures, 0);
  1291. assert_int_equal(stats->data_lost_events, 0);
  1292. assert_int_equal(stats->lost_bytes, 0);
  1293. assert_int_equal(stats->reconnects, 0);
  1294. free(connector_specific_config->credentials_file);
  1295. free(connector_specific_config->project_id);
  1296. free(connector_specific_config->topic_id);
  1297. }
  1298. #endif // ENABLE_EXPORTING_PUBSUB
  1299. #if HAVE_MONGOC
  1300. static void test_init_mongodb_instance(void **state)
  1301. {
  1302. struct engine *engine = *state;
  1303. struct instance *instance = engine->instance_root;
  1304. instance->config.options = EXPORTING_SOURCE_DATA_AS_COLLECTED | EXPORTING_OPTION_SEND_NAMES;
  1305. struct mongodb_specific_config *connector_specific_config = callocz(1, sizeof(struct mongodb_specific_config));
  1306. instance->config.connector_specific_config = connector_specific_config;
  1307. connector_specific_config->database = strdupz("test_database");
  1308. connector_specific_config->collection = strdupz("test_collection");
  1309. instance->config.buffer_on_failures = 10;
  1310. expect_function_call(__wrap_mongoc_init);
  1311. expect_function_call(__wrap_mongoc_uri_new_with_error);
  1312. expect_string(__wrap_mongoc_uri_new_with_error, uri_string, "localhost");
  1313. expect_not_value(__wrap_mongoc_uri_new_with_error, error, NULL);
  1314. will_return(__wrap_mongoc_uri_new_with_error, 0xf1);
  1315. expect_function_call(__wrap_mongoc_uri_get_option_as_int32);
  1316. expect_value(__wrap_mongoc_uri_get_option_as_int32, uri, 0xf1);
  1317. expect_string(__wrap_mongoc_uri_get_option_as_int32, option, MONGOC_URI_SOCKETTIMEOUTMS);
  1318. expect_value(__wrap_mongoc_uri_get_option_as_int32, fallback, 1000);
  1319. will_return(__wrap_mongoc_uri_get_option_as_int32, 1000);
  1320. expect_function_call(__wrap_mongoc_uri_set_option_as_int32);
  1321. expect_value(__wrap_mongoc_uri_set_option_as_int32, uri, 0xf1);
  1322. expect_string(__wrap_mongoc_uri_set_option_as_int32, option, MONGOC_URI_SOCKETTIMEOUTMS);
  1323. expect_value(__wrap_mongoc_uri_set_option_as_int32, value, 1000);
  1324. will_return(__wrap_mongoc_uri_set_option_as_int32, true);
  1325. expect_function_call(__wrap_mongoc_client_new_from_uri);
  1326. expect_value(__wrap_mongoc_client_new_from_uri, uri, 0xf1);
  1327. will_return(__wrap_mongoc_client_new_from_uri, 0xf2);
  1328. expect_function_call(__wrap_mongoc_client_set_appname);
  1329. expect_value(__wrap_mongoc_client_set_appname, client, 0xf2);
  1330. expect_string(__wrap_mongoc_client_set_appname, appname, "netdata");
  1331. will_return(__wrap_mongoc_client_set_appname, true);
  1332. expect_function_call(__wrap_mongoc_client_get_collection);
  1333. expect_value(__wrap_mongoc_client_get_collection, client, 0xf2);
  1334. expect_string(__wrap_mongoc_client_get_collection, db, "test_database");
  1335. expect_string(__wrap_mongoc_client_get_collection, collection, "test_collection");
  1336. will_return(__wrap_mongoc_client_get_collection, 0xf3);
  1337. expect_function_call(__wrap_mongoc_uri_destroy);
  1338. expect_value(__wrap_mongoc_uri_destroy, uri, 0xf1);
  1339. assert_int_equal(init_mongodb_instance(instance), 0);
  1340. assert_ptr_equal(instance->worker, mongodb_connector_worker);
  1341. assert_ptr_equal(instance->start_batch_formatting, NULL);
  1342. assert_ptr_equal(instance->start_host_formatting, format_host_labels_json_plaintext);
  1343. assert_ptr_equal(instance->start_chart_formatting, NULL);
  1344. assert_ptr_equal(instance->metric_formatting, format_dimension_collected_json_plaintext);
  1345. assert_ptr_equal(instance->end_chart_formatting, NULL);
  1346. assert_ptr_equal(instance->end_host_formatting, flush_host_labels);
  1347. assert_ptr_equal(instance->end_batch_formatting, format_batch_mongodb);
  1348. assert_ptr_equal(instance->prepare_header, NULL);
  1349. assert_ptr_equal(instance->check_response, NULL);
  1350. assert_ptr_not_equal(instance->buffer, NULL);
  1351. buffer_free(instance->buffer);
  1352. assert_ptr_not_equal(instance->connector_specific_data, NULL);
  1353. struct mongodb_specific_data *connector_specific_data =
  1354. (struct mongodb_specific_data *)instance->connector_specific_data;
  1355. size_t number_of_buffers = 1;
  1356. struct bson_buffer *current_buffer = connector_specific_data->first_buffer;
  1357. while (current_buffer->next != connector_specific_data->first_buffer) {
  1358. current_buffer = current_buffer->next;
  1359. number_of_buffers++;
  1360. if (number_of_buffers == (size_t)(instance->config.buffer_on_failures + 1)) {
  1361. number_of_buffers = 0;
  1362. break;
  1363. }
  1364. }
  1365. assert_int_equal(number_of_buffers, 9);
  1366. free(connector_specific_config->database);
  1367. free(connector_specific_config->collection);
  1368. }
  1369. static void test_format_batch_mongodb(void **state)
  1370. {
  1371. struct engine *engine = *state;
  1372. struct instance *instance = engine->instance_root;
  1373. struct stats *stats = &instance->stats;
  1374. struct mongodb_specific_data *connector_specific_data = mallocz(sizeof(struct mongodb_specific_data));
  1375. instance->connector_specific_data = (void *)connector_specific_data;
  1376. struct bson_buffer *current_buffer = callocz(1, sizeof(struct bson_buffer));
  1377. connector_specific_data->first_buffer = current_buffer;
  1378. connector_specific_data->first_buffer->next = current_buffer;
  1379. connector_specific_data->last_buffer = current_buffer;
  1380. BUFFER *buffer = buffer_create(0);
  1381. buffer_sprintf(buffer, "{ \"metric\": \"test_metric\" }\n");
  1382. instance->buffer = buffer;
  1383. stats->buffered_metrics = 1;
  1384. assert_int_equal(format_batch_mongodb(instance), 0);
  1385. assert_int_equal(connector_specific_data->last_buffer->documents_inserted, 1);
  1386. assert_int_equal(buffer_strlen(buffer), 0);
  1387. size_t len;
  1388. char *str = bson_as_canonical_extended_json(connector_specific_data->last_buffer->insert[0], &len);
  1389. assert_string_equal(str, "{ \"metric\" : \"test_metric\" }");
  1390. freez(str);
  1391. buffer_free(buffer);
  1392. }
  1393. static void test_mongodb_connector_worker(void **state)
  1394. {
  1395. struct engine *engine = *state;
  1396. struct instance *instance = engine->instance_root;
  1397. struct mongodb_specific_config *connector_specific_config = callocz(1, sizeof(struct mongodb_specific_config));
  1398. instance->config.connector_specific_config = connector_specific_config;
  1399. connector_specific_config->database = strdupz("test_database");
  1400. struct mongodb_specific_data *connector_specific_data = callocz(1, sizeof(struct mongodb_specific_data));
  1401. instance->connector_specific_data = (void *)connector_specific_data;
  1402. connector_specific_config->collection = strdupz("test_collection");
  1403. struct bson_buffer *buffer = callocz(1, sizeof(struct bson_buffer));
  1404. buffer->documents_inserted = 1;
  1405. connector_specific_data->first_buffer = buffer;
  1406. connector_specific_data->first_buffer->next = buffer;
  1407. connector_specific_data->first_buffer->insert = callocz(1, sizeof(bson_t *));
  1408. bson_error_t bson_error;
  1409. connector_specific_data->first_buffer->insert[0] =
  1410. bson_new_from_json((const uint8_t *)"{ \"test_key\" : \"test_value\" }", -1, &bson_error);
  1411. connector_specific_data->client = mongoc_client_new("mongodb://localhost");
  1412. connector_specific_data->collection =
  1413. __real_mongoc_client_get_collection(connector_specific_data->client, "test_database", "test_collection");
  1414. expect_function_call(__wrap_mongoc_collection_insert_many);
  1415. expect_value(__wrap_mongoc_collection_insert_many, collection, connector_specific_data->collection);
  1416. expect_value(__wrap_mongoc_collection_insert_many, documents, connector_specific_data->first_buffer->insert);
  1417. expect_value(__wrap_mongoc_collection_insert_many, n_documents, 1);
  1418. expect_value(__wrap_mongoc_collection_insert_many, opts, NULL);
  1419. expect_value(__wrap_mongoc_collection_insert_many, reply, NULL);
  1420. expect_not_value(__wrap_mongoc_collection_insert_many, error, NULL);
  1421. will_return(__wrap_mongoc_collection_insert_many, true);
  1422. expect_function_call(__wrap_send_internal_metrics);
  1423. expect_value(__wrap_send_internal_metrics, instance, instance);
  1424. will_return(__wrap_send_internal_metrics, 0);
  1425. mongodb_connector_worker(instance);
  1426. assert_ptr_equal(connector_specific_data->first_buffer->insert, NULL);
  1427. assert_int_equal(connector_specific_data->first_buffer->documents_inserted, 0);
  1428. assert_ptr_equal(connector_specific_data->first_buffer, connector_specific_data->first_buffer->next);
  1429. struct stats *stats = &instance->stats;
  1430. assert_int_equal(stats->buffered_metrics, 0);
  1431. assert_int_equal(stats->buffered_bytes, 0);
  1432. assert_int_equal(stats->received_bytes, 0);
  1433. assert_int_equal(stats->sent_bytes, 30);
  1434. assert_int_equal(stats->sent_metrics, 1);
  1435. assert_int_equal(stats->lost_metrics, 0);
  1436. assert_int_equal(stats->receptions, 1);
  1437. assert_int_equal(stats->transmission_successes, 1);
  1438. assert_int_equal(stats->transmission_failures, 0);
  1439. assert_int_equal(stats->data_lost_events, 0);
  1440. assert_int_equal(stats->lost_bytes, 0);
  1441. assert_int_equal(stats->reconnects, 0);
  1442. free(connector_specific_config->database);
  1443. free(connector_specific_config->collection);
  1444. }
  1445. #endif // HAVE_MONGOC
  1446. int main(void)
  1447. {
  1448. const struct CMUnitTest tests[] = {
  1449. cmocka_unit_test_setup_teardown(test_exporting_engine, setup_initialized_engine, teardown_initialized_engine),
  1450. cmocka_unit_test(test_read_exporting_config),
  1451. cmocka_unit_test_setup_teardown(test_init_connectors, setup_configured_engine, teardown_configured_engine),
  1452. cmocka_unit_test_setup_teardown(
  1453. test_init_graphite_instance, setup_configured_engine, teardown_configured_engine),
  1454. cmocka_unit_test_setup_teardown(
  1455. test_init_json_instance, setup_configured_engine, teardown_configured_engine),
  1456. cmocka_unit_test_setup_teardown(
  1457. test_init_opentsdb_telnet_instance, setup_configured_engine, teardown_configured_engine),
  1458. cmocka_unit_test_setup_teardown(
  1459. test_init_opentsdb_http_instance, setup_configured_engine, teardown_configured_engine),
  1460. cmocka_unit_test_setup_teardown(
  1461. test_mark_scheduled_instances, setup_initialized_engine, teardown_initialized_engine),
  1462. cmocka_unit_test_setup_teardown(
  1463. test_rrdhost_is_exportable, setup_initialized_engine, teardown_initialized_engine),
  1464. cmocka_unit_test_setup_teardown(
  1465. test_false_rrdhost_is_exportable, setup_initialized_engine, teardown_initialized_engine),
  1466. cmocka_unit_test_setup_teardown(
  1467. test_rrdset_is_exportable, setup_initialized_engine, teardown_initialized_engine),
  1468. cmocka_unit_test_setup_teardown(
  1469. test_false_rrdset_is_exportable, setup_initialized_engine, teardown_initialized_engine),
  1470. cmocka_unit_test_setup_teardown(
  1471. test_exporting_calculate_value_from_stored_data, setup_initialized_engine, teardown_initialized_engine),
  1472. cmocka_unit_test_setup_teardown(test_prepare_buffers, setup_initialized_engine, teardown_initialized_engine),
  1473. cmocka_unit_test(test_exporting_name_copy),
  1474. cmocka_unit_test_setup_teardown(
  1475. test_format_dimension_collected_graphite_plaintext, setup_initialized_engine, teardown_initialized_engine),
  1476. cmocka_unit_test_setup_teardown(
  1477. test_format_dimension_stored_graphite_plaintext, setup_initialized_engine, teardown_initialized_engine),
  1478. cmocka_unit_test_setup_teardown(
  1479. test_format_dimension_collected_json_plaintext, setup_initialized_engine, teardown_initialized_engine),
  1480. cmocka_unit_test_setup_teardown(
  1481. test_format_dimension_stored_json_plaintext, setup_initialized_engine, teardown_initialized_engine),
  1482. cmocka_unit_test_setup_teardown(
  1483. test_format_dimension_collected_opentsdb_telnet, setup_initialized_engine, teardown_initialized_engine),
  1484. cmocka_unit_test_setup_teardown(
  1485. test_format_dimension_stored_opentsdb_telnet, setup_initialized_engine, teardown_initialized_engine),
  1486. cmocka_unit_test_setup_teardown(
  1487. test_format_dimension_collected_opentsdb_http, setup_initialized_engine, teardown_initialized_engine),
  1488. cmocka_unit_test_setup_teardown(
  1489. test_format_dimension_stored_opentsdb_http, setup_initialized_engine, teardown_initialized_engine),
  1490. cmocka_unit_test_setup_teardown(
  1491. test_exporting_discard_response, setup_initialized_engine, teardown_initialized_engine),
  1492. cmocka_unit_test_setup_teardown(
  1493. test_simple_connector_receive_response, setup_initialized_engine, teardown_initialized_engine),
  1494. cmocka_unit_test_setup_teardown(
  1495. test_simple_connector_send_buffer, setup_initialized_engine, teardown_initialized_engine),
  1496. cmocka_unit_test_setup_teardown(
  1497. test_simple_connector_worker, setup_initialized_engine, teardown_initialized_engine),
  1498. };
  1499. const struct CMUnitTest label_tests[] = {
  1500. cmocka_unit_test(test_sanitize_json_string),
  1501. cmocka_unit_test(test_sanitize_graphite_label_value),
  1502. cmocka_unit_test(test_sanitize_opentsdb_label_value),
  1503. cmocka_unit_test_setup_teardown(
  1504. test_format_host_labels_json_plaintext, setup_initialized_engine, teardown_initialized_engine),
  1505. cmocka_unit_test_setup_teardown(
  1506. test_format_host_labels_graphite_plaintext, setup_initialized_engine, teardown_initialized_engine),
  1507. cmocka_unit_test_setup_teardown(
  1508. test_format_host_labels_opentsdb_telnet, setup_initialized_engine, teardown_initialized_engine),
  1509. cmocka_unit_test_setup_teardown(
  1510. test_format_host_labels_opentsdb_http, setup_initialized_engine, teardown_initialized_engine),
  1511. cmocka_unit_test_setup_teardown(test_flush_host_labels, setup_initialized_engine, teardown_initialized_engine),
  1512. };
  1513. int test_res = cmocka_run_group_tests_name("exporting_engine", tests, NULL, NULL) +
  1514. cmocka_run_group_tests_name("labels_in_exporting_engine", label_tests, NULL, NULL);
  1515. const struct CMUnitTest internal_metrics_tests[] = {
  1516. cmocka_unit_test_setup_teardown(test_create_main_rusage_chart, setup_rrdhost, teardown_rrdhost),
  1517. cmocka_unit_test(test_send_main_rusage),
  1518. cmocka_unit_test(test_send_internal_metrics),
  1519. };
  1520. test_res += cmocka_run_group_tests_name("internal_metrics", internal_metrics_tests, NULL, NULL);
  1521. const struct CMUnitTest prometheus_web_api_tests[] = {
  1522. cmocka_unit_test_setup_teardown(test_can_send_rrdset, setup_prometheus, teardown_prometheus),
  1523. cmocka_unit_test_setup_teardown(test_prometheus_name_copy, setup_prometheus, teardown_prometheus),
  1524. cmocka_unit_test_setup_teardown(test_prometheus_label_copy, setup_prometheus, teardown_prometheus),
  1525. cmocka_unit_test_setup_teardown(test_prometheus_units_copy, setup_prometheus, teardown_prometheus),
  1526. cmocka_unit_test_setup_teardown(
  1527. test_format_host_labels_prometheus, setup_initialized_engine, teardown_initialized_engine),
  1528. cmocka_unit_test_setup_teardown(
  1529. rrd_stats_api_v1_charts_allmetrics_prometheus, setup_prometheus, teardown_prometheus),
  1530. };
  1531. test_res += cmocka_run_group_tests_name("prometheus_web_api", prometheus_web_api_tests, NULL, NULL);
  1532. #if ENABLE_PROMETHEUS_REMOTE_WRITE
  1533. const struct CMUnitTest prometheus_remote_write_tests[] = {
  1534. cmocka_unit_test_setup_teardown(
  1535. test_init_prometheus_remote_write_instance, setup_configured_engine, teardown_configured_engine),
  1536. cmocka_unit_test_setup_teardown(
  1537. test_prometheus_remote_write_prepare_header, setup_initialized_engine, teardown_initialized_engine),
  1538. cmocka_unit_test(test_process_prometheus_remote_write_response),
  1539. cmocka_unit_test_setup_teardown(
  1540. test_format_host_prometheus_remote_write, setup_initialized_engine, teardown_initialized_engine),
  1541. cmocka_unit_test_setup_teardown(
  1542. test_format_dimension_prometheus_remote_write, setup_initialized_engine, teardown_initialized_engine),
  1543. cmocka_unit_test_setup_teardown(
  1544. test_format_batch_prometheus_remote_write, setup_initialized_engine, teardown_initialized_engine),
  1545. };
  1546. test_res += cmocka_run_group_tests_name(
  1547. "prometheus_remote_write_exporting_connector", prometheus_remote_write_tests, NULL, NULL);
  1548. #endif
  1549. #if HAVE_KINESIS
  1550. const struct CMUnitTest kinesis_tests[] = {
  1551. cmocka_unit_test_setup_teardown(
  1552. test_init_aws_kinesis_instance, setup_configured_engine, teardown_configured_engine),
  1553. cmocka_unit_test_setup_teardown(
  1554. test_aws_kinesis_connector_worker, setup_initialized_engine, teardown_initialized_engine),
  1555. };
  1556. test_res += cmocka_run_group_tests_name("kinesis_exporting_connector", kinesis_tests, NULL, NULL);
  1557. #endif
  1558. #if ENABLE_EXPORTING_PUBSUB
  1559. const struct CMUnitTest pubsub_tests[] = {
  1560. cmocka_unit_test_setup_teardown(
  1561. test_init_pubsub_instance, setup_configured_engine, teardown_configured_engine),
  1562. cmocka_unit_test_setup_teardown(
  1563. test_pubsub_connector_worker, setup_initialized_engine, teardown_initialized_engine),
  1564. };
  1565. test_res += cmocka_run_group_tests_name("pubsub_exporting_connector", pubsub_tests, NULL, NULL);
  1566. #endif
  1567. #if HAVE_MONGOC
  1568. const struct CMUnitTest mongodb_tests[] = {
  1569. cmocka_unit_test_setup_teardown(
  1570. test_init_mongodb_instance, setup_configured_engine, teardown_configured_engine),
  1571. cmocka_unit_test_setup_teardown(
  1572. test_format_batch_mongodb, setup_configured_engine, teardown_configured_engine),
  1573. cmocka_unit_test_setup_teardown(
  1574. test_mongodb_connector_worker, setup_configured_engine, teardown_configured_engine),
  1575. };
  1576. test_res += cmocka_run_group_tests_name("mongodb_exporting_connector", mongodb_tests, NULL, NULL);
  1577. #endif
  1578. return test_res;
  1579. }