Skip to content

Commit b570750

Browse files
committed
Fix race condition in tests using the same temporary data directories
Replace hardcoded temporary data directories in test application.yml/bootstrap.yml files by dynamic property sources to avoid unwanted dependencies from tests on the temporary directory existing, or parallel tests stepping on each other.
1 parent 67d020d commit b570750

File tree

27 files changed

+220
-245
lines changed

27 files changed

+220
-245
lines changed

src/apps/geoserver/gwc/src/test/java/org/geoserver/cloud/gwc/app/GeoWebCacheApplicationTest.java

+39
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,84 @@
55
package org.geoserver.cloud.gwc.app;
66

77
import static org.assertj.core.api.Assertions.assertThat;
8+
import static org.junit.Assert.assertTrue;
89
import static org.springframework.http.MediaType.APPLICATION_JSON;
910
import static org.springframework.http.MediaType.APPLICATION_XML;
1011

1112
import com.google.gson.JsonElement;
1213
import com.google.gson.JsonParser;
14+
import java.io.IOException;
1315
import java.net.URI;
16+
import java.nio.file.Files;
17+
import java.nio.file.Path;
1418
import org.junit.jupiter.api.BeforeEach;
19+
import org.junit.jupiter.api.MethodOrderer;
20+
import org.junit.jupiter.api.Order;
1521
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.TestMethodOrder;
23+
import org.junit.jupiter.api.io.TempDir;
1624
import org.springframework.beans.factory.annotation.Autowired;
1725
import org.springframework.boot.test.context.SpringBootTest;
1826
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
1927
import org.springframework.boot.test.web.client.TestRestTemplate;
2028
import org.springframework.http.HttpStatus;
2129
import org.springframework.http.MediaType;
2230
import org.springframework.http.ResponseEntity;
31+
import org.springframework.test.annotation.DirtiesContext;
2332
import org.springframework.test.context.ActiveProfiles;
33+
import org.springframework.test.context.DynamicPropertyRegistry;
34+
import org.springframework.test.context.DynamicPropertySource;
2435

