summaryrefslogtreecommitdiffstats
path: root/qa/tasks/cephfs/test_admin.py
blob: beb41019e6d61b2204b7451261d75ccc1fc44b42 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
import errno
import json
import logging
import uuid
from io import StringIO
from os.path import join as os_path_join
import re
from time import sleep

from teuthology.exceptions import CommandFailedError
from teuthology.contextutil import safe_while

from tasks.cephfs.cephfs_test_case import CephFSTestCase, classhook
from tasks.cephfs.filesystem import FileLayout, FSMissing
from tasks.cephfs.fuse_mount import FuseMount
from tasks.cephfs.caps_helper import (CapTester, gen_mon_cap_str,
                                      gen_osd_cap_str, gen_mds_cap_str)

log = logging.getLogger(__name__)
MDS_RESTART_GRACE = 60

class TestLabeledPerfCounters(CephFSTestCase):
    CLIENTS_REQUIRED = 2
    MDSS_REQUIRED = 1

    def _get_counters_for(self, filesystem, client_id):
        dump = self.fs.rank_tell(["counter", "dump"])
        per_client_metrics_key = f'mds_client_metrics-{filesystem}'
        counters = [c["counters"] for \
                    c in dump[per_client_metrics_key] if c["labels"]["client"] == client_id]
        return counters[0]

    def test_per_client_labeled_perf_counters_on_client_disconnect(self):
        """
        That the per-client labelled metrics are unavailable during client disconnect
        """
        mount_a_id = f'client.{self.mount_a.get_global_id()}'
        self.mount_a.teardown()
        with safe_while(sleep=1, tries=30, action=f'wait for counters - {mount_a_id}') as proceed:
            while proceed():
                dump = self.fs.rank_tell(["counter", "dump"])
                per_client_metrics_key = f"mds_client_metrics-{dump['mds_client_metrics'][0]['labels']['fs_name']}"
                clients = [c["labels"]["client"] for c in dump.get(per_client_metrics_key, {})]
                if clients and mount_a_id not in clients:
                    # success, no metrics.
                    return True

    def test_per_client_labeled_perf_counters_on_client_reconnect(self):
        """
        That the per-client labelled metrics are generated during client reconnect
        """
        # fail active mds and wait for reconnect
        mds = self.fs.get_active_names()[0]
        self.mds_cluster.mds_fail(mds)
        self.fs.wait_for_state('up:active', rank=0, timeout=MDS_RESTART_GRACE)
        mount_a_id = f'client.{self.mount_a.get_global_id()}'
        mount_b_id = f'client.{self.mount_b.get_global_id()}'
        fs_suffix = ""

        with safe_while(sleep=1, tries=30, action='wait for counters') as proceed:
            while proceed():
                dump = self.fs.rank_tell(["counter", "dump"])
                fs_suffix = dump['mds_client_metrics'][0]['labels']['fs_name']
                per_client_metrics_key = f"mds_client_metrics-{fs_suffix}"
                clients = [c["labels"]["client"] for c in dump.get(per_client_metrics_key, {})]
                if mount_a_id in clients and mount_b_id in clients:
                    # success, got metrics.
                    break # break to continue the test

        # Post reconnecting, validate the io perf counters
        # write workload
        self.mount_a.create_n_files("test_dir/test_file", 1000, sync=True)
        with safe_while(sleep=1, tries=30, action=f'wait for counters - {mount_a_id}') as proceed:
            while proceed():
                counters_dump_a = self._get_counters_for(fs_suffix, mount_a_id)
                if counters_dump_a["total_write_ops"] > 0 and counters_dump_a["total_write_size"] > 0 and \
                   counters_dump_a["avg_write_latency"] >= 0 and counters_dump_a["avg_metadata_latency"] >= 0 and  \
                   counters_dump_a["opened_files"] >= 0 and counters_dump_a["opened_inodes"] > 0 and \
                   counters_dump_a["cap_hits"] > 0 and counters_dump_a["dentry_lease_hits"] > 0 and \
                   counters_dump_a["pinned_icaps"] > 0:
                    break # break to continue the test

        # read from the other client
        for i in range(100):
            self.mount_b.open_background(basename=f'test_dir/test_file_{i}', write=False)
        with safe_while(sleep=1, tries=30, action=f'wait for counters - {mount_b_id}') as proceed:
            while proceed():
                counters_dump_b = self._get_counters_for(fs_suffix, mount_b_id)
                if counters_dump_b["total_read_ops"] >= 0 and counters_dump_b["total_read_size"] >= 0 and \
                   counters_dump_b["avg_read_latency"] >= 0 and counters_dump_b["avg_metadata_latency"] >= 0 and \
                   counters_dump_b["opened_files"] >= 0 and counters_dump_b["opened_inodes"] >= 0 and \
                   counters_dump_b["cap_hits"] > 0 and counters_dump_a["dentry_lease_hits"] > 0 and \
                   counters_dump_b["pinned_icaps"] > 0:
                    break # break to continue the test
        self.mount_a.teardown()
        self.mount_b.teardown()

    def test_per_client_labeled_perf_counters_io(self):
        """
        That the per-client labelled perf counters depict the clients performing IO.
        """
        # sleep a bit so that we get updated clients...
        sleep(10)

        # lookout for clients...
        dump = self.fs.rank_tell(["counter", "dump"])

        fs_suffix = dump["mds_client_metrics"][0]["labels"]["fs_name"]
        self.assertGreaterEqual(dump["mds_client_metrics"][0]["counters"]["num_clients"], 2)

        per_client_metrics_key = f'mds_client_metrics-{fs_suffix}'
        mount_a_id = f'client.{self.mount_a.get_global_id()}'
        mount_b_id = f'client.{self.mount_b.get_global_id()}'

        clients = [c["labels"]["client"] for c in dump[per_client_metrics_key]]
        self.assertIn(mount_a_id, clients)
        self.assertIn(mount_b_id, clients)

        # write workload
        self.mount_a.create_n_files("test_dir/test_file", 1000, sync=True)
        with safe_while(sleep=1, tries=30, action=f'wait for counters - {mount_a_id}') as proceed:
            while proceed():
                counters_dump_a = self._get_counters_for(fs_suffix, mount_a_id)
                if counters_dump_a["total_write_ops"] > 0 and counters_dump_a["total_write_size"] > 0 and \
                   counters_dump_a["avg_write_latency"] >= 0 and counters_dump_a["avg_metadata_latency"] >= 0 and  \
                   counters_dump_a["opened_files"] >= 0 and counters_dump_a["opened_inodes"] > 0 and \
                   counters_dump_a["cap_hits"] > 0 and counters_dump_a["dentry_lease_hits"] > 0 and \
                   counters_dump_a["pinned_icaps"] > 0:
                    break # break to continue the test

        # read from the other client
        for i in range(100):
            self.mount_b.open_background(basename=f'test_dir/test_file_{i}', write=False)
        with safe_while(sleep=1, tries=30, action=f'wait for counters - {mount_b_id}') as proceed:
            while proceed():
                counters_dump_b = self._get_counters_for(fs_suffix, mount_b_id)
                if counters_dump_b["total_read_ops"] >= 0 and counters_dump_b["total_read_size"] >= 0 and \
                   counters_dump_b["avg_read_latency"] >= 0 and counters_dump_b["avg_metadata_latency"] >= 0 and \
                   counters_dump_b["opened_files"] >= 0 and counters_dump_b["opened_inodes"] >= 0 and \
                   counters_dump_b["cap_hits"] > 0 and counters_dump_a["dentry_lease_hits"] > 0 and \
                   counters_dump_b["pinned_icaps"] > 0:
                    break # break to continue the test
        self.mount_a.teardown()
        self.mount_b.teardown()

class TestAdminCommands(CephFSTestCase):
    """
    Tests for administration command.
    """

    CLIENTS_REQUIRED = 1
    MDSS_REQUIRED = 1

    def check_pool_application_metadata_key_value(self, pool, app, key, value):
        output = self.get_ceph_cmd_stdout(
            'osd', 'pool', 'application', 'get', pool, app, key)
        self.assertEqual(str(output.strip()), value)

    def setup_ec_pools(self, n, metadata=True, overwrites=True):
        if metadata:
            self.run_ceph_cmd('osd', 'pool', 'create', n+"-meta", "8")
        cmd = ['osd', 'erasure-code-profile', 'set', n+"-profile", "m=2", "k=2", "crush-failure-domain=osd"]
        self.run_ceph_cmd(cmd)
        self.run_ceph_cmd('osd', 'pool', 'create', n+"-data", "8", "erasure", n+"-profile")
        if overwrites:
            self.run_ceph_cmd('osd', 'pool', 'set', n+"-data", 'allow_ec_overwrites', 'true')

    def gen_health_warn_mds_cache_oversized(self):
        health_warn = 'MDS_CACHE_OVERSIZED'

        self.config_set('mds', 'mds_cache_memory_limit', '1K')
        self.config_set('mds', 'mds_health_cache_threshold', '1.00000')
        self.mount_a.open_n_background('.', 400)

        self.wait_for_health(health_warn, 30)

    def gen_health_warn_mds_trim(self):
        health_warn = 'MDS_TRIM'

        # for generating health warning MDS_TRIM
        self.config_set('mds', 'mds_debug_subtrees', 'true')
        # this will really really slow the trimming, so that MDS_TRIM stays
        # for longer.
        self.config_set('mds', 'mds_log_trim_decay_rate', '60')
        self.config_set('mds', 'mds_log_trim_threshold', '1')
        self.mount_a.open_n_background('.', 400)

        self.wait_for_health(health_warn, 30)


class TestMdsLastSeen(CephFSTestCase):
    """
    Tests for `mds last-seen` command.
    """

    MDSS_REQUIRED = 2

    def test_in_text(self):
        """
        That `mds last-seen` returns 0 for an MDS currently in the map.
        """

        status = self.fs.status()
        r0 = self.fs.get_rank(0, status=status)
        s = self.get_ceph_cmd_stdout("mds", "last-seen", r0['name'])
        seconds = int(re.match(r"^(\d+)s$", s).group(1))
        self.assertEqual(seconds, 0)

    def test_in_json(self):
        """
        That `mds last-seen` returns 0 for an MDS currently in the map.
        """

        status = self.fs.status()
        r0 = self.fs.get_rank(0, status=status)
        s = self.get_ceph_cmd_stdout("--format=json", "mds", "last-seen", r0['name'])
        J = json.loads(s)
        seconds = int(re.match(r"^(\d+)s$", J['last-seen']).group(1))
        self.assertEqual(seconds, 0)

    def test_unknown(self):
        """
        That `mds last-seen` returns ENOENT for an mds not in recent maps.
        """

        try:
            self.get_ceph_cmd_stdout("--format=json", "mds", "last-seen", 'foo')
        except CommandFailedError as e:
            self.assertEqual(e.exitstatus, errno.ENOENT)
        else:
            self.fail("non-existent mds should fail ENOENT")

    def test_standby(self):
        """
        That `mds last-seen` returns 0 for a standby.
        """

        status = self.fs.status()
        for info in status.get_standbys():
            s = self.get_ceph_cmd_stdout("--format=json", "mds", "last-seen", info['name'])
            J = json.loads(s)
            seconds = int(re.match(r"^(\d+)s$", J['last-seen']).group(1))
            self.assertEqual(seconds, 0)

    def test_stopped(self):
        """
        That `mds last-seen` returns >0 for mds that is stopped.
        """

        status = self.fs.status()
        r0 = self.fs.get_rank(0, status=status)
        self.fs.mds_stop(mds_id=r0['name'])
        self.fs.rank_fail()
        sleep(2)
        with safe_while(sleep=1, tries=self.fs.beacon_timeout, action='wait for last-seen >0') as proceed:
            while proceed():
                s = self.get_ceph_cmd_stdout("--format=json", "mds", "last-seen", r0['name'])
                J = json.loads(s)
                seconds = int(re.match(r"^(\d+)s$", J['last-seen']).group(1))
                if seconds == 0:
                    continue
                self.assertGreater(seconds, 1)
                break

    def test_gc(self):
        """
        That historical mds information is eventually garbage collected.
        """

        prune_time = 20
        sleep_time = 2
        self.config_set('mon', 'mon_fsmap_prune_threshold', prune_time)
        status = self.fs.status()
        r0 = self.fs.get_rank(0, status=status)
        self.fs.mds_stop(mds_id=r0['name'])
        self.fs.rank_fail()
        last = 0
        for i in range(prune_time):
            sleep(sleep_time) # we will sleep twice prune_time
            try:
                s = self.get_ceph_cmd_stdout("--format=json", "mds", "last-seen", r0['name'])
                J = json.loads(s)
                seconds = int(re.match(r"^(\d+)s$", J['last-seen']).group(1))
                self.assertGreater(seconds, last)
                log.debug("last_seen: %ds", seconds)
                last = seconds
            except CommandFailedError as e:
                self.assertEqual(e.exitstatus, errno.ENOENT)
                self.assertGreaterEqual(last + sleep_time + 1, prune_time) # rounding error add 1
                return
        self.fail("map was no garbage collected as expected")

@classhook('_add_valid_tell')
class TestValidTell(TestAdminCommands):
    @classmethod
    def _add_valid_tell(cls):
        tells = [
          ['cache', 'status'],
          ['damage', 'ls'],
          ['dump_blocked_ops'],
          ['dump_blocked_ops_count'],
          ['dump_historic_ops'],
          ['dump_historic_ops_by_duration'],
          ['dump_mempools'],
          ['dump_ops_in_flight'],
          ['flush', 'journal'],
          ['get', 'subtrees'],
          ['ops', 'locks'],
          ['ops'],
          ['status'],
          ['version'],
        ]
        def test(c):
            def f(self):
                J = self.fs.rank_tell(c)
                json.dumps(J)
                log.debug("dumped:\n%s", str(J))
            return f
        for c in tells:
            setattr(cls, 'test_valid_' + '_'.join(c), test(c))

class TestFsStatus(TestAdminCommands):
    """
    Test "ceph fs status subcommand.
    """

    MDSS_REQUIRED = 3

    def test_fs_status(self):
        """
        That `ceph fs status` command functions.
        """

        s = self.get_ceph_cmd_stdout("fs", "status")
        self.assertTrue("active" in s)

        mdsmap = json.loads(self.get_ceph_cmd_stdout("fs", "status", "--format=json-pretty"))["mdsmap"]
        self.assertEqual(mdsmap[0]["state"], "active")

        mdsmap = json.loads(self.get_ceph_cmd_stdout("fs", "status", "--format=json"))["mdsmap"]
        self.assertEqual(mdsmap[0]["state"], "active")

    def test_fs_status_standby_replay(self):
        """
        That `ceph fs status` command functions.
        """

        self.fs.set_allow_standby_replay(True)

        s = self.get_ceph_cmd_stdout("fs", "status")
        self.assertTrue("active" in s)
        self.assertTrue("standby-replay" in s)
        self.assertTrue("0-s" in s)
        self.assertTrue("standby" in s)

        mdsmap = json.loads(self.get_ceph_cmd_stdout("fs", "status", "--format=json-pretty"))["mdsmap"]
        self.assertEqual(mdsmap[0]["state"], "active")
        self.assertEqual(mdsmap[1]["state"], "standby-replay")
        self.assertEqual(mdsmap[1]["rank"], "0-s")
        self.assertEqual(mdsmap[2]["state"], "standby")

        mdsmap = json.loads(self.get_ceph_cmd_stdout("fs", "status", "--format=json"))["mdsmap"]
        self.assertEqual(mdsmap[0]["state"], "active")
        self.assertEqual(mdsmap[1]["state"], "standby-replay")
        self.assertEqual(mdsmap[1]["rank"], "0-s")
        self.assertEqual(mdsmap[2]["state"], "standby")


