001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.backup;
019
020import java.io.IOException;
021import java.security.PrivilegedAction;
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.fs.FileSystem;
024import org.apache.hadoop.fs.Path;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseTestingUtil;
027import org.apache.hadoop.hbase.backup.util.BackupUtils;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.apache.hadoop.security.UserGroupInformation;
030import org.junit.Assert;
031import org.junit.ClassRule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037@Category(SmallTests.class)
038public class TestBackupUtils {
039  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041    HBaseClassTestRule.forClass(TestBackupUtils.class);
042  private static final Logger LOG = LoggerFactory.getLogger(TestBackupUtils.class);
043
044  protected static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
045  protected static Configuration conf = TEST_UTIL.getConfiguration();
046
047  @Test
048  public void TestGetBulkOutputDir() {
049    // Create a user who is not the current user
050    String fooUserName = "foo1234";
051    String fooGroupName = "group1";
052    UserGroupInformation ugi =
053      UserGroupInformation.createUserForTesting(fooUserName, new String[] { fooGroupName });
054    // Get user's home directory
055    Path fooHomeDirectory = ugi.doAs(new PrivilegedAction<Path>() {
056      @Override
057      public Path run() {
058        try (FileSystem fs = FileSystem.get(conf)) {
059          return fs.getHomeDirectory();
060        } catch (IOException ioe) {
061          LOG.error("Failed to get foo's home directory", ioe);
062        }
063        return null;
064      }
065    });
066
067    Path bulkOutputDir = ugi.doAs(new PrivilegedAction<Path>() {
068      @Override
069      public Path run() {
070        try {
071          return BackupUtils.getBulkOutputDir("test", conf, false);
072        } catch (IOException ioe) {
073          LOG.error("Failed to get bulk output dir path", ioe);
074        }
075        return null;
076      }
077    });
078    // Make sure the directory is in foo1234's home directory
079    Assert.assertTrue(bulkOutputDir.toString().startsWith(fooHomeDirectory.toString()));
080  }
081}