-
Notifications
You must be signed in to change notification settings - Fork 2
Added support for creating tarantool container from sdk #43
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/main/java/org/testcontainers/containers/TarantoolContainerImageHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package org.testcontainers.containers; | ||
|
||
import com.github.dockerjava.api.DockerClient; | ||
import com.github.dockerjava.api.command.BuildImageResultCallback; | ||
import com.github.dockerjava.api.model.Image; | ||
import com.github.dockerjava.core.DockerClientBuilder; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
|
||
/** | ||
* Class for working with docker directly | ||
* | ||
* @author Oleg Kuznetsov | ||
*/ | ||
class TarantoolContainerImageHelper { | ||
|
||
private static final DockerClient dockerClient = DockerClientBuilder.getInstance().build(); | ||
|
||
private TarantoolContainerImageHelper() { | ||
} | ||
|
||
/** | ||
* Checks image for existing by name and build if it not exist | ||
* | ||
* @param imageParams parameters for building tarantool image | ||
* @return image name | ||
*/ | ||
static String getImage(TarantoolImageParams imageParams) { | ||
final String sdkVersion = imageParams.getSdkVersion(); | ||
|
||
if (StringUtils.isEmpty(sdkVersion)) { | ||
throw new IllegalArgumentException("SDK version is null or empty!"); | ||
} | ||
|
||
if (!hasImage(sdkVersion)) { | ||
buildImage(imageParams); | ||
} | ||
|
||
return sdkVersion; | ||
} | ||
|
||
/** | ||
* Builds image from parameters | ||
* | ||
* @param imageParams parameters for building tarantool image | ||
*/ | ||
private static void buildImage(TarantoolImageParams imageParams) { | ||
final String sdkVersion = imageParams.getSdkVersion(); | ||
final String uri = System.getenv("URI"); | ||
|
||
if (StringUtils.isEmpty(uri)) { | ||
throw new IllegalStateException("URI environment variable must be specified!"); | ||
} | ||
|
||
dockerClient.buildImageCmd(imageParams.getDockerfile()) | ||
.withTags(new HashSet<>(Collections.singletonList(sdkVersion))) | ||
.withBuildArg("SDK_VERSION", sdkVersion) | ||
.withBuildArg("URI", uri) | ||
.exec(new BuildImageResultCallback()) | ||
.awaitImageId(); | ||
} | ||
|
||
/** | ||
* Checks image for existing by name | ||
* | ||
* @param imageName image name for searching | ||
* @return true if image exist and false if not | ||
*/ | ||
private static boolean hasImage(String imageName) { | ||
final List<Image> images = dockerClient.listImagesCmd().exec(); | ||
return images.stream() | ||
.map(Image::getRepoTags) | ||
.map(Arrays::asList) | ||
.flatMap(Collection::stream) | ||
.anyMatch(tag -> tag.equals(imageName + ":latest")); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/org/testcontainers/containers/TarantoolImageParams.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package org.testcontainers.containers; | ||
|
||
import java.io.File; | ||
import java.net.URISyntaxException; | ||
|
||
/** | ||
* Tarantool image parameters holder | ||
* | ||
* @author Oleg Kuznetsov | ||
*/ | ||
public class TarantoolImageParams { | ||
|
||
private final String sdkVersion; | ||
private final File dockerfile; | ||
|
||
/** | ||
* Basic constructor for tarantool image parameters | ||
* | ||
* @param sdkVersion version of tarantool sdk which will be downloaded from specified in env variables URI, | ||
* for example: tarantool-enterprise-bundle-2.8.3-21-g7d35cd2be-r470 | ||
*/ | ||
public TarantoolImageParams(String sdkVersion) { | ||
this.sdkVersion = sdkVersion; | ||
try { | ||
this.dockerfile = new File(TarantoolImageParams.class.getClassLoader() | ||
.getResource("sdk/Dockerfile").toURI()); | ||
} catch (URISyntaxException e) { | ||
throw new IllegalStateException("Can't access to Dockerfile for testcontainers"); | ||
} | ||
} | ||
|
||
/** | ||
* Custom constructor for tarantool image parameters | ||
* | ||
* @param sdkVersion version of tarantool sdk which will be downloaded from specified in env variables URI, | ||
* for example: tarantool-enterprise-bundle-2.8.3-21-g7d35cd2be-r470 | ||
* @param dockerfile dockerfile for building custom tarantool image | ||
*/ | ||
public TarantoolImageParams(String sdkVersion, File dockerfile) { | ||
wey1and marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.sdkVersion = sdkVersion; | ||
this.dockerfile = dockerfile; | ||
} | ||
|
||
/** | ||
* Getter for sdk version | ||
* | ||
* @return sdk version | ||
*/ | ||
public String getSdkVersion() { | ||
return sdkVersion; | ||
} | ||
|
||
/** | ||
* Getter for dockerfile | ||
* | ||
* @return dockerfile | ||
*/ | ||
public File getDockerfile() { | ||
return dockerfile; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
FROM centos:7 | ||
|
||
ARG TARANTOOL_WORKDIR="/app" | ||
ARG TARANTOOL_RUNDIR="/tmp/run" | ||
ARG TARANTOOL_DATADIR="/tmp/data" | ||
ARG SDK_TGT_DIR="/sdk" | ||
ARG URI="" | ||
ARG SDK_VERSION="" | ||
ARG SDK_TGZ=$SDK_VERSION.tar.gz | ||
|
||
ENV URI=$URI | ||
ENV SDK_VERSION=$SDK_VERSION | ||
ENV SDK_TGT_DIR=$SDK_TGT_DIR | ||
ENV TARANTOOL_WORKDIR=$TARANTOOL_WORKDIR | ||
ENV TARANTOOL_RUNDIR=$TARANTOOL_RUNDIR | ||
ENV TARANTOOL_DATADIR=$TARANTOOL_DATADIR | ||
|
||
RUN curl https://curl.se/ca/cacert.pem -o /etc/pki/tls/certs/ca-bundle.crt && \ | ||
yum -y install wget && \ | ||
wget $URI/$SDK_TGZ && \ | ||
mkdir ./tmp_sdk && tar -xf $SDK_TGZ -C ./tmp_sdk && \ | ||
mv ./tmp_sdk/tarantool-enterprise $SDK_TGT_DIR && rm $SDK_TGZ && \ | ||
cp $SDK_TGT_DIR/tarantool /usr/bin/tarantool | ||
|
||
WORKDIR $TARANTOOL_WORKDIR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/test/java/org/testcontainers/containers/TarantoolSdkContainerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.testcontainers.containers; | ||
|
||
|
||
import io.tarantool.driver.TarantoolVersion; | ||
import io.tarantool.driver.api.TarantoolClient; | ||
import io.tarantool.driver.api.TarantoolClientFactory; | ||
import io.tarantool.driver.api.TarantoolResult; | ||
import io.tarantool.driver.api.tuple.TarantoolTuple; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
import java.net.URISyntaxException; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Oleg Kuznetsov | ||
*/ | ||
public class TarantoolSdkContainerTest { | ||
wey1and marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Test | ||
void test_should_createTarantoolContainerFromSdk() { | ||
try (final TarantoolContainer tarantoolContainer = new TarantoolContainer( | ||
new TarantoolImageParams("tarantool-enterprise-bundle-2.8.3-21-g7d35cd2be-r470") | ||
)) { | ||
tarantoolContainer.start(); | ||
|
||
final TarantoolClient<TarantoolTuple, TarantoolResult<TarantoolTuple>> client = | ||
TarantoolClientFactory.createClient() | ||
.withCredentials("api_user", "secret") | ||
.withAddress(tarantoolContainer.getHost(), tarantoolContainer.getMappedPort(3301)) | ||
.build(); | ||
|
||
final List<?> result = client.eval("return 'test'").join(); | ||
final TarantoolVersion version = client.getVersion(); | ||
|
||
Assertions.assertEquals("test", result.get(0)); | ||
Assertions.assertTrue(version.toString().startsWith("Tarantool 2.8.3 (Binary)")); | ||
} | ||
} | ||
|
||
@Test | ||
void test_should_createTarantoolContainerFromSdk_ifDockerfileSpecified() throws URISyntaxException { | ||
final File dockerfile = new File( | ||
TarantoolSdkContainerTest.class.getClassLoader().getResource("testsdk/Dockerfile").toURI() | ||
); | ||
|
||
try (final TarantoolContainer tarantoolContainer = new TarantoolContainer( | ||
new TarantoolImageParams("testsdk", dockerfile)) | ||
.withDirectoryBinding("testsdk")) { | ||
|
||
tarantoolContainer.start(); | ||
|
||
final TarantoolClient<TarantoolTuple, TarantoolResult<TarantoolTuple>> client = | ||
TarantoolClientFactory.createClient() | ||
.withCredentials("api_user", "secret") | ||
.withAddress(tarantoolContainer.getHost(), tarantoolContainer.getMappedPort(3301)) | ||
.build(); | ||
|
||
final List<?> result = client.eval("return 'test'").join(); | ||
final TarantoolVersion version = client.getVersion(); | ||
|
||
Assertions.assertEquals("test", result.get(0)); | ||
Assertions.assertTrue(version.toString().startsWith("Tarantool 2.7.3 (Binary)")); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
FROM centos:7 | ||
|
||
ARG TARANTOOL_WORKDIR="/app" | ||
ARG TARANTOOL_RUNDIR="/tmp/run" | ||
ARG TARANTOOL_DATADIR="/tmp/data" | ||
ARG SDK_TGT_DIR="/sdk" | ||
ARG URI="" | ||
ARG SDK_TGZ="tarantool-enterprise-bundle-2.7.3-0-gdddf926c3-r443.tar.gz" | ||
|
||
ENV URI=$URI | ||
ENV SDK_VERSION="tarantool-enterprise-bundle-2.7.3-0-gdddf926c3-r443" | ||
ENV SDK_TGT_DIR=$SDK_TGT_DIR | ||
ENV TARANTOOL_WORKDIR=$TARANTOOL_WORKDIR | ||
ENV TARANTOOL_RUNDIR=$TARANTOOL_RUNDIR | ||
ENV TARANTOOL_DATADIR=$TARANTOOL_DATADIR | ||
|
||
RUN curl https://curl.se/ca/cacert.pem -o /etc/pki/tls/certs/ca-bundle.crt && \ | ||
yum -y install wget && \ | ||
wget $URI/$SDK_TGZ && \ | ||
mkdir ./tmp_sdk && tar -xf $SDK_TGZ -C ./tmp_sdk && \ | ||
mv ./tmp_sdk/tarantool-enterprise $SDK_TGT_DIR && rm $SDK_TGZ && \ | ||
cp $SDK_TGT_DIR/tarantool /usr/bin/tarantool | ||
|
||
WORKDIR $TARANTOOL_WORKDIR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
box.cfg { | ||
listen = 3301, | ||
memtx_memory = 128 * 1024 * 1024, -- 128 Mb | ||
-- log = 'file:/tmp/tarantool.log', | ||
log_level = 6, | ||
} | ||
-- API user will be able to login with this password | ||
box.schema.user.create('api_user', { password = 'secret', if_not_exists = true }) | ||
-- API user will be able to create spaces, add or remove data, execute functions | ||
box.schema.user.grant('api_user', 'read,write,execute', 'universe', nil, { if_not_exists = true }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.