class TestAddDataPool(TestAdminCommands):
    """
    Test "ceph fs add_data_pool" subcommand.
    """

    def test_add_data_pool_root(self):
        """
        That a new data pool can be added and used for the root directory.
        """

        p = self.fs.add_data_pool("foo")
        self.fs.set_dir_layout(self.mount_a, ".", FileLayout(pool=p))

    def test_add_data_pool_application_metadata(self):
        """
        That the application metadata set on a newly added data pool is as expected.
        """
        pool_name = "foo"
        mon_cmd = self.get_ceph_cmd_stdout
        mon_cmd('osd', 'pool', 'create', pool_name, '--pg_num_min',
                str(self.fs.pg_num_min))
        # Check whether https://tracker.ceph.com/issues/43061 is fixed
        mon_cmd('osd', 'pool', 'application', 'enable', pool_name, 'cephfs')
        self.fs.add_data_pool(pool_name, create=False)
        self.check_pool_application_metadata_key_value(
            pool_name, 'cephfs', 'data', self.fs.name)

    def test_add_data_pool_subdir(self):
        """
        That a new data pool can be added and used for a sub-directory.
        """

        p = self.fs.add_data_pool("foo")
        self.mount_a.run_shell("mkdir subdir")
        self.fs.set_dir_layout(self.mount_a, "subdir", FileLayout(pool=p))

    def test_add_data_pool_non_alphamueric_name_as_subdir(self):
        """
        That a new data pool with non-alphanumeric name can be added and used for a sub-directory.
        """
        p = self.fs.add_data_pool("I-am-data_pool00.")
        self.mount_a.run_shell("mkdir subdir")
        self.fs.set_dir_layout(self.mount_a, "subdir", FileLayout(pool=p))

    def test_add_data_pool_ec(self):
        """
        That a new EC data pool can be added.
        """

        n = "test_add_data_pool_ec"
        self.setup_ec_pools(n, metadata=False)
        self.fs.add_data_pool(n+"-data", create=False)

    def test_add_already_in_use_data_pool(self):
        """
        That command try to add data pool which is already in use with another fs.
        """

        # create first data pool, metadata pool and add with filesystem
        first_fs = "first_fs"
        first_metadata_pool = "first_metadata_pool"
        first_data_pool = "first_data_pool"
        self.run_ceph_cmd('osd', 'pool', 'create', first_metadata_pool)
        self.run_ceph_cmd('osd', 'pool', 'create', first_data_pool)
        self.run_ceph_cmd('fs', 'new', first_fs, first_metadata_pool, first_data_pool)

        # create second data pool, metadata pool and add with filesystem
        second_fs = "second_fs"
        second_metadata_pool = "second_metadata_pool"
        second_data_pool = "second_data_pool"
        self.run_ceph_cmd('osd', 'pool', 'create', second_metadata_pool)
        self.run_ceph_cmd('osd', 'pool', 'create', second_data_pool)
        self.run_ceph_cmd('fs', 'new', second_fs, second_metadata_pool, second_data_pool)

        # try to add 'first_data_pool' with 'second_fs'
        # Expecting EINVAL exit status because 'first_data_pool' is already in use with 'first_fs'
        try:
            self.run_ceph_cmd('fs', 'add_data_pool', second_fs, first_data_pool)
        except CommandFailedError as e:
            self.assertEqual(e.exitstatus, errno.EINVAL)
        else:
            self.fail("Expected EINVAL because data pool is already in use as data pool for first_fs")

    def test_add_already_in_use_metadata_pool(self):
        """
        That command try to add metadata pool which is already in use with another fs.
        """

        # create first data pool, metadata pool and add with filesystem
        first_fs = "first_fs"
        first_metadata_pool = "first_metadata_pool"
        first_data_pool = "first_data_pool"
        self.run_ceph_cmd('osd', 'pool', 'create', first_metadata_pool)
        self.run_ceph_cmd('osd', 'pool', 'create', first_data_pool)
        self.run_ceph_cmd('fs', 'new', first_fs, first_metadata_pool, first_data_pool)

        # create second data pool, metadata pool and add with filesystem
        second_fs = "second_fs"
        second_metadata_pool = "second_metadata_pool"
        second_data_pool = "second_data_pool"
        self.run_ceph_cmd('osd', 'pool', 'create', second_metadata_pool)
        self.run_ceph_cmd('osd', 'pool', 'create', second_data_pool)
        self.run_ceph_cmd('fs', 'new', second_fs, second_metadata_pool, second_data_pool)

        # try to add 'second_metadata_pool' with 'first_fs' as a data pool
        # Expecting EINVAL exit status because 'second_metadata_pool'
        # is already in use with 'second_fs' as a metadata pool
        try:
            self.run_ceph_cmd('fs', 'add_data_pool', first_fs, second_metadata_pool)
        except CommandFailedError as e:
            self.assertEqual(e.exitstatus, errno.EINVAL)
        else:
            self.fail("Expected EINVAL because data pool is already in use as metadata pool for 'second_fs'")

class TestFsNew(TestAdminCommands):
    """
    Test "ceph fs new" subcommand.
    """
    MDSS_REQUIRED = 3

    def test_fsnames_can_only_by_goodchars(self):
        n = 'test_fsnames_can_only_by_goodchars'
        metapoolname, datapoolname = n+'-testmetapool', n+'-testdatapool'
        badname = n+'badname@#'

        self.run_ceph_cmd('osd', 'pool', 'create', n+metapoolname)
        self.run_ceph_cmd('osd', 'pool', 'create', n+datapoolname)

        # test that fsname not with "goodchars" fails
        args = ['fs', 'new', badname, metapoolname, datapoolname]
        proc = self.run_ceph_cmd(args=args, stderr=StringIO(),
                                 check_status=False)
        self.assertIn('invalid chars', proc.stderr.getvalue().lower())

        self.run_ceph_cmd('osd', 'pool', 'rm', metapoolname,
                          metapoolname,
                          '--yes-i-really-really-mean-it-not-faking')
        self.run_ceph_cmd('osd', 'pool', 'rm', datapoolname,
                          datapoolname,
                          '--yes-i-really-really-mean-it-not-faking')

    def test_new_default_ec(self):
        """
        That a new file system warns/fails with an EC default data pool.
        """

        self.mount_a.umount_wait(require_clean=True)
        self.mds_cluster.delete_all_filesystems()
        n = "test_new_default_ec"
        self.setup_ec_pools(n)
        try:
            self.run_ceph_cmd('fs', 'new', n, n+"-meta", n+"-data")
        except CommandFailedError as e:
            if e.exitstatus == 22:
                pass
            else:
                raise
        else:
            raise RuntimeError("expected failure")

    def test_new_default_ec_force(self):
        """
        That a new file system succeeds with an EC default data pool with --force.
        """

        self.mount_a.umount_wait(require_clean=True)
        self.mds_cluster.delete_all_filesystems()
        n = "test_new_default_ec_force"
        self.setup_ec_pools(n)
        self.run_ceph_cmd('fs', 'new', n, n+"-meta", n+"-data", "--force")

    def test_new_default_ec_no_overwrite(self):
        """
        That a new file system fails with an EC default data pool without overwrite.
        """

        self.mount_a.umount_wait(require_clean=True)
        self.mds_cluster.delete_all_filesystems()
        n = "test_new_default_ec_no_overwrite"
        self.setup_ec_pools(n, overwrites=False)
        try:
            self.run_ceph_cmd('fs', 'new', n, n+"-meta", n+"-data")
        except CommandFailedError as e:
            if e.exitstatus == 22:
                pass
            else:
                raise
        else:
            raise RuntimeError("expected failure")
        # and even with --force !
        try:
            self.run_ceph_cmd('fs', 'new', n, n+"-meta", n+"-data", "--force")
        except CommandFailedError as e:
            if e.exitstatus == 22:
                pass
            else:
                raise
        else:
            raise RuntimeError("expected failure")

    def test_fs_new_pool_application_metadata(self):
        """
        That the application metadata set on the pools of a newly created filesystem are as expected.
        """
        self.mount_a.umount_wait(require_clean=True)
        self.mds_cluster.delete_all_filesystems()
        fs_name = "test_fs_new_pool_application"
        keys = ['metadata', 'data']
        pool_names = [fs_name+'-'+key for key in keys]
        mon_cmd = self.get_ceph_cmd_stdout
        for p in pool_names:
            mon_cmd('osd', 'pool', 'create', p, '--pg_num_min', str(self.fs.pg_num_min))
            mon_cmd('osd', 'pool', 'application', 'enable', p, 'cephfs')
        mon_cmd('fs', 'new', fs_name, pool_names[0], pool_names[1])
        for i in range(2):
            self.check_pool_application_metadata_key_value(
                pool_names[i], 'cephfs', keys[i], fs_name)

    def test_fs_new_with_specific_id(self):
        """
        That a file system can be created with a specific ID.
        """
        fs_name = "test_fs_specific_id"
        fscid = 100
        keys = ['metadata', 'data']
        pool_names = [fs_name+'-'+key for key in keys]
        for p in pool_names:
            self.run_ceph_cmd(f'osd pool create {p}')
        self.run_ceph_cmd(f'fs new {fs_name} {pool_names[0]} {pool_names[1]} --fscid  {fscid} --force')
        self.fs.status().get_fsmap(fscid)
        for i in range(2):
            self.check_pool_application_metadata_key_value(pool_names[i], 'cephfs', keys[i], fs_name)

    def test_fs_new_with_specific_id_idempotency(self):
        """
        That command to create file system with specific ID is idempotent.
        """
        fs_name = "test_fs_specific_id"
        fscid = 100
        keys = ['metadata', 'data']
        pool_names = [fs_name+'-'+key for key in keys]
        for p in pool_names:
            self.run_ceph_cmd(f'osd pool create {p}')
        self.run_ceph_cmd(f'fs new {fs_name} {pool_names[0]} {pool_names[1]} --fscid  {fscid} --force')
        self.run_ceph_cmd(f'fs new {fs_name} {pool_names[0]} {pool_names[1]} --fscid  {fscid} --force')
        self.fs.status().get_fsmap(fscid)

    def test_fs_new_with_specific_id_fails_without_force_flag(self):
        """
        That command to create file system with specific ID fails without '--force' flag.
        """
        fs_name = "test_fs_specific_id"
        fscid = 100
        keys = ['metadata', 'data']
        pool_names = [fs_name+'-'+key for key in keys]
        for p in pool_names:
            self.run_ceph_cmd(f'osd pool create {p}')
        try:
            self.run_ceph_cmd(f'fs new {fs_name} {pool_names[0]} {pool_names[1]} --fscid  {fscid}')
        except CommandFailedError as ce:
            self.assertEqual(ce.exitstatus, errno.EINVAL,
                "invalid error code on creating a file system with specifc ID without --force flag")
        else:
            self.fail("expected creating file system with specific ID without '--force' flag to fail")

    def test_fs_new_with_specific_id_fails_already_in_use(self):
        """
        That creating file system with ID already in use fails.
        """
        fs_name = "test_fs_specific_id"
        # file system ID already in use
        fscid =  self.fs.status().map['filesystems'][0]['id']
        keys = ['metadata', 'data']
        pool_names = [fs_name+'-'+key for key in keys]
        for p in pool_names:
            self.run_ceph_cmd(f'osd pool create {p}')
        try:
            self.run_ceph_cmd(f'fs new {fs_name} {pool_names[0]} {pool_names[1]} --fscid  {fscid} --force')
        except CommandFailedError as ce:
            self.assertEqual(ce.exitstatus, errno.EINVAL,
                "invalid error code on creating a file system with specifc ID that is already in use")
        else:
            self.fail("expected creating file system with ID already in use to fail")

    def test_fs_new_metadata_pool_already_in_use(self):
        """
        That creating file system with metadata pool already in use.
        """

        # create first data pool, metadata pool and add with filesystem
        first_fs = "first_fs"
        first_metadata_pool = "first_metadata_pool"
        first_data_pool = "first_data_pool"
        self.run_ceph_cmd('osd', 'pool', 'create', first_metadata_pool)
        self.run_ceph_cmd('osd', 'pool', 'create', first_data_pool)
        self.run_ceph_cmd('fs', 'new', first_fs, first_metadata_pool, first_data_pool)

        second_fs = "second_fs"
        second_data_pool = "second_data_pool"
        self.run_ceph_cmd('osd', 'pool', 'create', second_data_pool)

        # try to create new fs 'second_fs' with following configuration
        # metadata pool -> 'first_metadata_pool'
        # data pool -> 'second_data_pool'
        # Expecting EINVAL exit status because 'first_metadata_pool'
        # is already in use with 'first_fs'
        try:
            self.run_ceph_cmd('fs', 'new', second_fs, first_metadata_pool, second_data_pool)
        except CommandFailedError as e:
            self.assertEqual(e.exitstatus, errno.EINVAL)
        else:
            self.fail("Expected EINVAL because metadata  pool is already in use for 'first_fs'")

    def test_fs_new_data_pool_already_in_use(self):
        """
        That creating file system with data pool already in use.
        """

        # create first data pool, metadata pool and add with filesystem
        first_fs = "first_fs"
        first_metadata_pool = "first_metadata_pool"
        first_data_pool = "first_data_pool"
        self.run_ceph_cmd('osd', 'pool', 'create', first_metadata_pool)
        self.run_ceph_cmd('osd', 'pool', 'create', first_data_pool)
        self.run_ceph_cmd('fs', 'new', first_fs, first_metadata_pool, first_data_pool)

        second_fs = "second_fs"
        second_metadata_pool = "second_metadata_pool"
        self.run_ceph_cmd('osd', 'pool', 'create', second_metadata_pool)

        # try to create new fs 'second_fs' with following configuration
        # metadata pool -> 'second_metadata_pool'
        # data pool -> 'first_data_pool'
        # Expecting EINVAL exit status because 'first_data_pool'
        # is already in use with 'first_fs'
        try:
            self.run_ceph_cmd('fs', 'new', second_fs, second_metadata_pool, first_data_pool)
        except CommandFailedError as e:
            self.assertEqual(e.exitstatus, errno.EINVAL)
        else:
            self.fail("Expected EINVAL because data pool is already in use for 'first_fs'")

    def test_fs_new_metadata_and_data_pool_in_use_by_another_same_fs(self):
        """
        That creating file system with metadata and data pool which is already in use by another same fs.
        """

        # create first data pool, metadata pool and add with filesystem
        first_fs = "first_fs"
        first_metadata_pool = "first_metadata_pool"
        first_data_pool = "first_data_pool"
        self.run_ceph_cmd('osd', 'pool', 'create', first_metadata_pool)
        self.run_ceph_cmd('osd', 'pool', 'create', first_data_pool)
        self.run_ceph_cmd('fs', 'new', first_fs, first_metadata_pool, first_data_pool)

        second_fs = "second_fs"

        # try to create new fs 'second_fs' with following configuration
        # metadata pool -> 'first_metadata_pool'
        # data pool -> 'first_data_pool'
        # Expecting EINVAL exit status because 'first_metadata_pool' and 'first_data_pool'
        # is already in use with 'first_fs'
        try:
            self.run_ceph_cmd('fs', 'new', second_fs, first_metadata_pool, first_data_pool)
        except CommandFailedError as e:
            self.assertEqual(e.exitstatus, errno.EINVAL)
        else:
            self.fail("Expected EINVAL because metadata and data pool is already in use for 'first_fs'")

    def test_fs_new_metadata_and_data_pool_in_use_by_different_fs(self):
        """
        That creating file system with metadata and data pool which is already in use by different fs.
        """

        # create first data pool, metadata pool and add with filesystem
        first_fs = "first_fs"
        first_metadata_pool = "first_metadata_pool"
        first_data_pool = "first_data_pool"
        self.run_ceph_cmd('osd', 'pool', 'create', first_metadata_pool)
        self.run_ceph_cmd('osd', 'pool', 'create', first_data_pool)
        self.run_ceph_cmd('fs', 'new', first_fs, first_metadata_pool, first_data_pool)

        # create second data pool, metadata pool and add with filesystem
        second_fs = "second_fs"
        second_metadata_pool = "second_metadata_pool"
        second_data_pool = "second_data_pool"
        self.run_ceph_cmd('osd', 'pool', 'create', second_metadata_pool)
        self.run_ceph_cmd('osd', 'pool', 'create', second_data_pool)
        self.run_ceph_cmd('fs', 'new', second_fs, second_metadata_pool, second_data_pool)

        third_fs = "third_fs"

        # try to create new fs 'third_fs' with following configuration
        # metadata pool -> 'first_metadata_pool'
        # data pool -> 'second_data_pool'
        # Expecting EINVAL exit status because 'first_metadata_pool' and 'second_data_pool'
        # is already in use with 'first_fs' and 'second_fs'
        try:
            self.run_ceph_cmd('fs', 'new', third_fs, first_metadata_pool, second_data_pool)
        except CommandFailedError as e:
            self.assertEqual(e.exitstatus, errno.EINVAL)
        else:
            self.fail("Expected EINVAL because metadata and data pool is already in use for 'first_fs' and 'second_fs'")

    def test_fs_new_interchange_already_in_use_metadata_and_data_pool_of_same_fs(self):
        """
        That creating file system with interchanging metadata and data pool which is already in use by same fs.
        """

        # create first data pool, metadata pool and add with filesystem
        first_fs = "first_fs"
        first_metadata_pool = "first_metadata_pool"
        first_data_pool = "first_data_pool"
        self.run_ceph_cmd('osd', 'pool', 'create', first_metadata_pool)
        self.run_ceph_cmd('osd', 'pool', 'create', first_data_pool)
        self.run_ceph_cmd('fs', 'new', first_fs, first_metadata_pool, first_data_pool)

        second_fs = "second_fs"

        # try to create new fs 'second_fs' with following configuration
        # metadata pool -> 'first_data_pool' (already used as data pool for 'first_fs')
        # data pool -> 'first_metadata_pool' (already used as metadata pool for 'first_fs')
        # Expecting EINVAL exit status because 'first_data_pool' and 'first_metadata_pool'
        # is already in use with 'first_fs'
        try:
            self.run_ceph_cmd('fs', 'new', second_fs, first_data_pool, first_metadata_pool)
        except CommandFailedError as e:
            self.assertEqual(e.exitstatus, errno.EINVAL)
        else:
            self.fail("Expected EINVAL because metadata and data pool is already in use for 'first_fs'")

    def test_fs_new_interchange_already_in_use_metadata_and_data_pool_of_different_fs(self):
        """
        That creating file system with interchanging metadata and data pool which is already in use by defferent fs.
        """

        # create first data pool, metadata pool and add with filesystem
        first_fs = "first_fs"
        first_metadata_pool = "first_metadata_pool"
        first_data_pool = "first_data_pool"
        self.run_ceph_cmd('osd', 'pool', 'create', first_metadata_pool)
        self.run_ceph_cmd('osd', 'pool', 'create', first_data_pool)
        self.run_ceph_cmd('fs', 'new', first_fs, first_metadata_pool, first_data_pool)

        # create second data pool, metadata pool and add with filesystem
        second_fs = "second_fs"
        second_metadata_pool = "second_metadata_pool"
        second_data_pool = "second_data_pool"
        self.run_ceph_cmd('osd', 'pool', 'create', second_metadata_pool)
        self.run_ceph_cmd('osd', 'pool', 'create', second_data_pool)
        self.run_ceph_cmd('fs', 'new', second_fs, second_metadata_pool, second_data_pool)

        third_fs = "third_fs"

        # try to create new fs 'third_fs' with following configuration
        # metadata pool -> 'first_data_pool' (already used as data pool for 'first_fs')
        # data pool -> 'second_metadata_pool' (already used as metadata pool for 'second_fs')
        # Expecting EINVAL exit status because 'first_data_pool' and 'second_metadata_pool'
        # is already in use with 'first_fs' and 'second_fs'
        try:
            self.run_ceph_cmd('fs', 'new', third_fs, first_data_pool, second_metadata_pool)
        except CommandFailedError as e:
            self.assertEqual(e.exitstatus, errno.EINVAL)
        else:
            self.fail("Expected EINVAL because metadata and data pool is already in use for 'first_fs' and 'second_fs'")

    def test_fs_new_metadata_pool_already_in_use_with_rbd(self):
        """
        That creating new file system with metadata pool already used by rbd.
        """

        # create pool and initialise with rbd
        new_pool = "new_pool"
        self.run_ceph_cmd('osd', 'pool', 'create', new_pool)
        self.ctx.cluster.run(args=['rbd', 'pool', 'init', new_pool])

        new_fs = "new_fs"
        new_data_pool = "new_data_pool"

        self.run_ceph_cmd('osd', 'pool', 'create', new_data_pool)

        # try to create new fs 'new_fs' with following configuration
        # metadata pool -> 'new_pool' (already used by rbd app)
        # data pool -> 'new_data_pool'
        # Expecting EINVAL exit status because 'new_pool' is already in use with 'rbd' app
        try:
            self.run_ceph_cmd('fs', 'new', new_fs, new_pool, new_data_pool)
        except CommandFailedError as e:
            self.assertEqual(e.exitstatus, errno.EINVAL)
        else:
            self.fail("Expected EINVAL because metadata pool is already in use for rbd")

    def test_fs_new_data_pool_already_in_use_with_rbd(self):
        """
        That creating new file system with data pool already used by rbd.
        """

        # create pool and initialise with rbd
        new_pool = "new_pool"
        self.run_ceph_cmd('osd', 'pool', 'create', new_pool)
        self.ctx.cluster.run(args=['rbd', 'pool', 'init', new_pool])

        new_fs = "new_fs"
        new_metadata_pool = "new_metadata_pool"

        self.run_ceph_cmd('osd', 'pool', 'create', new_metadata_pool)

        # try to create new fs 'new_fs' with following configuration
        # metadata pool -> 'new_metadata_pool'
        # data pool -> 'new_pool' (already used by rbd app)
        # Expecting EINVAL exit status because 'new_pool' is already in use with 'rbd' app
        try:
            self.run_ceph_cmd('fs', 'new', new_fs, new_metadata_pool, new_pool)
        except CommandFailedError as e:
            self.assertEqual(e.exitstatus, errno.EINVAL)
        else:
            self.fail("Expected EINVAL because data pool is already in use for rbd")

