bowenli86 commented on a change in pull request #8987: [FLINK-13090][hive] Test Hive connector with hive runner URL: https://github.com/apache/flink/pull/8987#discussion_r300811534
########## File path: flink-connectors/flink-connector-hive/src/test/java/org/apache/flink/batch/connectors/hive/FlinkStandaloneHiveRunner.java ########## @@ -0,0 +1,465 @@ +/* + * 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.batch.connectors.hive; + +import org.apache.flink.table.catalog.hive.HiveTestUtils; +import org.apache.flink.util.Preconditions; + +import org.apache.flink.shaded.guava18.com.google.common.base.Joiner; +import org.apache.flink.shaded.guava18.com.google.common.base.Splitter; +import org.apache.flink.shaded.guava18.com.google.common.base.Throwables; +import org.apache.flink.shaded.guava18.com.google.common.io.Resources; + +import com.klarna.hiverunner.HiveServerContainer; +import com.klarna.hiverunner.HiveServerContext; +import com.klarna.hiverunner.HiveShell; +import com.klarna.hiverunner.HiveShellContainer; +import com.klarna.hiverunner.annotations.HiveProperties; +import com.klarna.hiverunner.annotations.HiveResource; +import com.klarna.hiverunner.annotations.HiveSQL; +import com.klarna.hiverunner.annotations.HiveSetupScript; +import com.klarna.hiverunner.builder.HiveShellBuilder; +import com.klarna.hiverunner.config.HiveRunnerConfig; +import com.klarna.reflection.ReflectionUtils; +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.metastore.HiveMetaStore; +import org.junit.Ignore; +import org.junit.internal.AssumptionViolatedException; +import org.junit.internal.runners.model.EachTestNotifier; +import org.junit.rules.ExternalResource; +import org.junit.rules.TemporaryFolder; +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runner.notification.RunNotifier; +import org.junit.runners.BlockJUnit4ClassRunner; +import org.junit.runners.model.FrameworkMethod; +import org.junit.runners.model.InitializationError; +import org.junit.runners.model.Statement; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import sun.net.www.ParseUtil; + +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.lang.reflect.Field; +import java.net.ConnectException; +import java.net.InetSocketAddress; +import java.nio.channels.SocketChannel; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; +import java.util.concurrent.TimeUnit; + +import static org.apache.hadoop.hive.conf.HiveConf.ConfVars.HIVEHISTORYFILELOC; +import static org.apache.hadoop.hive.conf.HiveConf.ConfVars.HIVE_WAREHOUSE_SUBDIR_INHERIT_PERMS; +import static org.apache.hadoop.hive.conf.HiveConf.ConfVars.LOCALSCRATCHDIR; +import static org.apache.hadoop.hive.conf.HiveConf.ConfVars.METASTORECONNECTURLKEY; +import static org.apache.hadoop.hive.conf.HiveConf.ConfVars.METASTOREWAREHOUSE; +import static org.apache.hadoop.hive.conf.HiveConf.ConfVars.SCRATCHDIR; +import static org.reflections.ReflectionUtils.withAnnotation; + +/** + * JUnit 4 runner that runs hive sql on a HiveServer residing in this JVM. No external dependencies needed. + * Inspired by StandaloneHiveRunner.java (almost copied), just using local meta store server instead of embedded + * hive meta store. + */ +public class FlinkStandaloneHiveRunner extends BlockJUnit4ClassRunner { + private static final Logger LOGGER = LoggerFactory.getLogger(FlinkStandaloneHiveRunner.class); + private static final Duration HMS_START_TIMEOUT = Duration.ofSeconds(30); + private Future<Void> hmsWatcher; + private int hmsPort; + private HiveShellContainer container; + private HiveRunnerConfig config = new HiveRunnerConfig(); + + public FlinkStandaloneHiveRunner(Class<?> clazz) throws InitializationError { + super(clazz); + } + + @Override + protected List<TestRule> classRules() { + final TemporaryFolder temporaryFolder = new TemporaryFolder(); + try { + hmsPort = HiveTestUtils.getFreePort(); + } catch (IOException e) { + throw new RuntimeException(e); + } + HiveServerContext context = new FlinkStandaloneHiveServerContext(temporaryFolder, config, hmsPort); + List<TestRule> rules = super.classRules(); + ExternalResource hms = new ExternalResource() { + @Override + protected void before() throws Throwable { + LOGGER.info("Setting up {} in {}", getName(), temporaryFolder.getRoot().getAbsolutePath()); + hmsWatcher = startHMS(context, hmsPort); + } + + @Override + protected void after() { + if (hmsWatcher != null) { + hmsWatcher.cancel(true); + } + } + }; + ExternalResource hiveShell = new ExternalResource() { + @Override + protected void before() throws Throwable { + container = createHiveServerContainer(getTestClass().getJavaClass(), context); + } + + @Override + protected void after() { + tearDown(); + } + }; + rules.add(hiveShell); + rules.add(hms); + rules.add(temporaryFolder); + return rules; + } + + @Override + protected void runChild(final FrameworkMethod method, RunNotifier notifier) { + Description description = describeChild(method); + if (method.getAnnotation(Ignore.class) != null) { + notifier.fireTestIgnored(description); + } else { + EachTestNotifier eachNotifier = new EachTestNotifier(notifier, description); + eachNotifier.fireTestStarted(); + try { + runTestMethod(method, eachNotifier); + } finally { + eachNotifier.fireTestFinished(); + } + } + } + + Review comment: remove extra line? ---------------------------------------------------------------- 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] With regards, Apache Git Services
