Skip to content

Commit

Permalink
Merge pull request #925 from HorizenOfficial/dd/SDK-1503-jacoco
Browse files Browse the repository at this point in the history
SDK-1503 : Jacoco integration
  • Loading branch information
paolocappelletti authored Nov 14, 2023
2 parents 04d5af2 + 1da80a8 commit 5f89839
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ qa/.metals/metals.lock.db
qa/.metals/metals.mv.db
qa/.metals/metals.log
.scalafmt.conf

# Jacoco reports
coverage-reports/sidechains-sdk-0.8.0-SNAPSHOT/sidechains-sdk-0.8.0-SNAPSHOT-jacoco-report/
57 changes: 57 additions & 0 deletions coverage-reports/generate_report.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash

# this script generates jacoco code coverage report
# before running it, it is necessary to have the following env vars set - $SIDECHAIN_SDK, $BITCOIND and $BITCOINCLI
# additionally, it is necessary to have org.jacoco.cli-0.8.9-nodeps.jar which can be found here - https://repo1.maven.org/maven2/org/jacoco/org.jacoco.cli/0.8.9/
# this script should be run in the root of coverage-reports folder

# Specify snapshot version
SNAPSHOT_VERSION_TAG="0.8.0-SNAPSHOT"

# Check if SIDECHAIN_SDK is set and not empty
if [ -z "$SIDECHAIN_SDK" ]; then
echo "Error: SIDECHAIN_SDK is not set or is empty. Please set the environment variable."
exit 1
fi

# Step 1: Change to the project directory
echo "Changing to the project directory..."
cd ../sdk || exit 1

# Step 2: Run 'mvn clean install' in the project directory
# it is necessary to let test phase run as well, because this phase creates .exec file for this code coverage report
echo "Running 'mvn clean install' in the project directory..."
mvn clean install

# Check if BITCOIND and BITCOINCLI are set and not empty before running python tests
if [ -n "$BITCOIND" ] && [ -n "$BITCOINCLI" ]; then
# Step 3: Execute run_sc_tests.sh
# this phase runs python tests which append to the previously created .exec file to get the full code coverage report
echo "Executing script for integration tests..."
cd ../qa || exit 1
./run_sc_tests.sh -jacoco
else
echo "Warning: Either BITCOIND or BITCOINCLI is not set or is empty. Please set both environment variables."
fi

# Step 3: Execute run_sc_tests.sh
# this phase runs python tests which append to the previously created .exec file to get the full code coverage report
echo "Executing script for integration tests..."
cd ../qa || exit 1
./run_sc_tests.sh -jacoco

# Step 4: Generate the JaCoCo code coverage report
# this phase creates the detailed report with html files for easier browsing
echo "Generating JaCoCo code coverage report..."

JACOCO_JAR_PATH="$HOME/.m2/repository/org/jacoco/org.jacoco.cli/0.8.9/org.jacoco.cli-0.8.9-nodeps.jar"
EXEC_PATH="$SIDECHAIN_SDK/coverage-reports/sidechains-sdk-${SNAPSHOT_VERSION_TAG}/sidechains-sdk-${SNAPSHOT_VERSION_TAG}-jacoco-report.exec"
CLASSFILES_PATH="$SIDECHAIN_SDK/sdk/target/classes"
HTML_PATH="$SIDECHAIN_SDK/coverage-reports/sidechains-sdk-${SNAPSHOT_VERSION_TAG}/sidechains-sdk-${SNAPSHOT_VERSION_TAG}-jacoco-report"

java -jar "$JACOCO_JAR_PATH" \
report "$EXEC_PATH" \
--classfiles "$CLASSFILES_PATH" \
--html "$HTML_PATH"

echo "Code coverage report generation complete."
6 changes: 5 additions & 1 deletion qa/SidechainTestFramework/sc_test_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from SidechainTestFramework.scutil import initialize_default_sc_chain_clean, \
start_sc_nodes, stop_sc_nodes, \
sync_sc_blocks, sync_sc_mempools, TimeoutException, bootstrap_sidechain_nodes, APP_LEVEL_INFO, set_sc_parallel_test, \
SNAPSHOT_VERSION_TAG
SNAPSHOT_VERSION_TAG, set_jacoco
import os
import tempfile
import traceback
Expand Down Expand Up @@ -186,6 +186,8 @@ def main(self):
help="Stores parallel process integer assigned to current test")
parser.add_option("--mcblockdelay", dest="mcblockdelay", type=int, default=0, action="store",
help="Stores mainchain block delay reference parameter")
parser.add_option("--jacoco", dest="jacoco", default=False, action="store_true",
help="Stores jacoco flag assigned to current test")

