-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial Version of Oracle Testcontainer
- Loading branch information
Bernd Eckenfels
committed
Oct 13, 2023
1 parent
657759f
commit 90f0584
Showing
7 changed files
with
504 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.vscode | ||
target/ | ||
.project |
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,19 @@ | ||
Maven/Java project used to test some of the resources in this repository. | ||
|
||
## Files | ||
|
||
* pom.xml - The Maven Project Model build file to run the tests | ||
* OracleTest.java - Tests ``../installation/systemdatabase/oracle/*.sql`` | ||
|
||
## Prerequiste | ||
|
||
This requires JDK 17, a running Docker environment and Maven 3.9.x on the path. | ||
|
||
## Run Tests | ||
|
||
Execute | ||
|
||
``` | ||
cd tests | ||
mvn verify | ||
``` |
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,71 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.seeburger.doc.bis6resoures</groupId> | ||
<artifactId>bis6resources-tests</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>Testing bis6-resources (installation scripts) with Testcontainers.</name> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
<maven.compiler.release>17</maven.compiler.release> | ||
</properties> | ||
|
||
<dependencies> | ||
<!-- Required as long as OraceFreeContainer is not in testcontainers-java --> | ||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-lang3</artifactId> | ||
<version>3.13.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>testcontainers</artifactId> | ||
<version>1.19.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>oracle-xe</artifactId> | ||
<version>1.19.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<version>5.7.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.oracle.database.jdbc</groupId> | ||
<artifactId>ojdbc11</artifactId> | ||
<version>23.3.0.23.09</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<inceptionYear>2023</inceptionYear> | ||
<organization> | ||
<name>SEEBURGER AG, Germany</name> | ||
<url>https://github.com/seeburger-ag/</url> | ||
</organization> | ||
<licenses> | ||
<license> | ||
<name>MIT License</name> | ||
<url>https://spdx.org/licenses/MIT.html</url> | ||
<comments>Applied to OracleFreeContainer.java</comments> | ||
</license> | ||
<license> | ||
<name>ASL-2.0</name> | ||
<comments>Applies to files in tests/ directory</comments> | ||
<url>https://spdx.org/licenses/Apache-2.0.html</url> | ||
</license> | ||
<license> | ||
<name>SEEBURGER Proprietary</name> | ||
<distribution>manual</distribution> | ||
<comments>See /LICENSE.txt</comments> | ||
<url>/LICENSE.txt</url> | ||
</license> | ||
</licenses> | ||
</project> |
183 changes: 183 additions & 0 deletions
183
tests/src/test/java/com/seeburger/bis6resources/tests/OracleTests.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,183 @@ | ||
/* | ||
* OracleTests.java | ||
* | ||
* created at 2023-10-07 by Bernd Eckenfels <[email protected]> | ||
* | ||
* Copyright (c) SEEBURGER AG, Germany. All Rights Reserved. | ||
*/ | ||
package com.seeburger.bis6resources.tests; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardOpenOption; | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
|
||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.testcontainers.containers.Container.ExecResult; | ||
import org.testcontainers.utility.MountableFile; | ||
import org.testcontainers.containers.OracleFreeContainer; | ||
|
||
/** | ||
* Testing the installation/systemdatabase/oracle/*.sql DDL scripts | ||
* <p> | ||
* This test will use Oracle Database in Testcontainer to apply the | ||
* sample scripts and validate that they are in effect. | ||
*/ | ||
class OracleTests | ||
{ | ||
static final private String BASE = "../installation/systemdatabase/oracle/"; | ||
|
||
static OracleFreeContainer dbContainer = null; | ||
|
||
@BeforeAll | ||
static void setUpBeforeClass() | ||
throws Exception | ||
{ | ||
System.out.println("Starting container..."); | ||
|
||
// we do this here to not hide the fail reason in Initializer Exception | ||
//DockerImageName name = DockerImageName.parse("container-registry.oracle.com/database/free:latest").asCompatibleSubstituteFor("gvenzl/oracle-xe"); | ||
dbContainer = new OracleFreeContainer(); | ||
|
||
dbContainer.start(); | ||
System.out.println("done."); | ||
} | ||
|
||
@AfterAll | ||
static void afterClass() | ||
throws Exception | ||
{ | ||
if (dbContainer != null) | ||
{ | ||
var tmp = dbContainer; | ||
dbContainer = null; | ||
|
||
System.out.println("Stopping container..."); | ||
tmp.stop(); | ||
System.out.println("done."); | ||
} | ||
} | ||
|
||
@Test | ||
void testCreateDatabase() throws UnsupportedOperationException, IOException, InterruptedException, SQLException | ||
{ | ||
// we need to run in PDB and not use OMF, also can use smaller files | ||
adjustAndTransferFile("create-tbs.sql", | ||
"-- ALTER SESSION SET CONTAINER=\"PDBSEEBIS\";", "ALTER SESSION SET CONTAINER=\"FREEPDB1\";", | ||
"SEEDTA DATAFILE SIZE 2G", "SEEDTA DATAFILE 'seedta.dbf' SIZE 20M", | ||
"SEEIDX DATAFILE SIZE 2G", "SEEIDX DATAFILE 'seeidx.dbf' SIZE 20M", | ||
"SEELOB DATAFILE SIZE 5G", "SEELOB DATAFILE 'seelob.dbf' SIZE 20M"); | ||
|
||
adjustAndTransferFile("create-roles.sql", | ||
"-- ALTER SESSION SET CONTAINER=\"PDBSEEBIS\";", "ALTER SESSION SET CONTAINER=\"FREEPDB1\";"); | ||
adjustAndTransferFile("create-schema.sql", | ||
"-- ALTER SESSION SET CONTAINER=\"PDBSEEBIS\";", "ALTER SESSION SET CONTAINER=\"FREEPDB1\";"); | ||
adjustAndTransferFile("create-user.sql", | ||
"-- ALTER SESSION SET CONTAINER=\"PDBSEEBIS\";", "ALTER SESSION SET CONTAINER=\"FREEPDB1\";"); | ||
|
||
//transferFiles("grant-runtime-user.sql"); | ||
|
||
executeScript("/ AS SYSDBA", "create-tbs.sql"); | ||
executeScript("/ AS SYSDBA", "create-roles.sql"); | ||
executeScript("/ AS SYSDBA", "create-schema.sql"); | ||
executeScript("/ AS SYSDBA", "create-user.sql"); | ||
|
||
// test db owner can login and create a table | ||
try(Connection c = DriverManager.getConnection(dbContainer.getJdbcUrl(), "seeasdb0", "secret"); | ||
Statement s = c.createStatement(); ) | ||
{ | ||
s.executeUpdate("CREATE TABLE tTest(cTest VARCHAR2(256))"); | ||
} | ||
|
||
// run the grant script and verify it has permission on new table | ||
adjustAndTransferFile("grant-runtime-user.sql", | ||
"SEERUN0B", "SEERUN0"); | ||
|
||
executeScript("seeasdb0/secret@FREEPDB1", "grant-runtime-user.sql"); | ||
|
||
// test runtimew user can login and select the new table | ||
try(Connection c = DriverManager.getConnection(dbContainer.getJdbcUrl(), "seerun0", "secret"); | ||
Statement s = c.createStatement(); ) | ||
{ | ||
// this also validated the search path finds the table | ||
s.executeUpdate("SELECT cTest from tTest"); | ||
} | ||
} | ||
|
||
/** Copy the file(s) from base directory to container /tmp. */ | ||
private void transferFiles(String... files) | ||
{ | ||
for(String f : files) | ||
{ | ||
MountableFile mf = MountableFile.forHostPath(BASE + f); | ||
dbContainer.copyFileToContainer(mf, "/tmp/"+f); | ||
} | ||
} | ||
|
||
/** Copy the file from base directory to container by string replacing arguments. */ | ||
private void adjustAndTransferFile(String file, String... replacements) throws IOException | ||
{ | ||
String content = Files.readString(Paths.get(BASE + file), StandardCharsets.ISO_8859_1); | ||
int pos = 0; | ||
while(pos < replacements.length) | ||
{ | ||
String search = replacements[pos++]; | ||
String replace = replacements[pos++]; | ||
|
||
if (!content.contains(search)) | ||
{ | ||
fail("Test setup changed, file " + file + " no longer contains <" + search + ">"); | ||
} | ||
|
||
content = content.replace(search, replace); | ||
} | ||
|
||
Path tmp = Paths.get("target/" + file); | ||
Files.writeString(tmp, content, StandardCharsets.ISO_8859_1, StandardOpenOption.CREATE); | ||
|
||
MountableFile mf = MountableFile.forHostPath(tmp); | ||
dbContainer.copyFileToContainer(mf, "/tmp/"+file); | ||
} | ||
|
||
/** | ||
* Copy file to container and execute it with sqlplus. | ||
* <p> | ||
* After command execution error code and output is checked for errors. | ||
* | ||
* @param login user string for sqlplus | ||
* @param file sql file name in /tmp directory in container | ||
*/ | ||
private void executeScript(String login, String file) throws UnsupportedOperationException, IOException, InterruptedException | ||
{ | ||
System.out.println("Executing " + file + " as " + login); | ||
|
||
// prepend the container change since we log into root CDB. | ||
ExecResult r = dbContainer.execInContainer("bash", "-c", | ||
"sqlplus -s " + login + " < /tmp/" + file); | ||
|
||
String result = "err: " + r.getStderr() + " out: " + r.getStdout(); | ||
|
||
if (result.contains("ERROR") ||result.contains("WARN") || result.contains("ORA-")) | ||
{ | ||
fail("The script execution of " + file + " showed error text " + result); | ||
} | ||
|
||
if (r.getExitCode() != 0) | ||
{ | ||
fail("The script executoin of " + file + " returned exit code " + r.getExitCode() + "out=" + result); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
Oops, something went wrong.