test_exporting_engine.c 83 KB

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