Issue 720: added live profile and logging to sshj

This commit is contained in:
Adrian Cole 2011-10-13 11:11:27 -07:00
parent e8f5d39c2e
commit bf4db76152
5 changed files with 135 additions and 20 deletions

View File

@ -44,6 +44,14 @@
</repository> </repository>
</repositories> </repositories>
<properties>
<test.ssh.host>localhost</test.ssh.host>
<test.ssh.port>22</test.ssh.port>
<test.ssh.username />
<test.ssh.password />
<test.ssh.keyfile />
</properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.jclouds</groupId> <groupId>org.jclouds</groupId>
@ -60,6 +68,12 @@
<artifactId>jclouds-slf4j</artifactId> <artifactId>jclouds-slf4j</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>0.9.30</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.jclouds.driver</groupId> <groupId>org.jclouds.driver</groupId>
<artifactId>jclouds-bouncycastle</artifactId> <artifactId>jclouds-bouncycastle</artifactId>
@ -99,4 +113,65 @@
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<profiles>
<profile>
<id>live</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>integration</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<!-- note that the groups/excluded groups don't work due to some problem
in surefire or testng. instead, we have to exclude via file path
<groups>live,integration</groups>
<excludedGroups>unit,performance</excludedGroups> -->
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<include>**/*IntegrationTest.java</include>
<include>**/*LiveTest.java</include>
</includes>
<systemProperties>
<property>
<name>file.encoding</name>
<value>UTF-8</value>
</property>
<property>
<name>test.ssh.host</name>
<value>${test.ssh.host}</value>
</property>
<property>
<name>test.ssh.port</name>
<value>${test.ssh.port}</value>
</property>
<property>
<name>test.ssh.username</name>
<value>${test.ssh.username}</value>
</property>
<property>
<name>test.ssh.keyfile</name>
<value>${test.ssh.keyfile}</value>
</property>
<property>
<name>test.ssh.password</name>
<value>${test.ssh.password}</value>
</property>
</systemProperties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project> </project>

View File

