5858import java .io .IOException ;
5959import java .io .InputStream ;
6060import java .net .URL ;
61- import java .nio .charset .Charset ;
6261import java .nio .charset .StandardCharsets ;
6362import java .util .Arrays ;
6463import java .util .Collections ;
7473import jenkins .util .VirtualFile ;
7574import org .apache .commons .io .IOUtils ;
7675import 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 ;
8076import org .junit .BeforeClass ;
8177import org .junit .Rule ;
8278import org .junit .Test ;
8379import org .jvnet .hudson .test .JenkinsRule ;
8480import 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}
0 commit comments