xintongsong commented on a change in pull request #13186:
URL: https://github.com/apache/flink/pull/13186#discussion_r476256982



##########
File path: 
flink-kubernetes/src/test/java/org/apache/flink/kubernetes/KubernetesResourceManagerDriverTest.java
##########
@@ -0,0 +1,295 @@
+/*
+ * 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.flink.kubernetes;
+
+import org.apache.flink.api.common.time.Time;
+import org.apache.flink.configuration.TaskManagerOptions;
+import org.apache.flink.kubernetes.configuration.KubernetesConfigOptions;
+import 
org.apache.flink.kubernetes.configuration.KubernetesResourceManagerDriverConfiguration;
+import 
org.apache.flink.kubernetes.kubeclient.FlinkKubeClient.PodCallbackHandler;
+import org.apache.flink.kubernetes.kubeclient.TestingFlinkKubeClient;
+import org.apache.flink.kubernetes.kubeclient.resources.KubernetesPod;
+import org.apache.flink.kubernetes.kubeclient.resources.TestingKubernetesPod;
+import org.apache.flink.kubernetes.utils.Constants;
+import org.apache.flink.runtime.clusterframework.TaskExecutorProcessSpec;
+import org.apache.flink.runtime.clusterframework.types.ResourceID;
+import org.apache.flink.runtime.concurrent.FutureUtils;
+import org.apache.flink.runtime.resourcemanager.active.ResourceManagerDriver;
+import 
org.apache.flink.runtime.resourcemanager.active.ResourceManagerDriverTestBase;
+
+import io.fabric8.kubernetes.api.model.ResourceRequirements;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Consumer;
+
+import static org.hamcrest.Matchers.greaterThanOrEqualTo;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Tests for {@link KubernetesResourceManagerDriver}.
+ */
+public class KubernetesResourceManagerDriverTest extends 
ResourceManagerDriverTestBase<KubernetesWorkerNode> {
+
+       private static final String CLUSTER_ID = "testing-flink-cluster";
+       private static final Time POD_CREATION_INTERVAL = 
Time.milliseconds(50L);
+       private static final KubernetesResourceManagerDriverConfiguration 
KUBERNETES_RESOURCE_MANAGER_CONFIGURATION =
+                       new 
KubernetesResourceManagerDriverConfiguration(CLUSTER_ID, POD_CREATION_INTERVAL);
+
+       @Test
+       public void testOnPodAdded() throws Exception {
+               new Context() {{
+                       final CompletableFuture<KubernetesPod> createPodFuture 
= new CompletableFuture<>();
+                       final CompletableFuture<KubernetesWorkerNode> 
requestResourceFuture = new CompletableFuture<>();
+
+                       
flinkKubeClientBuilder.setCreateTaskManagerPodFunction((pod) -> {
+                               createPodFuture.complete(pod);
+                               return FutureUtils.completedVoidFuture();
+                       });
+
+                       runTest(() -> {
+                               // request new pod
+                               runInMainThread(() -> 
getDriver().requestResource(TASK_EXECUTOR_PROCESS_SPEC).thenAccept(requestResourceFuture::complete));
+                               final KubernetesPod pod = 
createPodFuture.get(TIMEOUT_SEC, TimeUnit.SECONDS);
+
+                               // prepare validation:
+                               // - complete requestResourceFuture in main 
thread with correct KubernetesWorkerNode
+                               final CompletableFuture<Void> validationFuture 
= requestResourceFuture.thenAccept((workerNode) -> {
+                                       validateInMainThread();
+                                       
assertThat(workerNode.getResourceID().toString(), is(pod.getName()));
+                               });
+
+                               // send onAdded event
+                               
getPodCallbackHandler().onAdded(Collections.singletonList(pod));
+
+                               // make sure finishing validation
+                               validationFuture.get(TIMEOUT_SEC, 
TimeUnit.SECONDS);
+                       });
+               }};
+       }
+
+       @Test
+       public void testOnPodModified() throws Exception {
+               new Context() {{
+                       testOnPodTerminated((pod) -> 
getPodCallbackHandler().onModified(pod));
+               }};
+       }
+
+       @Test
+       public void testOnPodDeleted() throws Exception {
+               new Context() {{
+                       testOnPodTerminated((pod) -> 
getPodCallbackHandler().onDeleted(pod));
+               }};
+       }
+
+       @Test
+       public void testOnError() throws Exception {
+               new Context() {{
+                       testOnPodTerminated((pod) -> 
getPodCallbackHandler().onError(pod));
+               }};
+       }
+
+       @Test
+       public void testFatalHandleError() throws Exception {
+               new Context() {{
+                       final CompletableFuture<Throwable> onErrorFuture = new 
CompletableFuture<>();
+                       
resourceEventHandlerBuilder.setOnErrorConsumer(onErrorFuture::complete);
+
+                       runTest(() -> {
+                               final Throwable testingError = new 
Throwable("testing error");
+                               
getPodCallbackHandler().handleFatalError(testingError);
+                               assertThat(onErrorFuture.get(TIMEOUT_SEC, 
TimeUnit.SECONDS), is(testingError));
+                       });
+               }};
+       }
+
+       @Test
+       public void testPodCreationInterval() throws Exception {
+               new Context() {{
+                       final AtomicInteger createPodCount = new 
AtomicInteger(0);
+                       final List<CompletableFuture<Long>> 
createPodTimeFutures = new ArrayList<>();
+                       createPodTimeFutures.add(new CompletableFuture<>());
+                       createPodTimeFutures.add(new CompletableFuture<>());
+
+                       
flinkKubeClientBuilder.setCreateTaskManagerPodFunction((ignore) -> {
+                               int idx = createPodCount.getAndIncrement();
+                               if (idx < createPodTimeFutures.size()) {
+                                       
createPodTimeFutures.get(idx).complete(System.currentTimeMillis());
+                               }
+                               return FutureUtils.completedExceptionally(new 
Throwable("testing error"));
+                       });
+
+                       runTest(() -> {
+                               // re-request resource on pod creation failed
+                               runInMainThread(() -> 
getDriver().requestResource(TASK_EXECUTOR_PROCESS_SPEC)
+                                               .whenComplete((ignore1, 
ignore2) -> getDriver().requestResource(TASK_EXECUTOR_PROCESS_SPEC)));
+
+                               // validate trying creating pod twice, with 
proper interval
+                               long t1 = 
createPodTimeFutures.get(0).get(TIMEOUT_SEC, TimeUnit.SECONDS);
+                               long t2 = 
createPodTimeFutures.get(1).get(TIMEOUT_SEC, TimeUnit.SECONDS);
+                               assertThat((t2 - t1), 
greaterThanOrEqualTo(POD_CREATION_INTERVAL.toMilliseconds()));
+                       });
+               }};
+       }
+
+       @Override
+       protected ResourceManagerDriverTestBase<KubernetesWorkerNode>.Context 
createContext() {
+               return new Context();
+       }
+
+       private class Context extends 
ResourceManagerDriverTestBase<KubernetesWorkerNode>.Context {
+               private final KubernetesPod previousAttemptPod = new 
TestingKubernetesPod(CLUSTER_ID + "-taskmanager-1-1");
+
+               final CompletableFuture<PodCallbackHandler> 
setWatchPodsAndDoCallbackFuture = new CompletableFuture<>();

Review comment:
       Well, I was not intended to prevent these variables from being accessed 
by the test cases.
   But I think you are right, it does not hurt to make the `private`. They are 
not accessed from anywhere else, and one could change that once such accesses 
are needed.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to