com.sonyericsson.hudson.plugins.gerrit.trigger.test
Class SshdServerMock

java.lang.Object
  extended by com.sonyericsson.hudson.plugins.gerrit.trigger.test.SshdServerMock
All Implemented Interfaces:
org.apache.sshd.server.CommandFactory

public class SshdServerMock
extends java.lang.Object
implements org.apache.sshd.server.CommandFactory

The beginning of a mock of a sshd server. When it is done the idea is to use this to send in stream-events over the ssh connection, and make connection related tests without running Gerrit on the local machine. There is some progress but most of the predefined command types has issues.

Author:
Robert Sandell <robert.sandell@sonyericsson.com>

Nested Class Summary
static class SshdServerMock.CommandLookup
          Utility class for looking up and creating commands.
static class SshdServerMock.CommandMock
          A mocked ssh command.
static class SshdServerMock.EofCommandMock
          A command that immediately returns 0.
static class SshdServerMock.PrintLinesCommand
          A Command that prints a given list of lines when the SshdServerMock.PrintLinesCommand.now() method is called and then exits with 0.
 
Field Summary
static int GERRIT_SSH_PORT
          The default port that Gerrit usually listens to.
static java.lang.String GERRIT_STREAM_EVENTS
          The stream-events command.
protected static int MIN_SLEEP
          Minimum time a thread can sleep.
protected static int ONE_SECOND
          One second in ms.
protected static int WAIT_FOR_ERROR_OUTPUT
          How long to sleep to let the ssh-keygen error output appear on stderr.
 
Constructor Summary
SshdServerMock()
           
 
Method Summary
 org.apache.sshd.server.Command createCommand(java.lang.String s)
           
static java.io.File generateKeyPair()
          Generates a rsa key-pair in /tmp/jenkins-testkey for use with authenticating the trigger against the mock server.
 SshdServerMock.CommandMock getCurrentCommand()
          The last started command.
 SshdServerMock.CommandMock getRunningCommand(java.lang.String commandSearch)
          Gets the first running command that matches the given regular expression.
 void returnCommandFor(java.lang.String commandPattern, java.lang.Class<? extends SshdServerMock.CommandMock> cmd)
          Specifies a command type to instantiate and give to mina when a command matching the given regular expression is wanted.
 void returnCommandFor(java.lang.String commandPattern, java.lang.Class<? extends SshdServerMock.CommandMock> cmd, boolean oneShot, java.lang.Object[] arguments, java.lang.Class<?>[] types)
          Specifies a command type to instantiate and give to mina when a command matching the given regular expression is wanted.
 void returnCommandFor(java.lang.String commandPattern, java.lang.Class<? extends SshdServerMock.CommandMock> cmd, java.lang.Object[] arguments, java.lang.Class<?>[] types)
          Specifies a command type to instantiate and give to mina when a command matching the given regular expression is wanted.
protected  SshdServerMock.CommandMock setCurrentCommand(SshdServerMock.CommandMock command)
          Sets the current command being executed and adds it to the commandHistory.
static org.apache.sshd.SshServer startServer(int port, SshdServerMock server)
          Starts a ssh server on the provided port.
static org.apache.sshd.SshServer startServer(SshdServerMock server)
          Starts a ssh server on the standard Gerrit port.
 SshdServerMock.CommandMock waitForCommand(java.lang.String commandSearch, int timeout)
          Waits for a running command matching the provided regular expression to appear in the command history.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GERRIT_STREAM_EVENTS

public static final java.lang.String GERRIT_STREAM_EVENTS
The stream-events command.

See Also:
Constant Field Values

GERRIT_SSH_PORT

public static final int GERRIT_SSH_PORT
The default port that Gerrit usually listens to.

See Also:
Constant Field Values

WAIT_FOR_ERROR_OUTPUT

protected static final int WAIT_FOR_ERROR_OUTPUT
How long to sleep to let the ssh-keygen error output appear on stderr.

See Also:
Constant Field Values

ONE_SECOND

protected static final int ONE_SECOND
One second in ms.

See Also:
Constant Field Values

MIN_SLEEP

protected static final int MIN_SLEEP
Minimum time a thread can sleep.

