001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.backup;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.io.ByteArrayOutputStream;
024import java.io.PrintStream;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseConfiguration;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.apache.hadoop.util.ToolRunner;
030import org.junit.Before;
031import org.junit.ClassRule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034
035@Category(SmallTests.class)
036public class TestBackupCommandLineTool {
037
038  @ClassRule
039  public static final HBaseClassTestRule CLASS_RULE =
040    HBaseClassTestRule.forClass(TestBackupCommandLineTool.class);
041
042  private final static String USAGE_DESCRIBE = "Usage: hbase backup describe <backup_id>";
043  private final static String USAGE_CREATE = "Usage: hbase backup create";
044  private final static String USAGE_HISTORY = "Usage: hbase backup history";
045  private final static String USAGE_BACKUP = "Usage: hbase backup";
046  private final static String USAGE_DELETE = "Usage: hbase backup delete";
047  private final static String USAGE_PROGRESS = "Usage: hbase backup progress";
048  private final static String USAGE_SET = "Usage: hbase backup set";
049  private final static String USAGE_RESTORE = "Usage: hbase restore";
050
051  Configuration conf;
052
053  @Before
054  public void setUpBefore() throws Exception {
055    conf = HBaseConfiguration.create();
056    conf.setBoolean(BackupRestoreConstants.BACKUP_ENABLE_KEY, true);
057  }
058
059  @Test
060  public void testBackupDriverDescribeHelp() throws Exception {
061    ByteArrayOutputStream baos = new ByteArrayOutputStream();
062    System.setOut(new PrintStream(baos));
063    String[] args = new String[] { "describe", "-help" };
064    ToolRunner.run(conf, new BackupDriver(), args);
065
066    String output = baos.toString();
067    System.out.println(baos.toString());
068    assertTrue(output.indexOf(USAGE_DESCRIBE) >= 0);
069
070    baos = new ByteArrayOutputStream();
071    System.setOut(new PrintStream(baos));
072    args = new String[] { "describe", "-h" };
073    ToolRunner.run(conf, new BackupDriver(), args);
074
075    output = baos.toString();
076    System.out.println(baos.toString());
077    assertTrue(output.indexOf(USAGE_DESCRIBE) >= 0);
078
079    baos = new ByteArrayOutputStream();
080    System.setOut(new PrintStream(baos));
081    args = new String[] { "describe" };
082    ToolRunner.run(conf, new BackupDriver(), args);
083
084    output = baos.toString();
085    System.out.println(baos.toString());
086    assertTrue(output.indexOf(USAGE_DESCRIBE) >= 0);
087  }
088
089  @Test
090  public void testBackupDriverCreateTopLevelBackupDest() throws Exception {
091    String[] args = new String[] { "create", "full", "hdfs://localhost:1020", "-t", "t1" };
092    int result = ToolRunner.run(conf, new BackupDriver(), args);
093    // FAILED
094    assertEquals(1, result);
095  }
096
097  @Test
098  public void testBackupDriverCreateHelp() throws Exception {
099    ByteArrayOutputStream baos = new ByteArrayOutputStream();
100    System.setOut(new PrintStream(baos));
101    String[] args = new String[] { "create", "-help" };
102    ToolRunner.run(conf, new BackupDriver(), args);
103
104    String output = baos.toString();
105    System.out.println(baos.toString());
106    assertTrue(output.indexOf(USAGE_CREATE) >= 0);
107    assertTrue(output.indexOf(BackupRestoreConstants.OPTION_TABLE_LIST_DESC) > 0);
108
109    baos = new ByteArrayOutputStream();
110    System.setOut(new PrintStream(baos));
111    args = new String[] { "create", "-h" };
112    ToolRunner.run(conf, new BackupDriver(), args);
113
114    output = baos.toString();
115    System.out.println(baos.toString());
116    assertTrue(output.indexOf(USAGE_CREATE) >= 0);
117    assertTrue(output.indexOf(BackupRestoreConstants.OPTION_TABLE_LIST_DESC) > 0);
118
119    baos = new ByteArrayOutputStream();
120    System.setOut(new PrintStream(baos));
121    args = new String[] { "create" };
122    ToolRunner.run(conf, new BackupDriver(), args);
123
124    output = baos.toString();
125    System.out.println(baos.toString());
126    assertTrue(output.indexOf(USAGE_CREATE) >= 0);
127    assertTrue(output.indexOf(BackupRestoreConstants.OPTION_TABLE_LIST_DESC) > 0);
128
129  }
130
131  @Test
132  public void testBackupDriverHistoryHelp() throws Exception {
133    ByteArrayOutputStream baos = new ByteArrayOutputStream();
134    System.setOut(new PrintStream(baos));
135    String[] args = new String[] { "history", "-help" };
136    ToolRunner.run(conf, new BackupDriver(), args);
137
138    String output = baos.toString();
139    System.out.println(baos.toString());
140    assertTrue(output.indexOf(USAGE_HISTORY) >= 0);
141
142    baos = new ByteArrayOutputStream();
143    System.setOut(new PrintStream(baos));
144    args = new String[] { "history", "-h" };
145    ToolRunner.run(conf, new BackupDriver(), args);
146
147    output = baos.toString();
148    System.out.println(baos.toString());
149    assertTrue(output.indexOf(USAGE_HISTORY) >= 0);
150
151  }
152
153  @Test
154  public void testBackupDriverDeleteHelp() throws Exception {
155    ByteArrayOutputStream baos = new ByteArrayOutputStream();
156    System.setOut(new PrintStream(baos));
157    String[] args = new String[] { "delete", "-help" };
158    ToolRunner.run(conf, new BackupDriver(), args);
159
160    String output = baos.toString();
161    System.out.println(baos.toString());
162    assertTrue(output.indexOf(USAGE_DELETE) >= 0);
163
164    baos = new ByteArrayOutputStream();
165    System.setOut(new PrintStream(baos));
166    args = new String[] { "delete", "-h" };
167    ToolRunner.run(conf, new BackupDriver(), args);
168
169    output = baos.toString();
170    System.out.println(baos.toString());
171    assertTrue(output.indexOf(USAGE_DELETE) >= 0);
172
173    baos = new ByteArrayOutputStream();
174    System.setOut(new PrintStream(baos));
175    args = new String[] { "delete" };
176    ToolRunner.run(conf, new BackupDriver(), args);
177
178    output = baos.toString();
179    System.out.println(baos.toString());
180    assertTrue(output.indexOf(USAGE_DELETE) >= 0);
181  }
182
183  @Test
184  public void testBackupDriverProgressHelp() throws Exception {
185    ByteArrayOutputStream baos = new ByteArrayOutputStream();
186    System.setOut(new PrintStream(baos));
187    String[] args = new String[] { "progress", "-help" };
188    ToolRunner.run(conf, new BackupDriver(), args);
189
190    String output = baos.toString();
191    System.out.println(baos.toString());
192    assertTrue(output.indexOf(USAGE_PROGRESS) >= 0);
193
194    baos = new ByteArrayOutputStream();
195    System.setOut(new PrintStream(baos));
196    args = new String[] { "progress", "-h" };
197    ToolRunner.run(conf, new BackupDriver(), args);
198
199    output = baos.toString();
200    System.out.println(baos.toString());
201    assertTrue(output.indexOf(USAGE_PROGRESS) >= 0);
202  }
203
204  @Test
205  public void testBackupDriverSetHelp() throws Exception {
206    ByteArrayOutputStream baos = new ByteArrayOutputStream();
207    System.setOut(new PrintStream(baos));
208    String[] args = new String[] { "set", "-help" };
209    ToolRunner.run(conf, new BackupDriver(), args);
210
211    String output = baos.toString();
212    System.out.println(baos.toString());
213    assertTrue(output.indexOf(USAGE_SET) >= 0);
214
215    baos = new ByteArrayOutputStream();
216    System.setOut(new PrintStream(baos));
217    args = new String[] { "set", "-h" };
218    ToolRunner.run(conf, new BackupDriver(), args);
219
220    output = baos.toString();
221    System.out.println(baos.toString());
222    assertTrue(output.indexOf(USAGE_SET) >= 0);
223
224    baos = new ByteArrayOutputStream();
225    System.setOut(new PrintStream(baos));
226    args = new String[] { "set" };
227    ToolRunner.run(conf, new BackupDriver(), args);
228
229    output = baos.toString();
230    System.out.println(baos.toString());
231    assertTrue(output.indexOf(USAGE_SET) >= 0);
232
233  }
234
235  @Test
236  public void testBackupDriverHelp() throws Exception {
237    ByteArrayOutputStream baos = new ByteArrayOutputStream();
238    System.setOut(new PrintStream(baos));
239    String[] args = new String[] { "-help" };
240    ToolRunner.run(conf, new BackupDriver(), args);
241
242    String output = baos.toString();
243    System.out.println(baos.toString());
244    assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
245    baos = new ByteArrayOutputStream();
246    System.setOut(new PrintStream(baos));
247    args = new String[] { "-h" };
248    ToolRunner.run(conf, new BackupDriver(), args);
249
250    output = baos.toString();
251    System.out.println(baos.toString());
252    assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
253
254  }
255
256  @Test
257  public void testRestoreDriverHelp() throws Exception {
258    ByteArrayOutputStream baos = new ByteArrayOutputStream();
259    System.setOut(new PrintStream(baos));
260    String[] args = new String[] { "-help" };
261    ToolRunner.run(conf, new RestoreDriver(), args);
262
263    String output = baos.toString();
264    System.out.println(baos.toString());
265    assertTrue(output.indexOf(USAGE_RESTORE) >= 0);
266    assertTrue(output.indexOf(BackupRestoreConstants.OPTION_TABLE_LIST_DESC) > 0);
267
268    baos = new ByteArrayOutputStream();
269    System.setOut(new PrintStream(baos));
270    args = new String[] { "-h" };
271    ToolRunner.run(conf, new RestoreDriver(), args);
272
273    output = baos.toString();
274    System.out.println(baos.toString());
275    assertTrue(output.indexOf(USAGE_RESTORE) >= 0);
276    assertTrue(output.indexOf(BackupRestoreConstants.OPTION_TABLE_LIST_DESC) > 0);
277
278  }
279
280  @Test
281  public void testBackupDriverUnrecognizedCommand() throws Exception {
282    ByteArrayOutputStream baos = new ByteArrayOutputStream();
283    System.setOut(new PrintStream(baos));
284    String[] args = new String[] { "command" };
285    ToolRunner.run(conf, new BackupDriver(), args);
286
287    String output = baos.toString();
288    System.out.println(baos.toString());
289    assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
290
291    baos = new ByteArrayOutputStream();
292    System.setOut(new PrintStream(baos));
293    args = new String[] { "command" };
294    ToolRunner.run(conf, new BackupDriver(), args);
295
296    output = baos.toString();
297    System.out.println(baos.toString());
298    assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
299  }
300
301  @Test
302  public void testBackupDriverUnrecognizedOption() throws Exception {
303    ByteArrayOutputStream baos = new ByteArrayOutputStream();
304    System.setOut(new PrintStream(baos));
305    String[] args = new String[] { "create", "-xx" };
306    ToolRunner.run(conf, new BackupDriver(), args);
307
308    String output = baos.toString();
309    System.out.println(baos.toString());
310    assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
311
312    baos = new ByteArrayOutputStream();
313    System.setOut(new PrintStream(baos));
314    args = new String[] { "describe", "-xx" };
315    ToolRunner.run(conf, new BackupDriver(), args);
316
317    output = baos.toString();
318    System.out.println(baos.toString());
319    assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
320
321    baos = new ByteArrayOutputStream();
322    System.setOut(new PrintStream(baos));
323    args = new String[] { "history", "-xx" };
324    ToolRunner.run(conf, new BackupDriver(), args);
325
326    output = baos.toString();
327    System.out.println(baos.toString());
328    assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
329
330    baos = new ByteArrayOutputStream();
331    System.setOut(new PrintStream(baos));
332    args = new String[] { "delete", "-xx" };
333    ToolRunner.run(conf, new BackupDriver(), args);
334
335    output = baos.toString();
336    System.out.println(baos.toString());
337    assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
338
339    baos = new ByteArrayOutputStream();
340    System.setOut(new PrintStream(baos));
341    args = new String[] { "set", "-xx" };
342    ToolRunner.run(conf, new BackupDriver(), args);
343
344    output = baos.toString();
345    System.out.println(baos.toString());
346    assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
347  }
348
349  @Test
350  public void testRestoreDriverUnrecognizedOption() throws Exception {
351    ByteArrayOutputStream baos = new ByteArrayOutputStream();
352    System.setOut(new PrintStream(baos));
353    String[] args = new String[] { "-xx" };
354    ToolRunner.run(conf, new RestoreDriver(), args);
355
356    String output = baos.toString();
357    System.out.println(baos.toString());
358    assertTrue(output.indexOf(USAGE_RESTORE) >= 0);
359
360  }
361
362  @Test
363  public void testBackupDriverCreateWrongArgNumber() throws Exception {
364    ByteArrayOutputStream baos = new ByteArrayOutputStream();
365    System.setOut(new PrintStream(baos));
366    String[] args = new String[] { "create" };
367    ToolRunner.run(conf, new BackupDriver(), args);
368
369    String output = baos.toString();
370    System.out.println(baos.toString());
371    assertTrue(output.indexOf(USAGE_CREATE) >= 0);
372
373    baos = new ByteArrayOutputStream();
374    System.setOut(new PrintStream(baos));
375    args = new String[] { "create", "22" };
376    ToolRunner.run(conf, new BackupDriver(), args);
377
378    output = baos.toString();
379    System.out.println(baos.toString());
380    assertTrue(output.indexOf(USAGE_CREATE) >= 0);
381
382    baos = new ByteArrayOutputStream();
383    System.setOut(new PrintStream(baos));
384    args = new String[] { "create", "22", "22", "22", "22", "22" };
385    ToolRunner.run(conf, new BackupDriver(), args);
386
387    output = baos.toString();
388    System.out.println(baos.toString());
389    assertTrue(output.indexOf(USAGE_CREATE) >= 0);
390  }
391
392  @Test
393  public void testBackupDriverDeleteWrongArgNumber() throws Exception {
394    ByteArrayOutputStream baos = new ByteArrayOutputStream();
395    System.setOut(new PrintStream(baos));
396    String[] args = new String[] { "delete" };
397    ToolRunner.run(conf, new BackupDriver(), args);
398
399    String output = baos.toString();
400    System.out.println(baos.toString());
401    assertTrue(output.indexOf(USAGE_DELETE) >= 0);
402
403  }
404
405  @Test
406  public void testBackupDriverHistoryWrongArgs() throws Exception {
407    ByteArrayOutputStream baos = new ByteArrayOutputStream();
408    System.setOut(new PrintStream(baos));
409    String[] args = new String[] { "history", "-n", "xx" };
410    ToolRunner.run(conf, new BackupDriver(), args);
411
412    String output = baos.toString();
413    System.out.println(baos.toString());
414    assertTrue(output.indexOf(USAGE_HISTORY) >= 0);
415
416  }
417
418  @Test
419  public void testBackupDriverWrongBackupDestination() throws Exception {
420    ByteArrayOutputStream baos = new ByteArrayOutputStream();
421    System.setOut(new PrintStream(baos));
422    String[] args = new String[] { "create", "full", "clicks" };
423    ToolRunner.run(conf, new BackupDriver(), args);
424
425    String output = baos.toString();
426    System.out.println(baos.toString());
427    assertTrue(output.indexOf("ERROR: invalid backup destination") >= 0);
428
429  }
430
431  @Test
432  public void testBackupDriverBackupSetAndList() throws Exception {
433    ByteArrayOutputStream baos = new ByteArrayOutputStream();
434    System.setOut(new PrintStream(baos));
435    String[] args = new String[] { "create", "full", "file:/localhost", "-t", "clicks", "-s", "s" };
436    ToolRunner.run(conf, new BackupDriver(), args);
437
438    String output = baos.toString();
439    System.out.println(baos.toString());
440    assertTrue(output.indexOf("ERROR: You can specify either backup set or list") >= 0);
441
442  }
443
444}