@ -24,11 +24,13 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress;
import org.jclouds.compute.domain.ExecResponse; import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.domain.Credentials; import org.jclouds.domain.Credentials;
import org.jclouds.io.Payload; import org.jclouds.io.Payload;
import org.jclouds.io.Payloads; import org.jclouds.io.Payloads;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.net.IPSocket; import org.jclouds.net.IPSocket;
import org.jclouds.ssh.SshClient; import org.jclouds.ssh.SshClient;
import org.jclouds.sshj.config.SshjSshClientModule; import org.jclouds.sshj.config.SshjSshClientModule;
@ -44,7 +46,7 @@ import com.google.inject.Injector;
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
@Test(groups = "live") @Test(groups = "live", testName = "SshjSshClientLiveTest")
public class SshjSshClientLiveTest { public class SshjSshClientLiveTest {
protected static final String sshHost = System.getProperty("test.ssh.host", "localhost"); protected static final String sshHost = System.getProperty("test.ssh.host", "localhost");
protected static final String sshPort = System.getProperty("test.ssh.port", "22"); protected static final String sshPort = System.getProperty("test.ssh.port", "22");
@ -57,8 +59,8 @@ public class SshjSshClientLiveTest {
public SshClient setupClient() throws NumberFormatException, FileNotFoundException, IOException { public SshClient setupClient() throws NumberFormatException, FileNotFoundException, IOException {
int port = Integer.parseInt(sshPort); int port = Integer.parseInt(sshPort);
if (sshUser == null if (sshUser == null
|| ((sshPass == null || sshPass.trim().equals("")) && (sshKeyFile == null || sshKeyFile.trim().equals(""))) || ((sshPass == null || sshPass.trim().equals("")) && (sshKeyFile == null || sshKeyFile.trim()
|| sshUser.trim().equals("")) { .equals(""))) || sshUser.trim().equals("")) {
System.err.println("ssh credentials not present. Tests will be lame"); System.err.println("ssh credentials not present. Tests will be lame");
return new SshClient() { return new SshClient() {
@ -106,12 +108,12 @@ public class SshjSshClientLiveTest {
}; };
} else { } else {
Injector i = Guice.createInjector(new SshjSshClientModule()); Injector i = Guice.createInjector(new SshjSshClientModule(), new SLF4JLoggingModule());
SshClient.Factory factory = i.getInstance(SshClient.Factory.class); SshClient.Factory factory = i.getInstance(SshClient.Factory.class);
SshClient connection; SshClient connection;
if (sshKeyFile != null && !sshKeyFile.trim().equals("")) { if (sshKeyFile != null && !sshKeyFile.trim().equals("")) {
connection = factory.create(new IPSocket(sshHost, port), connection = factory.create(new IPSocket(sshHost, port), new Credentials(sshUser, Strings2
new Credentials(sshUser, Strings2.toStringAndClose(new FileInputStream(sshKeyFile)))); .toStringAndClose(new FileInputStream(sshKeyFile))));
} else { } else {
connection = factory.create(new IPSocket(sshHost, port), new Credentials(sshUser, sshPass)); connection = factory.create(new IPSocket(sshHost, port), new Credentials(sshUser, sshPass));
} }
@ -139,7 +141,8 @@ public class SshjSshClientLiveTest {
public void testExecHostname() throws IOException { public void testExecHostname() throws IOException {
ExecResponse response = setupClient().exec("hostname"); ExecResponse response = setupClient().exec("hostname");
assertEquals(response.getError(), ""); assertEquals(response.getError(), "");
assertEquals(response.getOutput().trim(), sshHost); assertEquals(response.getOutput().trim(), "localhost".equals(sshHost) ? InetAddress.getLocalHost().getHostName()
: sshHost);
} }
} }

View File

@ -18,8 +18,10 @@
*/ */
package org.jclouds.sshj; package org.jclouds.sshj;
import static com.google.inject.name.Names.bindProperties;
import java.io.IOException; import java.io.IOException;
import java.net.UnknownHostException; import java.util.Properties;
import net.schmizz.sshj.common.SSHException; import net.schmizz.sshj.common.SSHException;
import net.schmizz.sshj.connection.ConnectionException; import net.schmizz.sshj.connection.ConnectionException;
@ -27,6 +29,7 @@ import net.schmizz.sshj.transport.TransportException;
import net.schmizz.sshj.userauth.UserAuthException; import net.schmizz.sshj.userauth.UserAuthException;
import org.jclouds.domain.Credentials; import org.jclouds.domain.Credentials;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.net.IPSocket; import org.jclouds.net.IPSocket;
import org.jclouds.rest.AuthorizationException; import org.jclouds.rest.AuthorizationException;
import org.jclouds.ssh.SshClient; import org.jclouds.ssh.SshClient;
@ -34,6 +37,7 @@ import org.jclouds.sshj.config.SshjSshClientModule;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.inject.AbstractModule;
import com.google.inject.Guice; import com.google.inject.Guice;
import com.google.inject.Injector; import com.google.inject.Injector;
import com.google.inject.Module; import com.google.inject.Module;
@ -48,12 +52,23 @@ public class SshjSshClientTest {
protected SshjSshClient ssh; protected SshjSshClient ssh;
@BeforeTest @BeforeTest
public void setupSsh() throws UnknownHostException { public void setupSsh() {
ssh = createClient(); ssh = createClient();
} }
protected SshjSshClient createClient() throws UnknownHostException { protected SshjSshClient createClient() {
Injector i = Guice.createInjector(module()); return createClient(new Properties());
}
protected SshjSshClient createClient(final Properties props) {
Injector i = Guice.createInjector(module(), new AbstractModule() {
@Override
protected void configure() {
bindProperties(binder(), props);
}
}, new SLF4JLoggingModule());
SshClient.Factory factory = i.getInstance(SshClient.Factory.class); SshClient.Factory factory = i.getInstance(SshClient.Factory.class);
SshjSshClient ssh = SshjSshClient.class.cast(factory.create(new IPSocket("localhost", 22), new Credentials( SshjSshClient ssh = SshjSshClient.class.cast(factory.create(new IPSocket("localhost", 22), new Credentials(
"username", "password"))); "username", "password")));
@ -75,13 +90,20 @@ public class SshjSshClientTest {
assert !ssh.shouldRetry(new IOException("channel %s is not open", new NullPointerException())); assert !ssh.shouldRetry(new IOException("channel %s is not open", new NullPointerException()));
} }
public void testOnlyRetryAuthWhenSet() throws UnknownHostException { public void testOnlyRetryAuthWhenSet() {
SshjSshClient ssh1 = createClient(); SshjSshClient ssh1 = createClient();
assert !ssh1.shouldRetry(new AuthorizationException("problem", null)); assert !ssh1.shouldRetry(new AuthorizationException("problem", null));
ssh1.retryAuth = true; ssh1.retryAuth = true;
assert ssh1.shouldRetry(new AuthorizationException("problem", null)); assert ssh1.shouldRetry(new AuthorizationException("problem", null));
} }
public void testOnlyRetryAuthWhenSetViaProperties() {
Properties props = new Properties();
props.setProperty("jclouds.ssh.retry-auth", "true");
SshjSshClient ssh1 = createClient(props);
assert ssh1.shouldRetry(new AuthorizationException("problem", null));
}
public void testExceptionMessagesRetry() { public void testExceptionMessagesRetry() {
assert !ssh.shouldRetry(new SSHException("")); assert !ssh.shouldRetry(new SSHException(""));
assert !ssh.shouldRetry(new NullPointerException((String) null)); assert !ssh.shouldRetry(new NullPointerException((String) null));
@ -92,7 +114,8 @@ 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")).apply("java.net.Socket"); new SSHException("Session.connect: java.net.SocketException: Connection reset"))
.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");
} }
} }

View File

@ -18,9 +18,8 @@
*/ */
package org.jclouds.sshj.config; package org.jclouds.sshj.config;
import java.net.UnknownHostException;
import org.jclouds.domain.Credentials; import org.jclouds.domain.Credentials;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.net.IPSocket; import org.jclouds.net.IPSocket;
import org.jclouds.ssh.SshClient; import org.jclouds.ssh.SshClient;
import org.jclouds.sshj.SshjSshClient; import org.jclouds.sshj.SshjSshClient;
@ -37,9 +36,9 @@ import com.google.inject.Injector;
@Test @Test
public class SshjSshClientModuleTest { public class SshjSshClientModuleTest {
public void testConfigureBindsClient() throws UnknownHostException { public void testConfigureBindsClient() {
Injector i = Guice.createInjector(new SshjSshClientModule()); Injector i = Guice.createInjector(new SshjSshClientModule(), new SLF4JLoggingModule());
SshClient.Factory factory = i.getInstance(SshClient.Factory.class); SshClient.Factory factory = i.getInstance(SshClient.Factory.class);
SshClient connection = factory.create(new IPSocket("localhost", 22), new Credentials("username", "password")); SshClient connection = factory.create(new IPSocket("localhost", 22), new Credentials("username", "password"));
assert connection instanceof SshjSshClient; assert connection instanceof SshjSshClient;

View File

@ -0,0 +1,15 @@
<?xml version="1.0"?>
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>target/test-data/jclouds-ssh.log</file>
<append>true</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<logger name="jclouds.ssh" level="TRACE"/>
<logger name="net.schmizz" level="INFO"/>
<root level="INFO">
<appender-ref ref="FILE"/>
</root>
</configuration>