Skip to content

Commit 14b7159

Browse files
slfan1989ayushtkncnauroth
authored
YARN-11761. [JDK17] Upgrade JUnit from 4 to 5 in hadoop-yarn-services-core. (#7374)
Co-authored-by: Ayush Saxena <[email protected]> Co-authored-by: Chris Nauroth <[email protected]> Reviewed-by: Ayush Saxena <[email protected]> Reviewed-by: Chris Nauroth <[email protected]> Signed-off-by: Shilun Fan <[email protected]>
1 parent b4168c3 commit 14b7159

27 files changed

+670
-639
lines changed

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-services/hadoop-yarn-services-core/src/test/java/org/apache/hadoop/yarn/service/ServiceTestUtils.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@
5151
import org.apache.hadoop.yarn.service.utils.SliderFileSystem;
5252
import org.apache.hadoop.yarn.util.LinuxResourceCalculatorPlugin;
5353
import org.apache.hadoop.yarn.util.ProcfsBasedProcessTree;
54-
import org.junit.rules.TestWatcher;
55-
import org.junit.runner.Description;
54+
import org.junit.jupiter.api.extension.AfterEachCallback;
55+
import org.junit.jupiter.api.extension.BeforeEachCallback;
56+
import org.junit.jupiter.api.extension.ExtensionContext;
5657
import org.slf4j.Logger;
5758
import org.slf4j.LoggerFactory;
5859

@@ -393,17 +394,22 @@ protected CuratorService getCuratorService() throws IOException {
393394
* Watcher to initialize yarn service base path under target and deletes the
394395
* the test directory when finishes.
395396
*/
396-
public static class ServiceFSWatcher extends TestWatcher {
397+
public static class ServiceFSWatcher implements BeforeEachCallback, AfterEachCallback {
397398
private YarnConfiguration conf;
398399
private SliderFileSystem fs;
399400
private java.nio.file.Path serviceBasePath;
400401

401402
@Override
402-
protected void starting(Description description) {
403+
public void afterEach(ExtensionContext context) throws Exception {
404+
delete(context);
405+
}
406+
407+
@Override
408+
public void beforeEach(ExtensionContext context) throws Exception {
403409
conf = new YarnConfiguration();
404-
delete(description);
410+
delete(context);
405411
serviceBasePath = Paths.get("target",
406-
description.getClassName(), description.getMethodName());
412+
getClassName(context), getMethodName(context));
407413
conf.set(YARN_SERVICE_BASE_PATH, serviceBasePath.toString());
408414
try {
409415
Files.createDirectories(serviceBasePath);
@@ -415,14 +421,17 @@ protected void starting(Description description) {
415421
}
416422
}
417423

418-
@Override
419-
protected void finished(Description description) {
420-
delete(description);
424+
private void delete(ExtensionContext context) {
425+
FileUtils.deleteQuietly(Paths.get("target", getClassName(context)).toFile());
426+
}
427+
428+
private String getClassName(ExtensionContext context) {
429+
Class<?> requiredTestClass = context.getRequiredTestClass();
430+
return requiredTestClass.getName();
421431
}
422432

423-
private void delete(Description description) {
424-
FileUtils.deleteQuietly(Paths.get("target",
425-
description.getClassName()).toFile());
433+
private String getMethodName(ExtensionContext context) {
434+
return context.getTestMethod().get().getName();
426435
}
427436

428437
/**

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-services/hadoop-yarn-services-core/src/test/java/org/apache/hadoop/yarn/service/TestDefaultUpgradeComponentsFinder.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
import org.apache.hadoop.yarn.service.api.records.ConfigFile;
2323
import org.apache.hadoop.yarn.service.api.records.Configuration;
2424
import org.apache.hadoop.yarn.service.api.records.Service;
25-
import org.junit.Assert;
26-
import org.junit.Test;
25+
import org.junit.jupiter.api.Test;
2726

2827
import java.util.*;
2928

30-
import static org.junit.Assert.assertEquals;
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
import static org.junit.jupiter.api.Assertions.fail;
3131

3232
/**
3333
* Tests for {@link UpgradeComponentsFinder.DefaultUpgradeComponentsFinder}.
@@ -44,9 +44,9 @@ public void testServiceArtifactChange() {
4444
targetDef.getComponents().forEach(x -> x.setArtifact(
4545
TestServiceManager.createTestArtifact("v1")));
4646

47-
assertEquals("all components need upgrade",
48-
targetDef.getComponents(), finder.findTargetComponentSpecs(currentDef,
49-
targetDef));
47+
assertEquals(targetDef.getComponents(),
48+
finder.findTargetComponentSpecs(currentDef,
49+
targetDef), "all components need upgrade");
5050
}
5151

5252
@Test
@@ -60,7 +60,7 @@ public void testServiceUpgradeWithNewComponentAddition() {
6060

6161
try {
6262
finder.findTargetComponentSpecs(currentDef, targetDef);
63-
Assert.fail("Expected error since component does not exist in service "
63+
fail("Expected error since component does not exist in service "
6464
+ "definition");
6565
} catch (UnsupportedOperationException usoe) {
6666
assertEquals(
@@ -83,9 +83,8 @@ public void testComponentArtifactChange() {
8383
List<Component> expected = new ArrayList<>();
8484
expected.add(targetDef.getComponents().get(0));
8585

86-
assertEquals("single components needs upgrade",
87-
expected, finder.findTargetComponentSpecs(currentDef,
88-
targetDef));
86+
assertEquals(expected, finder.findTargetComponentSpecs(currentDef,
87+
targetDef), "single components needs upgrade");
8988
}
9089

9190
@Test
@@ -117,7 +116,7 @@ public void testChangeInConfigFileProperty() {
117116
List<Component> expected = new ArrayList<>();
118117
expected.addAll(targetDef.getComponents());
119118

120-
assertEquals("all components needs upgrade",
121-
expected, finder.findTargetComponentSpecs(currentDef, targetDef));
119+
assertEquals(expected, finder.findTargetComponentSpecs(currentDef, targetDef),
120+
"all components needs upgrade");
122121
}
123122
}

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-services/hadoop-yarn-services-core/src/test/java/org/apache/hadoop/yarn/service/TestServiceAM.java

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@
5151
import org.apache.hadoop.yarn.service.conf.YarnServiceConf;
5252
import org.apache.hadoop.yarn.util.DockerClientConfigHandler;
5353
import org.apache.hadoop.yarn.util.resource.ResourceUtils;
54-
import org.junit.After;
55-
import org.junit.Assert;
56-
import org.junit.Before;
57-
import org.junit.Rule;
58-
import org.junit.Test;
54+
import org.junit.jupiter.api.AfterEach;
55+
import org.junit.jupiter.api.BeforeEach;
56+
import org.junit.jupiter.api.Test;
57+
import org.junit.jupiter.api.Timeout;
58+
import org.junit.jupiter.api.extension.RegisterExtension;
5959
import org.mockito.Mockito;
6060
import org.slf4j.Logger;
6161
import org.slf4j.LoggerFactory;
@@ -71,7 +71,9 @@
7171
import java.util.concurrent.TimeoutException;
7272

7373
import static org.apache.hadoop.registry.client.api.RegistryConstants.KEY_REGISTRY_ZK_QUORUM;
74-
import static org.junit.Assert.assertEquals;
74+
import static org.junit.jupiter.api.Assertions.assertEquals;
75+
import static org.junit.jupiter.api.Assertions.assertTrue;
76+
import static org.junit.jupiter.api.Assertions.fail;
7577
import static org.mockito.Mockito.times;
7678
import static org.mockito.Mockito.verify;
7779

@@ -83,11 +85,11 @@ public class TestServiceAM extends ServiceTestUtils{
8385
private File basedir;
8486
YarnConfiguration conf = new YarnConfiguration();
8587
TestingCluster zkCluster;
86-
@Rule
87-
public ServiceTestUtils.ServiceFSWatcher rule =
88+
@RegisterExtension
89+
private ServiceTestUtils.ServiceFSWatcher rule =
8890
new ServiceTestUtils.ServiceFSWatcher();
8991

90-
@Before
92+
@BeforeEach
9193
public void setup() throws Exception {
9294
basedir = new File("target", "apps");
9395
if (basedir.exists()) {
@@ -101,7 +103,7 @@ public void setup() throws Exception {
101103
LOG.info("ZK cluster: {}", zkCluster.getConnectString());
102104
}
103105

104-
@After
106+
@AfterEach
105107
public void tearDown() throws IOException {
106108
if (basedir != null) {
107109
FileUtils.deleteDirectory(basedir);
@@ -147,15 +149,15 @@ public void testContainerCompleted() throws TimeoutException,
147149

148150
am.waitForCompInstanceState(compa0, ComponentInstanceState.INIT);
149151
// still 1 pending instance
150-
Assert.assertEquals(1,
151-
am.getComponent("compa").getPendingInstances().size());
152+
assertEquals(1, am.getComponent("compa").getPendingInstances().size());
152153
am.stop();
153154
}
154155

155156
// Test to verify that the containers of previous attempt are not prematurely
156157
// released. These containers are sent by the RM to the AM in the
157158
// heartbeat response.
158-
@Test(timeout = 200000)
159+
@Test
160+
@Timeout(value = 200)
159161
public void testContainersFromPreviousAttemptsWithRMRestart()
160162
throws Exception {
161163
ApplicationId applicationId = ApplicationId.newInstance(
@@ -182,23 +184,23 @@ public void testContainersFromPreviousAttemptsWithRMRestart()
182184
am.waitForCompInstanceState(comp10, ComponentInstanceState.STARTED);
183185

184186
// 0 pending instance
185-
Assert.assertEquals(0,
187+
assertEquals(0,
186188
am.getComponent(comp1Name).getPendingInstances().size());
187189

188190
GenericTestUtils.waitFor(() -> am.getCompInstance(comp1Name, comp1InstName)
189191
.getContainerStatus() != null, 2000, 200000);
190192

191-
Assert.assertEquals("container state",
192-
org.apache.hadoop.yarn.api.records.ContainerState.RUNNING,
193+
assertEquals(org.apache.hadoop.yarn.api.records.ContainerState.RUNNING,
193194
am.getCompInstance(comp1Name, comp1InstName).getContainerStatus()
194-
.getState());
195+
.getState(), "container state");
195196
am.stop();
196197
}
197198

198199
// Test to verify that the containers of previous attempt are released and the
199200
// component instance is added to the pending queue when the recovery wait
200201
// time interval elapses.
201-
@Test(timeout = 200000)
202+
@Test
203+
@Timeout(value = 200)
202204
public void testContainersReleasedWhenExpired()
203205
throws Exception {
204206
ApplicationId applicationId = ApplicationId.newInstance(
@@ -225,22 +227,22 @@ public void testContainersReleasedWhenExpired()
225227
.equals(ComponentState.FLEXING), 100, 2000);
226228

227229
// 1 pending instance
228-
Assert.assertEquals(1, am.getComponent(comp1Name).getPendingInstances()
230+
assertEquals(1, am.getComponent(comp1Name).getPendingInstances()
229231
.size());
230232

231233
am.feedContainerToComp(exampleApp, 2, comp1Name);
232234

233235
GenericTestUtils.waitFor(() -> am.getCompInstance(comp1Name, comp1InstName)
234236
.getContainerStatus() != null, 2000, 200000);
235-
Assert.assertEquals("container state",
236-
org.apache.hadoop.yarn.api.records.ContainerState.RUNNING,
237+
assertEquals(org.apache.hadoop.yarn.api.records.ContainerState.RUNNING,
237238
am.getCompInstance(comp1Name, comp1InstName).getContainerStatus()
238-
.getState());
239+
.getState(), "container state");
239240
}
240241

241242
// Test to verify that the AM doesn't wait for containers of a different app
242243
// even though it corresponds to the same service.
243-
@Test(timeout = 200000)
244+
@Test
245+
@Timeout(value = 200)
244246
public void testContainersFromDifferentApp()
245247
throws Exception {
246248
ApplicationId applicationId = ApplicationId.newInstance(
@@ -268,17 +270,16 @@ public void testContainersFromDifferentApp()
268270
am.start();
269271
// 1 pending instance since the container in registry belongs to a different
270272
// app.
271-
Assert.assertEquals(1,
273+
assertEquals(1,
272274
am.getComponent(comp1Name).getPendingInstances().size());
273275

274276
am.feedContainerToComp(exampleApp, 1, comp1Name);
275277
GenericTestUtils.waitFor(() -> am.getCompInstance(comp1Name, comp1InstName)
276278
.getContainerStatus() != null, 2000, 200000);
277279

278-
Assert.assertEquals("container state",
279-
org.apache.hadoop.yarn.api.records.ContainerState.RUNNING,
280+
assertEquals(org.apache.hadoop.yarn.api.records.ContainerState.RUNNING,
280281
am.getCompInstance(comp1Name, comp1InstName).getContainerStatus()
281-
.getState());
282+
.getState(), "container state");
282283
am.stop();
283284
}
284285

@@ -314,13 +315,12 @@ public void testScheduleWithMultipleResourceTypes()
314315

315316
Collection<AMRMClient.ContainerRequest> rr =
316317
amrmClientAsync.getMatchingRequests(0);
317-
Assert.assertEquals(1, rr.size());
318+
assertEquals(1, rr.size());
318319

319320
org.apache.hadoop.yarn.api.records.Resource capability =
320321
rr.iterator().next().getCapability();
321-
Assert.assertEquals(3333L, capability.getResourceValue("resource-1"));
322-
Assert.assertEquals("Gi",
323-
capability.getResourceInformation("resource-1").getUnits());
322+
assertEquals(3333L, capability.getResourceValue("resource-1"));
323+
assertEquals("Gi", capability.getResourceInformation("resource-1").getUnits());
324324

325325
am.stop();
326326
}
@@ -432,8 +432,7 @@ public void testRecordTokensForContainers() throws Exception {
432432

433433
assertEquals(2, amCreds.numberOfTokens());
434434
for (Token<? extends TokenIdentifier> tk : amCreds.getAllTokens()) {
435-
Assert.assertTrue(
436-
tk.getKind().equals(DockerCredentialTokenIdentifier.KIND));
435+
assertTrue(tk.getKind().equals(DockerCredentialTokenIdentifier.KIND));
437436
}
438437

439438
am.stop();
@@ -463,7 +462,7 @@ public void testIPChange() throws TimeoutException,
463462
GenericTestUtils.waitFor(() -> comp1inst0.getContainerStatus() != null,
464463
2000, 200000);
465464
// first host status will match the container nodeId
466-
Assert.assertEquals("localhost",
465+
assertEquals("localhost",
467466
comp1inst0.getContainerStatus().getHost());
468467

469468
LOG.info("Change the IP and host");
@@ -491,7 +490,8 @@ public void testIPChange() throws TimeoutException,
491490
In case the id is set to null or unset so it is effectively null,
492491
Path.checkPathArg throws an IllegalArgumentException.
493492
**/
494-
@Test(timeout = 30000)
493+
@Test
494+
@Timeout(value = 30)
495495
public void testContainersReleasedWhenPreLaunchFails()
496496
throws Exception {
497497
ApplicationId applicationId = ApplicationId.newInstance(
@@ -522,12 +522,12 @@ public void testContainersReleasedWhenPreLaunchFails()
522522
am.getComponent(compA.getName()).getPendingInstances()
523523
.contains(compAinst0), 2000, 30000);
524524

525-
Assert.assertEquals(1,
526-
am.getComponent("compa").getPendingInstances().size());
525+
assertEquals(1, am.getComponent("compa").getPendingInstances().size());
527526
am.stop();
528527
}
529528

530-
@Test(timeout = 30000)
529+
@Test
530+
@Timeout(value = 30)
531531
public void testSyncSysFS() {
532532
ApplicationId applicationId = ApplicationId.newInstance(
533533
System.currentTimeMillis(), 1);
@@ -554,7 +554,7 @@ public void testSyncSysFS() {
554554
am.close();
555555
} catch (Exception e) {
556556
LOG.error("Fail to sync sysfs.", e);
557-
Assert.fail("Fail to sync sysfs.");
557+
fail("Fail to sync sysfs.");
558558
}
559559
}
560560

@@ -593,14 +593,14 @@ public void testScheduleWithResourceAttributes() throws Exception {
593593

594594
Collection<AMRMClient.ContainerRequest> rr =
595595
amrmClientAsync.getMatchingRequests(0);
596-
Assert.assertEquals(1, rr.size());
596+
assertEquals(1, rr.size());
597597

598598
org.apache.hadoop.yarn.api.records.Resource capability =
599599
rr.iterator().next().getCapability();
600-
Assert.assertEquals(1234L, capability.getResourceValue("test-resource"));
601-
Assert.assertEquals("Gi",
600+
assertEquals(1234L, capability.getResourceValue("test-resource"));
601+
assertEquals("Gi",
602602
capability.getResourceInformation("test-resource").getUnits());
603-
Assert.assertEquals(2, capability.getResourceInformation("test-resource")
603+
assertEquals(2, capability.getResourceInformation("test-resource")
604604
.getAttributes().size());
605605
am.stop();
606606
}

0 commit comments

Comments
 (0)