Skip to content

Feature/version check #40

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

Closed
wants to merge 10 commits into from
Closed
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
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
sudo: required
language: java

services:
- docker

jdk:
- oraclejdk8

env:
global:
- DOCKER_CFG=$HOME/.docker
- CACHE_DIR=$HOME/.cache
- MAVEN_HOME=/usr/local/maven
- MAVEN_CFG=$HOME/.m2
- DB_URL="127.0.0.1:1521:XE"
- DB_USER=app
- DB_PASS=app
matrix:
- ORACLE_VERSION="11g-xe-r2" DOCKER_OPTIONS="--shm-size=1g"

cache:
directories:
- $DOCKER_CFG
- $CACHE_DIR
- $MAVEN_CFG

install:
- bash .travis/maven_cfg.sh
- bash .travis/start_db.sh
- bash .travis/install_utplsql.sh
- bash .travis/install_demo_project.sh

script:
- mvn package -DskipTests
Expand Down
17 changes: 17 additions & 0 deletions .travis/create_api_user.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
set -ev

sqlplus -S -L sys/oracle@//127.0.0.1:1521/xe AS SYSDBA <<EOF
create user api identified by api
quota unlimited on USERS
default tablespace USERS;
grant create session,
create procedure,
create type,
create table,
create sequence,
create view
to api;
grant select any dictionary to api;
exit;
EOF
44 changes: 44 additions & 0 deletions .travis/install_demo_project.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash
set -ev
cd $(dirname $(readlink -f $0))

PROJECT_FILE="utPLSQL-demo-project"
git clone -b develop --single-branch https://github.com/utPLSQL/utPLSQL-demo-project.git

cat > demo_project.sh.tmp <<EOF
sqlplus -S -L sys/oracle@//127.0.0.1:1521/xe AS SYSDBA <<SQL
create user ${DB_USER} identified by ${DB_PASS} quota unlimited on USERS default tablespace USERS;
grant create session, create procedure, create type, create table, create sequence, create view to ${DB_USER};
grant select any dictionary to ${DB_USER};
exit
SQL

cd ${PROJECT_FILE}
sqlplus -S -L ${DB_USER}/${DB_PASS}@//127.0.0.1:1521/xe <<SQL
whenever sqlerror exit failure rollback
whenever oserror exit failure rollback

@source/award_bonus/employees_test.sql
@source/award_bonus/award_bonus.prc

@source/between_string/betwnstr.fnc

@source/remove_rooms_by_name/rooms.sql
@source/remove_rooms_by_name/remove_rooms_by_name.prc

@test/award_bonus/test_award_bonus.pks
@test/award_bonus/test_award_bonus.pkb

@test/between_string/test_betwnstr.pks
@test/between_string/test_betwnstr.pkb

@test/remove_rooms_by_name/test_remove_rooms_by_name.pks
@test/remove_rooms_by_name/test_remove_rooms_by_name.pkb

exit
SQL
EOF

docker cp ./$PROJECT_FILE $ORACLE_VERSION:/$PROJECT_FILE
docker cp ./demo_project.sh.tmp $ORACLE_VERSION:/demo_project.sh
docker exec $ORACLE_VERSION bash demo_project.sh
36 changes: 36 additions & 0 deletions .travis/install_utplsql.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash
set -ev
cd $(dirname $(readlink -f $0))

# Download the specified version of utPLSQL.
UTPLSQL_VERSION="v3.0.4"
UTPLSQL_FILE="utPLSQL"
curl -L -O "https://github.com/utPLSQL/utPLSQL/releases/download/$UTPLSQL_VERSION/$UTPLSQL_FILE.tar.gz"

# Download develop branch of utPLSQL.
#UTPLSQL_VERSION="develop"
#UTPLSQL_FILE="utPLSQL"
#git clone -b develop --single-branch https://github.com/utPLSQL/utPLSQL.git
# tar -czf $UTPLSQL_FILE.tar.gz $UTPLSQL_FILE && rm -rf $UTPLSQL_FILE

# Create a temporary install script.
cat > install.sh.tmp <<EOF
tar -xzf ${UTPLSQL_FILE}.tar.gz && rm ${UTPLSQL_FILE}.tar.gz
cd ${UTPLSQL_FILE}/source
sqlplus -S -L sys/oracle@//127.0.0.1:1521/xe AS SYSDBA @install_headless.sql ut3 ut3 users
EOF

# Copy utPLSQL files to the container and install it.
docker cp ./$UTPLSQL_FILE.tar.gz $ORACLE_VERSION:/$UTPLSQL_FILE.tar.gz
# docker cp ./$UTPLSQL_FILE $ORACLE_VERSION:/$UTPLSQL_FILE
docker cp ./install.sh.tmp $ORACLE_VERSION:/install.sh
docker cp ./create_api_user.sh $ORACLE_VERSION:/create_api_user.sh