class TestRenameCommand(TestAdminCommands):
    """
    Tests for rename command.
    """

    CLIENTS_REQUIRED = 1
    MDSS_REQUIRED = 2

    def test_fs_rename(self):
        """
        That the file system can be renamed, and the application metadata set on its pools are as expected.
        """
        # Renaming the file system breaks this mount as the client uses
        # file system specific authorization. The client cannot read
        # or write even if the client's cephx ID caps are updated to access
        # the new file system name without the client being unmounted and
        # re-mounted.
        self.mount_a.umount_wait(require_clean=True)
        orig_fs_name = self.fs.name
        new_fs_name = 'new_cephfs'
        client_id = 'test_new_cephfs'

        self.run_ceph_cmd(f'fs fail {self.fs.name}')
        self.run_ceph_cmd(f'fs set {self.fs.name} refuse_client_session true')
        sleep(5)

        self.run_ceph_cmd(f'fs rename {orig_fs_name} {new_fs_name} --yes-i-really-mean-it')

        self.run_ceph_cmd(f'fs set {new_fs_name} joinable true')
        self.run_ceph_cmd(f'fs set {new_fs_name} refuse_client_session false')
        self.fs.wait_for_daemons()

        # authorize a cephx ID access to the renamed file system.
        # use the ID to write to the file system.
        self.fs.name = new_fs_name
        keyring = self.fs.authorize(client_id, ('/', 'rw'))
        keyring_path = self.mount_a.client_remote.mktemp(data=keyring)
        self.mount_a.remount(client_id=client_id,
                             client_keyring_path=keyring_path,
                             cephfs_mntpt='/',
                             cephfs_name=self.fs.name)
        filedata, filename = 'some data on fs', 'file_on_fs'
        filepath = os_path_join(self.mount_a.hostfs_mntpt, filename)
        self.mount_a.write_file(filepath, filedata)
        self.check_pool_application_metadata_key_value(
            self.fs.get_data_pool_name(), 'cephfs', 'data', new_fs_name)
        self.check_pool_application_metadata_key_value(
            self.fs.get_metadata_pool_name(), 'cephfs', 'metadata', new_fs_name)

        # cleanup
        self.mount_a.umount_wait()
        self.run_ceph_cmd(f'auth rm client.{client_id}')

    def test_fs_rename_idempotency(self):
        """
        That the file system rename operation is idempotent.
        """
        # Renaming the file system breaks this mount as the client uses
        # file system specific authorization.
        self.mount_a.umount_wait(require_clean=True)
        orig_fs_name = self.fs.name
        new_fs_name = 'new_cephfs'

        self.run_ceph_cmd(f'fs fail {self.fs.name}')
        self.run_ceph_cmd(f'fs set {self.fs.name} refuse_client_session true')
        sleep(5)

        self.run_ceph_cmd(f'fs rename {orig_fs_name} {new_fs_name} --yes-i-really-mean-it')
        self.run_ceph_cmd(f'fs rename {orig_fs_name} {new_fs_name} --yes-i-really-mean-it')

        self.run_ceph_cmd(f'fs set {new_fs_name} joinable true')
        self.run_ceph_cmd(f'fs set {new_fs_name} refuse_client_session false')
        self.fs.wait_for_daemons()

        # original file system name does not appear in `fs ls` command
        self.assertFalse(self.fs.exists())
        self.fs.name = new_fs_name
        self.assertTrue(self.fs.exists())

    def test_fs_rename_fs_new_fails_with_old_fsname_existing_pools(self):
        """
        That after renaming a file system, creating a file system with
        old name and existing FS pools fails.
        """
        # Renaming the file system breaks this mount as the client uses
        # file system specific authorization.
        self.mount_a.umount_wait(require_clean=True)
        orig_fs_name = self.fs.name
        new_fs_name = 'new_cephfs'
        data_pool = self.fs.get_data_pool_name()
        metadata_pool = self.fs.get_metadata_pool_name()

        self.run_ceph_cmd(f'fs fail {self.fs.name}')
        self.run_ceph_cmd(f'fs set {self.fs.name} refuse_client_session true')
        sleep(5)

        self.run_ceph_cmd(f'fs rename {orig_fs_name} {new_fs_name} --yes-i-really-mean-it')

        self.run_ceph_cmd(f'fs set {new_fs_name} joinable true')
        self.run_ceph_cmd(f'fs set {new_fs_name} refuse_client_session false')
        self.fs.wait_for_daemons()

        try:
            self.run_ceph_cmd(f"fs new {orig_fs_name} {metadata_pool} {data_pool}")
        except CommandFailedError as ce:
            self.assertEqual(ce.exitstatus, errno.EINVAL,
                "invalid error code on creating a new file system with old "
                "name and existing pools.")
        else:
            self.fail("expected creating new file system with old name and "
                      "existing pools to fail.")

        try:
            self.run_ceph_cmd(f"fs new {orig_fs_name} {metadata_pool} {data_pool} --force")
        except CommandFailedError as ce:
            self.assertEqual(ce.exitstatus, errno.EINVAL,
                "invalid error code on creating a new file system with old "
                "name, existing pools and --force flag.")
        else:
            self.fail("expected creating new file system with old name, "
                      "existing pools, and --force flag to fail.")

        try:
            self.run_ceph_cmd(f"fs new {orig_fs_name} {metadata_pool} {data_pool} "
                                 "--allow-dangerous-metadata-overlay")
        except CommandFailedError as ce:
            self.assertEqual(ce.exitstatus, errno.EINVAL,
                "invalid error code on creating a new file system with old name, "
                "existing pools and --allow-dangerous-metadata-overlay flag.")
        else:
            self.fail("expected creating new file system with old name, "
                      "existing pools, and --allow-dangerous-metadata-overlay flag to fail.")

    def test_fs_rename_fails_without_yes_i_really_mean_it_flag(self):
        """
        That renaming a file system without '--yes-i-really-mean-it' flag fails.
        """
        # Failing the file system breaks this mount
        self.mount_a.umount_wait(require_clean=True)

        self.run_ceph_cmd(f'fs fail {self.fs.name}')
        self.run_ceph_cmd(f'fs set {self.fs.name} refuse_client_session true')
        sleep(5)

        try:
            self.run_ceph_cmd(f"fs rename {self.fs.name} new_fs")
        except CommandFailedError as ce:
            self.assertEqual(ce.exitstatus, errno.EPERM,
                "invalid error code on renaming a file system without the  "
                "'--yes-i-really-mean-it' flag")
        else:
            self.fail("expected renaming of file system without the "
                      "'--yes-i-really-mean-it' flag to fail ")

        self.run_ceph_cmd(f'fs set {self.fs.name} joinable true')
        self.run_ceph_cmd(f'fs set {self.fs.name} refuse_client_session false')
        self.fs.wait_for_daemons()

    def test_fs_rename_fails_for_non_existent_fs(self):
        """
        That renaming a non-existent file system fails.
        """
        # Failing the file system breaks this mount
        self.mount_a.umount_wait(require_clean=True)

        self.run_ceph_cmd(f'fs fail {self.fs.name}')
        self.run_ceph_cmd(f'fs set {self.fs.name} refuse_client_session true')
        sleep(5)

        try:
            self.run_ceph_cmd("fs rename non_existent_fs new_fs --yes-i-really-mean-it")
        except CommandFailedError as ce:
            self.assertEqual(ce.exitstatus, errno.ENOENT, "invalid error code on renaming a non-existent fs")
        else:
            self.fail("expected renaming of a non-existent file system to fail")
        self.run_ceph_cmd(f'fs set {self.fs.name} joinable true')
        self.fs.wait_for_daemons()
        self.run_ceph_cmd(f'fs set {self.fs.name} refuse_client_session false')

    def test_fs_rename_fails_new_name_already_in_use(self):
        """
        That renaming a file system fails if the new name refers to an existing file system.
        """
        self.fs2 = self.mds_cluster.newfs(name='cephfs2', create=True)

        # let's unmount the client before failing the FS
        self.mount_a.umount_wait(require_clean=True)

        self.run_ceph_cmd(f'fs fail {self.fs.name}')
        self.run_ceph_cmd(f'fs set {self.fs.name} refuse_client_session true')
        sleep(5)

        try:
            self.run_ceph_cmd(f"fs rename {self.fs.name} {self.fs2.name} --yes-i-really-mean-it")
        except CommandFailedError as ce:
            self.assertEqual(ce.exitstatus, errno.EINVAL,
                             "invalid error code on renaming to a fs name that is already in use")
        else:
            self.fail("expected renaming to a new file system name that is already in use to fail.")

        self.run_ceph_cmd(f'fs set {self.fs.name} joinable true')
        self.run_ceph_cmd(f'fs set {self.fs.name} refuse_client_session false')
        self.fs.wait_for_daemons()

    def test_fs_rename_fails_with_mirroring_enabled(self):
        """
        That renaming a file system fails if mirroring is enabled on it.
        """
        orig_fs_name = self.fs.name
        new_fs_name = 'new_cephfs'

        # let's unmount the client before failing the FS
        self.mount_a.umount_wait(require_clean=True)

        self.run_ceph_cmd(f'fs mirror enable {orig_fs_name}')
        self.run_ceph_cmd(f'fs fail {self.fs.name}')
        self.run_ceph_cmd(f'fs set {self.fs.name} refuse_client_session true')
        sleep(5)

        try:
            self.run_ceph_cmd(f'fs rename {orig_fs_name} {new_fs_name} --yes-i-really-mean-it')
        except CommandFailedError as ce:
            self.assertEqual(ce.exitstatus, errno.EPERM, "invalid error code on renaming a mirrored file system")
        else:
            self.fail("expected renaming of a mirrored file system to fail")

        self.run_ceph_cmd(f'fs mirror disable {orig_fs_name}')
        self.run_ceph_cmd(f'fs set {self.fs.name} joinable true')
        self.run_ceph_cmd(f'fs set {self.fs.name} refuse_client_session false')
        self.fs.wait_for_daemons()

    def test_rename_when_fs_is_online(self):
        '''
        Test that the command "ceph fs swap" command fails when first of the
        two of FSs isn't failed/down.
        '''
        client_id = 'test_new_cephfs'
        new_fs_name = 'new_cephfs'

        self.run_ceph_cmd(f'fs set {self.fs.name} refuse_client_session true')

        self.negtest_ceph_cmd(
            args=(f'fs rename {self.fs.name} {new_fs_name} '
                   '--yes-i-really-mean-it'),
            errmsgs=(f"CephFS '{self.fs.name}' is not offline. Before "
                      "renaming a CephFS, it must be marked as down. See "
                      "`ceph fs fail`."),
            retval=errno.EPERM)

        self.run_ceph_cmd(f'fs set {self.fs.name} refuse_client_session false')

        self.fs.getinfo()
        keyring = self.fs.authorize(client_id, ('/', 'rw'))
        keyring_path = self.mount_a.client_remote.mktemp(data=keyring)
        self.mount_a.remount(client_id=client_id,
                             client_keyring_path=keyring_path,
                             cephfs_mntpt='/',
                             cephfs_name=self.fs.name)

        self.check_pool_application_metadata_key_value(
            self.fs.get_data_pool_name(), 'cephfs', 'data', self.fs.name)
        self.check_pool_application_metadata_key_value(
            self.fs.get_metadata_pool_name(), 'cephfs', 'metadata',
            self.fs.name)

    def test_rename_when_clients_not_refused(self):
        '''
        Test that "ceph fs rename" fails when client_refuse_session is not
        set.
        '''
        self.mount_a.umount_wait(require_clean=True)

        self.run_ceph_cmd(f'fs fail {self.fs.name}')

        self.negtest_ceph_cmd(
            args=f"fs rename {self.fs.name} new_fs --yes-i-really-mean-it",
            errmsgs=(f"CephFS '{self.fs.name}' doesn't refuse clients. "
                      "Before renaming a CephFS, flag "
                      "'refuse_client_session' must be set. See "
                      "`ceph fs set`."),
            retval=errno.EPERM)

        self.run_ceph_cmd(f'fs fail {self.fs.name}')


