HBASE-17614: Move Backup/Restore into separate module (Vladimir Rodionov)
[hbase.git] / hbase-backup / src / test / java / org / apache / hadoop / hbase / backup / TestBackupCommandLineTool.java
blob08002fbe0fd4da5a5098bf84369d70a1cf2f3e75
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase.backup;
20 import static org.junit.Assert.assertTrue;
22 import java.io.ByteArrayOutputStream;
23 import java.io.PrintStream;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.hbase.HBaseConfiguration;
27 import org.apache.hadoop.hbase.testclassification.SmallTests;
28 import org.apache.hadoop.util.ToolRunner;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.experimental.categories.Category;
33 @Category(SmallTests.class)
34 public class TestBackupCommandLineTool {
36 private final static String USAGE_DESCRIBE = "Usage: hbase backup describe <backup_id>";
37 private final static String USAGE_CREATE = "Usage: hbase backup create";
38 private final static String USAGE_HISTORY = "Usage: hbase backup history";
39 private final static String USAGE_BACKUP = "Usage: hbase backup";
40 private final static String USAGE_DELETE = "Usage: hbase backup delete";
41 private final static String USAGE_PROGRESS = "Usage: hbase backup progress";
42 private final static String USAGE_SET = "Usage: hbase backup set";
43 private final static String USAGE_RESTORE = "Usage: hbase restore";
45 Configuration conf;
47 @Before
48 public void setUpBefore() throws Exception {
49 conf = HBaseConfiguration.create();
50 conf.setBoolean(BackupRestoreConstants.BACKUP_ENABLE_KEY, true);
53 @Test
54 public void testBackupDriverDescribeHelp() throws Exception {
55 ByteArrayOutputStream baos = new ByteArrayOutputStream();
56 System.setOut(new PrintStream(baos));
57 String[] args = new String[] { "describe", "-help" };
58 ToolRunner.run(conf, new BackupDriver(), args);
60 String output = baos.toString();
61 System.out.println(baos.toString());
62 assertTrue(output.indexOf(USAGE_DESCRIBE) >= 0);
64 baos = new ByteArrayOutputStream();
65 System.setOut(new PrintStream(baos));
66 args = new String[] { "describe", "-h" };
67 ToolRunner.run(conf, new BackupDriver(), args);
69 output = baos.toString();
70 System.out.println(baos.toString());
71 assertTrue(output.indexOf(USAGE_DESCRIBE) >= 0);
73 baos = new ByteArrayOutputStream();
74 System.setOut(new PrintStream(baos));
75 args = new String[] { "describe" };
76 ToolRunner.run(conf, new BackupDriver(), args);
78 output = baos.toString();
79 System.out.println(baos.toString());
80 assertTrue(output.indexOf(USAGE_DESCRIBE) >= 0);
83 @Test
84 public void testBackupDriverCreateHelp() throws Exception {
85 ByteArrayOutputStream baos = new ByteArrayOutputStream();
86 System.setOut(new PrintStream(baos));
87 String[] args = new String[] { "create", "-help" };
88 ToolRunner.run(conf, new BackupDriver(), args);
90 String output = baos.toString();
91 System.out.println(baos.toString());
92 assertTrue(output.indexOf(USAGE_CREATE) >= 0);
93 assertTrue(output.indexOf(BackupRestoreConstants.OPTION_TABLE_LIST_DESC) > 0);
96 baos = new ByteArrayOutputStream();
97 System.setOut(new PrintStream(baos));
98 args = new String[] { "create", "-h" };
99 ToolRunner.run(conf, new BackupDriver(), args);
101 output = baos.toString();
102 System.out.println(baos.toString());
103 assertTrue(output.indexOf(USAGE_CREATE) >= 0);
104 assertTrue(output.indexOf(BackupRestoreConstants.OPTION_TABLE_LIST_DESC) > 0);
106 baos = new ByteArrayOutputStream();
107 System.setOut(new PrintStream(baos));
108 args = new String[] { "create" };
109 ToolRunner.run(conf, new BackupDriver(), args);
111 output = baos.toString();
112 System.out.println(baos.toString());
113 assertTrue(output.indexOf(USAGE_CREATE) >= 0);
114 assertTrue(output.indexOf(BackupRestoreConstants.OPTION_TABLE_LIST_DESC) > 0);
118 @Test
119 public void testBackupDriverHistoryHelp() throws Exception {
120 ByteArrayOutputStream baos = new ByteArrayOutputStream();
121 System.setOut(new PrintStream(baos));
122 String[] args = new String[] { "history", "-help" };
123 ToolRunner.run(conf, new BackupDriver(), args);
125 String output = baos.toString();
126 System.out.println(baos.toString());
127 assertTrue(output.indexOf(USAGE_HISTORY) >= 0);
129 baos = new ByteArrayOutputStream();
130 System.setOut(new PrintStream(baos));
131 args = new String[] { "history", "-h" };
132 ToolRunner.run(conf, new BackupDriver(), args);
134 output = baos.toString();
135 System.out.println(baos.toString());
136 assertTrue(output.indexOf(USAGE_HISTORY) >= 0);
140 @Test
141 public void testBackupDriverDeleteHelp() throws Exception {
142 ByteArrayOutputStream baos = new ByteArrayOutputStream();
143 System.setOut(new PrintStream(baos));
144 String[] args = new String[] { "delete", "-help" };
145 ToolRunner.run(conf, new BackupDriver(), args);
147 String output = baos.toString();
148 System.out.println(baos.toString());
149 assertTrue(output.indexOf(USAGE_DELETE) >= 0);
151 baos = new ByteArrayOutputStream();
152 System.setOut(new PrintStream(baos));
153 args = new String[] { "delete", "-h" };
154 ToolRunner.run(conf, new BackupDriver(), args);
156 output = baos.toString();
157 System.out.println(baos.toString());
158 assertTrue(output.indexOf(USAGE_DELETE) >= 0);
160 baos = new ByteArrayOutputStream();
161 System.setOut(new PrintStream(baos));
162 args = new String[] { "delete" };
163 ToolRunner.run(conf, new BackupDriver(), args);
165 output = baos.toString();
166 System.out.println(baos.toString());
167 assertTrue(output.indexOf(USAGE_DELETE) >= 0);
170 @Test
171 public void testBackupDriverProgressHelp() throws Exception {
172 ByteArrayOutputStream baos = new ByteArrayOutputStream();
173 System.setOut(new PrintStream(baos));
174 String[] args = new String[] { "progress", "-help" };
175 ToolRunner.run(conf, new BackupDriver(), args);
177 String output = baos.toString();
178 System.out.println(baos.toString());
179 assertTrue(output.indexOf(USAGE_PROGRESS) >= 0);
181 baos = new ByteArrayOutputStream();
182 System.setOut(new PrintStream(baos));
183 args = new String[] { "progress", "-h" };
184 ToolRunner.run(conf, new BackupDriver(), args);
186 output = baos.toString();
187 System.out.println(baos.toString());
188 assertTrue(output.indexOf(USAGE_PROGRESS) >= 0);
191 @Test
192 public void testBackupDriverSetHelp() throws Exception {
193 ByteArrayOutputStream baos = new ByteArrayOutputStream();
194 System.setOut(new PrintStream(baos));
195 String[] args = new String[] { "set", "-help" };
196 ToolRunner.run(conf, new BackupDriver(), args);
198 String output = baos.toString();
199 System.out.println(baos.toString());
200 assertTrue(output.indexOf(USAGE_SET) >= 0);
202 baos = new ByteArrayOutputStream();
203 System.setOut(new PrintStream(baos));
204 args = new String[] { "set", "-h" };
205 ToolRunner.run(conf, new BackupDriver(), args);
207 output = baos.toString();
208 System.out.println(baos.toString());
209 assertTrue(output.indexOf(USAGE_SET) >= 0);
211 baos = new ByteArrayOutputStream();
212 System.setOut(new PrintStream(baos));
213 args = new String[] { "set" };
214 ToolRunner.run(conf, new BackupDriver(), args);
216 output = baos.toString();
217 System.out.println(baos.toString());
218 assertTrue(output.indexOf(USAGE_SET) >= 0);
222 @Test
223 public void testBackupDriverHelp() throws Exception {
224 ByteArrayOutputStream baos = new ByteArrayOutputStream();
225 System.setOut(new PrintStream(baos));
226 String[] args = new String[] { "-help" };
227 ToolRunner.run(conf, new BackupDriver(), args);
229 String output = baos.toString();
230 System.out.println(baos.toString());
231 assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
232 baos = new ByteArrayOutputStream();
233 System.setOut(new PrintStream(baos));
234 args = new String[] { "-h" };
235 ToolRunner.run(conf, new BackupDriver(), args);
237 output = baos.toString();
238 System.out.println(baos.toString());
239 assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
243 @Test
244 public void testRestoreDriverHelp() throws Exception {
245 ByteArrayOutputStream baos = new ByteArrayOutputStream();
246 System.setOut(new PrintStream(baos));
247 String[] args = new String[] { "-help" };
248 ToolRunner.run(conf, new RestoreDriver(), args);
250 String output = baos.toString();
251 System.out.println(baos.toString());
252 assertTrue(output.indexOf(USAGE_RESTORE) >= 0);
253 assertTrue(output.indexOf(BackupRestoreConstants.OPTION_TABLE_LIST_DESC) > 0);
255 baos = new ByteArrayOutputStream();
256 System.setOut(new PrintStream(baos));
257 args = new String[] { "-h" };
258 ToolRunner.run(conf, new RestoreDriver(), args);
260 output = baos.toString();
261 System.out.println(baos.toString());
262 assertTrue(output.indexOf(USAGE_RESTORE) >= 0);
263 assertTrue(output.indexOf(BackupRestoreConstants.OPTION_TABLE_LIST_DESC) > 0);
267 @Test
268 public void testBackupDriverUnrecognizedCommand() throws Exception {
269 ByteArrayOutputStream baos = new ByteArrayOutputStream();
270 System.setOut(new PrintStream(baos));
271 String[] args = new String[] { "command" };
272 ToolRunner.run(conf, new BackupDriver(), args);
274 String output = baos.toString();
275 System.out.println(baos.toString());
276 assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
278 baos = new ByteArrayOutputStream();
279 System.setOut(new PrintStream(baos));
280 args = new String[] { "command" };
281 ToolRunner.run(conf, new BackupDriver(), args);
283 output = baos.toString();
284 System.out.println(baos.toString());
285 assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
288 @Test
289 public void testBackupDriverUnrecognizedOption() throws Exception {
290 ByteArrayOutputStream baos = new ByteArrayOutputStream();
291 System.setOut(new PrintStream(baos));
292 String[] args = new String[] { "create", "-xx" };
293 ToolRunner.run(conf, new BackupDriver(), args);
295 String output = baos.toString();
296 System.out.println(baos.toString());
297 assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
299 baos = new ByteArrayOutputStream();
300 System.setOut(new PrintStream(baos));
301 args = new String[] { "describe", "-xx" };
302 ToolRunner.run(conf, new BackupDriver(), args);
304 output = baos.toString();
305 System.out.println(baos.toString());
306 assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
308 baos = new ByteArrayOutputStream();
309 System.setOut(new PrintStream(baos));
310 args = new String[] { "history", "-xx" };
311 ToolRunner.run(conf, new BackupDriver(), args);
313 output = baos.toString();
314 System.out.println(baos.toString());
315 assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
317 baos = new ByteArrayOutputStream();
318 System.setOut(new PrintStream(baos));
319 args = new String[] { "delete", "-xx" };
320 ToolRunner.run(conf, new BackupDriver(), args);
322 output = baos.toString();
323 System.out.println(baos.toString());
324 assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
326 baos = new ByteArrayOutputStream();
327 System.setOut(new PrintStream(baos));
328 args = new String[] { "set", "-xx" };
329 ToolRunner.run(conf, new BackupDriver(), args);
331 output = baos.toString();
332 System.out.println(baos.toString());
333 assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
336 @Test
337 public void testRestoreDriverUnrecognizedOption() throws Exception {
338 ByteArrayOutputStream baos = new ByteArrayOutputStream();
339 System.setOut(new PrintStream(baos));
340 String[] args = new String[] { "-xx" };
341 ToolRunner.run(conf, new RestoreDriver(), args);
343 String output = baos.toString();
344 System.out.println(baos.toString());
345 assertTrue(output.indexOf(USAGE_RESTORE) >= 0);
349 @Test
350 public void testBackupDriverCreateWrongArgNumber() throws Exception {
351 ByteArrayOutputStream baos = new ByteArrayOutputStream();
352 System.setOut(new PrintStream(baos));
353 String[] args = new String[] { "create" };
354 ToolRunner.run(conf, new BackupDriver(), args);
356 String output = baos.toString();
357 System.out.println(baos.toString());
358 assertTrue(output.indexOf(USAGE_CREATE) >= 0);
360 baos = new ByteArrayOutputStream();
361 System.setOut(new PrintStream(baos));
362 args = new String[] { "create", "22" };
363 ToolRunner.run(conf, new BackupDriver(), args);
365 output = baos.toString();
366 System.out.println(baos.toString());
367 assertTrue(output.indexOf(USAGE_CREATE) >= 0);
369 baos = new ByteArrayOutputStream();
370 System.setOut(new PrintStream(baos));
371 args = new String[] { "create", "22", "22", "22", "22", "22" };
372 ToolRunner.run(conf, new BackupDriver(), args);
374 output = baos.toString();
375 System.out.println(baos.toString());
376 assertTrue(output.indexOf(USAGE_CREATE) >= 0);
379 @Test
380 public void testBackupDriverDeleteWrongArgNumber() throws Exception {
381 ByteArrayOutputStream baos = new ByteArrayOutputStream();
382 System.setOut(new PrintStream(baos));
383 String[] args = new String[] { "delete" };
384 ToolRunner.run(conf, new BackupDriver(), args);
386 String output = baos.toString();
387 System.out.println(baos.toString());
388 assertTrue(output.indexOf(USAGE_DELETE) >= 0);
392 @Test
393 public void testBackupDriverHistoryWrongArgs() throws Exception {
394 ByteArrayOutputStream baos = new ByteArrayOutputStream();
395 System.setOut(new PrintStream(baos));
396 String[] args = new String[] { "history", "-n", "xx" };
397 ToolRunner.run(conf, new BackupDriver(), args);
399 String output = baos.toString();
400 System.out.println(baos.toString());
401 assertTrue(output.indexOf(USAGE_HISTORY) >= 0);
405 @Test
406 public void testBackupDriverWrongBackupDestination() throws Exception {
407 ByteArrayOutputStream baos = new ByteArrayOutputStream();
408 System.setOut(new PrintStream(baos));
409 String[] args = new String[] { "create", "full", "clicks" };
410 ToolRunner.run(conf, new BackupDriver(), args);
412 String output = baos.toString();
413 System.out.println(baos.toString());
414 assertTrue(output.indexOf("ERROR: invalid backup destination") >= 0);
418 @Test
419 public void testBackupDriverBackupSetAndList() throws Exception {
420 ByteArrayOutputStream baos = new ByteArrayOutputStream();
421 System.setOut(new PrintStream(baos));
422 String[] args = new String[] { "create", "full", "file:/", "-t", "clicks", "-s", "s" };
423 ToolRunner.run(conf, new BackupDriver(), args);
425 String output = baos.toString();
426 System.out.println(baos.toString());
427 assertTrue(output.indexOf("ERROR: You can specify either backup set or list") >= 0);