2536
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
37+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
2638
@ActiveProfiles("test")
2739
class GeoWebCacheApplicationTest {
2840

2941
@Autowired
3042
private TestRestTemplate restTemplate;
3143

44+
static @TempDir Path datadir;
45+
46+
@DynamicPropertySource
47+
static void setUpDataDir(DynamicPropertyRegistry registry) throws IOException {
48+
var gwcdir = datadir.resolve("gwc");
49+
if (!Files.exists(gwcdir)) {
50+
Files.createDirectory(gwcdir);
51+
}
52+
registry.add("geoserver.backend.data-directory.location", datadir::toAbsolutePath);
53+
registry.add("gwc.cache-directory", gwcdir::toAbsolutePath);
54+
}
55+
3256
@BeforeEach
3357
void before() {
3458
restTemplate = restTemplate.withBasicAuth("admin", "geoserver");
3559
String rootUri = restTemplate.getRootUri();
3660
assertThat(rootUri).isNotEmpty();
3761
}
3862

63+
/**
64+
* REVISIT: for some reason, running the REST API tests right after starting off an empty data directory produce a 403 forbidden
65+
* response. We're hence forcing the order of the tests and the reload of the context for the time being
66+
*/
67+
@Test
68+
@Order(1)
69+
@DirtiesContext
70+
void smokeTest() {
71+
assertTrue(true);
72+
}
73+
3974
@Test
75+
@Order(2)
76+
@DirtiesContext
4077
void testRESTDefaultContentType() {
4178
ResponseEntity<String> response = testGetRequestContentType("/gwc/rest/layers", APPLICATION_JSON);
4279
JsonElement parsed = JsonParser.parseString(response.getBody());
4380
assertThat(parsed.isJsonArray()).isTrue();
4481
}
4582

4683
@Test
84+
@Order(3)
85+
@DirtiesContext
4786
void testRESTPathExtensionContentNegotiation() {
4887
ResponseEntity<String> response = testGetRequestContentType("/gwc/rest/layers.json", APPLICATION_JSON);
4988
JsonElement parsed = JsonParser.parseString(response.getBody());

src/apps/geoserver/gwc/src/test/resources/bootstrap-test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ geoserver:
1010
backend:
1111
data-directory:
1212
enabled: true
13-
location: ${data_directory:${java.io.tmpdir}/geoserver_cloud_data_directory}
13+
location: # to be set by test classes
1414

1515
logging:
1616
level:

src/apps/geoserver/restconfig/src/test/java/org/geoserver/cloud/restconfig/RestConfigApplicationTest.java

+41-3
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,82 @@
55
package org.geoserver.cloud.restconfig;
66

77
import static org.assertj.core.api.Assertions.assertThat;
8+
import static org.junit.Assert.assertTrue;
89
import static org.springframework.http.MediaType.APPLICATION_JSON;
910
import static org.springframework.http.MediaType.APPLICATION_XML;
1011
import static org.springframework.http.MediaType.TEXT_HTML;
1112

13+
import java.io.IOException;
14+
import java.nio.file.Files;
15+
import java.nio.file.Path;
1216
import org.geoserver.catalog.SLDHandler;
1317
import org.junit.jupiter.api.BeforeEach;
18+
import org.junit.jupiter.api.MethodOrderer;
19+
import org.junit.jupiter.api.Order;
1420
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.TestMethodOrder;
22+
import org.junit.jupiter.api.io.TempDir;
1523
import org.springframework.beans.factory.annotation.Autowired;
1624
import org.springframework.boot.test.context.SpringBootTest;
1725
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
1826
import org.springframework.boot.test.web.client.TestRestTemplate;
1927
import org.springframework.http.HttpStatus;
2028
import org.springframework.http.MediaType;
2129
import org.springframework.http.ResponseEntity;
30+
import org.springframework.test.annotation.DirtiesContext;
2231
import org.springframework.test.context.ActiveProfiles;
32+
import org.springframework.test.context.DynamicPropertyRegistry;
33+
import org.springframework.test.context.DynamicPropertySource;
2334

2435
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
36+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
2537
@ActiveProfiles("test")
2638
class RestConfigApplicationTest {
2739

2840
@Autowired
2941
private TestRestTemplate restTemplate;
3042

43+
static @TempDir Path datadir;
44+
45+
@DynamicPropertySource
46+
static void setUpDataDir(DynamicPropertyRegistry registry) throws IOException {
47+
var gwcdir = datadir.resolve("gwc");
48+
if (!Files.exists(gwcdir)) {
49+
Files.createDirectory(gwcdir);
50+
}
51+
registry.add("geoserver.backend.data-directory.location", datadir::toAbsolutePath);
52+
registry.add("gwc.cache-directory", gwcdir::toAbsolutePath);
53+
}
54+
3155
@BeforeEach
32-
void before() {
56+
void before() throws Exception {
3357
restTemplate = restTemplate.withBasicAuth("admin", "geoserver");
3458
}
3559

60+
/**
61+
* REVISIT: for some reason, running the REST API tests right after starting off
62+
* an empty data directory produce a 403 forbidden response. We're hence forcing
63+
* the order of the tests and the reload of the context for the time being
64+
*/
3665
@Test
37-
void testDefaultContentType() {
66+
@Order(1)
67+
@DirtiesContext
68+
void smokeTest() {
69+
assertTrue(true);
70+
}
3871

72+
@Test
73+
@Order(2)
74+
@DirtiesContext
75+
void testDefaultContentType() {
3976
testPathExtensionContentType("/rest/workspaces", APPLICATION_JSON);
4077
testPathExtensionContentType("/rest/layers", APPLICATION_JSON);
4178
}
4279

4380
@Test
81+
@Order(3)
82+
@DirtiesContext
4483
void testPathExtensionContentNegotiation() {
45-
4684
testPathExtensionContentType("/rest/styles/line.json", APPLICATION_JSON);
4785
testPathExtensionContentType("/rest/styles/line.xml", APPLICATION_XML);
4886
testPathExtensionContentType("/rest/styles/line.html", TEXT_HTML);

src/apps/geoserver/restconfig/src/test/resources/bootstrap-test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ geoserver:
1414
backend:
1515
data-directory:
1616
enabled: true
17-
location: ${data_directory:${java.io.tmpdir}/geoserver_cloud_data_directory}
17+
location: # to be set by the test class
1818

1919
logging:
2020
level:

src/apps/geoserver/wcs/src/test/java/org/geoserver/cloud/wcs/WcsApplicationTest.java

+12
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,30 @@
66

77
import static org.assertj.core.api.Assertions.assertThat;
88

9+
import java.io.IOException;
10+
import java.nio.file.Path;
911
import java.util.stream.Stream;
1012
import org.junit.jupiter.api.Test;
13+
import org.junit.jupiter.api.io.TempDir;
1114
import org.springframework.beans.factory.annotation.Autowired;
1215
import org.springframework.boot.test.context.SpringBootTest;
1316
import org.springframework.context.ConfigurableApplicationContext;
1417
import org.springframework.test.context.ActiveProfiles;
18+
import org.springframework.test.context.DynamicPropertyRegistry;
19+
import org.springframework.test.context.DynamicPropertySource;
1520

1621
@SpringBootTest
1722
@ActiveProfiles("test")
1823
class WcsApplicationTest {
1924
protected @Autowired ConfigurableApplicationContext context;
2025

26+
static @TempDir Path datadir;
27+
28+
@DynamicPropertySource
29+
static void setUpDataDir(DynamicPropertyRegistry registry) throws IOException {
30+
registry.add("geoserver.backend.data-directory.location", datadir::toAbsolutePath);
31+
}
32+
2133
@Test
2234
void testWcsCoreBeans() {
2335
expectBean("legacyWcsLoader", org.geoserver.wcs.WCSLoader.class);

src/apps/geoserver/wcs/src/test/resources/bootstrap-test.yml

+2-11
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,15 @@ spring:
66
cloud.config.enabled: false
77
cloud.config.discovery.enabled: false
88
cloud.discovery.enabled: false
9+
cloud.bus.enabled: false
910
eureka.client.enabled: false
1011

1112
geoserver:
1213
acl.enabled: false
1314
backend:
1415
data-directory:
1516
enabled: true
16-
location: ${data_directory:${java.io.tmpdir}/geoserver_cloud_data_directory}
17-
jdbcconfig:
18-
enabled: false
19-
web.enabled: false
20-
initdb: true
21-
cache-directory: ${java.io.tmpdir}/geoserver-jdbcconfig-cache
22-
datasource:
23-
driverClassname: org.h2.Driver
24-
url: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
25-
username: sa
26-
password:
17+
location: # to be set by the test class
2718

2819
logging:
2920
level:

src/apps/geoserver/webui/src/test/java/org/geoserver/cloud/web/app/AclIntegrationTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
import static org.junit.jupiter.api.Assertions.assertNotNull;
88

9+
import java.io.IOException;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
912
import java.util.ArrayList;
1013
import java.util.List;
1114
import java.util.Locale;
@@ -16,6 +19,7 @@
1619
import org.junit.jupiter.api.BeforeAll;
1720
import org.junit.jupiter.api.BeforeEach;
1821
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.io.TempDir;
1923
import org.springframework.beans.factory.annotation.Autowired;
2024
import org.springframework.boot.test.context.SpringBootTest;
2125
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@@ -24,6 +28,8 @@
2428
import org.springframework.security.core.context.SecurityContextHolder;
2529
import org.springframework.security.core.context.SecurityContextImpl;
2630
import org.springframework.test.context.ActiveProfiles;
31+
import org.springframework.test.context.DynamicPropertyRegistry;
32+
import org.springframework.test.context.DynamicPropertySource;
2733

2834
@SpringBootTest(
2935
properties = {
@@ -41,6 +47,16 @@ class AclIntegrationTest {
4147
private @Autowired GeoServerApplication app;
4248
private WicketTester tester;
4349

50+
static @TempDir Path datadir;
51+
52+
@DynamicPropertySource
53+
static void setUpDataDir(DynamicPropertyRegistry registry) throws IOException {
54+
var gwcdir = datadir.resolve("gwc");
55+
Files.createDirectory(gwcdir);
56+
registry.add("geoserver.backend.data-directory.location", datadir::toAbsolutePath);
57+
registry.add("gwc.cache-directory", gwcdir::toAbsolutePath);
58+
}
59+
4460
static @BeforeAll void beforeAll() {
4561
System.setProperty("wicket.configuration", "deployment");
4662
// Disable CSRF protection for tests, since the test framework doesn't set the Referer

src/apps/geoserver/webui/src/test/java/org/geoserver/cloud/web/app/WebUIApplicationTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import static org.junit.jupiter.api.Assertions.assertEquals;
99
import static org.junit.jupiter.api.Assertions.assertNotNull;
1010

11+
import java.io.IOException;
12+
import java.nio.file.Files;
13+
import java.nio.file.Path;
1114
import java.util.ArrayList;
1215
import java.util.List;
1316
import java.util.Locale;
@@ -27,6 +30,7 @@
2730
import org.junit.jupiter.api.BeforeAll;
2831
import org.junit.jupiter.api.BeforeEach;
2932
import org.junit.jupiter.api.Test;
33+
import org.junit.jupiter.api.io.TempDir;
3034
import org.springframework.beans.factory.annotation.Autowired;
3135
import org.springframework.boot.test.context.SpringBootTest;
3236
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@@ -35,6 +39,8 @@
3539
import org.springframework.security.core.context.SecurityContextHolder;
3640
import org.springframework.security.core.context.SecurityContextImpl;
3741
import org.springframework.test.context.ActiveProfiles;
42+
import org.springframework.test.context.DynamicPropertyRegistry;
43+
import org.springframework.test.context.DynamicPropertySource;
3844

3945
@SpringBootTest(
4046
properties = {
@@ -49,6 +55,17 @@ class WebUIApplicationTest {
4955
private @Autowired GeoServerApplication app;
5056
private WicketTester tester;
5157

58+
static @TempDir Path tmpdir;
59+
static Path datadir;
60+
61+
@DynamicPropertySource
62+
static void setUpDataDir(DynamicPropertyRegistry registry) throws IOException {
63+
datadir = Files.createDirectory(tmpdir.resolve("datadir"));
64+
var gwcdir = Files.createDirectory(datadir.resolve("gwc"));
65+
registry.add("geoserver.backend.data-directory.location", datadir::toAbsolutePath);
66+
registry.add("gwc.cache-directory", gwcdir::toAbsolutePath);
67+
}
68+
5269
static @BeforeAll void beforeAll() {
5370
System.setProperty("wicket.configuration", "deployment");
5471
// Disable CSRF protection for tests, since the test framework doesn't set the Referer

src/apps/geoserver/webui/src/test/resources/bootstrap-test.yml

+1-14
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,13 @@ spring:
88
cloud.discovery.enabled: false
99
eureka.client.enabled: false
1010

11-
gwc:
12-
cache-directory: ${java.io.tmpdir}/gs-webui-tmp
13-
1411
geoserver:
1512
acl.enabled: false
1613
security.enabled: true
1714
backend:
1815
data-directory:
1916
enabled: true
20-
location: ${data_directory:${java.io.tmpdir}/geoserver_cloud_data_directory}
21-
jdbcconfig:
22-
enabled: false
23-
web.enabled: false
24-
initdb: true
25-
cache-directory: ${java.io.tmpdir}/geoserver-jdbcconfig-cache
26-
datasource:
27-
driverClassname: org.h2.Driver
28-
url: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
29-
username: sa
30-
password:
17+
location: # to be set by the test classes
3118

3219
logging:
3320
level:

src/apps/geoserver/wfs/pom.xml

-22
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,4 @@
4545
<scope>test</scope>
4646
</dependency>
4747
</dependencies>
48-
<build>
49-
<plugins>
50-
<plugin>
51-
<groupId>org.apache.maven.plugins</groupId>
52-
<artifactId>maven-failsafe-plugin</artifactId>
53-
<executions>
54-
<execution>
55-
<goals>
56-
<goal>integration-test</goal>
57-
<goal>verify</goal>
58-
</goals>
59-
<configuration>
60-
<skip>false</skip>
61-
<forkCount>1</forkCount>
62-
<reuseForks>false</reuseForks>
63-
<trimStackTrace>false</trimStackTrace>
64-
</configuration>
65-
</execution>
66-
</executions>
67-
</plugin>
68-
</plugins>
69-
</build>
7048
</project>

0 commit comments

Comments
 (0)