TESTING.asciidoc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. mvn clean package -DskipTests
  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 && mvn 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. ./run.sh
  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. mvn test -Dtests.class=org.elasticsearch.package.ClassName
  39. mvn test "-Dtests.class=*.ClassName"
  40. ----------------------------------------------------------
  41. Run all tests in a package and sub-packages
  42. ----------------------------------------------------
  43. mvn test "-Dtests.class=org.elasticsearch.package.*"
  44. ----------------------------------------------------
  45. Run any test methods that contain 'esi' (like: ...r*esi*ze...).
  46. -------------------------------
  47. mvn 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. mvn 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. mvn 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. mvn 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. mvn 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. mvn 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. mvn 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. mvn test -Dtests.iters=N -Dtests.failfast=true -Dtestcase=...
  90. mvn 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. mvn test -Dtests.nightly=[false] - nightly test group (@Nightly)
  97. mvn test -Dtests.weekly=[false] - weekly tests (@Weekly)
  98. mvn test -Dtests.awaitsfix=[false] - known issue (@AwaitsFix)
  99. ------------------------------------------------------------------
  100. === Load balancing and caches.
  101. By default, the tests run sequentially on a single forked JVM.
  102. To run with more forked JVMs than the default use:
  103. ----------------------------
  104. mvn test -Dtests.jvms=8 test
  105. ----------------------------
  106. Don't count hypercores for CPU-intense tests and leave some slack
  107. for JVM-internal threads (like the garbage collector). Make sure there is
  108. enough RAM to handle child JVMs.
  109. === Test compatibility.
  110. It is possible to provide a version that allows to adapt the tests behaviour
  111. to older features or bugs that have been changed or fixed in the meantime.
  112. -----------------------------------------
  113. mvn test -Dtests.compatibility=1.0.0
  114. -----------------------------------------
  115. === Miscellaneous.
  116. Run all tests without stopping on errors (inspect log files).
  117. -----------------------------------------
  118. mvn test -Dtests.haltonfailure=false test
  119. -----------------------------------------
  120. Run more verbose output (slave JVM parameters, etc.).
  121. ----------------------
  122. mvn test -verbose test
  123. ----------------------
  124. Change the default suite timeout to 5 seconds for all
  125. tests (note the exclamation mark).
  126. ---------------------------------------
  127. mvn test -Dtests.timeoutSuite=5000! ...
  128. ---------------------------------------
  129. Change the logging level of ES (not mvn)
  130. --------------------------------
  131. mvn test -Des.logger.level=DEBUG
  132. --------------------------------
  133. Print all the logging output from the test runs to the commandline
  134. even if tests are passing.
  135. ------------------------------
  136. mvn test -Dtests.output=always
  137. ------------------------------
  138. Configure the heap size.
  139. ------------------------------
  140. mvn test -Dtests.heap.size=512m
  141. ------------------------------
  142. Pass arbitrary jvm arguments.
  143. ------------------------------
  144. mvn test -Dtests.jvm.argline="-XX:HeapDumpPath=/path/to/heapdumps"
  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. mvn 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. mvn 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 integration tests
  168. To run the integration tests:
  169. ---------------------------------------------------------------------------
  170. mvn verify
  171. ---------------------------------------------------------------------------
  172. Note that this will also run the unit tests first. If you want to just
  173. run the integration tests only (because you are debugging them):
  174. ---------------------------------------------------------------------------
  175. mvn verify -Dskip.unit.tests
  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 REST tests are run automatically when executing the maven test command. To run only the
  185. REST tests use the following command:
  186. ---------------------------------------------------------------------------
  187. mvn verify -Dtests.filter="@Rest"
  188. ---------------------------------------------------------------------------
  189. `RestNIT` are the executable test classes that runs all the
  190. yaml suites available within the `rest-api-spec` folder.
  191. The REST tests support all the options provided by the randomized runner, plus the following:
  192. * `tests.rest[true|false]`: determines whether the REST tests need to be run (default) or not.
  193. * `tests.rest.suite`: comma separated paths of the test suites to be run
  194. (by default loaded from /rest-api-spec/test). It is possible to run only a subset
  195. of the tests providing a sub-folder or even a single yaml file (the default
  196. /rest-api-spec/test prefix is optional when files are loaded from classpath)
  197. e.g. -Dtests.rest.suite=index,get,create/10_with_id
  198. * `tests.rest.blacklist`: comma separated globs that identify tests that are
  199. blacklisted and need to be skipped
  200. e.g. -Dtests.rest.blacklist=index/*/Index document,get/10_basic/*
  201. * `tests.rest.spec`: REST spec path (default /rest-api-spec/api)
  202. Note that the REST tests, like all the integration tests, can be run against an external
  203. cluster by specifying the `tests.cluster` property, which if present needs to contain a
  204. comma separated list of nodes to connect to (e.g. localhost:9300). A transport client will
  205. be created based on that and used for all the before|after test operations, and to extract
  206. the http addresses of the nodes so that REST requests can be sent to them.
  207. == Skip validate
  208. To disable validation step (forbidden API or `// NOCOMMIT`) use
  209. ---------------------------------------------------------------------------
  210. mvn test -Dvalidate.skip=true
  211. ---------------------------------------------------------------------------
  212. You can also skip this by using the "dev" profile:
  213. ---------------------------------------------------------------------------
  214. mvn test -Pdev
  215. ---------------------------------------------------------------------------
  216. == Testing scripts
  217. The simplest way to test scripts and the packaged distributions is to use
  218. Vagrant. You can get started by following there five easy steps:
  219. . Install Virtual Box and Vagrant.
  220. . (Optional) Install vagrant-cachier to squeeze a bit more performance out of
  221. the process:
  222. --------------------------------------
  223. vagrant plugin install vagrant-cachier
  224. --------------------------------------
  225. . Validate your installed dependencies:
  226. -------------------------------------
  227. mvn -Dtests.vagrant -pl qa/vagrant validate
  228. -------------------------------------
  229. . Download the VMs. Since Maven or ant or something eats the progress reports
  230. from Vagrant when you run it inside mvn its probably best if you run this one
  231. time to setup all the VMs one at a time. Run this to download and setup the VMs
  232. we use for testing by default:
  233. --------------------------------------------------------
  234. vagrant up --provision trusty --provider virtualbox && vagrant halt trusty
  235. vagrant up --provision centos-7 --provider virtualbox && vagrant halt centos-7
  236. --------------------------------------------------------
  237. or run this to download and setup all the VMs:
  238. -------------------------------------------------------------------------------
  239. vagrant halt
  240. for box in $(vagrant status | grep 'poweroff\|not created' | cut -f1 -d' '); do
  241. vagrant up --provision $box --provider virtualbox
  242. vagrant halt $box
  243. done
  244. -------------------------------------------------------------------------------
  245. . Smoke test the maven/ant dance that we use to get vagrant involved in
  246. integration testing is working:
  247. ---------------------------------------------
  248. mvn -Dtests.vagrant -Psmoke-vms -pl qa/vagrant verify
  249. ---------------------------------------------
  250. or this to validate all the VMs:
  251. -------------------------------------------------
  252. mvn -Dtests.vagrant=all -Psmoke-vms -pl qa/vagrant verify
  253. -------------------------------------------------
  254. That will start up the VMs and then immediate quit.
  255. . Finally run the tests. The fastest way to get this started is to run:
  256. -----------------------------------
  257. mvn clean install -DskipTests
  258. mvn -Dtests.vagrant -pl qa/vagrant verify
  259. -----------------------------------
  260. You could just run:
  261. --------------------
  262. mvn -Dtests.vagrant verify
  263. --------------------
  264. but that will run all the tests. Which is probably a good thing, but not always
  265. what you want.
  266. Whichever snippet you run mvn will build the tar, zip and deb packages. If you
  267. have rpmbuild installed it'll build the rpm package as well. Then mvn will
  268. spin up trusty and verify the tar, zip, and deb package. If you have rpmbuild
  269. installed it'll spin up centos-7 and verify the tar, zip and rpm packages. We
  270. chose those two distributions as the default because they cover deb and rpm
  271. packaging and SyvVinit and systemd.
  272. You can control the boxes that are used for testing like so. Run just
  273. fedora-22 with:
  274. --------------------------------------------
  275. mvn -Dtests.vagrant -pl qa/vagrant verify -DboxesToTest=fedora-22
  276. --------------------------------------------
  277. or run wheezy and trusty:
  278. ------------------------------------------------------------------
  279. mvn -Dtests.vagrant -pl qa/vagrant verify -DboxesToTest='wheezy, trusty'
  280. ------------------------------------------------------------------
  281. or run all the boxes:
  282. ---------------------------------------
  283. mvn -Dtests.vagrant=all -pl qa/vagrant verify
  284. ---------------------------------------
  285. Its important to know that if you ctrl-c any of these `mvn` runs that you'll
  286. probably leave a VM up. You can terminate it by running:
  287. ------------
  288. vagrant halt
  289. ------------
  290. This is just regular vagrant so you can run normal multi box vagrant commands
  291. to test things manually. Just run:
  292. ---------------------------------------
  293. vagrant up trusty --provider virtualbox && vagrant ssh trusty
  294. ---------------------------------------
  295. to get an Ubuntu or
  296. -------------------------------------------
  297. vagrant up centos-7 --provider virtualbox && vagrant ssh centos-7
  298. -------------------------------------------
  299. to get a CentOS. Once you are done with them you should halt them:
  300. -------------------
  301. vagrant halt trusty
  302. -------------------
  303. These are the linux flavors the Vagrantfile currently supports:
  304. * precise aka Ubuntu 12.04
  305. * trusty aka Ubuntu 14.04
  306. * vivid aka Ubuntun 15.04
  307. * wheezy aka Debian 7, the current debian oldstable distribution
  308. * jessie aka Debian 8, the current debina stable distribution
  309. * centos-6
  310. * centos-7
  311. * fedora-22
  312. * oel-7 aka Oracle Enterprise Linux 7
  313. We're missing the following from the support matrix because there aren't high
  314. quality boxes available in vagrant atlas:
  315. * sles-11
  316. * sles-12
  317. * opensuse-13
  318. * oel-6
  319. We're missing the follow because our tests are very linux/bash centric:
  320. * Windows Server 2012
  321. Its important to think of VMs like cattle: if they become lame you just shoot
  322. them and let vagrant reprovision them. Say you've hosed your precise VM:
  323. ----------------------------------------------------
  324. vagrant ssh precise -c 'sudo rm -rf /bin'; echo oops
  325. ----------------------------------------------------
  326. All you've got to do to get another one is
  327. ----------------------------------------------
  328. vagrant destroy -f trusty && vagrant up trusty --provider virtualbox
  329. ----------------------------------------------
  330. The whole process takes a minute and a half on a modern laptop, two and a half
  331. without vagrant-cachier.
  332. Its possible that some downloads will fail and it'll be impossible to restart
  333. them. This is a bug in vagrant. See the instructions here for how to work
  334. around it:
  335. https://github.com/mitchellh/vagrant/issues/4479
  336. Some vagrant commands will work on all VMs at once:
  337. ------------------
  338. vagrant halt
  339. vagrant destroy -f
  340. ------------------
  341. ----------
  342. vagrant up
  343. ----------
  344. would normally start all the VMs but we've prevented that because that'd
  345. consume a ton of ram.
  346. == Testing scripts more directly
  347. In general its best to stick to testing in vagrant because the bats scripts are
  348. destructive. When working with a single package its generally faster to run its
  349. tests in a tighter loop than maven provides. In one window:
  350. --------------------------------
  351. mvn -pl distribution/rpm package
  352. --------------------------------
  353. and in another window:
  354. ----------------------------------------------------
  355. vagrant up centos-7 --provider virtualbox && vagrant ssh centos-7
  356. cd $RPM
  357. sudo bats $BATS/*rpm*.bats
  358. ----------------------------------------------------
  359. If you wanted to retest all the release artifacts on a single VM you could:
  360. -------------------------------------------------
  361. # Build all the distributions fresh but skip recompiling elasticsearch:
  362. mvn -amd -pl distribution install -DskipTests
  363. # Copy them all the testroot
  364. mvn -Dtests.vagrant -pl qa/vagrant pre-integration-test
  365. vagrant up trusty --provider virtualbox && vagrant ssh trusty
  366. cd $TESTROOT
  367. sudo bats $BATS/*.bats
  368. -------------------------------------------------
  369. == Coverage analysis
  370. Tests can be run instrumented with jacoco to produce a coverage report in
  371. `target/site/jacoco/`.
  372. Unit test coverage:
  373. ---------------------------------------------------------------------------
  374. mvn -Dtests.coverage test jacoco:report
  375. ---------------------------------------------------------------------------
  376. Integration test coverage:
  377. ---------------------------------------------------------------------------
  378. mvn -Dtests.coverage -Dskip.unit.tests verify jacoco:report
  379. ---------------------------------------------------------------------------
  380. Combined (Unit+Integration) coverage:
  381. ---------------------------------------------------------------------------
  382. mvn -Dtests.coverage verify jacoco:report
  383. ---------------------------------------------------------------------------
  384. == Debugging from an IDE
  385. If you want to run elasticsearch from your IDE, you should execute ./run.sh
  386. It opens a remote debugging port that you can connect with your IDE.