LTP Style Guide


SUBMITTED BY: Guest

DATE: Aug. 7, 2014, 9:57 p.m.

FORMAT: Text only

SIZE: 14.2 kB

HITS: 805

  1. LTP Style Guide
  2. ===============
  3. **********************************************************************
  4. Welcome to the *LTP Project Coding Guideline* document! This document is
  5. designed to guide committers and contributors in producing consistent, quality
  6. deterministic testcases and port testcases from other sources to LTP so that
  7. they can be easily maintained by project members and external contributors.
  8. Please refer to the *Linux Kernel Coding Guidelines* unless stated otherwise
  9. in this document.
  10. Changelog:
  11. * Initial version: Garrett Cooper <yanegomi@gmail.com>
  12. * Reformated for asciidoc: Cyril Hrubis <chrubis@suse.cz>
  13. **********************************************************************
  14. Using libltp
  15. ------------
  16. Of course the manpages should be the primary source of information in
  17. terms of how you should use libltp, but this section provides a quick
  18. set of guidelines to hit the ground running with libltp.
  19. When you can use libltp please observe the following guidelines:
  20. 1. Should I use tst_*() interface in forked processes?
  21. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  22. No, only use libltp in non-forked processes and functions +perror(3)+ /
  23. +exit(3)+ otherwise. Reason being:
  24. * Calling +tst_res()+ from multiple processes is messy.
  25. * Calling cleanup can break test execution.
  26. * Establishing a complicated scheme of tracking the testcase state
  27. for teardown is undesirable.
  28. 2. Use SAFE_* macros
  29. ~~~~~~~~~~~~~~~~~~~~
  30. Use +SAFE_*+ macros (see +safe_macros.h+) instead of bare calls to libcalls and
  31. syscalls. This will:
  32. * Reduce number of lines wasted on repeated in multiple files with
  33. error catching logic.
  34. * Ensure that error catching is consistent and informative; all
  35. +SAFE_*+ macros contain the line number of the failure as well as
  36. the file where the failure occurred by design. So unless the
  37. stack gets stomped on, you will be able to determine where
  38. the failures occurred if and when they do occur with absolute
  39. determinism.
  40. * Mute some unchecked return code warnings.
  41. 3. Don't call cleanup() from within setup()
  42. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  43. Unless you need to clean up or reset system state that wouldn't otherwise be
  44. handled by a proper release of all OS resources.
  45. This includes (but is not limited to):
  46. * Memory mapped pages.
  47. * Mounted filesystems.
  48. * File descriptors (if they're in temporary directories, etc).
  49. * Temporary files / directories.
  50. * Waiting for child processes.
  51. * +/proc+ & +/sysfs+ tunable states.
  52. You don't need to clean up the following:
  53. * +malloc(3)+'ed memory.
  54. * Read-only file descriptors in persistent paths (i.e. not
  55. temporary directories).
  56. Please be aware of some of the caveats surrounding forking,
  57. exec'ing and descriptor inheritance, etc.
  58. 4. Call APIs that don't require freeing up resources on failure first
  59. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  60. * +tst_require_root(NULL)+
  61. * +tst_sig(...)+
  62. * +malloc(3)+
  63. * +tst_tmpdir()+
  64. That way you can simplify your setup and avoid calling cleanup whenever
  65. possible!
  66. 5. If the test needs to run as root
  67. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  68. If the test need to run as root, check to make sure that you're root
  69. *before doing any other setup* via +tst_require_root(...)+.
  70. 6. No custom reporting APIs
  71. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  72. Don't roll your own reporting APIs unless you're porting testcases or are
  73. concerned about portability.
  74. 7. Handle TBROK and TFAIL correctly
  75. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  76. Use +TBROK+ when an unexpected failure unrelated to the goal of the testcase
  77. occurred, and use +TFAIL+ when an unexpected failure related to the goal of
  78. the testcase occurred.
  79. 8. Don't roll your own syscall numbers
  80. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  81. Header +linux_syscall_numbers.h+ exists for this purpose and does a pretty
  82. dang good job.
  83. 9. Keep errors as short and sweet as possible
  84. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  85. For example:
  86. [source,c]
  87. ----------------------------------------------------
  88. if (fork() == -1)
  89. tst_brkm(TBROK, cleanup, "fork failed");
  90. /* or */
  91. if (fork() == -1)
  92. tst_brkm(TBROK, cleanup, "fork # 1 failed");
  93. if (fork() == -1)
  94. tst_brkm(TBROK, cleanup, "fork # 2 failed");
  95. ----------------------------------------------------
  96. If you can't determine where the failure has occurred in a testcase based on
  97. the entire content of the failure messages, then determining the cause of
  98. failure may be impossible.
  99. 10. Reporting errno and the TEST() macro
  100. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  101. Don't roll your own +errno+ / +strerror()+ printout when you use +tst_res()+
  102. calls. Use either +TERRNO+ or +TTERRNO+. Similarly, if a testcase passed and
  103. it's obvious why it passed (for example a function call returns +0+ or
  104. +TEST_RETURN == 0+).
  105. [source,c]
  106. -------------------------------------------------------------------------------
  107. /* Example without TEST(...) macro */
  108. exp_errno = ENOENT;
  109. if (fn() == -1) {
  110. /*
  111. * Using TERRNO here is valid because the error case
  112. * isn't static.
  113. */
  114. if (exp_errno == ENOENT)
  115. tst_resm(TPASS|TERRNO,
  116. "fn failed as expected");
  117. /*
  118. * Using strerror(TEST_ERRNO) here is valid because
  119. * the error case isn't static.
  120. */
  121. else
  122. tst_resm(TFAIL|TERRNO,
  123. "fn failed unexpectedly; expected "
  124. "%d - %s",
  125. exp_errno, strerror(exp_errno));
  126. } else
  127. tst_resm(TBROK, "fn passed unexpectedly");
  128. /* Example using TEST(...) macro */
  129. TEST(fn());
  130. if (TEST_RETURN == 0)
  131. tst_resm(TPASS, "fn passed as expected");
  132. else
  133. tst_resm(TFAIL|TTERRNO, "fn failed");
  134. -------------------------------------------------------------------------------
  135. [NOTE]
  136. The +TEST()+ macro is not thread-safe as it saves return value and errno into
  137. global variable.
  138. 12. Use tst_brkm() when possible
  139. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  140. Use...
  141. [source,c]
  142. ------------------------------
  143. tst_brkm(TBROK, cleanup, ...);
  144. ------------------------------
  145. ...instead of...
  146. [source,c]
  147. ------------------------------
  148. tst_resm(TBROK, ...);
  149. cleanup();
  150. tst_exit();
  151. ------------------------------
  152. [NOTE]
  153. As you see the +tst_brkm()+ no longer requires non +NULL+ cleanup_fn argument
  154. in order to call +tst_exit()+.
  155. 13. Indentation
  156. ~~~~~~~~~~~~~~~
  157. Use hard tabs for first-level indentation, and 4 spaces for every line longer
  158. than 80 columns. Use a linebreak with string constants in format functions
  159. like +*printf()+, the +tst_res()+ APIs, etc.
  160. Example:
  161. [source,c]
  162. -------------------------------------------------------------------------------
  163. if ((this_is_a_poorly_formed_really_long_variable = function_call()) == NULL &&
  164. statement1() && i < j && l != 5) {
  165. /* Use tabs here */
  166. printf("The rain in Spain falls mainly in the plain.\nThe quick brown "
  167. "fox jumped over the lazy yellow dog\n");
  168. tst_resm(TPASS,
  169. "Half would turn and fight. The other half would try to swim "
  170. "across. But my uncle told me about a few that... they'd swim "
  171. "halfway out, turn with the current, and ride it all the way out "
  172. "to sea. Fisherman would find them a mile offshore, swimming.");
  173. }
  174. -------------------------------------------------------------------------------
  175. 14. System headers
  176. ~~~~~~~~~~~~~~~~~~
  177. Don't use +linux/+ headers if at all possible. Usually they are replaced with
  178. +sys/+ headers as things work their way into glibc. Furthermore, +linux/+
  179. headers get shuffled around a lot more than their +sys/+ counterparts it
  180. seems.
  181. 15. Singnal handlers
  182. ~~~~~~~~~~~~~~~~~~~~
  183. Avoid doing tricky things in signal handlers. Calling most of the libc
  184. functions from signal handler may lead to deadlocks or memory corruption. If
  185. you really need to do anything more complicated that +should_run = 0;+ in your
  186. signal handler consult +man 7 signal+ for async-signal-safe functions.
  187. Porting Testcases
  188. -----------------
  189. If one of the following is true...
  190. 1. You are porting a testcase directly to LTP which doesn't need to
  191. be ported outside of Linux.
  192. 2. The beforementioned project or source is no longer contributing
  193. changes to the testcases.
  194. Then please fully port the testcase(s) to LTP. Otherwise, add robust porting
  195. shims around the testcases using libltp APIs to reduce longterm maintenance
  196. and leave the sources alone so it's easier to sync the testcases from the
  197. upstream source.
  198. New Testcases
  199. -------------
  200. 1. Always use libltp for Linux centric tests. No ifs, ands, or buts
  201. about it.
  202. 2. Sort headers, like:
  203. [source,c]
  204. ---------------------------------------------------------------------------
  205. /*
  206. * sys/types.h is usually (but not always) required by POSIX
  207. * APIs.
  208. */
  209. #include <sys/types.h>
  210. /* sys headers, alphabetical order. */
  211. /* directory prefixed libc headers. */
  212. /* non-directory prefixed libc headers. */
  213. /* directory prefixed non-libc (third-party) headers. */
  214. /* non-directory prefixed non-libc (third-party) headers. */
  215. /* Sourcebase relative includes. */
  216. /* e.g. */
  217. #include <sys/types.h>
  218. #include <sys/stat.h>
  219. #include <netinet/icmp.h>
  220. #include <stdio.h>
  221. #include <dbus-1.0/dbus/dbus.h>
  222. #include <archive.h>
  223. #include "foo/bar.h"
  224. #include "test.h"
  225. ---------------------------------------------------------------------------
  226. 3. Comments
  227. ~~~~~~~~~~~
  228. Do not use obvious comments ("child process", "parse options", etc). This
  229. leads to visual comment noise pollution.
  230. 4. Inline assigments
  231. ~~~~~~~~~~~~~~~~~~~~
  232. Don't do inline assignment, i.e.
  233. [source,c]
  234. ---------------------------------------------------------------------------
  235. int foo = 0;
  236. Use separate statements:
  237. int foo;
  238. foo = 0;
  239. ---------------------------------------------------------------------------
  240. The only exception to this is when you define global variables.
  241. 5. How to assert test is running as root?
  242. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  243. Your testcase should be runnable as root and non-root. What does that mean? It
  244. should fail gracefull as non-root if it has insufficient privileges, or use
  245. +tst_require_root(NULL)+ if root access is absolutely required.
  246. A lot of newer testcases don't honor this fact and it causes random
  247. unnecessary errors when run as non-privileged users.
  248. 6. Do I need to create temporary directory?
  249. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  250. Use +tst_tmpdir()+ if your testcase will:
  251. * Create temporary files.
  252. * Dump core.
  253. * Etc. Otherwise, don't bother with the API.
  254. [NOTE]
  255. If you created temporary directory with +tst_tmpdir()+ don't forget to call
  256. +tst_rmdir()+ when the test is cleaning up. This is *NOT* done automatically.
  257. 7. Setting up signal handlers
  258. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  259. Use +tst_sig()+ instead of bothering with sigaction / signal. This reduces
  260. potential of leaving a mess around if your test doesn't exit cleanly (now,
  261. there are some signals that can't be blocked, i.e. +SIGKILL+ and +SIGSTOP+,
  262. but the rest can be caught).
  263. 8. Basic template
  264. ~~~~~~~~~~~~~~~~~
  265. The following is a basic testcase template:
  266. [source,c]
  267. ---------------------------------------------------------------------------
  268. #include "test.h"
  269. char *TCID = "testname";
  270. int TST_TOTAL = /* ... */
  271. static void setup(void)
  272. {
  273. /* ... */
  274. tst_require_root(NULL);
  275. tst_tmpdir();
  276. /* ... */
  277. TEST_PAUSE;
  278. }
  279. static void cleanup(void)
  280. {
  281. TEST_CLEANUP;
  282. /* ... */
  283. tst_rmdir();
  284. }
  285. int main(void)
  286. {
  287. /* ... */
  288. setup(); /* Optional */
  289. cleanup(); /* Optional */
  290. tst_exit(); /* DON'T FORGET THIS -- this must be last! */
  291. }
  292. ---------------------------------------------------------------------------
  293. Fixing Legacy Testcases
  294. -----------------------
  295. Unless you interested in exercising self-masochism, do minimal changes
  296. to testcases when fixing them so it's easier to track potential
  297. breakage in testcases after a commit is made (mistakes happen and no
  298. one is perfect). If it works after you fix it -- great -- move on.
  299. It's more the job of the committers / maintainers to do things like
  300. style commits.
  301. It's better to focus on adding more content to LTP as a contributor
  302. and fixing functional issues with existing tests than it is worrying
  303. about whitespace, unnecessary pedanticness of function calls, etc.
  304. As ugly as the style is in the surrounding code may be, stick to it.
  305. Otherwise anyone reading the code will cringe at the chaos and
  306. non-uniform `style', and it will be more difficult to fix later.
  307. Fixing Open Posix Testsuite
  308. ---------------------------
  309. The *Open Posix Testsuite* does not use libltp interface and moreover aims to
  310. be usable on any *POSIX* compatible operating system.
  311. So *NO* Linux, BSD specific syscalls there.
  312. Contribution to the project
  313. ---------------------------
  314. Since CVS is effectively dead for LTP proper, we ask that you submit
  315. patches that are git friendly and patchable.
  316. Guidelines for submitting patches are as follows:
  317. 1. Use +git commit -s+ . You know you want to ;) .. (you may need to
  318. submit a correct signed-off-by line, e.g. use git config first).
  319. 2. Provide a short (<= 50 character) description of the commit.
  320. 3. Provide a little more terse (1-2 paragraph maximum, <= 72 character
  321. lines) description of what the commit does.
  322. 4. Format the email with +git format-patch --attach+ command .
  323. Example of a commit message:
  324. -------------------------------------------------------------------
  325. Short description of my commit.
  326. This is a much longer description of my commit. Blah blah blah blah
  327. blah blah blah blah blah.
  328. Signed-off-by: John Smith <dev@null>
  329. -------------------------------------------------------------------

comments powered by Disqus