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;
022
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.fs.FileSystem;
025import org.apache.hadoop.fs.Path;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseTestingUtil;
028import org.apache.hadoop.hbase.backup.util.BackupUtils;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.apache.hadoop.security.UserGroupInformation;
031import org.junit.Assert;
032import org.junit.ClassRule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037
038@Category(SmallTests.class)
039public class TestBackupUtils {
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042      HBaseClassTestRule.forClass(TestBackupUtils.class);
043  private static final Logger LOG = LoggerFactory.getLogger(TestBackupUtils.class);
044
045  protected static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
046  protected static Configuration conf = TEST_UTIL.getConfiguration();
047
048  @Test
049  public void TestGetBulkOutputDir() {
050    // Create a user who is not the current user
051    String fooUserName = "foo1234";
052    String fooGroupName = "group1";
053    UserGroupInformation
054        ugi = UserGroupInformation.createUserForTesting(fooUserName, new String[]{fooGroupName});
055    // Get user's home directory
056    Path fooHomeDirectory = ugi.doAs(new PrivilegedAction<Path>() {
057      @Override 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 public Path run() {
069        try {
070          return BackupUtils.getBulkOutputDir("test", conf, false);
071        } catch (IOException ioe) {
072          LOG.error("Failed to get bulk output dir path", ioe);
073        }
074        return null;
075      }
076    });
077    // Make sure the directory is in foo1234's home directory
078    Assert.assertTrue(bulkOutputDir.toString().startsWith(fooHomeDirectory.toString()));
079  }
080}