From 84f0d145d7ae5543c6a960a464056b7f24cde33e Mon Sep 17 00:00:00 2001 From: Andrew Purtell Date: Wed, 13 Jul 2022 09:28:30 -0700 Subject: [PATCH] HBASE-27194 Add test coverage for SimpleRpcServer (#4616) Add test coverage for SimpleRpcServer. Improve the way we test both SimpleRpcServer and NettyRpcServer. Use LoadTestKVGenerator to generate random values with varying sizes between 1000 bytes and 1M bytes, and also to verify them when reading the values back. Add secure test coverage for both SimpleRpcServer and NettyRpcServer. Signed-off-by: Duo Zhang Signed-off-by: Viraj Jasani --- .../hadoop/hbase/ipc/TestNettyRpcServer.java | 84 ++++++------- .../hbase/ipc/TestSecureNettyRpcServer.java | 100 ++++++++++++++++ .../hbase/ipc/TestSecureSimpleRpcServer.java | 101 ++++++++++++++++ .../hadoop/hbase/ipc/TestSimpleRpcServer.java | 110 ++++++++++++++++++ 4 files changed, 354 insertions(+), 41 deletions(-) create mode 100644 hbase-server/src/test/java/org/apache/hadoop/hbase/ipc/TestSecureNettyRpcServer.java create mode 100644 hbase-server/src/test/java/org/apache/hadoop/hbase/ipc/TestSecureSimpleRpcServer.java create mode 100644 hbase-server/src/test/java/org/apache/hadoop/hbase/ipc/TestSimpleRpcServer.java diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/ipc/TestNettyRpcServer.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/ipc/TestNettyRpcServer.java index eefadaf528c..6af67e2190d 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/ipc/TestNettyRpcServer.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/ipc/TestNettyRpcServer.java @@ -17,31 +17,32 @@ */ package org.apache.hadoop.hbase.ipc; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.List; import org.apache.hadoop.hbase.HBaseClassTestRule; import org.apache.hadoop.hbase.HBaseTestingUtil; import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.TableNameTestRule; +import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; -import org.apache.hadoop.hbase.client.ResultScanner; -import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.client.TableDescriptor; +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; +import org.apache.hadoop.hbase.regionserver.DisabledRegionSplitPolicy; import org.apache.hadoop.hbase.testclassification.MediumTests; import org.apache.hadoop.hbase.testclassification.RPCTests; import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.LoadTestKVGenerator; import org.junit.After; import org.junit.Before; import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; -import org.junit.rules.TestName; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @@ -54,14 +55,17 @@ public class TestNettyRpcServer { public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(TestNettyRpcServer.class); - @Rule - public TestName name = new TestName(); - private static HBaseTestingUtil TEST_UTIL; + private static final byte[] FAMILY = Bytes.toBytes("f"); + private static final byte[] QUALIFIER = Bytes.toBytes("q"); + private static final int NUM_ROWS = 100; + private static final int MIN_LEN = 1000; + private static final int MAX_LEN = 1000000; + protected static final LoadTestKVGenerator GENERATOR = new LoadTestKVGenerator(MIN_LEN, MAX_LEN); + protected static HBaseTestingUtil TEST_UTIL; + + @Rule + public TableNameTestRule name = new TableNameTestRule(); - private static TableName TABLE; - private static byte[] FAMILY = Bytes.toBytes("f1"); - private static byte[] PRIVATE_COL = Bytes.toBytes("private"); - private static byte[] PUBLIC_COL = Bytes.toBytes("public"); @Parameterized.Parameter public String allocatorType; @@ -74,8 +78,10 @@ public class TestNettyRpcServer { @Before public void setup() throws Exception { - TABLE = TableName.valueOf(name.getMethodName().replace('[', '_').replace(']', '_')); - TEST_UTIL = new HBaseTestingUtil(); + // A subclass may have already created TEST_UTIL and is now upcalling to us + if (TEST_UTIL == null) { + TEST_UTIL = new HBaseTestingUtil(); + } TEST_UTIL.getConfiguration().set(RpcServerFactory.CUSTOM_RPC_SERVER_IMPL_CONF_KEY, NettyRpcServer.class.getName()); TEST_UTIL.getConfiguration().set(NettyRpcServer.HBASE_NETTY_ALLOCATOR_KEY, allocatorType); @@ -89,34 +95,30 @@ public class TestNettyRpcServer { @Test public void testNettyRpcServer() throws Exception { - final Table table = TEST_UTIL.createTable(TABLE, FAMILY); - try { - // put some test data - List puts = new ArrayList(100); - for (int i = 0; i < 100; i++) { - Put p = new Put(Bytes.toBytes(i)); - p.addColumn(FAMILY, PRIVATE_COL, Bytes.toBytes("secret " + i)); - p.addColumn(FAMILY, PUBLIC_COL, Bytes.toBytes("info " + i)); - puts.add(p); - } - table.put(puts); + doTest(name.getTableName()); + } - // read to verify it. - Scan scan = new Scan(); - scan.setCaching(16); - ResultScanner rs = table.getScanner(scan); - int rowcnt = 0; - for (Result r : rs) { - rowcnt++; - int rownum = Bytes.toInt(r.getRow()); - assertTrue(r.containsColumn(FAMILY, PRIVATE_COL)); - assertEquals("secret " + rownum, Bytes.toString(r.getValue(FAMILY, PRIVATE_COL))); - assertTrue(r.containsColumn(FAMILY, PUBLIC_COL)); - assertEquals("info " + rownum, Bytes.toString(r.getValue(FAMILY, PUBLIC_COL))); + protected void doTest(TableName tableName) throws Exception { + // Splitting just complicates the test scenario, disable it + final TableDescriptor desc = TableDescriptorBuilder.newBuilder(tableName) + .setRegionSplitPolicyClassName(DisabledRegionSplitPolicy.class.getName()).build(); + try (Table table = + TEST_UTIL.createTable(desc, new byte[][] { FAMILY }, TEST_UTIL.getConfiguration())) { + // put some test data + for (int i = 0; i < NUM_ROWS; i++) { + final byte[] rowKey = Bytes.toBytes(LoadTestKVGenerator.md5PrefixedKey(i)); + final byte[] v = GENERATOR.generateRandomSizeValue(rowKey, QUALIFIER); + table.put(new Put(rowKey).addColumn(FAMILY, QUALIFIER, v)); + } + // read to verify it. + for (int i = 0; i < NUM_ROWS; i++) { + final byte[] rowKey = Bytes.toBytes(LoadTestKVGenerator.md5PrefixedKey(i)); + final Result r = table.get(new Get(rowKey).addColumn(FAMILY, QUALIFIER)); + assertNotNull("Result was empty", r); + final byte[] v = r.getValue(FAMILY, QUALIFIER); + assertNotNull("Result did not contain expected value", v); + assertTrue("Value was not verified", LoadTestKVGenerator.verify(v, rowKey, QUALIFIER)); } - assertEquals("Expected 100 rows returned", 100, rowcnt); - } finally { - table.close(); } } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/ipc/TestSecureNettyRpcServer.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/ipc/TestSecureNettyRpcServer.java new file mode 100644 index 00000000000..b1f650d440d --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/ipc/TestSecureNettyRpcServer.java @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.ipc; + +import java.io.File; +import java.security.PrivilegedExceptionAction; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.CommonConfigurationKeys; +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtil; +import org.apache.hadoop.hbase.TableNameTestRule; +import org.apache.hadoop.hbase.security.HBaseKerberosUtils; +import org.apache.hadoop.hbase.testclassification.MediumTests; +import org.apache.hadoop.hbase.testclassification.RPCTests; +import org.apache.hadoop.minikdc.MiniKdc; +import org.apache.hadoop.security.UserGroupInformation; +import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category({ RPCTests.class, MediumTests.class }) +public class TestSecureNettyRpcServer extends TestNettyRpcServer { + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestSecureNettyRpcServer.class); + + private static File KEYTAB_FILE; + private static MiniKdc KDC; + private static String HOST = "localhost"; + private static String PRINCIPAL; + private static UserGroupInformation UGI; + + @Rule + public TableNameTestRule name = new TableNameTestRule(); + + @Before + public void setup() throws Exception { + TEST_UTIL = new HBaseTestingUtil(); + KEYTAB_FILE = new File(TEST_UTIL.getDataTestDir("keytab").toUri().getPath()); + KDC = TEST_UTIL.setupMiniKdc(KEYTAB_FILE); + PRINCIPAL = "hbase/" + HOST; + KDC.createPrincipal(KEYTAB_FILE, PRINCIPAL); + String principalName = PRINCIPAL + "@" + KDC.getRealm(); + HBaseKerberosUtils.setPrincipalForTesting(principalName); + Configuration conf = TEST_UTIL.getConfiguration(); + HBaseKerberosUtils.setSecuredConfiguration(conf, principalName, principalName); + UGI = login(KEYTAB_FILE.toString(), principalName); + super.setup(); + } + + @After + public void tearDown() throws Exception { + super.tearDown(); + if (KDC != null) { + KDC.stop(); + } + KEYTAB_FILE.delete(); + TEST_UTIL.cleanupTestDir(); + } + + @Override + @Test + public void testNettyRpcServer() throws Exception { + UGI.doAs(new PrivilegedExceptionAction() { + @Override + public Void run() throws Exception { + doTest(name.getTableName()); + return null; + } + }); + } + + static UserGroupInformation login(String krbKeytab, String krbPrincipal) throws Exception { + Configuration conf = new Configuration(); + conf.set(CommonConfigurationKeys.HADOOP_SECURITY_AUTHENTICATION, "kerberos"); + UserGroupInformation.setConfiguration(conf); + UserGroupInformation.loginUserFromKeytab(krbPrincipal, krbKeytab); + return UserGroupInformation.getLoginUser(); + } + +} diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/ipc/TestSecureSimpleRpcServer.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/ipc/TestSecureSimpleRpcServer.java new file mode 100644 index 00000000000..9b5d1ebfe92 --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/ipc/TestSecureSimpleRpcServer.java @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.ipc; + +import java.io.File; +import java.security.PrivilegedExceptionAction; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.CommonConfigurationKeys; +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtil; +import org.apache.hadoop.hbase.TableNameTestRule; +import org.apache.hadoop.hbase.security.HBaseKerberosUtils; +import org.apache.hadoop.hbase.testclassification.MediumTests; +import org.apache.hadoop.hbase.testclassification.RPCTests; +import org.apache.hadoop.minikdc.MiniKdc; +import org.apache.hadoop.security.UserGroupInformation; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category({ RPCTests.class, MediumTests.class }) +public class TestSecureSimpleRpcServer extends TestSimpleRpcServer { + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestSecureSimpleRpcServer.class); + + private static File KEYTAB_FILE; + private static MiniKdc KDC; + private static String HOST = "localhost"; + private static String PRINCIPAL; + private static UserGroupInformation UGI; + + @Rule + public TableNameTestRule name = new TableNameTestRule(); + + @BeforeClass + public static void setupClass() throws Exception { + TEST_UTIL = new HBaseTestingUtil(); + KEYTAB_FILE = new File(TEST_UTIL.getDataTestDir("keytab").toUri().getPath()); + KDC = TEST_UTIL.setupMiniKdc(KEYTAB_FILE); + PRINCIPAL = "hbase/" + HOST; + KDC.createPrincipal(KEYTAB_FILE, PRINCIPAL); + String principalName = PRINCIPAL + "@" + KDC.getRealm(); + HBaseKerberosUtils.setPrincipalForTesting(principalName); + Configuration conf = TEST_UTIL.getConfiguration(); + HBaseKerberosUtils.setSecuredConfiguration(conf, principalName, principalName); + UGI = login(KEYTAB_FILE.toString(), principalName); + TestSimpleRpcServer.setupClass(); + + } + + @AfterClass + public static void tearDownClass() throws Exception { + TestSimpleRpcServer.tearDownClass(); + if (KDC != null) { + KDC.stop(); + } + KEYTAB_FILE.delete(); + TEST_UTIL.cleanupTestDir(); + } + + @Override + @Test + public void testSimpleRpcServer() throws Exception { + UGI.doAs(new PrivilegedExceptionAction() { + @Override + public Void run() throws Exception { + doTest(name.getTableName()); + return null; + } + }); + } + + static UserGroupInformation login(String krbKeytab, String krbPrincipal) throws Exception { + Configuration conf = new Configuration(); + conf.set(CommonConfigurationKeys.HADOOP_SECURITY_AUTHENTICATION, "kerberos"); + UserGroupInformation.setConfiguration(conf); + UserGroupInformation.loginUserFromKeytab(krbPrincipal, krbKeytab); + return UserGroupInformation.getLoginUser(); + } + +} diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/ipc/TestSimpleRpcServer.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/ipc/TestSimpleRpcServer.java new file mode 100644 index 00000000000..f2420e028b1 --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/ipc/TestSimpleRpcServer.java @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.ipc; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtil; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.TableNameTestRule; +import org.apache.hadoop.hbase.client.Get; +import org.apache.hadoop.hbase.client.Put; +import org.apache.hadoop.hbase.client.Result; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.client.TableDescriptor; +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; +import org.apache.hadoop.hbase.regionserver.DisabledRegionSplitPolicy; +import org.apache.hadoop.hbase.testclassification.MediumTests; +import org.apache.hadoop.hbase.testclassification.RPCTests; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.LoadTestKVGenerator; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category({ RPCTests.class, MediumTests.class }) +public class TestSimpleRpcServer { + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestSimpleRpcServer.class); + + private static final byte[] FAMILY = Bytes.toBytes("f"); + private static final byte[] QUALIFIER = Bytes.toBytes("q"); + private static final int NUM_ROWS = 100; + private static final int MIN_LEN = 1000; + private static final int MAX_LEN = 1000000; + protected static final LoadTestKVGenerator GENERATOR = new LoadTestKVGenerator(MIN_LEN, MAX_LEN); + protected static HBaseTestingUtil TEST_UTIL; + + @Rule + public TableNameTestRule name = new TableNameTestRule(); + + @SuppressWarnings("deprecation") + @BeforeClass + public static void setupClass() throws Exception { + // A subclass may have already created TEST_UTIL and is now upcalling to us + if (TEST_UTIL == null) { + TEST_UTIL = new HBaseTestingUtil(); + } + // Set RPC server impl to SimpleRpcServer + TEST_UTIL.getConfiguration().set(RpcServerFactory.CUSTOM_RPC_SERVER_IMPL_CONF_KEY, + SimpleRpcServer.class.getName()); + TEST_UTIL.startMiniCluster(); + } + + @AfterClass + public static void tearDownClass() throws Exception { + TEST_UTIL.shutdownMiniCluster(); + } + + @Test + public void testSimpleRpcServer() throws Exception { + doTest(name.getTableName()); + } + + protected void doTest(TableName tableName) throws Exception { + // Splitting just complicates the test scenario, disable it + final TableDescriptor desc = TableDescriptorBuilder.newBuilder(tableName) + .setRegionSplitPolicyClassName(DisabledRegionSplitPolicy.class.getName()).build(); + try (Table table = + TEST_UTIL.createTable(desc, new byte[][] { FAMILY }, TEST_UTIL.getConfiguration())) { + // put some test data + for (int i = 0; i < NUM_ROWS; i++) { + final byte[] rowKey = Bytes.toBytes(LoadTestKVGenerator.md5PrefixedKey(i)); + final byte[] v = GENERATOR.generateRandomSizeValue(rowKey, QUALIFIER); + table.put(new Put(rowKey).addColumn(FAMILY, QUALIFIER, v)); + } + // read to verify it. + for (int i = 0; i < NUM_ROWS; i++) { + final byte[] rowKey = Bytes.toBytes(LoadTestKVGenerator.md5PrefixedKey(i)); + final Result r = table.get(new Get(rowKey).addColumn(FAMILY, QUALIFIER)); + assertNotNull("Result was empty", r); + final byte[] v = r.getValue(FAMILY, QUALIFIER); + assertNotNull("Result did not contain expected value", v); + assertTrue("Value was not verified", LoadTestKVGenerator.verify(v, rowKey, QUALIFIER)); + } + } + } + +}