diff --git a/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestMsck.java b/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestMsck.java new file mode 100644 index 0000000000..f1335ed196 --- /dev/null +++ b/itests/hive-unit/src/test/java/org/apache/hive/beeline/TestMsck.java @@ -0,0 +1,112 @@ +/* + * 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.hive.beeline; + +import static org.junit.Assert.fail; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hive.jdbc.miniHS2.MiniHS2; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class TestMsck { + public static final boolean IS_METASTORE_REMOTE = true; + private static MiniHS2 miniHS2; + private static final String userName = System.getProperty("user.name"); + + /** + * Start up a local Hive Server 2 for these tests + */ + @BeforeClass + public static void preTests() throws Exception { + HiveConf hiveConf = new HiveConf(); + hiveConf.setVar(HiveConf.ConfVars.HIVE_LOCK_MANAGER, + "org.apache.hadoop.hive.ql.lockmgr.EmbeddedLockManager"); + hiveConf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_RESULTSET_DEFAULT_FETCH_SIZE, 10); + hiveConf.setBoolVar(HiveConf.ConfVars.HIVEOPTIMIZEMETADATAQUERIES, false); + hiveConf.set(HiveConf.ConfVars.HIVE_SERVER2_LOGGING_OPERATION_LEVEL.varname, "verbose"); + miniHS2 = new MiniHS2(hiveConf, MiniHS2.MiniClusterType.TEZ, false, IS_METASTORE_REMOTE); + Map confOverlay = new HashMap<>(); + miniHS2.start(confOverlay); + } + + @AfterClass + public static void postTests() { + if (miniHS2.isStarted()) { + miniHS2.stop(); + } + } + + @Test + public void testMsck() throws Throwable { + String script = "create external table t1 (c1 int, c2 int) partitioned by (c3 int) location 'file:${system:test.tmp.dir}/abcdef';\n" + + "insert into t1 partition(c3=1) values (1,1);\n" + + "insert into t1 partition(c3=2) values (2,2);\n" + + "insert into t1 partition(c3=3) values (3,3);\n" + + String.format("!sh rm -rf %s/abcdef/c3=3\n", System.getProperty("test.tmp.dir")) + + "msck repair table t1 sync partitions;\n"; + testScriptFile(script, args()); + } + + private static List args() { + return Arrays.asList("-d", BeeLine.BEELINE_DEFAULT_JDBC_DRIVER, + "-u", miniHS2.getBaseJdbcURL(), "-n", userName); + } + + private void testScriptFile(String scriptText, List argList) + throws Throwable { + File scriptFile = File.createTempFile(this.getClass().getSimpleName(), "temp"); + scriptFile.deleteOnExit(); + try (PrintStream os = new PrintStream(new FileOutputStream(scriptFile))) { + os.print(scriptText); + } + List copy = new ArrayList<>(argList); + copy.add("-f"); + copy.add(scriptFile.getAbsolutePath()); + String output = testCommandLineScript(copy, null); + System.out.printf(output); + scriptFile.delete(); + } + + private static String testCommandLineScript(List argList, InputStream inputStream) + throws Throwable { + BeeLine beeLine = new BeeLine(); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + PrintStream beelineOutputStream = new PrintStream(os); + beeLine.setOutputStream(beelineOutputStream); + String[] args = argList.toArray(new String[argList.size()]); + beeLine.begin(args, inputStream); + beeLine.close(); + beelineOutputStream.close(); + String output = os.toString("UTF8"); + return output; + } +}