The scope of this project is offer a simple way to execute tests with HBase.
HBase uses hostnames to pass connection data back out of the container (from it's internal Zookeeper) and this behaviour creates 2 problems:
- a hostname is needed
- TestContainer follow this principle: From the host's perspective Testcontainers actually exposes this on a random free port. This is by design, to avoid port collisions that may arise with locally running software or in between parallel test runs.
The class HBase Container breaks the Testcontainers' principle, mapping exposed ports to the same container ports. With this workaround we can call HBase from the host or from the container using the same address.
Remember to define a custom hosts file to use when you run your test. This file has to contain something like this:
127.0.0.1 hbase_container_name
You can see an example in the repository.
There are multiple ways to load this files: below some of them:
- as java option:
-Djdk.net.hosts.file=./path/hosts
- you can create a file on the root of the project
.mvn/jvm.config
and add the java option - in the pom.xml, you can add the parameter in the surfer plugin like this:
<properties>
<jvm.options>-Djdk.net.hosts.file=./path/hosts</jvm.options>
</properties>
...
<plugins>
...
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<argLine>${jvm.options}</argLine>
</configuration>
</plugin>
</plugin>
<dependency>
<groupId>io.github.abaddon.testcontainer</groupId>
<artifactId>hbasetestcontainer</artifactId>
<version>0.0.2</version>
</dependency>
- Use HBaseContainer class in your test class:
public class HBaseStoreTest {
private final static String HBASE_HOSTNAME = "hbase-docker";
@ClassRule
public static HBaseContainer hbaseContainer = new HBaseContainer(HBASE_HOSTNAME);
@Test
public void test() throws IOException {
....
}
- create a
hosts
file and load it when you run your tests (if you need it).
You can use the method withDataFolderBind(String hostPath)
. Where hostPath is the folder on the host.
If it's not exist it will be created automatically.
At the moment, the folder is deleted and recreated at each run.
@ClassRule
public static HBaseContainer hbaseContainer = new HBaseContainer(HBASE_HOSTNAME)
.withDataFolderBind("./hbase_data");;