Skip to content

Commit

Permalink
YARN-11761. [JDK17] Upgrade JUnit from 4 to 5 in hadoop-yarn-services…
Browse files Browse the repository at this point in the history
…-core. (#7374)

Co-authored-by: Ayush Saxena <ayushsaxena@apache.org>
Co-authored-by: Chris Nauroth <cnauroth@apache.org>
Reviewed-by: Ayush Saxena <ayushsaxena@apache.org>
Reviewed-by: Chris Nauroth <cnauroth@apache.org>
Signed-off-by: Shilun Fan <slfan1989@apache.org>
  • Loading branch information
3 people authored Feb 12, 2025
1 parent b4168c3 commit 14b7159
Show file tree
Hide file tree
Showing 27 changed files with 670 additions and 639 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@
import org.apache.hadoop.yarn.service.utils.SliderFileSystem;
import org.apache.hadoop.yarn.util.LinuxResourceCalculatorPlugin;
import org.apache.hadoop.yarn.util.ProcfsBasedProcessTree;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

@Override
protected void starting(Description description) {
public void afterEach(ExtensionContext context) throws Exception {
delete(context);
}

@Override
public void beforeEach(ExtensionContext context) throws Exception {
conf = new YarnConfiguration();
delete(description);
delete(context);
serviceBasePath = Paths.get("target",
description.getClassName(), description.getMethodName());
getClassName(context), getMethodName(context));
conf.set(YARN_SERVICE_BASE_PATH, serviceBasePath.toString());
try {
Files.createDirectories(serviceBasePath);
Expand All @@ -415,14 +421,17 @@ protected void starting(Description description) {
}
}

@Override
protected void finished(Description description) {
delete(description);
private void delete(ExtensionContext context) {
FileUtils.deleteQuietly(Paths.get("target", getClassName(context)).toFile());
}

private String getClassName(ExtensionContext context) {
Class<?> requiredTestClass = context.getRequiredTestClass();
return requiredTestClass.getName();
}

private void delete(Description description) {
FileUtils.deleteQuietly(Paths.get("target",
description.getClassName()).toFile());
private String getMethodName(ExtensionContext context) {
return context.getTestMethod().get().getName();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
import org.apache.hadoop.yarn.service.api.records.ConfigFile;
import org.apache.hadoop.yarn.service.api.records.Configuration;
import org.apache.hadoop.yarn.service.api.records.Service;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.util.*;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

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

assertEquals("all components need upgrade",
targetDef.getComponents(), finder.findTargetComponentSpecs(currentDef,
targetDef));
assertEquals(targetDef.getComponents(),
finder.findTargetComponentSpecs(currentDef,
targetDef), "all components need upgrade");
}

@Test
Expand All @@ -60,7 +60,7 @@ public void testServiceUpgradeWithNewComponentAddition() {

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

assertEquals("single components needs upgrade",
expected, finder.findTargetComponentSpecs(currentDef,
targetDef));
assertEquals(expected, finder.findTargetComponentSpecs(currentDef,
targetDef), "single components needs upgrade");
}

@Test
Expand Down Expand Up @@ -117,7 +116,7 @@ public void testChangeInConfigFileProperty() {
List<Component> expected = new ArrayList<>();
expected.addAll(targetDef.getComponents());

assertEquals("all components needs upgrade",
expected, finder.findTargetComponentSpecs(currentDef, targetDef));
assertEquals(expected, finder.findTargetComponentSpecs(currentDef, targetDef),
"all components needs upgrade");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@
import org.apache.hadoop.yarn.service.conf.YarnServiceConf;
import org.apache.hadoop.yarn.util.DockerClientConfigHandler;
import org.apache.hadoop.yarn.util.resource.ResourceUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -71,7 +71,9 @@
import java.util.concurrent.TimeoutException;

import static org.apache.hadoop.registry.client.api.RegistryConstants.KEY_REGISTRY_ZK_QUORUM;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

Expand All @@ -83,11 +85,11 @@ public class TestServiceAM extends ServiceTestUtils{
private File basedir;
YarnConfiguration conf = new YarnConfiguration();
TestingCluster zkCluster;
@Rule
public ServiceTestUtils.ServiceFSWatcher rule =
@RegisterExtension
private ServiceTestUtils.ServiceFSWatcher rule =
new ServiceTestUtils.ServiceFSWatcher();

@Before
@BeforeEach
public void setup() throws Exception {
basedir = new File("target", "apps");
if (basedir.exists()) {
Expand All @@ -101,7 +103,7 @@ public void setup() throws Exception {
LOG.info("ZK cluster: {}", zkCluster.getConnectString());
}

@After
@AfterEach
public void tearDown() throws IOException {
if (basedir != null) {
FileUtils.deleteDirectory(basedir);
Expand Down Expand Up @@ -147,15 +149,15 @@ public void testContainerCompleted() throws TimeoutException,

am.waitForCompInstanceState(compa0, ComponentInstanceState.INIT);
// still 1 pending instance
Assert.assertEquals(1,
am.getComponent("compa").getPendingInstances().size());
assertEquals(1, am.getComponent("compa").getPendingInstances().size());
am.stop();
}

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

// 0 pending instance
Assert.assertEquals(0,
assertEquals(0,
am.getComponent(comp1Name).getPendingInstances().size());

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

Assert.assertEquals("container state",
org.apache.hadoop.yarn.api.records.ContainerState.RUNNING,
assertEquals(org.apache.hadoop.yarn.api.records.ContainerState.RUNNING,
am.getCompInstance(comp1Name, comp1InstName).getContainerStatus()
.getState());
.getState(), "container state");
am.stop();
}

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

// 1 pending instance
Assert.assertEquals(1, am.getComponent(comp1Name).getPendingInstances()
assertEquals(1, am.getComponent(comp1Name).getPendingInstances()
.size());

am.feedContainerToComp(exampleApp, 2, comp1Name);

GenericTestUtils.waitFor(() -> am.getCompInstance(comp1Name, comp1InstName)
.getContainerStatus() != null, 2000, 200000);
Assert.assertEquals("container state",
org.apache.hadoop.yarn.api.records.ContainerState.RUNNING,
assertEquals(org.apache.hadoop.yarn.api.records.ContainerState.RUNNING,
am.getCompInstance(comp1Name, comp1InstName).getContainerStatus()
.getState());
.getState(), "container state");
}

// Test to verify that the AM doesn't wait for containers of a different app
// even though it corresponds to the same service.
@Test(timeout = 200000)
@Test
@Timeout(value = 200)
public void testContainersFromDifferentApp()
throws Exception {
ApplicationId applicationId = ApplicationId.newInstance(
Expand Down Expand Up @@ -268,17 +270,16 @@ public void testContainersFromDifferentApp()
am.start();
// 1 pending instance since the container in registry belongs to a different
// app.
Assert.assertEquals(1,
assertEquals(1,
am.getComponent(comp1Name).getPendingInstances().size());

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

Assert.assertEquals("container state",
org.apache.hadoop.yarn.api.records.ContainerState.RUNNING,
assertEquals(org.apache.hadoop.yarn.api.records.ContainerState.RUNNING,
am.getCompInstance(comp1Name, comp1InstName).getContainerStatus()
.getState());
.getState(), "container state");
am.stop();
}

Expand Down Expand Up @@ -314,13 +315,12 @@ public void testScheduleWithMultipleResourceTypes()

Collection<AMRMClient.ContainerRequest> rr =
amrmClientAsync.getMatchingRequests(0);
Assert.assertEquals(1, rr.size());
assertEquals(1, rr.size());

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

am.stop();
}
Expand Down Expand Up @@ -432,8 +432,7 @@ public void testRecordTokensForContainers() throws Exception {

assertEquals(2, amCreds.numberOfTokens());
for (Token<? extends TokenIdentifier> tk : amCreds.getAllTokens()) {
Assert.assertTrue(
tk.getKind().equals(DockerCredentialTokenIdentifier.KIND));
assertTrue(tk.getKind().equals(DockerCredentialTokenIdentifier.KIND));
}

am.stop();
Expand Down Expand Up @@ -463,7 +462,7 @@ public void testIPChange() throws TimeoutException,
GenericTestUtils.waitFor(() -> comp1inst0.getContainerStatus() != null,
2000, 200000);
// first host status will match the container nodeId
Assert.assertEquals("localhost",
assertEquals("localhost",
comp1inst0.getContainerStatus().getHost());

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

Assert.assertEquals(1,
am.getComponent("compa").getPendingInstances().size());
assertEquals(1, am.getComponent("compa").getPendingInstances().size());
am.stop();
}

@Test(timeout = 30000)
@Test
@Timeout(value = 30)
public void testSyncSysFS() {
ApplicationId applicationId = ApplicationId.newInstance(
System.currentTimeMillis(), 1);
Expand All @@ -554,7 +554,7 @@ public void testSyncSysFS() {
am.close();
} catch (Exception e) {
LOG.error("Fail to sync sysfs.", e);
Assert.fail("Fail to sync sysfs.");
fail("Fail to sync sysfs.");
}
}

Expand Down Expand Up @@ -593,14 +593,14 @@ public void testScheduleWithResourceAttributes() throws Exception {

Collection<AMRMClient.ContainerRequest> rr =
amrmClientAsync.getMatchingRequests(0);
Assert.assertEquals(1, rr.size());
assertEquals(1, rr.size());

org.apache.hadoop.yarn.api.records.Resource capability =
rr.iterator().next().getCapability();
Assert.assertEquals(1234L, capability.getResourceValue("test-resource"));
Assert.assertEquals("Gi",
assertEquals(1234L, capability.getResourceValue("test-resource"));
assertEquals("Gi",
capability.getResourceInformation("test-resource").getUnits());
Assert.assertEquals(2, capability.getResourceInformation("test-resource")
assertEquals(2, capability.getResourceInformation("test-resource")
.getAttributes().size());
am.stop();
}
Expand Down
Loading

0 comments on commit 14b7159

Please sign in to comment.