tillrohrmann commented on a change in pull request #17485: URL: https://github.com/apache/flink/pull/17485#discussion_r791733602
########## File path: flink-kubernetes/src/test/java/org/apache/flink/kubernetes/highavailability/KubernetesTestFixture.java ########## @@ -0,0 +1,236 @@ +/* + * 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.highavailability; + +import org.apache.flink.configuration.Configuration; +import org.apache.flink.kubernetes.configuration.KubernetesConfigOptions; +import org.apache.flink.kubernetes.kubeclient.FlinkKubeClient; +import org.apache.flink.kubernetes.kubeclient.KubernetesConfigMapSharedWatcher; +import org.apache.flink.kubernetes.kubeclient.TestingFlinkKubeClient; +import org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap; +import org.apache.flink.kubernetes.kubeclient.resources.KubernetesException; +import org.apache.flink.kubernetes.kubeclient.resources.KubernetesLeaderElector; +import org.apache.flink.kubernetes.utils.KubernetesUtils; +import org.apache.flink.util.concurrent.FutureUtils; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.TimeUnit; + +import static org.apache.flink.kubernetes.kubeclient.resources.KubernetesLeaderElector.LEADER_ANNOTATION_KEY; +import static org.apache.flink.kubernetes.utils.Constants.LABEL_CONFIGMAP_TYPE_HIGH_AVAILABILITY; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.greaterThanOrEqualTo; +import static org.hamcrest.Matchers.is; + +/** Test fixture for Kubernetes tests that sets up a mock {@link FlinkKubeClient}. */ +class KubernetesTestFixture { + private static final long TIMEOUT = 30L * 1000L; + + private final String leaderConfigmapName; + private final String lockIdentity; + + private final Configuration configuration; + + private final Map<String, KubernetesConfigMap> configMapStore = new HashMap<>(); + + private final List<CompletableFuture<FlinkKubeClient.WatchCallbackHandler<KubernetesConfigMap>>> + configMapCallbackFutures = new ArrayList<>(); + + private final List<TestingFlinkKubeClient.MockKubernetesWatch> configMapWatches = + new ArrayList<>(); + + private final CompletableFuture<Map<String, String>> deleteConfigMapByLabelsFuture = + new CompletableFuture<>(); + private final CompletableFuture<Void> closeKubeClientFuture = new CompletableFuture<>(); + + private final CompletableFuture<KubernetesLeaderElector.LeaderCallbackHandler> + leaderCallbackHandlerFuture = new CompletableFuture<>(); + + private final FlinkKubeClient flinkKubeClient; + + private final KubernetesConfigMapSharedWatcher configMapSharedWatcher; + + KubernetesTestFixture(String clusterId, String leaderConfigmapName, String lockIdentity) { + this.leaderConfigmapName = leaderConfigmapName; + this.lockIdentity = lockIdentity; + configuration = new Configuration(); + configuration.setString(KubernetesConfigOptions.CLUSTER_ID, clusterId); + + flinkKubeClient = createFlinkKubeClient(); + configMapSharedWatcher = + flinkKubeClient.createConfigMapSharedWatcher( + KubernetesUtils.getConfigMapLabels( + clusterId, LABEL_CONFIGMAP_TYPE_HIGH_AVAILABILITY)); + } + + void close() { + configMapSharedWatcher.close(); + } + + FlinkKubeClient getFlinkKubeClient() { + return flinkKubeClient; + } + + CompletableFuture<Void> getCloseKubeClientFuture() { + return closeKubeClientFuture; + } + + CompletableFuture<Map<String, String>> getDeleteConfigMapByLabelsFuture() { + return deleteConfigMapByLabelsFuture; + } + + KubernetesConfigMapSharedWatcher getConfigMapSharedWatcher() { + return configMapSharedWatcher; + } + + Configuration getConfiguration() { + return configuration; + } + + KubernetesConfigMap getLeaderConfigMap() { + final Optional<KubernetesConfigMap> configMapOpt = + flinkKubeClient.getConfigMap(leaderConfigmapName); + assertThat(configMapOpt.isPresent(), is(true)); + return configMapOpt.get(); + } + + // Use the leader callback to manually grant leadership + void leaderCallbackGrantLeadership() throws Exception { + createLeaderConfigMap(); + getLeaderCallback().isLeader(); + } + + FlinkKubeClient.WatchCallbackHandler<KubernetesConfigMap> getLeaderElectionConfigMapCallback() + throws Exception { + assertThat(configMapCallbackFutures.size(), is(greaterThanOrEqualTo(1))); Review comment: We probably are. I am also not super happy with the current state but I also didn't want to completely refactor the existing K8s tests. Hence, I would suggest to create a follow up task for cleaning this up. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
