Skip to content

Commit

Permalink
improve unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
lxhoan committed Jun 5, 2018
1 parent fd8f830 commit 232be1b
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 214 deletions.
126 changes: 41 additions & 85 deletions compile-and-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,6 @@
#
# script to start influxdb and compile influxdb-java with all tests.
#
# Note for Windows users:
# In case your docker still uses VirtualBox as a VM, you will probably have to
# inform the containers about the location of your repository.
# Please not that Docker for windows, enables you to mount everything
# from your Users (C:\Users in most cases) directory, so to keep it easy,
# it's better to keep your repository somewhere there.
# If you will decide to put your sources somewhere else, please visit your
# VirtualBox settings and check out the "Shared folder configuration".
# This script uses environment variable BUILD_HOME which should point to this
# project directory (i.e. //c/Users/MyWindowsUserName/Projects/influxdb-java)
#
# Of course you still need bash to launch this script. But this should be no
# problem either to install it (this script was tested with GitExtensions package).

set -e

DEFAULT_INFLUXDB_VERSION="1.5"
Expand All @@ -24,75 +10,45 @@ DEFAULT_MAVEN_JAVA_VERSION="3-jdk-10-slim"
INFLUXDB_VERSION="${INFLUXDB_VERSION:-$DEFAULT_INFLUXDB_VERSION}"
MAVEN_JAVA_VERSION="${MAVEN_JAVA_VERSION:-$DEFAULT_MAVEN_JAVA_VERSION}"

WORKDIR=/usr/src/mymaven

if [ -z "$BUILD_HOME" ] ; then
BUILD_HOME=$PWD
if [ -x /c/Windows/System32/ ] ; then
BUILD_HOME=/$PWD
fi
fi

if [ -x /c/Windows/System32/ ] ; then
echo "Detected Windows as a host system"
WORKDIR=//usr/src/mymaven
fi

echo Using build home: $BUILD_HOME

function run_test {
USE_PROXY=$1

INFLUXDB_API_URL=http://influxdb:8086
if [ "$USE_PROXY" == "nginx" ] ; then
echo Test with Nginx as proxy
INFLUXDB_API_URL=http://nginx:8080/influx-api/
fi


echo "Run tests with maven:${MAVEN_JAVA_VERSION} on onfluxdb-${INFLUXDB_VERSION}"
docker kill influxdb || true
docker rm influxdb || true
docker pull influxdb:${version}-alpine || true
docker run \
echo "Run tests with maven:${MAVEN_JAVA_VERSION} on onfluxdb-${INFLUXDB_VERSION}"
docker kill influxdb || true
docker rm influxdb || true
docker pull influxdb:${version}-alpine || true
docker run \
--detach \
--name influxdb \
--publish 8086:8086 \
--publish 8089:8089/udp \
--volume ${PWD}/influxdb.conf:/etc/influxdb/influxdb.conf \
influxdb:${INFLUXDB_VERSION}-alpine

echo Starting Nginx
docker kill nginx || true
docker rm nginx || true

echo ----- STARTING NGINX CONTAINER -----
docker run \
--detach \
--name influxdb \
--publish 8086:8086 \
--publish 8089:8089/udp \
--volume ${BUILD_HOME}/influxdb.conf:/etc/influxdb/influxdb.conf \
influxdb:${INFLUXDB_VERSION}-alpine

NGINX_LINK=
SKIP_TESTS=
if [ "$USE_PROXY" == "nginx" ] ; then
echo Starting Nginx
docker kill nginx || true
docker rm nginx || true
echo ----- STARTING NGINX CONTAINER -----
docker run \
--detach \
--name nginx \
--publish 8888:8080 \
--volume ${BUILD_HOME}/src/test/nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro \
--link influxdb:influxdb \
nginx nginx '-g' 'daemon off;'
NGINX_LINK=--link=nginx
SKIP_TESTS=-DsomeModule.test.excludes="**/*UDPInfluxDBTest*"
fi

docker run -it --rm \
--volume $BUILD_HOME:/usr/src/mymaven \
--volume $BUILD_HOME/.m2:/root/.m2 \
--workdir $WORKDIR \
--link=influxdb $NGINX_LINK \
--env INFLUXDB_API_URL=$INFLUXDB_API_URL \
maven:${MAVEN_JAVA_VERSION} mvn clean install $SKIP_TESTS

docker kill influxdb || true
if [ "$USE_PROXY" == "nginx" ] ; then
docker kill nginx || true
fi
}

run_test nginx
run_test
--name nginx \
--publish 8080:8080 \
--publish 8080:8080/udp \
--volume ${PWD}/src/test/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
--link influxdb:influxdb \
nginx:stable nginx '-g' 'daemon off;'

PROXY_API_URL=http://nginx:8080/influx-api/
PROXY_UDP_PORT=8080

docker run -it --rm \
--volume $PWD:/usr/src/mymaven \
--volume $PWD/.m2:/root/.m2 \
--workdir /usr/src/mymaven \
--link=influxdb \
--link=nginx \
--env INFLUXDB_IP=influxdb \
--env PROXY_API_URL=$PROXY_API_URL \
--env PROXY_UDP_PORT=$PROXY_UDP_PORT \
maven:${MAVEN_JAVA_VERSION} mvn clean install