class TestDump(CephFSTestCase):
    CLIENTS_REQUIRED = 0
    MDSS_REQUIRED = 1

    def test_fs_dump_epoch(self):
        """
        That dumping a specific epoch works.
        """

        status1 = self.fs.status()
        status2 = self.fs.status(epoch=status1["epoch"]-1)
        self.assertEqual(status1["epoch"], status2["epoch"]+1)

    def test_fsmap_trim(self):
        """
        That the fsmap is trimmed normally.
        """

        paxos_service_trim_min = 25
        self.config_set('mon', 'paxos_service_trim_min', paxos_service_trim_min)
        mon_max_mdsmap_epochs = 20
        self.config_set('mon', 'mon_max_mdsmap_epochs', mon_max_mdsmap_epochs)

        status = self.fs.status()
        epoch = status["epoch"]

        # for N mutations
        mutations = paxos_service_trim_min + mon_max_mdsmap_epochs
        b = False
        for i in range(mutations):
            self.fs.set_joinable(b)
            b = not b

        sleep(10) # for tick/compaction

        try:
            self.fs.status(epoch=epoch)
        except CommandFailedError as e:
            self.assertEqual(e.exitstatus, errno.ENOENT, "invalid error code when trying to fetch FSMap that was trimmed")
        else:
            self.fail("trimming did not occur as expected")

    def test_fsmap_force_trim(self):
        """
        That the fsmap is trimmed forcefully.
        """

        status = self.fs.status()
        epoch = status["epoch"]

        paxos_service_trim_min = 1
        self.config_set('mon', 'paxos_service_trim_min', paxos_service_trim_min)
        mon_mds_force_trim_to = epoch+1
        self.config_set('mon', 'mon_mds_force_trim_to', mon_mds_force_trim_to)

        # force a new fsmap
        self.fs.set_joinable(False)
        sleep(10) # for tick/compaction

        status = self.fs.status()
        log.debug(f"new epoch is {status['epoch']}")
        self.fs.status(epoch=epoch+1) # epoch+1 is not trimmed, may not == status["epoch"]

        try:
            self.fs.status(epoch=epoch)
        except CommandFailedError as e:
            self.assertEqual(e.exitstatus, errno.ENOENT, "invalid error code when trying to fetch FSMap that was trimmed")
        else:
            self.fail("trimming did not occur as expected")


class TestRequiredClientFeatures(CephFSTestCase):
    CLIENTS_REQUIRED = 0
    MDSS_REQUIRED = 1

    def test_required_client_features(self):
        """
        That `ceph fs required_client_features` command functions.
        """

        def is_required(index):
            out = self.get_ceph_cmd_stdout('fs', 'get', self.fs.name, '--format=json-pretty')
            features = json.loads(out)['mdsmap']['required_client_features']
            if "feature_{0}".format(index) in features:
                return True;
            return False;

        features = json.loads(self.get_ceph_cmd_stdout('fs', 'feature', 'ls', '--format=json-pretty'))
        self.assertGreater(len(features), 0);

        for f in features:
            self.fs.required_client_features('rm', str(f['index']))

        for f in features:
            index = f['index']
            feature = f['name']
            if feature == 'reserved':
                feature = str(index)

            if index % 3 == 0:
                continue;
            self.fs.required_client_features('add', feature)
            self.assertTrue(is_required(index))

            if index % 2 == 0:
                continue;
            self.fs.required_client_features('rm', feature)
            self.assertFalse(is_required(index))

    def test_required_client_feature_add_reserved(self):
        """
        That `ceph fs required_client_features X add reserved` fails.
        """

        p = self.fs.required_client_features('add', 'reserved', check_status=False, stderr=StringIO())
        self.assertIn('Invalid feature name', p.stderr.getvalue())

    def test_required_client_feature_rm_reserved(self):
        """
        That `ceph fs required_client_features X rm reserved` fails.
        """

        p = self.fs.required_client_features('rm', 'reserved', check_status=False, stderr=StringIO())
        self.assertIn('Invalid feature name', p.stderr.getvalue())

    def test_required_client_feature_add_reserved_bit(self):
        """
        That `ceph fs required_client_features X add <reserved_bit>` passes.
        """

        p = self.fs.required_client_features('add', '1', stderr=StringIO())
        self.assertIn("added feature 'reserved' to required_client_features", p.stderr.getvalue())

    def test_required_client_feature_rm_reserved_bit(self):
        """
        That `ceph fs required_client_features X rm <reserved_bit>` passes.
        """

        self.fs.required_client_features('add', '1')
        p = self.fs.required_client_features('rm', '1', stderr=StringIO())
        self.assertIn("removed feature 'reserved' from required_client_features", p.stderr.getvalue())

class TestCompatCommands(CephFSTestCase):
    """
    """

    CLIENTS_REQUIRED = 0
    MDSS_REQUIRED = 3

    def test_add_compat(self):
        """
        Test adding a compat.
        """

        self.fs.fail()
        self.fs.add_compat(63, 'placeholder')
        mdsmap = self.fs.get_mds_map()
        self.assertIn("feature_63", mdsmap['compat']['compat'])

    def test_add_incompat(self):
        """
        Test adding an incompat.
        """

        self.fs.fail()
        self.fs.add_incompat(63, 'placeholder')
        mdsmap = self.fs.get_mds_map()
        log.info(f"{mdsmap}")
        self.assertIn("feature_63", mdsmap['compat']['incompat'])

    def test_rm_compat(self):
        """
        Test removing a compat.
        """

        self.fs.fail()
        self.fs.add_compat(63, 'placeholder')
        self.fs.rm_compat(63)
        mdsmap = self.fs.get_mds_map()
        self.assertNotIn("feature_63", mdsmap['compat']['compat'])

    def test_rm_incompat(self):
        """
        Test removing an incompat.
        """

        self.fs.fail()
        self.fs.add_incompat(63, 'placeholder')
        self.fs.rm_incompat(63)
        mdsmap = self.fs.get_mds_map()
        self.assertNotIn("feature_63", mdsmap['compat']['incompat'])

    def test_standby_compat(self):
        """
        That adding a compat does not prevent standbys from joining.
        """

        self.fs.fail()
        self.fs.add_compat(63, "placeholder")
        self.fs.set_joinable()
        self.fs.wait_for_daemons()
        mdsmap = self.fs.get_mds_map()
        self.assertIn("feature_63", mdsmap['compat']['compat'])

    def test_standby_incompat_reject(self):
        """
        That adding an incompat feature prevents incompatible daemons from joining.
        """

        self.fs.fail()
        self.fs.add_incompat(63, "placeholder")
        self.fs.set_joinable()
        try:
            self.fs.wait_for_daemons(timeout=60)
        except RuntimeError as e:
            if "Timed out waiting for MDS daemons to become healthy" in str(e):
                pass
            else:
                raise
        else:
            self.fail()

    def test_standby_incompat_upgrade(self):
        """
        That an MDS can upgrade the compat of a fs.
        """

        self.fs.fail()
        self.fs.rm_incompat(1)
        self.fs.set_joinable()
        self.fs.wait_for_daemons()
        mdsmap = self.fs.get_mds_map()
        self.assertIn("feature_1", mdsmap['compat']['incompat'])

    def test_standby_replay_not_upgradeable(self):
        """
        That the mons will not upgrade the MDSMap compat if standby-replay is
        enabled.
        """

        self.fs.fail()
        self.fs.rm_incompat(1)
        self.fs.set_allow_standby_replay(True)
        self.fs.set_joinable()
        try:
            self.fs.wait_for_daemons(timeout=60)
        except RuntimeError as e:
            if "Timed out waiting for MDS daemons to become healthy" in str(e):
                pass
            else:
                raise
        else:
            self.fail()

    def test_standby_incompat_reject_multifs(self):
        """
        Like test_standby_incompat_reject but with a second fs.
        """

        fs2 = self.mds_cluster.newfs(name="cephfs2", create=True)
        fs2.fail()
        fs2.add_incompat(63, 'placeholder')
        fs2.set_joinable()
        try:
            fs2.wait_for_daemons(timeout=60)
        except RuntimeError as e:
            if "Timed out waiting for MDS daemons to become healthy" in str(e):
                pass
            else:
                raise
        else:
            self.fail()
        # did self.fs lose MDS or standbys suicide?
        self.fs.wait_for_daemons()
        mdsmap = fs2.get_mds_map()
        self.assertIn("feature_63", mdsmap['compat']['incompat'])

class TestConfigCommands(CephFSTestCase):
    """
    Test that daemons and clients respond to the otherwise rarely-used
    runtime config modification operations.
    """

    CLIENTS_REQUIRED = 1
    MDSS_REQUIRED = 1

    def test_ceph_config_show(self):
        """
        That I can successfully show MDS configuration.
        """

        names = self.fs.get_rank_names()
        for n in names:
            s = self.get_ceph_cmd_stdout("config", "show", "mds."+n)
            self.assertTrue("NAME" in s)
            self.assertTrue("mon_host" in s)


    def test_client_config(self):
        """
        That I can successfully issue asok "config set" commands

        :return:
        """

        if not isinstance(self.mount_a, FuseMount):
            self.skipTest("Test only applies to FUSE clients")

        test_key = "client_cache_size"
        test_val = "123"
        self.mount_a.admin_socket(['config', 'set', test_key, test_val])
        out = self.mount_a.admin_socket(['config', 'get', test_key])
        self.assertEqual(out[test_key], test_val)


    def test_mds_config_asok(self):
        test_key = "mds_max_purge_ops"
        test_val = "123"
        self.fs.mds_asok(['config', 'set', test_key, test_val])
        out = self.fs.mds_asok(['config', 'get', test_key])
        self.assertEqual(out[test_key], test_val)

    def test_mds_dump_cache_asok(self):
        cache_file = "cache_file"
        timeout = "1"
        self.fs.rank_asok(['dump', 'cache', cache_file, timeout])

    def test_mds_config_tell(self):
        test_key = "mds_max_purge_ops"
        test_val = "123"

        self.fs.rank_tell(['injectargs', "--{0}={1}".format(test_key, test_val)])

        # Read it back with asok because there is no `tell` equivalent
        out = self.fs.rank_tell(['config', 'get', test_key])
        self.assertEqual(out[test_key], test_val)


