Skip to content

Commit c687b2e

Browse files
Provide IT setup
1 parent de85e6b commit c687b2e

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

jjava/pom.xml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@
3131
<groupId>org.apache.maven</groupId>
3232
<artifactId>maven-model-builder</artifactId>
3333
</dependency>
34+
<dependency>
35+
<groupId>org.junit.jupiter</groupId>
36+
<artifactId>junit-jupiter-api</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.testcontainers</groupId>
41+
<artifactId>testcontainers</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.slf4j</groupId>
46+
<artifactId>slf4j-simple</artifactId>
47+
<scope>test</scope>
48+
</dependency>
3449
</dependencies>
3550

3651
<build>
@@ -177,4 +192,4 @@
177192
</plugins>
178193
</build>
179194

180-
</project>
195+
</project>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.dflib.jjava.jupyter.kernel.testcontainers;
2+
3+
import org.testcontainers.containers.BindMode;
4+
import org.testcontainers.containers.Container;
5+
import org.testcontainers.containers.GenericContainer;
6+
import org.testcontainers.containers.wait.strategy.Wait;
7+
8+
import java.io.IOException;
9+
import java.time.Duration;
10+
11+
public abstract class KernelIT {
12+
13+
private static final String BASE_IMAGE = String.format("eclipse-temurin:%s", Runtime.version().version().get(0));
14+
private static final String FS_KERNELSPEC = "../kernelspec/java";
15+
private static final String CONTAINER_KERNELSPEC = "/usr/share/jupyter/kernels/java";
16+
private static final String WORKING_DIRECTORY = "/test";
17+
18+
protected static final GenericContainer<?> container;
19+
20+
static {
21+
container = new GenericContainer<>(BASE_IMAGE)
22+
.withWorkingDirectory(WORKING_DIRECTORY)
23+
.withCommand("bash", "-c", getStartupCommand())
24+
.withFileSystemBind(FS_KERNELSPEC, CONTAINER_KERNELSPEC, BindMode.READ_ONLY)
25+
.waitingFor(Wait.forSuccessfulCommand(getSuccessfulCommand()))
26+
.withStartupTimeout(Duration.ofMinutes(3));
27+
container.start();
28+
}
29+
30+
private static String getStartupCommand() {
31+
return String.join(" && ",
32+
"apt-get update",
33+
"apt-get install --no-install-recommends -y python3 python3-pip python3-venv",
34+
"python3 -m venv ./venv",
35+
venvCommand("pip install jupyter-console --progress-bar off"),
36+
"tail -f /dev/null"
37+
);
38+
}
39+
40+
private static String getSuccessfulCommand() {
41+
return venvCommand("jupyter kernelspec list")
42+
+ "| grep ' java ' &&"
43+
+ venvCommand("jupyter console --version");
44+
}
45+
46+
private static String venvCommand(String command) {
47+
return WORKING_DIRECTORY + "/venv/bin/" + command;
48+
}
49+
50+
protected Container.ExecResult executeInKernel(String snippet) throws IOException, InterruptedException {
51+
return container.execInContainer(
52+
"bash",
53+
"-c",
54+
"echo '" + snippet + "' | " + venvCommand("jupyter console --kernel=java --simple-prompt")
55+
);
56+
}
57+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.dflib.jjava.jupyter.kernel.testcontainers;
2+
3+
import org.hamcrest.CoreMatchers;
4+
import org.hamcrest.MatcherAssert;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.Test;
7+
import org.testcontainers.containers.Container;
8+
9+
class KernelStartupTestIT extends KernelIT {
10+
11+
@Test
12+
void startUp() throws Exception {
13+
Container.ExecResult snippetResult = executeInKernel("System.out.println(\"Hello world!\");");
14+
15+
Assertions.assertEquals(0, snippetResult.getExitCode(), snippetResult.getStderr());
16+
MatcherAssert.assertThat(snippetResult.getStdout(), CoreMatchers.containsString("Hello world!"));
17+
}
18+
}

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
<junit5.version>5.11.2</junit5.version>
5353
<hamcrest.version>1.3</hamcrest.version>
5454
<jimfs.version>1.3.0</jimfs.version>
55+
<testcontainers.version>1.20.2</testcontainers.version>
56+
<slf4j.version>1.7.36</slf4j.version>
5557
</properties>
5658

5759
<url>https://github.com/dflib/jjava</url>
@@ -119,6 +121,16 @@
119121
<artifactId>jimfs</artifactId>
120122
<version>${jimfs.version}</version>
121123
</dependency>
124+
<dependency>
125+
<groupId>org.testcontainers</groupId>
126+
<artifactId>testcontainers</artifactId>
127+
<version>${testcontainers.version}</version>
128+
</dependency>
129+
<dependency>
130+
<groupId>org.slf4j</groupId>
131+
<artifactId>slf4j-simple</artifactId>
132+
<version>${slf4j.version}</version>
133+
</dependency>
122134
</dependencies>
123135
</dependencyManagement>
124136

0 commit comments

Comments
 (0)