Testing Antora sites
This project provides utilities and guidelines for testing Antora documentation sites.
Quarkiverse projects usually host their Antora documentation in a dedicated docs Maven module.
After adding the quarkus-antora dependency, you can not only
life-edit the site in Quarkus dev mode but also
-
Test that Antora is able to generate the site from your AsciiDoc sources
-
Ensure that all external links are valid.
Basic @QuarkusTest
The way we test the site generation is not much different from testing any other Quarkus application.
First, you need to add the necessary dependencies
<dependency>
<groupId>io.quarkiverse.antora</groupId>
<artifactId>quarkus-antora</artifactId>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
Then you can add a basic @QuarkusTest as follows:
import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.Test;
import io.quarkiverse.antorassured.AntorAssured;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
@QuarkusTest /* the @QuarkusTest annotation lets Quarkus build and start the Antora site on http://localhost:8081/ */
public class AntoraSiteTest {
@Test
public void getIndex() {
/*
* If the site was built successfully,
* we can get the index page and check that
* it contains some expected content
*/
RestAssured
.given()
.contentType(ContentType.HTML)
.get("/quarkus-antora/dev/index.html")
.then()
.statusCode(200)
.body(CoreMatchers.containsString("<h1 class=\"page\">Quarkus Antora</h1>"));
}
...
}
|
The sample code snippets used in this section come from the
|
Detect dead links with AntorAssured
Antora takes care for validating internal cross-references but it does not help much with ensuring
that external links point to existing resources.
Page fragments (the part starting with # in page.html#anchor) are not validated by Antora too.
That’s where the io.quarkiverse.antorassured.AntorAssured class provided by Quarkus Antora may come in handy.
To use it, you need to add the following dependency:
<dependency>
<groupId>io.quarkiverse.antora</groupId>
<artifactId>quarkus-antorassured</artifactId>
<scope>test</scope>
</dependency>
Then you can use it in a test as follows:
@Test
public void validateLinks() {
AntorAssured
.links()
.excludeResolved(
/* Broken links available in test-page.adoc for the sake of testing */
"https://salkjasjhashgajhhsahgahjas.com",
"https://quarkus.io/fake-page",
"https://quarkus.io/guides/building-native-image#fake-fragment",
"http://localhost:8083/retry-me",
/* When running this test, the port is 8081, so the following link is not expected to work */
"http://localhost:8080/quarkus-antora/dev/index.html")
.excludeEditThisPage()
/*
* Retry once (default) in case of HTTP status 301, 429, 500, 501, 502, 503
* or 504, after 10 seconds or what the Retry-After HTTP header prescribes,
* but at most 120 seconds
*/
.retryAttempts(1)
.validate()
.assertValid();
}
In the test, we instruct the link validator to ignore some remote URIs, that we intentionally placed into test-page.adoc
for the sake of testing AntoraAssured itself.
If you would like to see the dead link detection in action, just remove the whole excludeResolved(…) method invocation.
After that, you should see the test fail with a report like the following:
[ERROR] AntoraSiteTest.validateLinks:48
- /home/user/projects/quarkus-antora/docs/modules/ROOT/pages/index.adoc:59
- http://localhost:8080/quarkus-antora/dev/index.html -> http://localhost:8080/quarkus-antora/dev/index.html
- Unable to connect: Connection refused
- /home/user/projects/quarkus-antora/docs/modules/ROOT/pages/test-page.adoc:15
- https://quarkus.io/fake-page -> https://quarkus.io/fake-page
- 404
- /home/user/projects/quarkus-antora/docs/modules/ROOT/pages/test-page.adoc:14
- https://salkjasjhashgajhhsahgahjas.com -> https://salkjasjhashgajhhsahgahjas.com
- Unknown host salkjasjhashgajhhsahgahjas.com
- /home/user/projects/quarkus-antora/docs/modules/ROOT/pages/test-page.adoc:16
- https://quarkus.io/guides/building-native-image#fake-fragment -> https://quarkus.io/guides/building-native-image#fake-fragment
- Could not find #fake-fragment
Persistent cache of remote pages
Link validation typically runs with every build, while most remote pages change rarely.
Some remote servers do not tolerate being accessed that often and respond with 429 Too Many Requests.
Therefore, since Quarkus Antora 4.0.0, AntorAssured stores the remote pages it has fetched in the local file system
and reuses them in subsequent runs.
The cache is enabled by default.
| Setting | Default |
|---|---|
Cache directory |
|
Expiration: pages fetched longer ago are fetched again |
36 hours |
Garbage collection age: pages fetched longer ago are deleted from the cache directory |
180 days |
The cache works as follows:
-
Before a page is fetched, the cache directory is consulted. If the page was stored there within the expiration period, it is not requested from the remote server. The stored page is used instead, also for validating the fragments of the links.
-
A cached page does not count against any
RateLimitand it is never delayed by it. It is still counted by policies such asAggregatePolicy.countAtLeast(200, n). -
Only responses having a
2xxstatus are stored. Other responses, such as404,429or503, timeouts and connection failures are never stored, so broken links are checked against the remote server with each run. -
The site under test is never stored. By default, this applies to all URIs whose host is
localhostor a loopback IP address, such as127.0.0.1.LinkStream.validate()(without parameters) in addition excludes all URIs under the base URI of the site under test, which is important if you setquarkus.http.test-hostto some other host name. -
URIs containing credentials, such as
https://user:password@example.com/, are never stored. -
Pages requested with different
Acceptheaders are stored separately. Other request headers are not taken into account. -
The cache directory can be used by several builds at the same time.
Stale pages
The cache makes the validation faster and less demanding for the remote servers at the cost of working with pages that may be up to 36 hours old (or whatever expiration you set):
-
A link to a page that was removed from the remote server in the meantime is still reported as valid.
-
A link to a fragment that was added to the remote page in the meantime is still reported as invalid.
Both issues go away after the cached page expires.
If you do not want to wait, delete the cache directory or only the directory of the given page.
The path of the directory is derived from the URI of the page as follows:
<cache directory>/<reversed host name>/<scheme>/<port>/<accept header>/<path>.
For example, https://quarkus.io/guides/building-native-image is stored in
~/.cache/antorassured/pages/io/quarkus/https/443/no-accept/guides/building-native-image.
Characters that are not allowed in file names on some operating system are replaced by underscores.
Deleting the files is always safe. The validation never fails because of a missing or broken content of the cache directory.
Garbage collection
At the end of each validation that has used the cache, the pages fetched more than 180 days ago are deleted from the cache
directory, together with the directories that became empty.
These are typically pages that are not linked from your documentation anymore.
Files not created by AntorAssured are never deleted.
Using the cache on CI
The cache is useful on CI only if the cache directory is restored before the build and saved after it.
The directory to save and restore is ~/.cache/antorassured/pages, unless you have configured another one.
It can be copied to another machine, even to one running a different operating system.
|
The pages are stored as they were received, without any encryption.
Keep that in mind if you validate links to private resources, for example using |
Configuring the cache
The cache can be configured only by passing a custom LinkValidatorImpl to LinkStream.validate(LinkValidator):
AntorAssured
.links()
.excludeEditThisPage()
.validate(
new LinkValidatorImpl(
/* Where the pages are stored */
Path.of("target/antorassured-cache"),
/* Pages fetched more than 12 hours ago are fetched again */
Duration.ofHours(12),
/* Pages fetched more than 30 days ago are deleted from the cache directory */
Duration.ofDays(30),
LinkValidatorImpl.DEFAULT_CACHE_LOCK_RETRY_DELAY,
LinkValidatorImpl.DEFAULT_CACHE_LOCK_RETRIES,
/* Which URIs may be cached: anything but the site under test running on localhost */
LinkValidatorImpl.notLoopback(),
Clock.systemUTC()))
.assertValid();
|
A |
There is no dedicated option for disabling the cache.
Pass a predicate returning false for every URI instead:
AntorAssured
.links()
.excludeEditThisPage()
.validate(
new LinkValidatorImpl(
LinkValidatorImpl.defaultCacheDirectory(),
LinkValidatorImpl.DEFAULT_CACHE_EXPIRATION,
LinkValidatorImpl.DEFAULT_CACHE_GC_AGE,
LinkValidatorImpl.DEFAULT_CACHE_LOCK_RETRY_DELAY,
LinkValidatorImpl.DEFAULT_CACHE_LOCK_RETRIES,
/* No URI may be cached */
uri -> false,
Clock.systemUTC()))
.assertValid();