int port = Integer.parseInt(sshPort);
if (sshUser == null
|| ((sshPass == null || sshPass.trim().equals("")) && (sshKeyFile == null || sshKeyFile.trim().equals("")))
|| sshUser.trim().equals("")) {
System.err.println("ssh credentials not present. Tests will be lame");
return new SshClient() {
public void connect() {
}
public void disconnect() {
}
public Payload get(String path) {
if (path.equals("/etc/passwd")) {
return Payloads.newStringPayload("root");
} else if (path.equals(temp.getAbsolutePath())) {
return Payloads.newStringPayload("rabbit");
}
throw new RuntimeException("path " + path + " not stubbed");
}
public ExecResponse exec(String command) {
if (command.equals("hostname")) {
return new ExecResponse(sshHost, "", 0);
}
throw new RuntimeException("command " + command + " not stubbed");
}
@Override
public void put(String path, Payload contents) {
}
@Override
public String getHostAddress() {
return null;
}
@Override
public String getUsername() {
return null;
}
@Override
public void put(String path, String contents) {
}
@Override
public ExecChannel execChannel(String command) {
if (command.equals("hostname")) {
return new ExecChannel(new ByteArrayOutputStream(), new ByteArrayInputStream(sshHost.getBytes()),
new ByteArrayInputStream(new byte[] {}), Suppliers.ofInstance(0), new Closeable() {
@Override
public void close() {
}
});
}
throw new RuntimeException("command " + command + " not stubbed");
}
};
} else {
Injector i = Guice.createInjector(new JschSshClientModule(), new SLF4JLoggingModule());
SshClient.Factory factory = i.getInstance(SshClient.Factory.class);
SshClient connection;
if (Strings.emptyToNull(sshKeyFile) != null) {
connection = factory.create(HostAndPort.fromParts(sshHost, port), LoginCredentials.builder().user(sshUser)
.privateKey(Files.toString(new File(sshKeyFile), Charsets.UTF_8)).build());
} else {
connection = factory.create(HostAndPort.fromParts(sshHost, port),
LoginCredentials.builder().user(sshUser).password(sshPass).build());
}
connection.connect();
return connection;
}
}