mirror of https://github.com/apache/jclouds.git
Issue 760:sshj driver throws exception disconnecting when client is already disconnected
This commit is contained in:
parent
4981ca6efb
commit
09678a1df0
|
@ -39,6 +39,7 @@ import javax.annotation.PreDestroy;
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.inject.Named;
|
import javax.inject.Named;
|
||||||
|
|
||||||
|
import net.schmizz.sshj.SSHClient;
|
||||||
import net.schmizz.sshj.common.IOUtils;
|
import net.schmizz.sshj.common.IOUtils;
|
||||||
import net.schmizz.sshj.connection.ConnectionException;
|
import net.schmizz.sshj.connection.ConnectionException;
|
||||||
import net.schmizz.sshj.connection.channel.direct.Session;
|
import net.schmizz.sshj.connection.channel.direct.Session;
|
||||||
|
@ -126,7 +127,8 @@ public class SshjSshClient implements SshClient {
|
||||||
@Named("jclouds.ssh")
|
@Named("jclouds.ssh")
|
||||||
protected Logger logger = Logger.NULL;
|
protected Logger logger = Logger.NULL;
|
||||||
|
|
||||||
private net.schmizz.sshj.SSHClient ssh;
|
@VisibleForTesting
|
||||||
|
SSHClient ssh;
|
||||||
private final byte[] privateKey;
|
private final byte[] privateKey;
|
||||||
final byte[] emptyPassPhrase = new byte[0];
|
final byte[] emptyPassPhrase = new byte[0];
|
||||||
private final int timeoutMillis;
|
private final int timeoutMillis;
|
||||||
|
@ -176,7 +178,7 @@ public class SshjSshClient implements SshClient {
|
||||||
try {
|
try {
|
||||||
ssh.disconnect();
|
ssh.disconnect();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Throwables.propagate(e);
|
logger.warn(e, "<< exception disconnecting from %s: %s", e, e.getMessage());
|
||||||
}
|
}
|
||||||
ssh = null;
|
ssh = null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,12 +19,18 @@
|
||||||
package org.jclouds.sshj;
|
package org.jclouds.sshj;
|
||||||
|
|
||||||
import static com.google.inject.name.Names.bindProperties;
|
import static com.google.inject.name.Names.bindProperties;
|
||||||
|
import static org.easymock.EasyMock.expect;
|
||||||
|
import static org.easymock.EasyMock.expectLastCall;
|
||||||
|
import static org.easymock.classextension.EasyMock.createMock;
|
||||||
|
import static org.easymock.classextension.EasyMock.replay;
|
||||||
|
import static org.easymock.classextension.EasyMock.verify;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.ConnectException;
|
import java.net.ConnectException;
|
||||||
import java.net.SocketTimeoutException;
|
import java.net.SocketTimeoutException;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import net.schmizz.sshj.SSHClient;
|
||||||
import net.schmizz.sshj.common.SSHException;
|
import net.schmizz.sshj.common.SSHException;
|
||||||
import net.schmizz.sshj.connection.ConnectionException;
|
import net.schmizz.sshj.connection.ConnectionException;
|
||||||
import net.schmizz.sshj.transport.TransportException;
|
import net.schmizz.sshj.transport.TransportException;
|
||||||
|
@ -123,8 +129,7 @@ public class SshjSshClientTest {
|
||||||
new SSHException("Session.connect: java.io.IOException: End of IO Stream Read")).apply(
|
new SSHException("Session.connect: java.io.IOException: End of IO Stream Read")).apply(
|
||||||
" End of IO Stream Read");
|
" End of IO Stream Read");
|
||||||
assert ssh.causalChainHasMessageContaining(
|
assert ssh.causalChainHasMessageContaining(
|
||||||
new SSHException("Session.connect: java.net.SocketException: Connection reset"))
|
new SSHException("Session.connect: java.net.SocketException: Connection reset")).apply("java.net.Socket");
|
||||||
.apply("java.net.Socket");
|
|
||||||
assert !ssh.causalChainHasMessageContaining(new NullPointerException()).apply(" End of IO Stream Read");
|
assert !ssh.causalChainHasMessageContaining(new NullPointerException()).apply(" End of IO Stream Read");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,7 +145,10 @@ public class SshjSshClientTest {
|
||||||
private static class ExceptionWithStrangeToString extends RuntimeException {
|
private static class ExceptionWithStrangeToString extends RuntimeException {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
private static final String MESSAGE = "foo-bar-exception-tostring";
|
private static final String MESSAGE = "foo-bar-exception-tostring";
|
||||||
public String toString() { return MESSAGE; }
|
|
||||||
|
public String toString() {
|
||||||
|
return MESSAGE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testRetryOnToStringCustom() {
|
public void testRetryOnToStringCustom() {
|
||||||
|
@ -151,6 +159,18 @@ public class SshjSshClientTest {
|
||||||
assert ssh1.shouldRetry(new RuntimeException(nex));
|
assert ssh1.shouldRetry(new RuntimeException(nex));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testDontThrowIOExceptionOnClear() throws Exception {
|
||||||
|
SshjSshClient ssh1 = createClient();
|
||||||
|
SSHClient ssh = createMock(SSHClient.class);
|
||||||
|
expect(ssh.isConnected()).andReturn(true);
|
||||||
|
ssh.disconnect();
|
||||||
|
expectLastCall().andThrow(new ConnectionException("disconnected"));
|
||||||
|
replay(ssh);
|
||||||
|
ssh1.ssh = ssh;
|
||||||
|
ssh1.sshConnection.clear();
|
||||||
|
verify(ssh);
|
||||||
|
}
|
||||||
|
|
||||||
public void testRetryNotOnToStringCustomMismatch() {
|
public void testRetryNotOnToStringCustomMismatch() {
|
||||||
Exception nex = new ExceptionWithStrangeToString();
|
Exception nex = new ExceptionWithStrangeToString();
|
||||||
Properties props = new Properties();
|
Properties props = new Properties();
|
||||||
|
|
Loading…
Reference in New Issue