1- /*
1+ /*d
22 * The MIT License
33 *
44 * Copyright 2018 CloudBees, Inc.
3535import static org .junit .Assert .assertTrue ;
3636import static org .junit .Assert .fail ;
3737
38- import com .cloudbees .plugins .credentials .CredentialsProvider ;
39- import com .cloudbees .plugins .credentials .CredentialsScope ;
40- import com .cloudbees .plugins .credentials .common .StandardUsernameCredentials ;
41- import com .cloudbees .plugins .credentials .domains .Domain ;
42- import com .cloudbees .plugins .credentials .impl .UsernamePasswordCredentialsImpl ;
4338import hudson .AbortException ;
4439import hudson .EnvVars ;
4540import hudson .ExtensionList ;
5045import hudson .model .FreeStyleBuild ;
5146import hudson .model .FreeStyleProject ;
5247import hudson .model .TaskListener ;
53- import hudson .plugins .sshslaves .SSHLauncher ;
5448import hudson .remoting .Callable ;
5549import hudson .slaves .DumbSlave ;
5650import hudson .tasks .ArtifactArchiver ;
6761import jenkins .model .ArtifactManager ;
6862import jenkins .model .ArtifactManagerConfiguration ;
6963import jenkins .model .ArtifactManagerFactory ;
70- import jenkins .model .Jenkins ;
7164import jenkins .model .StandardArtifactManager ;
7265import jenkins .security .MasterToSlaveCallable ;
7366import jenkins .util .VirtualFile ;
7467import org .apache .commons .io .IOUtils ;
7568import org .jenkinsci .plugins .workflow .flow .StashManager ;
76- import org .junit .BeforeClass ;
7769import org .junit .Rule ;
7870import org .junit .Test ;
7971import org .jvnet .hudson .test .JenkinsRule ;
8072import org .jvnet .hudson .test .LoggerRule ;
81- import org .testcontainers .DockerClientFactory ;
82- import org .testcontainers .containers .GenericContainer ;
83- import org .testcontainers .images .builder .ImageFromDockerfile ;
73+ import test .ssh_agent .OutboundAgent ;
8474
8575/**
8676 * {@link #artifactArchiveAndDelete} and variants allow an implementation of {@link ArtifactManager} plus {@link VirtualFile} to be run through a standard gantlet of tests.
@@ -90,23 +80,6 @@ public class ArtifactManagerTest {
9080 @ Rule public JenkinsRule r = new JenkinsRule ();
9181 @ Rule public LoggerRule logging = new LoggerRule ();
9282
93- private static GenericContainer <?> container ;
94-
95- @ BeforeClass public static void doPrepareImage () {
96- container = prepareImage ();
97- }
98-
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 );
104- } else {
105- System .err .println ("No Docker support; falling back to running tests against an agent in a process on the same machine." );
106- return null ;
107- }
108- }
109-
11083 /**
11184 * Creates an agent, in a Docker container when possible, calls {@link #setUpWorkspace}, then runs some tests.
11285 */
@@ -115,18 +88,10 @@ private static void wrapInContainer(@NonNull JenkinsRule r, @CheckForNull Artifa
11588 if (factory != null ) {
11689 ArtifactManagerConfiguration .get ().getArtifactManagerFactories ().add (factory );
11790 }
118- try {
119- DumbSlave agent ;
120- if (container != null ) {
121- container .start ();
122- StandardUsernameCredentials creds = new UsernamePasswordCredentialsImpl (CredentialsScope .SYSTEM , "test" , "desc" , "test" , "test" );
123- CredentialsProvider .lookupStores (Jenkins .get ()).iterator ().next ().addCredentials (Domain .global (), creds );
124- agent = new DumbSlave ("test-agent" , "/home/test/slave" , new SSHLauncher (container .getHost (), container .getMappedPort (22 ), "test" ));
125- Jenkins .get ().addNode (agent );
126- r .waitOnline (agent );
127- } else {
128- agent = r .createOnlineSlave ();
129- }
91+ try (var outboundAgent = new OutboundAgent ()) {
92+ OutboundAgent .createAgent (r , "remote" , outboundAgent .start ());
93+ var agent = (DumbSlave ) r .jenkins .getNode ("remote" );
94+ r .waitOnline (agent );
13095 FreeStyleProject p = r .createFreeStyleProject ();
13196 p .setAssignedNode (agent );
13297 FilePath ws = agent .getWorkspaceFor (p );
@@ -136,10 +101,6 @@ private static void wrapInContainer(@NonNull JenkinsRule r, @CheckForNull Artifa
136101 p .getPublishersList ().add (aa );
137102 FreeStyleBuild b = r .buildAndAssertSuccess (p );
138103 f .apply (agent , p , b , ws );
139- } finally {
140- if (container != null ) {
141- container .stop ();
142- }
143104 }
144105 }
145106
0 commit comments