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.snapshot;
019
020import static org.junit.Assert.assertFalse;
021import static org.junit.Assert.assertTrue;
022import static org.junit.Assert.fail;
023
024import java.io.IOException;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.fs.FileSystem;
027import org.apache.hadoop.fs.Path;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HBaseConfiguration;
030import org.apache.hadoop.hbase.HBaseTestingUtility;
031import org.apache.hadoop.hbase.HConstants;
032import org.apache.hadoop.hbase.testclassification.MediumTests;
033import org.apache.hadoop.hbase.testclassification.RegionServerTests;
034import org.apache.hadoop.hbase.util.EnvironmentEdgeManagerTestHelper;
035import org.junit.After;
036import org.junit.BeforeClass;
037import org.junit.ClassRule;
038import org.junit.Test;
039import org.junit.experimental.categories.Category;
040import org.slf4j.Logger;
041import org.slf4j.LoggerFactory;
042
043import org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos.SnapshotDescription;
044
045/**
046 * Test that the {@link SnapshotDescription} helper is helping correctly.
047 */
048@Category({RegionServerTests.class, MediumTests.class})
049public class TestSnapshotDescriptionUtils {
050
051  @ClassRule
052  public static final HBaseClassTestRule CLASS_RULE =
053      HBaseClassTestRule.forClass(TestSnapshotDescriptionUtils.class);
054
055  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
056  private static FileSystem fs;
057  private static Path root;
058
059  @BeforeClass
060  public static void setupFS() throws Exception {
061    fs = UTIL.getTestFileSystem();
062    root = new Path(UTIL.getDataTestDir(), "hbase");
063  }
064
065  @After
066  public void cleanupFS() throws Exception {
067    if (fs.exists(root)) {
068      if (!fs.delete(root, true)) {
069        throw new IOException("Failed to delete root test dir: " + root);
070      }
071      if (!fs.mkdirs(root)) {
072        throw new IOException("Failed to create root test dir: " + root);
073      }
074    }
075    EnvironmentEdgeManagerTestHelper.reset();
076  }
077
078  private static final Logger LOG = LoggerFactory.getLogger(TestSnapshotDescriptionUtils.class);
079
080  @Test
081  public void testValidateMissingTableName() throws IOException {
082    Configuration conf = new Configuration(false);
083    try {
084      SnapshotDescriptionUtils.validate(SnapshotDescription.newBuilder().setName("fail").build(),
085        conf);
086      fail("Snapshot was considered valid without a table name");
087    } catch (IllegalArgumentException e) {
088      LOG.debug("Correctly failed when snapshot doesn't have a tablename");
089    }
090  }
091
092  /**
093   * Test that we throw an exception if there is no working snapshot directory when we attempt to
094   * 'complete' the snapshot
095   * @throws Exception on failure
096   */
097  @Test
098  public void testCompleteSnapshotWithNoSnapshotDirectoryFailure() throws Exception {
099    Path snapshotDir = new Path(root, HConstants.SNAPSHOT_DIR_NAME);
100    Path tmpDir = new Path(snapshotDir, ".tmp");
101    Path workingDir = new Path(tmpDir, "not_a_snapshot");
102    assertFalse("Already have working snapshot dir: " + workingDir
103        + " but shouldn't. Test file leak?", fs.exists(workingDir));
104    SnapshotDescription snapshot = SnapshotDescription.newBuilder().setName("snapshot").build();
105    try {
106      SnapshotDescriptionUtils.completeSnapshot(snapshot, root, workingDir, fs);
107      fail("Shouldn't successfully complete move of a non-existent directory.");
108    } catch (IOException e) {
109      LOG.info("Correctly failed to move non-existant directory: " + e.getMessage());
110    }
111  }
112
113  @Test
114  public void testIsSubDirectoryWorks() {
115    Path rootDir = new Path("hdfs://root/.hbase-snapshot/");
116
117    assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf(rootDir, rootDir));
118    assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf(
119        new Path("hdfs://root/.hbase-snapshotdir"), rootDir));
120    assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf(
121        new Path("hdfs://root/.hbase-snapshot"), rootDir));
122    assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf(
123        new Path("hdfs://.hbase-snapshot"), rootDir));
124    assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf(
125        new Path("hdfs://.hbase-snapshot/.tmp"), rootDir));
126    assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf(new Path("hdfs://root"), rootDir));
127    assertTrue(SnapshotDescriptionUtils.isSubDirectoryOf(
128        new Path("hdfs://root/.hbase-snapshot/.tmp"), rootDir));
129    assertTrue(SnapshotDescriptionUtils.isSubDirectoryOf(
130        new Path("hdfs://root/.hbase-snapshot/.tmp/snapshot"), rootDir));
131
132    assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf(
133        new Path("s3://root/.hbase-snapshot/"), rootDir));
134    assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf(new Path("s3://root"), rootDir));
135    assertFalse(SnapshotDescriptionUtils.isSubDirectoryOf(
136        new Path("s3://root/.hbase-snapshot/.tmp/snapshot"), rootDir));
137  }
138
139  @Test
140  public void testIsWithinWorkingDir() throws IOException {
141    Configuration conf = new Configuration();
142    conf.set(HConstants.HBASE_DIR, "hdfs://localhost/root/");
143
144    assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
145        new Path("hdfs://localhost/root/"), conf));
146    assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
147        new Path("hdfs://localhost/root/.hbase-snapshotdir"), conf));
148    assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
149        new Path("hdfs://localhost/root/.hbase-snapshot"), conf));
150    assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
151        new Path("hdfs://localhost/.hbase-snapshot"), conf));
152    assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
153        new Path("hdfs://localhost/.hbase-snapshot/.tmp"), conf));
154    assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
155      new Path("hdfs://localhost/root"), conf));
156    assertTrue(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
157        new Path("hdfs://localhost/root/.hbase-snapshot/.tmp"), conf));
158    assertTrue(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
159        new Path("hdfs://localhost/root/.hbase-snapshot/.tmp/snapshot"), conf));
160
161    assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
162        new Path("s3://localhost/root/.hbase-snapshot/"), conf));
163    assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
164      new Path("s3://localhost/root"), conf));
165    assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
166        new Path("s3://localhost/root/.hbase-snapshot/.tmp/snapshot"), conf));
167
168    // for local mode
169    conf = HBaseConfiguration.create();
170    String hbsaeDir = conf.get(HConstants.HBASE_DIR);
171
172    assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
173      new Path("file:" + hbsaeDir + "/"), conf));
174    assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
175      new Path("file:" + hbsaeDir + "/.hbase-snapshotdir"), conf));
176    assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
177      new Path("file:" + hbsaeDir + "/.hbase-snapshot"), conf));
178    assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
179      new Path("file:/.hbase-snapshot"), conf));
180    assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
181      new Path("file:/.hbase-snapshot/.tmp"), conf));
182    assertFalse(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
183      new Path("file:" + hbsaeDir), conf));
184    assertTrue(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
185      new Path("file:" + hbsaeDir + "/.hbase-snapshot/.tmp"), conf));
186    assertTrue(SnapshotDescriptionUtils.isWithinDefaultWorkingDir(
187      new Path("file:" + hbsaeDir + "/.hbase-snapshot/.tmp/snapshot"), conf));
188  }
189}