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