Groovy Testcase with SSH Mock

package eu.christophburmeister.playground

class SshApp {

    private static final long SSH_TIMEOUT = 10000

    public String execute(String user, String host, int port, boolean strictHostKeyChecking, String command){
        String cmd

        if (strictHostKeyChecking) {
            cmd = "ssh ${user}@${host} -p ${port} '${command}'"
        } else {
            cmd = "ssh ${user}@${host} -p ${port} -o StrictHostKeyChecking=no ${command}"
        }

        def cmdWithNewShell = ['/bin/sh', '-c', cmd]

        println cmd

        Process process = cmdWithNewShell.execute()
        def outBuffer = new StringBuffer()
        def errBuffer = new StringBuffer()

        process.consumeProcessOutput( outBuffer, errBuffer )
        process.waitForOrKill(SSH_TIMEOUT)

        if( outBuffer.size() > 0 ) return outBuffer.toString()
        if( errBuffer.size() > 0 ) throw new Exception(errBuffer.toString())
    }
}
eu.christophburmeister.playground

import static com.aestasit.ssh.mocks.MockSshServer.*

/** https://github.com/aestasit/groovy-sshd-mock ... Thx guys, nice work 🙂 */
class SshAppTest extends GroovyTestCase{
    private static final String EXPECTED_RESPONSE = '''
            total 20
            drwxr-xr-x 3 1100 1100 4096 Aug  7 16:52 .
            drwxr-xr-x 8 1100 1100 4096 Aug  1 17:53 ..
            drwxr-xr-x 3 1100 1100 4096 Aug  7 16:49 examples
            '''.stripIndent()
    private static final String EXPECTED_SUBSTRING = 'Unknown command: cat /etc/hosts'
    private static final String USER = 'christoph'
    private static final String HOST = 'localhost'
    private static final int SSH_PORT = 2323
    private static final boolean HOST_KEY_CHECK = false
    private static final String COMMAND_POSITIVE = 'ls -la'
    private static final String COMMAND_NEGATIVE = 'cat /etc/hosts'
    private SshApp cut;

    protected void setUp() {
        cut = new SshApp()
        command('^ls.*$') { inp, out, err, callback, env ->
            out << EXPECTED_RESPONSE
            callback.onExit(0)
        }
        startSshd(2323)
    }

    protected void tearDown() {
        stopSshd()
    }

    /** the command 'ls' is available in the ssh server's expectation */
    void testExecutePositive(){
        String actualResponse_positive = cut.execute(USER, HOST, SSH_PORT, HOST_KEY_CHECK, COMMAND_POSITIVE)
        assertEquals(EXPECTED_RESPONSE, actualResponse_positive)
    }

    /** the command 'cat' is not (!) available in the ssh server's expectation */
    void testExecuteNegative(){
        String excMsg = shouldFail(){
            cut.execute(USER, HOST, SSH_PORT, HOST_KEY_CHECK, COMMAND_NEGATIVE)
        }
        assertTrue(excMsg.contains(EXPECTED_SUBSTRING))
    }
}
group 'eu.christophburmeister.playground'
version '1.0-SNAPSHOT'

apply plugin: 'groovy'

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.11'
    testCompile group: 'junit', name: 'junit', version: '4.11'
    testCompile group: 'com.aestasit.infrastructure.mock', name: 'groovy-sshd-mock', version: '0.3'
}