TESTING.asciidoc 18 KB

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