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