-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
SftpContainerTest.java
52 lines (47 loc) · 1.98 KB
/
SftpContainerTest.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
package org.example;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import org.junit.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.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
public class SftpContainerTest {
@Test
public void test() throws Exception {
try (
GenericContainer<?> sftp = new GenericContainer<>("atmoz/sftp:alpine-3.7")
.withCopyFileToContainer(
MountableFile.forClasspathResource("testcontainers/", 0777),
"/home/foo/upload/testcontainers"
)
.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");
jschSession.setConfig("StrictHostKeyChecking", "no");
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"));
}
}
}