ihuzenko commented on a change in pull request #1613: DRILL-6977: Improve Hive tests configuration URL: https://github.com/apache/drill/pull/1613#discussion_r249121739
########## File path: contrib/storage-hive/core/src/test/java/org/apache/drill/exec/hive/HiveTestConfig.java ########## @@ -0,0 +1,214 @@ +/* + * 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.drill.exec.hive; + +import java.io.File; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.function.BiConsumer; +import java.util.function.Consumer; +import java.util.stream.Stream; + +import org.apache.drill.common.exceptions.ExecutionSetupException; +import org.apache.drill.exec.server.Drillbit; +import org.apache.drill.exec.store.StoragePluginRegistry; +import org.apache.drill.exec.store.hive.HiveStoragePlugin; +import org.apache.drill.exec.store.hive.HiveStoragePluginConfig; +import org.apache.drill.shaded.guava.com.google.common.base.Preconditions; +import org.apache.drill.test.BaseDirTestWatcher; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.conf.HiveConf.ConfVars; +import org.apache.hadoop.hive.ql.Driver; +import org.apache.hadoop.hive.ql.session.SessionState; + +import static org.apache.drill.exec.hive.HiveTestUtilities.createDirWithPosixPermissions; + + +/** + * Class for flexible configuration of Hive tests, it's designed to have + * minimal public API but feel free to add methods if you feel that it's + * really useful and necessary. + * <p> + * With this class you're not tied to HiveTestBase or HiveTestDataGenerator anymore + * and can generate only specific test data necessary for your test class. Such + * strategy allows faster execution of tests comparing to extending of HiveTestBase, + * because you don't have to wait while all those data from HiveTestDataGenerator will be set up. + * <p> + * Example of how this class may be used with ClusterFixture: + * <p> + * <pre><code> + * public class HiveExampleTest extends ClusterTest { + * private static HiveTestConfig hiveTestConfig; + * + * {@literal @}BeforeClass + * public static void setUp() throws Exception { + * startCluster(ClusterFixture.builder(dirTestWatcher)); + * hiveTestConfig = HiveTestConfig.dirWatcher(dirTestWatcher) + * .data(HiveExampleTest::generateData) + * .addHivePluginTo(cluster.drillbit()); + * } + * + * // may be method whose parameters suitable for Consumer<Driver> or BiConsumer<HiveTestConfig, Driver> + * private static void generateData(Driver driver) { + * // set up data using HiveTestUtilities.executeQuery(driver, sqlQuery); + * } + * + * {@literal @}AfterClass + * public static void tearDown() throws Exception { + * hiveTestConfig.removeHivePluginFrom(cluster.drillbit()); + * } + * } + * </code></pre> + */ +public class HiveTestConfig { + + private final BaseDirTestWatcher dirWatcher; + + private final File baseDir; + + private final String dbDir; + + private final String whDir; + + private HiveTestConfig(BaseDirTestWatcher dirWatcher) { + this.dirWatcher = dirWatcher; + this.baseDir = Preconditions.checkNotNull(dirWatcher.getRootDir()); + this.dbDir = new File(baseDir, "metastore_db").getAbsolutePath(); + this.whDir = new File(baseDir, "warehouse").getAbsolutePath(); + } + + public static HiveTestConfig dirWatcher(BaseDirTestWatcher dirWatcher) { + return new HiveTestConfig(dirWatcher); + } + + public HiveTestConfig data(Consumer<Driver> driverConsumer) { + // Hive data setup + final HiveConf hiveConf = newHiveConf(); + SessionState ss = new SessionState(hiveConf); + try (AutoCloseable toBeClosed = ss::close) { + SessionState.start(ss); + driverConsumer.accept(new Driver(hiveConf)); + } catch (Exception e) { + throw new RuntimeException(e); + } + return this; + } + + public HiveTestConfig data(BiConsumer<HiveTestConfig, Driver> driverConsumer) { + return data(driver -> driverConsumer.accept(this, driver)); + } + + public HiveTestConfig addHivePluginTo(Drillbit... drillbits) { + return addHivePluginTo(Arrays.asList(drillbits)); + } + + public HiveTestConfig addHivePluginTo(Iterable<Drillbit> drillbits) { + drillbits.forEach(this::addHivePlugin); + return this; + } + + public HiveTestConfig removeHivePluginFrom(Drillbit... drillbits) { + return removeHivePluginFrom(Arrays.asList(drillbits)); + } + + public HiveTestConfig removeHivePluginFrom(Iterable<Drillbit> drillbits) { + drillbits.forEach(this::removeHivePlugin); + return this; + } + + public HiveTestConfig updateHivePluginFor(Stream<Drillbit> drillbits, Review comment: Yes, fixed. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services