test_exporting_engine.c 83 KB

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