Skip to content

Commit 3d414a8

Browse files
committed
[JENKINS-75679] Reproduced FilePathPickle usage in test
1 parent d0fdd5b commit 3d414a8

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed

src/test/java/org/jenkinsci/plugins/docker/workflow/RegistryEndpointStepTest.java

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,28 +59,38 @@
5959
import org.junit.Rule;
6060
import org.junit.Test;
6161
import org.jvnet.hudson.test.Issue;
62-
import org.jvnet.hudson.test.JenkinsRule;
6362
import org.jvnet.hudson.test.MockAuthorizationStrategy;
6463
import org.jvnet.hudson.test.MockQueueItemAuthenticator;
6564
import org.jvnet.hudson.test.TestExtension;
6665
import org.kohsuke.stapler.DataBoundConstructor;
6766

6867
import java.io.Serializable;
68+
import java.nio.charset.StandardCharsets;
69+
import java.nio.file.Files;
6970
import java.util.Set;
7071
import java.util.logging.Level;
72+
import org.apache.commons.text.StringEscapeUtils;
73+
import static org.hamcrest.MatcherAssert.assertThat;
74+
import static org.hamcrest.Matchers.containsString;
75+
import static org.hamcrest.Matchers.not;
7176
import org.jenkinsci.plugins.structs.describable.DescribableModel;
77+
import org.jenkinsci.plugins.workflow.support.pickles.FilePathPickle;
78+
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
79+
import org.junit.ClassRule;
7280
import org.jvnet.hudson.test.BuildWatcher;
81+
import org.jvnet.hudson.test.JenkinsSessionRule;
7382
import org.jvnet.hudson.test.LoggerRule;
7483

7584
public class RegistryEndpointStepTest {
7685

77-
@Rule public JenkinsRule r = new JenkinsRule();
86+
@Rule public final JenkinsSessionRule rr = new JenkinsSessionRule();
7887
@Rule public LoggerRule logging = new LoggerRule();
79-
@Rule public BuildWatcher bw = new BuildWatcher();
88+
@ClassRule public static BuildWatcher bw = new BuildWatcher();
8089

8190
@Issue("JENKINS-51395")
82-
@Test public void configRoundTrip() throws Exception {
91+
@Test public void configRoundTrip() throws Throwable {
8392
logging.record(DescribableModel.class, Level.FINE);
93+
rr.then(r -> {
8494
{ // Recommended syntax.
8595
SnippetizerTester st = new SnippetizerTester(r);
8696
RegistryEndpointStep step = new RegistryEndpointStep(new DockerRegistryEndpoint("https://myreg/", null));
@@ -116,12 +126,14 @@ public class RegistryEndpointStepTest {
116126
assertEquals("registryCreds", registry.getCredentialsId());
117127
// TODO check toolName
118128
}
129+
});
119130
}
120131

121132
@Test
122-
public void stepExecutionWithCredentials() throws Exception {
133+
public void stepExecutionWithCredentials() throws Throwable {
123134
assumeNotWindows();
124135

136+
rr.then(r -> {
125137
IdCredentials registryCredentials = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "registryCreds", null, "me", "s3cr3t");
126138
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), registryCredentials);
127139

@@ -137,12 +149,14 @@ public void stepExecutionWithCredentials() throws Exception {
137149
r.assertBuildStatusSuccess(r.waitForCompletion(b));
138150
r.assertLogContains("docker login -u me -p ******** https://my-reg:1234", b);
139151
r.assertLogNotContains("s3cr3t", b);
152+
});
140153
}
141154

142155
@Test
143-
public void stepExecutionWithCredentialsAndQueueItemAuthenticator() throws Exception {
156+
public void stepExecutionWithCredentialsAndQueueItemAuthenticator() throws Throwable {
144157
assumeNotWindows();
145158

159+
rr.then(r -> {
146160
r.getInstance().setSecurityRealm(r.createDummySecurityRealm());
147161
MockAuthorizationStrategy auth = new MockAuthorizationStrategy()
148162
.grant(Jenkins.READ).everywhere().to("alice", "bob")
@@ -181,6 +195,39 @@ public void stepExecutionWithCredentialsAndQueueItemAuthenticator() throws Excep
181195

182196
// Bob does not have Credentials.USE_ITEM permission and should not be able to use the credential.
183197
r.assertBuildStatus(Result.FAILURE, p2.scheduleBuild2(0));
198+
});
199+
}
200+
201+
@Issue("JENKINS-75679")
202+
@Test public void noFilePathPickle() throws Throwable {
203+
assumeNotWindows();
204+
rr.then(r -> {
205+
var registryCredentials = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "registryCreds", null, "me", "s3cr3t");
206+
CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), registryCredentials);
207+
var p = r.createProject(WorkflowJob.class, "p");
208+
p.setDefinition(new CpsFlowDefinition(
209+
"""
210+
node {
211+
mockDockerLogin {
212+
withDockerRegistry(url: 'https://my-reg:1234', credentialsId: 'registryCreds') {
213+
semaphore 'wait'
214+
}
215+
}
216+
}
217+
""", true));
218+
var b = p.scheduleBuild2(0).waitForStart();
219+
SemaphoreStep.waitForStart("wait/1", b);
220+
});
221+
@SuppressWarnings("deprecation")
222+
var verboten = FilePathPickle.class.getName();
223+
assertThat(StringEscapeUtils.escapeJava(Files.readString(rr.getHome().toPath().resolve("jobs/p/builds/1/program.dat"), StandardCharsets.ISO_8859_1)), not(containsString(verboten)));
224+
rr.then(r -> {
225+
var b = r.jenkins.getItemByFullName("p", WorkflowJob.class).getBuildByNumber(1);
226+
SemaphoreStep.success("wait/1", null);
227+
r.assertBuildStatusSuccess(r.waitForCompletion(b));
228+
r.assertLogContains("docker login -u me -p ******** https://my-reg:1234", b);
229+
r.assertLogNotContains("s3cr3t", b);
230+
});
184231
}
185232

186233
public static class MockLauncherStep extends Step {

0 commit comments

Comments
 (0)