TESTING.asciidoc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. [[TestingFrameworkCheatsheet]]
  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. To create a platform-specific build including the x-pack modules, use the
  14. following depending on your operating system:
  15. -----------------------------
  16. ./gradlew :distribution:archives:linux-tar:assemble
  17. ./gradlew :distribution:archives:darwin-tar:assemble
  18. ./gradlew :distribution:archives:windows-zip:assemble
  19. -----------------------------
  20. === Running Elasticsearch from a checkout
  21. In order to run Elasticsearch from source without building a package, you can
  22. run it using Gradle:
  23. -------------------------------------
  24. ./gradlew run
  25. -------------------------------------
  26. ==== Launching and debugging from an IDE
  27. If you want to run Elasticsearch from your IDE, the `./gradlew run` task
  28. supports a remote debugging option:
  29. ---------------------------------------------------------------------------
  30. ./gradlew run --debug-jvm
  31. ---------------------------------------------------------------------------
  32. This will instruct all JVMs (including any that run cli tools such as creating the keyring or adding users)
  33. to suspend and initiate a debug connection on port incrementing from `5005`.
  34. As such the IDE needs to be instructed to listen for connections on this port.
  35. Since we might run multiple JVMs as part of configuring and starting the cluster it's
  36. recommended to configure the IDE to initiate multiple listening attempts. In case of IntelliJ, this option
  37. is called "Auto restart" and needs to be checked. In case of Eclipse, "Connection limit" setting
  38. needs to be configured with a greater value (ie 10 or more).
  39. NOTE: If you have imported the project into IntelliJ according to the instructions in
  40. link:/CONTRIBUTING.md#importing-the-project-into-intellij-idea[CONTRIBUTING.md] then a debug run configuration
  41. named "Debug Elasticsearch" will be created for you and configured appropriately.
  42. ==== Distribution
  43. By default a node is started with the zip distribution.
  44. In order to start with a different distribution use the `-Drun.distribution` argument.
  45. To for example start the open source distribution:
  46. -------------------------------------
  47. ./gradlew run -Drun.distribution=oss
  48. -------------------------------------
  49. ==== License type
  50. By default a node is started with the `basic` license type.
  51. In order to start with a different license type use the `-Drun.license_type` argument.
  52. In order to start a node with a trial license execute the following command:
  53. -------------------------------------
  54. ./gradlew run -Drun.license_type=trial
  55. -------------------------------------
  56. This enables security and other paid features and adds a superuser with the username: `elastic-admin` and
  57. password: `elastic-password`.
  58. ==== Other useful arguments
  59. In order to start a node with a different max heap space add: `-Dtests.heap.size=4G`
  60. In order to disable assertions add: `-Dtests.asserts=false`
  61. In order to set an Elasticsearch setting, provide a setting with the following prefix: `-Dtests.es.`
  62. === Test case filtering.
  63. You can run a single test, provided that you specify the Gradle project. See the documentation on
  64. https://docs.gradle.org/current/userguide/userguide_single.html#simple_name_pattern[simple name pattern filtering].
  65. Run a single test case in the `server` project:
  66. ----------------------------------------------------------
  67. ./gradlew :server:test --tests org.elasticsearch.package.ClassName
  68. ----------------------------------------------------------
  69. Run all tests in a package and its sub-packages:
  70. ----------------------------------------------------
  71. ./gradlew :server:test --tests 'org.elasticsearch.package.*'
  72. ----------------------------------------------------
  73. Run all tests that are waiting for a bugfix (disabled by default)
  74. ------------------------------------------------
  75. ./gradlew test -Dtests.filter=@awaitsfix
  76. ------------------------------------------------
  77. === Seed and repetitions.
  78. Run with a given seed (seed is a hex-encoded long).
  79. ------------------------------
  80. ./gradlew test -Dtests.seed=DEADBEEF
  81. ------------------------------
  82. === Repeats _all_ tests of ClassName N times.
  83. Every test repetition will have a different method seed
  84. (derived from a single random master seed).
  85. --------------------------------------------------
  86. ./gradlew :server:test -Dtests.iters=N --tests org.elasticsearch.package.ClassName
  87. --------------------------------------------------
  88. === Repeats _all_ tests of ClassName N times.
  89. Every test repetition will have exactly the same master (0xdead) and
  90. method-level (0xbeef) seed.
  91. ------------------------------------------------------------------------
  92. ./gradlew :server:test -Dtests.iters=N -Dtests.seed=DEAD:BEEF --tests org.elasticsearch.package.ClassName
  93. ------------------------------------------------------------------------
  94. === Repeats a given test N times
  95. (note the filters - individual test repetitions are given suffixes,
  96. ie: testFoo[0], testFoo[1], etc... so using testmethod or tests.method
  97. ending in a glob is necessary to ensure iterations are run).
  98. -------------------------------------------------------------------------
  99. ./gradlew :server:test -Dtests.iters=N --tests org.elasticsearch.package.ClassName.methodName
  100. -------------------------------------------------------------------------
  101. Repeats N times but skips any tests after the first failure or M initial failures.
  102. -------------------------------------------------------------
  103. ./gradlew test -Dtests.iters=N -Dtests.failfast=true ...
  104. ./gradlew test -Dtests.iters=N -Dtests.maxfailures=M ...
  105. -------------------------------------------------------------
  106. === Test groups.
  107. Test groups can be enabled or disabled (true/false).
  108. Default value provided below in [brackets].
  109. ------------------------------------------------------------------
  110. ./gradlew test -Dtests.awaitsfix=[false] - known issue (@AwaitsFix)
  111. ------------------------------------------------------------------
  112. === Load balancing and caches.
  113. By default the tests run on multiple processes using all the available cores on all
  114. available CPUs. Not including hyper-threading.
  115. If you want to explicitly specify the number of JVMs you can do so on the command
  116. line:
  117. ----------------------------
  118. ./gradlew test -Dtests.jvms=8
  119. ----------------------------
  120. Or in `~/.gradle/gradle.properties`:
  121. ----------------------------
  122. systemProp.tests.jvms=8
  123. ----------------------------
  124. Its difficult to pick the "right" number here. Hypercores don't count for CPU
  125. intensive tests and you should leave some slack for JVM-internal threads like
  126. the garbage collector. And you have to have enough RAM to handle each JVM.
  127. === Test compatibility.
  128. It is possible to provide a version that allows to adapt the tests behaviour
  129. to older features or bugs that have been changed or fixed in the meantime.
  130. -----------------------------------------
  131. ./gradlew test -Dtests.compatibility=1.0.0
  132. -----------------------------------------
  133. === Miscellaneous.
  134. Run all tests without stopping on errors (inspect log files).
  135. -----------------------------------------
  136. ./gradlew test -Dtests.haltonfailure=false
  137. -----------------------------------------
  138. Run more verbose output (slave JVM parameters, etc.).
  139. ----------------------
  140. ./gradlew test -verbose
  141. ----------------------
  142. Change the default suite timeout to 5 seconds for all
  143. tests (note the exclamation mark).
  144. ---------------------------------------
  145. ./gradlew test -Dtests.timeoutSuite=5000! ...
  146. ---------------------------------------
  147. Change the logging level of ES (not Gradle)
  148. --------------------------------
  149. ./gradlew test -Dtests.es.logger.level=DEBUG
  150. --------------------------------
  151. Print all the logging output from the test runs to the commandline
  152. even if tests are passing.
  153. ------------------------------
  154. ./gradlew test -Dtests.output=always
  155. ------------------------------
  156. Configure the heap size.
  157. ------------------------------
  158. ./gradlew test -Dtests.heap.size=512m
  159. ------------------------------
  160. Pass arbitrary jvm arguments.
  161. ------------------------------
  162. # specify heap dump path
  163. ./gradlew test -Dtests.jvm.argline="-XX:HeapDumpPath=/path/to/heapdumps"
  164. # enable gc logging
  165. ./gradlew test -Dtests.jvm.argline="-verbose:gc"
  166. # enable security debugging
  167. ./gradlew test -Dtests.jvm.argline="-Djava.security.debug=access,failure"
  168. ------------------------------
  169. == Running verification tasks
  170. To run all verification tasks, including static checks, unit tests, and integration tests:
  171. ---------------------------------------------------------------------------
  172. ./gradlew check
  173. ---------------------------------------------------------------------------
  174. Note that this will also run the unit tests and precommit tasks first. If you want to just
  175. run the in memory cluster integration tests (because you are debugging them):
  176. ---------------------------------------------------------------------------
  177. ./gradlew internalClusterTest
  178. ---------------------------------------------------------------------------
  179. If you want to just run the precommit checks:
  180. ---------------------------------------------------------------------------
  181. ./gradlew precommit
  182. ---------------------------------------------------------------------------
  183. Some of these checks will require `docker-compose` installed for bringing up
  184. test fixtures. If it's not present those checks will be skipped automatically.
  185. The host running Docker (or VM if you're using Docker Desktop) needs 4GB of
  186. memory or some of the containers will fail to start. You can tell that you
  187. are short of memory if containers are exiting quickly after starting with
  188. code 137 (128 + 9, where 9 means SIGKILL).
  189. == Testing the REST layer
  190. The REST layer is tested through specific tests that are executed against
  191. a cluster that is configured and initialized via Gradle. The tests
  192. themselves can be written in either Java or with a YAML based DSL.
  193. YAML based REST tests should be preferred since these are shared between all
  194. the elasticsearch official clients. The YAML based tests describe the
  195. operations to be executed and the obtained results that need to be tested.
  196. The YAML tests 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]
  197. In order to run the the YAML tests, the relevant API specification needs
  198. to be on the test classpath. Any gradle project that has support for REST
  199. tests will get the primary API on it's class path. However, to better support
  200. Gradle incremental builds, it is recommended to explicitly declare which
  201. parts of the API the tests depend upon.
  202. For example:
  203. ---------------------------------------------------------------------------
  204. restResources {
  205. restApi {
  206. includeCore '_common', 'indices', 'index', 'cluster', 'nodes', 'get', 'ingest'
  207. }
  208. }
  209. ---------------------------------------------------------------------------
  210. YAML tests that include x-pack specific APIs need to explicitly declare
  211. which APIs are required through a similar `includeXpack` configuration.
  212. The REST tests are run automatically when executing the "./gradlew check" command. To run only the
  213. REST tests use the following command:
  214. ---------------------------------------------------------------------------
  215. ./gradlew :distribution:archives:integ-test-zip:integTestRunner \
  216. --tests "org.elasticsearch.test.rest.IntegTestZipClientYamlTestSuiteIT"
  217. ---------------------------------------------------------------------------
  218. A specific test case can be run with the following command:
  219. ---------------------------------------------------------------------------
  220. ./gradlew ':distribution:archives:integ-test-zip:integTestRunner' \
  221. --tests "org.elasticsearch.test.rest.IntegTestZipClientYamlTestSuiteIT" \
  222. -Dtests.method="test {p0=cat.segments/10_basic/Help}"
  223. ---------------------------------------------------------------------------
  224. The REST tests support all the options provided by the randomized runner, plus the following:
  225. * `tests.rest[true|false]`: determines whether the REST tests need to be run (default) or not.
  226. * `tests.rest.suite`: comma separated paths of the test suites to be run
  227. (by default loaded from /rest-api-spec/test). It is possible to run only a subset
  228. of the tests providing a sub-folder or even a single yaml file (the default
  229. /rest-api-spec/test prefix is optional when files are loaded from classpath)
  230. e.g. -Dtests.rest.suite=index,get,create/10_with_id
  231. * `tests.rest.blacklist`: comma separated globs that identify tests that are
  232. blacklisted and need to be skipped
  233. e.g. -Dtests.rest.blacklist=index/*/Index document,get/10_basic/*
  234. == Testing packaging
  235. The packaging tests use Vagrant virtual machines or cloud instances to verify
  236. that installing and running Elasticsearch distributions works correctly on
  237. supported operating systems. These tests should really only be run on ephemeral
  238. systems because they're destructive; that is, these tests install and remove
  239. packages and freely modify system settings, so you will probably regret it if
  240. you execute them on your development machine.
  241. When you run a packaging test, Gradle will set up the target VM and mount your
  242. repository directory in the VM. Once this is done, a Gradle task will issue a
  243. Vagrant command to run a *nested* Gradle task on the VM. This nested Gradle
  244. runs the actual "destructive" test classes.
  245. . Install Virtual Box and Vagrant.
  246. +
  247. . (Optional) Install https://github.com/fgrehm/vagrant-cachier[vagrant-cachier] to squeeze
  248. a bit more performance out of the process:
  249. +
  250. --------------------------------------
  251. vagrant plugin install vagrant-cachier
  252. --------------------------------------
  253. +
  254. . You can run all of the OS packaging tests with `./gradlew packagingTest`.
  255. This task includes our legacy `bats` tests. To run only the OS tests that are
  256. written in Java, run `.gradlew distroTest`, will cause Gradle to build the tar,
  257. zip, and deb packages and all the plugins. It will then run the tests on every
  258. available system. This will take a very long time.
  259. +
  260. Fortunately, the various systems under test have their own Gradle tasks under
  261. `qa/os`. To find the systems tested, do a listing of the `qa/os` directory.
  262. To find out what packaging combinations can be tested on a system, run
  263. the `tasks` task. For example:
  264. +
  265. ----------------------------------
  266. ./gradlew :qa:os:ubuntu-1804:tasks
  267. ----------------------------------
  268. +
  269. If you want a quick test of the tarball and RPM packagings for Centos 7, you
  270. would run:
  271. +
  272. -------------------------------------------------------------------------------------------------
  273. ./gradlew :qa:os:centos-7:distroTest.default-rpm :qa:os:centos-7:distroTest.default-linux-archive
  274. -------------------------------------------------------------------------------------------------
  275. Note that if you interrupt Gradle in the middle of running these tasks, any boxes started
  276. will remain running and you'll have to stop them manually with `./gradlew --stop` or
  277. `vagrant halt`.
  278. All the regular vagrant commands should just work so you can get a shell in a
  279. VM running trusty by running
  280. `vagrant up ubuntu-1804 --provider virtualbox && vagrant ssh ubuntu-1804`.
  281. === Testing packaging on Windows
  282. The packaging tests also support Windows Server 2012R2 and Windows Server 2016.
  283. Unfortunately we're not able to provide boxes for them in open source use
  284. because of licensing issues. Any Virtualbox image that has WinRM and Powershell
  285. enabled for remote users should work.
  286. Specify the image IDs of the Windows boxes to gradle with the following project
  287. properties. They can be set in `~/.gradle/gradle.properties` like
  288. ------------------------------------
  289. vagrant.windows-2012r2.id=my-image-id
  290. vagrant.windows-2016.id=another-image-id
  291. ------------------------------------
  292. or passed on the command line like `-Pvagrant.windows-2012r2.id=my-image-id`
  293. `-Pvagrant.windows-2016=another-image-id`
  294. These properties are required for Windows support in all gradle tasks that
  295. handle packaging tests. Either or both may be specified.
  296. If you're running vagrant commands outside of gradle, specify the Windows boxes
  297. with the environment variables
  298. * `VAGRANT_WINDOWS_2012R2_BOX`
  299. * `VAGRANT_WINDOWS_2016_BOX`
  300. === Testing VMs are disposable
  301. It's important to think of VMs like cattle. If they become lame you just shoot
  302. them and let vagrant reprovision them. Say you've hosed your precise VM:
  303. ----------------------------------------------------
  304. vagrant ssh ubuntu-1604 -c 'sudo rm -rf /bin'; echo oops
  305. ----------------------------------------------------
  306. All you've got to do to get another one is
  307. ----------------------------------------------
  308. vagrant destroy -f ubuntu-1604 && vagrant up ubuntu-1604 --provider virtualbox
  309. ----------------------------------------------
  310. The whole process takes a minute and a half on a modern laptop, two and a half
  311. without vagrant-cachier.
  312. Its possible that some downloads will fail and it'll be impossible to restart
  313. them. This is a bug in vagrant. See the instructions here for how to work
  314. around it:
  315. https://github.com/mitchellh/vagrant/issues/4479
  316. Some vagrant commands will work on all VMs at once:
  317. ------------------
  318. vagrant halt
  319. vagrant destroy -f
  320. ------------------
  321. `vagrant up` would normally start all the VMs but we've prevented that because
  322. that'd consume a ton of ram.
  323. === Iterating on packaging tests
  324. Because our packaging tests are capable of testing many combinations of OS
  325. (e.g., Windows, Linux, etc.), package type (e.g., zip file, RPM, etc.),
  326. Elasticsearch distribution type (e.g., default or OSS), and so forth, it's
  327. faster to develop against smaller subsets of the tests. For example, to run
  328. tests for the default archive distribution on Fedora 28:
  329. -----------------------------------------------------------
  330. ./gradlew :qa:os:fedora-28:distroTest.default-linux-archive
  331. -----------------------------------------------------------
  332. These test tasks can use the `--tests`, `--info`, and `--debug` parameters just like
  333. non-OS tests can. For example:
  334. -----------------------------------------------------------
  335. ./gradlew :qa:os:fedora-28:distroTest.default-linux-archive \
  336. --tests "com.elasticsearch.packaging.test.ArchiveTests"
  337. -----------------------------------------------------------
  338. == Testing backwards compatibility
  339. Backwards compatibility tests exist to test upgrading from each supported version
  340. to the current version. To run them all use:
  341. -------------------------------------------------
  342. ./gradlew bwcTest
  343. -------------------------------------------------
  344. A specific version can be tested as well. For example, to test bwc with
  345. version 5.3.2 run:
  346. -------------------------------------------------
  347. ./gradlew v5.3.2#bwcTest
  348. -------------------------------------------------
  349. Use -Dtest.class and -Dtests.method to run a specific bwcTest test.
  350. For example to run a specific tests from the x-pack rolling upgrade from 7.7.0:
  351. -------------------------------------------------
  352. ./gradlew :x-pack:qa:rolling-upgrade:v7.7.0#bwcTest \
  353. -Dtests.class=org.elasticsearch.upgrades.UpgradeClusterClientYamlTestSuiteIT \
  354. -Dtests.method="test {p0=*/40_ml_datafeed_crud/*}"
  355. -------------------------------------------------
  356. Tests are ran for versions that are not yet released but with which the current version will be compatible with.
  357. These are automatically checked out and built from source.
  358. See link:./buildSrc/src/main/java/org/elasticsearch/gradle/VersionCollection.java[VersionCollection]
  359. and link:./distribution/bwc/build.gradle[distribution/bwc/build.gradle]
  360. for more information.
  361. When running `./gradlew check`, minimal bwc checks are also run against compatible versions that are not yet released.
  362. ==== BWC Testing against a specific remote/branch
  363. Sometimes a backward compatibility change spans two versions. A common case is a new functionality
  364. that needs a BWC bridge in an unreleased versioned of a release branch (for example, 5.x).
  365. To test the changes, you can instruct Gradle to build the BWC version from a another remote/branch combination instead of
  366. pulling the release branch from GitHub. You do so using the `bwc.remote` and `bwc.refspec.BRANCH` system properties:
  367. -------------------------------------------------
  368. ./gradlew check -Dbwc.remote=${remote} -Dbwc.refspec.5.x=index_req_bwc_5.x
  369. -------------------------------------------------
  370. The branch needs to be available on the remote that the BWC makes of the
  371. repository you run the tests from. Using the remote is a handy trick to make
  372. sure that a branch is available and is up to date in the case of multiple runs.
  373. Example:
  374. Say you need to make a change to `master` and have a BWC layer in `5.x`. You
  375. will need to:
  376. . Create a branch called `index_req_change` off your remote `${remote}`. This
  377. will contain your change.
  378. . Create a branch called `index_req_bwc_5.x` off `5.x`. This will contain your bwc layer.
  379. . Push both branches to your remote repository.
  380. . Run the tests with `./gradlew check -Dbwc.remote=${remote} -Dbwc.refspec.5.x=index_req_bwc_5.x`.
  381. ==== Skip fetching latest
  382. For some BWC testing scenarios, you want to use the local clone of the
  383. repository without fetching latest. For these use cases, you can set the system
  384. property `tests.bwc.git_fetch_latest` to `false` and the BWC builds will skip
  385. fetching the latest from the remote.
  386. == How to write good tests?
  387. === Base classes for test cases
  388. There are multiple base classes for tests:
  389. * **`ESTestCase`**: The base class of all tests. It is typically extended
  390. directly by unit tests.
  391. * **`ESSingleNodeTestCase`**: This test case sets up a cluster that has a
  392. single node.
  393. * **`ESIntegTestCase`**: An integration test case that creates a cluster that
  394. might have multiple nodes.
  395. * **`ESRestTestCase`**: An integration tests that interacts with an external
  396. cluster via the REST API. This is used for Java based REST tests.
  397. * **`ESClientYamlSuiteTestCase` **: A subclass of `ESRestTestCase` used to run
  398. YAML based REST tests.
  399. === Good practices
  400. ==== What kind of tests should I write?
  401. Unit tests are the preferred way to test some functionality: most of the time
  402. they are simpler to understand, more likely to reproduce, and unlikely to be
  403. affected by changes that are unrelated to the piece of functionality that is
  404. being tested.
  405. The reason why `ESSingleNodeTestCase` exists is that all our components used to
  406. be very hard to set up in isolation, which had led us to having a number of
  407. integration tests but close to no unit tests. `ESSingleNodeTestCase` is a
  408. workaround for this issue which provides an easy way to spin up a node and get
  409. access to components that are hard to instantiate like `IndicesService`.
  410. Whenever practical, you should prefer unit tests.
  411. Many tests extend `ESIntegTestCase`, mostly because this is how most tests used
  412. to work in the early days of Elasticsearch. However the complexity of these
  413. tests tends to make them hard to debug. Whenever the functionality that is
  414. being tested isn't intimately dependent on how Elasticsearch behaves as a
  415. cluster, it is recommended to write unit tests or REST tests instead.
  416. In short, most new functionality should come with unit tests, and optionally
  417. REST tests to test integration.
  418. ==== Refactor code to make it easier to test
  419. Unfortunately, a large part of our code base is still hard to unit test.
  420. Sometimes because some classes have lots of dependencies that make them hard to
  421. instantiate. Sometimes because API contracts make tests hard to write. Code
  422. refactors that make functionality easier to unit test are encouraged. If this
  423. sounds very abstract to you, you can have a look at
  424. https://github.com/elastic/elasticsearch/pull/16610[this pull request] for
  425. instance, which is a good example. It refactors `IndicesRequestCache` in such
  426. a way that:
  427. - it no longer depends on objects that are hard to instantiate such as
  428. `IndexShard` or `SearchContext`,
  429. - time-based eviction is applied on top of the cache rather than internally,
  430. which makes it easier to assert on what the cache is expected to contain at
  431. a given time.
  432. === Bad practices
  433. ==== Use randomized-testing for coverage
  434. In general, randomization should be used for parameters that are not expected
  435. to affect the behavior of the functionality that is being tested. For instance
  436. the number of shards should not impact `date_histogram` aggregations, and the
  437. choice of the `store` type (`niofs` vs `mmapfs`) does not affect the results of
  438. a query. Such randomization helps improve confidence that we are not relying on
  439. implementation details of one component or specifics of some setup.
  440. However it should not be used for coverage. For instance if you are testing a
  441. piece of functionality that enters different code paths depending on whether
  442. the index has 1 shards or 2+ shards, then we shouldn't just test against an
  443. index with a random number of shards: there should be one test for the 1-shard
  444. case, and another test for the 2+ shards case.
  445. ==== Abuse randomization in multi-threaded tests
  446. Multi-threaded tests are often not reproducible due to the fact that there is
  447. no guarantee on the order in which operations occur across threads. Adding
  448. randomization to the mix usually makes things worse and should be done with
  449. care.
  450. == Test coverage analysis
  451. Generating test coverage reports for Elasticsearch is currently not possible through Gradle.
  452. However, it _is_ possible to gain insight in code coverage using IntelliJ's built-in coverage
  453. analysis tool that can measure coverage upon executing specific tests. Eclipse may also be able
  454. to do the same using the EclEmma plugin.
  455. Test coverage reporting used to be possible with JaCoCo when Elasticsearch was using Maven
  456. as its build system. Since the switch to Gradle though, this is no longer possible, seeing as
  457. the code currently used to build Elasticsearch does not allow JaCoCo to recognize its tests.
  458. For more information on this, see the discussion in https://github.com/elastic/elasticsearch/issues/28867[issue #28867].
  459. == Debugging remotely from an IDE
  460. If you want to run Elasticsearch and be able to remotely attach the process
  461. for debugging purposes from your IDE, can start Elasticsearch using `ES_JAVA_OPTS`:
  462. ---------------------------------------------------------------------------
  463. ES_JAVA_OPTS="-Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=4000,suspend=y" ./bin/elasticsearch
  464. ---------------------------------------------------------------------------
  465. Read your IDE documentation for how to attach a debugger to a JVM process.
  466. == Building with extra plugins
  467. Additional plugins may be built alongside elasticsearch, where their
  468. dependency on elasticsearch will be substituted with the local elasticsearch
  469. build. To add your plugin, create a directory called elasticsearch-extra as
  470. a sibling of elasticsearch. Checkout your plugin underneath elasticsearch-extra
  471. and the build will automatically pick it up. You can verify the plugin is
  472. included as part of the build by checking the projects of the build.
  473. ---------------------------------------------------------------------------
  474. ./gradlew projects
  475. ---------------------------------------------------------------------------
  476. == Environment misc
  477. There is a known issue with macOS localhost resolve strategy that can cause
  478. some integration tests to fail. This is because integration tests have timings
  479. for cluster formation, discovery, etc. that can be exceeded if name resolution
  480. takes a long time.
  481. To fix this, make sure you have your computer name (as returned by `hostname`)
  482. inside `/etc/hosts`, e.g.:
  483. ....
  484. 127.0.0.1 localhost ElasticMBP.local
  485. 255.255.255.255 broadcasthost
  486. ::1 localhost ElasticMBP.local`
  487. ....
  488. == Benchmarking
  489. For changes that might affect the performance characteristics of Elasticsearch
  490. you should also run macrobenchmarks. We maintain a macrobenchmarking tool
  491. called https://github.com/elastic/rally[Rally]
  492. which you can use to measure the performance impact. It comes with a set of
  493. default benchmarks that we also
  494. https://elasticsearch-benchmarks.elastic.co/[run every night]. To get started,
  495. please see https://esrally.readthedocs.io/en/stable/[Rally's documentation].
  496. == Test doc builds
  497. The Elasticsearch docs are in AsciiDoc format. You can test and build the docs
  498. locally using the Elasticsearch documentation build process. See
  499. https://github.com/elastic/docs.