class TestMirroringCommands(CephFSTestCase):
    CLIENTS_REQUIRED = 1
    MDSS_REQUIRED = 1

    def _enable_mirroring(self, fs_name):
        self.run_ceph_cmd("fs", "mirror", "enable", fs_name)

    def _disable_mirroring(self, fs_name):
        self.run_ceph_cmd("fs", "mirror", "disable", fs_name)

    def _add_peer(self, fs_name, peer_spec, remote_fs_name):
        peer_uuid = str(uuid.uuid4())
        self.run_ceph_cmd("fs", "mirror", "peer_add", fs_name, peer_uuid, peer_spec, remote_fs_name)

    def _remove_peer(self, fs_name, peer_uuid):
        self.run_ceph_cmd("fs", "mirror", "peer_remove", fs_name, peer_uuid)

    def _verify_mirroring(self, fs_name, flag_str):
        status = self.fs.status()
        fs_map = status.get_fsmap_byname(fs_name)
        if flag_str == 'enabled':
            self.assertTrue('mirror_info' in fs_map)
        elif flag_str == 'disabled':
            self.assertTrue('mirror_info' not in fs_map)
        else:
            raise RuntimeError(f'invalid flag_str {flag_str}')

    def _get_peer_uuid(self, fs_name, peer_spec):
        status = self.fs.status()
        fs_map = status.get_fsmap_byname(fs_name)
        mirror_info = fs_map.get('mirror_info', None)
        self.assertTrue(mirror_info is not None)
        for peer_uuid, remote in mirror_info['peers'].items():
            client_name = remote['remote']['client_name']
            cluster_name = remote['remote']['cluster_name']
            spec = f'{client_name}@{cluster_name}'
            if spec == peer_spec:
                return peer_uuid
        return None

    def test_mirroring_command(self):
        """basic mirroring command test -- enable, disable mirroring on a
        filesystem"""
        self._enable_mirroring(self.fs.name)
        self._verify_mirroring(self.fs.name, "enabled")
        self._disable_mirroring(self.fs.name)
        self._verify_mirroring(self.fs.name, "disabled")

    def test_mirroring_peer_commands(self):
        """test adding and removing peers to a mirror enabled filesystem"""
        self._enable_mirroring(self.fs.name)
        self._add_peer(self.fs.name, "client.site-b@site-b", "fs_b")
        self._add_peer(self.fs.name, "client.site-c@site-c", "fs_c")
        self._verify_mirroring(self.fs.name, "enabled")
        uuid_peer_b = self._get_peer_uuid(self.fs.name, "client.site-b@site-b")
        uuid_peer_c = self._get_peer_uuid(self.fs.name, "client.site-c@site-c")
        self.assertTrue(uuid_peer_b is not None)
        self.assertTrue(uuid_peer_c is not None)
        self._remove_peer(self.fs.name, uuid_peer_b)
        self._remove_peer(self.fs.name, uuid_peer_c)
        self._disable_mirroring(self.fs.name)
        self._verify_mirroring(self.fs.name, "disabled")

    def test_mirroring_command_idempotency(self):
        """test to check idempotency of mirroring family of commands """
        self._enable_mirroring(self.fs.name)
        self._verify_mirroring(self.fs.name, "enabled")
        self._enable_mirroring(self.fs.name)
        # add peer
        self._add_peer(self.fs.name, "client.site-b@site-b", "fs_b")
        uuid_peer_b1 = self._get_peer_uuid(self.fs.name, "client.site-b@site-b")
        self.assertTrue(uuid_peer_b1 is not None)
        # adding the peer again should be idempotent
        self._add_peer(self.fs.name, "client.site-b@site-b", "fs_b")
        uuid_peer_b2 = self._get_peer_uuid(self.fs.name, "client.site-b@site-b")
        self.assertTrue(uuid_peer_b2 is not None)
        self.assertTrue(uuid_peer_b1 == uuid_peer_b2)
        # remove peer
        self._remove_peer(self.fs.name, uuid_peer_b1)
        uuid_peer_b3 = self._get_peer_uuid(self.fs.name, "client.site-b@site-b")
        self.assertTrue(uuid_peer_b3 is None)
        # removing the peer again should be idempotent
        self._remove_peer(self.fs.name, uuid_peer_b1)
        self._disable_mirroring(self.fs.name)
        self._verify_mirroring(self.fs.name, "disabled")
        self._disable_mirroring(self.fs.name)

    def test_mirroring_disable_with_peers(self):
        """test disabling mirroring for a filesystem with active peers"""
        self._enable_mirroring(self.fs.name)
        self._add_peer(self.fs.name, "client.site-b@site-b", "fs_b")
        self._verify_mirroring(self.fs.name, "enabled")
        uuid_peer_b = self._get_peer_uuid(self.fs.name, "client.site-b@site-b")
        self.assertTrue(uuid_peer_b is not None)
        self._disable_mirroring(self.fs.name)
        self._verify_mirroring(self.fs.name, "disabled")
        # enable mirroring to check old peers
        self._enable_mirroring(self.fs.name)
        self._verify_mirroring(self.fs.name, "enabled")
        # peer should be gone
        uuid_peer_b = self._get_peer_uuid(self.fs.name, "client.site-b@site-b")
        self.assertTrue(uuid_peer_b is None)
        self._disable_mirroring(self.fs.name)
        self._verify_mirroring(self.fs.name, "disabled")

    def test_mirroring_with_filesystem_reset(self):
        """test to verify mirroring state post filesystem reset"""
        self._enable_mirroring(self.fs.name)
        self._add_peer(self.fs.name, "client.site-b@site-b", "fs_b")
        self._verify_mirroring(self.fs.name, "enabled")
        uuid_peer_b = self._get_peer_uuid(self.fs.name, "client.site-b@site-b")
        self.assertTrue(uuid_peer_b is not None)
        # reset filesystem
        self.fs.fail()
        self.fs.reset()
        self.fs.wait_for_daemons()
        self._verify_mirroring(self.fs.name, "disabled")


class TestFsAuthorize(CephFSTestCase):
    client_id = 'testuser'
    client_name = 'client.' + client_id
    CLIENTS_REQUIRED = 2
    MDSS_REQUIRED = 3

    def test_single_path_r(self):
        PERM = 'r'
        FS_AUTH_CAPS = (('/', PERM),)
        self.captester = CapTester(self.mount_a, '/')
        keyring = self.fs.authorize(self.client_id, FS_AUTH_CAPS)

        self._remount(keyring)
        self.captester.run_cap_tests(self.fs, self.client_id, PERM)

    def test_single_path_rw(self):
        PERM = 'rw'
        FS_AUTH_CAPS = (('/', PERM),)
        self.captester = CapTester(self.mount_a, '/')
        keyring = self.fs.authorize(self.client_id, FS_AUTH_CAPS)

        self._remount(keyring)
        self.captester.run_cap_tests(self.fs, self.client_id, PERM)

    def test_single_path_rootsquash(self):
        if not isinstance(self.mount_a, FuseMount):
            self.skipTest("only FUSE client has CEPHFS_FEATURE_MDS_AUTH_CAPS "
                          "needed to enforce root_squash MDS caps")

        PERM = 'rw'
        FS_AUTH_CAPS = (('/', PERM, 'root_squash'),)
        self.captester = CapTester(self.mount_a, '/')
        keyring = self.fs.authorize(self.client_id, FS_AUTH_CAPS)

        self._remount(keyring)
        # testing MDS caps...
        # Since root_squash is set in client caps, client can read but not
        # write even thought access level is set to "rw".
        self.captester.conduct_pos_test_for_read_caps()
        self.captester.conduct_pos_test_for_open_caps()
        self.captester.conduct_neg_test_for_write_caps(sudo_write=True)
        self.captester.conduct_neg_test_for_chown_caps()
        self.captester.conduct_neg_test_for_truncate_caps()

    def test_multifs_single_path_rootsquash(self):
        """
        Test root_squash with multi fs
        """
        self.skipTest('this test is broken ATM, see: '
                      'https://tracker.ceph.com/issues/66076.')

        self.fs1 = self.fs
        self.fs2 = self.mds_cluster.newfs('testcephfs2')
        self.mount_b.remount(cephfs_name=self.fs2.name)
        self.captester1 = CapTester(self.mount_a)
        self.captester2 = CapTester(self.mount_b)

        PERM = 'rw'
        FS_AUTH_CAPS = (('/', PERM, 'root_squash'),)

        self.fs1.authorize(self.client_id, FS_AUTH_CAPS)
        self.fs2.authorize(self.client_id, FS_AUTH_CAPS)
        keyring = self.fs.mon_manager.get_keyring(self.client_id)

        keyring_path = self.mount_a.client_remote.mktemp(data=keyring)
        self.mount_a.remount(client_id=self.client_id,
                             client_keyring_path=keyring_path)
        # testing MDS caps...
        # Since root_squash is set in client caps, client can read but not
        # write even though access level is set to "rw" on both fses
        self.captester1.conduct_pos_test_for_read_caps()
        self.captester1.conduct_pos_test_for_open_caps()
        self.captester1.conduct_neg_test_for_write_caps(sudo_write=True)
        self.captester1.conduct_neg_test_for_chown_caps()
        self.captester1.conduct_neg_test_for_truncate_caps()

        keyring_path = self.mount_b.client_remote.mktemp(data=keyring)
        self.mount_b.remount(client_id=self.client_id,
                             client_keyring_path=keyring_path)
        self.captester2.conduct_pos_test_for_read_caps()
        self.captester2.conduct_pos_test_for_open_caps()
        self.captester2.conduct_neg_test_for_write_caps(sudo_write=True)
        self.captester2.conduct_neg_test_for_chown_caps()
        self.captester2.conduct_neg_test_for_truncate_caps()

    def test_multifs_rootsquash_nofeature(self):
        """
        That having root_squash on one fs doesn't prevent access to others.
        """

        if not isinstance(self.mount_a, FuseMount):
            self.skipTest("only FUSE client has CEPHFS_FEATURE_MDS_AUTH_CAPS "
                          "needed to enforce root_squash MDS caps")

        self.fs1 = self.fs
        self.fs2 = self.mds_cluster.newfs('testcephfs2')

        self.mount_a.umount_wait()

        # Authorize client to fs1
        FS_AUTH_CAPS = (('/', 'rw'),)
        self.fs1.authorize(self.client_id, FS_AUTH_CAPS)

        FS_AUTH_CAPS = (('/', 'rw', 'root_squash'),)
        keyring = self.fs2.authorize(self.client_id, FS_AUTH_CAPS)

        CEPHFS_FEATURE_MDS_AUTH_CAPS_CHECK = 21
        # all but CEPHFS_FEATURE_MDS_AUTH_CAPS_CHECK
        features = ",".join([str(i) for i in range(CEPHFS_FEATURE_MDS_AUTH_CAPS_CHECK)])
        mntargs = [f"--client_debug_inject_features={features}"]

        # should succeed
        with self.assert_cluster_log("report clients with broken root_squash", present=False):
            keyring_path = self.mount_a.client_remote.mktemp(data=keyring)
            self.mount_a.remount(client_id=self.client_id, client_keyring_path=keyring_path, mntargs=mntargs, cephfs_name=self.fs1.name)

        captester = CapTester(self.mount_a, '/')
        captester.conduct_pos_test_for_read_caps()
        captester.conduct_pos_test_for_open_caps()

    def test_rootsquash_nofeature(self):
        """
        That having root_squash on an fs without the feature bit raises a HEALTH_ERR warning.
        """

        if not isinstance(self.mount_a, FuseMount):
            self.skipTest("only FUSE client has CEPHFS_FEATURE_MDS_AUTH_CAPS "
                          "needed to enforce root_squash MDS caps")

        self.mount_a.umount_wait()
        self.mount_b.umount_wait()

        FS_AUTH_CAPS = (('/', 'rw', 'root_squash'),)
        keyring = self.fs.authorize(self.client_id, FS_AUTH_CAPS)

        CEPHFS_FEATURE_MDS_AUTH_CAPS_CHECK = 21
        # all but CEPHFS_FEATURE_MDS_AUTH_CAPS_CHECK
        features = ",".join([str(i) for i in range(CEPHFS_FEATURE_MDS_AUTH_CAPS_CHECK)])
        mntargs = [f"--client_debug_inject_features={features}"]

        # should succeed
        with self.assert_cluster_log("with broken root_squash implementation"):
            keyring_path = self.mount_a.client_remote.mktemp(data=keyring)
            self.mount_a.remount(client_id=self.client_id, client_keyring_path=keyring_path, mntargs=mntargs, cephfs_name=self.fs.name)
            self.wait_for_health("MDS_CLIENTS_BROKEN_ROOTSQUASH", 60)
            self.assertFalse(self.mount_a.is_blocked())

        self.mount_a.umount_wait()
        self.wait_for_health_clear(60)

    def test_rootsquash_nofeature_evict(self):
        """
        That having root_squash on an fs without the feature bit can be evicted.
        """

        if not isinstance(self.mount_a, FuseMount):
            self.skipTest("only FUSE client has CEPHFS_FEATURE_MDS_AUTH_CAPS "
                          "needed to enforce root_squash MDS caps")

        self.mount_a.umount_wait()
        self.mount_b.umount_wait()

        FS_AUTH_CAPS = (('/', 'rw', 'root_squash'),)
        keyring = self.fs.authorize(self.client_id, FS_AUTH_CAPS)

        CEPHFS_FEATURE_MDS_AUTH_CAPS_CHECK = 21
        # all but CEPHFS_FEATURE_MDS_AUTH_CAPS_CHECK
        features = ",".join([str(i) for i in range(CEPHFS_FEATURE_MDS_AUTH_CAPS_CHECK)])
        mntargs = [f"--client_debug_inject_features={features}"]

        # should succeed
        keyring_path = self.mount_a.client_remote.mktemp(data=keyring)
        self.mount_a.remount(client_id=self.client_id, client_keyring_path=keyring_path, mntargs=mntargs, cephfs_name=self.fs.name)
        self.wait_for_health("MDS_CLIENTS_BROKEN_ROOTSQUASH", 60)

        self.fs.required_client_features("add", "client_mds_auth_caps")
        self.wait_for_health_clear(60)
        self.assertTrue(self.mount_a.is_blocked())


    def test_single_path_rootsquash_issue_56067(self):
        """
        That a FS client using root squash MDS caps allows non-root user to write data
        to a file. And after client remount, the non-root user can read the data that
        was previously written by it. https://tracker.ceph.com/issues/56067
        """
        if not isinstance(self.mount_a, FuseMount):
            self.skipTest("only FUSE client has CEPHFS_FEATURE_MDS_AUTH_CAPS "
                          "needed to enforce root_squash MDS caps")

        keyring = self.fs.authorize(self.client_id, ('/', 'rw', 'root_squash'))
        keyring_path = self.mount_a.client_remote.mktemp(data=keyring)
        self.mount_a.remount(client_id=self.client_id,
                             client_keyring_path=keyring_path,
                             cephfs_mntpt='/')
        filedata, filename = 'some data on fs 1', 'file_on_fs1'
        filepath = os_path_join(self.mount_a.hostfs_mntpt, filename)
        self.mount_a.write_file(filepath, filedata)

        self.mount_a.remount(client_id=self.client_id,
                             client_keyring_path=keyring_path,
                             cephfs_mntpt='/')
        if filepath.find(self.mount_a.hostfs_mntpt) != -1:
            contents = self.mount_a.read_file(filepath)
            self.assertEqual(filedata, contents)

    def test_single_path_authorize_on_nonalphanumeric_fsname(self):
        """
        That fs authorize command works on filesystems with names having [_.-]
        characters
        """
        self.mount_a.umount_wait(require_clean=True)
        # let's unmount both client before deleting the FS
        self.mount_b.umount_wait(require_clean=True)
        self.mds_cluster.delete_all_filesystems()
        fs_name = "cephfs-_."
        self.fs = self.mds_cluster.newfs(name=fs_name)
        self.fs.wait_for_daemons()
        self.run_ceph_cmd(f'auth caps client.{self.mount_a.client_id} '
                          f'mon "allow r" '
                          f'osd "allow rw pool={self.fs.get_data_pool_name()}" '
                          f'mds allow')
        self.mount_a.remount(cephfs_name=self.fs.name)
        PERM = 'rw'
        FS_AUTH_CAPS = (('/', PERM),)
        self.captester = CapTester(self.mount_a, '/')
        keyring = self.fs.authorize(self.client_id, FS_AUTH_CAPS)

        self._remount(keyring)
        self.captester.run_mds_cap_tests(PERM)

    def test_fs_read_and_single_path_rw(self):
        """
        Tests the file creation using 'touch' cmd on a specific path
        which has 'rw' caps and 'r' caps on the rest of the fs.

        The mds auth caps with 'rw' caps on a specific path and 'r' caps
        on the rest of the fs has an issue. The file creation using 'touch'
        cmd on the fuse client used to fail while doing setattr.
        Please see https://tracker.ceph.com/issues/67212

        The new file creation test using 'touch' cmd is added to
        'MdsCapTester.run_mds_cap_tests' which eventually gets
        called by '_remount_and_run_tests'
        """
        FS_AUTH_CAPS = (('/', 'r'), ('/dir2', 'rw'))
        self.mount_a.run_shell('mkdir -p ./dir2')
        self.captesters = (CapTester(self.mount_a, '/'),
                           CapTester(self.mount_a, '/dir2'))
        keyring = self.fs.authorize(self.client_id, FS_AUTH_CAPS)

        self._remount_and_run_tests(FS_AUTH_CAPS, keyring)

    def test_multiple_path_r(self):
        PERM = 'r'
        FS_AUTH_CAPS = (('/dir1/dir12', PERM), ('/dir2/dir22', PERM))
        for c in FS_AUTH_CAPS:
            self.mount_a.run_shell(f'mkdir -p .{c[0]}')
        self.captesters = (CapTester(self.mount_a, '/dir1/dir12'),
                           CapTester(self.mount_a, '/dir2/dir22'))
        keyring = self.fs.authorize(self.client_id, FS_AUTH_CAPS)

        self._remount_and_run_tests(FS_AUTH_CAPS, keyring)

    def test_multiple_path_rw(self):
        PERM = 'rw'
        FS_AUTH_CAPS = (('/dir1/dir12', PERM), ('/dir2/dir22', PERM))
        for c in FS_AUTH_CAPS:
            self.mount_a.run_shell(f'mkdir -p .{c[0]}')
        self.captesters = (CapTester(self.mount_a, '/dir1/dir12'),
                           CapTester(self.mount_a, '/dir2/dir22'))
        keyring = self.fs.authorize(self.client_id, FS_AUTH_CAPS)

        self._remount_and_run_tests(FS_AUTH_CAPS, keyring)

    def test_when_MDS_caps_needs_update_but_others_dont(self):
        '''
        Test that the command "ceph fs authorize" successfully updates MDS
        caps even when MON and OSD caps don't need an update.

        Tests: https://tracker.ceph.com/issues/64182

        In this test we run -

            ceph fs authorize cephfs1 client.x / rw
            ceph fs authorize cephfs2 client.x / rw
            ceph fs authorize cephfs2 client.x /dir1 rw

        The first command will create the keyring by adding the MDS cap for
        root path & MON and OSD caps with name of the FS name (say "cephfs1").
        Second command will update the all of client's caps -- MON, OSD and
        MDS caps to add caps for 2nd CephFS. The third command doesn't need
        to add MON and OSD caps since cap for "cephfs2" has been granted
        already. Thus, third command only need to update the MDS cap, thus
        testing the bug under consideration here.
        '''
        PERM = 'rw'
        DIR = 'dir1'

        self.fs2 = self.mds_cluster.newfs(name='cephfs2', create=True)
        self.mount_b.remount(cephfs_name=self.fs2.name)
        self.mount_b.run_shell(f'mkdir {DIR}')
        self.captesters = (CapTester(self.mount_a, '/'),
                           CapTester(self.mount_b, '/'),
                           CapTester(self.mount_b, f'/{DIR}'))

        FS_AUTH_CAPS = (('/', PERM), ('/', PERM), (DIR, PERM))
        keyring = self.fs.authorize(self.client_id, FS_AUTH_CAPS[0])
        keyring = self.fs2.authorize(self.client_id, FS_AUTH_CAPS[1])

        # if the following block of code pass it implies that
        # https://tracker.ceph.com/issues/64182 has been fixed
        keyring = self.fs2.authorize(self.client_id, FS_AUTH_CAPS[2])
        mdscaps = ('caps mds = "'
                   f'allow {PERM} fsname={self.fs.name}, '
                   f'allow {PERM} fsname={self.fs2.name}, '
                   f'allow {PERM} fsname={self.fs2.name} path={DIR}')
        self.assertIn(mdscaps, keyring)

        self._remount_and_run_tests_for_cap(
            FS_AUTH_CAPS[0], self.captesters[0], self.fs.name, self.mount_a,
            keyring)
        self._remount_and_run_tests_for_cap(
            FS_AUTH_CAPS[1], self.captesters[1], self.fs2.name, self.mount_b,
            keyring)
        self._remount_and_run_tests_for_cap(
            FS_AUTH_CAPS[2], self.captesters[2], self.fs2.name, self.mount_b,
            keyring)

    def test_adding_multiple_caps(self):
        '''
        Test that the command "ceph fs authorize" is successful in updating
        the entity's caps when multiple caps are passed to it in one go.

        Tests: https://tracker.ceph.com/issues/64127
        Tests: https://tracker.ceph.com/issues/64417
        '''
        DIR = 'dir1'
        self.mount_a.run_shell(f'mkdir {DIR}')
        self.captesters = (CapTester(self.mount_a, '/'),
                           CapTester(self.mount_a, f'/{DIR}'))
        self.fs.authorize(self.client_id, ('/', 'rw'))

        FS_AUTH_CAPS = ('/', 'rw', 'root_squash'), (f'/{DIR}', 'rw' )
        # The fact that following line passes means
        # https://tracker.ceph.com/issues/64127 has been fixed
        keyring = self.fs.authorize(self.client_id, FS_AUTH_CAPS)

        # The fact that following lines passes means
        # https://tracker.ceph.com/issues/64417 has been fixed.
        mdscaps = (f'allow rw fsname={self.fs.name} root_squash, '
                   f'allow rw fsname={self.fs.name} path={DIR}')
        self.assertIn(mdscaps, keyring)

        self._remount_and_run_tests(FS_AUTH_CAPS, keyring)

    def _remount_and_run_tests(self, fs_auth_caps, keyring):
        '''
        This method is specifically designed to meet needs of most of the
        test case in this class. Following are assumptions made here -

        1. CephFS to be mounted is self.fs
        2. Mount object to be used is self.mount_a
        3. Keyring file will be created on the host specified in self.mount_a.
        4. CephFS dir that will serve as root is PATH component of particular
           cap from FS_AUTH_CAPS.
        '''
        for i, c in enumerate(fs_auth_caps):
            self.assertIn(i, (0, 1))
            PATH = c[0]
            PERM = c[1]
            self._remount(keyring, PATH)
            # actual tests...
            self.captesters[i].run_cap_tests(self.fs, self.client_id, PERM,
                                             PATH)

    def _remount(self, keyring, path='/'):
        keyring_path = self.mount_a.client_remote.mktemp(data=keyring)
        self.mount_a.remount(client_id=self.client_id,
                             client_keyring_path=keyring_path,
                             cephfs_mntpt=path)

    def _remount_and_run_tests_for_cap(self, cap, captester, fsname, mount,
                                       keyring):
        PATH = cap[0]
        PERM = cap[1]

        cephfs_mntpt = os_path_join('/', PATH)
        keyring_path = mount.client_remote.mktemp(data=keyring)
        mount.remount(client_id=self.client_id, cephfs_mntpt=cephfs_mntpt,
                      cephfs_name=fsname, client_keyring_path=keyring_path)

        captester.run_cap_tests(self.fs, self.client_id, PERM, PATH)

    def tearDown(self):
        self.mount_a.umount_wait()
        self.run_ceph_cmd(f'auth rm {self.client_name}')

        super(type(self), self).tearDown()