docker kill influxdb || true
docker kill nginx || true
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<configuration>
<excludes>
<exclude>${someModule.test.excludes}</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
7 changes: 4 additions & 3 deletions src/test/java/org/influxdb/InfluxDBFactoryTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package org.influxdb;

import okhttp3.OkHttpClient;
import org.influxdb.dto.Pong;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

import okhttp3.OkHttpClient;

/**
* Test the InfluxDB Factory API.
*
Expand All @@ -21,7 +22,7 @@ public class InfluxDBFactoryTest {
*/
@Test
public void testCreateInfluxDBInstanceWithoutUserNameAndPassword() {
InfluxDB influxDB = InfluxDBFactory.connect(TestUtils.getInfluxURL());
InfluxDB influxDB = InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true));
verifyInfluxDBInstance(influxDB);
}

Expand All @@ -37,7 +38,7 @@ private void verifyInfluxDBInstance(InfluxDB influxDB) {
*/
@Test
public void testCreateInfluxDBInstanceWithClientAndWithoutUserNameAndPassword() {
InfluxDB influxDB = InfluxDBFactory.connect(TestUtils.getInfluxURL(), new OkHttpClient.Builder());
InfluxDB influxDB = InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true), new OkHttpClient.Builder());
verifyInfluxDBInstance(influxDB);
}

Expand Down
77 changes: 77 additions & 0 deletions src/test/java/org/influxdb/InfluxDBProxyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.influxdb;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.influxdb.dto.Point;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
/**
* Test the InfluxDB API.
*
* @author hoan.le [at] bonitoo.io
*
*/
@RunWith(JUnitPlatform.class)
public class InfluxDBProxyTest {
private InfluxDB influxDB;
private String db = "udp";

@BeforeEach
public void setUp() throws InterruptedException, IOException {
this.influxDB = TestUtils.connectToInfluxDB(TestUtils.getProxyApiUrl());
this.influxDB.createDatabase(db);
influxDB.setDatabase(db);
}

/**
* delete database after all tests end.
*/
@AfterEach
public void cleanup(){
this.influxDB.deleteDatabase(db);
}

@Test
public void testWriteSomePointThroughTcpProxy() {
for(int i = 0; i < 20; i++) {
Point point = Point.measurement("weather")
.time(i,TimeUnit.HOURS)
.addField("temperature", (double) i)
.addField("humidity", (double) (i) * 1.1)
.addField("uv_index", "moderate").build();
influxDB.write(point);
}

QueryResult result = influxDB.query(new Query("select * from weather", db));
//check points written already to DB
Assertions.assertEquals(20, result.getResults().get(0).getSeries().get(0).getValues().size());

}

@Test
public void testWriteSomePointThroughUdpProxy() throws InterruptedException {
int proxyUdpPort = Integer.parseInt(TestUtils.getProxyUdpPort());
for(int i = 0; i < 20; i++) {
Point point = Point.measurement("weather")
.time(i,TimeUnit.HOURS)
.addField("temperature", (double) i)
.addField("humidity", (double) (i) * 1.1)
.addField("uv_index", "moderate").build();
influxDB.write(proxyUdpPort, point);
}

Thread.sleep(2000);
QueryResult result = influxDB.query(new Query("select * from weather", db));
//check points written already to DB
Assertions.assertEquals(20, result.getResults().get(0).getSeries().get(0).getValues().size());

}

}
6 changes: 3 additions & 3 deletions src/test/java/org/influxdb/InfluxDBTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ public void testBatchEnabledTwice() {
*/
@Test
public void testCloseInfluxDBClient() {
InfluxDB influxDB = InfluxDBFactory.connect(TestUtils.getInfluxURL(), "admin", "admin");
InfluxDB influxDB = InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true), "admin", "admin");
influxDB.enableBatch(1, 1, TimeUnit.SECONDS);
Assertions.assertTrue(influxDB.isBatchEnabled());
influxDB.close();
Expand All @@ -670,7 +670,7 @@ public void testCloseInfluxDBClient() {
*/
@Test
public void testWriteEnableGzip() {
InfluxDB influxDBForTestGzip = InfluxDBFactory.connect(TestUtils.getInfluxURL(), "admin", "admin");
InfluxDB influxDBForTestGzip = InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true), "admin", "admin");
String dbName = "write_unittest_" + System.currentTimeMillis();
try {
influxDBForTestGzip.setLogLevel(LogLevel.NONE);
Expand Down Expand Up @@ -702,7 +702,7 @@ public void testWriteEnableGzip() {
*/
@Test
public void testWriteEnableGzipAndDisableGzip() {
InfluxDB influxDBForTestGzip = InfluxDBFactory.connect(TestUtils.getInfluxURL(), "admin", "admin");
InfluxDB influxDBForTestGzip = InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true), "admin", "admin");
try {
//test default: gzip is disable
Assertions.assertFalse(influxDBForTestGzip.isGzipEnabled());
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/influxdb/PerformanceTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class PerformanceTests {

@BeforeEach
public void setUp() {
this.influxDB = InfluxDBFactory.connect(TestUtils.getInfluxURL(), "root", "root");
this.influxDB = InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true), "root", "root");
this.influxDB.setLogLevel(LogLevel.NONE);
this.influxDB.createDatabase(UDP_DATABASE);
}
Expand Down
Loading

0 comments on commit 232be1b

Please sign in to comment.