Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport release/1.9.x] Fix race condition in tests using the same temporary data directories #586

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,84 @@
package org.geoserver.cloud.gwc.app;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.http.MediaType.APPLICATION_XML;

import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@ActiveProfiles("test")
class GeoWebCacheApplicationTest {

@Autowired
private TestRestTemplate restTemplate;

static @TempDir Path datadir;

@DynamicPropertySource
static void setUpDataDir(DynamicPropertyRegistry registry) throws IOException {
var gwcdir = datadir.resolve("gwc");
if (!Files.exists(gwcdir)) {
Files.createDirectory(gwcdir);
}
registry.add("geoserver.backend.data-directory.location", datadir::toAbsolutePath);
registry.add("gwc.cache-directory", gwcdir::toAbsolutePath);
}

@BeforeEach
void before() {
restTemplate = restTemplate.withBasicAuth("admin", "geoserver");
String rootUri = restTemplate.getRootUri();
assertThat(rootUri).isNotEmpty();
}

/**
* REVISIT: for some reason, running the REST API tests right after starting off an empty data directory produce a 403 forbidden
* response. We're hence forcing the order of the tests and the reload of the context for the time being
*/
@Test
@Order(1)
@DirtiesContext
void smokeTest() {
assertTrue(true);
}

@Test
@Order(2)
@DirtiesContext
void testRESTDefaultContentType() {
ResponseEntity<String> response = testGetRequestContentType("/gwc/rest/layers", APPLICATION_JSON);
JsonElement parsed = JsonParser.parseString(response.getBody());
assertThat(parsed.isJsonArray()).isTrue();
}

@Test
@Order(3)
@DirtiesContext
void testRESTPathExtensionContentNegotiation() {
ResponseEntity<String> response = testGetRequestContentType("/gwc/rest/layers.json", APPLICATION_JSON);
JsonElement parsed = JsonParser.parseString(response.getBody());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ geoserver:
backend:
data-directory:
enabled: true
location: ${data_directory:${java.io.tmpdir}/geoserver_cloud_data_directory}
location: # to be set by test classes

logging:
level:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,82 @@
package org.geoserver.cloud.restconfig;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.http.MediaType.APPLICATION_XML;
import static org.springframework.http.MediaType.TEXT_HTML;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.geoserver.catalog.SLDHandler;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@ActiveProfiles("test")
class RestConfigApplicationTest {

@Autowired
private TestRestTemplate restTemplate;

static @TempDir Path datadir;

@DynamicPropertySource
static void setUpDataDir(DynamicPropertyRegistry registry) throws IOException {
var gwcdir = datadir.resolve("gwc");
if (!Files.exists(gwcdir)) {
Files.createDirectory(gwcdir);
}
registry.add("geoserver.backend.data-directory.location", datadir::toAbsolutePath);
registry.add("gwc.cache-directory", gwcdir::toAbsolutePath);
}

@BeforeEach
void before() {
void before() throws Exception {
restTemplate = restTemplate.withBasicAuth("admin", "geoserver");
}

/**
* REVISIT: for some reason, running the REST API tests right after starting off
* an empty data directory produce a 403 forbidden response. We're hence forcing
* the order of the tests and the reload of the context for the time being
*/
@Test
void testDefaultContentType() {
@Order(1)
@DirtiesContext
void smokeTest() {
assertTrue(true);
}

@Test
@Order(2)
@DirtiesContext
void testDefaultContentType() {
testPathExtensionContentType("/rest/workspaces", APPLICATION_JSON);
testPathExtensionContentType("/rest/layers", APPLICATION_JSON);
}

@Test
@Order(3)
@DirtiesContext
void testPathExtensionContentNegotiation() {

testPathExtensionContentType("/rest/styles/line.json", APPLICATION_JSON);
testPathExtensionContentType("/rest/styles/line.xml", APPLICATION_XML);
testPathExtensionContentType("/rest/styles/line.html", TEXT_HTML);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ geoserver:
backend:
data-directory:
enabled: true
location: ${data_directory:${java.io.tmpdir}/geoserver_cloud_data_directory}
location: # to be set by the test class

logging:
level:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,30 @@

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

import java.io.IOException;
import java.nio.file.Path;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;

@SpringBootTest
@ActiveProfiles("test")
class WcsApplicationTest {
protected @Autowired ConfigurableApplicationContext context;

static @TempDir Path datadir;

@DynamicPropertySource
static void setUpDataDir(DynamicPropertyRegistry registry) throws IOException {
registry.add("geoserver.backend.data-directory.location", datadir::toAbsolutePath);
}

@Test
void testWcsCoreBeans() {
expectBean("legacyWcsLoader", org.geoserver.wcs.WCSLoader.class);
Expand Down
13 changes: 2 additions & 11 deletions src/apps/geoserver/wcs/src/test/resources/bootstrap-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,15 @@ spring:
cloud.config.enabled: false
cloud.config.discovery.enabled: false
cloud.discovery.enabled: false
cloud.bus.enabled: false
eureka.client.enabled: false

geoserver:
acl.enabled: false
backend:
data-directory:
enabled: true
location: ${data_directory:${java.io.tmpdir}/geoserver_cloud_data_directory}
jdbcconfig:
enabled: false
web.enabled: false
initdb: true
cache-directory: ${java.io.tmpdir}/geoserver-jdbcconfig-cache
datasource:
driverClassname: org.h2.Driver
url: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
username: sa
password:
location: # to be set by the test class

logging:
level:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

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

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
Expand All @@ -16,6 +19,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
Expand All @@ -24,6 +28,8 @@
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;

@SpringBootTest(
properties = {
Expand All @@ -41,6 +47,16 @@ class AclIntegrationTest {
private @Autowired GeoServerApplication app;
private WicketTester tester;

static @TempDir Path datadir;

@DynamicPropertySource
static void setUpDataDir(DynamicPropertyRegistry registry) throws IOException {
var gwcdir = datadir.resolve("gwc");
Files.createDirectory(gwcdir);
registry.add("geoserver.backend.data-directory.location", datadir::toAbsolutePath);
registry.add("gwc.cache-directory", gwcdir::toAbsolutePath);
}

static @BeforeAll void beforeAll() {
System.setProperty("wicket.configuration", "deployment");
// Disable CSRF protection for tests, since the test framework doesn't set the Referer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
Expand All @@ -27,6 +30,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
Expand All @@ -35,6 +39,8 @@
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;

@SpringBootTest(
properties = {
Expand All @@ -49,6 +55,17 @@ class WebUIApplicationTest {
private @Autowired GeoServerApplication app;
private WicketTester tester;

static @TempDir Path tmpdir;
static Path datadir;

@DynamicPropertySource
static void setUpDataDir(DynamicPropertyRegistry registry) throws IOException {
datadir = Files.createDirectory(tmpdir.resolve("datadir"));
var gwcdir = Files.createDirectory(datadir.resolve("gwc"));
registry.add("geoserver.backend.data-directory.location", datadir::toAbsolutePath);
registry.add("gwc.cache-directory", gwcdir::toAbsolutePath);
}

static @BeforeAll void beforeAll() {
System.setProperty("wicket.configuration", "deployment");
// Disable CSRF protection for tests, since the test framework doesn't set the Referer
Expand Down
16 changes: 2 additions & 14 deletions src/apps/geoserver/webui/src/test/resources/bootstrap-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,16 @@ spring:
cloud.config.enabled: false
cloud.config.discovery.enabled: false
cloud.discovery.enabled: false
cloud.bus.enabled: false
eureka.client.enabled: false

gwc:
cache-directory: ${java.io.tmpdir}/gs-webui-tmp

geoserver:
acl.enabled: false
security.enabled: true
backend:
data-directory:
enabled: true
location: ${data_directory:${java.io.tmpdir}/geoserver_cloud_data_directory}
jdbcconfig:
enabled: false
web.enabled: false
initdb: true
cache-directory: ${java.io.tmpdir}/geoserver-jdbcconfig-cache
datasource:
driverClassname: org.h2.Driver
url: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
username: sa
password:
location: # to be set by the test classes

logging:
level:
Expand Down
22 changes: 0 additions & 22 deletions src/apps/geoserver/wfs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,4 @@
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<skip>false</skip>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
<trimStackTrace>false</trimStackTrace>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Loading
Loading