Skip to content

Commit d8cd864

Browse files
authored
HBASE-30071 Upgrade hbase-server to use junit5 Part11 (#8068) (#8074) (#8080)
Signed-off-by: Xiao Liu <liuxiaocs@apache.org> (cherry picked from commit 235ea44) (cherry picked from commit 1e47a52)
1 parent 70a10f2 commit d8cd864

51 files changed

Lines changed: 1261 additions & 1466 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.hbase;
19+
20+
import org.junit.jupiter.api.extension.BeforeEachCallback;
21+
import org.junit.jupiter.api.extension.ExtensionContext;
22+
23+
/**
24+
* Returns a {@code TableName} based on currently running test method name.
25+
*/
26+
public class TableNameTestExtension implements BeforeEachCallback {
27+
28+
private TableName tableName;
29+
30+
/**
31+
* Helper to handle parameterized method names. Unlike regular test methods, parameterized method
32+
* names look like 'foo[x]'. This is problematic for tests that use this name for HBase tables.
33+
* This helper strips out the parameter suffixes.
34+
* @return current test method name without parameterized suffixes.
35+
*/
36+
public static String cleanUpTestName(String methodName) {
37+
int index = methodName.indexOf('[');
38+
if (index == -1) {
39+
return methodName;
40+
}
41+
return methodName.substring(0, index);
42+
}
43+
44+
public TableName getTableName() {
45+
return tableName;
46+
}
47+
48+
@Override
49+
public void beforeEach(ExtensionContext context) {
50+
tableName = TableName.valueOf(cleanUpTestName(context.getRequiredTestMethod().getName()));
51+
}
52+
}

hbase-server/src/test/java/org/apache/hadoop/hbase/security/TestUser.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,33 @@
1717
*/
1818
package org.apache.hadoop.hbase.security;
1919

20-
import static org.junit.Assert.*;
20+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertFalse;
23+
import static org.junit.jupiter.api.Assertions.assertNotNull;
24+
import static org.junit.jupiter.api.Assertions.assertTrue;
2125

2226
import java.io.IOException;
2327
import java.security.PrivilegedAction;
2428
import java.security.PrivilegedExceptionAction;
2529
import org.apache.commons.lang3.SystemUtils;
2630
import org.apache.hadoop.conf.Configuration;
2731
import org.apache.hadoop.fs.CommonConfigurationKeys;
28-
import org.apache.hadoop.hbase.HBaseClassTestRule;
2932
import org.apache.hadoop.hbase.HBaseConfiguration;
3033
import org.apache.hadoop.hbase.testclassification.SecurityTests;
3134
import org.apache.hadoop.hbase.testclassification.SmallTests;
3235
import org.apache.hadoop.security.UserGroupInformation;
33-
import org.junit.ClassRule;
34-
import org.junit.Test;
35-
import org.junit.experimental.categories.Category;
36+
import org.junit.jupiter.api.Tag;
37+
import org.junit.jupiter.api.Test;
3638
import org.slf4j.Logger;
3739
import org.slf4j.LoggerFactory;
3840

3941
import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableSet;
4042

41-
@Category({ SecurityTests.class, SmallTests.class })
43+
@Tag(SecurityTests.TAG)
44+
@Tag(SmallTests.TAG)
4245
public class TestUser {
4346

44-
@ClassRule
45-
public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(TestUser.class);
46-
4747
private static final Logger LOG = LoggerFactory.getLogger(TestUser.class);
4848

4949
@Test
@@ -113,8 +113,8 @@ public void testCacheGetGroupsRoot() throws Exception {
113113
public void testBasicAttributes() throws Exception {
114114
Configuration conf = HBaseConfiguration.create();
115115
User user = User.createUserForTesting(conf, "simple", new String[] { "foo" });
116-
assertEquals("Username should match", "simple", user.getName());
117-
assertEquals("Short username should match", "simple", user.getShortName());
116+
assertEquals("simple", user.getName(), "Username should match");
117+
assertEquals("simple", user.getShortName(), "Short username should match");
118118
// don't test shortening of kerberos names because regular Hadoop doesn't support them
119119
}
120120

@@ -131,12 +131,12 @@ public String run() throws IOException {
131131
};
132132

133133
String username = user.runAs(action);
134-
assertEquals("Current user within runAs() should match", "testuser", username);
134+
assertEquals("testuser", username, "Current user within runAs() should match");
135135

136136
// ensure the next run is correctly set
137137
User user2 = User.createUserForTesting(conf, "testuser2", new String[] { "foo" });
138138
String username2 = user2.runAs(action);
139-
assertEquals("Second username should match second user", "testuser2", username2);
139+
assertEquals("testuser2", username2, "Second username should match second user");
140140

141141
// check the exception version
142142
username = user.runAs(new PrivilegedExceptionAction<String>() {
@@ -145,16 +145,16 @@ public String run() throws Exception {
145145
return User.getCurrent().getName();
146146
}
147147
});
148-
assertEquals("User name in runAs() should match", "testuser", username);
148+
assertEquals("testuser", username, "User name in runAs() should match");
149149

150150
// verify that nested contexts work
151151
user2.runAs(new PrivilegedExceptionAction<Object>() {
152152
@Override
153153
public Object run() throws IOException, InterruptedException {
154154
String nestedName = user.runAs(action);
155-
assertEquals("Nest name should match nested user", "testuser", nestedName);
156-
assertEquals("Current name should match current user", "testuser2",
157-
User.getCurrent().getName());
155+
assertEquals("testuser", nestedName, "Nest name should match nested user");
156+
assertEquals("testuser2", User.getCurrent().getName(),
157+
"Current name should match current user");
158158
return null;
159159
}
160160
});
@@ -173,7 +173,7 @@ public String run() {
173173
}
174174
});
175175

176-
assertEquals("Current user within runAs() should match", "testuser", username);
176+
assertEquals("testuser", username, "Current user within runAs() should match");
177177
}
178178

