Skip to content

Commit

Permalink
Initial Version of Oracle Testcontainer
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernd Eckenfels committed Oct 13, 2023
1 parent 657759f commit 90f0584
Show file tree
Hide file tree
Showing 7 changed files with 504 additions and 4 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/maven-verify.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven

name: Java CI with Maven
name: "Verify Resources (java-maven CI)"

on:
push:
Expand All @@ -10,12 +10,12 @@ on:
branches: [ "main" ]

jobs:
build:
verify:

runs-on: ubuntu-latest

steps:
- name: Check our Repository
- name: Check out Repository
uses: actions/checkout@v3

- name: Set up JDK 21
Expand All @@ -25,7 +25,7 @@ jobs:
distribution: 'zulu'
cache: maven
- name: Build with Maven
run: cd tests/ && mvn -B -npr verify
run: cd tests/ && mvn -B -V -ntp -e verify

# Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
- name: Update dependency graph
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Official resources for SEEBURGER BIS 6.7 customers.
## Subdirectories

* [`installation`](installation/) - scripts and templates mentioned in the *SEEBURGER BIS6 Installation Manual* and the *BIS6 System Database Manual*.
* [`tests`](tests/) - Maven/Java project used to test some of the resources in this repository.

## Project Download and Usage

Expand Down
3 changes: 3 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vscode
target/
.project
19 changes: 19 additions & 0 deletions tests/README.md
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
```
71 changes: 71 additions & 0 deletions tests/pom.xml
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 tests/src/test/java/com/seeburger/bis6resources/tests/OracleTests.java
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);
}
}
}



Loading

0 comments on commit 90f0584

Please sign in to comment.