All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Resources.FitNesseRoot.PlugIns.SshFixture.UserGuide.Customization.CustomSession.wiki Maven / Gradle / Ivy

The newest version!
---
Static
---
!1 !-SshFixture-! custom session
Custom session can be implemented to customize ssh sessions.

!2 Custom session example
In this example is defined a custom session implemented with Apache MINA SSHD.

!3 Custom session implementation
Create the following class, compile it and make it available in the class-path

{{{!-
package dev.my.custom.processor;

import com.github.gun88.fitnesse.fixture.ssh.endpoint.Endpoint;
import com.github.gun88.fitnesse.fixture.ssh.option.Options;
import com.github.gun88.fitnesse.fixture.ssh.result.ExecutionResult;
import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.channel.ClientChannel;
import org.apache.sshd.client.channel.ClientChannelEvent;
import org.apache.sshd.client.session.ClientSession;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.EnumSet;
import java.util.concurrent.TimeUnit;

public class MinaSession implements SshSession {

    private ClientSession session;
    private SshClient client;

    @Override
    public void open(Endpoint endpoint, Options options) throws IOException {
        client = SshClient.setUpDefaultClient();
        client.start();
        session = client.connect(endpoint.getUsername(), endpoint.getHost(), endpoint.getPort())
                .verify(options.getConnectionTimeout(), TimeUnit.MILLISECONDS)
                .getSession();
        session.addPasswordIdentity(endpoint.getPassword());
        session.auth().verify(options.getConnectionTimeout(), TimeUnit.MILLISECONDS);
    }

    @Override
    public void close() throws IOException {
        session.close();
        client.stop();
    }

    @Override
    public ExecutionResult execute(String command) throws IOException {
        ExecutionResult executionResult = new ExecutionResult();
        try (ByteArrayOutputStream responseStream = new ByteArrayOutputStream();
             ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
             ClientChannel channel = session.createExecChannel(command)) {
            channel.setOut(responseStream);
            channel.setErr(errorStream);
            try {
                channel.open().verify(2, TimeUnit.SECONDS);
                channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), TimeUnit.SECONDS.toMillis(3));
                executionResult.setOutput(new String(responseStream.toByteArray()));
                executionResult.setError(new String(errorStream.toByteArray()));
                executionResult.setExitCode(channel.getExitStatus());
            } finally {
                channel.close(false);
            }
        }
        return executionResult;
    }

    @Override
    public ExecutionResult download(String source, String destination) throws IOException {
        throw new RuntimeException("Not implemented");
    }

    @Override
    public ExecutionResult upload(String source, String destination) throws IOException {
        throw new RuntimeException("Not implemented");
    }
}
-!}}}

!3 Create test
In this test results are same as default but, under the hood, the implementation is Apache MINA SSHD. The class-name for the custom session must be defined via the SESSION_CLASS option.

|import                               |
|com.github.gun88.fitnesse.fixture.ssh|

|script    |ssh client        |foo/[email protected]                                            |
|add option|CONNECTION_TIMEOUT|with value|3000                                                     |
|add option|SESSION_CLASS     |with value|com.github.gun88.fitnesse.fixture.ssh.session.MinaSession|
|open connection                                                                                   |
|execute   |echo foo; echo bar >&2; exit 234                                                       |
|check     |output            |foo                                                                 |
|check     |error             |bar                                                                 |
|check     |exit code         |234                                                                 |
|close connection                                                                                  |

Note: remember to add compiled code to classpath

!***> code
{{{!-
|import                               |
|com.github.gun88.fitnesse.fixture.ssh|

|script    |ssh client        |foo/[email protected]                                            |
|add option|CONNECTION_TIMEOUT|with value|3000                                                     |
|add option|SESSION_CLASS     |with value|com.github.gun88.fitnesse.fixture.ssh.session.MinaSession|
|open connection                                                                                   |
|execute   |echo foo; echo bar >&2; exit 234                                                       |
|check     |output            |foo                                                                 |
|check     |error             |bar                                                                 |
|check     |exit code         |234                                                                 |
|close connection                                                                                  |

!define TEST_SYSTEM {slim}
!path ${jsch.jar.path}
!path ${sshFixture.jar.path}
!path /path/to/your/code/containing/customized/output/processor
-!}}}
Note: if !-SshFixture-! is not installed as a plugin, remember to set or replace properties: jsch.jar.path, sshFixture.jar.path; with actual paths.

Note: In this example the connection was built via username and password, that are set in plain text in table header. Click [[here][




© 2015 - 2025 Weber Informatics LLC | Privacy Policy