test_exporting_engine.c 83 KB

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