|
| 1 | +package org.apache.hadoop.hdfs.server.federation.router.async; |
| 2 | + |
| 3 | +import org.apache.hadoop.crypto.CryptoProtocolVersion; |
| 4 | +import org.apache.hadoop.fs.CreateFlag; |
| 5 | +import org.apache.hadoop.fs.FsServerDefaults; |
| 6 | +import org.apache.hadoop.fs.permission.AclEntry; |
| 7 | +import org.apache.hadoop.fs.permission.FsPermission; |
| 8 | +import org.apache.hadoop.hdfs.protocol.DatanodeInfo; |
| 9 | +import org.apache.hadoop.hdfs.protocol.DirectoryListing; |
| 10 | +import org.apache.hadoop.hdfs.protocol.HdfsConstants; |
| 11 | +import org.apache.hadoop.hdfs.protocol.HdfsFileStatus; |
| 12 | +import org.apache.hadoop.hdfs.server.federation.router.RouterClientProtocol; |
| 13 | +import org.apache.hadoop.io.EnumSetWritable; |
| 14 | +import org.apache.hadoop.util.Lists; |
| 15 | +import org.junit.Before; |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.EnumSet; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import static org.apache.hadoop.crypto.CryptoProtocolVersion.ENCRYPTION_ZONES; |
| 23 | +import static org.apache.hadoop.fs.permission.AclEntryScope.DEFAULT; |
| 24 | +import static org.apache.hadoop.fs.permission.AclEntryType.USER; |
| 25 | +import static org.apache.hadoop.fs.permission.FsAction.ALL; |
| 26 | +import static org.apache.hadoop.fs.permission.FsAction.NONE; |
| 27 | +import static org.apache.hadoop.fs.permission.FsAction.READ; |
| 28 | +import static org.apache.hadoop.fs.permission.FsAction.READ_WRITE; |
| 29 | +import static org.apache.hadoop.hdfs.server.federation.router.async.utils.AsyncUtil.syncReturn; |
| 30 | +import static org.apache.hadoop.hdfs.server.namenode.AclTestHelpers.aclEntry; |
| 31 | +import static org.junit.Assert.assertEquals; |
| 32 | +import static org.junit.Assert.assertTrue; |
| 33 | + |
| 34 | +/** |
| 35 | + * Used to test the functionality of {@link RouterAsyncClientProtocol}. |
| 36 | + */ |
| 37 | +public class TestRouterAsyncClientProtocol extends RouterAsyncProtocolTestBase { |
| 38 | + private RouterAsyncClientProtocol asyncClientProtocol; |
| 39 | + private RouterClientProtocol clientProtocol; |
| 40 | + private final String testPath = TEST_DIR_PATH + "/test"; |
| 41 | + |
| 42 | + @Before |
| 43 | + public void setup() throws IOException { |
| 44 | + asyncClientProtocol = new RouterAsyncClientProtocol(getRouterConf(), getRouterAsyncRpcServer()); |
| 45 | + clientProtocol = new RouterClientProtocol(getRouterConf(), getRouterRpcServer()); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testGetServerDefaults() throws Exception { |
| 50 | + FsServerDefaults serverDefaults = clientProtocol.getServerDefaults(); |
| 51 | + asyncClientProtocol.getServerDefaults(); |
| 52 | + FsServerDefaults fsServerDefaults = syncReturn(FsServerDefaults.class); |
| 53 | + assertEquals(serverDefaults.getBlockSize(), fsServerDefaults.getBlockSize()); |
| 54 | + assertEquals(serverDefaults.getReplication(), fsServerDefaults.getReplication()); |
| 55 | + assertEquals(serverDefaults.getChecksumType(), fsServerDefaults.getChecksumType()); |
| 56 | + assertEquals( |
| 57 | + serverDefaults.getDefaultStoragePolicyId(), fsServerDefaults.getDefaultStoragePolicyId()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testClientProtocolRpc() throws Exception { |
| 62 | + asyncClientProtocol.mkdirs(testPath, new FsPermission(ALL, ALL, ALL), false); |
| 63 | + Boolean success = syncReturn(Boolean.class); |
| 64 | + assertTrue(success); |
| 65 | + |
| 66 | + asyncClientProtocol.setPermission(testPath, new FsPermission(READ_WRITE, READ, NONE)); |
| 67 | + syncReturn(Void.class); |
| 68 | + |
| 69 | + asyncClientProtocol.getFileInfo(testPath); |
| 70 | + HdfsFileStatus hdfsFileStatus = syncReturn(HdfsFileStatus.class); |
| 71 | + assertEquals(hdfsFileStatus.getPermission(), new FsPermission(READ_WRITE, READ, NONE)); |
| 72 | + |
| 73 | + List<AclEntry> aclSpec = Lists.newArrayList(aclEntry(DEFAULT, USER, "tmpUser", ALL)); |
| 74 | + asyncClientProtocol.setAcl(testPath, aclSpec); |
| 75 | + syncReturn(Void.class); |
| 76 | + asyncClientProtocol.setOwner(testPath, "tmpUser", "tmpUserGroup"); |
| 77 | + syncReturn(Void.class); |
| 78 | + |
| 79 | + asyncClientProtocol.getFileInfo(testPath); |
| 80 | + hdfsFileStatus = syncReturn(HdfsFileStatus.class); |
| 81 | + assertEquals("tmpUser", hdfsFileStatus.getOwner()); |
| 82 | + assertEquals("tmpUserGroup", hdfsFileStatus.getGroup()); |
| 83 | + |
| 84 | + asyncClientProtocol.create(testPath + "/testCreate.file", |
| 85 | + new FsPermission(ALL, ALL, ALL), "testAsyncClient", |
| 86 | + new EnumSetWritable<>(EnumSet.of(CreateFlag.CREATE)), |
| 87 | + false, (short) 1, 128 * 1024 * 1024L, new CryptoProtocolVersion[]{ENCRYPTION_ZONES}, |
| 88 | + null, null); |
| 89 | + hdfsFileStatus = syncReturn(HdfsFileStatus.class); |
| 90 | + assertTrue(hdfsFileStatus.isFile()); |
| 91 | + assertEquals(128 * 1024 * 1024, hdfsFileStatus.getBlockSize()); |
| 92 | + |
| 93 | + asyncClientProtocol.getListing(testPath, new byte[1], true); |
| 94 | + DirectoryListing directoryListing = syncReturn(DirectoryListing.class); |
| 95 | + assertEquals(1, directoryListing.getPartialListing().length); |
| 96 | + |
| 97 | + asyncClientProtocol.getDatanodeReport(HdfsConstants.DatanodeReportType.ALL); |
| 98 | + DatanodeInfo[] datanodeInfos = syncReturn(DatanodeInfo[].class); |
| 99 | + assertEquals(3, datanodeInfos.length); |
| 100 | + |
| 101 | + asyncClientProtocol.createSymlink(testPath + "/testCreate.file", |
| 102 | + "/link/link.file", new FsPermission(ALL, ALL, ALL), true); |
| 103 | + syncReturn(Void.class); |
| 104 | + |
| 105 | + asyncClientProtocol.getFileLinkInfo("/link/link.file"); |
| 106 | + hdfsFileStatus = syncReturn(HdfsFileStatus.class); |
| 107 | + assertEquals("testCreate.file", hdfsFileStatus.getSymlink().getName()); |
| 108 | + |
| 109 | + asyncClientProtocol.rename(testPath + "/testCreate.file", |
| 110 | + testPath + "/testRename.file"); |
| 111 | + success = syncReturn(boolean.class); |
| 112 | + assertTrue(success); |
| 113 | + |
| 114 | + asyncClientProtocol.delete(testPath, true); |
| 115 | + success = syncReturn(boolean.class); |
| 116 | + assertTrue(success); |
| 117 | + } |
| 118 | +} |
0 commit comments