Wiki source code of Docker-based Testing
Last modified by Vincent Massol on 2021/03/29 15:08
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | {{box cssClass="floatinginfobox" title="**Contents**"}} | ||
2 | {{toc/}} | ||
3 | {{/box}} | ||
4 | |||
5 | = Usage = | ||
6 | |||
7 | * Based on [[TestContainers>>https://www.testcontainers.org/]] and uses Docker to execute the tests under various Databases, Servlet Engines and Browsers. | ||
8 | ** [[Introductory talk about TestContainers and XWiki at Devoxx FR 2019>>https://youtu.be/0TvWv4L_IJM]] (In French) | ||
9 | * The only requirements for running these tests is to have Docker installed locally and to have the user under which you run your IDE and the Maven build be able to use the docker executable. | ||
10 | * Tests are marked as docker-based tests with the ##@UITest## annotation. | ||
11 | * Tests get injected ##XWikiWebDriver##, ##TestUtils##, ##TestInfo## and ##TestReference## instances as test method parameters. | ||
12 | * Test results is recording in a FLV file in the ##target## directory. | ||
13 | * A screenshot of the UI when the test is failing is also taken in the ##target## directory. | ||
14 | * When test execute it's possible to connect to the running VNC server and see the UI by using a VNC client and connect to the VNC URL printed in the console, e.g. ##vnc:~/~/vnc:[email protected]:32936##. | ||
15 | ** Note that if you want to debug and interact with the XWiki UI you should use a ##servletEngine## other than the default Jetty Standalone one since otherwise the debug point in your IDE will prevent the XWiki code from executing and you'll see a spinning wheel until you step forward in your debugger. You can use for example ##servletEngine = ServletEngine.TOMCAT##. | ||
16 | * The version for the platform dependencies is specified using the ##platform.version## property in the ##pom.xml## file executing the tests. | ||
17 | * The test configuration options can be accessed from tests by specifying a method parameter of type ##TestConfiguration## (see examples below). | ||
18 | ** You can get access to the IP address/port to use to connect to XWiki from the host machine: ##testConfiguration.getServletEngine().getIP()/getPort()## | ||
19 | ** You can get access to the IP address to use to connect to the host machine from inside XWiki: ##testConfiguration.getServletEngine().getHostIP()## | ||
20 | ** You can get access to the IP address/port used internally by XWiki (for example to generate URLs): ##testConfiguration.getServletEngine().getInternalIP()/getInternalPort()## | ||
21 | |||
22 | = Configurations options = | ||
23 | |||
24 | {{info}} | ||
25 | Note: | ||
26 | |||
27 | * When passing as Java annotation attributes, in functional tests, the format is ##@UITest(<Option Name> = <Value>)## | ||
28 | * When passing the options as System Properties, the format is ##-Dxwiki.test.ui.<Option Name>=<Value>## | ||
29 | ** Except for passing ##properties## where the format is ##-Dxwiki.test.ui.properties.<Property name>=<Property Value>## | ||
30 | ** Except for ##extraJARs##, ##sshPorts## & ##forbiddenServletEngines## which cannot be set from System Properties. | ||
31 | ** Except for passing ##databaseCommands## where the format is ##-Dxwiki.test.ui.database.commands.<Command name>=<Command Value>## | ||
32 | * System properties have priority over Java annotation attributes | ||
33 | * The ##xwiki.test.ui.dependencies## configuration value can only be passed as a system property. | ||
34 | {{/info}} | ||
35 | |||
36 | (% style="overflow-x: auto;" %) | ||
37 | ((( | ||
38 | |=Option Name|=Default Value|=Valid Values|=Description | ||
39 | |##browser##|##firefox##|((( | ||
40 | * ##firefox## (##Browser.FIREFOX##) | ||
41 | * ##chrome## (##Browser.CHROME##) | ||
42 | )))|The browser used in the tests. Note that the version of the browser used is controlled by the version of Selenium that is defined in the ##pom.xml## of ##xwiki-platform-test-docker##. | ||
43 | |##database##|##hsqldb_embedded##|((( | ||
44 | * ##mysql## (##Database.MYSQL##) | ||
45 | * ##postgresql## (##Database.POSTGRESQL##) | ||
46 | * ##hsqldb_embedded## (##Database.HSQLDB_EMBEDDED##) | ||
47 | * ##oracle DB## (##Database.ORACLE##) | ||
48 | )))|The database used in the tests. Available tags are those listed on the Dockerhub page for the database image used. Note: for Oracle, we have a [[custom image>>https://hub.docker.com/r/xwiki/oracle-database/tags?page=1&ordering=last_updated]]. | ||
49 | |##servletEngine##|##jetty_standalone##|((( | ||
50 | * ##tomcat## (##ServletEngine.TOMCAT##) | ||
51 | * ##jetty## (##ServletEngine.JETTY##) | ||
52 | * ##jetty_standalone## (##ServletEngine.JETTY_STANDALONE##) | ||
53 | * ##external## (##ServletEngine.EXTERNAL##) | ||
54 | )))|The Servlet Engine used in the tests. Use ##external## to use your own started and alreayd provisioned XWiki instance. | ||
55 | |##verbose##|##false##|((( | ||
56 | * ##true## | ||
57 | * ##false## | ||
58 | )))|When active, displays more logs in the console (especially container startup logs) | ||
59 | |##debug##|##false##|((( | ||
60 | * ##true## | ||
61 | * ##false## | ||
62 | )))|When active, starts XWiki in debug mode when using the "Jetty Standalone" and Tomcat Servlet Engines (and starts it with suspend on, i.e. wait for the remote debugger to connect to it before progressing, to make it easy to debug XWiki's server side). You can then attach a remote debugger. Since Tomcat runs in a Docker container, you'll need to do a ##docker ps## to find the local mapped port corresponding to port 5005 and then use ##localhost## as the JVM host name. | ||
63 | |##saveDatabaseData##|##false##|((( | ||
64 | * ##true## | ||
65 | * ##false## | ||
66 | )))|By default, database data is not saved between test executions. {{warning}}Note that if you decide to save database data, they'll be saved under a ##docker## user by Docker and you need your local user to be able to remove them if you want to clean your Maven ##target## directory.{{/warning}} | ||
67 | |##office##|##false##|((( | ||
68 | * ##true## | ||
69 | * ##false## | ||
70 | )))|((( | ||
71 | When active it creates or gets a custom docker image based on the servlet engine image provided but with LibreOffice installed on it. The created image is named and tagged using the following scheme: ##xwiki-[servlet engine name]-office:[servlet engine tag]##. | ||
72 | |||
73 | The LibreOffice version used for the install is based on the Maven property ##libreoffice.version##. | ||
74 | ))) | ||
75 | |##offline##|##false##|((( | ||
76 | * ##true## | ||
77 | * ##false## | ||
78 | )))|When offline, the custom XWiki WAR generation and the XWiki provisioning are done solely from your local Maven repository. Otherwise, when artifacts are not present locally or newer SNAPSHOT versions are available, they'll be fetched from Maven remote repositories. Moreover no docker pull check is performed in offline mode. | ||
79 | |##databaseTag##|Default tag version defined by TestContainers (except for Oracle where it's ##xwiki/oracle-database##)|Any Docker tag available from Dockerhub for the container image|Version of the database to use. Isn't supported for HSQLDB Embedded since it doesn't run in a Docker container. See the [[latest supported version by XWiki>>Community.DatabaseSupportStrategy]]. | ||
80 | |##servletEngineTag##|Default tag version defined by TestContainers|Any Docker tag available from Dockerhub for the container image|Version of the Servlet Engine to use. Isn't supported for Jetty Standalone since it doesn't run in a Docker container. See the [[latest supported version by XWiki>>Community.ServletContainerSupportStrategy.WebHome]]. | ||
81 | |##jdbcDriverVersion##|Latest version validated by the XWiki dev team|Any version for the specified database that is available on Maven Central, e.g. for MySQL the groupId/artifactId is ##mysql##/##mysql-connector-java##.| | ||
82 | |##vnc##|##true##|((( | ||
83 | * ##true## | ||
84 | * ##false## | ||
85 | )))|When active a VNC container is started to record a video of the tests and more generally to allow connecting to the UI running the tests. Useful when debugging. | ||
86 | |##properties##|No default (empty)|Check the ##*.vm## files for the configuration files to see the list of possible values.|(% style="word-break:break-all" %)Velocity properties that are applied when generating XWiki's configuration files: ##xwiki.cfg##, ##xwiki.properties## and ##hibernate.cfg.xml##. Example: Tell XWiki that minification is off: ##-Dxwiki.test.ui.properties.xwikiPropertiesAdditionalProperties=debug.minify=false## or ##@UITest(properties = { "xwikiPropertiesAdditionalProperties=debug.minify=false" })##. Note that the ##xwikiCfgPlugins## property will be merged with the default values unless you start the new property value with the ##^## character. | ||
87 | |##extraJARs##|No default (empty)|Comma-separated list of ##<groupId>:<artifactId>## or ##<groupId>:<artifactId>:<version>##|(% style="word-break:break-all" %)There are cases where the module being tested cannot be fully deployed as an extension into a running XWiki and it needs to deploy one or several JARs into ##WEB-INF/lib## (as core extensions). One such example is when an extension includes a Hibernate HBM file, as right now only HBM files located in JARs in ##WEB-INF/lib## are supported. Note that when the version is not specified the current POM's version is used. | ||
88 | |##sshPorts##|No default (empty)|List of integers|The list of ports that should be SSH-forwarded when connecting from a Docker container to the host (i.e. when using the ##host.testcontainers.internal##. This is in addition to port ##8080## which is always added. For example if you need XWiki to send a mail to a SMTP server running on port 3025 on the host, you should add port 3025 to the list. | ||
89 | |##profiles##|No default (empty)|Comma-separated list of Maven profile ids|When specified the defined profiles are active when resolving dependencies from the current POM. | ||
90 | |##forbiddenServletEngines##|No default (empty)|List of ##ServletEngine##s enums|List of Servlet Engines for which the tests will be skipped (usually because they'd fail on them). | ||
91 | |##databaseProperties##|No default (empty)|See Docker documentation for the target image|List of database docker commands to use and that will override default commands (example of command ##character-set-server=utf8mb4## which will translate into ##~-~-character-set-server=utf8mb4## at runtime). | ||
92 | |##dependencies##|No default (empty)|Comma-separated list of Maven coordinates|List artifacts that will be provisioned in the running XWiki instance. When specified then current pom.xml dependencies are not used to find out what to provision. This can be used for example in cases when you don't want to use project dependencies in order to not draw older version of dependencies (such as when you need to run functional docker-based tests in a specific more recent vesion of XWiki than the one used to build the code). Accepted format is ##groupId:artifactId##, ##groupId:artifactId:version## and ##groupId:artifactId:type:version##. {{info}}Since 11.2RC1{{/info}}. | ||
93 | |##extensionOverrides##|No default (empty)|List Extension Manager overrides|See example below for the format. {{info}}Since 11.6RC1{{/info}}. | ||
94 | |##resolveExtraJARs##|##false##|((( | ||
95 | * ##true## | ||
96 | * ##false## | ||
97 | )))|Specifies if the extra JAR versions must be resolved (when their versions are not specified), by checking the transitive list of dependencies in the current POM. Note that there are 2 limitations: resolving takes time and SNAPSHOT versions will be resolved to the latest published SNAPSHOT. This is why it's off by default. When false the current POM version will be used for the missing exyta JAR versions. The main use case for using true is in contrib extensions where the current POM version usually don't match extra JARs versions and you may not want to hardcode the version and thus you can let the docker test framework find it for you. {{info}}Since 12.5RC1{{/info}}. | ||
98 | |||
99 | = Examples = | ||
100 | |||
101 | == Full Examples == | ||
102 | |||
103 | * {{scm path="xwiki-platform-core/xwiki-platform-menu/xwiki-platform-menu-test/xwiki-platform-menu-test-docker/src/test/it/org/xwiki/menu/test/ui/MenuIT.java"}}MenuIT{{/scm}} | ||
104 | * {{scm path="xwiki-platform-core/xwiki-platform-mail/xwiki-platform-mail-test/xwiki-platform-mail-test-docker/src/test/it/org/xwiki/mail/test/ui/MailIT.java"}}MailIT{{/scm}} | ||
105 | |||
106 | == Example 1: Basic test == | ||
107 | |||
108 | {{code language="java"}} | ||
109 | @UITest | ||
110 | public class SeleniumTest | ||
111 | { | ||
112 | @Test | ||
113 | public void test(XWikiWebDriver driver, TestUtils setup) | ||
114 | { | ||
115 | driver.get("http://xwiki.org"); | ||
116 | assertThat(driver.getTitle(), | ||
117 | containsString("XWiki - The Advanced Open Source Enterprise and Application Wiki")); | ||
118 | driver.findElement(By.linkText("XWiki's concept")).click(); | ||
119 | } | ||
120 | } | ||
121 | {{/code}} | ||
122 | |||
123 | == Example 2: Choosing Container + DB + Browser and versions == | ||
124 | |||
125 | {{code language="java"}} | ||
126 | @UITest(database = Database.MYSQL, databaseTag = "5", servletEngine = ServletEngine.TOMCAT, servletEngineTag = "8", | ||
127 | browser = Browser.CHROME) | ||
128 | public class MenuIT | ||
129 | ... | ||
130 | {{/code}} | ||
131 | |||
132 | {{code language="java"}} | ||
133 | @UITest(database = Database.POSTGRESQL, databaseTag = "9", servletEngine = ServletEngine.JETTY, | ||
134 | servletEngineTag = "9", browser = Browser.CHROME) | ||
135 | public class MenuIT | ||
136 | ... | ||
137 | {{/code}} | ||
138 | |||
139 | {{code language="java"}} | ||
140 | @UITest(database = Database.HSQLDB_EMBEDDED, servletEngine = ServletEngine.JETTY_STANDALONE, | ||
141 | browser = Browser.FIREFOX) | ||
142 | public class MenuIT | ||
143 | ... | ||
144 | {{/code}} | ||
145 | |||
146 | == Example 3: Test using Greenmail + Hibernate HBM == | ||
147 | |||
148 | {{code language="java"}} | ||
149 | @UITest(sshPorts = { | ||
150 | 3025 | ||
151 | }, | ||
152 | properties = { | ||
153 | "xwikiDbHbmCommonExtraMappings=mailsender.hbm.xml", | ||
154 | // Pages created in the tests need to have PR since we ask for PR to send mails so we need to exclude them from | ||
155 | // the PR checker. | ||
156 | "xwikiPropertiesAdditionalProperties=test.prchecker.excludePattern=.*:MailIT\\..*" | ||
157 | }, | ||
158 | extraJARs = { | ||
159 | "org.xwiki.platform:xwiki-platform-mail-send-storage" | ||
160 | } | ||
161 | ) | ||
162 | public class MailIT | ||
163 | ... | ||
164 | {{/code}} | ||
165 | |||
166 | == Example 4: Passing Test Configuration == | ||
167 | |||
168 | {{code language="java"}} | ||
169 | @Test | ||
170 | public void verifyMail(TestUtils setup, XWikiWebDriver webDriver, TestConfiguration testConfiguration) | ||
171 | throws Exception | ||
172 | { | ||
173 | .... | ||
174 | } | ||
175 | {{/code}} | ||
176 | |||
177 | == Example 5: Specifying explictly dependencies == | ||
178 | |||
179 | Example of deploying a ##1.10-SNAPSHOT## version of ##org.xwiki.contrib.latex:latex-export## (and its dependencies) into a ##11.2-SNAPSHOT## version of XWiki. In this example the POM of ##org.xwiki.contrib.latex:latex-export## depends on XWiki 10.2. Thus this example demonstrates running the tests in a more recent version of XWiki than the code was built on. | ||
180 | |||
181 | {{code language="xml"}} | ||
182 | ... | ||
183 | <parent> | ||
184 | <groupId>org.xwiki.platform</groupId> | ||
185 | <artifactId>xwiki-platform</artifactId> | ||
186 | <version>11.2-SNAPSHOT</version> | ||
187 | </parent> | ||
188 | <groupId>org.xwiki.contrib.latex</groupId> | ||
189 | <artifactId>latex-test</artifactId> | ||
190 | <version>1.10-SNAPSHOT</version> | ||
191 | ... | ||
192 | <plugin> | ||
193 | <groupId>org.apache.maven.plugins</groupId> | ||
194 | <artifactId>maven-failsafe-plugin</artifactId> | ||
195 | <configuration> | ||
196 | <includes> | ||
197 | <include>**/AllIT.java</include> | ||
198 | </includes> | ||
199 | <systemProperties> | ||
200 | <xwiki.test.ui.dependencies>org.xwiki.contrib.latex:latex-export</xwiki.test.ui.dependencies> | ||
201 | </systemProperties> | ||
202 | </configuration> | ||
203 | </plugin> | ||
204 | {{/code}} | ||
205 | |||
206 | == Example 6: Passing extension overrides == | ||
207 | |||
208 | The following tells the Extension Manager to use the existing core extension ##com.google.code.findbugs:annotations## whenever the extension ##com.google.code.findbugs:jsr305## is requested. | ||
209 | |||
210 | {{code language="java"}} | ||
211 | @UITest(extensionOverrides = { | ||
212 | @ExtensionOverride( | ||
213 | extensionId = "com.google.code.findbugs:jsr305", | ||
214 | overrides = { | ||
215 | "features=com.google.code.findbugs:annotations" | ||
216 | } | ||
217 | ) | ||
218 | }) | ||
219 | ... | ||
220 | {{/code}} | ||
221 | |||
222 | Note that in the XWikio build, these extension overrides are defined in the top level XWiki Platform POM and for this specific case it's defined as: | ||
223 | |||
224 | {{code language="xml"}} | ||
225 | ... | ||
226 | <extensionOverride> | ||
227 | <id>com.google.code.findbugs:jsr305</id> | ||
228 | <features>com.google.code.findbugs:annotations</features> | ||
229 | </extensionOverride> | ||
230 | ... | ||
231 | {{/code}} | ||
232 | |||
233 | Another example: | ||
234 | |||
235 | {{code language="java"}} | ||
236 | ... | ||
237 | extensionOverrides = { | ||
238 | @ExtensionOverride( | ||
239 | extensionId = "org.xwiki.platform:xwiki-platform-web", | ||
240 | overrides = { | ||
241 | // We set a default UI for the subwiki in the webapp, so that the Wiki Creation UI knows which extension | ||
242 | // to install on a subwiki by default (which is something we test) | ||
243 | // Otherwise the wiki creation form will display the flavor picker and the functional tests do not handle it. | ||
244 | "properties=xwiki.extension.distribution.wikiui=org.xwiki.platform:xwiki-platform-wiki-ui-wiki" | ||
245 | } | ||
246 | ) | ||
247 | } | ||
248 | ... | ||
249 | {{/code}} | ||
250 | |||
251 | = Best Practices = | ||
252 | |||
253 | == Scenarios == | ||
254 | |||
255 | Always use scenarios and use JUnit5's ##@Order(...)## annotation. For example: | ||
256 | |||
257 | {{code}} | ||
258 | @UITest | ||
259 | public class MenuIT | ||
260 | { | ||
261 | @Test | ||
262 | @Order(1) | ||
263 | public void verifyMenuInApplicationsIndex(TestUtils setup) | ||
264 | { | ||
265 | ... | ||
266 | } | ||
267 | |||
268 | @Test | ||
269 | @Order(2) | ||
270 | public void verifyMenuCreationInLeftPanelWithCurrentWikiVisibility(TestUtils setup) | ||
271 | { | ||
272 | ... | ||
273 | } | ||
274 | |||
275 | @Test | ||
276 | @Order(3) | ||
277 | public void verifyMenuIsAvailableInAdministration(TestUtils setup) throws Exception | ||
278 | { | ||
279 | ... | ||
280 | } | ||
281 | } | ||
282 | {{/code}} | ||
283 | |||
284 | == Suites == | ||
285 | |||
286 | Use JUnit5's ##@Nested## annotation. For example: | ||
287 | |||
288 | {{code language='java'}} | ||
289 | @UITest | ||
290 | public class AllIT | ||
291 | { | ||
292 | @Nested | ||
293 | @DisplayName("Office Import Tests") | ||
294 | class NestedOfficeImporterIT extends OfficeImporterIT | ||
295 | { | ||
296 | } | ||
297 | |||
298 | @Nested | ||
299 | @DisplayName("Office Export Tests") | ||
300 | class NestedOfficeExportIT extends OfficeExportIT | ||
301 | { | ||
302 | } | ||
303 | } | ||
304 | {{/code}} | ||
305 | |||
306 | {{version since='12.8RC1'}} | ||
307 | When using nested tests, only configure any ##@UITest## parameter in the nested tests themselves and not in the parent (e.g. at the ##AllIT## level the ##@UITest## annotation should be empty). This is because ##@UITest## annotation parameters are automatically merged. | ||
308 | {{/version}} | ||
309 | |||
310 | == Test Reference == | ||
311 | |||
312 | A lot of tests need to create a wiki page for testing. Example to do this: | ||
313 | |||
314 | {{code language="java"}} | ||
315 | @Test | ||
316 | public void showAndHideEditComments(TestUtils setup, TestReference reference) throws Exception | ||
317 | { | ||
318 | ViewPage vp = setup.gotoPage(reference); | ||
319 | ... | ||
320 | {{/code}} | ||
321 | |||
322 | More generic way (more verbose but can useful in some cases) using JUnit5's ##TestInfo## class: | ||
323 | |||
324 | {{code language="java"}} | ||
325 | public class MailIT | ||
326 | { | ||
327 | private String testClassName; | ||
328 | |||
329 | @BeforeEach | ||
330 | public void setUp(TestInfo info) | ||
331 | { | ||
332 | this.testClassName = info.getTestClass().get().getSimpleName(); | ||
333 | } | ||
334 | ... | ||
335 | } | ||
336 | {{/code}} | ||
337 | |||
338 | == Docker out of Docker == | ||
339 | |||
340 | The [[XWiki's CI>>https://ci.xwiki.org]] is using a [[Jenkins Agent Docker image>>https://github.com/xwiki/xwiki-jenkins-slave]] to execute the job builds. Thus, the Docker-based functional tests spawn Docker containers (using TestContainers) that execute inside Docker. This is the pattern called DOOD (Docker out of Docker). See [[TC's best practice>>https://www.testcontainers.org/supported_docker_environment/continuous_integration/dind_patterns/]] about this. | ||
341 | |||
342 | There are some consequences when writing tests for XWiki: | ||
343 | |||
344 | * Never use volume mapping or it won't execute fine. This is because that would require the Jenkins Agent Docker container to have that volume mapping defined too and the mapping would need to use the exact host path and container path. So, in short, it won't work. Instead copy files that you need to have in the container. Adn copy file out if need be. Here's an example from the LaTeX extension on how to achieve this:((( | ||
345 | {{code language="java"}} | ||
346 | try (GenericContainer container = new GenericContainer("blang/latex:ubuntu")) { | ||
347 | // Note: we copy files instead of mounting volumes so that this can work when using DOOD | ||
348 | // (Docker out of Docker). | ||
349 | MountableFile mountableDirectory = MountableFile.forHostPath("target/latex"); | ||
350 | container.withCopyFileToContainer(mountableDirectory, "/data"); | ||
351 | |||
352 | container.withCommand("/bin/sh", "-c", "sleep infinity"); | ||
353 | container.withLogConsumer(new Slf4jLogConsumer(LOGGER)); | ||
354 | container.start(); | ||
355 | Container.ExecResult result = container.execInContainer("pdflatex", "-shell-escape", "index.tex"); | ||
356 | String stdout = result.getStdout(); | ||
357 | LOGGER.info(stdout); | ||
358 | assertTrue(stdout.contains("Output written on index.pdf")); | ||
359 | |||
360 | container.copyFileFromContainer("/data/index.pdf", "target/latex/index.pdf"); | ||
361 | } | ||
362 | {{/code}} | ||
363 | ))) | ||
364 | |||
365 | == Stdout/stderr validation errors == | ||
366 | |||
367 | We have a [[check>>Community.Building.WebHome||anchor="HAutomaticChecks"]] that verifies if functional tests output some invalid content to stdout/stderr. If your test contains such errors it'll fail and then you have 3 options: | ||
368 | |||
369 | * It's a normal and expected output from the test (for ex the test verifies an error condition and it's expected it will raise a stack trace in the console). In this case add an expectation in the test. For example:((( | ||
370 | {{code language="java"}} | ||
371 | public void wrongTemplate(LogCaptureConfiguration logCaptureConfiguration) | ||
372 | { | ||
373 | ... | ||
374 | logCaptureConfiguration.registerExpected( "Possible break-in attempt!", | ||
375 | "Error getting resource [null]"); | ||
376 | } | ||
377 | {{/code}} | ||
378 | ))) | ||
379 | * It's a non-expected error. You then have 2 sub-choices: | ||
380 | ** Fix the problem (the best and recommended solution!) | ||
381 | ** Increase the technical debt by adding an exclude. For example:((( | ||
382 | {{code language="java"}} | ||
383 | logCaptureConfiguration.registerExcludes( | ||
384 | "java.lang.IllegalStateException: Response is committed"); | ||
385 | {{/code}} | ||
386 | ))) | ||
387 | |||
388 | = Test Resources = | ||
389 | |||
390 | You might need test resources to be used in interaction with the wiki (e.g. to upload an attachment). Any test resources placed in ##src/test/resources## is made automatically available to the browser container by mounting the dedicated volume mapped to ##target/test-classes##. Then, in your test, you can use the dedicated method to get access to the files located in ##target/test-classes##. | ||
391 | |||
392 | For example: | ||
393 | |||
394 | {{code language="java"}} | ||
395 | String resourceDirPath = testConfiguration.getBrowser().getTestResourcesPath(); | ||
396 | File myResourceFile = new File(resourceDirPath, filename); | ||
397 | {{/code}} | ||
398 | |||
399 | = Code Resources = | ||
400 | |||
401 | You might need custom test code deployed to the XWiki webapp for the test (to override some components for example). Any classes that land into ##target/classes## will be copied to the webapp's ##WEB-INF/classes## directory. Thus you can, for example, put sources in ##src/main/*## to see it deployed in the webapp. | ||
402 | |||
403 | = Test Logging = | ||
404 | |||
405 | You might want to activate some debug logs specific to the test. This can be achieved by providing a ##logback-override.xml## file in your ##src/test/resources## directory of your Maven project. | ||
406 | |||
407 | = Test Coverage = | ||
408 | |||
409 | Activate support for Clover by setting the ##xwiki.test.ui.profiles## property in the ##clover## profile in the ##pom.xml## file for the test. | ||
410 | |||
411 | Example: | ||
412 | |||
413 | {{code language="xml"}} | ||
414 | <profiles> | ||
415 | <profile> | ||
416 | <id>clover</id> | ||
417 | <!-- Add the Clover JAR to the WAR so that it's available at runtime when XWiki executes. | ||
418 | It's needed because instrumented jars in the WAR will call Clover APIs at runtime when they execute. --> | ||
419 | <dependencies> | ||
420 | <dependency> | ||
421 | <groupId>org.openclover</groupId> | ||
422 | <artifactId>clover</artifactId> | ||
423 | </dependency> | ||
424 | </dependencies> | ||
425 | <build> | ||
426 | <plugins> | ||
427 | <plugin> | ||
428 | <groupId>org.apache.maven.plugins</groupId> | ||
429 | <artifactId>maven-failsafe-plugin</artifactId> | ||
430 | <configuration> | ||
431 | <systemProperties combine.children="append"> | ||
432 | <!-- Tell the Docker-based test to activate the Clover profile so that the Clover JAR is added to | ||
433 | WEB-INF/lib --> | ||
434 | <property> | ||
435 | <name>xwiki.test.ui.profiles</name> | ||
436 | <value>clover</value> | ||
437 | </property> | ||
438 | </systemProperties> | ||
439 | </configuration> | ||
440 | </plugin> | ||
441 | ... | ||
442 | {{/code}} | ||
443 | |||
444 | = Existing XWiki instance = | ||
445 | |||
446 | If you want to avoid the Docker-based tests to start and stop XWiki every time you re-run the tests (for example when you're developping/debugging something), you can tell the Docker Test framework to not provision an XWiki instance and to use your XWiki instance instead. | ||
447 | |||
448 | This is achieved by specifying {{code}}@UITest(servletEngine = ServletEngine.EXTERNAL){{/code}} or passing the {{code}}xwiki.test.ui.servletEngine=external{{/code}} system property. | ||
449 | |||
450 | A common use case is to debug a failing test. Here's what you can do: | ||
451 | |||
452 | {{code language="none"}} | ||
453 | ## Run the test once to see if it fails and to generate the XWiki instance. This may take some time. | ||
454 | > mvn clean install -Dit.test=NavigationPanelIT | ||
455 | ## Open a new console, look for the generated XWiki instance inside the target folder and start it. | ||
456 | ../target/../jetty > start_xwiki.sh | ||
457 | ## Modify the test as needed then go back to the initial console and compile the tests. | ||
458 | ## Note that you may also need to build the page objects module if you change those too. | ||
459 | > mvn compiler:testCompile | ||
460 | ## Then run the test again to see the changes. This should be way faster than the initial test run. | ||
461 | > mvn failsafe:integration-test -Dxwiki.test.ui.servletEngine=external -Dit.test=NavigationPanelIT | ||
462 | {{/code}} | ||
463 | |||
464 | = Run a single test = | ||
465 | |||
466 | For example to run the ##NestedResetPasswordIT## tests from an ##AllITs## test class defined as: | ||
467 | |||
468 | {{code language='java'}} | ||
469 | @UITest | ||
470 | public class AllITs | ||
471 | { | ||
472 | @Nested | ||
473 | @DisplayName("Overall Administration UI") | ||
474 | class NestedAdministrationIT extends AdministrationIT | ||
475 | { | ||
476 | } | ||
477 | |||
478 | @Nested | ||
479 | @DisplayName("Reset Password") | ||
480 | class NestedResetPasswordIT extends ResetPasswordIT | ||
481 | { | ||
482 | } | ||
483 | ... | ||
484 | {{/code}} | ||
485 | |||
486 | You'd use: | ||
487 | |||
488 | {{code language='none'}} | ||
489 | mvn -Dit.test=*NestedResetPasswordIT* clean verify | ||
490 | {{/code}} | ||
491 | |||
492 | See also the [[Existing XWiki instance section>>||anchor='HExistingXWikiinstance']]. | ||
493 | |||
494 | = Architecture = | ||
495 | |||
496 | {{image reference="xwiki-testcontainers.png"/}} | ||
497 | //See [[Vincent's blog>>https://massol.myxwiki.org/xwiki/bin/view/Blog/EnvironmentTestingExperimentations#HExperimentation5:inJavaTestsusingTestContainers]] for more details// | ||
498 | ))) | ||
499 | |||
500 | = CI Jobs = | ||
501 | |||
502 | There are 3 CI jobs executing Docker-based functional tests: | ||
503 | |||
504 | * [[Job 1>>https://ci.xwiki.org/view/Recommended%20Builds/job/xwiki-platform_docker/]]: run all tests on latest versions of supported configs. | ||
505 | * [[Job 2>>https://ci.xwiki.org/view/Recommended%20Builds/job/xwiki-platform_docker_all/]]: run smoke tests (ie a few tests only) on all supported configs. | ||
506 | * [[Job 3>>https://ci.xwiki.org/view/Recommended%20Builds/job/xwiki-platform_docker_unsupported/]]: run smoke tests on unsupported configs (MySQL 8.x, Tomcat 9.x, etc) that we want to support in the future. | ||
507 | |||
508 | Configuration definitions and job sources are all {{scm project="xwiki-jenkins-pipeline" path="vars/dockerConfigurations.groovy"}}here{{/scm}}. |