forked from testcontainers/testcontainers-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSftpContainerTest.java
More file actions
61 lines (56 loc) · 2.53 KB
/
SftpContainerTest.java
File metadata and controls
61 lines (56 loc) · 2.53 KB
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
package org.example;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.HostKey;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.MountableFile;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
class SftpContainerTest {
@Test
void test() throws Exception {
try (
GenericContainer<?> sftp = new GenericContainer<>("jmcombs/sftp:alpine")
.withCopyFileToContainer(
MountableFile.forClasspathResource("testcontainers/", 0777),
"/home/foo/upload/testcontainers"
)
.withCopyFileToContainer(
MountableFile.forClasspathResource("./ssh_host_ed25519_key", 0400),
"/etc/ssh/ssh_host_ed25519_key"
)
.withExposedPorts(22)
.withCommand("foo:pass:::upload")
) {
sftp.start();
JSch jsch = new JSch();
Session jschSession = jsch.getSession("foo", sftp.getHost(), sftp.getMappedPort(22));
jschSession.setPassword("pass");
// hostKeyString is string starting with AAAA from file known_hosts or ssh_host_*_key.pub
String hostKeyString = "AAAAC3NzaC1lZDI1NTE5AAAAINaBuegbLGHOgpXCePq80uY79Xw716jWXAwWjRdFYi53";
HostKey hostKey = new HostKey(sftp.getHost(), Base64.getDecoder().decode(hostKeyString));
jschSession.getHostKeyRepository().add(hostKey, null);
jschSession.connect();
ChannelSftp channel = (ChannelSftp) jschSession.openChannel("sftp");
channel.connect();
assertThat(channel.ls("/upload/testcontainers")).anyMatch(item -> item.toString().contains("file.txt"));
assertThat(
new BufferedReader(
new InputStreamReader(channel.get("/upload/testcontainers/file.txt"), StandardCharsets.UTF_8)
)
.lines()
.collect(Collectors.joining("\n"))
)
.contains("Testcontainers");
channel.rm("/upload/testcontainers/file.txt");
assertThat(channel.ls("/upload/testcontainers/"))
.noneMatch(item -> item.toString().contains("testcontainers/file.txt"));
}
}
}