PageRenderTime 35ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/jsch/src/test/java/org/jclouds/ssh/jsch/JschSshClientTest.java

http://github.com/jclouds/jclouds
Java | 154 lines | 114 code | 22 blank | 18 comment | 0 complexity | e972bec352364aac2dee65c66d9c93ba MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.jclouds.ssh.jsch;
  18. import static com.google.inject.name.Names.bindProperties;
  19. import java.io.IOException;
  20. import java.net.ConnectException;
  21. import java.net.UnknownHostException;
  22. import java.util.Properties;
  23. import org.jclouds.domain.LoginCredentials;
  24. import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
  25. import org.jclouds.rest.AuthorizationException;
  26. import org.jclouds.ssh.SshClient;
  27. import org.jclouds.ssh.jsch.config.JschSshClientModule;
  28. import org.testng.annotations.BeforeTest;
  29. import org.testng.annotations.Test;
  30. import com.google.common.net.HostAndPort;
  31. import com.google.inject.AbstractModule;
  32. import com.google.inject.Guice;
  33. import com.google.inject.Injector;
  34. import com.google.inject.Module;
  35. import com.jcraft.jsch.ChannelSftp;
  36. import com.jcraft.jsch.JSchException;
  37. import com.jcraft.jsch.SftpException;
  38. @Test
  39. public class JschSshClientTest {
  40. protected JschSshClient ssh;
  41. @BeforeTest
  42. public void setupSsh() throws UnknownHostException {
  43. ssh = createClient(new Properties());
  44. }
  45. protected JschSshClient createClient() throws UnknownHostException {
  46. return createClient(new Properties());
  47. }
  48. protected JschSshClient createClient(final Properties props) throws UnknownHostException {
  49. Injector i = Guice.createInjector(module(), new AbstractModule() {
  50. @Override
  51. protected void configure() {
  52. bindProperties(binder(), props);
  53. }
  54. }, new SLF4JLoggingModule());
  55. SshClient.Factory factory = i.getInstance(SshClient.Factory.class);
  56. JschSshClient ssh = JschSshClient.class.cast(factory.create(HostAndPort.fromParts("localhost", 22), LoginCredentials
  57. .builder().user("username").password("password").build()));
  58. return ssh;
  59. }
  60. protected Module module() {
  61. return new JschSshClientModule();
  62. }
  63. @Test(expectedExceptions = AuthorizationException.class)
  64. public void testPropateConvertsAuthException() {
  65. ssh.propagate(new JSchException("Auth fail"), "");
  66. }
  67. public void testExceptionClassesRetry() {
  68. assert ssh.shouldRetry(new JSchException("io error", new IOException("socket closed")));
  69. assert ssh.shouldRetry(new JSchException("connect error", new ConnectException("problem")));
  70. assert ssh.shouldRetry(new IOException("channel is not open", new NullPointerException()));
  71. assert ssh.shouldRetry(new IOException("channel is not open", new NullPointerException(null)));
  72. }
  73. public void testOnlyRetryAuthWhenSet() throws UnknownHostException {
  74. JschSshClient ssh1 = createClient();
  75. assert !ssh1.shouldRetry(new AuthorizationException("problem", null));
  76. ssh1.retryAuth = true;
  77. assert ssh1.shouldRetry(new AuthorizationException("problem", null));
  78. }
  79. public void testOnlyRetryAuthWhenSetViaProperties() throws UnknownHostException {
  80. Properties props = new Properties();
  81. props.setProperty("jclouds.ssh.retry-auth", "true");
  82. JschSshClient ssh1 = createClient(props);
  83. assert ssh1.shouldRetry(new AuthorizationException("problem", null));
  84. }
  85. public void testExceptionMessagesRetry() {
  86. assert !ssh.shouldRetry(new NullPointerException(""));
  87. assert !ssh.shouldRetry(new NullPointerException((String) null));
  88. assert ssh.shouldRetry(new JSchException("Session.connect: java.io.IOException: End of IO Stream Read"));
  89. assert ssh.shouldRetry(new JSchException("Session.connect: invalid data"));
  90. assert ssh.shouldRetry(new JSchException("Session.connect: java.net.SocketException: Connection reset"));
  91. }
  92. public void testDoNotRetryOnGeneralSftpError() {
  93. // http://sourceforge.net/mailarchive/forum.php?thread_name=CAARMrHVhASeku48xoAgWEb-nEpUuYkMA03PoA5TvvFdk%3DjGKMA%40mail.gmail.com&forum_name=jsch-users
  94. assert !ssh.shouldRetry(new SftpException(ChannelSftp.SSH_FX_FAILURE, new NullPointerException().toString()));
  95. }
  96. public void testCausalChainHasMessageContaining() {
  97. assert ssh.causalChainHasMessageContaining(
  98. new JSchException("Session.connect: java.io.IOException: End of IO Stream Read")).apply(
  99. " End of IO Stream Read");
  100. assert ssh.causalChainHasMessageContaining(new JSchException("Session.connect: invalid data")).apply(
  101. " invalid data");
  102. assert ssh.causalChainHasMessageContaining(
  103. new JSchException("Session.connect: java.net.SocketException: Connection reset")).apply("java.net.Socket");
  104. assert !ssh.causalChainHasMessageContaining(new NullPointerException()).apply(" End of IO Stream Read");
  105. }
  106. public void testRetryOnToStringNpe() throws UnknownHostException {
  107. Exception nex = new NullPointerException();
  108. Properties props = new Properties();
  109. // ensure we test toString on the exception independently
  110. props.setProperty("jclouds.ssh.retryable-messages", nex.toString());
  111. JschSshClient ssh1 = createClient(props);
  112. assert ssh1.shouldRetry(new RuntimeException(nex));
  113. }
  114. private static class ExceptionWithStrangeToString extends RuntimeException {
  115. private static final String MESSAGE = "foo-bar-exception-tostring";
  116. public String toString() { return MESSAGE; }
  117. }
  118. public void testRetryOnToStringCustom() throws UnknownHostException {
  119. Exception nex = new ExceptionWithStrangeToString();
  120. Properties props = new Properties();
  121. props.setProperty("jclouds.ssh.retryable-messages", "foo-bar");
  122. JschSshClient ssh1 = createClient(props);
  123. assert ssh1.shouldRetry(new RuntimeException(nex));
  124. }
  125. public void testRetryNotOnToStringCustomMismatch() throws UnknownHostException {
  126. Exception nex = new ExceptionWithStrangeToString();
  127. Properties props = new Properties();
  128. props.setProperty("jclouds.ssh.retryable-messages", "foo-baR");
  129. JschSshClient ssh1 = createClient(props);
  130. assert !ssh1.shouldRetry(new RuntimeException(nex));
  131. }
  132. }