Skip to content

Commit f606866

Browse files
Replace docker-fixtures with testcontainers
1 parent f682ae8 commit f606866

File tree

3 files changed

+47
-43
lines changed

3 files changed

+47
-43
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@
123123
<scope>test</scope>
124124
</dependency>
125125
<dependency>
126-
<groupId>org.jenkins-ci.test</groupId>
127-
<artifactId>docker-fixtures</artifactId>
128-
<version>200.v22a_e8766731c</version>
126+
<groupId>org.testcontainers</groupId>
127+
<artifactId>testcontainers</artifactId>
128+
<version>1.21.3</version>
129129
<scope>test</scope>
130130
</dependency>
131131
<dependency>

src/test/java/org/jenkinsci/plugins/workflow/ArtifactManagerTest.java

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
import java.io.IOException;
5959
import java.io.InputStream;
6060
import java.net.URL;
61-
import java.nio.charset.Charset;
6261
import java.nio.charset.StandardCharsets;
6362
import java.util.Arrays;
6463
import java.util.Collections;
@@ -74,14 +73,14 @@
7473
import jenkins.util.VirtualFile;
7574
import org.apache.commons.io.IOUtils;
7675
import org.jenkinsci.plugins.workflow.flow.StashManager;
77-
import org.jenkinsci.test.acceptance.docker.Docker;
78-
import org.jenkinsci.test.acceptance.docker.DockerImage;
79-
import org.jenkinsci.test.acceptance.docker.fixtures.JavaContainer;
8076
import org.junit.BeforeClass;
8177
import org.junit.Rule;
8278
import org.junit.Test;
8379
import org.jvnet.hudson.test.JenkinsRule;
8480
import org.jvnet.hudson.test.LoggerRule;
81+
import org.testcontainers.DockerClientFactory;
82+
import org.testcontainers.containers.GenericContainer;
83+
import org.testcontainers.images.builder.ImageFromDockerfile;
8584

