-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractIntegrationTest.java
65 lines (51 loc) · 2.39 KB
/
AbstractIntegrationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.antonzhdanov.apache.sshd.agent.cloud;
import com.antonzhdanov.apache.sshd.agent.CloudSshAgentFactory;
import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.session.ClientSession;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.security.PublicKey;
import java.time.Duration;
import static com.antonzhdanov.apache.sshd.agent.cloud.TestUtils.readPublicKey;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
public abstract class AbstractIntegrationTest<K extends CloudKeyInfo> {
private static final String ECHO_STRING = String.valueOf(System.currentTimeMillis());
private final SshClient sshClient = SshClient.setUpDefaultClient();
private CloudSshAgentFactory<K> cloudFactory;
@BeforeClass
public void init() throws Exception {
cloudFactory = createCloudFactory();
sshClient.setAgentFactory(cloudFactory);
sshClient.start();
}
@AfterClass
public void close() throws Exception {
sshClient.close();
}
@Test(dataProvider = "testData")
public void testAuthSucceeded(Object publicKeyObj, K keyInfo) throws Exception {
PublicKey publicKey;
if (publicKeyObj instanceof PublicKey) {
publicKey = (PublicKey) publicKeyObj;
} else if (publicKeyObj instanceof String){
publicKey = readPublicKey((String) publicKeyObj);
} else {
throw new RuntimeException("Unknown argument type " + publicKeyObj.getClass().getName());
}
try (OpenSshServerContainer container = new OpenSshServerContainer(publicKey)) {
assertTrue(container.isRunning(), "Open SSH Server container did not start");
try (ClientSession session = sshClient.connect("user", "localhost", container.getFirstMappedPort())
.verify(Duration.ofSeconds(5)).getSession()) {
try (var unused = cloudFactory.withKeyInfo(session, keyInfo)) {
session.auth().verify(Duration.ofSeconds(10));
}
assertEquals(ECHO_STRING,
session.executeRemoteCommand("echo " + ECHO_STRING).replace("\n", ""));
}
}
}
protected abstract Object[][] testData();
protected abstract CloudSshAgentFactory<K> createCloudFactory() throws Exception;
}