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