Skip to content
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

Add support for tarantool version in ENV #102

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
## [1.1.1] - 2023-12-13

- Change private to protected in TarantoolCartridgeContainer
- Add support for the `TARANTOOL_VERSION` environment variable to specify the version in the image name
`tarantool/tarantool:<TARANTOOL_VERSION>-centos7 when calling the constructor without arguments
([#51](https://github.com/tarantool/testcontainers-java-tarantool/pull/102))

## [1.1.0] - 2023-12-12

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public class TarantoolContainer extends GenericContainer<TarantoolContainer>
implements TarantoolContainerOperations<TarantoolContainer> {

public static final String TARANTOOL_IMAGE = "tarantool/tarantool";
public static final String DEFAULT_IMAGE_VERSION = "2.x-centos7";
public static final String DEFAULT_TARANTOOL_BASE_IMAGE =
String.format("%s:%s", TARANTOOL_IMAGE, DEFAULT_IMAGE_VERSION);
public static final String DEFAULT_IMAGE_VERSION = "2.10.5";
public static final String DEFAULT_TARANTOOL_BASE_IMAGE = String.format("%s:%s-centos7", TARANTOOL_IMAGE, DEFAULT_IMAGE_VERSION);


private static final String DEFAULT_HOST = "localhost";
private static final int DEFAULT_PORT = 3301;
Expand Down Expand Up @@ -50,7 +50,8 @@ public class TarantoolContainer extends GenericContainer<TarantoolContainer>
* Constructor for {@link TarantoolContainer}
*/
public TarantoolContainer() {
this(String.format("%s:%s", TARANTOOL_IMAGE, DEFAULT_IMAGE_VERSION));
this(DEFAULT_TARANTOOL_BASE_IMAGE);
setImageNameFromEnv();
}

/**
Expand Down Expand Up @@ -381,4 +382,11 @@ public Container.ExecResult executeCommand(String command) throws Exception {
public <T> T executeCommandDecoded(String command) throws Exception {
return clientHelper.executeCommandDecoded(command, this.sslContext);
}

private void setImageNameFromEnv() {
String version = System.getenv("TARANTOOL_VERSION");
if (version != null && !version.trim().isEmpty()) {
setDockerImageName(String.format("%s:%s-centos7", TARANTOOL_IMAGE, version));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
package org.testcontainers.containers;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;

/**
* @author Alexey Kuzin
* @author Ivan Dneprov
*/
class TarantoolContainerTest {

private static final String ENV_TARANTOOL_VERSION = "TARANTOOL_VERSION";

private void addEnv(String key, String value) throws NoSuchFieldException, IllegalAccessException {
Class<?> classOfMap = System.getenv().getClass();
Field field = classOfMap.getDeclaredField("m");
field.setAccessible(true);
Map<String, String> writeableEnvironmentVariables = (Map<String, String>) field.get(System.getenv());
writeableEnvironmentVariables.put(key, value);
field.setAccessible(false);
}

private void removeEnv(String key, String value) throws NoSuchFieldException, IllegalAccessException {
Class<?> classOfMap = System.getenv().getClass();
Field field = classOfMap.getDeclaredField("m");
field.setAccessible(true);
Map<String, String> writeableEnvironmentVariables = (Map<String, String>) field.get(System.getenv());
writeableEnvironmentVariables.remove(key);
field.setAccessible(false);
}

@Test
public void testExecuteScript() throws Exception {
try (TarantoolContainer container = new TarantoolContainer()) {
Expand Down Expand Up @@ -47,4 +70,55 @@ public void testContainerWithParameters() throws Exception {
assertEquals(result.get(0), 5);
}
}

@Test
public void testContainerWithTrueVersion() throws Exception {
final String version = "2.11.0";
addEnv(ENV_TARANTOOL_VERSION, version);

List<String> result;
try (TarantoolContainer container = new TarantoolContainer()) {
container.start();
ArtDu marked this conversation as resolved.
Show resolved Hide resolved
result = container.executeCommandDecoded("return _TARANTOOL");
}

removeEnv(ENV_TARANTOOL_VERSION, version);
assertEquals(1, result.size());
assertTrue(result.get(0).startsWith(version));
}

@Test
public void testContainerWithDefaultVersionVersion() throws Exception {

List<String> result;
try (TarantoolContainer container = new TarantoolContainer()) {
container.start();
result = container.executeCommandDecoded("return _TARANTOOL");
}

assertEquals(1, result.size());
assertTrue(result.get(0).startsWith(TarantoolContainer.DEFAULT_IMAGE_VERSION));
}

@Test
public void testContainerWithEmptyVersion() throws Exception {
final String version = " ";
addEnv(ENV_TARANTOOL_VERSION, version);
try (TarantoolContainer container = new TarantoolContainer()) {
container.start();
}
removeEnv(ENV_TARANTOOL_VERSION, version);
}

@Test
public void testContainerWithWrongVersion() throws Exception {
final String version = "wrong_version";
addEnv(ENV_TARANTOOL_VERSION, version);
try (TarantoolContainer container = new TarantoolContainer()) {
container.start();
}catch (Exception exc) {
assertEquals(ContainerFetchException.class, exc.getClass());
}
removeEnv(ENV_TARANTOOL_VERSION, version);
}
}
Loading