class TestFsAuthorizeUpdate(CephFSTestCase):
    client_id = 'testuser'
    client_name = f'client.{client_id}'
    CLIENTS_REQUIRED = 2
    MDSS_REQUIRED = 3

    ######################################
    # cases where "fs authorize" adds caps
    ######################################

    def test_add_caps_for_another_FS(self):
        """
        Test that "ceph fs authorize" adds caps for a second FS to a keyring
        that already had caps for first FS.
        """
        self.fs1 = self.fs
        self.fs2 = self.mds_cluster.newfs('testcephfs2')
        self.mount_b.remount(cephfs_name=self.fs2.name)
        self.captesters = (CapTester(self.mount_a), CapTester(self.mount_b))
        self.fs1.authorize(self.client_id, ('/', 'rw'))

        ########### testing begins here.
        TEST_PERM = 'rw'
        self.fs2.authorize(self.client_id, ('/', TEST_PERM,))
        keyring = self.fs.mon_manager.get_keyring(self.client_id)
        moncap = gen_mon_cap_str((('r', self.fs1.name),
                                  ('r', self.fs2.name)))
        osdcap = gen_osd_cap_str(((TEST_PERM, self.fs1.name),
                                  (TEST_PERM, self.fs2.name)))
        mdscap = gen_mds_cap_str(((TEST_PERM, self.fs1.name),
                                  (TEST_PERM, self.fs2.name)))
        for cap in (moncap, osdcap, mdscap):
            self.assertIn(cap, keyring)
        self._remount(self.mount_a, self.fs1.name, keyring)
        self._remount(self.mount_b, self.fs2.name, keyring)
        self.captesters[0].run_cap_tests(self.fs1, self.client_id, TEST_PERM)
        self.captesters[0].run_cap_tests(self.fs1, self.client_id, TEST_PERM)

    def test_add_caps_for_same_FS_diff_path(self):
        '''
        Test that "ceph fs authorze" grants a new cap when it is run for a
        second time for same client and same FS but with a differnt paths.

        In other words, running following two commands -
        $ ceph fs authorize a client.x1 /dir1 rw
        $ ceph fs authorize a client.x1 /dir2 rw

        Should produce following MDS caps -
        caps mon = "allow r fsname=a"
        caps osd = "allow rw tag cephfs data=a"
        caps mds = "allow rw fsname=a path=dir1, allow rw fsname=a path=dir2"
        '''
        PERM, PATH1, PATH2 = 'rw', 'dir1', 'dir2'
        self.mount_a.run_shell('mkdir dir1 dir2')
        self.captester_a = CapTester(self.mount_a, PATH1)
        self.captester_b = CapTester(self.mount_b, PATH2)
        self.fs.authorize(self.client_id, ('/', PERM))

        ########## testing begins here.
        self.fs.authorize(self.client_id, (PATH1, PERM))
        keyring = self.fs.mon_manager.get_keyring(self.client_id)
        moncap = gen_mon_cap_str((('r', self.fs.name),))
        osdcap = gen_osd_cap_str(((PERM, self.fs.name),))
        mdscap = gen_mds_cap_str(((PERM, self.fs.name, PATH1),))
        for cap in (moncap, osdcap, mdscap):
            self.assertIn(cap, keyring)
        self._remount(self.mount_a, self.fs.name, keyring, PATH1)
        self.captester_a.run_cap_tests(self.fs, self.client_id, PERM, PATH1)

        ########### testing once more
        self.fs.authorize(self.client_id, (PATH2, PERM))
        keyring = self.fs.mon_manager.get_keyring(self.client_id)
        moncap = gen_mon_cap_str((('r', self.fs.name),))
        osdcap = gen_osd_cap_str(((PERM, self.fs.name),))
        mdscap = gen_mds_cap_str(((PERM, self.fs.name, PATH1),
                                  (PERM, self.fs.name, PATH2)))
        for cap in (moncap, osdcap, mdscap):
            self.assertIn(cap, keyring)
        self._remount(self.mount_b, self.fs.name, keyring, PATH2)
        self.captester_b.run_cap_tests(self.fs, self.client_id, PERM, PATH2)

    def test_add_caps_for_client_with_no_caps(self):
        """
        Test that "ceph fs authorize" adds caps to the keyring when the
        entity already has a keyring but it contains no caps.
        """
        TEST_PERM = 'rw'
        self.captester = CapTester(self.mount_a)
        self.run_ceph_cmd(f'auth add {self.client_name}')

        ######## testing begins here.
        self.fs.authorize(self.client_id, ('/', TEST_PERM))
        keyring = self.fs.mon_manager.get_keyring(self.client_id)
        moncap = gen_mon_cap_str((('r', self.fs.name,),))
        osdcap = gen_osd_cap_str(((TEST_PERM, self.fs.name),))
        mdscap = gen_mds_cap_str(((TEST_PERM, self.fs.name),))
        for cap in (moncap, osdcap, mdscap):
            self.assertIn(cap, keyring)
        self._remount(self.mount_a, self.fs.name, keyring)
        self.captester.run_cap_tests(self.fs, self.client_id, TEST_PERM)


    #########################################
    # cases where "fs authorize" changes caps
    #########################################

    def test_change_perms(self):
        """
        Test that "ceph fs authorize" updates the caps for a FS when the caps
        for that FS were already present in that keyring.
        """
        OLD_PERM = 'rw'
        NEW_PERM = 'r'
        self.captester = CapTester(self.mount_a)

        self.fs.authorize(self.client_id, ('/', OLD_PERM))
        ########### testing begins here
        self.fs.authorize(self.client_id, ('/', NEW_PERM))
        keyring = self.fs.mon_manager.get_keyring(self.client_id)
        moncap = gen_mon_cap_str((('r', self.fs.name,),))
        osdcap = gen_osd_cap_str(((NEW_PERM, self.fs.name),))
        mdscap = gen_mds_cap_str(((NEW_PERM, self.fs.name),))
        for cap in (moncap, osdcap, mdscap):
            self.assertIn(cap, keyring)
        self._remount(self.mount_a, self.fs.name, keyring)
        self.captester.run_cap_tests(self.fs, self.client_id, NEW_PERM)

    ################################################
    # Cases where fs authorize maintains idempotency
    ################################################

    def test_idem_caps_passed_same_as_current_caps(self):
        """
        Test that "ceph fs authorize" exits with the keyring on stdout and the
        expected error message on stderr when caps supplied to the subcommand
        are already present in the entity's keyring.
        """
        PERM = 'rw'
        self.captester = CapTester(self.mount_a)
        self.fs.authorize(self.client_id, ('/', PERM))
        keyring1 = self.fs.mon_manager.get_keyring(self.client_id)

        ############# testing begins here.
        proc = self.fs.mon_manager.run_cluster_cmd(
            args=f'fs authorize {self.fs.name} {self.client_name} / {PERM}',
            stdout=StringIO(), stderr=StringIO())
        errmsg = proc.stderr.getvalue()
        self.assertIn(f'no update for caps of {self.client_name}', errmsg)

        keyring2 = self.fs.mon_manager.get_keyring(self.client_id)
        self.assertIn(keyring1, keyring2)

        self._remount(self.mount_a, self.fs.name, keyring2)
        self.captester.run_cap_tests(self.fs, self.client_id, PERM)

    def test_idem_unaffected_root_squash(self):
        """
        Test that "root_squash" is not deleted from MDS caps when user runs
        "fs authorize" a second time passing same FS name and path but not
        with "root_squash"
        In other words,
        $ ceph fs authorize a client.x / rw root_squash
        [client.x]
        key = AQCvsyVkiDJZBBAAn1ClsPKvTfrCkUs01Eh8og==
        $ ceph auth get client.x
        [client.x]
                key = AQD61CVkcA1QCRAAd0XYqPbHvcc+lpUAuc6Vcw==
                caps mds = "allow rw fsname=a root_squash"
                caps mon = "allow r fsname=a"
                caps osd = "allow rw tag cephfs data=a"
        $ ceph fs authorize a client.x / rw
        $ ceph auth get client.x
        [client.x]
                key = AQD61CVkcA1QCRAAd0XYqPbHvcc+lpUAuc6Vcw==
                caps mds = "allow rw fsname=a root_squash"
                caps mon = "allow r fsname=a"
                caps osd = "allow rw tag cephfs data=a"
        """
        PERM, PATH = 'rw', 'dir1'
        self.mount_a.run_shell(f'mkdir {PATH}')
        self.captester = CapTester(self.mount_a, PATH)
        self.fs.authorize(self.client_id, (PATH, PERM, 'root_squash'))

        ############# testing begins here.
        self.fs.authorize(self.client_id, (PATH, PERM))
        keyring = self.fs.mon_manager.get_keyring(self.client_id)
        moncap = gen_mon_cap_str((('r', self.fs.name,),))
        osdcap = gen_osd_cap_str(((PERM, self.fs.name),))
        mdscap = gen_mds_cap_str(((PERM, self.fs.name, PATH, True),))
        for cap in (moncap, osdcap, mdscap):
            self.assertIn(cap, keyring)

    def _get_uid(self):
        return self.mount_a.client_remote.run(
            args='id -u', stdout=StringIO()).stdout.getvalue().strip()

    def _get_gid(self):
        return self.mount_a.client_remote.run(
            args='id -g', stdout=StringIO()).stdout.getvalue().strip()

    def test_idem_unaffected_uid(self):
        '''
        1. Create a client with caps that has FS name and UID in it.
        2. Run "ceph fs authorize" command for that client with same FS name.
        3. Test that UID (as well as any other part of cap) is unaffected,
           i.e. same as before.
        '''
        PERM, UID = 'rw', self._get_uid()
        self.captester = CapTester(self.mount_a)
        moncap = gen_mon_cap_str((('r', self.fs.name,),))
        osdcap = gen_osd_cap_str(((PERM, self.fs.name),))
        mdscap = f'allow rw uid={UID}'
        self.fs.mon_manager.run_cluster_cmd(
            args=(f'auth add {self.client_name} mon "{moncap}" '
                  f'osd "{osdcap}" mds "{mdscap}"'))

        ############# testing begins here.
        self.fs.authorize(self.client_id, ('/', PERM))
        keyring = self.fs.mon_manager.get_keyring(self.client_id)
        for cap in (moncap, osdcap, mdscap):
            self.assertIn(cap, keyring)
        self._remount(self.mount_a, self.fs.name, keyring)
        self.captester.run_cap_tests(self.fs, self.client_id, PERM)

    def test_idem_unaffected_gids(self):
        '''
        1. Create a client with caps that has FS name, UID and GID in it.
        2. Run "ceph fs authorize" command for that client with same FS name.
        3. Test that GID (as well as any other part of cap) is unaffected,
           i.e. same as before.
        '''
        PERM, UID, GID = 'rw', self._get_uid(), self._get_gid()
        self.captester = CapTester(self.mount_a)
        moncap = gen_mon_cap_str((('r', self.fs.name),))
        osdcap = gen_osd_cap_str(((PERM, self.fs.name),))
        mdscap = f'allow rw uid={UID} gids={GID}'
        self.fs.mon_manager.run_cluster_cmd(
            args=(f'auth add {self.client_name} mon "{moncap}" '
                  f'osd "{osdcap}" mds "{mdscap}"'))

        ############# testing begins here.
        self.fs.authorize(self.client_id, ('/', PERM))
        keyring = self.fs.mon_manager.get_keyring(self.client_id)
        for cap in (moncap, osdcap, mdscap):
            self.assertIn(cap, keyring)
        self._remount(self.mount_a, self.fs.name, keyring)
        self.captester.run_cap_tests(self.fs, self.client_id, PERM)

    def test_idem_unaffected_gids_multiple(self):
        '''
        1. Create client with caps with FS name, UID & multiple GIDs in it.
        2. Run "ceph fs authorize" command for that client with same FS name.
        3. Test that multiple GIDs (as well as any other part of cap) is
           unaffected, i.e. same as before.
        '''
        PERM, UID = 'rw', self._get_uid()
        gids = [int(self._get_gid()), 1001, 1002]
        # Apparently code for MDS caps always arranges GID in ascending
        # order. Let's sort so that latter cap string matches with keyring.
        gids.sort()
        gids = f'{gids[0]},{gids[1]},{gids[2]}'
        self.captester = CapTester(self.mount_a)
        moncap = gen_mon_cap_str((('r', self.fs.name),))
        osdcap = gen_osd_cap_str(((PERM, self.fs.name),))
        mdscap = f'allow rw uid={UID} gids={gids}'
        self.fs.mon_manager.run_cluster_cmd(
            args=(f'auth add {self.client_name} mon "{moncap}" '
                  f'osd "{osdcap}" mds "{mdscap}"'))

        ############# testing begins here.
        self.fs.authorize(self.client_id, ('/', PERM))
        keyring = self.fs.mon_manager.get_keyring(self.client_id)
        for cap in (moncap, osdcap, mdscap):
            self.assertIn(cap, keyring)
        self._remount(self.mount_a, self.fs.name, keyring)
        self.captester.run_cap_tests(self.fs, self.client_id, PERM)

    def _remount(self, mount_x, fsname, keyring, cephfs_mntpt='/'):
        if len(cephfs_mntpt) > 1 and cephfs_mntpt[0] != '/':
            cephfs_mntpt = '/' + cephfs_mntpt
        keyring_path = mount_x.client_remote.mktemp(data=keyring)
        mount_x.remount(client_id=self.client_id,
                        client_keyring_path=keyring_path,
                        cephfs_mntpt=cephfs_mntpt, cephfs_name=fsname)


