Statistics
| Branch: | Revision:

root / extensions / ssh / jsch / src / test / java / org / jclouds / ssh / jsch / JschSshClientLiveTest.java @ 35e7942d

History | View | Annotate | Download (5.4 kB)

1
/**
2
 *
3
 * Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
4
 *
5
 * ====================================================================
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * 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, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 * ====================================================================
18
 */
19
package org.jclouds.ssh.jsch;
20

    
21
import static org.testng.Assert.assertEquals;
22

    
23
import java.io.File;
24
import java.io.FileInputStream;
25
import java.io.FileNotFoundException;
26
import java.io.IOException;
27
import java.io.InputStream;
28
import java.net.InetAddress;
29
import java.net.InetSocketAddress;
30
import java.net.UnknownHostException;
31

    
32
import org.jclouds.ssh.ExecResponse;
33
import org.jclouds.ssh.SshClient;
34
import org.jclouds.ssh.jsch.config.JschSshClientModule;
35
import org.jclouds.util.Utils;
36
import org.testng.annotations.BeforeGroups;
37
import org.testng.annotations.Test;
38

    
39
import com.google.inject.Guice;
40
import com.google.inject.Injector;
41

    
42
/**
43
 * Tests the ability of a {@link JschSshClient}
44
 * 
45
 * @author Adrian Cole
46
 */
47
@Test(groups = "live", testName = "ssh.JschSshClientLiveTest")
48
public class JschSshClientLiveTest {
49
   protected static final String sshHost = System.getProperty("jclouds.test.ssh.host");
50
   protected static final String sshPort = System.getProperty("jclouds.test.ssh.port");
51
   protected static final String sshUser = System.getProperty("jclouds.test.ssh.username");
52
   protected static final String sshPass = System.getProperty("jclouds.test.ssh.password");
53
   protected static final String sshKeyFile = System.getProperty("jclouds.test.ssh.keyfile");
54
   private File temp;
55

    
56
   @BeforeGroups(groups = { "live" })
57
   public SshClient setupClient() throws NumberFormatException, FileNotFoundException, IOException {
58
      int port = (sshPort != null) ? Integer.parseInt(sshPort) : 22;
59
      InetAddress host = (sshHost != null) ? InetAddress.getByName(sshHost) : InetAddress
60
               .getLocalHost();
61
      if (sshUser == null
62
               || ((sshPass == null || sshPass.trim().equals("")) && (sshKeyFile == null || sshKeyFile
63
                        .trim().equals(""))) || sshUser.trim().equals("")) {
64
         System.err.println("ssh credentials not present.  Tests will be lame");
65
         return new SshClient() {
66

    
67
            public void connect() {
68
            }
69

    
70
            public void disconnect() {
71
            }
72

    
73
            public InputStream get(String path) {
74
               if (path.equals("/etc/passwd")) {
75
                  return Utils.toInputStream("root");
76
               } else if (path.equals(temp.getAbsolutePath())) {
77
                  return Utils.toInputStream("rabbit");
78
               }
79
               throw new RuntimeException("path " + path + " not stubbed");
80
            }
81

    
82
            public ExecResponse exec(String command) {
83
               if (command.equals("hostname")) {
84
                  try {
85
                     return new ExecResponse(InetAddress.getLocalHost().getHostName(), "", 0);
86
                  } catch (UnknownHostException e) {
87
                     throw new RuntimeException(e);
88
                  }
89
               }
90
               throw new RuntimeException("command " + command + " not stubbed");
91
            }
92

    
93
            @Override
94
            public void put(String path, InputStream contents) {
95

    
96
            }
97

    
98
            @Override
99
            public String getHostAddress() {
100
               return null;
101
            }
102

    
103
            @Override
104
            public String getUsername() {
105
               return null;
106
            }
107

    
108
         };
109
      } else {
110
         Injector i = Guice.createInjector(new JschSshClientModule());
111
         SshClient.Factory factory = i.getInstance(SshClient.Factory.class);
112
         SshClient connection;
113
         if (sshKeyFile != null && !sshKeyFile.trim().equals("")) {
114
            connection = factory.create(new InetSocketAddress(host, port), sshUser, Utils
115
                     .toStringAndClose(new FileInputStream(sshKeyFile)).getBytes());
116
         } else {
117
            connection = factory.create(new InetSocketAddress(host, port), sshUser, sshPass);
118
         }
119
         connection.connect();
120
         return connection;
121
      }
122
   }
123

    
124
   public void testPutAndGet() throws IOException {
125
      temp = File.createTempFile("foo", "bar");
126
      temp.deleteOnExit();
127
      SshClient client = setupClient();
128
      client.put(temp.getAbsolutePath(), Utils.toInputStream("rabbit"));
129
      InputStream input = setupClient().get(temp.getAbsolutePath());
130
      String contents = Utils.toStringAndClose(input);
131
      assertEquals(contents, "rabbit");
132
   }
133

    
134
   public void testGetEtcPassword() throws IOException {
135
      InputStream input = setupClient().get("/etc/passwd");
136
      String contents = Utils.toStringAndClose(input);
137
      assert contents.indexOf("root") >= 0 : "no root in " + contents;
138
   }
139

    
140
   public void testExecHostname() throws IOException {
141
      ExecResponse response = setupClient().exec("hostname");
142
      assertEquals(response.getError(), "");
143
      assertEquals(response.getOutput().trim(), InetAddress.getLocalHost().getHostName());
144
   }
145

    
146
}