HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-backup / src / test / java / org / apache / hadoop / hbase / backup / TestBackupCommandLineTool.java
blobacde21e3eb5a27725637b16b56c39d85b2128784
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.assertEquals;
21 import static org.junit.Assert.assertTrue;
23 import java.io.ByteArrayOutputStream;
24 import java.io.PrintStream;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.hbase.HBaseClassTestRule;
27 import org.apache.hadoop.hbase.HBaseConfiguration;
28 import org.apache.hadoop.hbase.testclassification.SmallTests;
29 import org.apache.hadoop.util.ToolRunner;
30 import org.junit.Before;
31 import org.junit.ClassRule;
32 import org.junit.Test;
33 import org.junit.experimental.categories.Category;
35 @Category(SmallTests.class)
36 public class TestBackupCommandLineTool {
38 @ClassRule
39 public static final HBaseClassTestRule CLASS_RULE =
40 HBaseClassTestRule.forClass(TestBackupCommandLineTool.class);
42 private final static String USAGE_DESCRIBE = "Usage: hbase backup describe <backup_id>";
43 private final static String USAGE_CREATE = "Usage: hbase backup create";
44 private final static String USAGE_HISTORY = "Usage: hbase backup history";
45 private final static String USAGE_BACKUP = "Usage: hbase backup";
46 private final static String USAGE_DELETE = "Usage: hbase backup delete";
47 private final static String USAGE_PROGRESS = "Usage: hbase backup progress";
48 private final static String USAGE_SET = "Usage: hbase backup set";
49 private final static String USAGE_RESTORE = "Usage: hbase restore";
51 Configuration conf;
53 @Before
54 public void setUpBefore() throws Exception {
55 conf = HBaseConfiguration.create();
56 conf.setBoolean(BackupRestoreConstants.BACKUP_ENABLE_KEY, true);
59 @Test
60 public void testBackupDriverDescribeHelp() throws Exception {
61 ByteArrayOutputStream baos = new ByteArrayOutputStream();
62 System.setOut(new PrintStream(baos));
63 String[] args = new String[] { "describe", "-help" };
64 ToolRunner.run(conf, new BackupDriver(), args);
66 String output = baos.toString();
67 System.out.println(baos.toString());
68 assertTrue(output.indexOf(USAGE_DESCRIBE) >= 0);
70 baos = new ByteArrayOutputStream();
71 System.setOut(new PrintStream(baos));
72 args = new String[] { "describe", "-h" };
73 ToolRunner.run(conf, new BackupDriver(), args);
75 output = baos.toString();
76 System.out.println(baos.toString());
77 assertTrue(output.indexOf(USAGE_DESCRIBE) >= 0);
79 baos = new ByteArrayOutputStream();
80 System.setOut(new PrintStream(baos));
81 args = new String[] { "describe" };
82 ToolRunner.run(conf, new BackupDriver(), args);
84 output = baos.toString();
85 System.out.println(baos.toString());
86 assertTrue(output.indexOf(USAGE_DESCRIBE) >= 0);
90 @Test
91 public void testBackupDriverCreateTopLevelBackupDest() throws Exception {
92 String[] args = new String[] { "create", "full", "hdfs://localhost:1020", "-t", "t1" };
93 int result = ToolRunner.run(conf, new BackupDriver(), args);
94 // FAILED
95 assertEquals(1, result);
98 @Test
99 public void testBackupDriverCreateHelp() throws Exception {
100 ByteArrayOutputStream baos = new ByteArrayOutputStream();
101 System.setOut(new PrintStream(baos));
102 String[] args = new String[] { "create", "-help" };
103 ToolRunner.run(conf, new BackupDriver(), args);
105 String output = baos.toString();
106 System.out.println(baos.toString());
107 assertTrue(output.indexOf(USAGE_CREATE) >= 0);
108 assertTrue(output.indexOf(BackupRestoreConstants.OPTION_TABLE_LIST_DESC) > 0);
111 baos = new ByteArrayOutputStream();
112 System.setOut(new PrintStream(baos));
113 args = new String[] { "create", "-h" };
114 ToolRunner.run(conf, new BackupDriver(), args);
116 output = baos.toString();
117 System.out.println(baos.toString());
118 assertTrue(output.indexOf(USAGE_CREATE) >= 0);
119 assertTrue(output.indexOf(BackupRestoreConstants.OPTION_TABLE_LIST_DESC) > 0);
121 baos = new ByteArrayOutputStream();
122 System.setOut(new PrintStream(baos));
123 args = new String[] { "create" };
124 ToolRunner.run(conf, new BackupDriver(), args);
126 output = baos.toString();
127 System.out.println(baos.toString());
128 assertTrue(output.indexOf(USAGE_CREATE) >= 0);
129 assertTrue(output.indexOf(BackupRestoreConstants.OPTION_TABLE_LIST_DESC) > 0);
133 @Test
134 public void testBackupDriverHistoryHelp() throws Exception {
135 ByteArrayOutputStream baos = new ByteArrayOutputStream();
136 System.setOut(new PrintStream(baos));
137 String[] args = new String[] { "history", "-help" };
138 ToolRunner.run(conf, new BackupDriver(), args);
140 String output = baos.toString();
141 System.out.println(baos.toString());
142 assertTrue(output.indexOf(USAGE_HISTORY) >= 0);
144 baos = new ByteArrayOutputStream();
145 System.setOut(new PrintStream(baos));
146 args = new String[] { "history", "-h" };
147 ToolRunner.run(conf, new BackupDriver(), args);
149 output = baos.toString();
150 System.out.println(baos.toString());
151 assertTrue(output.indexOf(USAGE_HISTORY) >= 0);
155 @Test
156 public void testBackupDriverDeleteHelp() throws Exception {
157 ByteArrayOutputStream baos = new ByteArrayOutputStream();
158 System.setOut(new PrintStream(baos));
159 String[] args = new String[] { "delete", "-help" };
160 ToolRunner.run(conf, new BackupDriver(), args);
162 String output = baos.toString();
163 System.out.println(baos.toString());
164 assertTrue(output.indexOf(USAGE_DELETE) >= 0);
166 baos = new ByteArrayOutputStream();
167 System.setOut(new PrintStream(baos));
168 args = new String[] { "delete", "-h" };
169 ToolRunner.run(conf, new BackupDriver(), args);
171 output = baos.toString();
172 System.out.println(baos.toString());
173 assertTrue(output.indexOf(USAGE_DELETE) >= 0);
175 baos = new ByteArrayOutputStream();
176 System.setOut(new PrintStream(baos));
177 args = new String[] { "delete" };
178 ToolRunner.run(conf, new BackupDriver(), args);
180 output = baos.toString();
181 System.out.println(baos.toString());
182 assertTrue(output.indexOf(USAGE_DELETE) >= 0);
185 @Test
186 public void testBackupDriverProgressHelp() throws Exception {
187 ByteArrayOutputStream baos = new ByteArrayOutputStream();
188 System.setOut(new PrintStream(baos));
189 String[] args = new String[] { "progress", "-help" };
190 ToolRunner.run(conf, new BackupDriver(), args);
192 String output = baos.toString();
193 System.out.println(baos.toString());
194 assertTrue(output.indexOf(USAGE_PROGRESS) >= 0);
196 baos = new ByteArrayOutputStream();
197 System.setOut(new PrintStream(baos));
198 args = new String[] { "progress", "-h" };
199 ToolRunner.run(conf, new BackupDriver(), args);
201 output = baos.toString();
202 System.out.println(baos.toString());
203 assertTrue(output.indexOf(USAGE_PROGRESS) >= 0);
206 @Test
207 public void testBackupDriverSetHelp() throws Exception {
208 ByteArrayOutputStream baos = new ByteArrayOutputStream();
209 System.setOut(new PrintStream(baos));
210 String[] args = new String[] { "set", "-help" };
211 ToolRunner.run(conf, new BackupDriver(), args);
213 String output = baos.toString();
214 System.out.println(baos.toString());
215 assertTrue(output.indexOf(USAGE_SET) >= 0);
217 baos = new ByteArrayOutputStream();
218 System.setOut(new PrintStream(baos));
219 args = new String[] { "set", "-h" };
220 ToolRunner.run(conf, new BackupDriver(), args);
222 output = baos.toString();
223 System.out.println(baos.toString());
224 assertTrue(output.indexOf(USAGE_SET) >= 0);
226 baos = new ByteArrayOutputStream();
227 System.setOut(new PrintStream(baos));
228 args = new String[] { "set" };
229 ToolRunner.run(conf, new BackupDriver(), args);
231 output = baos.toString();
232 System.out.println(baos.toString());
233 assertTrue(output.indexOf(USAGE_SET) >= 0);
237 @Test
238 public void testBackupDriverHelp() throws Exception {
239 ByteArrayOutputStream baos = new ByteArrayOutputStream();
240 System.setOut(new PrintStream(baos));
241 String[] args = new String[] { "-help" };
242 ToolRunner.run(conf, new BackupDriver(), args);
244 String output = baos.toString();
245 System.out.println(baos.toString());
246 assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
247 baos = new ByteArrayOutputStream();
248 System.setOut(new PrintStream(baos));
249 args = new String[] { "-h" };
250 ToolRunner.run(conf, new BackupDriver(), args);
252 output = baos.toString();
253 System.out.println(baos.toString());
254 assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
258 @Test
259 public void testRestoreDriverHelp() throws Exception {
260 ByteArrayOutputStream baos = new ByteArrayOutputStream();
261 System.setOut(new PrintStream(baos));
262 String[] args = new String[] { "-help" };
263 ToolRunner.run(conf, new RestoreDriver(), args);
265 String output = baos.toString();
266 System.out.println(baos.toString());
267 assertTrue(output.indexOf(USAGE_RESTORE) >= 0);
268 assertTrue(output.indexOf(BackupRestoreConstants.OPTION_TABLE_LIST_DESC) > 0);
270 baos = new ByteArrayOutputStream();
271 System.setOut(new PrintStream(baos));
272 args = new String[] { "-h" };
273 ToolRunner.run(conf, new RestoreDriver(), args);
275 output = baos.toString();
276 System.out.println(baos.toString());
277 assertTrue(output.indexOf(USAGE_RESTORE) >= 0);
278 assertTrue(output.indexOf(BackupRestoreConstants.OPTION_TABLE_LIST_DESC) > 0);
282 @Test
283 public void testBackupDriverUnrecognizedCommand() throws Exception {
284 ByteArrayOutputStream baos = new ByteArrayOutputStream();
285 System.setOut(new PrintStream(baos));
286 String[] args = new String[] { "command" };
287 ToolRunner.run(conf, new BackupDriver(), args);
289 String output = baos.toString();
290 System.out.println(baos.toString());
291 assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
293 baos = new ByteArrayOutputStream();
294 System.setOut(new PrintStream(baos));
295 args = new String[] { "command" };
296 ToolRunner.run(conf, new BackupDriver(), args);
298 output = baos.toString();
299 System.out.println(baos.toString());
300 assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
303 @Test
304 public void testBackupDriverUnrecognizedOption() throws Exception {
305 ByteArrayOutputStream baos = new ByteArrayOutputStream();
306 System.setOut(new PrintStream(baos));
307 String[] args = new String[] { "create", "-xx" };
308 ToolRunner.run(conf, new BackupDriver(), args);
310 String output = baos.toString();
311 System.out.println(baos.toString());
312 assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
314 baos = new ByteArrayOutputStream();
315 System.setOut(new PrintStream(baos));
316 args = new String[] { "describe", "-xx" };
317 ToolRunner.run(conf, new BackupDriver(), args);
319 output = baos.toString();
320 System.out.println(baos.toString());
321 assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
323 baos = new ByteArrayOutputStream();
324 System.setOut(new PrintStream(baos));
325 args = new String[] { "history", "-xx" };
326 ToolRunner.run(conf, new BackupDriver(), args);
328 output = baos.toString();
329 System.out.println(baos.toString());
330 assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
332 baos = new ByteArrayOutputStream();
333 System.setOut(new PrintStream(baos));
334 args = new String[] { "delete", "-xx" };
335 ToolRunner.run(conf, new BackupDriver(), args);
337 output = baos.toString();
338 System.out.println(baos.toString());
339 assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
341 baos = new ByteArrayOutputStream();
342 System.setOut(new PrintStream(baos));
343 args = new String[] { "set", "-xx" };
344 ToolRunner.run(conf, new BackupDriver(), args);
346 output = baos.toString();
347 System.out.println(baos.toString());
348 assertTrue(output.indexOf(USAGE_BACKUP) >= 0);
351 @Test
352 public void testRestoreDriverUnrecognizedOption() throws Exception {
353 ByteArrayOutputStream baos = new ByteArrayOutputStream();
354 System.setOut(new PrintStream(baos));
355 String[] args = new String[] { "-xx" };
356 ToolRunner.run(conf, new RestoreDriver(), args);
358 String output = baos.toString();
359 System.out.println(baos.toString());
360 assertTrue(output.indexOf(USAGE_RESTORE) >= 0);
364 @Test
365 public void testBackupDriverCreateWrongArgNumber() throws Exception {
366 ByteArrayOutputStream baos = new ByteArrayOutputStream();
367 System.setOut(new PrintStream(baos));
368 String[] args = new String[] { "create" };
369 ToolRunner.run(conf, new BackupDriver(), args);
371 String output = baos.toString();
372 System.out.println(baos.toString());
373 assertTrue(output.indexOf(USAGE_CREATE) >= 0);
375 baos = new ByteArrayOutputStream();
376 System.setOut(new PrintStream(baos));
377 args = new String[] { "create", "22" };
378 ToolRunner.run(conf, new BackupDriver(), args);
380 output = baos.toString();
381 System.out.println(baos.toString());
382 assertTrue(output.indexOf(USAGE_CREATE) >= 0);
384 baos = new ByteArrayOutputStream();
385 System.setOut(new PrintStream(baos));
386 args = new String[] { "create", "22", "22", "22", "22", "22" };
387 ToolRunner.run(conf, new BackupDriver(), args);
389 output = baos.toString();
390 System.out.println(baos.toString());
391 assertTrue(output.indexOf(USAGE_CREATE) >= 0);
394 @Test
395 public void testBackupDriverDeleteWrongArgNumber() throws Exception {
396 ByteArrayOutputStream baos = new ByteArrayOutputStream();
397 System.setOut(new PrintStream(baos));
398 String[] args = new String[] { "delete" };
399 ToolRunner.run(conf, new BackupDriver(), args);
401 String output = baos.toString();
402 System.out.println(baos.toString());
403 assertTrue(output.indexOf(USAGE_DELETE) >= 0);
407 @Test
408 public void testBackupDriverHistoryWrongArgs() throws Exception {
409 ByteArrayOutputStream baos = new ByteArrayOutputStream();
410 System.setOut(new PrintStream(baos));
411 String[] args = new String[] { "history", "-n", "xx" };
412 ToolRunner.run(conf, new BackupDriver(), args);
414 String output = baos.toString();
415 System.out.println(baos.toString());
416 assertTrue(output.indexOf(USAGE_HISTORY) >= 0);
420 @Test
421 public void testBackupDriverWrongBackupDestination() throws Exception {
422 ByteArrayOutputStream baos = new ByteArrayOutputStream();
423 System.setOut(new PrintStream(baos));
424 String[] args = new String[] { "create", "full", "clicks" };
425 ToolRunner.run(conf, new BackupDriver(), args);
427 String output = baos.toString();
428 System.out.println(baos.toString());
429 assertTrue(output.indexOf("ERROR: invalid backup destination") >= 0);
433 @Test
434 public void testBackupDriverBackupSetAndList() throws Exception {
435 ByteArrayOutputStream baos = new ByteArrayOutputStream();
436 System.setOut(new PrintStream(baos));
437 String[] args = new String[] { "create", "full", "file:/localhost", "-t", "clicks", "-s", "s" };
438 ToolRunner.run(conf, new BackupDriver(), args);
440 String output = baos.toString();
441 System.out.println(baos.toString());
442 assertTrue(output.indexOf("ERROR: You can specify either backup set or list") >= 0);