-
Notifications
You must be signed in to change notification settings - Fork 8.7k
/
TestRouterAdminCLI.java
1871 lines (1630 loc) · 74.4 KB
/
TestRouterAdminCLI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.hdfs.server.federation.router;
import static org.apache.hadoop.hdfs.server.federation.FederationTestUtils.createNamenodeReport;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.StorageType;
import org.apache.hadoop.ha.HAServiceProtocol.HAServiceState;
import org.apache.hadoop.hdfs.protocol.HdfsConstants;
import org.apache.hadoop.hdfs.server.federation.MiniRouterDFSCluster.RouterContext;
import org.apache.hadoop.hdfs.server.federation.RouterConfigBuilder;
import org.apache.hadoop.hdfs.server.federation.StateStoreDFSCluster;
import org.apache.hadoop.hdfs.server.federation.metrics.RBFMetrics;
import org.apache.hadoop.hdfs.server.federation.resolver.ActiveNamenodeResolver;
import org.apache.hadoop.hdfs.server.federation.resolver.FederationNamenodeServiceState;
import org.apache.hadoop.hdfs.server.federation.resolver.MountTableManager;
import org.apache.hadoop.hdfs.server.federation.resolver.MountTableResolver;
import org.apache.hadoop.hdfs.server.federation.resolver.MultipleDestinationMountTableResolver;
import org.apache.hadoop.hdfs.server.federation.resolver.RemoteLocation;
import org.apache.hadoop.hdfs.server.federation.resolver.order.DestinationOrder;
import org.apache.hadoop.hdfs.server.federation.store.StateStoreService;
import org.apache.hadoop.hdfs.server.federation.store.driver.StateStoreDriver;
import org.apache.hadoop.hdfs.server.federation.store.impl.DisabledNameserviceStoreImpl;
import org.apache.hadoop.hdfs.server.federation.store.impl.MountTableStoreImpl;
import org.apache.hadoop.hdfs.server.federation.store.protocol.GetMountTableEntriesRequest;
import org.apache.hadoop.hdfs.server.federation.store.protocol.GetMountTableEntriesResponse;
import org.apache.hadoop.hdfs.server.federation.store.records.MembershipState;
import org.apache.hadoop.hdfs.server.federation.store.records.MockStateStoreDriver;
import org.apache.hadoop.hdfs.server.federation.store.records.MountTable;
import org.apache.hadoop.hdfs.tools.federation.RouterAdmin;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.test.GenericTestUtils;
import org.apache.hadoop.test.Whitebox;
import org.apache.hadoop.util.ToolRunner;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;
import java.util.function.Supplier;
/**
* Tests Router admin commands.
*/
public class TestRouterAdminCLI {
private static StateStoreDFSCluster cluster;
private static RouterContext routerContext;
private static StateStoreService stateStore;
private static RouterAdmin admin;
private static RouterClient client;
private static Router router;
private static final String TEST_USER = "test-user";
private final ByteArrayOutputStream out = new ByteArrayOutputStream();
private final ByteArrayOutputStream err = new ByteArrayOutputStream();
private static final PrintStream OLD_OUT = System.out;
private static final PrintStream OLD_ERR = System.err;
@BeforeClass
public static void globalSetUp() throws Exception {
cluster = new StateStoreDFSCluster(false, 1,
MultipleDestinationMountTableResolver.class);
// Build and start a router with State Store + admin + RPC
Configuration conf = new RouterConfigBuilder()
.stateStore()
.metrics()
.admin()
.rpc()
.quota()
.safemode()
.build();
cluster.addRouterOverrides(conf);
// Start routers
cluster.startRouters();
routerContext = cluster.getRandomRouter();
router = routerContext.getRouter();
stateStore = router.getStateStore();
Configuration routerConf = new Configuration();
InetSocketAddress routerSocket = router.getAdminServerAddress();
routerConf.setSocketAddr(RBFConfigKeys.DFS_ROUTER_ADMIN_ADDRESS_KEY,
routerSocket);
admin = new RouterAdmin(routerConf);
client = routerContext.getAdminClient();
// Add two fake name services to testing disabling them
ActiveNamenodeResolver membership = router.getNamenodeResolver();
membership.registerNamenode(
createNamenodeReport("ns0", "nn1", HAServiceState.ACTIVE));
membership.registerNamenode(
createNamenodeReport("ns1", "nn1", HAServiceState.ACTIVE));
stateStore.refreshCaches(true);
// Mock the quota module since no real namenode is started up.
Quota quota = Mockito
.spy(routerContext.getRouter().createRpcServer().getQuotaModule());
Mockito.doNothing().when(quota)
.setQuota(Mockito.anyString(), Mockito.anyLong(), Mockito.anyLong(),
Mockito.any(), Mockito.anyBoolean());
Whitebox.setInternalState(
routerContext.getRouter().getRpcServer(), "quotaCall", quota);
RouterRpcServer spyRpcServer =
Mockito.spy(routerContext.getRouter().createRpcServer());
Whitebox
.setInternalState(routerContext.getRouter(), "rpcServer", spyRpcServer);
Mockito.doReturn(null).when(spyRpcServer).getFileInfo(Mockito.anyString());
}
@AfterClass
public static void tearDownCluster() {
cluster.stopRouter(routerContext);
cluster.shutdown();
cluster = null;
}
@After
public void tearDown() {
// set back system out/err
System.setOut(OLD_OUT);
System.setErr(OLD_ERR);
}
@Test
public void testAddMountTable() throws Exception {
String nsId = "ns0,ns1";
String src = "/test-addmounttable";
String dest = "/addmounttable";
String[] argv = new String[] {"-add", src, nsId, dest};
assertEquals(0, ToolRunner.run(admin, argv));
assertEquals(-1, ToolRunner.run(admin, argv));
stateStore.loadCache(MountTableStoreImpl.class, true);
verifyMountTableContents(src, dest);
GetMountTableEntriesRequest getRequest = GetMountTableEntriesRequest
.newInstance(src);
GetMountTableEntriesResponse getResponse = client.getMountTableManager()
.getMountTableEntries(getRequest);
MountTable mountTable = getResponse.getEntries().get(0);
List<RemoteLocation> destinations = mountTable.getDestinations();
assertEquals(2, destinations.size());
assertEquals(src, mountTable.getSourcePath());
assertEquals("ns0", destinations.get(0).getNameserviceId());
assertEquals(dest, destinations.get(0).getDest());
assertEquals("ns1", destinations.get(1).getNameserviceId());
assertEquals(dest, destinations.get(1).getDest());
assertFalse(mountTable.isReadOnly());
assertFalse(mountTable.isFaultTolerant());
// test mount table update behavior
dest = dest + "-new";
argv = new String[] {"-add", src, nsId, dest, "-readonly",
"-faulttolerant", "-order", "HASH_ALL"};
assertEquals(0, ToolRunner.run(admin, argv));
stateStore.loadCache(MountTableStoreImpl.class, true);
getResponse = client.getMountTableManager()
.getMountTableEntries(getRequest);
mountTable = getResponse.getEntries().get(0);
assertEquals(4, mountTable.getDestinations().size());
RemoteLocation loc2 = mountTable.getDestinations().get(2);
assertEquals("ns0", loc2.getNameserviceId());
assertEquals(dest, loc2.getDest());
RemoteLocation loc3 = mountTable.getDestinations().get(3);
assertEquals("ns1", loc3.getNameserviceId());
assertEquals(dest, loc3.getDest());
assertTrue(mountTable.isReadOnly());
assertTrue(mountTable.isFaultTolerant());
}
private void verifyMountTableContents(String src, String dest) throws Exception {
String[] argv = new String[] {"-ls", "/"};
System.setOut(new PrintStream(out));
assertEquals(0, ToolRunner.run(admin, argv));
String response = out.toString();
assertTrue("The response should have " + src + ": " + response, response.contains(src));
assertTrue("The response should have " + dest + ": " + response, response.contains(dest));
}
@Test
public void testAddMountTableNotNormalized() throws Exception {
String nsId = "ns0";
String src = "/test-addmounttable-notnormalized";
String srcWithSlash = src + "/";
String dest = "/addmounttable-notnormalized";
String[] argv = new String[] {"-add", srcWithSlash, nsId, dest};
assertEquals(0, ToolRunner.run(admin, argv));
stateStore.loadCache(MountTableStoreImpl.class, true);
GetMountTableEntriesRequest getRequest = GetMountTableEntriesRequest
.newInstance(src);
GetMountTableEntriesResponse getResponse = client.getMountTableManager()
.getMountTableEntries(getRequest);
MountTable mountTable = getResponse.getEntries().get(0);
List<RemoteLocation> destinations = mountTable.getDestinations();
assertEquals(1, destinations.size());
assertEquals(src, mountTable.getSourcePath());
assertEquals(nsId, destinations.get(0).getNameserviceId());
assertEquals(dest, destinations.get(0).getDest());
assertFalse(mountTable.isReadOnly());
assertFalse(mountTable.isFaultTolerant());
// test mount table update behavior
dest = dest + "-new";
argv = new String[] {"-add", srcWithSlash, nsId, dest, "-readonly"};
assertEquals(0, ToolRunner.run(admin, argv));
stateStore.loadCache(MountTableStoreImpl.class, true);
getResponse = client.getMountTableManager()
.getMountTableEntries(getRequest);
mountTable = getResponse.getEntries().get(0);
assertEquals(2, mountTable.getDestinations().size());
assertEquals(nsId, mountTable.getDestinations().get(1).getNameserviceId());
assertEquals(dest, mountTable.getDestinations().get(1).getDest());
assertTrue(mountTable.isReadOnly());
}
@Test
public void testAddOrderMountTable() throws Exception {
testAddOrderMountTable(DestinationOrder.HASH);
testAddOrderMountTable(DestinationOrder.LOCAL);
testAddOrderMountTable(DestinationOrder.RANDOM);
testAddOrderMountTable(DestinationOrder.HASH_ALL);
testAddOrderMountTable(DestinationOrder.SPACE);
}
@Test
public void testAddOrderErrorMsg() throws Exception {
DestinationOrder order = DestinationOrder.HASH;
final String mnt = "/newAdd1" + order;
final String nsId = "ns0,ns1";
final String dest = "/changAdd";
String[] argv1 = new String[] {"-add", mnt, nsId, dest, "-order",
order.toString()};
assertEquals(0, ToolRunner.run(admin, argv1));
// Add the order with wrong command
String[] argv = new String[] {"-add", mnt, nsId, dest, "-orde",
order.toString()};
assertEquals(-1, ToolRunner.run(admin, argv));
}
private void testAddOrderMountTable(DestinationOrder order)
throws Exception {
final String mnt = "/" + order;
final String nsId = "ns0,ns1";
final String dest = "/";
String[] argv = new String[] {
"-add", mnt, nsId, dest, "-order", order.toString()};
assertEquals(0, ToolRunner.run(admin, argv));
// Check the state in the State Store
stateStore.loadCache(MountTableStoreImpl.class, true);
MountTableManager mountTable = client.getMountTableManager();
GetMountTableEntriesRequest request =
GetMountTableEntriesRequest.newInstance(mnt);
GetMountTableEntriesResponse response =
mountTable.getMountTableEntries(request);
List<MountTable> entries = response.getEntries();
assertEquals(1, entries.size());
assertEquals(2, entries.get(0).getDestinations().size());
assertEquals(order, response.getEntries().get(0).getDestOrder());
}
@Test
public void testListMountTable() throws Exception {
String nsId = "ns0";
String src = "/test-lsmounttable";
String srcWithSlash = src + "/";
String dest = "/lsmounttable";
String[] argv = new String[] {"-add", src, nsId, dest};
assertEquals(0, ToolRunner.run(admin, argv));
// re-set system out for testing
System.setOut(new PrintStream(out));
stateStore.loadCache(MountTableStoreImpl.class, true);
argv = new String[] {"-ls", src};
assertEquals(0, ToolRunner.run(admin, argv));
String response = out.toString();
assertTrue("Wrong response: " + response, response.contains(src));
// Test with not-normalized src input
argv = new String[] {"-ls", srcWithSlash};
assertEquals(0, ToolRunner.run(admin, argv));
response = out.toString();
assertTrue("Wrong response: " + response, response.contains(src));
// Test with wrong number of arguments
argv = new String[] {"-ls", srcWithSlash, "check", "check2"};
System.setErr(new PrintStream(err));
ToolRunner.run(admin, argv);
response = err.toString();
assertTrue("Wrong response: " + response,
response.contains("Too many arguments, Max=2 argument allowed"));
out.reset();
GetMountTableEntriesRequest getRequest = GetMountTableEntriesRequest
.newInstance("/");
GetMountTableEntriesResponse getResponse = client.getMountTableManager()
.getMountTableEntries(getRequest);
// Test ls command without input path, it will list
// mount table under root path.
argv = new String[] {"-ls"};
assertEquals(0, ToolRunner.run(admin, argv));
response = out.toString();
assertTrue("Wrong response: " + response, response.contains(src));
// verify if all the mount table are listed
for (MountTable entry : getResponse.getEntries()) {
assertTrue("Wrong response: " + response,
response.contains(entry.getSourcePath()));
}
}
@Test
public void testListWithDetails() throws Exception {
// Create mount entry.
String[] argv = new String[] {"-add", "/testLsWithDetails", "ns0,ns1",
"/dest", "-order", "HASH_ALL", "-readonly", "-faulttolerant"};
assertEquals(0, ToolRunner.run(admin, argv));
System.setOut(new PrintStream(out));
stateStore.loadCache(MountTableStoreImpl.class, true);
// Test list with detail for a mount entry.
argv = new String[] {"-ls", "-d", "/testLsWithDetails"};
assertEquals(0, ToolRunner.run(admin, argv));
String response = out.toString();
assertTrue(response.contains("Read-Only"));
assertTrue(response.contains("Fault-Tolerant"));
out.reset();
// Test list with detail without path.
argv = new String[] {"-ls", "-d"};
assertEquals(0, ToolRunner.run(admin, argv));
response = out.toString();
assertTrue("Wrong response: " + response, response.contains("Read-Only"));
assertTrue("Wrong response: " + response,
response.contains("Fault-Tolerant"));
}
@Test
public void testListNestedMountTable() throws Exception {
String dir1 = "/test-ls";
String dir2 = "/test-ls-longger";
String[] nsIdList = {"ns0", "ns1", "ns2", "ns3", "ns3"};
String[] sourceList =
{dir1, dir1 + "/subdir1", dir2, dir2 + "/subdir1", dir2 + "/subdir2"};
String[] destList =
{"/test-ls", "/test-ls/subdir1", "/ls", "/ls/subdir1", "/ls/subdir2"};
for (int i = 0; i < nsIdList.length; i++) {
String[] argv =
new String[] {"-add", sourceList[i], nsIdList[i], destList[i]};
assertEquals(0, ToolRunner.run(admin, argv));
}
// prepare for test
System.setOut(new PrintStream(out));
stateStore.loadCache(MountTableStoreImpl.class, true);
// Test ls dir1
String[] argv = new String[] {"-ls", dir1};
assertEquals(0, ToolRunner.run(admin, argv));
String outStr = out.toString();
assertTrue(out.toString().contains(dir1 + "/subdir1"));
assertFalse(out.toString().contains(dir2));
// Test ls dir2
argv = new String[] {"-ls", dir2};
assertEquals(0, ToolRunner.run(admin, argv));
assertTrue(out.toString().contains(dir2 + "/subdir1"));
assertTrue(out.toString().contains(dir2 + "/subdir2"));
}
@Test
public void testRemoveMountTable() throws Exception {
String nsId = "ns0";
String src = "/test-rmmounttable";
String dest = "/rmmounttable";
String[] argv = new String[] {"-add", src, nsId, dest};
assertEquals(0, ToolRunner.run(admin, argv));
stateStore.loadCache(MountTableStoreImpl.class, true);
GetMountTableEntriesRequest getRequest = GetMountTableEntriesRequest
.newInstance(src);
GetMountTableEntriesResponse getResponse = client.getMountTableManager()
.getMountTableEntries(getRequest);
// ensure mount table added successfully
MountTable mountTable = getResponse.getEntries().get(0);
assertEquals(src, mountTable.getSourcePath());
argv = new String[] {"-rm", src};
assertEquals(0, ToolRunner.run(admin, argv));
stateStore.loadCache(MountTableStoreImpl.class, true);
getResponse = client.getMountTableManager()
.getMountTableEntries(getRequest);
assertEquals(0, getResponse.getEntries().size());
// remove an invalid mount table
String invalidPath = "/invalid";
System.setOut(new PrintStream(out));
argv = new String[] {"-rm", invalidPath};
assertEquals(0, ToolRunner.run(admin, argv));
assertTrue(out.toString().contains(
"Cannot remove mount point " + invalidPath));
}
@Test
public void testMultiArgsRemoveMountTable() throws Exception {
String nsId = "ns0";
String src1 = "/test-rmmounttable1";
String src2 = "/test-rmmounttable2";
String dest1 = "/rmmounttable1";
String dest2 = "/rmmounttable2";
// Adding mount table entries
String[] argv = new String[] {"-add", src1, nsId, dest1};
assertEquals(0, ToolRunner.run(admin, argv));
argv = new String[] {"-add", src2, nsId, dest2};
assertEquals(0, ToolRunner.run(admin, argv));
stateStore.loadCache(MountTableStoreImpl.class, true);
// Ensure mount table entries added successfully
GetMountTableEntriesRequest getRequest =
GetMountTableEntriesRequest.newInstance(src1);
GetMountTableEntriesResponse getResponse =
client.getMountTableManager().getMountTableEntries(getRequest);
MountTable mountTable = getResponse.getEntries().get(0);
getRequest = GetMountTableEntriesRequest.newInstance(src2);
getResponse =
client.getMountTableManager().getMountTableEntries(getRequest);
assertEquals(src1, mountTable.getSourcePath());
mountTable = getResponse.getEntries().get(0);
assertEquals(src2, mountTable.getSourcePath());
// Remove multiple mount table entries
argv = new String[] {"-rm", src1, src2};
assertEquals(0, ToolRunner.run(admin, argv));
stateStore.loadCache(MountTableStoreImpl.class, true);
// Verify successful deletion of mount table entries
getResponse =
client.getMountTableManager().getMountTableEntries(getRequest);
assertEquals(0, getResponse.getEntries().size());
}
@Test
public void testRemoveMountTableNotNormalized() throws Exception {
String nsId = "ns0";
String src = "/test-rmmounttable-notnormalized";
String srcWithSlash = src + "/";
String dest = "/rmmounttable-notnormalized";
String[] argv = new String[] {"-add", src, nsId, dest};
assertEquals(0, ToolRunner.run(admin, argv));
stateStore.loadCache(MountTableStoreImpl.class, true);
GetMountTableEntriesRequest getRequest = GetMountTableEntriesRequest
.newInstance(src);
GetMountTableEntriesResponse getResponse = client.getMountTableManager()
.getMountTableEntries(getRequest);
// ensure mount table added successfully
MountTable mountTable = getResponse.getEntries().get(0);
assertEquals(src, mountTable.getSourcePath());
argv = new String[] {"-rm", srcWithSlash};
assertEquals(0, ToolRunner.run(admin, argv));
stateStore.loadCache(MountTableStoreImpl.class, true);
getResponse = client.getMountTableManager()
.getMountTableEntries(getRequest);
assertEquals(0, getResponse.getEntries().size());
}
@Test
public void testMountTableDefaultACL() throws Exception {
String[] argv = new String[] {"-add", "/testpath0", "ns0", "/testdir0"};
assertEquals(0, ToolRunner.run(admin, argv));
stateStore.loadCache(MountTableStoreImpl.class, true);
GetMountTableEntriesRequest getRequest = GetMountTableEntriesRequest
.newInstance("/testpath0");
GetMountTableEntriesResponse getResponse = client.getMountTableManager()
.getMountTableEntries(getRequest);
MountTable mountTable = getResponse.getEntries().get(0);
UserGroupInformation ugi = UserGroupInformation.getCurrentUser();
String group = ugi.getGroups().isEmpty() ? ugi.getShortUserName()
: ugi.getPrimaryGroupName();
assertEquals(ugi.getShortUserName(), mountTable.getOwnerName());
assertEquals(group, mountTable.getGroupName());
assertEquals((short) 0755, mountTable.getMode().toShort());
}
@Test
public void testUpdateMountTableWithoutPermission() throws Exception {
UserGroupInformation superUser = UserGroupInformation.getCurrentUser();
String superUserName = superUser.getShortUserName();
// re-set system out for testing
System.setOut(new PrintStream(out));
try {
stateStore.loadCache(MountTableStoreImpl.class, true);
// add mount table using super user.
String[] argv = new String[]{"-add", "/testpath3-1", "ns0", "/testdir3-1",
"-owner", superUserName, "-group", superUserName, "-mode", "755"};
assertEquals(0, ToolRunner.run(admin, argv));
UserGroupInformation remoteUser = UserGroupInformation
.createRemoteUser(TEST_USER);
UserGroupInformation.setLoginUser(remoteUser);
stateStore.loadCache(MountTableStoreImpl.class, true);
// update mount table using normal user
argv = new String[]{"-update", "/testpath3-1", "ns0", "/testdir3-2",
"-owner", TEST_USER, "-group", TEST_USER, "-mode", "777"};
assertEquals("Normal user update mount table which created by " +
"superuser unexpected.", -1, ToolRunner.run(admin, argv));
} finally {
// set back login user
UserGroupInformation.setLoginUser(superUser);
}
}
@Test
public void testOperateMountTableWithGroupPermission() throws Exception {
UserGroupInformation superUser = UserGroupInformation.getCurrentUser();
// re-set system out for testing
System.setOut(new PrintStream(out));
try {
String testUserA = "test-user-a";
String testUserB = "test-user-b";
String testUserC = "test-user-c";
String testUserD = "test-user-d";
String testGroup = "test-group";
stateStore.loadCache(MountTableStoreImpl.class, true);
// add mount point with usera.
UserGroupInformation userA = UserGroupInformation.createUserForTesting(
testUserA, new String[] {testGroup});
UserGroupInformation.setLoginUser(userA);
String[] argv = new String[]{"-add", "/testpath4-1", "ns0", "/testdir4-1",
"-owner", testUserA, "-group", testGroup, "-mode", "775"};
assertEquals("Normal user can't add mount table unexpected.", 0,
ToolRunner.run(admin, argv));
stateStore.loadCache(MountTableStoreImpl.class, true);
// update mount point with userb which is same group with owner.
UserGroupInformation userB = UserGroupInformation.createUserForTesting(
testUserB, new String[] {testGroup});
UserGroupInformation.setLoginUser(userB);
argv = new String[]{"-update", "/testpath4-1", "ns0", "/testdir4-2",
"-owner", testUserA, "-group", testGroup, "-mode", "775"};
assertEquals("Another user in same group can't update mount table " +
"unexpected.", 0, ToolRunner.run(admin, argv));
stateStore.loadCache(MountTableStoreImpl.class, true);
// update mount point with userc which is not same group with owner.
UserGroupInformation userC = UserGroupInformation.createUserForTesting(
testUserC, new String[] {});
UserGroupInformation.setLoginUser(userC);
argv = new String[]{"-update", "/testpath4-1", "ns0", "/testdir4-3",
"-owner", testUserA, "-group", testGroup, "-mode", "775"};
assertEquals("Another user not in same group have no permission but " +
"update mount table successful unexpected.", -1,
ToolRunner.run(admin, argv));
stateStore.loadCache(MountTableStoreImpl.class, true);
// add mount point with userd but immediate parent of mount point
// does not exist.
UserGroupInformation userD = UserGroupInformation.createUserForTesting(
testUserD, new String[] {testGroup});
UserGroupInformation.setLoginUser(userD);
argv = new String[]{"-add", "/testpath4-1/foo/bar", "ns0",
"/testdir4-1/foo/bar", "-owner", testUserD, "-group", testGroup,
"-mode", "775"};
assertEquals("Normal user can't add mount table unexpected.", 0,
ToolRunner.run(admin, argv));
// test remove mount point with userc.
UserGroupInformation.setLoginUser(userC);
argv = new String[]{"-rm", "/testpath4-1"};
assertEquals(-1, ToolRunner.run(admin, argv));
// test remove mount point with userb.
UserGroupInformation.setLoginUser(userB);
assertEquals("Another user in same group can't remove mount table " +
"unexpected.", 0, ToolRunner.run(admin, argv));
} finally {
// set back login user
UserGroupInformation.setLoginUser(superUser);
}
}
@Test
public void testOperateMountTableWithSuperUserPermission() throws Exception {
UserGroupInformation superUser = UserGroupInformation.getCurrentUser();
// re-set system out for testing
System.setOut(new PrintStream(out));
try {
String testUserA = "test-user-a";
String testGroup = "test-group";
stateStore.loadCache(MountTableStoreImpl.class, true);
// add mount point with usera.
UserGroupInformation userA = UserGroupInformation.createUserForTesting(
testUserA, new String[] {testGroup});
UserGroupInformation.setLoginUser(userA);
String[] argv = new String[]{"-add", "/testpath5-1", "ns0", "/testdir5-1",
"-owner", testUserA, "-group", testGroup, "-mode", "755"};
assertEquals(0, ToolRunner.run(admin, argv));
// test update mount point with super user.
stateStore.loadCache(MountTableStoreImpl.class, true);
UserGroupInformation.setLoginUser(superUser);
argv = new String[]{"-update", "/testpath5-1", "ns0", "/testdir5-2",
"-owner", testUserA, "-group", testGroup, "-mode", "755"};
assertEquals("Super user can't update mount table unexpected.", 0,
ToolRunner.run(admin, argv));
// test remove mount point with super user.
argv = new String[]{"-rm", "/testpath5-1"};
assertEquals("Super user can't remove mount table unexpected.", 0,
ToolRunner.run(admin, argv));
} finally {
// set back login user
UserGroupInformation.setLoginUser(superUser);
}
}
@Test
public void testAddMountTableIfParentExist() throws Exception {
UserGroupInformation superUser = UserGroupInformation.getCurrentUser();
// re-set system out for testing
System.setOut(new PrintStream(out));
try {
String testUserA = "test-user-a";
String testUserB = "test-user-b";
String testGroup = "test-group";
stateStore.loadCache(MountTableStoreImpl.class, true);
// add mount point with usera.
UserGroupInformation userA = UserGroupInformation.createUserForTesting(
testUserA, new String[] {testGroup});
UserGroupInformation.setLoginUser(userA);
String[] argv = new String[]{"-add", "/testpath6-1", "ns0", "/testdir6-1",
"-owner", testUserA, "-group", testGroup, "-mode", "755"};
assertEquals(0, ToolRunner.run(admin, argv));
// add mount point with userb will be success since the nearest parent
// does not exist but have other EXECUTE permission.
UserGroupInformation userB = UserGroupInformation.createUserForTesting(
testUserB, new String[] {testGroup});
UserGroupInformation.setLoginUser(userB);
argv = new String[]{"-add", "/testpath6-1/parent/foo", "ns0",
"/testdir6-1/parent/foo", "-owner", testUserA, "-group", testGroup,
"-mode", "755"};
assertEquals(0, ToolRunner.run(admin, argv));
// add mount point with userb will be failure since the nearest parent
// does exist but no WRITE permission.
argv = new String[]{"-add", "/testpath6-1/foo", "ns0",
"/testdir6-1/foo", "-owner", testUserA, "-group", testGroup,
"-mode", "755"};
assertEquals(-1, ToolRunner.run(admin, argv));
} finally {
// set back login user
UserGroupInformation.setLoginUser(superUser);
}
}
@Test
public void testMountTablePermissions() throws Exception {
// re-set system out for testing
System.setOut(new PrintStream(out));
// use superuser to add new mount table with only read permission
String[] argv = new String[] {"-add", "/testpath2-1", "ns0", "/testdir2-1",
"-owner", TEST_USER, "-group", TEST_USER, "-mode", "0455"};
assertEquals(0, ToolRunner.run(admin, argv));
String superUser = UserGroupInformation.
getCurrentUser().getShortUserName();
// use normal user as current user to test
UserGroupInformation remoteUser = UserGroupInformation
.createRemoteUser(TEST_USER);
UserGroupInformation.setLoginUser(remoteUser);
// verify read permission by executing other commands
verifyExecutionResult("/testpath2-1", true, -1, -1);
// add new mount table with only write permission
argv = new String[] {"-add", "/testpath2-2", "ns0", "/testdir2-2",
"-owner", TEST_USER, "-group", TEST_USER, "-mode", "0255"};
assertEquals(0, ToolRunner.run(admin, argv));
verifyExecutionResult("/testpath2-2", false, -1, 0);
// set mount table entry with read and write permission
argv = new String[] {"-add", "/testpath2-3", "ns0", "/testdir2-3",
"-owner", TEST_USER, "-group", TEST_USER, "-mode", "0755"};
assertEquals(0, ToolRunner.run(admin, argv));
verifyExecutionResult("/testpath2-3", true, 0, 0);
// set back login user
remoteUser = UserGroupInformation.createRemoteUser(superUser);
UserGroupInformation.setLoginUser(remoteUser);
}
/**
* Verify router admin commands execution result.
*
* @param mount
* target mount table
* @param canRead
* whether you can list mount tables under specified mount
* @param addCommandCode
* expected return code of add command executed for specified mount
* @param rmCommandCode
* expected return code of rm command executed for specified mount
* @throws Exception
*/
private void verifyExecutionResult(String mount, boolean canRead,
int addCommandCode, int rmCommandCode) throws Exception {
String[] argv = null;
stateStore.loadCache(MountTableStoreImpl.class, true);
out.reset();
// execute ls command
argv = new String[] {"-ls", mount};
assertEquals(0, ToolRunner.run(admin, argv));
assertEquals(canRead, out.toString().contains(mount));
// execute add/update command
argv = new String[] {"-add", mount, "ns0", mount + "newdir"};
assertEquals(addCommandCode, ToolRunner.run(admin, argv));
stateStore.loadCache(MountTableStoreImpl.class, true);
// execute remove command
argv = new String[] {"-rm", mount};
assertEquals(rmCommandCode, ToolRunner.run(admin, argv));
}
@Test
public void testInvalidArgumentMessage() throws Exception {
String nsId = "ns0";
String src = "/testSource";
System.setOut(new PrintStream(out));
String[] argv = new String[] {"-add", src, nsId};
assertEquals(-1, ToolRunner.run(admin, argv));
assertTrue("Wrong message: " + out, out.toString().contains(
"\t[-add <source> <nameservice1, nameservice2, ...> <destination> "
+ "[-readonly] [-faulttolerant] "
+ "[-order HASH|LOCAL|RANDOM|HASH_ALL|SPACE] "
+ "-owner <owner> -group <group> -mode <mode>]"));
out.reset();
argv = new String[] {"-update", src, nsId};
assertEquals(-1, ToolRunner.run(admin, argv));
assertTrue("Wrong message: " + out, out.toString().contains(
"\t[-update <source> [<nameservice1, nameservice2, ...> <destination>] "
+ "[-readonly true|false] [-faulttolerant true|false] "
+ "[-order HASH|LOCAL|RANDOM|HASH_ALL|SPACE] "
+ "-owner <owner> -group <group> -mode <mode>]"));
out.reset();
argv = new String[] {"-rm"};
assertEquals(-1, ToolRunner.run(admin, argv));
assertTrue(out.toString().contains("\t[-rm <source>]"));
out.reset();
argv = new String[] {"-setQuota", src};
assertEquals(-1, ToolRunner.run(admin, argv));
assertTrue(out.toString()
.contains("\t[-setQuota <path> -nsQuota <nsQuota> -ssQuota "
+ "<quota in bytes or quota size string>]"));
out.reset();
argv = new String[] {"-clrQuota"};
assertEquals(-1, ToolRunner.run(admin, argv));
assertTrue(out.toString().contains("\t[-clrQuota <path>]"));
out.reset();
argv = new String[] {"-safemode"};
assertEquals(-1, ToolRunner.run(admin, argv));
assertTrue(out.toString().contains("\t[-safemode enter | leave | get]"));
out.reset();
argv = new String[] {"-nameservice", nsId};
assertEquals(-1, ToolRunner.run(admin, argv));
assertTrue(out.toString()
.contains("\t[-nameservice enable | disable <nameservice>]"));
out.reset();
argv = new String[] {"-getDestination"};
assertEquals(-1, ToolRunner.run(admin, argv));
assertTrue(out.toString().contains("\t[-getDestination <path>]"));
out.reset();
argv = new String[] {"-refreshRouterArgs"};
assertEquals(-1, ToolRunner.run(admin, argv));
assertTrue(out.toString().contains("\t[-refreshRouterArgs " +
"<host:ipc_port> <key> [arg1..argn]]"));
out.reset();
argv = new String[] {"-Random"};
assertEquals(-1, ToolRunner.run(admin, argv));
String expected = "Usage: hdfs dfsrouteradmin :\n"
+ "\t[-add <source> <nameservice1, nameservice2, ...> <destination> "
+ "[-readonly] [-faulttolerant] "
+ "[-order HASH|LOCAL|RANDOM|HASH_ALL|SPACE] "
+ "-owner <owner> -group <group> -mode <mode>]\n"
+ "\t[-update <source> [<nameservice1, nameservice2, ...> "
+ "<destination>] [-readonly true|false]"
+ " [-faulttolerant true|false] "
+ "[-order HASH|LOCAL|RANDOM|HASH_ALL|SPACE] "
+ "-owner <owner> -group <group> -mode <mode>]\n" + "\t[-rm <source>]\n"
+ "\t[-ls [-d] <path>]\n"
+ "\t[-getDestination <path>]\n"
+ "\t[-setQuota <path> -nsQuota <nsQuota> -ssQuota"
+ " <quota in bytes or quota size string>]\n"
+ "\t[-setStorageTypeQuota <path> -storageType <storage type>"
+ " <quota in bytes or quota size string>]\n"
+ "\t[-clrQuota <path>]\n"
+ "\t[-clrStorageTypeQuota <path>]\n"
+ "\t[-dumpState]\n"
+ "\t[-safemode enter | leave | get]\n"
+ "\t[-nameservice enable | disable <nameservice>]\n"
+ "\t[-getDisabledNameservices]\n"
+ "\t[-refresh]\n"
+ "\t[-refreshRouterArgs <host:ipc_port> <key> [arg1..argn]]";
assertTrue("Wrong message: " + out, out.toString().contains(expected));
out.reset();
}
/**
* Test command -setStorageTypeQuota with wrong arguments.
*/
@Test
public void testWrongArgumentsWhenSetStorageTypeQuota() throws Exception {
String src = "/type-QuotaMounttable";
// verify wrong arguments.
System.setErr(new PrintStream(err));
String[] argv =
new String[] {"-setStorageTypeQuota", src, "check", "c2", "c3"};
ToolRunner.run(admin, argv);
assertTrue(err.toString().contains("Invalid argument : check"));
}
/**
* Test command -setStorageTypeQuota.
*/
@Test
public void testSetStorageTypeQuota() throws Exception {
String nsId = "ns0";
String src = "/type-QuotaMounttable";
String dest = "/type-QuotaMounttable";
try {
addMountTable(src, nsId, dest);
// verify the default quota.
MountTable mountTable = getMountTable(src).get(0);
RouterQuotaUsage quotaUsage = mountTable.getQuota();
for (StorageType t : StorageType.values()) {
assertEquals(RouterQuotaUsage.QUOTA_USAGE_COUNT_DEFAULT,
quotaUsage.getTypeConsumed(t));
assertEquals(HdfsConstants.QUOTA_RESET, quotaUsage.getTypeQuota(t));
}
// set storage type quota.
long ssQuota = 100;
setStorageTypeQuota(src, ssQuota, StorageType.DISK);
// verify if the quota is set
mountTable = getMountTable(src).get(0);
quotaUsage = mountTable.getQuota();
assertEquals(ssQuota, quotaUsage.getTypeQuota(StorageType.DISK));
} finally {
rmMountTable(src);
}
}
/**
* Test command -clrStorageTypeQuota.
*/
@Test
public void testClearStorageTypeQuota() throws Exception {
String nsId = "ns0";
String src = "/type-QuotaMounttable";
String src1 = "/type-QuotaMounttable1";
String dest = "/type-QuotaMounttable";
String dest1 = "/type-QuotaMounttable1";
long ssQuota = 100;
try {
// add mount points.
addMountTable(src, nsId, dest);
addMountTable(src1, nsId, dest1);
// set storage type quota to src and src1.
setStorageTypeQuota(src, ssQuota, StorageType.DISK);
assertEquals(ssQuota,
getMountTable(src).get(0).getQuota().getTypeQuota(StorageType.DISK));
setStorageTypeQuota(src1, ssQuota, StorageType.DISK);
assertEquals(ssQuota,
getMountTable(src1).get(0).getQuota().getTypeQuota(StorageType.DISK));
// clrQuota of src and src1.
assertEquals(0, ToolRunner
.run(admin, new String[] {"-clrStorageTypeQuota", src, src1}));
stateStore.loadCache(MountTableStoreImpl.class, true);
// Verify whether the storage type quotas are cleared.
List<MountTable> mountTables = getMountTable("/");
for (int i = 0; i < 2; i++) {
MountTable mountTable = mountTables.get(i);
RouterQuotaUsage quotaUsage = mountTable.getQuota();
for (StorageType t : StorageType.values()) {
assertEquals(RouterQuotaUsage.QUOTA_USAGE_COUNT_DEFAULT,
quotaUsage.getTypeConsumed(t));
assertEquals(HdfsConstants.QUOTA_RESET, quotaUsage.getTypeQuota(t));
}
}
} finally {
rmMountTable(src);
rmMountTable(src1);
}
}
@Test
public void testSetAndClearQuota() throws Exception {
String nsId = "ns0";
String src = "/test-QuotaMounttable";
String src1 = "/test-QuotaMounttable1";
String dest = "/QuotaMounttable";
String[] argv = new String[] {"-add", src, nsId, dest};
assertEquals(0, ToolRunner.run(admin, argv));
stateStore.loadCache(MountTableStoreImpl.class, true);
GetMountTableEntriesRequest getRequest = GetMountTableEntriesRequest
.newInstance(src);
GetMountTableEntriesResponse getResponse = client.getMountTableManager()
.getMountTableEntries(getRequest);
MountTable mountTable = getResponse.getEntries().get(0);
RouterQuotaUsage quotaUsage = mountTable.getQuota();
// verify the default quota set
assertEquals(RouterQuotaUsage.QUOTA_USAGE_COUNT_DEFAULT,
quotaUsage.getFileAndDirectoryCount());
assertEquals(HdfsConstants.QUOTA_RESET, quotaUsage.getQuota());
assertEquals(RouterQuotaUsage.QUOTA_USAGE_COUNT_DEFAULT,
quotaUsage.getSpaceConsumed());
assertEquals(HdfsConstants.QUOTA_RESET, quotaUsage.getSpaceQuota());
long nsQuota = 50;
long ssQuota = 100;
argv = new String[] {"-setQuota", src, "-nsQuota", String.valueOf(nsQuota),
"-ssQuota", String.valueOf(ssQuota)};