TESTING.asciidoc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. ./gradlew 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. ./gradlew run
  18. -------------------------------------
  19. === Test case filtering.
  20. - `tests.class` is a class-filtering shell-like glob pattern,
  21. - `tests.method` is a method-filtering glob pattern.
  22. Run a single test case (variants)
  23. ----------------------------------------------------------
  24. ./gradlew test -Dtests.class=org.elasticsearch.package.ClassName
  25. ./gradlew test "-Dtests.class=*.ClassName"
  26. ----------------------------------------------------------
  27. Run all tests in a package and sub-packages
  28. ----------------------------------------------------
  29. ./gradlew test "-Dtests.class=org.elasticsearch.package.*"
  30. ----------------------------------------------------
  31. Run any test methods that contain 'esi' (like: ...r*esi*ze...).
  32. -------------------------------
  33. ./gradlew test "-Dtests.method=*esi*"
  34. -------------------------------
  35. You can also filter tests by certain annotations ie:
  36. * `@Nightly` - tests that only run in nightly builds (disabled by default)
  37. * `@Backwards` - backwards compatibility tests (disabled by default)
  38. * `@AwaitsFix` - tests that are waiting for a bugfix (disabled by default)
  39. * `@BadApple` - tests that are known to fail randomly (disabled by default)
  40. Those annotation names can be combined into a filter expression like:
  41. ------------------------------------------------
  42. ./gradlew test -Dtests.filter="@nightly and not @backwards"
  43. ------------------------------------------------
  44. to run all nightly test but not the ones that are backwards tests. `tests.filter` supports
  45. the boolean operators `and, or, not` and grouping ie:
  46. ---------------------------------------------------------------
  47. ./gradlew test -Dtests.filter="@nightly and not(@badapple or @backwards)"
  48. ---------------------------------------------------------------
  49. === Seed and repetitions.
  50. Run with a given seed (seed is a hex-encoded long).
  51. ------------------------------
  52. ./gradlew test -Dtests.seed=DEADBEEF
  53. ------------------------------
  54. === Repeats _all_ tests of ClassName N times.
  55. Every test repetition will have a different method seed
  56. (derived from a single random master seed).
  57. --------------------------------------------------
  58. ./gradlew test -Dtests.iters=N -Dtests.class=*.ClassName
  59. --------------------------------------------------
  60. === Repeats _all_ tests of ClassName N times.
  61. Every test repetition will have exactly the same master (0xdead) and
  62. method-level (0xbeef) seed.
  63. ------------------------------------------------------------------------
  64. ./gradlew test -Dtests.iters=N -Dtests.class=*.ClassName -Dtests.seed=DEAD:BEEF
  65. ------------------------------------------------------------------------
  66. === Repeats a given test N times
  67. (note the filters - individual test repetitions are given suffixes,
  68. ie: testFoo[0], testFoo[1], etc... so using testmethod or tests.method
  69. ending in a glob is necessary to ensure iterations are run).
  70. -------------------------------------------------------------------------
  71. ./gradlew test -Dtests.iters=N -Dtests.class=*.ClassName -Dtests.method=mytest*
  72. -------------------------------------------------------------------------
  73. Repeats N times but skips any tests after the first failure or M initial failures.
  74. -------------------------------------------------------------
  75. ./gradlew test -Dtests.iters=N -Dtests.failfast=true -Dtestcase=...
  76. ./gradlew test -Dtests.iters=N -Dtests.maxfailures=M -Dtestcase=...
  77. -------------------------------------------------------------
  78. === Test groups.
  79. Test groups can be enabled or disabled (true/false).
  80. Default value provided below in [brackets].
  81. ------------------------------------------------------------------
  82. ./gradlew test -Dtests.nightly=[false] - nightly test group (@Nightly)
  83. ./gradlew test -Dtests.weekly=[false] - weekly tests (@Weekly)
  84. ./gradlew test -Dtests.awaitsfix=[false] - known issue (@AwaitsFix)
  85. ------------------------------------------------------------------
  86. === Load balancing and caches.
  87. By default the tests run on up to 4 JVMs based on the number of cores. If you
  88. want to explicitly specify the number of JVMs you can do so on the command
  89. line:
  90. ----------------------------
  91. ./gradlew test -Dtests.jvms=8
  92. ----------------------------
  93. Or in `~/.gradle/gradle.properties`:
  94. ----------------------------
  95. systemProp.tests.jvms=8
  96. ----------------------------
  97. Its difficult to pick the "right" number here. Hypercores don't count for CPU
  98. intensive tests and you should leave some slack for JVM-interal threads like
  99. the garbage collector. And you have to have enough RAM to handle each JVM.
  100. === Test compatibility.
  101. It is possible to provide a version that allows to adapt the tests behaviour
  102. to older features or bugs that have been changed or fixed in the meantime.
  103. -----------------------------------------
  104. ./gradlew test -Dtests.compatibility=1.0.0
  105. -----------------------------------------
  106. === Miscellaneous.
  107. Run all tests without stopping on errors (inspect log files).
  108. -----------------------------------------
  109. ./gradlew test -Dtests.haltonfailure=false
  110. -----------------------------------------
  111. Run more verbose output (slave JVM parameters, etc.).
  112. ----------------------
  113. ./gradlew test -verbose
  114. ----------------------
  115. Change the default suite timeout to 5 seconds for all
  116. tests (note the exclamation mark).
  117. ---------------------------------------
  118. ./gradlew test -Dtests.timeoutSuite=5000! ...
  119. ---------------------------------------
  120. Change the logging level of ES (not Gradle)
  121. --------------------------------
  122. ./gradlew test -Dtests.es.logger.level=DEBUG
  123. --------------------------------
  124. Print all the logging output from the test runs to the commandline
  125. even if tests are passing.
  126. ------------------------------
  127. ./gradlew test -Dtests.output=always
  128. ------------------------------
  129. Configure the heap size.
  130. ------------------------------
  131. ./gradlew test -Dtests.heap.size=512m
  132. ------------------------------
  133. Pass arbitrary jvm arguments.
  134. ------------------------------
  135. # specify heap dump path
  136. ./gradlew test -Dtests.jvm.argline="-XX:HeapDumpPath=/path/to/heapdumps"
  137. # enable gc logging
  138. ./gradlew test -Dtests.jvm.argline="-verbose:gc"
  139. # enable security debugging
  140. ./gradlew test -Dtests.jvm.argline="-Djava.security.debug=access,failure"
  141. ------------------------------
  142. == Backwards Compatibility Tests
  143. Running backwards compatibility tests is disabled by default since it
  144. requires a release version of elasticsearch to be present on the test system.
  145. To run backwards compatibilty tests untar or unzip a release and run the tests
  146. with the following command:
  147. ---------------------------------------------------------------------------
  148. ./gradlew test -Dtests.filter="@backwards" -Dtests.bwc.version=x.y.z -Dtests.bwc.path=/path/to/elasticsearch -Dtests.security.manager=false
  149. ---------------------------------------------------------------------------
  150. Note that backwards tests must be run with security manager disabled.
  151. If the elasticsearch release is placed under `./backwards/elasticsearch-x.y.z` the path
  152. can be omitted:
  153. ---------------------------------------------------------------------------
  154. ./gradlew test -Dtests.filter="@backwards" -Dtests.bwc.version=x.y.z -Dtests.security.manager=false
  155. ---------------------------------------------------------------------------
  156. To setup the bwc test environment execute the following steps (provided you are
  157. already in your elasticsearch clone):
  158. ---------------------------------------------------------------------------
  159. $ mkdir backwards && cd backwards
  160. $ curl -O https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.2.1.tar.gz
  161. $ tar -xzf elasticsearch-1.2.1.tar.gz
  162. ---------------------------------------------------------------------------
  163. == Running verification tasks
  164. To run all verification tasks, including static checks, unit tests, and integration tests:
  165. ---------------------------------------------------------------------------
  166. ./gradlew check
  167. ---------------------------------------------------------------------------
  168. Note that this will also run the unit tests and precommit tasks first. If you want to just
  169. run the integration tests (because you are debugging them):
  170. ---------------------------------------------------------------------------
  171. ./gradlew integTest
  172. ---------------------------------------------------------------------------
  173. If you want to just run the precommit checks:
  174. ---------------------------------------------------------------------------
  175. ./gradlew precommit
  176. ---------------------------------------------------------------------------
  177. == Testing the REST layer
  178. The available integration tests make use of the java API to communicate with
  179. the elasticsearch nodes, using the internal binary transport (port 9300 by
  180. default).
  181. The REST layer is tested through specific tests that are shared between all
  182. the elasticsearch official clients and consist of YAML files that describe the
  183. operations to be executed and the obtained results that need to be tested.
  184. The YAML files support various operators defined in the link:/rest-api-spec/src/main/resources/rest-api-spec/test/README.asciidoc[rest-api-spec] and adhere to the link:/rest-api-spec/README.markdown[Elasticsearch REST API JSON specification]
  185. The REST tests are run automatically when executing the "./gradlew check" command. To run only the
  186. REST tests use the following command:
  187. ---------------------------------------------------------------------------
  188. ./gradlew :distribution:archives:integ-test-zip:integTest \
  189. -Dtests.class="org.elasticsearch.test.rest.*Yaml*IT"
  190. ---------------------------------------------------------------------------
  191. A specific test case can be run with
  192. ---------------------------------------------------------------------------
  193. ./gradlew :distribution:archives:integ-test-zip:integTest \
  194. -Dtests.class="org.elasticsearch.test.rest.*Yaml*IT" \
  195. -Dtests.method="test {p0=cat.shards/10_basic/Help}"
  196. ---------------------------------------------------------------------------
  197. `*Yaml*IT` are the executable test classes that runs all the
  198. yaml suites available within the `rest-api-spec` folder.
  199. The REST tests support all the options provided by the randomized runner, plus the following:
  200. * `tests.rest[true|false]`: determines whether the REST tests need to be run (default) or not.
  201. * `tests.rest.suite`: comma separated paths of the test suites to be run
  202. (by default loaded from /rest-api-spec/test). It is possible to run only a subset
  203. of the tests providing a sub-folder or even a single yaml file (the default
  204. /rest-api-spec/test prefix is optional when files are loaded from classpath)
  205. e.g. -Dtests.rest.suite=index,get,create/10_with_id
  206. * `tests.rest.blacklist`: comma separated globs that identify tests that are
  207. blacklisted and need to be skipped
  208. e.g. -Dtests.rest.blacklist=index/*/Index document,get/10_basic/*
  209. Note that the REST tests, like all the integration tests, can be run against an external
  210. cluster by specifying the `tests.cluster` property, which if present needs to contain a
  211. comma separated list of nodes to connect to (e.g. localhost:9300). A transport client will
  212. be created based on that and used for all the before|after test operations, and to extract
  213. the http addresses of the nodes so that REST requests can be sent to them.
  214. == Testing scripts
  215. The simplest way to test scripts and the packaged distributions is to use
  216. Vagrant. You can get started by following there five easy steps:
  217. . Install Virtual Box and Vagrant.
  218. . (Optional) Install vagrant-cachier to squeeze a bit more performance out of
  219. the process:
  220. --------------------------------------
  221. vagrant plugin install vagrant-cachier
  222. --------------------------------------
  223. . Validate your installed dependencies:
  224. -------------------------------------
  225. ./gradlew :qa:vagrant:vagrantCheckVersion
  226. -------------------------------------
  227. . Download and smoke test the VMs with `./gradlew vagrantSmokeTest` or
  228. `./gradlew -Pvagrant.boxes=all vagrantSmokeTest`. The first time you run this it will
  229. download the base images and provision the boxes and immediately quit. If you
  230. you this again it'll skip the download step.
  231. . Run the tests with `./gradlew packagingTest`. This will cause Gradle to build
  232. the tar, zip, and deb packages and all the plugins. It will then run the tests
  233. on ubuntu-1404 and centos-7. We chose those two distributions as the default
  234. because they cover deb and rpm packaging and SyvVinit and systemd.
  235. You can run on all the VMs by running `./gradlew -Pvagrant.boxes=all
  236. packagingTest`. You can run a particular VM with a command like `./gradlew
  237. -Pvagrant.boxes=oel-7 packagingTest`. See `./gradlew tasks` for a complete list
  238. of available vagrant boxes for testing. It's important to know that if you
  239. interrupt any of these Gradle commands then the boxes will remain running and
  240. you'll have to terminate them with `./gradlew stop`.
  241. All the regular vagrant commands should just work so you can get a shell in a
  242. VM running trusty by running
  243. `vagrant up ubuntu-1404 --provider virtualbox && vagrant ssh ubuntu-1404`.
  244. These are the linux flavors the Vagrantfile currently supports:
  245. * ubuntu-1404 aka trusty
  246. * ubuntu-1604 aka xenial
  247. * debian-8 aka jessie
  248. * debian-9 aka stretch, the current debian stable distribution
  249. * centos-6
  250. * centos-7
  251. * fedora-26
  252. * fedora-27
  253. * oel-6 aka Oracle Enterprise Linux 6
  254. * oel-7 aka Oracle Enterprise Linux 7
  255. * sles-12
  256. * opensuse-42 aka Leap
  257. We're missing the following from the support matrix because there aren't high
  258. quality boxes available in vagrant atlas:
  259. * sles-11
  260. We're missing the following because our tests are very linux/bash centric:
  261. * Windows Server 2012
  262. It's important to think of VMs like cattle. If they become lame you just shoot
  263. them and let vagrant reprovision them. Say you've hosed your precise VM:
  264. ----------------------------------------------------
  265. vagrant ssh ubuntu-1404 -c 'sudo rm -rf /bin'; echo oops
  266. ----------------------------------------------------
  267. All you've got to do to get another one is
  268. ----------------------------------------------
  269. vagrant destroy -f ubuntu-1404 && vagrant up ubuntu-1404 --provider virtualbox
  270. ----------------------------------------------
  271. The whole process takes a minute and a half on a modern laptop, two and a half
  272. without vagrant-cachier.
  273. Its possible that some downloads will fail and it'll be impossible to restart
  274. them. This is a bug in vagrant. See the instructions here for how to work
  275. around it:
  276. https://github.com/mitchellh/vagrant/issues/4479
  277. Some vagrant commands will work on all VMs at once:
  278. ------------------
  279. vagrant halt
  280. vagrant destroy -f
  281. ------------------
  282. `vagrant up` would normally start all the VMs but we've prevented that because
  283. that'd consume a ton of ram.
  284. == Testing scripts more directly
  285. In general its best to stick to testing in vagrant because the bats scripts are
  286. destructive. When working with a single package it's generally faster to run its
  287. tests in a tighter loop than Gradle provides. In one window:
  288. --------------------------------
  289. ./gradlew :distribution:packages:rpm:assemble
  290. --------------------------------
  291. and in another window:
  292. ----------------------------------------------------
  293. vagrant up centos-7 --provider virtualbox && vagrant ssh centos-7
  294. cd $PACKAGING_ARCHIVES
  295. sudo -E bats $BATS_TESTS/*rpm*.bats
  296. ----------------------------------------------------
  297. If you wanted to retest all the release artifacts on a single VM you could:
  298. -------------------------------------------------
  299. ./gradlew setupPackagingTest
  300. cd qa/vagrant; vagrant up ubuntu-1404 --provider virtualbox && vagrant ssh ubuntu-1404
  301. cd $PACKAGING_ARCHIVES
  302. sudo -E bats $BATS_TESTS/*.bats
  303. -------------------------------------------------
  304. You can also use Gradle to prepare the test environment and then starts a single VM:
  305. -------------------------------------------------
  306. ./gradlew vagrantFedora27#up
  307. -------------------------------------------------
  308. Or any of vagrantCentos6#up, vagrantCentos7#up, vagrantDebian8#up,
  309. vagrantDebian9#up, vagrantFedora26#up, vagrantFedora27#up, vagrantOel6#up, vagrantOel7#up,
  310. vagrantOpensuse42#up,vagrantSles12#up, vagrantUbuntu1404#up, vagrantUbuntu1604#up.
  311. Once up, you can then connect to the VM using SSH from the elasticsearch directory:
  312. -------------------------------------------------
  313. vagrant ssh fedora-27
  314. -------------------------------------------------
  315. Or from another directory:
  316. -------------------------------------------------
  317. VAGRANT_CWD=/path/to/elasticsearch vagrant ssh fedora-27
  318. -------------------------------------------------
  319. Note: Starting vagrant VM outside of the elasticsearch folder requires to
  320. indicates the folder that contains the Vagrantfile using the VAGRANT_CWD
  321. environment variable.
  322. == Testing backwards compatibility
  323. Backwards compatibility tests exist to test upgrading from each supported version
  324. to the current version. To run all backcompat tests use:
  325. -------------------------------------------------
  326. ./gradlew bwcTest
  327. -------------------------------------------------
  328. A specific version can be tested as well. For example, to test backcompat with
  329. version 5.3.2 run:
  330. -------------------------------------------------
  331. ./gradlew v5.3.2#bwcTest
  332. -------------------------------------------------
  333. When running `./gradlew check`, some minimal backcompat checks are run. Which version
  334. is tested depends on the branch. On master, this will test against the current
  335. stable branch. On the stable branch, it will test against the latest release
  336. branch. Finally, on a release branch, it will test against the most recent release.
  337. === BWC Testing against a specific remote/branch
  338. Sometimes a backward compatibility change spans two versions. A common case is a new functionality
  339. that needs a BWC bridge in an unreleased versioned of a release branch (for example, 5.x).
  340. To test the changes, you can instruct Gradle to build the BWC version from a another remote/branch combination instead of
  341. pulling the release branch from GitHub. You do so using the `tests.bwc.remote` and `tests.bwc.refspec.BRANCH` system properties:
  342. -------------------------------------------------
  343. ./gradlew check -Dtests.bwc.remote=${remote} -Dtests.bwc.refspec.5.x=index_req_bwc_5.x
  344. -------------------------------------------------
  345. The branch needs to be available on the remote that the BWC makes of the
  346. repository you run the tests from. Using the remote is a handy trick to make
  347. sure that a branch is available and is up to date in the case of multiple runs.
  348. Example:
  349. Say you need to make a change to `master` and have a BWC layer in `5.x`. You
  350. will need to:
  351. . Create a branch called `index_req_change` off your remote `${remote}`. This
  352. will contain your change.
  353. . Create a branch called `index_req_bwc_5.x` off `5.x`. This will contain your bwc layer.
  354. . Push both branches to your remote repository.
  355. . Run the tests with `./gradlew check -Dtests.bwc.remote=${remote} -Dtests.bwc.refspec.5.x=index_req_bwc_5.x`.
  356. == Test coverage analysis
  357. Generating test coverage reports for Elasticsearch is currently not possible through Gradle.
  358. However, it _is_ possible to gain insight in code coverage using IntelliJ's built-in coverage
  359. analysis tool that can measure coverage upon executing specific tests. Eclipse may also be able
  360. to do the same using the EclEmma plugin.
  361. Test coverage reporting used to be possible with JaCoCo when Elasticsearch was using Maven
  362. as its build system. Since the switch to Gradle though, this is no longer possible, seeing as
  363. the code currently used to build Elasticsearch does not allow JaCoCo to recognize its tests.
  364. For more information on this, see the discussion in https://github.com/elastic/elasticsearch/issues/28867[issue #28867].
  365. == Launching and debugging from an IDE
  366. If you want to run Elasticsearch from your IDE, the `./gradlew run` task
  367. supports a remote debugging option:
  368. ---------------------------------------------------------------------------
  369. ./gradlew run --debug-jvm
  370. ---------------------------------------------------------------------------
  371. == Debugging remotely from an IDE
  372. If you want to run Elasticsearch and be able to remotely attach the process
  373. for debugging purposes from your IDE, can start Elasticsearch using `ES_JAVA_OPTS`:
  374. ---------------------------------------------------------------------------
  375. ES_JAVA_OPTS="-Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=4000,suspend=y" ./bin/elasticsearch
  376. ---------------------------------------------------------------------------
  377. Read your IDE documentation for how to attach a debugger to a JVM process.
  378. == Building with extra plugins
  379. Additional plugins may be built alongside elasticsearch, where their
  380. dependency on elasticsearch will be substituted with the local elasticsearch
  381. build. To add your plugin, create a directory called elasticsearch-extra as
  382. a sibling of elasticsearch. Checkout your plugin underneath elasticsearch-extra
  383. and the build will automatically pick it up. You can verify the plugin is
  384. included as part of the build by checking the projects of the build.
  385. ---------------------------------------------------------------------------
  386. ./gradlew projects
  387. ---------------------------------------------------------------------------
  388. == Environment misc
  389. There is a known issue with macOS localhost resolve strategy that can cause
  390. some integration tests to fail. This is because integration tests have timings
  391. for cluster formation, discovery, etc. that can be exceeded if name resolution
  392. takes a long time.
  393. To fix this, make sure you have your computer name (as returned by `hostname`)
  394. inside `/etc/hosts`, e.g.:
  395. ....
  396. 127.0.0.1 localhost ElasticMBP.local
  397. 255.255.255.255 broadcasthost
  398. ::1 localhost ElasticMBP.local`
  399. ....