TESTING.asciidoc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. [[Testing Framework Cheatsheet]]
  2. = Testing
  3. [partintro]
  4. Elasticsearch uses jUnit for testing, it also uses randomness in the
  5. tests, that can be set using a seed, the following is a cheatsheet of
  6. options for running the tests for ES.
  7. == Creating packages
  8. To create a distribution without running the tests, simply run the
  9. following:
  10. -----------------------------
  11. gradle assemble
  12. -----------------------------
  13. === Running Elasticsearch from a checkout
  14. In order to run Elasticsearch from source without building a package, you can
  15. run it using Gradle:
  16. -------------------------------------
  17. gradle run
  18. -------------------------------------
  19. or to attach a remote debugger, run it as:
  20. -------------------------------------
  21. gradle run --debug-jvm
  22. -------------------------------------
  23. === Test case filtering.
  24. - `tests.class` is a class-filtering shell-like glob pattern,
  25. - `tests.method` is a method-filtering glob pattern.
  26. Run a single test case (variants)
  27. ----------------------------------------------------------
  28. gradle test -Dtests.class=org.elasticsearch.package.ClassName
  29. gradle test "-Dtests.class=*.ClassName"
  30. ----------------------------------------------------------
  31. Run all tests in a package and sub-packages
  32. ----------------------------------------------------
  33. gradle test "-Dtests.class=org.elasticsearch.package.*"
  34. ----------------------------------------------------
  35. Run any test methods that contain 'esi' (like: ...r*esi*ze...).
  36. -------------------------------
  37. gradle test "-Dtests.method=*esi*"
  38. -------------------------------
  39. You can also filter tests by certain annotations ie:
  40. * `@Nightly` - tests that only run in nightly builds (disabled by default)
  41. * `@Backwards` - backwards compatibility tests (disabled by default)
  42. * `@AwaitsFix` - tests that are waiting for a bugfix (disabled by default)
  43. * `@BadApple` - tests that are known to fail randomly (disabled by default)
  44. Those annotation names can be combined into a filter expression like:
  45. ------------------------------------------------
  46. gradle test -Dtests.filter="@nightly and not @backwards"
  47. ------------------------------------------------
  48. to run all nightly test but not the ones that are backwards tests. `tests.filter` supports
  49. the boolean operators `and, or, not` and grouping ie:
  50. ---------------------------------------------------------------
  51. gradle test -Dtests.filter="@nightly and not(@badapple or @backwards)"
  52. ---------------------------------------------------------------
  53. === Seed and repetitions.
  54. Run with a given seed (seed is a hex-encoded long).
  55. ------------------------------
  56. gradle test -Dtests.seed=DEADBEEF
  57. ------------------------------
  58. === Repeats _all_ tests of ClassName N times.
  59. Every test repetition will have a different method seed
  60. (derived from a single random master seed).
  61. --------------------------------------------------
  62. gradle test -Dtests.iters=N -Dtests.class=*.ClassName
  63. --------------------------------------------------
  64. === Repeats _all_ tests of ClassName N times.
  65. Every test repetition will have exactly the same master (0xdead) and
  66. method-level (0xbeef) seed.
  67. ------------------------------------------------------------------------
  68. gradle test -Dtests.iters=N -Dtests.class=*.ClassName -Dtests.seed=DEAD:BEEF
  69. ------------------------------------------------------------------------
  70. === Repeats a given test N times
  71. (note the filters - individual test repetitions are given suffixes,
  72. ie: testFoo[0], testFoo[1], etc... so using testmethod or tests.method
  73. ending in a glob is necessary to ensure iterations are run).
  74. -------------------------------------------------------------------------
  75. gradle test -Dtests.iters=N -Dtests.class=*.ClassName -Dtests.method=mytest*
  76. -------------------------------------------------------------------------
  77. Repeats N times but skips any tests after the first failure or M initial failures.
  78. -------------------------------------------------------------
  79. gradle test -Dtests.iters=N -Dtests.failfast=true -Dtestcase=...
  80. gradle test -Dtests.iters=N -Dtests.maxfailures=M -Dtestcase=...
  81. -------------------------------------------------------------
  82. === Test groups.
  83. Test groups can be enabled or disabled (true/false).
  84. Default value provided below in [brackets].
  85. ------------------------------------------------------------------
  86. gradle test -Dtests.nightly=[false] - nightly test group (@Nightly)
  87. gradle test -Dtests.weekly=[false] - weekly tests (@Weekly)
  88. gradle test -Dtests.awaitsfix=[false] - known issue (@AwaitsFix)
  89. ------------------------------------------------------------------
  90. === Load balancing and caches.
  91. By default the tests run on up to 4 JVMs based on the number of cores. If you
  92. want to explicitly specify the number of JVMs you can do so on the command
  93. line:
  94. ----------------------------
  95. gradle test -Dtests.jvms=8
  96. ----------------------------
  97. Or in `~/.gradle/gradle.properties`:
  98. ----------------------------
  99. systemProp.tests.jvms=8
  100. ----------------------------
  101. Its difficult to pick the "right" number here. Hypercores don't count for CPU
  102. intensive tests and you should leave some slack for JVM-interal threads like
  103. the garbage collector. And you have to have enough RAM to handle each JVM.
  104. === Test compatibility.
  105. It is possible to provide a version that allows to adapt the tests behaviour
  106. to older features or bugs that have been changed or fixed in the meantime.
  107. -----------------------------------------
  108. gradle test -Dtests.compatibility=1.0.0
  109. -----------------------------------------
  110. === Miscellaneous.
  111. Run all tests without stopping on errors (inspect log files).
  112. -----------------------------------------
  113. gradle test -Dtests.haltonfailure=false
  114. -----------------------------------------
  115. Run more verbose output (slave JVM parameters, etc.).
  116. ----------------------
  117. gradle test -verbose
  118. ----------------------
  119. Change the default suite timeout to 5 seconds for all
  120. tests (note the exclamation mark).
  121. ---------------------------------------
  122. gradle test -Dtests.timeoutSuite=5000! ...
  123. ---------------------------------------
  124. Change the logging level of ES (not gradle)
  125. --------------------------------
  126. gradle test -Dtests.es.logger.level=DEBUG
  127. --------------------------------
  128. Print all the logging output from the test runs to the commandline
  129. even if tests are passing.
  130. ------------------------------
  131. gradle test -Dtests.output=always
  132. ------------------------------
  133. Configure the heap size.
  134. ------------------------------
  135. gradle test -Dtests.heap.size=512m
  136. ------------------------------
  137. Pass arbitrary jvm arguments.
  138. ------------------------------
  139. # specify heap dump path
  140. gradle test -Dtests.jvm.argline="-XX:HeapDumpPath=/path/to/heapdumps"
  141. # enable gc logging
  142. gradle test -Dtests.jvm.argline="-verbose:gc"
  143. # enable security debugging
  144. gradle test -Dtests.jvm.argline="-Djava.security.debug=access,failure"
  145. ------------------------------
  146. == Backwards Compatibility Tests
  147. Running backwards compatibility tests is disabled by default since it
  148. requires a release version of elasticsearch to be present on the test system.
  149. To run backwards compatibilty tests untar or unzip a release and run the tests
  150. with the following command:
  151. ---------------------------------------------------------------------------
  152. gradle test -Dtests.filter="@backwards" -Dtests.bwc.version=x.y.z -Dtests.bwc.path=/path/to/elasticsearch -Dtests.security.manager=false
  153. ---------------------------------------------------------------------------
  154. Note that backwards tests must be run with security manager disabled.
  155. If the elasticsearch release is placed under `./backwards/elasticsearch-x.y.z` the path
  156. can be omitted:
  157. ---------------------------------------------------------------------------
  158. gradle test -Dtests.filter="@backwards" -Dtests.bwc.version=x.y.z -Dtests.security.manager=false
  159. ---------------------------------------------------------------------------
  160. To setup the bwc test environment execute the following steps (provided you are
  161. already in your elasticsearch clone):
  162. ---------------------------------------------------------------------------
  163. $ mkdir backwards && cd backwards
  164. $ curl -O https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.2.1.tar.gz
  165. $ tar -xzf elasticsearch-1.2.1.tar.gz
  166. ---------------------------------------------------------------------------
  167. == Running verification tasks
  168. To run all verification tasks, including static checks, unit tests, and integration tests:
  169. ---------------------------------------------------------------------------
  170. gradle check
  171. ---------------------------------------------------------------------------
  172. Note that this will also run the unit tests and precommit tasks first. If you want to just
  173. run the integration tests (because you are debugging them):
  174. ---------------------------------------------------------------------------
  175. gradle integTest
  176. ---------------------------------------------------------------------------
  177. If you want to just run the precommit checks:
  178. ---------------------------------------------------------------------------
  179. gradle precommit
  180. ---------------------------------------------------------------------------
  181. == Testing the REST layer
  182. The available integration tests make use of the java API to communicate with
  183. the elasticsearch nodes, using the internal binary transport (port 9300 by
  184. default).
  185. The REST layer is tested through specific tests that are shared between all
  186. the elasticsearch official clients and consist of YAML files that describe the
  187. operations to be executed and the obtained results that need to be tested.
  188. The REST tests are run automatically when executing the "gradle check" command. To run only the
  189. REST tests use the following command:
  190. ---------------------------------------------------------------------------
  191. gradle :distribution:integ-test-zip:integTest \
  192. -Dtests.class="org.elasticsearch.test.rest.*Yaml*IT"
  193. ---------------------------------------------------------------------------
  194. A specific test case can be run with
  195. ---------------------------------------------------------------------------
  196. gradle :distribution:integ-test-zip:integTest \
  197. -Dtests.class="org.elasticsearch.test.rest.*Yaml*IT" \
  198. -Dtests.method="test {p0=cat.shards/10_basic/Help}"
  199. ---------------------------------------------------------------------------
  200. `*Yaml*IT` are the executable test classes that runs all the
  201. yaml suites available within the `rest-api-spec` folder.
  202. The REST tests support all the options provided by the randomized runner, plus the following:
  203. * `tests.rest[true|false]`: determines whether the REST tests need to be run (default) or not.
  204. * `tests.rest.suite`: comma separated paths of the test suites to be run
  205. (by default loaded from /rest-api-spec/test). It is possible to run only a subset
  206. of the tests providing a sub-folder or even a single yaml file (the default
  207. /rest-api-spec/test prefix is optional when files are loaded from classpath)
  208. e.g. -Dtests.rest.suite=index,get,create/10_with_id
  209. * `tests.rest.blacklist`: comma separated globs that identify tests that are
  210. blacklisted and need to be skipped
  211. e.g. -Dtests.rest.blacklist=index/*/Index document,get/10_basic/*
  212. * `tests.rest.spec`: REST spec path (default /rest-api-spec/api)
  213. Note that the REST tests, like all the integration tests, can be run against an external
  214. cluster by specifying the `tests.cluster` property, which if present needs to contain a
  215. comma separated list of nodes to connect to (e.g. localhost:9300). A transport client will
  216. be created based on that and used for all the before|after test operations, and to extract
  217. the http addresses of the nodes so that REST requests can be sent to them.
  218. == Testing scripts
  219. The simplest way to test scripts and the packaged distributions is to use
  220. Vagrant. You can get started by following there five easy steps:
  221. . Install Virtual Box and Vagrant.
  222. . (Optional) Install vagrant-cachier to squeeze a bit more performance out of
  223. the process:
  224. --------------------------------------
  225. vagrant plugin install vagrant-cachier
  226. --------------------------------------
  227. . Validate your installed dependencies:
  228. -------------------------------------
  229. gradle :qa:vagrant:vagrantCheckVersion
  230. -------------------------------------
  231. . Download and smoke test the VMs with `gradle vagrantSmokeTest` or
  232. `gradle -Pvagrant.boxes=all vagrantSmokeTest`. The first time you run this it will
  233. download the base images and provision the boxes and immediately quit. If you
  234. you this again it'll skip the download step.
  235. . Run the tests with `gradle packagingTest`. This will cause gradle to build
  236. the tar, zip, and deb packages and all the plugins. It will then run the tests
  237. on ubuntu-1404 and centos-7. We chose those two distributions as the default
  238. because they cover deb and rpm packaging and SyvVinit and systemd.
  239. You can run on all the VMs by running `gradle -Pvagrant.boxes=all packagingTest`.
  240. You can run a particular VM with a command like
  241. `gradle -Pvagrant.boxes=oel-7 packagingTest`. See `gradle tasks` for a complete
  242. list of available vagrant boxes for testing. It's important to know that if you
  243. ctrl-c any of these `gradle` commands then the boxes will remain running and
  244. you'll have to terminate them with 'gradle stop'.
  245. All the regular vagrant commands should just work so you can get a shell in a
  246. VM running trusty by running
  247. `vagrant up ubuntu-1404 --provider virtualbox && vagrant ssh ubuntu-1404`.
  248. These are the linux flavors the Vagrantfile currently supports:
  249. * ubuntu-1204 aka precise
  250. * ubuntu-1404 aka trusty
  251. * ubuntu-1604 aka xenial
  252. * debian-8 aka jessie, the current debian stable distribution
  253. * centos-6
  254. * centos-7
  255. * fedora-24
  256. * oel-6 aka Oracle Enterprise Linux 6
  257. * oel-7 aka Oracle Enterprise Linux 7
  258. * sles-12
  259. * opensuse-13
  260. We're missing the following from the support matrix because there aren't high
  261. quality boxes available in vagrant atlas:
  262. * sles-11
  263. We're missing the follow because our tests are very linux/bash centric:
  264. * Windows Server 2012
  265. It's important to think of VMs like cattle. If they become lame you just shoot
  266. them and let vagrant reprovision them. Say you've hosed your precise VM:
  267. ----------------------------------------------------
  268. vagrant ssh ubuntu-1404 -c 'sudo rm -rf /bin'; echo oops
  269. ----------------------------------------------------
  270. All you've got to do to get another one is
  271. ----------------------------------------------
  272. vagrant destroy -f ubuntu-1404 && vagrant up ubuntu-1404 --provider virtualbox
  273. ----------------------------------------------
  274. The whole process takes a minute and a half on a modern laptop, two and a half
  275. without vagrant-cachier.
  276. Its possible that some downloads will fail and it'll be impossible to restart
  277. them. This is a bug in vagrant. See the instructions here for how to work
  278. around it:
  279. https://github.com/mitchellh/vagrant/issues/4479
  280. Some vagrant commands will work on all VMs at once:
  281. ------------------
  282. vagrant halt
  283. vagrant destroy -f
  284. ------------------
  285. `vagrant up` would normally start all the VMs but we've prevented that because
  286. that'd consume a ton of ram.
  287. == Testing scripts more directly
  288. In general its best to stick to testing in vagrant because the bats scripts are
  289. destructive. When working with a single package it's generally faster to run its
  290. tests in a tighter loop than gradle provides. In one window:
  291. --------------------------------
  292. gradle :distribution:rpm:assemble
  293. --------------------------------
  294. and in another window:
  295. ----------------------------------------------------
  296. vagrant up centos-7 --provider virtualbox && vagrant ssh centos-7
  297. cd $BATS_ARCHIVES
  298. sudo -E bats $BATS_TESTS/*rpm*.bats
  299. ----------------------------------------------------
  300. If you wanted to retest all the release artifacts on a single VM you could:
  301. -------------------------------------------------
  302. gradle vagrantSetUp
  303. vagrant up ubuntu-1404 --provider virtualbox && vagrant ssh ubuntu-1404
  304. cd $BATS_ARCHIVES
  305. sudo -E bats $BATS_TESTS/*.bats
  306. -------------------------------------------------
  307. Note: Starting vagrant VM outside of the elasticsearch folder requires to
  308. indicates the folder that contains the Vagrantfile using the VAGRANT_CWD
  309. environment variable:
  310. -------------------------------------------------
  311. gradle vagrantSetUp
  312. VAGRANT_CWD=/path/to/elasticsearch vagrant up centos-7 --provider virtualbox
  313. -------------------------------------------------
  314. == Coverage analysis
  315. Tests can be run instrumented with jacoco to produce a coverage report in
  316. `target/site/jacoco/`.
  317. Unit test coverage:
  318. ---------------------------------------------------------------------------
  319. mvn -Dtests.coverage test jacoco:report
  320. ---------------------------------------------------------------------------
  321. Integration test coverage:
  322. ---------------------------------------------------------------------------
  323. mvn -Dtests.coverage -Dskip.unit.tests verify jacoco:report
  324. ---------------------------------------------------------------------------
  325. Combined (Unit+Integration) coverage:
  326. ---------------------------------------------------------------------------
  327. mvn -Dtests.coverage verify jacoco:report
  328. ---------------------------------------------------------------------------
  329. == Debugging from an IDE
  330. If you want to run elasticsearch from your IDE, the `gradle run` task
  331. supports a remote debugging option:
  332. ---------------------------------------------------------------------------
  333. gradle run --debug-jvm
  334. ---------------------------------------------------------------------------
  335. == Building with extra plugins
  336. Additional plugins may be built alongside elasticsearch, where their
  337. dependency on elasticsearch will be substituted with the local elasticsearch
  338. build. To add your plugin, create a directory called elasticsearch-extra as
  339. a sibling of elasticsearch. Checkout your plugin underneath elasticsearch-extra
  340. and the build will automatically pick it up. You can verify the plugin is
  341. included as part of the build by checking the projects of the build.
  342. ---------------------------------------------------------------------------
  343. gradle projects
  344. ---------------------------------------------------------------------------