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