View Javadoc

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   */
18  package org.apache.hadoop.hbase.snapshot;
19  
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.fail;
22  
23  import java.io.IOException;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.fs.FileSystem;
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.hbase.HBaseTestingUtility;
31  import org.apache.hadoop.hbase.HConstants;
32  import org.apache.hadoop.hbase.testclassification.MediumTests;
33  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
34  import org.apache.hadoop.hbase.util.EnvironmentEdgeManagerTestHelper;
35  import org.junit.After;
36  import org.junit.BeforeClass;
37  import org.junit.Test;
38  import org.junit.experimental.categories.Category;
39  
40  /**
41   * Test that the {@link SnapshotDescription} helper is helping correctly.
42   */
43  @Category(MediumTests.class)
44  public class TestSnapshotDescriptionUtils {
45    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
46    private static FileSystem fs;
47    private static Path root;
48  
49    @BeforeClass
50    public static void setupFS() throws Exception {
51      fs = UTIL.getTestFileSystem();
52      root = new Path(UTIL.getDataTestDir(), "hbase");
53    }
54  
55    @After
56    public void cleanupFS() throws Exception {
57      if (fs.exists(root)) {
58        if (!fs.delete(root, true)) {
59          throw new IOException("Failed to delete root test dir: " + root);
60        }
61        if (!fs.mkdirs(root)) {
62          throw new IOException("Failed to create root test dir: " + root);
63        }
64      }
65      EnvironmentEdgeManagerTestHelper.reset();
66    }
67  
68    private static final Log LOG = LogFactory.getLog(TestSnapshotDescriptionUtils.class);
69  
70    @Test
71    public void testValidateMissingTableName() {
72      Configuration conf = new Configuration(false);
73      try {
74        SnapshotDescriptionUtils.validate(SnapshotDescription.newBuilder().setName("fail").build(),
75          conf);
76        fail("Snapshot was considered valid without a table name");
77      } catch (IllegalArgumentException e) {
78        LOG.debug("Correctly failed when snapshot doesn't have a tablename");
79      } catch (IOException e) {
80        LOG.debug("Correctly failed when saving acl into snapshot");
81      }
82    }
83  
84    /**
85     * Test that we throw an exception if there is no working snapshot directory when we attempt to
86     * 'complete' the snapshot
87     * @throws Exception on failure
88     */
89    @Test
90    public void testCompleteSnapshotWithNoSnapshotDirectoryFailure() throws Exception {
91      Path snapshotDir = new Path(root, HConstants.SNAPSHOT_DIR_NAME);
92      Path tmpDir = new Path(snapshotDir, ".tmp");
93      Path workingDir = new Path(tmpDir, "not_a_snapshot");
94      assertFalse("Already have working snapshot dir: " + workingDir
95          + " but shouldn't. Test file leak?", fs.exists(workingDir));
96      SnapshotDescription snapshot = SnapshotDescription.newBuilder().setName("snapshot").build();
97      try {
98        SnapshotDescriptionUtils.completeSnapshot(snapshot, root, workingDir, fs);
99        fail("Shouldn't successfully complete move of a non-existent directory.");
100     } catch (IOException e) {
101       LOG.info("Correctly failed to move non-existant directory: " + e.getMessage());
102     }
103   }
104 }