See Also:
Constant Field Values
Constructor Detail

SshdServerMock

public SshdServerMock()
Method Detail

createCommand

public org.apache.sshd.server.Command createCommand(java.lang.String s)
Specified by:
createCommand in interface org.apache.sshd.server.CommandFactory

setCurrentCommand

protected SshdServerMock.CommandMock setCurrentCommand(SshdServerMock.CommandMock command)
Sets the current command being executed and adds it to the commandHistory.

Parameters:
command - the command.
Returns:
the command.

getCurrentCommand

public SshdServerMock.CommandMock getCurrentCommand()
The last started command. There could be other commands running in parallel.

Returns:
the current command.

getRunningCommand

public SshdServerMock.CommandMock getRunningCommand(java.lang.String commandSearch)
Gets the first running command that matches the given regular expression.

Parameters:
commandSearch - the regular expression to match.
Returns:
the found command or null.

returnCommandFor

public void returnCommandFor(java.lang.String commandPattern,
                             java.lang.Class<? extends SshdServerMock.CommandMock> cmd)
                      throws java.lang.NoSuchMethodException
Specifies a command type to instantiate and give to mina when a command matching the given regular expression is wanted.

Parameters:
commandPattern - the regular expression
cmd - the class to create the command from. The class must have a constructor taking a single String argument which is the command requested.
Throws:
java.lang.NoSuchMethodException - if there is no matching constructor.

returnCommandFor

public void returnCommandFor(java.lang.String commandPattern,
                             java.lang.Class<? extends SshdServerMock.CommandMock> cmd,
                             java.lang.Object[] arguments,
                             java.lang.Class<?>[] types)
                      throws java.lang.NoSuchMethodException
Specifies a command type to instantiate and give to mina when a command matching the given regular expression is wanted.

Parameters:
commandPattern - the regular expression
cmd - the class to create the command from. The class must have a constructor where the first argument is a String followed by the class types provided by the types parameter.
arguments - the other arguments to the constructor besides the command.
types - the other class types to match the constructor against.
Throws:
java.lang.NoSuchMethodException - if there is no matching constructor.

returnCommandFor

public void returnCommandFor(java.lang.String commandPattern,
                             java.lang.Class<? extends SshdServerMock.CommandMock> cmd,
                             boolean oneShot,
                             java.lang.Object[] arguments,
                             java.lang.Class<?>[] types)
                      throws java.lang.NoSuchMethodException
Specifies a command type to instantiate and give to mina when a command matching the given regular expression is wanted.

Parameters:
commandPattern - the regular expression
cmd - the class to create the command from. The class must have a constructor where the first argument is a String followed by the class types provided by the types parameter.
oneShot - if this command should only be returned the first time it is called for.
arguments - the other arguments to the constructor besides the command.
types - the other class types to match the constructor against.
Throws:
java.lang.NoSuchMethodException - if there is no matching constructor.

waitForCommand

public SshdServerMock.CommandMock waitForCommand(java.lang.String commandSearch,
                                                 int timeout)
Waits for a running command matching the provided regular expression to appear in the command history.

Parameters:
commandSearch - a regular expression.
timeout - the maximum time to wait for the command in ms.
Returns:
the command.

startServer

public static org.apache.sshd.SshServer startServer(int port,
                                                    SshdServerMock server)
                                             throws java.io.IOException
Starts a ssh server on the provided port.

Parameters:
port - the port to listen to.
Returns:
the server.
Throws:
java.io.IOException - if so.

startServer

public static org.apache.sshd.SshServer startServer(SshdServerMock server)
                                             throws java.io.IOException
Starts a ssh server on the standard Gerrit port.

Returns:
the server.
Throws:
java.io.IOException - if so.
See Also:
GERRIT_SSH_PORT

generateKeyPair

public static java.io.File generateKeyPair()
                                    throws java.io.IOException,
                                           java.lang.InterruptedException
Generates a rsa key-pair in /tmp/jenkins-testkey for use with authenticating the trigger against the mock server.

Returns:
the path to the private key file
Throws:
java.io.IOException - if so.
java.lang.InterruptedException - if interrupted while waiting for ssh-keygen to finish.


Copyright © 2004-2013. All Rights Reserved.