Skip to content
Permalink
Browse files
[FLINK-25543][yarn][tests] Migrate tests to JUnit5
  • Loading branch information
RocMarshal committed May 17, 2022
1 parent f5aee6e commit 74a61e49d276337bca19bd87896522d01c24f7aa
Showing 23 changed files with 1,020 additions and 1,099 deletions.
@@ -24,12 +24,13 @@
import org.apache.flink.runtime.clusterframework.TaskExecutorProcessUtils;
import org.apache.flink.runtime.clusterframework.types.ResourceIDRetrievable;
import org.apache.flink.runtime.resourcemanager.WorkerResourceSpec;
import org.apache.flink.util.TestLogger;
import org.apache.flink.util.TestLoggerExtension;
import org.apache.flink.util.concurrent.ScheduledExecutor;
import org.apache.flink.util.concurrent.ScheduledExecutorServiceAdapter;
import org.apache.flink.util.function.RunnableWithException;

import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.util.Collection;
import java.util.Collections;
@@ -39,12 +40,11 @@
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;

/** Common test cases for implementations of {@link ResourceManagerDriver}. */
public abstract class ResourceManagerDriverTestBase<WorkerType extends ResourceIDRetrievable>
extends TestLogger {
@ExtendWith(TestLoggerExtension.class)
public abstract class ResourceManagerDriverTestBase<WorkerType extends ResourceIDRetrievable> {

protected static final long TIMEOUT_SEC = 5L;

@@ -202,7 +202,7 @@ protected final <T> CompletableFuture<T> runInMainThread(Supplier<T> supplier) {
}

protected final void validateInMainThread() {
assertThat(Thread.currentThread().getName(), is(MAIN_THREAD_NAME));
assertThat(Thread.currentThread().getName()).isEqualTo(MAIN_THREAD_NAME);
}

protected abstract void prepareRunTest() throws Exception;
@@ -21,7 +21,6 @@
import org.apache.flink.client.deployment.ClusterRetrieveException;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.util.Preconditions;
import org.apache.flink.util.TestLogger;

import org.apache.hadoop.yarn.api.records.ApplicationAttemptId;
import org.apache.hadoop.yarn.api.records.ApplicationId;
@@ -33,22 +32,23 @@
import org.apache.hadoop.yarn.conf.YarnConfiguration;
import org.apache.hadoop.yarn.exceptions.YarnException;
import org.apache.hadoop.yarn.util.Records;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Map;

/** Tests for the {@link YarnClusterDescriptor}. */
public class AbstractYarnClusterTest extends TestLogger {
import static org.assertj.core.api.Assertions.assertThatThrownBy;

@Rule public TemporaryFolder temporaryFolder = new TemporaryFolder();
/** Tests for the {@link YarnClusterDescriptor}. */
class AbstractYarnClusterTest {

/** Tests that the cluster retrieval of a finished YARN application fails. */
@Test(expected = ClusterRetrieveException.class)
public void testClusterClientRetrievalOfFinishedYarnApplication() throws Exception {
@Test
void testClusterClientRetrievalOfFinishedYarnApplication(@TempDir Path tempDir) {

final ApplicationId applicationId =
ApplicationId.newInstance(System.currentTimeMillis(), 42);
final ApplicationReport applicationReport =
@@ -63,18 +63,15 @@ public void testClusterClientRetrievalOfFinishedYarnApplication() throws Excepti
yarnClient.init(yarnConfiguration);
yarnClient.start();

final YarnClusterDescriptor clusterDescriptor =
try (YarnClusterDescriptor clusterDescriptor =
YarnTestUtils.createClusterDescriptorWithLogging(
temporaryFolder.newFolder().getAbsolutePath(),
tempDir.toFile().getAbsolutePath(),
new Configuration(),
yarnConfiguration,
yarnClient,
false);

try {
clusterDescriptor.retrieve(applicationId);
} finally {
clusterDescriptor.close();
false)) {
assertThatThrownBy(() -> clusterDescriptor.retrieve(applicationId))
.isInstanceOf(ClusterRetrieveException.class);
}
}

@@ -19,67 +19,63 @@
package org.apache.flink.yarn;

import org.apache.flink.runtime.util.HadoopUtils;
import org.apache.flink.util.TestLogger;

import org.apache.hadoop.yarn.api.records.Priority;
import org.apache.hadoop.yarn.api.records.Resource;
import org.apache.hadoop.yarn.client.api.AMRMClient;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat;

/** Tests for {@link ContainerRequestReflector}. */
public class ContainerRequestReflectorTest extends TestLogger {
class ContainerRequestReflectorTest {

@Test
public void testGetContainerRequestIfConstructorPresent() {
void testGetContainerRequestIfConstructorPresent() {
final ContainerRequestReflector containerRequestReflector =
new ContainerRequestReflector(ContainerRequestWithConstructor.class);
Resource resource = Resource.newInstance(100, 1);
Priority priority = Priority.newInstance(1);

AMRMClient.ContainerRequest containerRequest =
containerRequestReflector.getContainerRequest(resource, priority, "GPU");
assertTrue(containerRequest instanceof ContainerRequestWithConstructor);
assertThat(containerRequest).isInstanceOf(ContainerRequestWithConstructor.class);
ContainerRequestWithConstructor containerRequestWithConstructor =
(ContainerRequestWithConstructor) containerRequest;
assertEquals("GPU", containerRequestWithConstructor.getNodeLabelsExpression());
assertThat(containerRequestWithConstructor.getNodeLabelsExpression()).isEqualTo("GPU");

containerRequest = containerRequestReflector.getContainerRequest(resource, priority, null);
assertFalse(containerRequest instanceof ContainerRequestWithConstructor);
assertThat(containerRequest).isNotInstanceOf(ContainerRequestWithConstructor.class);

containerRequest = containerRequestReflector.getContainerRequest(resource, priority, "");
assertFalse(containerRequest instanceof ContainerRequestWithConstructor);
assertThat(containerRequest).isNotInstanceOf(ContainerRequestWithConstructor.class);
}

@Test
public void testGetContainerRequestIfConstructorAbsent() {
void testGetContainerRequestIfConstructorAbsent() {
final ContainerRequestReflector containerRequestReflector =
new ContainerRequestReflector(ContainerRequestWithoutConstructor.class);
Resource resource = Resource.newInstance(100, 1);
Priority priority = Priority.newInstance(1);

AMRMClient.ContainerRequest containerRequest =
containerRequestReflector.getContainerRequest(resource, priority, "GPU");
assertFalse(containerRequest instanceof ContainerRequestWithoutConstructor);
assertThat(containerRequest).isNotInstanceOf(ContainerRequestWithoutConstructor.class);

containerRequest = containerRequestReflector.getContainerRequest(resource, priority, null);
assertFalse(containerRequest instanceof ContainerRequestWithoutConstructor);
assertThat(containerRequest).isNotInstanceOf(ContainerRequestWithoutConstructor.class);

containerRequest = containerRequestReflector.getContainerRequest(resource, priority, "");
assertFalse(containerRequest instanceof ContainerRequestWithoutConstructor);
assertThat(containerRequest).isNotInstanceOf(ContainerRequestWithoutConstructor.class);
}

@Test
public void testGetContainerRequestWithoutYarnSupport() {
assumeTrue(HadoopUtils.isMaxHadoopVersion(2, 6));
void testGetContainerRequestWithoutYarnSupport() {
assumeThat(HadoopUtils.isMaxHadoopVersion(2, 6)).isTrue();

Resource resource = Resource.newInstance(100, 1);
Priority priority = Priority.newInstance(1);
@@ -90,24 +86,24 @@ public void testGetContainerRequestWithoutYarnSupport() {
}

@Test
public void testGetContainerRequestWithYarnSupport()
void testGetContainerRequestWithYarnSupport()
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
assumeTrue(HadoopUtils.isMinHadoopVersion(2, 6));
assumeThat(HadoopUtils.isMinHadoopVersion(2, 6)).isTrue();

Resource resource = Resource.newInstance(100, 1);
Priority priority = Priority.newInstance(1);

AMRMClient.ContainerRequest containerRequest =
ContainerRequestReflector.INSTANCE.getContainerRequest(resource, priority, "GPU");
assertEquals("GPU", getNodeLabelExpressionWithReflector(containerRequest));
assertThat(getNodeLabelExpressionWithReflector(containerRequest)).isEqualTo("GPU");

containerRequest =
ContainerRequestReflector.INSTANCE.getContainerRequest(resource, priority, null);
assertNull(getNodeLabelExpressionWithReflector(containerRequest));
assertThat(getNodeLabelExpressionWithReflector(containerRequest)).isNull();

containerRequest =
ContainerRequestReflector.INSTANCE.getContainerRequest(resource, priority, "");
assertNull(getNodeLabelExpressionWithReflector(containerRequest));
assertThat(getNodeLabelExpressionWithReflector(containerRequest)).isNull();
}

private String getNodeLabelExpressionWithReflector(AMRMClient.ContainerRequest containerRequest)
@@ -27,28 +27,36 @@
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.hadoop.yarn.api.records.ApplicationId;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import static org.junit.Assert.assertFalse;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/** Tests for the {@link FallbackYarnSessionCliTest}. */
public class FallbackYarnSessionCliTest {
class FallbackYarnSessionCliTest {

@Test(expected = IllegalStateException.class)
public void testExceptionWhenActiveWithYarnApplicationId() throws ParseException {
checkIfYarnFallbackCLIisActiveWithCLIArgs(
"run", "-yid", ApplicationId.newInstance(0L, 0).toString());
@Test
void testExceptionWhenActiveWithYarnApplicationId() {
assertThatThrownBy(
() ->
checkIfYarnFallbackCLIisActiveWithCLIArgs(
"run", "-yid", ApplicationId.newInstance(0L, 0).toString()))
.isInstanceOf(IllegalStateException.class);
}

@Test(expected = IllegalStateException.class)
public void testExceptionWhenActiveWithExplicitClusterType() throws ParseException {
checkIfYarnFallbackCLIisActiveWithCLIArgs("run", "-m", FallbackYarnSessionCli.ID);
@Test
void testExceptionWhenActiveWithExplicitClusterType() {
assertThatThrownBy(
() ->
checkIfYarnFallbackCLIisActiveWithCLIArgs(
"run", "-m", FallbackYarnSessionCli.ID))
.isInstanceOf(IllegalStateException.class);
}

@Test
public void testFalseWhenNotActive() throws ParseException {
void testFalseWhenNotActive() throws ParseException {
final boolean isActive = checkIfYarnFallbackCLIisActiveWithCLIArgs("run");
assertFalse(isActive);
assertThat(isActive).isFalse();
}

private boolean checkIfYarnFallbackCLIisActiveWithCLIArgs(final String... args)

0 comments on commit 74a61e4

Please sign in to comment.