Skip to content

Commit

Permalink
[JENKINS-34336] Use JenkinsRule in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
armfergom committed Apr 19, 2016
1 parent 45e1d5e commit 23d013a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 34 deletions.
Expand Up @@ -24,9 +24,7 @@
package com.cloudbees.jenkins.plugins.sshcredentials.impl;

import com.cloudbees.jenkins.plugins.sshcredentials.SSHAuthenticator;
import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPassword;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.jcraft.jsch.HostKey;
import com.jcraft.jsch.HostKeyRepository;
Expand All @@ -40,29 +38,33 @@
import org.apache.sshd.server.auth.UserAuthPassword;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.session.ServerSession;
import org.jvnet.hudson.test.HudsonTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.util.Arrays;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

public class JSchSSHPasswordAuthenticatorTest extends HudsonTestCase {
public class JSchSSHPasswordAuthenticatorTest {

private JSchConnector connector;
private StandardUsernamePasswordCredentials user;

@Override
protected void tearDown() throws Exception {
@Rule public JenkinsRule r = new JenkinsRule();

@After
public void tearDown() throws Exception {
if (connector != null) {
connector.close();
connector = null;
}
super.tearDown();
}

// disabled as Apache MINA sshd does not provide easy mech for giving a Keyboard Interactive authenticator
Expand All @@ -87,11 +89,12 @@ public void dontTestKeyboardInteractive() throws Exception {
assertThat(connector.getSession().isConnected(), is(true));
}

protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
user =(StandardUsernamePasswordCredentials) Items.XSTREAM.fromXML(Items.XSTREAM.toXML(new BasicSSHUserPassword(CredentialsScope.SYSTEM, null, "foobar", "foomanchu", null)));
}

@Test
public void testPassword() throws Exception {
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(0);
Expand Down Expand Up @@ -123,6 +126,7 @@ public boolean authenticate(String username, String password, ServerSession sess
}
}

@Test
public void testFactory() throws Exception {
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(0);
Expand Down
Expand Up @@ -27,8 +27,6 @@
import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;
import com.cloudbees.plugins.credentials.CredentialsDescriptor;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.trilead.ssh2.Connection;
import com.trilead.ssh2.ServerHostKeyVerifier;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.util.Secret;
Expand All @@ -39,7 +37,11 @@
import org.apache.sshd.server.auth.UserAuthPublicKey;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.session.ServerSession;
import org.jvnet.hudson.test.HudsonTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.security.PublicKey;
import java.util.Arrays;
Expand All @@ -52,22 +54,23 @@
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

public class JSchSSHPublicKeyAuthenticatorTest extends HudsonTestCase {
public class JSchSSHPublicKeyAuthenticatorTest {

private JSchConnector connector;
private SSHUserPrivateKey user;

@Override
protected void tearDown() throws Exception {
@Rule public JenkinsRule r = new JenkinsRule();

@After
public void tearDown() throws Exception {
if (connector != null) {
connector.close();
connector = null;
}
super.tearDown();
}

protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
user = new SSHUserPrivateKey() {

@NonNull
Expand Down Expand Up @@ -132,6 +135,7 @@ public List<String> getPrivateKeys() {
};
}

@Test
public void testAuthenticate() throws Exception {
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(0);
Expand Down Expand Up @@ -164,6 +168,7 @@ public boolean authenticate(String username, PublicKey key, ServerSession sessio
}
}

@Test
public void testFactory() throws Exception {
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(0);
Expand Down
Expand Up @@ -41,7 +41,11 @@
import org.apache.sshd.server.auth.UserAuthPassword;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.session.ServerSession;
import org.jvnet.hudson.test.HudsonTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.util.Arrays;
import java.util.logging.Level;
Expand All @@ -50,14 +54,16 @@
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class TrileadSSHPasswordAuthenticatorTest extends HudsonTestCase {
public class TrileadSSHPasswordAuthenticatorTest {

private Connection connection;
private StandardUsernamePasswordCredentials user;
private SshServer sshd;

@Override
protected void tearDown() throws Exception {
@Rule public JenkinsRule r = new JenkinsRule();

@After
public void tearDown() throws Exception {
if (connection != null) {
connection.close();
connection = null;
Expand All @@ -69,7 +75,6 @@ protected void tearDown() throws Exception {
Logger.getLogger(getClass().getName()).log(Level.WARNING, "Problems shutting down ssh server", t);
}
}
super.tearDown();
}

// disabled as Apache MINA sshd does not provide easy mech for giving a Keyboard Interactive authenticator
Expand All @@ -87,11 +92,12 @@ public void dontTestKeyboardInteractive() throws Exception {
assertThat(instance.isAuthenticated(), is(true));
}

protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
user =(StandardUsernamePasswordCredentials) Items.XSTREAM.fromXML(Items.XSTREAM.toXML(new BasicSSHUserPassword(CredentialsScope.SYSTEM, null, "foobar", "foomanchu", null)));
}

@Test
public void testPassword() throws Exception {
sshd = createPasswordAuthenticatedSshServer();
sshd.start();
Expand Down Expand Up @@ -122,6 +128,7 @@ public boolean authenticate(String _username, String password, ServerSession ses
return sshd;
}

@Test
public void testFactory() throws Exception {
sshd = createPasswordAuthenticatedSshServer();
sshd.start();
Expand All @@ -134,6 +141,7 @@ public void testFactory() throws Exception {
assertThat(instance.isAuthenticated(), is(true));
}

@Test
public void testFactoryAltUsername() throws Exception {
sshd = createPasswordAuthenticatedSshServer("bill");
sshd.start();
Expand All @@ -156,11 +164,12 @@ public void testFactoryAltUsername() throws Exception {
/**
* Brings the {@link SSHAuthenticatorFactory} to a slave.
*/
@Test
public void testSlave() throws Exception {
SshServer sshd = createPasswordAuthenticatedSshServer();
sshd.start();

DumbSlave s = createSlave();
DumbSlave s = r.createSlave();
Computer c = s.toComputer();
c.connect(false).get();

Expand Down
Expand Up @@ -39,7 +39,11 @@
import org.apache.sshd.server.auth.UserAuthPublicKey;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.session.ServerSession;
import org.jvnet.hudson.test.HudsonTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.security.PublicKey;
import java.util.Arrays;
Expand All @@ -51,22 +55,23 @@
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class TrileadSSHPublicKeyAuthenticatorTest extends HudsonTestCase {
public class TrileadSSHPublicKeyAuthenticatorTest {

private Connection connection;
private SSHUserPrivateKey user;

@Override
protected void tearDown() throws Exception {
@Rule public JenkinsRule r = new JenkinsRule();

@After
public void tearDown() throws Exception {
if (connection != null) {
connection.close();
connection = null;
}
super.tearDown();
}

protected void setUp() throws Exception {
super.setUp();
@Before
public void setUp() throws Exception {
user = new SSHUserPrivateKey() {

@NonNull
Expand Down Expand Up @@ -130,6 +135,7 @@ public List<String> getPrivateKeys() {
};
}

@Test
public void testAuthenticate() throws Exception {
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(0);
Expand Down Expand Up @@ -165,6 +171,7 @@ public boolean verifyServerHostKey(String hostname, int port, String serverHostK
}
}

@Test
public void testFactory() throws Exception {
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(0);
Expand Down Expand Up @@ -199,6 +206,7 @@ public boolean verifyServerHostKey(String hostname, int port, String serverHostK
}
}

@Test
public void testAltUsername() throws Exception {
SshServer sshd = SshServer.setUpDefaultServer();
sshd.setPort(0);
Expand Down

0 comments on commit 23d013a

Please sign in to comment.