| /** |
| * 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.hcatalog.cli; |
| |
| import java.io.IOException; |
| import java.net.URI; |
| |
| import junit.framework.TestCase; |
| |
| import org.apache.hadoop.fs.Path; |
| import org.apache.hadoop.fs.permission.FsPermission; |
| import org.apache.hadoop.hive.cli.CliSessionState; |
| import org.apache.hadoop.hive.conf.HiveConf; |
| import org.apache.hadoop.hive.conf.HiveConf.ConfVars; |
| import org.apache.hadoop.hive.metastore.Warehouse; |
| import org.apache.hadoop.hive.metastore.api.MetaException; |
| import org.apache.hadoop.hive.ql.metadata.Hive; |
| import org.apache.hadoop.hive.ql.metadata.HiveException; |
| import org.apache.hadoop.hive.ql.processors.CommandProcessorResponse; |
| import org.apache.hadoop.hive.ql.session.SessionState; |
| import org.apache.hcatalog.MiniCluster; |
| import org.apache.hcatalog.cli.SemanticAnalysis.HCatSemanticAnalyzer; |
| import org.apache.hcatalog.common.HCatConstants; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| |
| public class TestEximSemanticAnalysis extends TestCase { |
| |
| private final MiniCluster cluster = MiniCluster.buildCluster(); |
| private HiveConf hcatConf; |
| private HCatDriver hcatDriver; |
| private Warehouse wh; |
| private static final Logger LOG = LoggerFactory.getLogger(TestEximSemanticAnalysis.class); |
| |
| @Override |
| protected void setUp() throws Exception { |
| |
| hcatConf = new HiveConf(this.getClass()); |
| hcatConf.set(HiveConf.ConfVars.PREEXECHOOKS.varname, ""); |
| hcatConf.set(HiveConf.ConfVars.POSTEXECHOOKS.varname, ""); |
| hcatConf.set(HiveConf.ConfVars.HIVE_SUPPORT_CONCURRENCY.varname, "false"); |
| hcatConf.set(ConfVars.SEMANTIC_ANALYZER_HOOK.varname, HCatSemanticAnalyzer.class.getName()); |
| hcatConf.set("fs.pfile.impl", "org.apache.hadoop.fs.ProxyLocalFileSystem"); |
| URI fsuri = cluster.getFileSystem().getUri(); |
| Path whPath = new Path(fsuri.getScheme(), fsuri.getAuthority(), "/user/hive/warehouse"); |
| hcatConf.set(HiveConf.ConfVars.HADOOPFS.varname, fsuri.toString()); |
| hcatConf.set(ConfVars.METASTOREWAREHOUSE.varname, whPath.toString()); |
| wh = new Warehouse(hcatConf); |
| SessionState.start(new CliSessionState(hcatConf)); |
| |
| hcatDriver = new HCatDriver(); |
| } |
| |
| @Override |
| protected void tearDown() throws Exception { |
| } |
| |
| public void testExportPerms() throws IOException, MetaException, HiveException { |
| |
| hcatDriver.run("drop table junit_sem_analysis"); |
| CommandProcessorResponse response = hcatDriver |
| .run("create table junit_sem_analysis (a int) partitioned by (b string) stored as RCFILE"); |
| assertEquals(0, response.getResponseCode()); |
| Path whPath = wh.getTablePath(Hive.get(hcatConf).getDatabase("default"), "junit_sem_analysis"); |
| cluster.getFileSystem().setPermission(whPath, FsPermission.valueOf("-rwxrwx-wx")); |
| cluster.getFileSystem().setOwner(whPath, "nosuchuser", "nosuchgroup"); |
| |
| Runtime.getRuntime().exec("rm -rf /tmp/hcat"); |
| response = hcatDriver |
| .run("export table junit_sem_analysis to 'pfile://local:9080/tmp/hcat/exports/junit_sem_analysis'"); |
| |
| assertEquals(10, response.getResponseCode()); |
| assertTrue("Permission denied expected : "+response.getErrorMessage(), |
| response.getErrorMessage().startsWith( |
| "FAILED: Error in semantic analysis: org.apache.hcatalog.common.HCatException : 3000 : Permission denied")); |
| Runtime.getRuntime().exec("rm -rf /tmp/hcat"); |
| response = hcatDriver.run("drop table junit_sem_analysis"); |
| if (response.getResponseCode() != 0) { |
| LOG.error(response.getErrorMessage()); |
| fail("Drop table failed"); |
| } |
| } |
| |
| public void testImportPerms() throws IOException, MetaException, HiveException { |
| |
| hcatDriver.run("drop table junit_sem_analysis"); |
| CommandProcessorResponse response = hcatDriver |
| .run("create table junit_sem_analysis (a int) partitioned by (b string) stored as RCFILE"); |
| assertEquals(0, response.getResponseCode()); |
| Runtime.getRuntime().exec("rm -rf /tmp/hcat"); |
| response = hcatDriver |
| .run("export table junit_sem_analysis to 'pfile://local:9080/tmp/hcat/exports/junit_sem_analysis'"); |
| assertEquals(0, response.getResponseCode()); |
| response = hcatDriver.run("drop table junit_sem_analysis"); |
| assertEquals(0, response.getResponseCode()); |
| response = hcatDriver |
| .run("create table junit_sem_analysis (a int) partitioned by (b string) stored as RCFILE"); |
| assertEquals(0, response.getResponseCode()); |
| Path whPath = wh.getTablePath(Hive.get(hcatConf).getDatabase("default"), "junit_sem_analysis"); |
| cluster.getFileSystem().setPermission(whPath, FsPermission.valueOf("-rwxrwxr-x")); |
| cluster.getFileSystem().setOwner(whPath, "nosuchuser", "nosuchgroup"); |
| |
| response = hcatDriver |
| .run("import table junit_sem_analysis from 'pfile://local:9080/tmp/hcat/exports/junit_sem_analysis'"); |
| |
| assertEquals(10, response.getResponseCode()); |
| assertTrue( |
| "Permission denied expected: "+response.getErrorMessage() , |
| response.getErrorMessage().startsWith( |
| "FAILED: Error in semantic analysis: org.apache.hcatalog.common.HCatException : 3000 : Permission denied")); |
| Runtime.getRuntime().exec("rm -rf /tmp/hcat"); |
| |
| cluster.getFileSystem().setPermission(whPath, FsPermission.valueOf("-rwxrwxrwx")); |
| response = hcatDriver.run("drop table junit_sem_analysis"); |
| if (response.getResponseCode() != 0) { |
| LOG.error(response.getErrorMessage()); |
| fail("Drop table failed"); |
| } |
| } |
| |
| public void testImportSetPermsGroup() throws IOException, MetaException, HiveException { |
| |
| hcatDriver.run("drop table junit_sem_analysis"); |
| hcatDriver.run("drop table junit_sem_analysis_imported"); |
| CommandProcessorResponse response = hcatDriver |
| .run("create table junit_sem_analysis (a int) partitioned by (b string) stored as RCFILE"); |
| assertEquals(0, response.getResponseCode()); |
| Runtime.getRuntime().exec("rm -rf /tmp/hcat"); |
| response = hcatDriver |
| .run("export table junit_sem_analysis to 'pfile://local:9080/tmp/hcat/exports/junit_sem_analysis'"); |
| assertEquals(0, response.getResponseCode()); |
| response = hcatDriver.run("drop table junit_sem_analysis"); |
| assertEquals(0, response.getResponseCode()); |
| |
| hcatConf.set(HCatConstants.HCAT_PERMS, "-rwxrw-r--"); |
| hcatConf.set(HCatConstants.HCAT_GROUP, "nosuchgroup"); |
| |
| response = hcatDriver |
| .run("import table junit_sem_analysis_imported from 'pfile://local:9080/tmp/hcat/exports/junit_sem_analysis'"); |
| assertEquals(0, response.getResponseCode()); |
| |
| Path whPath = wh.getTablePath(Hive.get(hcatConf).getDatabase("default"), "junit_sem_analysis_imported"); |
| assertEquals(FsPermission.valueOf("-rwxrw-r--"), cluster.getFileSystem().getFileStatus(whPath).getPermission()); |
| assertEquals("nosuchgroup", cluster.getFileSystem().getFileStatus(whPath).getGroup()); |
| |
| Runtime.getRuntime().exec("rm -rf /tmp/hcat"); |
| |
| response = hcatDriver.run("drop table junit_sem_analysis_imported"); |
| if (response.getResponseCode() != 0) { |
| LOG.error(response.getErrorMessage()); |
| fail("Drop table failed"); |
| } |
| } |
| |
| |
| } |
| |