README.asciidoc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. The Elasticsearch docs are in AsciiDoc format and can be built using the
  2. Elasticsearch documentation build process.
  3. See: https://github.com/elastic/docs
  4. Snippets marked with `// CONSOLE` are automatically annotated with "VIEW IN
  5. CONSOLE" and "COPY AS CURL" in the documentation and are automatically tested
  6. by the command `gradle :docs:check`. To test just the docs from a single page,
  7. use e.g. `gradle :docs:check -Dtests.method="\*rollover*"`.
  8. By default each `// CONSOLE` snippet runs as its own isolated test. You can
  9. manipulate the test execution in the following ways:
  10. * `// TEST`: Explicitly marks a snippet as a test. Snippets marked this way
  11. are tests even if they don't have `// CONSOLE` but usually `// TEST` is used
  12. for its modifiers:
  13. * `// TEST[s/foo/bar/]`: Replace `foo` with `bar` in the generated test. This
  14. should be used sparingly because it makes the snippet "lie". Sometimes,
  15. though, you can use it to make the snippet more clear more clear. Keep in
  16. mind the that if there are multiple substitutions then they are applied in
  17. the order that they are defined.
  18. * `// TEST[catch:foo]`: Used to expect errors in the requests. Replace `foo`
  19. with `request` to expect a 400 error, for example. If the snippet contains
  20. multiple requests then only the last request will expect the error.
  21. * `// TEST[continued]`: Continue the test started in the last snippet. Between
  22. tests the nodes are cleaned: indexes are removed, etc. This prevents that
  23. from happening between snippets because the two snippets are a single test.
  24. This is most useful when you have text and snippets that work together to
  25. tell the story of some use case because it merges the snippets (and thus the
  26. use case) into one big test.
  27. * `// TEST[skip:reason]`: Skip this test. Replace `reason` with the actual
  28. reason to skip the test. Snippets without `// TEST` or `// CONSOLE` aren't
  29. considered tests anyway but this is useful for explicitly documenting the
  30. reason why the test shouldn't be run.
  31. * `// TEST[setup:name]`: Run some setup code before running the snippet. This
  32. is useful for creating and populating indexes used in the snippet. The setup
  33. code is defined in `docs/build.gradle`.
  34. * `// TEST[warning:some warning]`: Expect the response to include a `Warning`
  35. header. If the response doesn't include a `Warning` header with the exact
  36. text then the test fails. If the response includes `Warning` headers that
  37. aren't expected then the test fails.
  38. * `// TESTRESPONSE`: Matches this snippet against the body of the response of
  39. the last test. If the response is JSON then order is ignored. If you add
  40. `// TEST[continued]` to the snippet after `// TESTRESPONSE` it will continue
  41. in the same test, allowing you to interleave requests with responses to check.
  42. * `// TESTRESPONSE[s/foo/bar/]`: Substitutions. See `// TEST[s/foo/bar]` for
  43. how it works. These are much more common than `// TEST[s/foo/bar]` because
  44. they are useful for eliding portions of the response that are not pertinent
  45. to the documentation.
  46. * One interesting difference here is that you often want to match against
  47. the response from Elasticsearch. To do that you can reference the "body" of
  48. the response like this: `// TESTRESPONSE[s/"took": 25/"took": $body.took/]`.
  49. Note the `$body` string. This says "I don't expect that 25 number in the
  50. response, just match against what is in the response." Instead of writing
  51. the path into the response after `$body` you can write `$_path` which
  52. "figures out" the path. This is especially useful for making sweeping
  53. assertions like "I made up all the numbers in this example, don't compare
  54. them" which looks like `// TESTRESPONSE[s/\d+/$body.$_path/]`.
  55. * `// TESTRESPONSE[_cat]`: Add substitutions for testing `_cat` responses. Use
  56. this after all other substitutions so it doesn't make other substitutions
  57. difficult.
  58. * `// TESTSETUP`: Marks this snippet as the "setup" for all other snippets in
  59. this file. This is a somewhat natural way of structuring documentation. You
  60. say "this is the data we use to explain this feature" then you add the
  61. snippet that you mark `// TESTSETUP` and then every snippet will turn into
  62. a test that runs the setup snippet first. See the "painless" docs for a file
  63. that puts this to good use. This is fairly similar to `// TEST[setup:name]`
  64. but rather than the setup defined in `docs/build.gradle` the setup is defined
  65. right in the documentation file.
  66. In addition to the standard CONSOLE syntax these snippets can contain blocks
  67. of yaml surrounded by markers like this:
  68. ```
  69. startyaml
  70. - compare_analyzers: {index: thai_example, first: thai, second: rebuilt_thai}
  71. endyaml
  72. ```
  73. This allows slightly more expressive testing of the snippets. Since that syntax
  74. is not supported by CONSOLE the usual way to incorporate it is with a
  75. `// TEST[s//]` marker like this:
  76. ```
  77. // TEST[s/\n$/\nstartyaml\n - compare_analyzers: {index: thai_example, first: thai, second: rebuilt_thai}\nendyaml\n/]
  78. ```
  79. Any place you can use json you can use elements like `$body.path.to.thing`
  80. which is replaced on the fly with the contents of the thing at `path.to.thing`
  81. in the last response.