Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.util.Secret;
Expand All @@ -53,8 +54,8 @@ public class SSHUserPrivateKeyBinding extends MultiBinding<SSHUserPrivateKey> {
}

@DataBoundSetter
public void setUsernameVariable(@Nonnull final String usernameVariable) {
this.usernameVariable = usernameVariable;
public void setUsernameVariable(@CheckForNull String usernameVariable) {
this.usernameVariable = Util.fixEmptyAndTrim(usernameVariable);
}

@CheckForNull
Expand All @@ -63,8 +64,8 @@ public String getUsernameVariable() {
}

@DataBoundSetter
public void setPassphraseVariable(@Nonnull final String passphraseVariable) {
this.passphraseVariable = passphraseVariable;
public void setPassphraseVariable(@CheckForNull String passphraseVariable) {
this.passphraseVariable = Util.fixEmptyAndTrim(passphraseVariable);
}

@CheckForNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public void setUp() throws IOException {
}
}

// TODO configRoundtrip to test form, null hygiene on @DataBoundSetter

@Test
public void basics() throws Exception {
String alias = "androiddebugkey";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import hudson.util.Secret;
import org.jenkinsci.plugins.credentialsbinding.MultiBinding;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.cps.SnippetizerTester;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.steps.StepConfigTester;
Expand All @@ -51,7 +52,7 @@
import org.junit.ClassRule;
import org.jvnet.hudson.test.BuildWatcher;

public class SSHUserPrivateKeyTest {
public class SSHUserPrivateKeyBindingTest {

@Rule public RestartableJenkinsRule story = new RestartableJenkinsRule();
@ClassRule public static BuildWatcher bw = new BuildWatcher();
Expand Down Expand Up @@ -119,21 +120,22 @@ public CredentialsScope getScope() {
}

@Test public void configRoundTrip() throws Exception {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
SSHUserPrivateKey c = new DummyPrivateKey("creds", "bob", "secret", "the-key");
CredentialsProvider.lookupStores(story.j.jenkins).iterator().next().addCredentials(Domain.global(), c);
SSHUserPrivateKeyBinding binding = new SSHUserPrivateKeyBinding("keyFile", "creds");
binding.setPassphraseVariable("passphrase");
binding.setUsernameVariable("user");
BindingStep s = new StepConfigTester(story.j).configRoundTrip(new BindingStep(
Collections.<MultiBinding>singletonList(binding)));
story.j.assertEqualDataBoundBeans(s.getBindings(), Collections.singletonList(binding));
}
story.then(r -> {
SnippetizerTester st = new SnippetizerTester(r);
SSHUserPrivateKey c = new DummyPrivateKey("creds", "bob", "secret", "the-key");
CredentialsProvider.lookupStores(story.j.jenkins).iterator().next().addCredentials(Domain.global(), c);
SSHUserPrivateKeyBinding binding = new SSHUserPrivateKeyBinding("keyFile", "creds");
BindingStep s = new StepConfigTester(story.j).configRoundTrip(new BindingStep(Collections.<MultiBinding>singletonList(binding)));
st.assertRoundTrip(s, "withCredentials([sshUserPrivateKey(credentialsId: 'creds', keyFileVariable: 'keyFile')]) {\n // some block\n}");
r.assertEqualDataBoundBeans(s.getBindings(), Collections.singletonList(binding));
binding.setPassphraseVariable("passphrase");
binding.setUsernameVariable("user");
s = new StepConfigTester(story.j).configRoundTrip(new BindingStep(Collections.<MultiBinding>singletonList(binding)));
st.assertRoundTrip(s, "withCredentials([sshUserPrivateKey(credentialsId: 'creds', keyFileVariable: 'keyFile', passphraseVariable: 'passphrase', usernameVariable: 'user')]) {\n // some block\n}");
r.assertEqualDataBoundBeans(s.getBindings(), Collections.singletonList(binding));
});
}


@Test public void basics() throws Exception {
final String credentialsId = "creds";
final String username = "bob";
Expand Down