PageRenderTime 40ms CodeModel.GetById 11ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.0.0-rc0/src/test/org/apache/hcatalog/cli/TestPermsGrp.java

#
Java | 239 lines | 169 code | 43 blank | 27 comment | 2 complexity | 2769d197eeeb4c2dfa8009a76b3a8423 MD5 | raw file
  1/*
  2 * Licensed to the Apache Software Foundation (ASF) under one
  3 * or more contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  The ASF licenses this file
  6 * to you under the Apache License, Version 2.0 (the
  7 * "License"); you may not use this file except in compliance
  8 * with the License.  You may obtain a copy of the License at
  9 *
 10 *     http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18package org.apache.hcatalog.cli;
 19
 20import java.io.FileNotFoundException;
 21import java.util.ArrayList;
 22
 23import junit.framework.TestCase;
 24
 25import org.apache.hadoop.fs.Path;
 26import org.apache.hadoop.fs.permission.FsPermission;
 27import org.apache.hadoop.hive.conf.HiveConf;
 28import org.apache.hadoop.hive.metastore.HiveMetaStore;
 29import org.apache.hadoop.hive.metastore.HiveMetaStoreClient;
 30import org.apache.hadoop.hive.metastore.MetaStoreUtils;
 31import org.apache.hadoop.hive.metastore.Warehouse;
 32import org.apache.hadoop.hive.metastore.api.AlreadyExistsException;
 33import org.apache.hadoop.hive.metastore.api.FieldSchema;
 34import org.apache.hadoop.hive.metastore.api.InvalidObjectException;
 35import org.apache.hadoop.hive.metastore.api.InvalidOperationException;
 36import org.apache.hadoop.hive.metastore.api.MetaException;
 37import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
 38import org.apache.hadoop.hive.metastore.api.SerDeInfo;
 39import org.apache.hadoop.hive.metastore.api.StorageDescriptor;
 40import org.apache.hadoop.hive.metastore.api.Table;
 41import org.apache.hadoop.hive.metastore.api.Type;
 42import org.apache.hadoop.hive.serde.Constants;
 43import org.apache.hadoop.util.StringUtils;
 44import org.apache.hcatalog.ExitException;
 45import org.apache.hcatalog.NoExitSecurityManager;
 46import org.apache.hcatalog.cli.HCatCli;
 47import org.apache.hcatalog.cli.SemanticAnalysis.HCatSemanticAnalyzer;
 48import org.apache.hcatalog.common.HCatConstants;
 49import org.apache.thrift.TException;
 50
 51public class TestPermsGrp extends TestCase {
 52
 53  private boolean isServerRunning = false;
 54  private static final String msPort = "20101";
 55  private HiveConf howlConf;
 56  private Warehouse clientWH;
 57  private Thread t;
 58  private HiveMetaStoreClient msc;
 59
 60  private static class RunMS implements Runnable {
 61
 62    @Override
 63    public void run() {
 64      HiveMetaStore.main(new String[]{msPort});
 65    }
 66
 67  }
 68
 69  @Override
 70  protected void tearDown() throws Exception {
 71    System.setSecurityManager(securityManager);
 72  }
 73
 74  @Override
 75  protected void setUp() throws Exception {
 76
 77    if(isServerRunning) {
 78      return;
 79    }
 80
 81    t = new Thread(new RunMS());
 82    t.start();
 83    Thread.sleep(40000);
 84
 85    isServerRunning = true;
 86
 87    securityManager = System.getSecurityManager();
 88    System.setSecurityManager(new NoExitSecurityManager());
 89
 90    howlConf = new HiveConf(this.getClass());
 91    howlConf.set("hive.metastore.local", "false");
 92    howlConf.setVar(HiveConf.ConfVars.METASTOREURIS, "thrift://localhost:" + msPort);
 93    howlConf.setIntVar(HiveConf.ConfVars.METASTORETHRIFTRETRIES, 3);
 94
 95    howlConf.set(HiveConf.ConfVars.SEMANTIC_ANALYZER_HOOK.varname, HCatSemanticAnalyzer.class.getName());
 96    howlConf.set(HiveConf.ConfVars.PREEXECHOOKS.varname, "");
 97    howlConf.set(HiveConf.ConfVars.POSTEXECHOOKS.varname, "");
 98    howlConf.set(HiveConf.ConfVars.HIVE_SUPPORT_CONCURRENCY.varname, "false");
 99    clientWH = new Warehouse(howlConf);
100    msc = new HiveMetaStoreClient(howlConf,null);
101    System.setProperty(HiveConf.ConfVars.PREEXECHOOKS.varname, " ");
102    System.setProperty(HiveConf.ConfVars.POSTEXECHOOKS.varname, " ");
103  }
104
105
106  public void testCustomPerms() throws Exception {
107
108    String dbName = MetaStoreUtils.DEFAULT_DATABASE_NAME;
109    String tblName = "simptbl";
110    String typeName = "Person";
111
112    try {
113
114      // Lets first test for default permissions, this is the case when user specified nothing.
115      Table tbl = getTable(dbName,tblName,typeName);
116      msc.createTable(tbl);
117      Path dfsPath = clientWH.getDefaultTablePath(dbName, tblName);
118      cleanupTbl(dbName, tblName, typeName);
119
120      // Next user did specify perms.
121      try{
122        HCatCli.main(new String[]{"-e","create table simptbl (name string) stored as RCFILE", "-p","rwx-wx---"});
123      }
124      catch(Exception e){
125        assertTrue(e instanceof ExitException);
126        assertEquals(((ExitException)e).getStatus(), 0);
127      }
128      dfsPath = clientWH.getDefaultTablePath(dbName, tblName);
129      assertTrue(dfsPath.getFileSystem(howlConf).getFileStatus(dfsPath).getPermission().equals(FsPermission.valueOf("drwx-wx---")));
130
131      cleanupTbl(dbName, tblName, typeName);
132
133      // User specified perms in invalid format.
134      howlConf.set(HCatConstants.HCAT_PERMS, "rwx");
135      // make sure create table fails.
136      try{
137        HCatCli.main(new String[]{"-e","create table simptbl (name string) stored as RCFILE", "-p","rwx"});
138        assert false;
139      }catch(Exception me){
140        assertTrue(me instanceof ExitException);
141      }
142      // No physical dir gets created.
143      dfsPath = clientWH.getDefaultTablePath(MetaStoreUtils.DEFAULT_DATABASE_NAME,tblName);
144      try{
145        dfsPath.getFileSystem(howlConf).getFileStatus(dfsPath);
146        assert false;
147      } catch(Exception fnfe){
148        assertTrue(fnfe instanceof FileNotFoundException);
149      }
150
151      // And no metadata gets created.
152      try{
153        msc.getTable(MetaStoreUtils.DEFAULT_DATABASE_NAME, tblName);
154        assert false;
155      }catch (Exception e){
156        assertTrue(e instanceof NoSuchObjectException);
157        assertEquals("default.simptbl table not found", e.getMessage());
158      }
159
160      // test for invalid group name
161      howlConf.set(HCatConstants.HCAT_PERMS, "drw-rw-rw-");
162      howlConf.set(HCatConstants.HCAT_GROUP, "THIS_CANNOT_BE_A_VALID_GRP_NAME_EVER");
163
164      try{
165        // create table must fail.
166        HCatCli.main(new String[]{"-e","create table simptbl (name string) stored as RCFILE", "-p","rw-rw-rw-","-g","THIS_CANNOT_BE_A_VALID_GRP_NAME_EVER"});
167        assert false;
168      }catch (Exception me){
169        assertTrue(me instanceof SecurityException);
170      }
171
172      try{
173        // no metadata should get created.
174        msc.getTable(dbName, tblName);
175        assert false;
176      }catch (Exception e){
177        assertTrue(e instanceof NoSuchObjectException);
178        assertEquals("default.simptbl table not found", e.getMessage());
179      }
180      try{
181        // neither dir should get created.
182        dfsPath.getFileSystem(howlConf).getFileStatus(dfsPath);
183        assert false;
184      } catch(Exception e){
185        assertTrue(e instanceof FileNotFoundException);
186      }
187
188    } catch (Exception e) {
189      System.err.println(StringUtils.stringifyException(e));
190      System.err.println("testCustomPerms failed.");
191      throw e;
192    }
193  }
194
195  private void silentDropDatabase(String dbName) throws MetaException, TException {
196    try {
197      for (String tableName : msc.getTables(dbName, "*")) {
198        msc.dropTable(dbName, tableName);
199      }
200
201    } catch (NoSuchObjectException e) {
202    }
203  }
204
205  private void cleanupTbl(String dbName, String tblName, String typeName) throws NoSuchObjectException, MetaException, TException, InvalidOperationException{
206
207    msc.dropTable(dbName, tblName);
208    msc.dropType(typeName);
209  }
210
211  private Table getTable(String dbName, String tblName, String typeName) throws NoSuchObjectException, MetaException, TException, AlreadyExistsException, InvalidObjectException{
212
213    msc.dropTable(dbName, tblName);
214    silentDropDatabase(dbName);
215
216
217    msc.dropType(typeName);
218    Type typ1 = new Type();
219    typ1.setName(typeName);
220    typ1.setFields(new ArrayList<FieldSchema>(1));
221    typ1.getFields().add(new FieldSchema("name", Constants.STRING_TYPE_NAME, ""));
222    msc.createType(typ1);
223
224    Table tbl = new Table();
225    tbl.setDbName(dbName);
226    tbl.setTableName(tblName);
227    StorageDescriptor sd = new StorageDescriptor();
228    tbl.setSd(sd);
229    sd.setCols(typ1.getFields());
230
231    sd.setSerdeInfo(new SerDeInfo());
232    return tbl;
233  }
234
235
236
237  private SecurityManager securityManager;
238
239}