class TestAdminCommandIdempotency(CephFSTestCase):
    """
    Tests for administration command idempotency.
    """

    CLIENTS_REQUIRED = 0
    MDSS_REQUIRED = 1

    def test_rm_idempotency(self):
        """
        That a removing a fs twice is idempotent.
        """

        data_pools = self.fs.get_data_pool_names(refresh=True)
        self.fs.fail()
        self.fs.rm()
        try:
            self.fs.get_mds_map()
        except FSMissing:
            pass
        else:
            self.fail("get_mds_map should raise")
        p = self.fs.rm()
        self.assertIn("does not exist", p.stderr.getvalue())
        self.fs.remove_pools(data_pools)


class TestAdminCommandDumpTree(CephFSTestCase):
    """
    Tests for administration command subtrees.
    """

    CLIENTS_REQUIRED = 0
    MDSS_REQUIRED = 1

    def test_dump_subtrees(self):
        """
        Dump all the subtrees to make sure the MDS daemon won't crash.
        """

        subtrees = self.fs.mds_asok(['get', 'subtrees'])
        log.info(f"dumping {len(subtrees)} subtrees:")
        for subtree in subtrees:
            log.info(f"  subtree: '{subtree['dir']['path']}'")
            self.fs.mds_asok(['dump', 'tree', subtree['dir']['path']])

        log.info("dumping 2 special subtrees:")
        log.info("  subtree: '/'")
        self.fs.mds_asok(['dump', 'tree', '/'])
        log.info("  subtree: '~mdsdir'")
        self.fs.mds_asok(['dump', 'tree', '~mdsdir'])

class TestAdminCommandDumpLoads(CephFSTestCase):
    """
    Tests for administration command dump loads.
    """

    CLIENTS_REQUIRED = 0
    MDSS_REQUIRED = 1

    def test_dump_loads(self):
        """
        make sure depth limit param is considered when dump loads for a MDS daemon.
        """

        log.info("dumping loads")
        loads = self.fs.mds_asok(['dump', 'loads', '1'])
        self.assertIsNotNone(loads)
        self.assertIn("dirfrags", loads)
        for d in loads["dirfrags"]:
            self.assertLessEqual(d["path"].count("/"), 1)

class TestFsBalRankMask(CephFSTestCase):
    """
    Tests ceph fs set <fs_name> bal_rank_mask
    """

    CLIENTS_REQUIRED = 0
    MDSS_REQUIRED = 2

    def test_bal_rank_mask(self):
        """
        check whether a specified bal_rank_mask value is valid or not.
        """
        bal_rank_mask = '0x0'
        log.info(f"set bal_rank_mask {bal_rank_mask}")
        self.fs.set_bal_rank_mask(bal_rank_mask)
        self.assertEqual(bal_rank_mask, self.fs.get_var('bal_rank_mask'))

        bal_rank_mask = '0'
        log.info(f"set bal_rank_mask {bal_rank_mask}")
        self.fs.set_bal_rank_mask(bal_rank_mask)
        self.assertEqual(bal_rank_mask, self.fs.get_var('bal_rank_mask'))

        bal_rank_mask = '-1'
        log.info(f"set bal_rank_mask {bal_rank_mask}")
        self.fs.set_bal_rank_mask(bal_rank_mask)
        self.assertEqual(bal_rank_mask, self.fs.get_var('bal_rank_mask'))

        bal_rank_mask = 'all'
        log.info(f"set bal_rank_mask {bal_rank_mask}")
        self.fs.set_bal_rank_mask(bal_rank_mask)
        self.assertEqual(bal_rank_mask, self.fs.get_var('bal_rank_mask'))

        bal_rank_mask = '0x1'
        log.info(f"set bal_rank_mask {bal_rank_mask}")
        self.fs.set_bal_rank_mask(bal_rank_mask)
        self.assertEqual(bal_rank_mask, self.fs.get_var('bal_rank_mask'))

        bal_rank_mask = '1'
        log.info(f"set bal_rank_mask {bal_rank_mask}")
        self.fs.set_bal_rank_mask(bal_rank_mask)
        self.assertEqual(bal_rank_mask, self.fs.get_var('bal_rank_mask'))

        bal_rank_mask = 'f0'
        log.info(f"set bal_rank_mask {bal_rank_mask}")
        self.fs.set_bal_rank_mask(bal_rank_mask)
        self.assertEqual(bal_rank_mask, self.fs.get_var('bal_rank_mask'))

        bal_rank_mask = 'ab'
        log.info(f"set bal_rank_mask {bal_rank_mask}")
        self.fs.set_bal_rank_mask(bal_rank_mask)
        self.assertEqual(bal_rank_mask, self.fs.get_var('bal_rank_mask'))

        bal_rank_mask = '0xfff0'
        log.info(f"set bal_rank_mask {bal_rank_mask}")
        self.fs.set_bal_rank_mask(bal_rank_mask)
        self.assertEqual(bal_rank_mask, self.fs.get_var('bal_rank_mask'))

        MAX_MDS = 256
        bal_rank_mask = '0x' + 'f' * int(MAX_MDS / 4)
        log.info(f"set bal_rank_mask {bal_rank_mask}")
        self.fs.set_bal_rank_mask(bal_rank_mask)
        self.assertEqual(bal_rank_mask, self.fs.get_var('bal_rank_mask'))

        bal_rank_mask = ''
        log.info("set bal_rank_mask to empty string")
        try:
            self.fs.set_bal_rank_mask(bal_rank_mask)
        except CommandFailedError as e:
            self.assertEqual(e.exitstatus, errno.EINVAL)

        bal_rank_mask = '0x1' + 'f' * int(MAX_MDS / 4)
        log.info(f"set bal_rank_mask {bal_rank_mask}")
        try:
            self.fs.set_bal_rank_mask(bal_rank_mask)
        except CommandFailedError as e:
            self.assertEqual(e.exitstatus, errno.EINVAL)


class TestPermErrMsg(CephFSTestCase):

    CLIENT_NAME = 'client.testuser'
    FS1_NAME, FS2_NAME, FS3_NAME = 'abcd', 'efgh', 'ijkl'

    EXPECTED_ERRNO = 22
    EXPECTED_ERRMSG = ("Permission flags in MDS capability string must be '*' "
                       "or 'all' or must start with 'r'")

    MONCAP = f'allow r fsname={FS1_NAME}'
    OSDCAP = f'allow rw tag cephfs data={FS1_NAME}'
    MDSCAPS = [
        'allow w',
        f'allow w fsname={FS1_NAME}',

        f'allow rw fsname={FS1_NAME}, allow w fsname={FS2_NAME}',
        f'allow w fsname={FS1_NAME}, allow rw fsname={FS2_NAME}',
        f'allow w fsname={FS1_NAME}, allow w fsname={FS2_NAME}',

        (f'allow rw fsname={FS1_NAME}, allow rw fsname={FS2_NAME}, allow '
         f'w fsname={FS3_NAME}'),

        # without space after comma
        f'allow rw fsname={FS1_NAME},allow w fsname={FS2_NAME}',


        'allow wr',
        f'allow wr fsname={FS1_NAME}',

        f'allow rw fsname={FS1_NAME}, allow wr fsname={FS2_NAME}',
        f'allow wr fsname={FS1_NAME}, allow rw fsname={FS2_NAME}',
        f'allow wr fsname={FS1_NAME}, allow wr fsname={FS2_NAME}',

        (f'allow rw fsname={FS1_NAME}, allow rw fsname={FS2_NAME}, allow '
         f'wr fsname={FS3_NAME}'),

        # without space after comma
        f'allow rw fsname={FS1_NAME},allow wr fsname={FS2_NAME}']

    def _negtestcmd(self, SUBCMD, MDSCAP):
        return self.negtest_ceph_cmd(
            args=(f'{SUBCMD} {self.CLIENT_NAME} '
                  f'mon "{self.MONCAP}" osd "{self.OSDCAP}" mds "{MDSCAP}"'),
            retval=self.EXPECTED_ERRNO, errmsgs=self.EXPECTED_ERRMSG)

    def test_auth_add(self):
        for mdscap in self.MDSCAPS:
            self._negtestcmd('auth add', mdscap)

    def test_auth_caps(self):
        for mdscap in self.MDSCAPS:
            self.fs.mon_manager.run_cluster_cmd(
                args=f'auth add {self.CLIENT_NAME}')

            self._negtestcmd('auth caps', mdscap)

            self.fs.mon_manager.run_cluster_cmd(
                args=f'auth rm {self.CLIENT_NAME}')

    def test_auth_get_or_create(self):
        for mdscap in self.MDSCAPS:
            self._negtestcmd('auth get-or-create', mdscap)

    def test_auth_get_or_create_key(self):
        for mdscap in self.MDSCAPS:
            self._negtestcmd('auth get-or-create-key', mdscap)

    def test_fs_authorize(self):
        for wrong_perm in ('w', 'wr'):
            self.negtest_ceph_cmd(
                args=(f'fs authorize {self.fs.name} {self.CLIENT_NAME} / '
                      f'{wrong_perm}'), retval=self.EXPECTED_ERRNO,
                errmsgs=self.EXPECTED_ERRMSG)


