PageRenderTime 794ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/sshj/src/test/java/org/jclouds/sshj/SshjSshClientLiveTest.java

https://gitlab.com/evgenyg/jclouds
Java | 190 lines | 147 code | 24 blank | 19 comment | 18 complexity | 06aca45566e22cee388991901058aa92 MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.jclouds.sshj;
  18. import static org.testng.Assert.assertEquals;
  19. import java.io.ByteArrayInputStream;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.Closeable;
  22. import java.io.File;
  23. import java.io.FileNotFoundException;
  24. import java.io.IOException;
  25. import java.io.PrintStream;
  26. import java.net.InetAddress;
  27. import org.jclouds.compute.domain.ExecChannel;
  28. import org.jclouds.compute.domain.ExecResponse;
  29. import org.jclouds.domain.LoginCredentials;
  30. import org.jclouds.io.Payload;
  31. import org.jclouds.io.Payloads;
  32. import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
  33. import org.jclouds.ssh.SshClient;
  34. import org.jclouds.sshj.config.SshjSshClientModule;
  35. import org.jclouds.util.Closeables2;
  36. import org.jclouds.util.Strings2;
  37. import org.testng.annotations.BeforeGroups;
  38. import org.testng.annotations.Test;
  39. import com.google.common.base.Charsets;
  40. import com.google.common.base.Strings;
  41. import com.google.common.base.Suppliers;
  42. import com.google.common.io.Files;
  43. import com.google.common.net.HostAndPort;
  44. import com.google.inject.Guice;
  45. import com.google.inject.Injector;
  46. /**
  47. * Tests the ability of a {@link SshjSshClient}
  48. */
  49. @Test(groups = "live", testName = "SshjSshClientLiveTest")
  50. public class SshjSshClientLiveTest {
  51. protected static final String sshHost = System.getProperty("test.ssh.host", "localhost");
  52. protected static final String sshPort = System.getProperty("test.ssh.port", "22");
  53. protected static final String sshUser = System.getProperty("test.ssh.username");
  54. protected static final String sshPass = System.getProperty("test.ssh.password");
  55. protected static final String sshKeyFile = System.getProperty("test.ssh.keyfile");
  56. private File temp;
  57. @BeforeGroups(groups = { "live" })
  58. public SshClient setupClient() throws NumberFormatException, FileNotFoundException, IOException {
  59. int port = Integer.parseInt(sshPort);
  60. if (sshUser == null
  61. || ((sshPass == null || sshPass.trim().equals("")) && (sshKeyFile == null || sshKeyFile.trim()
  62. .equals(""))) || sshUser.trim().equals("")) {
  63. System.err.println("ssh credentials not present. Tests will be lame");
  64. return new SshClient() {
  65. public void connect() {
  66. }
  67. public void disconnect() {
  68. }
  69. public Payload get(String path) {
  70. if (path.equals("/etc/passwd")) {
  71. return Payloads.newStringPayload("root");
  72. } else if (path.equals(temp.getAbsolutePath())) {
  73. return Payloads.newStringPayload("rabbit");
  74. }
  75. throw new RuntimeException("path " + path + " not stubbed");
  76. }
  77. public ExecResponse exec(String command) {
  78. if (command.equals("hostname")) {
  79. return new ExecResponse(sshHost, "", 0);
  80. }
  81. throw new RuntimeException("command " + command + " not stubbed");
  82. }
  83. @Override
  84. public void put(String path, Payload contents) {
  85. }
  86. @Override
  87. public String getHostAddress() {
  88. return null;
  89. }
  90. @Override
  91. public String getUsername() {
  92. return null;
  93. }
  94. @Override
  95. public void put(String path, String contents) {
  96. }
  97. @Override
  98. public ExecChannel execChannel(String command) {
  99. if (command.equals("hostname")) {
  100. return new ExecChannel(new ByteArrayOutputStream(), new ByteArrayInputStream(sshHost.getBytes()),
  101. new ByteArrayInputStream(new byte[] {}), Suppliers.ofInstance(0), new Closeable() {
  102. @Override
  103. public void close() {
  104. }
  105. });
  106. }
  107. throw new RuntimeException("command " + command + " not stubbed");
  108. }
  109. };
  110. } else {
  111. Injector i = Guice.createInjector(new SshjSshClientModule(), new SLF4JLoggingModule());
  112. SshClient.Factory factory = i.getInstance(SshClient.Factory.class);
  113. SshClient connection;
  114. if (Strings.emptyToNull(sshKeyFile) != null) {
  115. connection = factory.create(HostAndPort.fromParts(sshHost, port), LoginCredentials.builder().user(sshUser)
  116. .privateKey(Files.toString(new File(sshKeyFile), Charsets.UTF_8)).build());
  117. } else {
  118. connection = factory.create(HostAndPort.fromParts(sshHost, port),
  119. LoginCredentials.builder().user(sshUser).password(sshPass).build());
  120. }
  121. connection.connect();
  122. return connection;
  123. }
  124. }
  125. public void testPutAndGet() throws IOException {
  126. temp = File.createTempFile("foo", "bar");
  127. try {
  128. SshClient client = setupClient();
  129. client.put(temp.getAbsolutePath(), Payloads.newStringPayload("rabbit"));
  130. Payload input = client.get(temp.getAbsolutePath());
  131. String contents = Strings2.toStringAndClose(input.openStream());
  132. assertEquals(contents, "rabbit");
  133. } finally {
  134. temp.delete();
  135. }
  136. }
  137. public void testGetEtcPassword() throws IOException {
  138. Payload input = setupClient().get("/etc/passwd");
  139. String contents = Strings2.toStringAndClose(input.openStream());
  140. assert contents.indexOf("root") >= 0 : "no root in " + contents;
  141. }
  142. public void testExecHostname() throws IOException, InterruptedException {
  143. SshClient client = setupClient();
  144. ExecResponse response = client.exec("hostname");
  145. assertEquals(response.getError(), "");
  146. assertEquals(response.getOutput().trim(), "localhost".equals(sshHost) ? InetAddress.getLocalHost().getHostName()
  147. : sshHost);
  148. }
  149. public void testExecChannelTakesStdinAndNoEchoOfCharsInOuputAndOutlivesClient() throws IOException {
  150. SshClient client = setupClient();
  151. ExecChannel response = client.execChannel("cat <<EOF");
  152. client.disconnect();
  153. assertEquals(response.getExitStatus().get(), null);
  154. try {
  155. PrintStream printStream = new PrintStream(response.getInput());
  156. printStream.append("foo\n");
  157. printStream.append("EOF\n");
  158. printStream.close();
  159. assertEquals(Strings2.toStringAndClose(response.getError()), "");
  160. assertEquals(Strings2.toStringAndClose(response.getOutput()), "");
  161. } finally {
  162. Closeables2.closeQuietly(response);
  163. }
  164. assertEquals(response.getExitStatus().get(), Integer.valueOf(0));
  165. }
  166. }