Skip to content

Commit

Permalink
HBASE-27194 Add test coverage for SimpleRpcServer (#4616)
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Viraj Jasani <[email protected]>
  • Loading branch information
apurtell authored Jul 13, 2022
1 parent ef1641d commit 84f0d14
Show file tree
Hide file tree
Showing 4 changed files with 351 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -54,14 +55,17 @@ public class TestNettyRpcServer {
public static final HBaseClassTestRule CLASS_RULE =
HBaseClassTestRule.forClass(TestNettyRpcServer.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 TestName name = new TestName();
private static HBaseTestingUtil TEST_UTIL;
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;

Expand All @@ -74,8 +78,10 @@ public static Collection<Object[]> parameters() {

@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);
Expand All @@ -89,34 +95,30 @@ public void tearDown() throws Exception {

@Test
public void testNettyRpcServer() throws Exception {
final Table table = TEST_UTIL.createTable(TABLE, FAMILY);
try {
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
List<Put> puts = new ArrayList<Put>(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);
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));
}
table.put(puts);

// 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)));
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();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Void>() {
@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();
}

}
Original file line number Diff line number Diff line change
@@ -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<Void>() {
@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();
}

}
Loading

0 comments on commit 84f0d14

Please sign in to comment.