self.add_options(parser)
self.sc_add_options(parser)
Expand All @@ -209,6 +211,8 @@ def main(self):
if parallel_group > 0:
self.set_parallel_test(parallel_group)

set_jacoco(self.options.jacoco)

self.setup_chain()

self.setup_network()
Expand Down
18 changes: 17 additions & 1 deletion qa/SidechainTestFramework/scutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
# Parallel Testing
parallel_test = 0

# flag for jacoco code coverage analysis
is_jacoco_included = False


class TimeoutException(Exception):
def __init__(self, operation):
Expand All @@ -74,6 +77,9 @@ def set_sc_parallel_test(n):
global parallel_test
parallel_test = n

def set_jacoco(value):
global is_jacoco_included
is_jacoco_included = value

def start_port_modifier():
if parallel_test > 0:
Expand Down Expand Up @@ -691,7 +697,17 @@ def start_sc_node(i, dirname, extra_args=None, rpchost=None, timewait=None, bina
Currently, it is permitted by default and a warning is issued.
The --add-opens VM option remove this warning.
'''
bashcmd = 'java --add-opens java.base/java.lang=ALL-UNNAMED ' + dbg_agent_opt + ' -cp ' + binary + " " + cfgFileName \

user_home = os.path.expanduser("~")
jacoco_agent_path = os.path.join(user_home, ".m2", "repository", "org", "jacoco", "org.jacoco.agent", "0.8.9",
"org.jacoco.agent-0.8.9-runtime.jar")
jacoco_cmd = f'-javaagent:{jacoco_agent_path}=destfile=../coverage-reports/sidechains-sdk-{SNAPSHOT_VERSION_TAG}/sidechains-sdk-{SNAPSHOT_VERSION_TAG}-jacoco-report.exec,append=true'

if is_jacoco_included:
bashcmd = 'java --add-opens java.base/java.lang=ALL-UNNAMED ' + jacoco_cmd + dbg_agent_opt + ' -cp ' + binary + " " + cfgFileName \
+ " " + mc_block_delay_ref
else:
bashcmd = 'java --add-opens java.base/java.lang=ALL-UNNAMED ' + dbg_agent_opt + ' -cp ' + binary + " " + cfgFileName \
+ " " + mc_block_delay_ref

if print_output_to_file:
Expand Down
14 changes: 14 additions & 0 deletions qa/run_sc_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ for i in "$@"; do
EVM_ONLY="true"
shift
;;
-jacoco)
JACOCO="true"
shift
;;
-utxo_only)
UTXO_ONLY="true"
shift
Expand Down Expand Up @@ -265,6 +269,16 @@ if [ ! -z "$EXCLUDE" ]; then
done
fi

# add --jacoco flag to each test if jacoco flag set to true
if [ ! -z "$JACOCO" ] && [ "${JACOCO}" = "true" ]; then
modifiedList=()
for testFile in "${testScripts[@]}"; do
modified_test="${testFile} --jacoco"
modifiedList+=("$modified_test")
done
testScripts=("${modifiedList[@]}")
fi

# split array into m parts and only run tests of part n where SPLIT=m:n
if [ ! -z "$SPLIT" ]; then
chunks="${SPLIT%*:*}"
Expand Down
33 changes: 33 additions & 0 deletions sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,39 @@
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.9</version> <!-- Use the latest version available -->
<configuration>
<excludes>
<exclude>org/apache/logging/log4j/core/util/**</exclude>
<exclude>org/apache/logging/log4j/core/util/SystemClock.class</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>../coverage-reports/${project.artifactId}-${project.version}/${project.artifactId}-${project.version}-jacoco-report.exec</destFile>
<append>true</append>
</configuration>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>../coverage-reports/${project.artifactId}-${project.version}/${project.artifactId}-${project.version}-jacoco-report.exec</dataFile>
<outputDirectory>../coverage-reports/${project.artifactId}-${project.version}/${project.artifactId}-${project.version}-jacoco-report</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
Expand Down

0 comments on commit 5f89839

Please sign in to comment.