179179
/**
@@ -211,32 +211,32 @@ public void testUserGroupNames() throws Exception {
211211
}
212212

213213
private void assertUserGroup(User user, ImmutableSet<String> groups) {
214-
assertNotNull("GroupNames should be not null", user.getGroupNames());
215-
assertTrue("UserGroupNames length should be == " + groups.size(),
216-
user.getGroupNames().length == groups.size());
214+
assertNotNull(user.getGroupNames(), "GroupNames should be not null");
215+
assertTrue(user.getGroupNames().length == groups.size(),
216+
"UserGroupNames length should be == " + groups.size());
217217

218218
for (String group : user.getGroupNames()) {
219-
assertTrue("groupName should be in set ", groups.contains(group));
219+
assertTrue(groups.contains(group), "groupName should be in set ");
220220
}
221221
}
222222

223223
@Test
224224
public void testSecurityForNonSecureHadoop() {
225-
assertFalse("Security should be disable in non-secure Hadoop", User.isSecurityEnabled());
225+
assertFalse(User.isSecurityEnabled(), "Security should be disable in non-secure Hadoop");
226226

227227
Configuration conf = HBaseConfiguration.create();
228228
conf.set(CommonConfigurationKeys.HADOOP_SECURITY_AUTHENTICATION, "kerberos");
229229
conf.set(User.HBASE_SECURITY_CONF_KEY, "kerberos");
230-
assertTrue("Security should be enabled", User.isHBaseSecurityEnabled(conf));
230+
assertTrue(User.isHBaseSecurityEnabled(conf), "Security should be enabled");
231231

232232
conf = HBaseConfiguration.create();
233233
conf.set(CommonConfigurationKeys.HADOOP_SECURITY_AUTHENTICATION, "kerberos");
234-
assertFalse("HBase security should not be enabled if " + User.HBASE_SECURITY_CONF_KEY
235-
+ " is not set accordingly", User.isHBaseSecurityEnabled(conf));
234+
assertFalse(User.isHBaseSecurityEnabled(conf), "HBase security should not be enabled if "
235+
+ User.HBASE_SECURITY_CONF_KEY + " is not set accordingly");
236236

237237
conf = HBaseConfiguration.create();
238238
conf.set(User.HBASE_SECURITY_CONF_KEY, "kerberos");
239-
assertTrue("HBase security should be enabled regardless of underlying " + "HDFS settings",
240-
User.isHBaseSecurityEnabled(conf));
239+
assertTrue(User.isHBaseSecurityEnabled(conf),
240+
"HBase security should be enabled regardless of underlying " + "HDFS settings");
241241
}
242242
}

hbase-server/src/test/java/org/apache/hadoop/hbase/security/TestUsersOperationsWithSecureHadoop.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,29 @@
2222
import static org.apache.hadoop.hbase.security.HBaseKerberosUtils.getKeytabFileForTesting;
2323
import static org.apache.hadoop.hbase.security.HBaseKerberosUtils.getPrincipalForTesting;
2424
import static org.apache.hadoop.hbase.security.HBaseKerberosUtils.getSecuredConfiguration;
25-
import static org.junit.Assert.assertEquals;
26-
import static org.junit.Assert.assertFalse;
27-
import static org.junit.Assert.assertNotNull;
28-
import static org.junit.Assert.assertTrue;
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
import static org.junit.jupiter.api.Assertions.assertFalse;
27+
import static org.junit.jupiter.api.Assertions.assertNotNull;
28+
import static org.junit.jupiter.api.Assertions.assertTrue;
2929

3030
import java.io.File;
3131
import java.io.IOException;
3232
import org.apache.hadoop.conf.Configuration;
3333
import org.apache.hadoop.hbase.AuthUtil;
34-
import org.apache.hadoop.hbase.HBaseClassTestRule;
3534
import org.apache.hadoop.hbase.HBaseTestingUtility;
3635
import org.apache.hadoop.hbase.testclassification.SecurityTests;
3736
import org.apache.hadoop.hbase.testclassification.SmallTests;
3837
import org.apache.hadoop.minikdc.MiniKdc;
3938
import org.apache.hadoop.security.UserGroupInformation;
40-
import org.junit.AfterClass;
41-
import org.junit.BeforeClass;
42-
import org.junit.ClassRule;
43-
import org.junit.Test;
44-
import org.junit.experimental.categories.Category;
39+
import org.junit.jupiter.api.AfterAll;
40+
import org.junit.jupiter.api.BeforeAll;
41+
import org.junit.jupiter.api.Tag;
42+
import org.junit.jupiter.api.Test;
4543

46-
@Category({ SecurityTests.class, SmallTests.class })
44+
@Tag(SecurityTests.TAG)
45+
@Tag(SmallTests.TAG)
4746
public class TestUsersOperationsWithSecureHadoop {
4847

49-
@ClassRule
50-
public static final HBaseClassTestRule CLASS_RULE =
51-
HBaseClassTestRule.forClass(TestUsersOperationsWithSecureHadoop.class);
52-
5348
private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
5449
private static final File KEYTAB_FILE =
5550
new File(TEST_UTIL.getDataTestDir("keytab").toUri().getPath());
@@ -64,7 +59,7 @@ public class TestUsersOperationsWithSecureHadoop {
6459

6560
private static String OTHER_CLIENT_NAME;
6661

67-
@BeforeClass
62+
@BeforeAll
6863
public static void setUp() throws Exception {
6964
KDC = TEST_UTIL.setupMiniKdc(KEYTAB_FILE);
7065
PRINCIPAL = "hbase/" + HOST;
@@ -77,7 +72,7 @@ public static void setUp() throws Exception {
7772
HBaseKerberosUtils.setClientKeytabForTesting(KEYTAB_FILE.getAbsolutePath());
7873
}
7974

80-
@AfterClass
75+
@AfterAll
8176
public static void tearDown() throws IOException {
8277
if (KDC != null) {
8378
KDC.stop();
@@ -110,8 +105,8 @@ public void testUserLoginInSecureHadoop() throws Exception {
110105
User.login(conf, HBaseKerberosUtils.KRB_KEYTAB_FILE, HBaseKerberosUtils.KRB_PRINCIPAL,
111106
"localhost");
112107
UserGroupInformation successLogin = UserGroupInformation.getLoginUser();
113-
assertFalse("ugi should be different in in case success login",
114-
defaultLogin.equals(successLogin));
108+
assertFalse(defaultLogin.equals(successLogin),
109+
"ugi should be different in in case success login");
115110
}
116111

117112
@Test
@@ -127,7 +122,7 @@ public void testLoginWithUserKeytabAndPrincipal() throws Exception {
127122
UserGroupInformation.setConfiguration(conf);
128123

129124
UserProvider provider = UserProvider.instantiate(conf);
130-
assertTrue("Client principal or keytab is empty", provider.shouldLoginFromKeytab());
125+
assertTrue(provider.shouldLoginFromKeytab(), "Client principal or keytab is empty");
131126

132127
provider.login(AuthUtil.HBASE_CLIENT_KEYTAB_FILE, AuthUtil.HBASE_CLIENT_KERBEROS_PRINCIPAL);
133128
User loginUser = provider.getCurrent();

hbase-server/src/test/java/org/apache/hadoop/hbase/security/access/SecureTestUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
*/
1818
package org.apache.hadoop.hbase.security.access;
1919

20-
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.fail;
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.fail;
2222

2323
import com.google.protobuf.ServiceException;
2424
import java.io.IOException;

0 commit comments

Comments
 (0)