PageRenderTime 99ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/jsch/src/test/java/org/jclouds/ssh/jsch/JschSshClientLiveTest.java

https://github.com/regularfry/jclouds
Java | 145 lines | 101 code | 21 blank | 23 comment | 18 complexity | 082f74e7b730ca0ccb2776a12af97627 MD5 | raw file
  1. /**
  2. * Licensed to jclouds, Inc. (jclouds) under one or more
  3. * contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. jclouds licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.jclouds.ssh.jsch;
  20. import static org.testng.Assert.assertEquals;
  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.FileNotFoundException;
  24. import java.io.IOException;
  25. import org.jclouds.compute.domain.ExecResponse;
  26. import org.jclouds.domain.Credentials;
  27. import org.jclouds.io.Payload;
  28. import org.jclouds.io.Payloads;
  29. import org.jclouds.net.IPSocket;
  30. import org.jclouds.ssh.SshClient;
  31. import org.jclouds.ssh.jsch.config.JschSshClientModule;
  32. import org.jclouds.util.Strings2;
  33. import org.testng.annotations.BeforeGroups;
  34. import org.testng.annotations.Test;
  35. import com.google.inject.Guice;
  36. import com.google.inject.Injector;
  37. /**
  38. * Tests the ability of a {@link JschSshClient}
  39. *
  40. * @author Adrian Cole
  41. */
  42. @Test(groups = "live")
  43. public class JschSshClientLiveTest {
  44. protected static final String sshHost = System.getProperty("test.ssh.host", "localhost");
  45. protected static final String sshPort = System.getProperty("test.ssh.port", "22");
  46. protected static final String sshUser = System.getProperty("test.ssh.username");
  47. protected static final String sshPass = System.getProperty("test.ssh.password");
  48. protected static final String sshKeyFile = System.getProperty("test.ssh.keyfile");
  49. private File temp;
  50. @BeforeGroups(groups = { "live" })
  51. public SshClient setupClient() throws NumberFormatException, FileNotFoundException, IOException {
  52. int port = Integer.parseInt(sshPort);
  53. if (sshUser == null
  54. || ((sshPass == null || sshPass.trim().equals("")) && (sshKeyFile == null || sshKeyFile.trim().equals("")))
  55. || sshUser.trim().equals("")) {
  56. System.err.println("ssh credentials not present. Tests will be lame");
  57. return new SshClient() {
  58. public void connect() {
  59. }
  60. public void disconnect() {
  61. }
  62. public Payload get(String path) {
  63. if (path.equals("/etc/passwd")) {
  64. return Payloads.newStringPayload("root");
  65. } else if (path.equals(temp.getAbsolutePath())) {
  66. return Payloads.newStringPayload("rabbit");
  67. }
  68. throw new RuntimeException("path " + path + " not stubbed");
  69. }
  70. public ExecResponse exec(String command) {
  71. if (command.equals("hostname")) {
  72. return new ExecResponse(sshHost, "", 0);
  73. }
  74. throw new RuntimeException("command " + command + " not stubbed");
  75. }
  76. @Override
  77. public void put(String path, Payload contents) {
  78. }
  79. @Override
  80. public String getHostAddress() {
  81. return null;
  82. }
  83. @Override
  84. public String getUsername() {
  85. return null;
  86. }
  87. @Override
  88. public void put(String path, String contents) {
  89. }
  90. };
  91. } else {
  92. Injector i = Guice.createInjector(new JschSshClientModule());
  93. SshClient.Factory factory = i.getInstance(SshClient.Factory.class);
  94. SshClient connection;
  95. if (sshKeyFile != null && !sshKeyFile.trim().equals("")) {
  96. connection = factory.create(new IPSocket(sshHost, port),
  97. new Credentials(sshUser, Strings2.toStringAndClose(new FileInputStream(sshKeyFile))));
  98. } else {
  99. connection = factory.create(new IPSocket(sshHost, port), new Credentials(sshUser, sshPass));
  100. }
  101. connection.connect();
  102. return connection;
  103. }
  104. }
  105. public void testPutAndGet() throws IOException {
  106. temp = File.createTempFile("foo", "bar");
  107. temp.deleteOnExit();
  108. SshClient client = setupClient();
  109. client.put(temp.getAbsolutePath(), Payloads.newStringPayload("rabbit"));
  110. Payload input = setupClient().get(temp.getAbsolutePath());
  111. String contents = Strings2.toStringAndClose(input.getInput());
  112. assertEquals(contents, "rabbit");
  113. }
  114. public void testGetEtcPassword() throws IOException {
  115. Payload input = setupClient().get("/etc/passwd");
  116. String contents = Strings2.toStringAndClose(input.getInput());
  117. assert contents.indexOf("root") >= 0 : "no root in " + contents;
  118. }
  119. public void testExecHostname() throws IOException {
  120. ExecResponse response = setupClient().exec("hostname");
  121. assertEquals(response.getError(), "");
  122. assertEquals(response.getOutput().trim(), sshHost);
  123. }
  124. }