2018-10-15 01:54:18 -04:00
[[TestingFrameworkCheatsheet]]
2013-09-13 13:50:56 -04:00
= Testing
[partintro]
2014-04-02 08:27:31 -04:00
2013-09-13 13:50:56 -04:00
Elasticsearch uses jUnit for testing, it also uses randomness in the
tests, that can be set using a seed, the following is a cheatsheet of
options for running the tests for ES.
== Creating packages
To create a distribution without running the tests, simply run the
following:
-----------------------------
2018-01-05 17:19:34 -05:00
./gradlew assemble
2013-09-13 13:50:56 -04:00
-----------------------------
2019-02-14 10:13:05 -05:00
To create a platform-specific build including the x-pack modules, use the
following depending on your operating system:
-----------------------------
./gradlew :distribution:archives:linux-tar:assemble --parallel
./gradlew :distribution:archives:darwin-tar:assemble --parallel
./gradlew :distribution:archives:windows-zip:assemble --parallel
-----------------------------
2015-03-26 17:55:25 -04:00
=== Running Elasticsearch from a checkout
In order to run Elasticsearch from source without building a package, you can
2016-07-28 08:38:13 -04:00
run it using Gradle:
2015-03-26 17:55:25 -04:00
-------------------------------------
2018-01-05 17:19:34 -05:00
./gradlew run
2015-03-26 17:55:25 -04:00
-------------------------------------
2018-05-18 08:11:11 -04:00
==== Launching and debugging from an IDE
If you want to run Elasticsearch from your IDE, the `./gradlew run` task
supports a remote debugging option:
---------------------------------------------------------------------------
./gradlew run --debug-jvm
---------------------------------------------------------------------------
==== Distribution
By default a node is started with the zip distribution.
In order to start with a different distribution use the `-Drun.distribution` argument.
To for example start the open source distribution:
-------------------------------------
2019-01-21 15:37:17 -05:00
./gradlew run -Drun.distribution=oss
2018-05-18 08:11:11 -04:00
-------------------------------------
==== License type
By default a node is started with the `basic` license type.
In order to start with a different license type use the `-Drun.license_type` argument.
In order to start a node with a trial license execute the following command:
-------------------------------------
./gradlew run -Drun.license_type=trial
-------------------------------------
This enables security and other paid features and adds a superuser with the username: `elastic-admin` and
password: `elastic-password`.
==== Other useful arguments
In order to start a node with a different max heap space add: `-Dtests.heap.size=4G`
In order to disable annotations add: `-Dtests.asserts=false`
In order to set an Elasticsearch setting, provide a setting with the following prefix: `-Dtests.es.`
2013-09-13 13:50:56 -04:00
=== Test case filtering.
- `tests.class` is a class-filtering shell-like glob pattern,
- `tests.method` is a method-filtering glob pattern.
Run a single test case (variants)
----------------------------------------------------------
2018-01-05 17:19:34 -05:00
./gradlew test -Dtests.class=org.elasticsearch.package.ClassName
./gradlew test "-Dtests.class=*.ClassName"
2013-09-13 13:50:56 -04:00
----------------------------------------------------------
2018-10-23 05:36:42 -04:00
Run all tests in a package and its sub-packages
2013-09-13 13:50:56 -04:00
----------------------------------------------------
2018-01-05 17:19:34 -05:00
./gradlew test "-Dtests.class=org.elasticsearch.package.*"
2013-09-13 13:50:56 -04:00
----------------------------------------------------
2018-10-23 05:36:42 -04:00
Run any test methods that contain 'esi' (like: ...r*esi*ze...)
2013-09-13 13:50:56 -04:00
-------------------------------
2018-01-05 17:19:34 -05:00
./gradlew test "-Dtests.method=*esi*"
2013-09-13 13:50:56 -04:00
-------------------------------
2018-10-23 05:36:42 -04:00
Run all tests that are waiting for a bugfix (disabled by default)
2014-07-03 04:52:29 -04:00
------------------------------------------------
2018-10-23 05:36:42 -04:00
./gradlew test -Dtests.filter=@awaitsfix
2014-07-03 04:52:29 -04:00
------------------------------------------------
2013-09-13 13:50:56 -04:00
=== Seed and repetitions.
Run with a given seed (seed is a hex-encoded long).
------------------------------
2018-01-05 17:19:34 -05:00
./gradlew test -Dtests.seed=DEADBEEF
2013-09-13 13:50:56 -04:00
------------------------------
=== Repeats _all_ tests of ClassName N times.
2015-08-04 09:36:22 -04:00
Every test repetition will have a different method seed
2014-02-20 08:46:59 -05:00
(derived from a single random master seed).
2013-09-13 13:50:56 -04:00
--------------------------------------------------
2018-01-05 17:19:34 -05:00
./gradlew test -Dtests.iters=N -Dtests.class=*.ClassName
2013-09-13 13:50:56 -04:00
--------------------------------------------------
=== Repeats _all_ tests of ClassName N times.
2014-02-20 08:46:59 -05:00
Every test repetition will have exactly the same master (0xdead) and
method-level (0xbeef) seed.
2013-09-13 13:50:56 -04:00
------------------------------------------------------------------------
2018-01-05 17:19:34 -05:00
./gradlew test -Dtests.iters=N -Dtests.class=*.ClassName -Dtests.seed=DEAD:BEEF
2013-09-13 13:50:56 -04:00
------------------------------------------------------------------------
=== Repeats a given test N times
(note the filters - individual test repetitions are given suffixes,
ie: testFoo[0], testFoo[1], etc... so using testmethod or tests.method
ending in a glob is necessary to ensure iterations are run).
-------------------------------------------------------------------------
2018-01-05 17:19:34 -05:00
./gradlew test -Dtests.iters=N -Dtests.class=*.ClassName -Dtests.method=mytest*
2013-09-13 13:50:56 -04:00
-------------------------------------------------------------------------
Repeats N times but skips any tests after the first failure or M initial failures.
-------------------------------------------------------------
2018-01-05 17:19:34 -05:00
./gradlew test -Dtests.iters=N -Dtests.failfast=true -Dtestcase=...
./gradlew test -Dtests.iters=N -Dtests.maxfailures=M -Dtestcase=...
2013-09-13 13:50:56 -04:00
-------------------------------------------------------------
=== Test groups.
Test groups can be enabled or disabled (true/false).
Default value provided below in [brackets].
------------------------------------------------------------------
2018-01-05 17:19:34 -05:00
./gradlew test -Dtests.awaitsfix=[false] - known issue (@AwaitsFix)
2013-09-13 13:50:56 -04:00
------------------------------------------------------------------
=== Load balancing and caches.
2019-10-16 16:24:50 -04:00
By default the tests run on multiple processes using all the available cores on all
2018-11-19 07:01:32 -05:00
available CPUs. Not including hyper-threading.
If you want to explicitly specify the number of JVMs you can do so on the command
2015-10-08 15:16:47 -04:00
line:
2013-09-13 13:50:56 -04:00
----------------------------
2018-01-05 17:19:34 -05:00
./gradlew test -Dtests.jvms=8
2013-09-13 13:50:56 -04:00
----------------------------
2015-10-08 15:16:47 -04:00
Or in `~/.gradle/gradle.properties`:
----------------------------
systemProp.tests.jvms=8
----------------------------
Its difficult to pick the "right" number here. Hypercores don't count for CPU
intensive tests and you should leave some slack for JVM-interal threads like
the garbage collector. And you have to have enough RAM to handle each JVM.
2014-02-25 06:38:22 -05:00
2014-04-22 05:36:53 -04:00
=== Test compatibility.
It is possible to provide a version that allows to adapt the tests behaviour
to older features or bugs that have been changed or fixed in the meantime.
-----------------------------------------
2018-01-05 17:19:34 -05:00
./gradlew test -Dtests.compatibility=1.0.0
2014-04-22 05:36:53 -04:00
-----------------------------------------
2014-02-25 06:38:22 -05:00
2013-09-13 13:50:56 -04:00
=== Miscellaneous.
Run all tests without stopping on errors (inspect log files).
-----------------------------------------
2018-01-05 17:19:34 -05:00
./gradlew test -Dtests.haltonfailure=false
2013-09-13 13:50:56 -04:00
-----------------------------------------
Run more verbose output (slave JVM parameters, etc.).
----------------------
2018-01-05 17:19:34 -05:00
./gradlew test -verbose
2013-09-13 13:50:56 -04:00
----------------------
2014-02-25 06:38:22 -05:00
Change the default suite timeout to 5 seconds for all
tests (note the exclamation mark).
2013-09-13 13:50:56 -04:00
---------------------------------------
2018-01-05 17:19:34 -05:00
./gradlew test -Dtests.timeoutSuite=5000! ...
2013-09-13 13:50:56 -04:00
---------------------------------------
2018-01-05 17:19:34 -05:00
Change the logging level of ES (not Gradle)
2013-09-13 13:50:56 -04:00
--------------------------------
2018-01-05 17:19:34 -05:00
./gradlew test -Dtests.es.logger.level=DEBUG
2013-09-13 13:50:56 -04:00
--------------------------------
Print all the logging output from the test runs to the commandline
even if tests are passing.
------------------------------
2018-01-05 17:19:34 -05:00
./gradlew test -Dtests.output=always
2013-09-13 13:50:56 -04:00
------------------------------
2013-12-07 09:41:51 -05:00
2014-08-21 07:11:49 -04:00
Configure the heap size.
------------------------------
2018-01-05 17:19:34 -05:00
./gradlew test -Dtests.heap.size=512m
2014-08-21 07:11:49 -04:00
------------------------------
Pass arbitrary jvm arguments.
------------------------------
2015-10-28 15:28:30 -04:00
# specify heap dump path
2018-01-05 17:19:34 -05:00
./gradlew test -Dtests.jvm.argline="-XX:HeapDumpPath=/path/to/heapdumps"
2015-10-28 15:28:30 -04:00
# enable gc logging
2018-01-05 17:19:34 -05:00
./gradlew test -Dtests.jvm.argline="-verbose:gc"
2015-10-28 15:28:30 -04:00
# enable security debugging
2018-01-05 17:19:34 -05:00
./gradlew test -Dtests.jvm.argline="-Djava.security.debug=access,failure"
2014-08-21 07:11:49 -04:00
------------------------------
2015-10-29 14:40:19 -04:00
== Running verification tasks
2015-08-03 23:08:18 -04:00
2015-10-29 14:40:19 -04:00
To run all verification tasks, including static checks, unit tests, and integration tests:
2015-08-03 23:08:18 -04:00
---------------------------------------------------------------------------
2018-01-05 17:19:34 -05:00
./gradlew check
2015-08-03 23:08:18 -04:00
---------------------------------------------------------------------------
2015-10-29 14:40:19 -04:00
Note that this will also run the unit tests and precommit tasks first. If you want to just
run the integration tests (because you are debugging them):
2015-08-03 23:08:18 -04:00
---------------------------------------------------------------------------
2018-01-05 17:19:34 -05:00
./gradlew integTest
2015-10-29 14:40:19 -04:00
---------------------------------------------------------------------------
If you want to just run the precommit checks:
---------------------------------------------------------------------------
2018-01-05 17:19:34 -05:00
./gradlew precommit
2015-08-03 23:08:18 -04:00
---------------------------------------------------------------------------
2018-11-29 02:43:16 -05:00
Some of these checks will require `docker-compose` installed for bringing up
test fixtures. If it's not present those checks will be skipped automatically.
2013-12-07 09:41:51 -05:00
== Testing the REST layer
The available integration tests make use of the java API to communicate with
the elasticsearch nodes, using the internal binary transport (port 9300 by
default).
The REST layer is tested through specific tests that are shared between all
2013-12-27 14:36:12 -05:00
the elasticsearch official clients and consist of YAML files that describe the
operations to be executed and the obtained results that need to be tested.
2013-12-07 09:41:51 -05:00
2018-02-09 18:05:19 -05:00
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]
2018-01-05 17:19:34 -05:00
The REST tests are run automatically when executing the "./gradlew check" command. To run only the
2013-12-18 04:55:57 -05:00
REST tests use the following command:
---------------------------------------------------------------------------
2018-03-09 14:54:59 -05:00
./gradlew :distribution:archives:integ-test-zip:integTest \
2016-08-09 11:02:27 -04:00
-Dtests.class="org.elasticsearch.test.rest.*Yaml*IT"
2015-12-01 11:33:46 -05:00
---------------------------------------------------------------------------
A specific test case can be run with
---------------------------------------------------------------------------
2018-03-09 14:54:59 -05:00
./gradlew :distribution:archives:integ-test-zip:integTest \
2016-08-09 11:02:27 -04:00
-Dtests.class="org.elasticsearch.test.rest.*Yaml*IT" \
2015-12-01 11:33:46 -05:00
-Dtests.method="test {p0=cat.shards/10_basic/Help}"
2013-12-18 04:55:57 -05:00
---------------------------------------------------------------------------
2016-08-03 07:50:24 -04:00
`*Yaml*IT` are the executable test classes that runs all the
2013-12-27 14:43:16 -05:00
yaml suites available within the `rest-api-spec` folder.
2013-12-07 09:41:51 -05:00
2014-03-10 08:48:17 -04:00
The REST tests support all the options provided by the randomized runner, plus the following:
2013-12-07 09:41:51 -05:00
2014-03-10 08:48:17 -04:00
* `tests.rest[true|false]`: determines whether the REST tests need to be run (default) or not.
2013-12-07 09:41:51 -05:00
* `tests.rest.suite`: comma separated paths of the test suites to be run
2013-12-27 14:43:16 -05:00
(by default loaded from /rest-api-spec/test). It is possible to run only a subset
2013-12-07 09:41:51 -05:00
of the tests providing a sub-folder or even a single yaml file (the default
2013-12-27 14:43:16 -05:00
/rest-api-spec/test prefix is optional when files are loaded from classpath)
2013-12-07 09:41:51 -05:00
e.g. -Dtests.rest.suite=index,get,create/10_with_id
2014-04-19 05:13:51 -04:00
* `tests.rest.blacklist`: comma separated globs that identify tests that are
blacklisted and need to be skipped
e.g. -Dtests.rest.blacklist=index/*/Index document,get/10_basic/*
2014-03-10 08:48:17 -04:00
Note that the REST tests, like all the integration tests, can be run against an external
cluster by specifying the `tests.cluster` property, which if present needs to contain a
comma separated list of nodes to connect to (e.g. localhost:9300). A transport client will
be created based on that and used for all the before|after test operations, and to extract
the http addresses of the nodes so that REST requests can be sent to them.
2014-05-14 11:52:02 -04:00
2018-05-16 19:42:08 -04:00
== Testing packaging
2015-04-14 10:22:37 -04:00
2019-10-16 16:24:50 -04:00
The packaging tests use Vagrant virtual machines or cloud instances to verify
that installing and running Elasticsearch distributions works correctly on
supported operating systems. These tests should really only be run on ephemeral
systems because they're destructive; that is, these tests install and remove
packages and freely modify system settings, so you will probably regret it if
you execute them on your development machine.
When you run a packaging test, Gradle will set up the target VM and mount your
repository directory in the VM. Once this is done, a Gradle task will issue a
Vagrant command to run a *nested* Gradle task on the VM. This nested Gradle
runs the actual "destructive" test classes.
2015-04-14 10:22:37 -04:00
2015-08-04 09:36:22 -04:00
. Install Virtual Box and Vagrant.
2018-09-20 16:14:56 -04:00
+
2018-05-16 19:42:08 -04:00
. (Optional) Install https://github.com/fgrehm/vagrant-cachier[vagrant-cachier] to squeeze
a bit more performance out of the process:
2018-09-20 16:14:56 -04:00
+
2015-08-04 09:36:22 -04:00
--------------------------------------
vagrant plugin install vagrant-cachier
--------------------------------------
2018-09-20 16:14:56 -04:00
+
2019-10-16 16:24:50 -04:00
. You can run all of the OS packaging tests with `./gradlew packagingTest`.
This task includes our legacy `bats` tests. To run only the OS tests that are
written in Java, run `.gradlew distroTest`, will cause Gradle to build the tar,
zip, and deb packages and all the plugins. It will then run the tests on every
available system. This will take a very long time.
2018-09-20 16:14:56 -04:00
+
2019-10-16 16:24:50 -04:00
Fortunately, the various systems under test have their own Gradle tasks under
`qa/os`. To find out what packaging combinations can be tested on a system, run
the `tasks` task. For example:
2018-09-20 16:14:56 -04:00
+
2019-10-16 16:24:50 -04:00
----------------------------------
./gradlew :qa:os:ubuntu-1804:tasks
----------------------------------
2018-09-20 16:14:56 -04:00
+
2019-10-16 16:24:50 -04:00
If you want a quick test of the tarball and RPM packagings for Centos 7, you
would run:
+
-------------------------------------------------------------------------------------------------
./gradlew :qa:os:centos-7:distroTest.default-rpm :qa:os:centos-7:distroTest.default-linux-archive
-------------------------------------------------------------------------------------------------
Note that if you interrupt Gradle in the middle of running these tasks, any boxes started
will remain running and you'll have to stop them manually with `./gradlew --stop` or
2018-05-16 19:42:08 -04:00
`vagrant halt`.
2015-08-17 11:45:18 -04:00
2015-11-02 12:40:47 -05:00
All the regular vagrant commands should just work so you can get a shell in a
VM running trusty by running
2019-04-02 08:51:11 -04:00
`vagrant up ubuntu-1604 --provider virtualbox && vagrant ssh ubuntu-1604`.
2015-08-04 09:36:22 -04:00
2018-05-16 19:42:08 -04:00
These are the linux flavors supported, all of which we provide images for
2015-08-17 11:45:18 -04:00
2016-09-12 11:58:32 -04:00
* ubuntu-1604 aka xenial
2018-09-28 17:33:29 -04:00
* ubuntu-1804 aka bionic beaver
2017-07-03 04:02:17 -04:00
* debian-8 aka jessie
* debian-9 aka stretch, the current debian stable distribution
2015-08-04 09:36:22 -04:00
* centos-6
* centos-7
2019-05-01 18:53:33 -04:00
* rhel-8
2018-05-22 00:22:00 -04:00
* fedora-28
2019-02-12 07:19:27 -05:00
* fedora-29
2016-09-12 10:56:13 -04:00
* oel-6 aka Oracle Enterprise Linux 6
2015-08-04 09:36:22 -04:00
* oel-7 aka Oracle Enterprise Linux 7
2015-09-22 08:59:39 -04:00
* sles-12
2017-06-01 08:40:47 -04:00
* opensuse-42 aka Leap
2015-08-04 09:36:22 -04:00
We're missing the following from the support matrix because there aren't high
quality boxes available in vagrant atlas:
2015-08-17 11:45:18 -04:00
2015-08-04 09:36:22 -04:00
* sles-11
2018-05-16 19:42:08 -04:00
=== Testing packaging on Windows
The packaging tests also support Windows Server 2012R2 and Windows Server 2016.
Unfortunately we're not able to provide boxes for them in open source use
because of licensing issues. Any Virtualbox image that has WinRM and Powershell
enabled for remote users should work.
Testing on Windows requires the https://github.com/criteo/vagrant-winrm[vagrant-winrm] plugin.
------------------------------------
vagrant plugin install vagrant-winrm
------------------------------------
Specify the image IDs of the Windows boxes to gradle with the following project
properties. They can be set in `~/.gradle/gradle.properties` like
------------------------------------
vagrant.windows-2012r2.id=my-image-id
vagrant.windows-2016.id=another-image-id
------------------------------------
or passed on the command line like `-Pvagrant.windows-2012r2.id=my-image-id`
`-Pvagrant.windows-2016=another-image-id`
These properties are required for Windows support in all gradle tasks that
2019-10-16 16:24:50 -04:00
handle packaging tests. Either or both may be specified.
2015-08-17 11:45:18 -04:00
2018-05-16 19:42:08 -04:00
If you're running vagrant commands outside of gradle, specify the Windows boxes
with the environment variables
* `VAGRANT_WINDOWS_2012R2_BOX`
* `VAGRANT_WINDOWS_2016_BOX`
=== Testing VMs are disposable
2015-08-04 09:36:22 -04:00
2016-04-06 19:08:39 -04:00
It's important to think of VMs like cattle. If they become lame you just shoot
2015-08-04 09:36:22 -04:00
them and let vagrant reprovision them. Say you've hosed your precise VM:
2015-08-17 11:45:18 -04:00
2015-08-04 09:36:22 -04:00
----------------------------------------------------
2019-04-02 08:51:11 -04:00
vagrant ssh ubuntu-1604 -c 'sudo rm -rf /bin'; echo oops
2015-08-04 09:36:22 -04:00
----------------------------------------------------
2015-08-17 11:45:18 -04:00
2015-08-04 09:36:22 -04:00
All you've got to do to get another one is
2015-08-17 11:45:18 -04:00
2015-08-04 09:36:22 -04:00
----------------------------------------------
2019-04-02 08:51:11 -04:00
vagrant destroy -f ubuntu-1604 && vagrant up ubuntu-1604 --provider virtualbox
2015-08-04 09:36:22 -04:00
----------------------------------------------
2015-08-17 11:45:18 -04:00
2015-08-04 09:36:22 -04:00
The whole process takes a minute and a half on a modern laptop, two and a half
without vagrant-cachier.
Its possible that some downloads will fail and it'll be impossible to restart
them. This is a bug in vagrant. See the instructions here for how to work
around it:
https://github.com/mitchellh/vagrant/issues/4479
Some vagrant commands will work on all VMs at once:
2015-08-17 11:45:18 -04:00
2015-08-04 09:36:22 -04:00
------------------
vagrant halt
vagrant destroy -f
------------------
2015-11-02 12:40:47 -05:00
`vagrant up` would normally start all the VMs but we've prevented that because
that'd consume a ton of ram.
2015-08-04 09:36:22 -04:00
2018-05-16 19:42:08 -04:00
=== Iterating on packaging tests
2015-08-04 09:36:22 -04:00
2019-10-16 16:24:50 -04:00
Because our packaging tests are capable of testing many combinations of OS
(e.g., Windows, Linux, etc.), package type (e.g., zip file, RPM, etc.),
Elasticsearch distribution type (e.g., default or OSS), and so forth, it's
faster to develop against smaller subsets of the tests. For example, to run
tests for the default archive distribution on Fedora 28:
2018-05-23 13:37:57 -04:00
2019-10-16 16:24:50 -04:00
-----------------------------------------------------------
./gradlew :qa:os:fedora-28:distroTest.default-linux-archive
-----------------------------------------------------------
2017-01-10 03:45:33 -05:00
2019-10-16 16:24:50 -04:00
These test tasks can use the `--tests`, `--info`, and `--debug` parameters just like
non-OS tests can. For example:
2017-01-10 03:45:33 -05:00
2019-10-16 16:24:50 -04:00
-----------------------------------------------------------
./gradlew :qa:os:fedora-28:distroTest.default-linux-archive \
--tests "com.elasticsearch.packaging.test.ArchiveTests"
-----------------------------------------------------------
2017-01-10 03:45:33 -05:00
2017-05-18 04:14:24 -04:00
== Testing backwards compatibility
Backwards compatibility tests exist to test upgrading from each supported version
2018-11-02 00:34:29 -04:00
to the current version. To run them all use:
2017-05-18 04:14:24 -04:00
-------------------------------------------------
2018-01-05 17:19:34 -05:00
./gradlew bwcTest
2017-05-18 04:14:24 -04:00
-------------------------------------------------
2018-11-02 00:34:29 -04:00
A specific version can be tested as well. For example, to test bwc with
2017-05-18 04:14:24 -04:00
version 5.3.2 run:
-------------------------------------------------
2018-01-05 17:19:34 -05:00
./gradlew v5.3.2#bwcTest
2017-05-18 04:14:24 -04:00
-------------------------------------------------
2018-11-02 00:34:29 -04:00
Tests are ran for versions that are not yet released but with which the current version will be compatible with.
These are automatically checked out and built from source.
See link:./buildSrc/src/main/java/org/elasticsearch/gradle/VersionCollection.java[VersionCollection]
2019-10-16 16:24:50 -04:00
and link:./distribution/bwc/build.gradle[distribution/bwc/build.gradle]
2018-11-02 00:34:29 -04:00
for more information.
When running `./gradlew check`, minimal bwc checks are also run against compatible versions that are not yet released.
2017-01-10 03:45:33 -05:00
2018-11-02 00:34:29 -04:00
==== BWC Testing against a specific remote/branch
2017-07-07 05:18:03 -04:00
Sometimes a backward compatibility change spans two versions. A common case is a new functionality
2018-03-29 14:59:52 -04:00
that needs a BWC bridge in an unreleased versioned of a release branch (for example, 5.x).
2018-01-05 17:19:34 -05:00
To test the changes, you can instruct Gradle to build the BWC version from a another remote/branch combination instead of
2019-08-16 11:43:14 -04:00
pulling the release branch from GitHub. You do so using the `bwc.remote` and `bwc.refspec.BRANCH` system properties:
2017-07-07 05:18:03 -04:00
-------------------------------------------------
2019-08-16 11:43:14 -04:00
./gradlew check -Dbwc.remote=${remote} -Dbwc.refspec.5.x=index_req_bwc_5.x
2017-07-07 05:18:03 -04:00
-------------------------------------------------
2017-10-07 13:40:18 -04:00
The branch needs to be available on the remote that the BWC makes of the
repository you run the tests from. Using the remote is a handy trick to make
sure that a branch is available and is up to date in the case of multiple runs.
2017-07-07 05:18:03 -04:00
Example:
2017-10-07 13:40:18 -04:00
Say you need to make a change to `master` and have a BWC layer in `5.x`. You
will need to:
. Create a branch called `index_req_change` off your remote `${remote}`. This
will contain your change.
2017-07-07 05:18:03 -04:00
. Create a branch called `index_req_bwc_5.x` off `5.x`. This will contain your bwc layer.
2017-10-07 13:40:18 -04:00
. Push both branches to your remote repository.
2019-08-16 11:43:14 -04:00
. Run the tests with `./gradlew check -Dbwc.remote=${remote} -Dbwc.refspec.5.x=index_req_bwc_5.x`.
2017-07-07 05:18:03 -04:00
2018-11-02 00:34:29 -04:00
==== Skip fetching latest
2018-04-13 09:31:06 -04:00
For some BWC testing scenarios, you want to use the local clone of the
repository without fetching latest. For these use cases, you can set the system
property `tests.bwc.git_fetch_latest` to `false` and the BWC builds will skip
fetching the latest from the remote.
2019-03-01 09:13:39 -05:00
== How to write good tests?
=== Base classes for test cases
There are multiple base classes for tests:
2019-03-01 09:16:52 -05:00
* **`ESTestCase`**: The base class of all tests. It is typically extended
directly by unit tests.
* **`ESSingleNodeTestCase`**: This test case sets up a cluster that has a
single node.
* **`ESIntegTestCase`**: An integration test case that creates a cluster that
might have multiple nodes.
* **`ESRestTestCase`**: An integration tests that interacts with an external
cluster via the REST API. For instance, YAML tests run via sub classes of
`ESRestTestCase`.
2019-03-01 09:13:39 -05:00
=== Good practices
==== What kind of tests should I write?
Unit tests are the preferred way to test some functionality: most of the time
they are simpler to understand, more likely to reproduce, and unlikely to be
affected by changes that are unrelated to the piece of functionality that is
being tested.
The reason why `ESSingleNodeTestCase` exists is that all our components used to
be very hard to set up in isolation, which had led us to having a number of
integration tests but close to no unit tests. `ESSingleNodeTestCase` is a
workaround for this issue which provides an easy way to spin up a node and get
access to components that are hard to instantiate like `IndicesService`.
Whenever practical, you should prefer unit tests.
Many tests extend `ESIntegTestCase`, mostly because this is how most tests used
to work in the early days of Elasticsearch. However the complexity of these
tests tends to make them hard to debug. Whenever the functionality that is
being tested isn't intimately dependent on how Elasticsearch behaves as a
cluster, it is recommended to write unit tests or REST tests instead.
In short, most new functionality should come with unit tests, and optionally
REST tests to test integration.
==== Refactor code to make it easier to test
Unfortunately, a large part of our code base is still hard to unit test.
Sometimes because some classes have lots of dependencies that make them hard to
instantiate. Sometimes because API contracts make tests hard to write. Code
refactors that make functionality easier to unit test are encouraged. If this
sounds very abstract to you, you can have a look at
https://github.com/elastic/elasticsearch/pull/16610[this pull request] for
instance, which is a good example. It refactors `IndicesRequestCache` in such
a way that:
- it no longer depends on objects that are hard to instantiate such as
`IndexShard` or `SearchContext`,
- time-based eviction is applied on top of the cache rather than internally,
which makes it easier to assert on what the cache is expected to contain at
a given time.
=== Bad practices
==== Use randomized-testing for coverage
In general, randomization should be used for parameters that are not expected
to affect the behavior of the functionality that is being tested. For instance
the number of shards should not impact `date_histogram` aggregations, and the
choice of the `store` type (`niofs` vs `mmapfs`) does not affect the results of
a query. Such randomization helps improve confidence that we are not relying on
implementation details of one component or specifics of some setup.
However it should not be used for coverage. For instance if you are testing a
piece of functionality that enters different code paths depending on whether
the index has 1 shards or 2+ shards, then we shouldn't just test against an
index with a random number of shards: there should be one test for the 1-shard
case, and another test for the 2+ shards case.
==== Abuse randomization in multi-threaded tests
Multi-threaded tests are often not reproducible due to the fact that there is
no guarantee on the order in which operations occur across threads. Adding
randomization to the mix usually makes things worse and should be done with
care.
2018-03-27 17:29:19 -04:00
== Test coverage analysis
2015-08-03 23:08:18 -04:00
2018-03-27 17:29:19 -04:00
Generating test coverage reports for Elasticsearch is currently not possible through Gradle.
2019-10-16 16:24:50 -04:00
However, it _is_ possible to gain insight in code coverage using IntelliJ's built-in coverage
2018-03-27 17:29:19 -04:00
analysis tool that can measure coverage upon executing specific tests. Eclipse may also be able
to do the same using the EclEmma plugin.
2015-09-01 20:57:08 -04:00
2018-03-27 17:29:19 -04:00
Test coverage reporting used to be possible with JaCoCo when Elasticsearch was using Maven
as its build system. Since the switch to Gradle though, this is no longer possible, seeing as
the code currently used to build Elasticsearch does not allow JaCoCo to recognize its tests.
For more information on this, see the discussion in https://github.com/elastic/elasticsearch/issues/28867[issue #28867].
2015-09-01 20:57:08 -04:00
2017-01-13 08:57:40 -05:00
== Debugging remotely from an IDE
2017-01-16 04:26:12 -05:00
If you want to run Elasticsearch and be able to remotely attach the process
for debugging purposes from your IDE, can start Elasticsearch using `ES_JAVA_OPTS`:
2017-01-13 08:57:40 -05:00
---------------------------------------------------------------------------
2017-01-16 04:26:12 -05:00
ES_JAVA_OPTS="-Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=4000,suspend=y" ./bin/elasticsearch
2017-01-13 08:57:40 -05:00
---------------------------------------------------------------------------
2017-01-16 04:26:12 -05:00
Read your IDE documentation for how to attach a debugger to a JVM process.
2017-01-13 08:57:40 -05:00
2015-11-20 14:58:50 -05:00
== Building with extra plugins
Additional plugins may be built alongside elasticsearch, where their
dependency on elasticsearch will be substituted with the local elasticsearch
2016-12-14 18:02:07 -05:00
build. To add your plugin, create a directory called elasticsearch-extra as
a sibling of elasticsearch. Checkout your plugin underneath elasticsearch-extra
and the build will automatically pick it up. You can verify the plugin is
included as part of the build by checking the projects of the build.
2015-11-20 14:58:50 -05:00
---------------------------------------------------------------------------
2018-01-05 17:19:34 -05:00
./gradlew projects
2015-11-20 14:58:50 -05:00
---------------------------------------------------------------------------
2017-08-23 04:59:25 -04:00
== Environment misc
There is a known issue with macOS localhost resolve strategy that can cause
some integration tests to fail. This is because integration tests have timings
for cluster formation, discovery, etc. that can be exceeded if name resolution
takes a long time.
To fix this, make sure you have your computer name (as returned by `hostname`)
inside `/etc/hosts`, e.g.:
....
127.0.0.1 localhost ElasticMBP.local
255.255.255.255 broadcasthost
::1 localhost ElasticMBP.local`
....
2019-01-23 06:15:00 -05:00
== Benchmarking
For changes that might affect the performance characteristics of Elasticsearch
you should also run macrobenchmarks. We maintain a macrobenchmarking tool
called https://github.com/elastic/rally[Rally]
which you can use to measure the performance impact. It comes with a set of
default benchmarks that we also
https://elasticsearch-benchmarks.elastic.co/[run every night]. To get started,
please see https://esrally.readthedocs.io/en/stable/[Rally's documentation].