-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support mapping volumes in Dev Service container based databases
This is a very useful feature that allows mapping volumes in the Dev Service containers. For example: to keep the Postgres data folder in the localsystem and hence have the data persistently. Fix #30595 Co-authored-by: Yoann Rodière <[email protected]>
- Loading branch information
Showing
12 changed files
with
184 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
extensions/devservices/common/src/main/java/io/quarkus/devservices/common/Volumes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.quarkus.devservices.common; | ||
|
||
import java.net.URL; | ||
import java.util.Map; | ||
|
||
import org.testcontainers.containers.BindMode; | ||
import org.testcontainers.containers.GenericContainer; | ||
|
||
public final class Volumes { | ||
|
||
private static final String CLASSPATH = "classpath:"; | ||
private static final String EMPTY = ""; | ||
|
||
private Volumes() { | ||
|
||
} | ||
|
||
public static void addVolumes(GenericContainer<?> container, Map<String, String> volumes) { | ||
for (Map.Entry<String, String> volume : volumes.entrySet()) { | ||
String hostLocation = volume.getKey(); | ||
BindMode bindMode = BindMode.READ_WRITE; | ||
if (volume.getKey().startsWith(CLASSPATH)) { | ||
URL url = Thread.currentThread().getContextClassLoader() | ||
.getResource(hostLocation.replaceFirst(CLASSPATH, EMPTY)); | ||
if (url == null) { | ||
throw new IllegalStateException("Classpath resource at '" + hostLocation + "' not found!"); | ||
} | ||
|
||
hostLocation = url.getPath(); | ||
bindMode = BindMode.READ_ONLY; | ||
} | ||
|
||
container.withFileSystemBind(hostLocation, volume.getValue(), bindMode); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...quarkus/jdbc/postgresql/deployment/DevServicesPostgresqlDatasourceWithVolumeTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.quarkus.jdbc.postgresql.deployment; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.sql.CallableStatement; | ||
import java.sql.Connection; | ||
import java.sql.ResultSet; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class DevServicesPostgresqlDatasourceWithVolumeTestCase { | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest test = new QuarkusUnitTest() | ||
.overrideConfigKey("quarkus.datasource.db-kind", "postgresql") | ||
// The official postgres image will execute all the scripts in the folder "docker-entrypoint-initdb.d" | ||
.overrideConfigKey("quarkus.datasource.devservices.volumes.\"classpath:./init-db.sql\"", | ||
"/docker-entrypoint-initdb.d/init-db.sql"); | ||
|
||
@Inject | ||
DataSource ds; | ||
|
||
@Test | ||
@DisplayName("Test if volume is mounted successfully") | ||
public void testDatasource() throws Exception { | ||
int result = 0; | ||
try (Connection con = ds.getConnection(); | ||
CallableStatement cs = con.prepareCall("SELECT my_func()"); | ||
ResultSet rs = cs.executeQuery()) { | ||
if (rs.next()) { | ||
result = rs.getInt(1); | ||
} | ||
} | ||
assertEquals(100, result, "The init script should have been executed"); | ||
} | ||
} |