Commit 76b9beb5 authored by Combava Orange's avatar Combava Orange
Browse files

Unit test of clea-batch

parent 0785e4fe
......@@ -82,6 +82,7 @@
<configuration>
<verbose>false</verbose>
<offline>true</offline>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
</configuration>
</plugin>
<plugin>
......@@ -145,5 +146,126 @@
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>test-bats</id>
<activation>
<property>
<name>test.bats</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.11.2</version>
<executions>
<execution>
<id>clone-bats-core</id>
<goals>
<goal>checkout</goal>
</goals>
<phase>test</phase>
<configuration>
<checkoutDirectory>${project.basedir}/target/bats/core</checkoutDirectory>
<connectionType>connection</connectionType>
<connectionUrl>scm:git:https://github.com/bats-core/bats-core.git</connectionUrl>
</configuration>
</execution>
<execution>
<id>clone-bats-support</id>
<goals>
<goal>checkout</goal>
</goals>
<phase>test</phase>
<configuration>
<checkoutDirectory>${project.basedir}/target/bats/support</checkoutDirectory>
<connectionType>connection</connectionType>
<connectionUrl>scm:git:https://github.com/ztombol/bats-support.git</connectionUrl>
</configuration>
</execution>
<execution>
<id>clone-bats-assert</id>
<goals>
<goal>checkout</goal>
</goals>
<phase>test</phase>
<configuration>
<checkoutDirectory>${project.basedir}/target/bats/assert</checkoutDirectory>
<connectionType>connection</connectionType>
<connectionUrl>scm:git:https://github.com/ztombol/bats-assert.git</connectionUrl>
</configuration>
</execution>
</executions>
<configuration>
<providerImplementations>
<git>jgit</git>
</providerImplementations>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-provider-jgit</artifactId>
<version>1.11.2</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>chmod</id>
<goals>
<goal>exec</goal>
</goals>
<phase>test</phase>
<configuration>
<executable>chmod</executable>
<arguments>
<argument>+x</argument>
<argument>target/bats/core/bin/bats</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>chmod-libexec</id>
<goals>
<goal>exec</goal>
</goals>
<phase>test</phase>
<configuration>
<executable>chmod</executable>
<arguments>
<argument>+x</argument>
<argument>target/bats/core/libexec/bats-core/bats-exec-suite</argument>
<argument>target/bats/core/libexec/bats-core/bats-exec-file</argument>
<argument>target/bats/core/libexec/bats-core/bats-exec-test</argument>
<argument>target/bats/core/libexec/bats-core/bats-preprocess</argument>
<argument>target/bats/core/libexec/bats-core/bats-format-pretty</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>bats</id>
<goals>
<goal>exec</goal>
</goals>
<phase>test</phase>
<configuration>
<executable>target/bats/core/bin/bats</executable>
<arguments>
<argument>src/test/bats</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
\ No newline at end of file
#!/bin/bash
#!/usr/bin/env bash
PROGNAM=$(basename $0)
die() { echo "[$PROGNAM] $*" 1>&2 ; exit 1; }
WORKDIR=${CLEA_BATCH_CLUSTER_OUTPUT_PATH:-/tmp/v1}
BUCKET=${BUCKET:-cleacluster-eu-west-3}
BUCKET=${BUCKET:-}
set -o pipefail # trace ERR through pipes
set -o errtrace # trace ERR through 'time command' and other functions
......@@ -13,21 +13,20 @@ set -o nounset ## set -u : exit the script if you try to use an uninitialised
set +e
[ -n "${BUCKET}" ] || die "Environment variable BUCKET required"
if ! java -jar clea-batch.jar $@ ; then
die "Java batch fails"
fi
# ============================================
# !!!!! MOCK !!!!!!
# ============================================
#GEN=$((1 + $RANDOM % 100))
#mkdir -p $WORKDIR/v1/$GEN
#for i in {1..11} ; do NEW_UUID=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 6 | head -n 1); touch $WORKDIR/v1/$GEN/$NEW_UUID.json; done
#touch /tmp/v1/$GEN/indexCluster.json
# ============================================
echo "[$PROGNAM] Coying files...."
# Test that output folder exists, computing NBFILES fails if folder doesn't exist
[ -d $WORKDIR ] || die "Working directory $WORKDIR not exists"
# count that there is at least "n" cluster files (to not push empty list)
MIN_FILES=1
NBFILES=$(find $WORKDIR -type f | wc -l)
......
#!/usr/bin/env bats
#
# this script is suppose to be execute, by bats (https://github.com/bats-core/bats-core),
# from the root level of "clea" project.
#
# those tests check the batch itself, not the functionnality of the java batch,
# nor the command s3cmd. java and s3cmd can return 0 (success) or <>0 (fails)
#
# Example of execution from "clea" root folder
# $ docker run -it --rm -v $PWD:/app --workdir=/app bats/bats src/test/bats
# ==============================================================================
load '../../../target/bats/support/load'
load '../../../target/bats/assert/load'
setup() {
export BUCKET=clea-batch
echo "Setting BUCKET=clea-batch"
}
@test "Should fail if no BUCKET env" {
unset BUCKET
run src/main/scripts/clea-batch.sh
assert_failure
assert_output --partial "BUCKET required"
}
@test "Should fail on Java failure" {
java() { echo "CALLING_JAVA ${*}" ; return 1; }
export -f java
run src/main/scripts/clea-batch.sh
assert_failure
assert_output --partial "Java batch fails"
}
@test "Should fail if not exists output dir" {
java() { echo "CALLING_JAVA ${*}" ; return 0; }
export -f java
run src/main/scripts/clea-batch.sh
assert_failure
assert_output --regexp "Working directory .+ not exists"
}
@test "Should fail if not enough generated files" {
java() { echo "CALLING_JAVA ${*}" ; return 0; }
export -f java
mkdir -p /tmp/v1/123
rm -rf /tmp/v1/123/*
run src/main/scripts/clea-batch.sh
assert_failure
assert_output --partial "not enough clusterfiles to continue"
}
@test "Should fail if no Cluster index" {
java() { echo "CALLING_JAVA ${*}" ; return 0; }
export -f java
mkdir -p /tmp/v1/123
rm -rf /tmp/v1/123/*
touch /tmp/v1/123/ab.json
touch /tmp/v1/123/ac.json
touch /tmp/v1/123/ad.json
run src/main/scripts/clea-batch.sh
assert_failure
assert_output --partial "No clusterIndex.json generated"
}
@test "Should fail if S3cmd copy of cluster files returns error" {
java() { echo "CALLING_JAVA ${*}" ; return 0; }
export -f java
# fails for s3cmd sync but not for s3cmd put
s3cmd() { echo "CALLING_S3CMD ${*}" ; [ $1 == "sync" ] && return 1 || return 0; }
export -f s3cmd
mkdir -p /tmp/v1/123
rm -rf /tmp/v1/123/*
touch /tmp/v1/123/ab.json
touch /tmp/v1/123/ac.json
touch /tmp/v1/123/ad.json
touch /tmp/v1/clusterIndex.json
run src/main/scripts/clea-batch.sh
assert_failure
assert_output --partial "fails to copy cluster files"
}
@test "Should fail if S3cmd copy of cluster index returns error" {
java() { echo "CALLING_JAVA ${*}" ; return 0; }
export -f java
# fails for s3cmd put but not for s3cmd sync
s3cmd() { echo "CALLING_S3CMD ${*}" ; [ $1 == "put" ] && return 1 || return 0; }
export -f s3cmd
mkdir -p /tmp/v1/123
rm -rf /tmp/v1/123/*
touch /tmp/v1/123/ab.json
touch /tmp/v1/123/ac.json
touch /tmp/v1/123/ad.json
touch /tmp/v1/clusterIndex.json
run src/main/scripts/clea-batch.sh
assert_failure
assert_output --partial "fails to copy clusterIndex"
}
@test "Should succeds with presence of ClusterIndex" {
java() { echo "CALLING_JAVA ${*}" ; return 0; }
export -f java
s3cmd() { echo "CALLING_S3CMD ${*}" ; return 0; }
export -f s3cmd
mkdir -p /tmp/v1/123
rm -rf /tmp/v1/123/*
touch /tmp/v1/123/ab.json
touch /tmp/v1/123/ac.json
touch /tmp/v1/123/ad.json
touch /tmp/v1/clusterIndex.json
run src/main/scripts/clea-batch.sh
assert_success
assert_output --partial "CALLING_JAVA"
assert_output --partial "CALLING_S3CMD sync"
assert_output --partial "CALLING_S3CMD put"
}
@test "Succeeds to purge temporary files at end of batch" {
java() { echo "CALLING_JAVA ${*}" ; return 0; }
export -f java
s3cmd() { echo "CALLING_S3CMD ${*}" ; return 0; }
export -f s3cmd
mkdir -p /tmp/v1/123
rm -rf /tmp/v1/123/*
touch /tmp/v1/123/ab.json
touch /tmp/v1/123/ac.json
touch /tmp/v1/123/ad.json
touch /tmp/v1/clusterIndex.json
run src/main/scripts/clea-batch.sh
assert_success
# test purge
nb_files=$(find /tmp/v1 -type f -print |wc -l)
assert_equal "$nb_files" '0'
}
# clea-batch unit tests with bats
Bats documentation : https://bats-core.readthedocs.io/en/latest/
This script is suppose to be execute, by [bats](https://github.com/bats-core/bats-core), from the root level of "clea" project.
Those tests check the batch itself, not the functionnality of the java batch, neither the command s3cmd. java and s3cmd can return 0 (success) or <>0 (fails)
## Dependencies
This module needs bats-core, bats-support and bats-assert.
From the root folder:
```bash
git clone https://github.com/bats-core/bats-core target/bats/core
git clone https://github.com/ztombol/bats-support.git target/bats/support
git clone https://github.com/ztombol/bats-assert.git target/bats/assert
```
## usage
Example of execution from "clea" root folder
```bash
$ docker run -it --rm -v $PWD:/app --workdir=/app bats/bats src/test/bats
1..9
ok 1 Should fail if no BUCKET env
ok 2 Should fail on Java failure
ok 3 Should fail if not exists output dir
ok 4 Should fail if not enough generated files
ok 5 Should fail if no Cluster index
ok 6 Should fail if S3cmd copy of cluster files returns error
ok 7 Should fail if S3cmd copy of cluster index returns error
ok 8 Should succeds with presence of ClusterIndex
ok 9 Succeeds to purge temporary files at end of batch
$
```
same result with
```bash
$ target/bats/core/bin/bats src/test/bats
✓ Should fail if no BUCKET env
✓ Should fail on Java failure
✓ Should fail if not exists output dir
✓ Should fail if not enough generated files
✓ Should fail if no Cluster index
✓ Should fail if S3cmd copy of cluster files returns error
✓ Should fail if S3cmd copy of cluster index returns error
✓ Should succeds with presence of ClusterIndex
✓ Succeeds to purge temporary files at end of batch
9 tests, 0 failures
$
```
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment