|
@@ -17,18 +17,35 @@
|
|
|
*/
|
|
|
package org.apache.hadoop.fs.ftp;
|
|
|
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.util.Comparator;
|
|
|
+
|
|
|
import com.google.common.base.Preconditions;
|
|
|
import org.apache.commons.net.ftp.FTP;
|
|
|
-
|
|
|
import org.apache.commons.net.ftp.FTPClient;
|
|
|
import org.apache.commons.net.ftp.FTPFile;
|
|
|
+import org.apache.ftpserver.usermanager.impl.BaseUser;
|
|
|
+import org.apache.ftpserver.usermanager.impl.WritePermission;
|
|
|
import org.apache.hadoop.conf.Configuration;
|
|
|
+import org.apache.hadoop.fs.FSDataInputStream;
|
|
|
+import org.apache.hadoop.fs.FSDataOutputStream;
|
|
|
+import org.apache.hadoop.fs.FileSystem;
|
|
|
+import org.apache.hadoop.fs.Path;
|
|
|
import org.apache.hadoop.fs.permission.FsAction;
|
|
|
+import org.apache.hadoop.io.IOUtils;
|
|
|
+import org.apache.hadoop.test.GenericTestUtils;
|
|
|
+import org.apache.hadoop.test.LambdaTestUtils;
|
|
|
+import org.junit.After;
|
|
|
+import org.junit.Before;
|
|
|
import org.junit.Rule;
|
|
|
import org.junit.Test;
|
|
|
import org.junit.rules.Timeout;
|
|
|
|
|
|
-
|
|
|
+import static org.hamcrest.CoreMatchers.equalTo;
|
|
|
+import static org.hamcrest.MatcherAssert.assertThat;
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
|
/**
|
|
@@ -37,9 +54,72 @@ import static org.junit.Assert.assertEquals;
|
|
|
*/
|
|
|
public class TestFTPFileSystem {
|
|
|
|
|
|
+ private FtpTestServer server;
|
|
|
+
|
|
|
@Rule
|
|
|
public Timeout testTimeout = new Timeout(180000);
|
|
|
|
|
|
+ @Before
|
|
|
+ public void setUp() throws Exception {
|
|
|
+ server = new FtpTestServer(GenericTestUtils.getTestDir().toPath()).start();
|
|
|
+ }
|
|
|
+
|
|
|
+ @After
|
|
|
+ @SuppressWarnings("ResultOfMethodCallIgnored")
|
|
|
+ public void tearDown() throws Exception {
|
|
|
+ if (server != null) {
|
|
|
+ server.stop();
|
|
|
+ Files.walk(server.getFtpRoot())
|
|
|
+ .sorted(Comparator.reverseOrder())
|
|
|
+ .map(java.nio.file.Path::toFile)
|
|
|
+ .forEach(File::delete);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testCreateWithWritePermissions() throws Exception {
|
|
|
+ BaseUser user = server.addUser("test", "password", new WritePermission());
|
|
|
+ Configuration configuration = new Configuration();
|
|
|
+ configuration.set("fs.defaultFS", "ftp:///");
|
|
|
+ configuration.set("fs.ftp.host", "localhost");
|
|
|
+ configuration.setInt("fs.ftp.host.port", server.getPort());
|
|
|
+ configuration.set("fs.ftp.user.localhost", user.getName());
|
|
|
+ configuration.set("fs.ftp.password.localhost", user.getPassword());
|
|
|
+ configuration.setBoolean("fs.ftp.impl.disable.cache", true);
|
|
|
+
|
|
|
+ FileSystem fs = FileSystem.get(configuration);
|
|
|
+ byte[] bytesExpected = "hello world".getBytes(StandardCharsets.UTF_8);
|
|
|
+ try (FSDataOutputStream outputStream = fs.create(new Path("test1.txt"))) {
|
|
|
+ outputStream.write(bytesExpected);
|
|
|
+ }
|
|
|
+ try (FSDataInputStream input = fs.open(new Path("test1.txt"))) {
|
|
|
+ assertThat(bytesExpected, equalTo(IOUtils.readFullyToByteArray(input)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testCreateWithoutWritePermissions() throws Exception {
|
|
|
+ BaseUser user = server.addUser("test", "password");
|
|
|
+ Configuration configuration = new Configuration();
|
|
|
+ configuration.set("fs.defaultFS", "ftp:///");
|
|
|
+ configuration.set("fs.ftp.host", "localhost");
|
|
|
+ configuration.setInt("fs.ftp.host.port", server.getPort());
|
|
|
+ configuration.set("fs.ftp.user.localhost", user.getName());
|
|
|
+ configuration.set("fs.ftp.password.localhost", user.getPassword());
|
|
|
+ configuration.setBoolean("fs.ftp.impl.disable.cache", true);
|
|
|
+
|
|
|
+ FileSystem fs = FileSystem.get(configuration);
|
|
|
+ byte[] bytesExpected = "hello world".getBytes(StandardCharsets.UTF_8);
|
|
|
+ LambdaTestUtils.intercept(
|
|
|
+ IOException.class, "Unable to create file: test1.txt, Aborting",
|
|
|
+ () -> {
|
|
|
+ try (FSDataOutputStream out = fs.create(new Path("test1.txt"))) {
|
|
|
+ out.write(bytesExpected);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
@Test
|
|
|
public void testFTPDefaultPort() throws Exception {
|
|
|
FTPFileSystem ftp = new FTPFileSystem();
|