# Remove temporary files.
# rm $UTPLSQL_FILE.tar.gz
rm -rf $UTPLSQL_FILE
rm install.sh.tmp

# Execute the utPLSQL installation inside the container.
docker exec $ORACLE_VERSION bash install.sh
docker exec $ORACLE_VERSION bash create_api_user.sh
14 changes: 14 additions & 0 deletions .travis/start_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
set -ev

# If docker credentials are not cached, do the login.
if [ ! -f $DOCKER_CFG/config.json ]; then
docker login -u "$DOCKER_USER" -p "$DOCKER_PASSWORD"
else
echo "Using docker login from cache..."
fi

# Pull the specified db version from docker hub.
docker pull $DOCKER_REPO:$ORACLE_VERSION
docker run -d --name $ORACLE_VERSION $DOCKER_OPTIONS -p 1521:1521 $DOCKER_REPO:$ORACLE_VERSION
docker logs -f $ORACLE_VERSION | grep -m 1 "DATABASE IS READY TO USE!" --line-buffered
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ You can download development versions on [Bintray](https://bintray.com/viniciusa
* [Java SE Runtime Environment 8](http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html)
* When using reporters for Sonar or Coveralls client needs to be invoked from project's root directory.

## Compatibility
The latest CLI is always compatible with all database frameworks of the same major version.
For example CLI-3.0.4 is compatible with database framework 3.0.0-3.0.4 but not with database framework 2.x.

## Usage
utplsql run \<ConnectionURL\> [-p=(ut_path|ut_paths)] [-f=format [-o=output_file] [-s] ...]

Expand Down Expand Up @@ -74,6 +78,8 @@ utplsql run \<ConnectionURL\> [-p=(ut_path|ut_paths)] [-f=format [-o=output_file
-c - If specified, enables printing of test results in colors as defined by ANSICONSOLE standards.
Works only on reporeters that support colors (ut_documentation_reporter).
--failure-exit-code - Override the exit code on failure, defaults to 1. You can set it to 0 to always exit with a success status.
-scc - If specified, skips the compatibility-check with the version of the database framework.
If you skip compatibility-check, CLI will expect the most actual framework version
```

Parameters -f, -o, -s are correlated. That is parameters -o and -s are controlling outputs for reporter specified by the preceding -f parameter.
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.utplsql</groupId>
<artifactId>cli</artifactId>
<version>1.0-SNAPSHOT</version>
<version>3.0.4-SNAPSHOT</version>
<packaging>jar</packaging>

<name>cli</name>
Expand All @@ -20,7 +20,7 @@
<dependency>
<groupId>org.utplsql</groupId>
<artifactId>java-api</artifactId>
<version>1.0-SNAPSHOT</version>
<version>3.0.4-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
117 changes: 96 additions & 21 deletions src/main/java/org/utplsql/cli/RunCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import org.utplsql.api.*;
import org.utplsql.api.exception.DatabaseNotCompatibleException;
import org.utplsql.api.exception.SomeTestsFailedException;
import org.utplsql.api.reporter.Reporter;
import org.utplsql.api.reporter.ReporterFactory;
Expand All @@ -21,6 +22,9 @@

/**
* Created by vinicius.moreira on 19/04/2017.
*
* @author vinicious moreira
* @author pesse
*/
@Parameters(separators = "=", commandDescription = "run tests")
public class RunCommand {
Expand Down Expand Up @@ -70,6 +74,12 @@ public class RunCommand {
"-name_subexpression=0] - path to project test files")
private List<String> testPathParams = new ArrayList<>();

@Parameter(
names = {"-scc", "--skip-compatibility-check"},
description = "Skips the check for compatibility with database framework. CLI expects the framework to be " +
"most actual. Use this if you use CLI with a development version of utPLSQL-framework")
private boolean skipCompatibilityCheck = false;

public ConnectionInfo getConnectionInfo() {
return connectionInfoList.get(0);
}
Expand All @@ -81,36 +91,27 @@ public List<String> getTestPaths() {
public int run() throws Exception {
final ConnectionInfo ci = getConnectionInfo();

final List<Reporter> reporterList;
final List<ReporterOptions> reporterOptionsList = getReporterOptionsList();
final List<String> testPaths = getTestPaths();
final List<Reporter> reporterList = new ArrayList<>();

final File baseDir = new File("").getAbsoluteFile();
final FileMapperOptions[] sourceMappingOptions = {null};
final FileMapperOptions[] testMappingOptions = {null};

final int[] returnCode = {0};

if (!this.sourcePathParams.isEmpty()) {
String sourcePath = this.sourcePathParams.get(0);
List<String> sourceFiles = new FileWalker().getFileList(baseDir, sourcePath);
sourceMappingOptions[0] = getMapperOptions(this.sourcePathParams, sourceFiles);
}

if (!this.testPathParams.isEmpty()) {
String testPath = this.testPathParams.get(0);
List<String> testFiles = new FileWalker().getFileList(baseDir, testPath);
testMappingOptions[0] = getMapperOptions(this.testPathParams, testFiles);
}
sourceMappingOptions[0] = getFileMapperOptionsByParamListItem(this.sourcePathParams, baseDir);
testMappingOptions[0] = getFileMapperOptionsByParamListItem(this.testPathParams, baseDir);

// Do the reporters initialization, so we can use the id to run and gather results.
try (Connection conn = ci.getConnection()) {
for (ReporterOptions ro : reporterOptionsList) {
Reporter reporter = ReporterFactory.createReporter(ro.getReporterName());
reporter.init(conn);
ro.setReporterObj(reporter);
reporterList.add(reporter);
}

// First of all do a compatibility check and fail-fast
checkFrameworkCompatibility(conn);

reporterList = initReporters(conn, reporterOptionsList);

} catch (SQLException e) {
System.out.println(e.getMessage());
return Cli.DEFAULT_ERROR_CODE;
Expand All @@ -128,6 +129,7 @@ public int run() throws Exception {
.testMappingOptions(testMappingOptions[0])
.colorConsole(this.colorConsole)
.failOnErrors(true)
.skipCompatibilityCheck(skipCompatibilityCheck)
.run(conn);
} catch (SomeTestsFailedException e) {
returnCode[0] = this.failureExitCode;
Expand All @@ -138,6 +140,44 @@ public int run() throws Exception {
}
});

// Gather each reporter results on a separate thread.
startReporterGatherers(reporterOptionsList, executorService, ci, returnCode);

executorService.shutdown();
executorService.awaitTermination(60, TimeUnit.MINUTES);
return returnCode[0];
}

/** Initializes the reporters so we can use the id to gather results
*
* @param conn Active Connection
* @param reporterOptionsList
* @return List of Reporters
* @throws SQLException
*/
private List<Reporter> initReporters( Connection conn, List<ReporterOptions> reporterOptionsList ) throws SQLException
{
final List<Reporter> reporterList = new ArrayList<>();

for (ReporterOptions ro : reporterOptionsList) {
Reporter reporter = ReporterFactory.createReporter(ro.getReporterName());
reporter.init(conn);
ro.setReporterObj(reporter);
reporterList.add(reporter);
}

return reporterList;
}

/** Starts a separate thread for each Reporter to gather its results
*
* @param reporterOptionsList
* @param executorService
* @param ci
* @param returnCode
*/
private void startReporterGatherers(List<ReporterOptions> reporterOptionsList, ExecutorService executorService, final ConnectionInfo ci, final int[] returnCode)
{
// Gather each reporter results on a separate thread.
for (ReporterOptions ro : reporterOptionsList) {
executorService.submit(() -> {
Expand Down Expand Up @@ -165,10 +205,23 @@ public int run() throws Exception {
}
});
}
}

executorService.shutdown();
executorService.awaitTermination(60, TimeUnit.MINUTES);
return returnCode[0];
/** Returns FileMapperOptions for the first item of a given param list in a baseDir
*
* @param pathParams
* @param baseDir
* @return FileMapperOptions or null
*/
private FileMapperOptions getFileMapperOptionsByParamListItem(List<String> pathParams, File baseDir )
{
if (!pathParams.isEmpty()) {
String sourcePath = pathParams.get(0);
List<String> files = new FileWalker().getFileList(baseDir, sourcePath);
return getMapperOptions(pathParams, files);
}

return null;
}

public List<ReporterOptions> getReporterOptionsList() {
Expand Down Expand Up @@ -198,6 +251,28 @@ public List<ReporterOptions> getReporterOptionsList() {
return reporterOptionsList;
}

/** Checks whether cli is compatible with the database framework
*
* @param conn Active Connection
* @throws SQLException
*/
private void checkFrameworkCompatibility(Connection conn) throws SQLException {

if ( !skipCompatibilityCheck ) {
try {
DBHelper.failOnVersionCompatibilityCheckFailed(conn);
} catch (DatabaseNotCompatibleException e) {
System.out.println(e.getMessage());

throw e;
}
}
else {
System.out.println("Skipping Compatibility check with framework version, expecting the latest version " +
"to be installed in database");
}
}

public FileMapperOptions getMapperOptions(List<String> mappingParams, List<String> filePaths) {
FileMapperOptions mapperOptions = new FileMapperOptions(filePaths);

Expand Down
Loading