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.fail;
022
023import java.io.IOException;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.fs.FileSystem;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtility;
029import org.apache.hadoop.hbase.HConstants;
030import org.apache.hadoop.hbase.testclassification.MediumTests;
031import org.apache.hadoop.hbase.testclassification.RegionServerTests;
032import org.apache.hadoop.hbase.util.EnvironmentEdgeManagerTestHelper;
033import org.junit.After;
034import org.junit.BeforeClass;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038import org.slf4j.Logger;
039import org.slf4j.LoggerFactory;
040
041import org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos.SnapshotDescription;
042
043/**
044 * Test that the {@link SnapshotDescription} helper is helping correctly.
045 */
046@Category({RegionServerTests.class, MediumTests.class})
047public class TestSnapshotDescriptionUtils {
048
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051      HBaseClassTestRule.forClass(TestSnapshotDescriptionUtils.class);
052
053  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
054  private static FileSystem fs;
055  private static Path root;
056
057  @BeforeClass
058  public static void setupFS() throws Exception {
059    fs = UTIL.getTestFileSystem();
060    root = new Path(UTIL.getDataTestDir(), "hbase");
061  }
062
063  @After
064  public void cleanupFS() throws Exception {
065    if (fs.exists(root)) {
066      if (!fs.delete(root, true)) {
067        throw new IOException("Failed to delete root test dir: " + root);
068      }
069      if (!fs.mkdirs(root)) {
070        throw new IOException("Failed to create root test dir: " + root);
071      }
072    }
073    EnvironmentEdgeManagerTestHelper.reset();
074  }
075
076  private static final Logger LOG = LoggerFactory.getLogger(TestSnapshotDescriptionUtils.class);
077
078  @Test
079  public void testValidateMissingTableName() throws IOException {
080    Configuration conf = new Configuration(false);
081    try {
082      SnapshotDescriptionUtils.validate(SnapshotDescription.newBuilder().setName("fail").build(),
083        conf);
084      fail("Snapshot was considered valid without a table name");
085    } catch (IllegalArgumentException e) {
086      LOG.debug("Correctly failed when snapshot doesn't have a tablename");
087    }
088  }
089
090  /**
091   * Test that we throw an exception if there is no working snapshot directory when we attempt to
092   * 'complete' the snapshot
093   * @throws Exception on failure
094   */
095  @Test
096  public void testCompleteSnapshotWithNoSnapshotDirectoryFailure() throws Exception {
097    Path snapshotDir = new Path(root, HConstants.SNAPSHOT_DIR_NAME);
098    Path tmpDir = new Path(snapshotDir, ".tmp");
099    Path workingDir = new Path(tmpDir, "not_a_snapshot");
100    assertFalse("Already have working snapshot dir: " + workingDir
101        + " but shouldn't. Test file leak?", fs.exists(workingDir));
102    SnapshotDescription snapshot = SnapshotDescription.newBuilder().setName("snapshot").build();
103    try {
104      SnapshotDescriptionUtils.completeSnapshot(snapshot, root, workingDir, fs);
105      fail("Shouldn't successfully complete move of a non-existent directory.");
106    } catch (IOException e) {
107      LOG.info("Correctly failed to move non-existant directory: " + e.getMessage());
108    }
109  }
110}