class TestFSFail(TestAdminCommands):

    MDSS_REQUIRED = 2
    CLIENTS_REQUIRED = 1

    def test_with_health_warn_cache_oversized(self):
        '''
        Test that, when health warning MDS_CACHE_OVERSIZE is present for an
        MDS, command "ceph fs fail" fails without confirmation flag and passes
        when confirmation flag is passed.
        '''
        health_warn = 'MDS_CACHE_OVERSIZED'
        self.gen_health_warn_mds_cache_oversized()

        # actual testing begins now.
        self.negtest_ceph_cmd(args=f'fs fail {self.fs.name}',
                              retval=1, errmsgs=health_warn)
        self.run_ceph_cmd(f'fs fail {self.fs.name} --yes-i-really-mean-it')

        # Bring and wait for MDS to be up since it is needed for unmounting
        # of CephFS in CephFSTestCase.tearDown() to be successful.
        self.fs.set_joinable()
        self.fs.wait_for_daemons()

    def test_with_health_warn_trim(self):
        '''
        Test that, when health warning MDS_TRIM is present for an MDS, command
        "ceph fs fail" fails without confirmation flag and passes when
        confirmation flag is passed.
        '''
        health_warn = 'MDS_TRIM'
        self.gen_health_warn_mds_trim()

        # actual testing begins now.
        self.negtest_ceph_cmd(args=f'fs fail {self.fs.name}',
                              retval=1, errmsgs=health_warn)
        self.run_ceph_cmd(f'fs fail {self.fs.name} --yes-i-really-mean-it')

        # Bring and wait for MDS to be up since it is needed for unmounting
        # of CephFS in CephFSTestCase.tearDown() to be successful.
        self.fs.set_joinable()
        self.fs.wait_for_daemons()

    def test_with_health_warn_with_2_active_MDSs(self):
        '''
        Test that, when a CephFS has 2 active MDSs and one of them have either
        health warning MDS_TRIM or MDS_CACHE_OVERSIZE, running "ceph fs fail"
        fails without confirmation flag and passes when confirmation flag is
        passed.
        '''
        health_warn = 'MDS_CACHE_OVERSIZED'
        self.fs.set_max_mds(2)
        self.gen_health_warn_mds_cache_oversized()

        # actual testing begins now.
        self.negtest_ceph_cmd(args=f'fs fail {self.fs.name}',
                              retval=1, errmsgs=health_warn)
        self.run_ceph_cmd(f'fs fail {self.fs.name} --yes-i-really-mean-it')

        # Bring and wait for MDS to be up since it is needed for unmounting
        # of CephFS in CephFSTestCase.tearDown() to be successful.
        self.fs.set_joinable()
        self.fs.wait_for_daemons()


class TestMDSFail(TestAdminCommands):

    MDSS_REQUIRED = 2
    CLIENTS_REQUIRED = 1

    def test_with_health_warn_cache_oversized(self):
        '''
        Test that, when health warning MDS_CACHE_OVERSIZE is present for an
        MDS, command "ceph mds fail" fails without confirmation flag and
        passes when confirmation flag is passed.
        '''
        health_warn = 'MDS_CACHE_OVERSIZED'
        self.gen_health_warn_mds_cache_oversized()

        # actual testing begins now.
        active_mds_id = self.fs.get_active_names()[0]
        self.negtest_ceph_cmd(args=f'mds fail {active_mds_id}',
                              retval=1, errmsgs=health_warn)
        self.run_ceph_cmd(f'mds fail {active_mds_id} --yes-i-really-mean-it')

    def test_with_health_warn_trim(self):
        '''
        Test that, when health warning MDS_TRIM is present for an MDS, command
        "ceph mds fail" fails without confirmation flag and passes when
        confirmation is passed.
        '''
        health_warn = 'MDS_TRIM'
        self.gen_health_warn_mds_trim()

        # actual testing begins now...
        active_mds_id = self.fs.get_active_names()[0]
        self.negtest_ceph_cmd(args=f'mds fail {active_mds_id}',
                              retval=1, errmsgs=health_warn)
        self.run_ceph_cmd(f'mds fail {active_mds_id} --yes-i-really-mean-it')

    def _get_unhealthy_mds_id(self, health_warn):
        '''
        Return MDS ID for which health warning in "health_warn" has been
        generated.
        '''
        health_report = json.loads(self.get_ceph_cmd_stdout('health detail '
                                                            '--format json'))
        # variable "msg" should hold string something like this -
        # 'mds.b(mds.0): Behind on trimming (865/10) max_segments: 10,
        # num_segments: 86
        msg = health_report['checks'][health_warn]['detail'][0]['message']
        mds_id = msg.split('(')[0]
        mds_id = mds_id.replace('mds.', '')
        return mds_id

    def test_with_health_warn_with_2_active_MDSs(self):
        '''
        Test when a CephFS has 2 active MDSs and one of them have either
        health warning MDS_TRIM or MDS_CACHE_OVERSIZE, running "ceph mds fail"
        fails for both MDSs without confirmation flag and passes for both when
        confirmation flag is passed.
        '''
        health_warn = 'MDS_CACHE_OVERSIZED'
        self.fs.set_max_mds(2)
        self.gen_health_warn_mds_cache_oversized()
        mds1_id, mds2_id = self.fs.get_active_names()

        # MDS ID for which health warning has been generated.
        hw_mds_id = self._get_unhealthy_mds_id(health_warn)
        if mds1_id == hw_mds_id:
            non_hw_mds_id = mds2_id
        elif mds2_id == hw_mds_id:
            non_hw_mds_id = mds1_id
        else:
            raise RuntimeError('There are only 2 MDSs right now but apparently'
                               'health warning was raised for an MDS other '
                               'than these two. This is definitely an error.')

        # actual testing begins now...
        self.negtest_ceph_cmd(args=f'mds fail {non_hw_mds_id}', retval=1,
                              errmsgs=health_warn)
        self.negtest_ceph_cmd(args=f'mds fail {hw_mds_id}', retval=1,
                              errmsgs=health_warn)
        self.run_ceph_cmd(f'mds fail {mds1_id} --yes-i-really-mean-it')
        self.run_ceph_cmd(f'mds fail {mds2_id} --yes-i-really-mean-it')


class TestFSSetMaxMDS(TestAdminCommands):

    def test_when_unhealthy_without_confirm(self):
        '''
        Test that command "ceph fs set <fsname> max_mds <num>" without the
        confirmation flag (--yes-i-really-mean-it) fails when cluster is
        unhealthy.
        '''
        self.gen_health_warn_mds_cache_oversized()

        with self.assertRaises(CommandFailedError) as cfe:
            self.fs.set_max_mds(2, confirm=False)
        self.assertEqual(cfe.exception.exitstatus, errno.EPERM)

    def test_when_unhealthy_with_confirm(self):
        '''
        Test that command "ceph fs set <fsname> max_mds <num>
        --yes-i-really-mean-it" runs successfully when cluster is unhealthy.
        '''
        self.gen_health_warn_mds_cache_oversized()

        self.fs.set_max_mds(2, confirm=True)
        self.assertEqual(self.fs.get_var('max_mds'), 2)

    def test_when_mds_trim_without_confirm(self):
        '''
        Test that command "ceph fs set <fsname> max_mds <num>" without the
        confirmation flag (--yes-i-really-mean-it) fails when cluster has
        MDS_TRIM health warning.
        '''
        self.gen_health_warn_mds_trim()

        with self.assertRaises(CommandFailedError) as cfe:
            self.fs.set_max_mds(2, confirm=False)
        self.assertEqual(cfe.exception.exitstatus, errno.EPERM)

    def test_when_mds_trim_when_with_confirm(self):
        '''
        Test that command "ceph fs set <fsname> max_mds <num>
        --yes-i-really-mean-it" runs successfully when cluster has MDS_TRIM
        health warning.
        '''
        self.gen_health_warn_mds_trim()

        self.fs.set_max_mds(2, confirm=True)
        self.assertEqual(self.fs.get_var('max_mds'), 2)

    def test_when_healthy_with_confirm(self):
        '''
        Test that command "ceph fs set <fsname> max_mds <num>
        --yes-i-really-mean-it" runs successfully also when cluster is
        healthy.
        '''
        self.fs.set_max_mds(2, confirm=True)
        self.assertEqual(self.fs.get_var('max_mds'), 2)


class TestToggleVolumes(CephFSTestCase):
    '''
    Contains code for enabling/disabling mgr/volumes plugin.
    '''

    VOL_MOD_NAME = 'volumes'
    CONFIRM = '--yes-i-really-mean-it'

    def tearDown(self):
        '''
        Ensure that the volumes plugin is enabled after the test has finished
        running since not doing so might affect tearDown() of CephFSTestCase or
        other superclasses.
        '''
        json_output = self.get_ceph_cmd_stdout('mgr module ls --format json')
        json_output = json.loads(json_output)

        if 'volumes' in json_output['force_disabled_modules']:
            self.run_ceph_cmd(f'mgr module enable {self.VOL_MOD_NAME}')

        super(TestToggleVolumes, self).tearDown()

    def test_force_disable_with_confirmation(self):
        '''
        Test that running "ceph mgr module force disable volumes
        --yes-i-really-mean-it" successfully disables volumes plugin.

        Also test "ceph mgr module ls" output after this.
        '''
        self.run_ceph_cmd(f'mgr module force disable {self.VOL_MOD_NAME} '
                          f'{self.CONFIRM}')

        json_output = self.get_ceph_cmd_stdout('mgr module ls --format json')
        json_output = json.loads(json_output)

        self.assertIn(self.VOL_MOD_NAME, json_output['always_on_modules'])
        self.assertIn(self.VOL_MOD_NAME, json_output['force_disabled_modules'])

        self.assertNotIn(self.VOL_MOD_NAME, json_output['enabled_modules'])
        self.assertNotIn(self.VOL_MOD_NAME, json_output['disabled_modules'])

    def test_force_disable_fails_without_confirmation(self):
        '''
        Test that running "ceph mgr module force disable volumes" fails with
        EPERM when confirmation flag is not passed along.

        Also test that output of this command suggests user to pass
        --yes-i-really-mean-it.
        '''
        proc = self.run_ceph_cmd(
            f'mgr module force disable {self.VOL_MOD_NAME}',
            stderr=StringIO(), check_status=False)

        self.assertEqual(proc.returncode, errno.EPERM)

        proc_stderr = proc.stderr.getvalue()
        self.assertIn('EPERM', proc_stderr)
        # ensure that the confirmation flag was recommended
        self.assertIn(self.CONFIRM, proc_stderr)

    def test_force_disable_idempotency(self):
        '''
        Test that running "ceph mgr module force disable volumes" passes when
        volumes plugin was already force disabled.
        '''
        self.run_ceph_cmd(f'mgr module force disable {self.VOL_MOD_NAME} '
                          f'{self.CONFIRM}')
        sleep(5)

        json_output = self.get_ceph_cmd_stdout('mgr module ls --format '
                                              'json-pretty')
        json_output = json.loads(json_output)

        self.assertIn(self.VOL_MOD_NAME, json_output['always_on_modules'])
        self.assertIn(self.VOL_MOD_NAME, json_output['force_disabled_modules'])

        self.assertNotIn(self.VOL_MOD_NAME, json_output['enabled_modules'])
        self.assertNotIn(self.VOL_MOD_NAME, json_output['disabled_modules'])

        # XXX: this this test, running this command 2nd time should pass.
        self.run_ceph_cmd(f'mgr module force disable {self.VOL_MOD_NAME}')

    def test_force_disable_nonexistent_mod(self):
        '''
        Test that passing non-existent name to "ceph mgr module force disable"
        command leads to an error.
        '''
        proc = self.run_ceph_cmd(
            f'mgr module force disable abcd {self.CONFIRM}',
            check_status=False, stderr=StringIO())
        self.assertEqual(proc.returncode, errno.EINVAL)
        self.assertIn('EINVAL', proc.stderr.getvalue())

    def test_force_disable_non_alwayson_mod(self):
        '''
        Test that passing non-existent name to "ceph mgr module force disable"
        command leads to an error.
        '''
        json_output = self.get_ceph_cmd_stdout(
            'mgr module ls --format json-pretty', check_status=False,
            stderr=StringIO())
        output_dict = json.loads(json_output)
        some_non_alwayson_mod = output_dict['enabled_modules'][0]

        proc = self.run_ceph_cmd(
            f'mgr module force disable {some_non_alwayson_mod} {self.CONFIRM}',
            check_status=False, stderr=StringIO())
        self.assertEqual(proc.returncode, errno.EINVAL)
        self.assertIn('EINVAL', proc.stderr.getvalue())

    def test_enabled_by_default(self):
        '''
        Test that volumes plugin is enabled by default and is also reported as
        "always on".
        '''
        json_output = self.get_ceph_cmd_stdout('mgr module ls --format json')
        json_output = json.loads(json_output)

        self.assertIn(self.VOL_MOD_NAME, json_output['always_on_modules'])

        self.assertNotIn(self.VOL_MOD_NAME, json_output['enabled_modules'])
        self.assertNotIn(self.VOL_MOD_NAME, json_output['disabled_modules'])
        self.assertNotIn(self.VOL_MOD_NAME, json_output['force_disabled_modules'])

    def test_disable_fails(self):
        '''
        Test that running "ceph mgr module disable volumes" fails with EPERM.

        This is expected since volumes is an always-on module and therefore
        it can only be disabled using command "ceph mgr module force disable
        volumes".
        '''
        proc = self.run_ceph_cmd(f'mgr module disable {self.VOL_MOD_NAME}',
                                 stderr=StringIO(), check_status=False)
        self.assertEqual(proc.returncode, errno.EPERM)

        proc_stderr = proc.stderr.getvalue()
        self.assertIn('EPERM', proc_stderr)

    def test_enable_idempotency(self):
        '''
        Test that enabling volumes plugin when it is already enabled doesn't
        exit with non-zero return value.

        Also test that it reports plugin as already enabled.
        '''
        proc = self.run_ceph_cmd(f'mgr module enable {self.VOL_MOD_NAME}',
                                 stderr=StringIO())
        self.assertEqual(proc.returncode, 0)

        proc_stderr = proc.stderr.getvalue()
        self.assertIn('already enabled', proc_stderr)
        self.assertIn('always-on', proc_stderr)

    def test_enable_post_disabling(self):
        '''
        Test that enabling volumes plugin after (force-)disabling it works
        successfully.

        Alo test "ceph mgr module ls" output for volumes plugin afterwards.
        '''
        self.run_ceph_cmd(f'mgr module force disable {self.VOL_MOD_NAME} '
                          f'{self.CONFIRM}')
        # give bit of time for plugin to be disabled.
        sleep(5)

        self.run_ceph_cmd(f'mgr module enable {self.VOL_MOD_NAME}')
        # give bit of time for plugin to be functional again
        sleep(5)
        json_output = self.get_ceph_cmd_stdout('mgr module ls --format json')
        json_output = json.loads(json_output)
        self.assertIn(self.VOL_MOD_NAME, json_output['always_on_modules'])
        self.assertNotIn(self.VOL_MOD_NAME, json_output['enabled_modules'])
        self.assertNotIn(self.VOL_MOD_NAME, json_output['disabled_modules'])
        self.assertNotIn(self.VOL_MOD_NAME, json_output['force_disabled_modules'])

        # plugin is reported properly by "ceph mgr module ls" command, check if
        # it is also working fine.
        self.run_ceph_cmd('fs volume ls')