8685
/**
8786
* {@link #artifactArchiveAndDelete} and variants allow an implementation of {@link ArtifactManager} plus {@link VirtualFile} to be run through a standard gantlet of tests.
@@ -90,21 +89,18 @@ public class ArtifactManagerTest {
9089

9190
@Rule public JenkinsRule r = new JenkinsRule();
9291
@Rule public LoggerRule logging = new LoggerRule();
93-
94-
private static DockerImage image;
95-
96-
@BeforeClass public static void doPrepareImage() throws Exception {
97-
image = prepareImage();
92+
93+
private static GenericContainer<?> container;
94+
95+
@BeforeClass public static void doPrepareImage() {
96+
container = prepareImage();
9897
}
9998

100-
/**
101-
* @deprecated Not actually used externally.
102-
*/
103-
@Deprecated
104-
public static @CheckForNull DockerImage prepareImage() throws Exception {
105-
Docker docker = new Docker();
106-
if (!Functions.isWindows() && docker.isAvailable()) { // TODO: Windows agents on ci.jenkins.io have Docker, but cannot build the image.
107-
return docker.build(JavaContainer.class);
99+
public static @CheckForNull GenericContainer<?> prepareImage() {
100+
if (!Functions.isWindows() && DockerClientFactory.instance().isDockerAvailable()) { // TODO: Windows agents on ci.jenkins.io have Docker, but cannot build the image.
101+
return new GenericContainer<>(new ImageFromDockerfile("java17-ssh", false)
102+
.withFileFromClasspath("Dockerfile", ArtifactManagerTest.class.getName().replace('.', '/') + "/Dockerfile"))
103+
.withExposedPorts(22);
108104
} else {
109105
System.err.println("No Docker support; falling back to running tests against an agent in a process on the same machine.");
110106
return null;
@@ -119,14 +115,13 @@ private static void wrapInContainer(@NonNull JenkinsRule r, @CheckForNull Artifa
119115
if (factory != null) {
120116
ArtifactManagerConfiguration.get().getArtifactManagerFactories().add(factory);
121117
}
122-
JavaContainer runningContainer = null;
123118
try {
124119
DumbSlave agent;
125-
if (image != null) {
126-
runningContainer = image.start(JavaContainer.class).start();
120+
if (container != null) {
121+
container.start();
127122
StandardUsernameCredentials creds = new UsernamePasswordCredentialsImpl(CredentialsScope.SYSTEM, "test", "desc", "test", "test");
128123
CredentialsProvider.lookupStores(Jenkins.get()).iterator().next().addCredentials(Domain.global(), creds);
129-
agent = new DumbSlave("test-agent", "/home/test/slave", new SSHLauncher(runningContainer.ipBound(22), runningContainer.port(22), "test"));
124+
agent = new DumbSlave("test-agent", "/home/test/slave", new SSHLauncher(container.getHost(), container.getMappedPort(22), "test"));
130125
Jenkins.get().addNode(agent);
131126
r.waitOnline(agent);
132127
} else {
@@ -142,8 +137,8 @@ private static void wrapInContainer(@NonNull JenkinsRule r, @CheckForNull Artifa
142137
FreeStyleBuild b = r.buildAndAssertSuccess(p);
143138
f.apply(agent, p, b, ws);
144139
} finally {
145-
if (runningContainer != null) {
146-
runningContainer.close();
140+
if (container != null) {
141+
container.stop();
147142
}
148143
}
149144
}
@@ -161,10 +156,6 @@ public static void artifactArchive(@NonNull JenkinsRule r, @CheckForNull Artifac
161156
assertTrue(b.getArtifactManager().root().child("file").isFile());
162157
});
163158
}
164-
@Deprecated
165-
public static void artifactArchive(@NonNull JenkinsRule r, @CheckForNull ArtifactManagerFactory factory, boolean weirdCharacters, @CheckForNull DockerImage image) throws Exception {
166-
artifactArchive(r, factory, weirdCharacters);
167-
}
168159

169160
/**
170161
* Test artifact archiving in a plain manager.
@@ -180,10 +171,6 @@ public static void artifactArchiveAndDelete(@NonNull JenkinsRule r, @CheckForNul
180171
assertFalse(b.getArtifactManager().delete());
181172
});
182173
}
183-
@Deprecated
184-
public static void artifactArchiveAndDelete(@NonNull JenkinsRule r, @CheckForNull ArtifactManagerFactory factory, boolean weirdCharacters, @CheckForNull DockerImage image) throws Exception {
185-
artifactArchiveAndDelete(r, factory, weirdCharacters);
186-
}
187174

188175
/**
189176
* Test stashing and unstashing with a {@link StashManager.StashAwareArtifactManager} that does <em>not</em> honor deletion requests.
@@ -197,10 +184,6 @@ public static void artifactStash(@NonNull JenkinsRule r, @CheckForNull ArtifactM
197184
assertTrue(ws.child("file").exists());
198185
}));
199186
}
200-
@Deprecated
201-
public static void artifactStash(@NonNull JenkinsRule r, @CheckForNull ArtifactManagerFactory factory, boolean weirdCharacters, @CheckForNull DockerImage image) throws Exception {
202-
artifactStash(r, factory, weirdCharacters);
203-
}
204187

205188
/**
206189
* Test stashing and unstashing with a {@link StashManager.StashAwareArtifactManager} with standard behavior.
@@ -218,10 +201,6 @@ public static void artifactStashAndDelete(@NonNull JenkinsRule r, @CheckForNull
218201
assertFalse(ws.child("file").exists());
219202
}));
220203
}
221-
@Deprecated
222-
public static void artifactStashAndDelete(@NonNull JenkinsRule r, @CheckForNull ArtifactManagerFactory factory, boolean weirdCharacters, @CheckForNull DockerImage image) throws Exception {
223-
artifactStashAndDelete(r, factory, weirdCharacters);
224-
}
225204

226205
/**
227206
* Creates a variety of files in a directory structure designed to exercise interesting aspects of {@link VirtualFile}.
@@ -471,7 +450,7 @@ private static void assertNonexistent(VirtualFile f) throws IOException {
471450
@Test public void standard() throws Exception {
472451
logging.record(StandardArtifactManager.class, Level.FINE);
473452
// Who knows about weird characters on NTFS; also case-sensitivity could confuse things
474-
artifactArchiveAndDelete(r, null, !Functions.isWindows(), image);
453+
artifactArchiveAndDelete(r, null, !Functions.isWindows());
475454
}
476455

477456
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
FROM ubuntu:noble
2+
3+
# install requirements
4+
RUN apt-get update -y && \
5+
apt-get install --no-install-recommends -y \
6+
openjdk-17-jdk-headless \
7+
openssh-server \
8+
locales
9+
10+
RUN mkdir -p /var/run/sshd
11+
12+
# create a test user
13+
RUN useradd test -d /home/test && \
14+
mkdir -p /home/test/.ssh && \
15+
chown -R test:test /home/test && \
16+
echo "test:test" | chpasswd
17+
18+
# https://stackoverflow.com/a/38553499/12916
19+
RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
20+
dpkg-reconfigure --frontend=noninteractive locales && \
21+
update-locale LANG=en_US.UTF-8
22+
ENV LANG en_US.UTF-8
23+
24+
# run SSHD in the foreground with error messages to stderr
25+
ENTRYPOINT ["/usr/sbin/sshd", "-D", "-e"]

0 commit comments

Comments
 (0)