tester.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /*
  2. *
  3. * Embedded library
  4. *
  5. * Copyright (C) 2021 Intel Corporation. All rights reserved.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #define _GNU_SOURCE
  26. #include <getopt.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <signal.h>
  30. #include <sys/time.h>
  31. #include "idle.h"
  32. #include "log.h"
  33. #include "private.h"
  34. #include "queue.h"
  35. #include "time.h"
  36. #include "timeout.h"
  37. #include "useful.h"
  38. #include "tester.h"
  39. /**
  40. * SECTION:tester
  41. * @short_description: Non-interactive test framework
  42. *
  43. * Non-interactive test framework
  44. */
  45. #define COLOR_OFF "\x1B[0m"
  46. #define COLOR_BLACK "\x1B[0;30m"
  47. #define COLOR_RED "\x1B[0;31m"
  48. #define COLOR_GREEN "\x1B[0;32m"
  49. #define COLOR_YELLOW "\x1B[0;33m"
  50. #define COLOR_BLUE "\x1B[0;34m"
  51. #define COLOR_MAGENTA "\x1B[0;35m"
  52. #define COLOR_HIGHLIGHT "\x1B[1;39m"
  53. #define print_text(color, fmt, args...) \
  54. l_info(color fmt COLOR_OFF, ## args)
  55. #define print_summary(label, color, value, fmt, args...) \
  56. l_info("%-52s " color "%-10s" COLOR_OFF fmt, \
  57. label, value, ## args)
  58. #define print_progress(name, color, fmt, args...) \
  59. l_info(COLOR_HIGHLIGHT "%s" COLOR_OFF " - " \
  60. color fmt COLOR_OFF, name, ## args)
  61. enum test_result {
  62. TEST_RESULT_NOT_RUN,
  63. TEST_RESULT_PASSED,
  64. TEST_RESULT_FAILED,
  65. TEST_RESULT_TIMED_OUT,
  66. };
  67. struct l_tester {
  68. uint64_t start_time;
  69. struct l_queue *tests;
  70. const struct l_queue_entry *test_entry;
  71. bool list_cases;
  72. const char *prefix;
  73. const char *substring;
  74. l_tester_finish_func_t finish_callback;
  75. };
  76. struct test_case {
  77. uint64_t start_time;
  78. uint64_t end_time;
  79. char *name;
  80. enum test_result result;
  81. enum l_tester_stage stage;
  82. const void *test_data;
  83. l_tester_data_func_t pre_setup_func;
  84. l_tester_data_func_t setup_func;
  85. l_tester_data_func_t test_func;
  86. l_tester_data_func_t teardown_func;
  87. l_tester_data_func_t post_teardown_func;
  88. unsigned int timeout;
  89. struct l_timeout *run_timer;
  90. l_tester_destroy_func_t destroy;
  91. void *user_data;
  92. bool teardown;
  93. };
  94. static void destroy_test(void *data)
  95. {
  96. struct test_case *test = data;
  97. l_timeout_remove(test->run_timer);
  98. if (test->destroy)
  99. test->destroy(test->user_data);
  100. l_free(test->name);
  101. l_free(test);
  102. }
  103. static uint64_t get_elapsed_time(uint64_t base)
  104. {
  105. uint64_t now;
  106. now = l_time_now();
  107. return l_time_diff(base, now);
  108. }
  109. static void teardown_callback(void *user_data)
  110. {
  111. struct l_tester *tester = user_data;
  112. struct test_case *test;
  113. test = tester->test_entry->data;
  114. test->stage = L_TESTER_STAGE_TEARDOWN;
  115. test->teardown = false;
  116. print_progress(test->name, COLOR_MAGENTA, "teardown");
  117. if (test->teardown_func)
  118. test->teardown_func(test->test_data);
  119. else
  120. l_tester_teardown_complete(tester);
  121. }
  122. static void test_timeout(struct l_timeout *timer, void *user_data)
  123. {
  124. struct l_tester *tester = user_data;
  125. struct test_case *test;
  126. test = tester->test_entry->data;
  127. l_timeout_remove(timer);
  128. test->run_timer = NULL;
  129. test->result = TEST_RESULT_TIMED_OUT;
  130. print_progress(test->name, COLOR_RED, "test timed out");
  131. l_idle_oneshot(teardown_callback, tester, NULL);
  132. }
  133. static void next_test_case(struct l_tester *tester)
  134. {
  135. struct test_case *test;
  136. if (tester->test_entry)
  137. tester->test_entry = tester->test_entry->next;
  138. else
  139. tester->test_entry = l_queue_get_entries(tester->tests);
  140. if (!tester->test_entry) {
  141. if (tester->finish_callback)
  142. tester->finish_callback(tester);
  143. return;
  144. }
  145. test = tester->test_entry->data;
  146. print_progress(test->name, COLOR_BLACK, "init");
  147. test->start_time = get_elapsed_time(tester->start_time);
  148. if (test->timeout > 0)
  149. test->run_timer = l_timeout_create(test->timeout, test_timeout,
  150. tester, NULL);
  151. test->stage = L_TESTER_STAGE_PRE_SETUP;
  152. if (test->pre_setup_func)
  153. test->pre_setup_func(test->test_data);
  154. else
  155. l_tester_pre_setup_complete(tester);
  156. }
  157. static void setup_callback(void *user_data)
  158. {
  159. struct l_tester *tester = user_data;
  160. struct test_case *test = tester->test_entry->data;
  161. test->stage = L_TESTER_STAGE_SETUP;
  162. print_progress(test->name, COLOR_BLUE, "setup");
  163. if (test->setup_func)
  164. test->setup_func(test->test_data);
  165. else
  166. l_tester_setup_complete(tester);
  167. }
  168. static void run_callback(void *user_data)
  169. {
  170. struct l_tester *tester = user_data;
  171. struct test_case *test = tester->test_entry->data;
  172. test->stage = L_TESTER_STAGE_RUN;
  173. print_progress(test->name, COLOR_BLACK, "run");
  174. test->test_func(test->test_data);
  175. }
  176. static void done_callback(void *user_data)
  177. {
  178. struct l_tester *tester = user_data;
  179. struct test_case *test = tester->test_entry->data;
  180. test->end_time = get_elapsed_time(tester->start_time);
  181. print_progress(test->name, COLOR_BLACK, "done");
  182. next_test_case(tester);
  183. }
  184. LIB_EXPORT void *l_tester_get_data(struct l_tester *tester)
  185. {
  186. struct test_case *test;
  187. if (unlikely(!tester))
  188. return NULL;
  189. if (!tester->test_entry)
  190. return NULL;
  191. test = tester->test_entry->data;
  192. return test->user_data;
  193. }
  194. LIB_EXPORT void l_tester_pre_setup_complete(struct l_tester *tester)
  195. {
  196. struct test_case *test;
  197. if (unlikely(!tester))
  198. return;
  199. if (!tester->test_entry)
  200. return;
  201. test = tester->test_entry->data;
  202. if (test->stage != L_TESTER_STAGE_PRE_SETUP)
  203. return;
  204. l_idle_oneshot(setup_callback, tester, NULL);
  205. }
  206. LIB_EXPORT void l_tester_pre_setup_failed(struct l_tester *tester)
  207. {
  208. struct test_case *test;
  209. if (unlikely(!tester))
  210. return;
  211. if (!tester->test_entry)
  212. return;
  213. test = tester->test_entry->data;
  214. if (test->stage != L_TESTER_STAGE_PRE_SETUP)
  215. return;
  216. print_progress(test->name, COLOR_RED, "pre setup failed");
  217. l_timeout_remove(test->run_timer);
  218. test->run_timer = NULL;
  219. l_idle_oneshot(done_callback, tester, NULL);
  220. }
  221. LIB_EXPORT void l_tester_setup_complete(struct l_tester *tester)
  222. {
  223. struct test_case *test;
  224. if (unlikely(!tester))
  225. return;
  226. if (!tester->test_entry)
  227. return;
  228. test = tester->test_entry->data;
  229. if (test->stage != L_TESTER_STAGE_SETUP)
  230. return;
  231. print_progress(test->name, COLOR_BLUE, "setup complete");
  232. l_idle_oneshot(run_callback, tester, NULL);
  233. }
  234. LIB_EXPORT void l_tester_setup_failed(struct l_tester *tester)
  235. {
  236. struct test_case *test;
  237. if (unlikely(!tester))
  238. return;
  239. if (!tester->test_entry)
  240. return;
  241. test = tester->test_entry->data;
  242. if (test->stage != L_TESTER_STAGE_SETUP)
  243. return;
  244. test->stage = L_TESTER_STAGE_POST_TEARDOWN;
  245. l_timeout_remove(test->run_timer);
  246. test->run_timer = NULL;
  247. print_progress(test->name, COLOR_RED, "setup failed");
  248. print_progress(test->name, COLOR_MAGENTA, "teardown");
  249. test->post_teardown_func(test->test_data);
  250. }
  251. static void test_result(struct l_tester *tester, enum test_result result)
  252. {
  253. struct test_case *test;
  254. if (unlikely(!tester))
  255. return;
  256. if (!tester->test_entry)
  257. return;
  258. test = tester->test_entry->data;
  259. if (test->stage != L_TESTER_STAGE_RUN)
  260. return;
  261. l_timeout_remove(test->run_timer);
  262. test->run_timer = NULL;
  263. test->result = result;
  264. switch (result) {
  265. case TEST_RESULT_PASSED:
  266. print_progress(test->name, COLOR_GREEN, "test passed");
  267. break;
  268. case TEST_RESULT_FAILED:
  269. print_progress(test->name, COLOR_RED, "test failed");
  270. break;
  271. case TEST_RESULT_NOT_RUN:
  272. print_progress(test->name, COLOR_YELLOW, "test not run");
  273. break;
  274. case TEST_RESULT_TIMED_OUT:
  275. print_progress(test->name, COLOR_RED, "test timed out");
  276. break;
  277. }
  278. if (test->teardown)
  279. return;
  280. test->teardown = true;
  281. l_idle_oneshot(teardown_callback, tester, NULL);
  282. }
  283. LIB_EXPORT void l_tester_test_passed(struct l_tester *tester)
  284. {
  285. if (unlikely(!tester))
  286. return;
  287. test_result(tester, TEST_RESULT_PASSED);
  288. }
  289. LIB_EXPORT void l_tester_test_failed(struct l_tester *tester)
  290. {
  291. if (unlikely(!tester))
  292. return;
  293. test_result(tester, TEST_RESULT_FAILED);
  294. }
  295. LIB_EXPORT void l_tester_test_abort(struct l_tester *tester)
  296. {
  297. if (unlikely(!tester))
  298. return;
  299. test_result(tester, TEST_RESULT_NOT_RUN);
  300. }
  301. LIB_EXPORT void l_tester_teardown_complete(struct l_tester *tester)
  302. {
  303. struct test_case *test;
  304. if (unlikely(!tester))
  305. return;
  306. if (!tester->test_entry)
  307. return;
  308. test = tester->test_entry->data;
  309. if (test->stage != L_TESTER_STAGE_TEARDOWN)
  310. return;
  311. test->stage = L_TESTER_STAGE_POST_TEARDOWN;
  312. if (test->post_teardown_func)
  313. test->post_teardown_func(test->test_data);
  314. else
  315. l_tester_post_teardown_complete(tester);
  316. }
  317. LIB_EXPORT void l_tester_teardown_failed(struct l_tester *tester)
  318. {
  319. struct test_case *test;
  320. if (unlikely(!tester))
  321. return;
  322. if (!tester->test_entry)
  323. return;
  324. test = tester->test_entry->data;
  325. if (test->stage != L_TESTER_STAGE_TEARDOWN)
  326. return;
  327. test->stage = L_TESTER_STAGE_POST_TEARDOWN;
  328. l_tester_post_teardown_failed(tester);
  329. }
  330. LIB_EXPORT void l_tester_post_teardown_complete(struct l_tester *tester)
  331. {
  332. struct test_case *test;
  333. if (unlikely(!tester))
  334. return;
  335. if (!tester->test_entry)
  336. return;
  337. test = tester->test_entry->data;
  338. if (test->stage != L_TESTER_STAGE_POST_TEARDOWN)
  339. return;
  340. print_progress(test->name, COLOR_MAGENTA, "teardown complete");
  341. l_idle_oneshot(done_callback, tester, NULL);
  342. }
  343. LIB_EXPORT void l_tester_post_teardown_failed(struct l_tester *tester)
  344. {
  345. struct test_case *test;
  346. if (unlikely(!tester))
  347. return;
  348. if (!tester->test_entry)
  349. return;
  350. test = tester->test_entry->data;
  351. if (test->stage != L_TESTER_STAGE_POST_TEARDOWN)
  352. return;
  353. print_progress(test->name, COLOR_RED, "teardown failed");
  354. l_idle_oneshot(done_callback, tester, NULL);
  355. }
  356. struct wait_data {
  357. unsigned int seconds;
  358. struct test_case *test;
  359. l_tester_wait_func_t func;
  360. void *user_data;
  361. };
  362. static void wait_callback(struct l_timeout *timer, void *user_data)
  363. {
  364. struct wait_data *wait = user_data;
  365. struct test_case *test = wait->test;
  366. wait->seconds--;
  367. if (wait->seconds > 0) {
  368. print_progress(test->name, COLOR_BLACK, "%u seconds left",
  369. wait->seconds);
  370. return;
  371. }
  372. print_progress(test->name, COLOR_BLACK, "waiting done");
  373. wait->func(wait->user_data);
  374. l_free(wait);
  375. l_timeout_remove(timer);
  376. }
  377. LIB_EXPORT void l_tester_wait(struct l_tester *tester, unsigned int seconds,
  378. l_tester_wait_func_t func, void *user_data)
  379. {
  380. struct test_case *test;
  381. struct wait_data *wait;
  382. if (unlikely(!tester))
  383. return;
  384. if (!func || seconds < 1)
  385. return;
  386. if (!tester->test_entry)
  387. return;
  388. test = tester->test_entry->data;
  389. wait = l_new(struct wait_data, 1);
  390. wait->seconds = seconds;
  391. wait->test = test;
  392. wait->func = func;
  393. wait->user_data = user_data;
  394. l_timeout_create(seconds, wait_callback, wait, NULL);
  395. print_progress(test->name, COLOR_BLACK, "waiting %u seconds", seconds);
  396. }
  397. /**
  398. * l_tester_add_full:
  399. * @tester: tester instance
  400. * @name: test case name
  401. * @test_data: test data
  402. * @pre_setup_func: test pre-setup function
  403. * @setup_func: test setup function
  404. * @test_func: test function
  405. * @teardown_func: test teardown function
  406. * @teardown_func: test post-teardown function
  407. * @timeout: test teardown function
  408. * @user_data: user data
  409. * @destroy: user data destroy function
  410. *
  411. * Add a new test case.
  412. **/
  413. LIB_EXPORT void l_tester_add_full(struct l_tester *tester, const char *name,
  414. const void *test_data,
  415. l_tester_data_func_t pre_setup_func,
  416. l_tester_data_func_t setup_func,
  417. l_tester_data_func_t test_func,
  418. l_tester_data_func_t teardown_func,
  419. l_tester_data_func_t post_teardown_func,
  420. unsigned int timeout,
  421. void *user_data,
  422. l_tester_destroy_func_t destroy)
  423. {
  424. struct test_case *test;
  425. if (unlikely(!tester || !test_func))
  426. return;
  427. if (tester->prefix && !l_str_has_prefix(name, tester->prefix)) {
  428. if (destroy)
  429. destroy(user_data);
  430. return;
  431. }
  432. if (tester->substring && !strstr(name, tester->substring)) {
  433. if (destroy)
  434. destroy(user_data);
  435. return;
  436. }
  437. if (tester->list_cases) {
  438. l_info("%s", name);
  439. if (destroy)
  440. destroy(user_data);
  441. return;
  442. }
  443. test = l_new(struct test_case, 1);
  444. test->name = l_strdup(name);
  445. test->result = TEST_RESULT_NOT_RUN;
  446. test->stage = L_TESTER_STAGE_INVALID;
  447. test->test_data = test_data;
  448. test->pre_setup_func = pre_setup_func;
  449. test->setup_func = setup_func;
  450. test->test_func = test_func;
  451. test->teardown_func = teardown_func;
  452. test->post_teardown_func = post_teardown_func;
  453. test->timeout = timeout;
  454. test->destroy = destroy;
  455. test->user_data = user_data;
  456. l_queue_push_tail(tester->tests, test);
  457. }
  458. /**
  459. * l_tester_add:
  460. * @tester: tester instance
  461. * @name: test case name
  462. * @test_data: test data
  463. * @setup_func: test setup function
  464. * @test_func: test function
  465. * @teardown_func: test teardown function
  466. *
  467. * Add a new test with default settings for timeout and no pre-setup procedure.
  468. **/
  469. LIB_EXPORT void l_tester_add(struct l_tester *tester, const char *name,
  470. const void *test_data,
  471. l_tester_data_func_t setup_func,
  472. l_tester_data_func_t test_func,
  473. l_tester_data_func_t teardown_func)
  474. {
  475. l_tester_add_full(tester, name, test_data, NULL, setup_func, test_func,
  476. teardown_func, NULL, 0, NULL, NULL);
  477. }
  478. /**
  479. * l_tester_new:
  480. *
  481. * Initialize tester framework.
  482. *
  483. * Returns: new tester instance
  484. **/
  485. LIB_EXPORT struct l_tester *l_tester_new(const char *prefix,
  486. const char *substring, bool list_cases)
  487. {
  488. struct l_tester *tester = l_new(struct l_tester, 1);
  489. tester->prefix = prefix;
  490. tester->substring = substring;
  491. tester->list_cases = list_cases;
  492. tester->tests = l_queue_new();
  493. return tester;
  494. }
  495. /**
  496. * l_tester_start:
  497. * @tester: tester instance
  498. *
  499. * Kick off execution of the test queue
  500. *
  501. **/
  502. LIB_EXPORT void l_tester_start(struct l_tester *tester,
  503. l_tester_finish_func_t finish_func)
  504. {
  505. if (unlikely(!tester))
  506. return;
  507. if (!tester->tests)
  508. return;
  509. tester->finish_callback = finish_func;
  510. tester->start_time = l_time_now();
  511. next_test_case(tester);
  512. }
  513. /**
  514. * l_tester_summarize:
  515. * @tester: tester instance
  516. *
  517. * Print summary of all added test cases.
  518. *
  519. * Returns: true, if all the tests passed
  520. * false, if any of the tests failed
  521. **/
  522. LIB_EXPORT bool l_tester_summarize(struct l_tester *tester)
  523. {
  524. unsigned int not_run = 0, passed = 0, failed = 0;
  525. double execution_time;
  526. const struct l_queue_entry *entry;
  527. if (unlikely(!tester))
  528. return false;
  529. l_info(COLOR_HIGHLIGHT "%s" COLOR_OFF,
  530. "\n\nTest Summary\n------------");
  531. entry = l_queue_get_entries(tester->tests);
  532. for (; entry; entry = entry->next) {
  533. struct test_case *test = entry->data;
  534. double exec_time;
  535. exec_time = (test->end_time - test->start_time) /
  536. (double)L_USEC_PER_SEC;
  537. switch (test->result) {
  538. case TEST_RESULT_NOT_RUN:
  539. print_summary(test->name, COLOR_YELLOW, "Not Run", "");
  540. not_run++;
  541. break;
  542. case TEST_RESULT_PASSED:
  543. print_summary(test->name, COLOR_GREEN, "Passed",
  544. "%8.3f seconds", exec_time);
  545. passed++;
  546. break;
  547. case TEST_RESULT_FAILED:
  548. print_summary(test->name, COLOR_RED, "Failed",
  549. "%8.3f seconds", exec_time);
  550. failed++;
  551. break;
  552. case TEST_RESULT_TIMED_OUT:
  553. print_summary(test->name, COLOR_RED, "Timed out",
  554. "%8.3f seconds", exec_time);
  555. failed++;
  556. break;
  557. }
  558. }
  559. l_info("Total: %d, "
  560. COLOR_GREEN "Passed: %d (%.1f%%)" COLOR_OFF ", "
  561. COLOR_RED "Failed: %d" COLOR_OFF ", "
  562. COLOR_YELLOW "Not Run: %d" COLOR_OFF,
  563. not_run + passed + failed, passed,
  564. (not_run + passed + failed) ?
  565. (float) passed * 100 / (not_run + passed + failed) : 0,
  566. failed, not_run);
  567. execution_time = get_elapsed_time(tester->start_time);
  568. l_info("Overall execution time: %8.3f seconds",
  569. execution_time / (double)L_USEC_PER_SEC);
  570. return failed;
  571. }
  572. /**
  573. * l_tester_destroy:
  574. * @tester: tester instance
  575. *
  576. * Free up the teter framework resources
  577. *
  578. **/
  579. LIB_EXPORT void l_tester_destroy(struct l_tester *tester)
  580. {
  581. if (unlikely(!tester))
  582. return;
  583. l_queue_destroy(tester->tests, destroy_test);
  584. l_free(tester);
  585. }
  586. /**
  587. * l_tester_get_stage:
  588. * @tester: tester instance
  589. *
  590. * Get the current test stage
  591. *
  592. * Returns: the stage of the current test that is being processing.
  593. *
  594. **/
  595. LIB_EXPORT enum l_tester_stage l_tester_get_stage(struct l_tester *tester)
  596. {
  597. struct test_case *test;
  598. if (unlikely(!tester))
  599. return L_TESTER_STAGE_INVALID;
  600. if (!tester->test_entry)
  601. return L_TESTER_STAGE_INVALID;
  602. test = tester->test_entry->data;
  603. return test->stage;
  604. }