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 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257
|
/*
Copyright (c) 2017, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _WIN32
#include <pwd.h> // getpwuid
#include <sys/stat.h>
#endif
#include <fstream>
#include <string>
#include <system_error>
#include <gmock/gmock-matchers.h>
#include <gtest/gtest.h>
#ifdef RAPIDJSON_NO_SIZETYPEDEFINE
#include "my_rapidjson_size_t.h"
#endif
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include "common.h" // truncate_string
#include "dim.h"
#include "harness_assert.h"
#include "keyring/keyring_manager.h"
#include "mock_server_rest_client.h"
#include "mock_server_testutils.h"
#include "mysql/harness/net_ts/impl/resolver.h"
#include "mysql/harness/net_ts/internet.h"
#include "mysql/harness/stdx/expected.h"
#include "mysql/harness/string_utils.h" // split_string
#include "mysqld_error.h"
#include "mysqlrouter/cluster_metadata.h"
#include "mysqlrouter/utils.h" // getpwuid
#include "random_generator.h"
#include "rest_api_testutils.h"
#include "router_component_test.h"
#include "router_component_testutils.h"
#include "router_config.h"
#include "router_test_helpers.h" // get_file_output
#include "script_generator.h"
#include "socket_operations.h"
#include "tcp_port_pool.h"
#include "test/temp_directory.h"
/**
* @file
* @brief Component Tests for the bootstrap operation
*/
using namespace std::chrono_literals;
using namespace std::string_literals;
using mysqlrouter::ClusterType;
// for the test with no param
class RouterBootstrapTest : public RouterComponentBootstrapTest {};
#ifndef _WIN32
// needs symlink()
TEST_F(RouterBootstrapTest, bootstrap_and_run_from_symlinked_dir) {
RecordProperty("Description",
"Bootstrap into a symlinked directory and check that the "
"router can run from that directory.");
const auto server_port = port_pool_.get_next_available();
const auto server_x_port = port_pool_.get_next_available();
const auto http_port = port_pool_.get_next_available();
std::vector<Config> config{
{"127.0.0.1", server_port, http_port,
get_data_dir().join("bootstrap_gr.js").str()},
};
SCOPED_TRACE("// prepare symlinked directory");
TempDirectory tmpdir;
auto subdir = mysql_harness::Path(tmpdir.name()).join("subdir").str();
auto symlinkdir = mysql_harness::Path(tmpdir.name()).join("symlink").str();
ASSERT_EQ(mysql_harness::mkdir(subdir, 0700), 0);
ASSERT_EQ(symlink(subdir.c_str(), symlinkdir.c_str()), 0);
// point the bootstrap at the symlink dir.
bootstrap_dir.reset(symlinkdir);
SCOPED_TRACE("// bootstrap into the symlink dir");
ASSERT_NO_FATAL_FAILURE(bootstrap_failover(
config, ClusterType::GR_V2, {}, EXIT_SUCCESS, {}, 30s, {2, 0, 3},
{
"--conf-set-option=DEFAULT.plugin_folder=" +
mysql_harness::get_plugin_dir(get_origin().str()),
"--conf-set-option=DEFAULT.logging_folder=" + get_logging_dir().str(),
"--conf-set-option=DEFAULT.keyring_path=" + symlinkdir +
"/data/keyring",
}));
SCOPED_TRACE("// launch mock-server for router");
const std::string runtime_json_stmts =
get_data_dir().join("metadata_dynamic_nodes_v2_gr.js").str();
// launch mock server that is our metadata server
launch_mysql_server_mock(runtime_json_stmts, server_port, EXIT_SUCCESS, false,
http_port);
set_mock_metadata(http_port, "cluster-specific-id", {GRNode{server_port}}, 0,
{ClusterNode{server_port, "uuid-1", server_x_port}});
SCOPED_TRACE("// launch router with bootstrapped config");
launch_router({"-c", bootstrap_dir.name() + "/mysqlrouter.conf"});
}
#endif
struct BootstrapTestParam {
ClusterType cluster_type;
std::string description;
std::string trace_file;
std::string trace_file2;
std::string trace_file3;
};
auto get_test_description(
const ::testing::TestParamInfo<BootstrapTestParam> &info) {
return info.param.description;
}
class RouterBootstrapOkTest
: public RouterComponentBootstrapTest,
public ::testing::WithParamInterface<BootstrapTestParam> {};
/**
* @test
* verify that the router's \c --bootstrap can bootstrap
* from metadata-servers's PRIMARY over TCP/IP
* @test
* Group Replication roles:
* - PRIMARY
*/
TEST_P(RouterBootstrapOkTest, BootstrapOk) {
const auto param = GetParam();
std::vector<Config> config{
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join(param.trace_file).str()},
};
std::vector<std::string> expected_output{
"# Bootstrapping MySQL Router "s + MYSQL_ROUTER_VERSION + " \\(" +
MYSQL_ROUTER_VERSION_EDITION + "\\) instance at"};
// For metadata version 1.x we should get deprecation warning
if (param.cluster_type == ClusterType::GR_V1) {
RecordProperty("Worklog", "15876");
RecordProperty("RequirementId", "FR2");
RecordProperty("Description",
"Checks that the Router prints a deprecation warning for "
"metadata version 1.x");
expected_output.push_back(
"WARNING: The target Cluster's Metadata version \\('1.0.2'\\) is "
"deprecated. Please use the latest MySQL Shell to upgrade it using "
"'dba.upgradeMetadata\\(\\)'.");
}
ASSERT_NO_FATAL_FAILURE(bootstrap_failover(config, param.cluster_type, {},
EXIT_SUCCESS, expected_output));
// let's check if the actual config file output is what we expect:
const char *expected_config_default_part = "unknown_config_option=error";
const char *expected_config_gr_part1 =
R"([metadata_cache:bootstrap]
cluster_type=gr
router_id=1)";
// we skip user as it is random and would require regex matching which would
// require tons of escaping
// user=mysql_router1_daxi69tk9btt
const char *expected_config_gr_part2 =
R"(metadata_cluster=mycluster
ttl=0.5
auth_cache_ttl=-1
auth_cache_refresh_interval=2
use_gr_notifications=0
[routing:bootstrap_rw]
bind_address=0.0.0.0
bind_port=6446
destinations=metadata-cache://mycluster/?role=PRIMARY
routing_strategy=first-available
protocol=classic
[routing:bootstrap_ro]
bind_address=0.0.0.0
bind_port=6447
destinations=metadata-cache://mycluster/?role=SECONDARY
routing_strategy=round-robin-with-fallback
protocol=classic
[routing:bootstrap_x_rw]
bind_address=0.0.0.0
bind_port=6448
destinations=metadata-cache://mycluster/?role=PRIMARY
routing_strategy=first-available
protocol=x
[routing:bootstrap_x_ro]
bind_address=0.0.0.0
bind_port=6449
destinations=metadata-cache://mycluster/?role=SECONDARY
routing_strategy=round-robin-with-fallback
protocol=x)";
const char *expected_config_ar_part1 =
R"([metadata_cache:bootstrap]
cluster_type=rs
router_id=1)";
// we skip user as it is random and would require regex matching which would
// require tons of escaping
// user=mysql_router1_ritc56yrjz42
const char *expected_config_ar_part2 =
R"(metadata_cluster=mycluster
ttl=0.5
auth_cache_ttl=-1
auth_cache_refresh_interval=2
[routing:bootstrap_rw]
bind_address=0.0.0.0
bind_port=6446
destinations=metadata-cache://mycluster/?role=PRIMARY
routing_strategy=first-available
protocol=classic
[routing:bootstrap_ro]
bind_address=0.0.0.0
bind_port=6447
destinations=metadata-cache://mycluster/?role=SECONDARY
routing_strategy=round-robin-with-fallback
protocol=classic
[routing:bootstrap_x_rw]
bind_address=0.0.0.0
bind_port=6448
destinations=metadata-cache://mycluster/?role=PRIMARY
routing_strategy=first-available
protocol=x
[routing:bootstrap_x_ro]
bind_address=0.0.0.0
bind_port=6449
destinations=metadata-cache://mycluster/?role=SECONDARY
routing_strategy=round-robin-with-fallback
protocol=x)";
const std::string config_file_expected1 =
GetParam().cluster_type == ClusterType::RS_V2 ? expected_config_ar_part1
: expected_config_gr_part1;
const std::string config_file_expected2 =
GetParam().cluster_type == ClusterType::RS_V2 ? expected_config_ar_part2
: expected_config_gr_part2;
// 'config_file' is set as side-effect of bootstrap_failover()
ASSERT_THAT(config_file, ::testing::Not(::testing::IsEmpty()));
const std::string config_file_str = get_file_output(config_file);
EXPECT_THAT(
config_file_str,
::testing::AllOf(::testing::HasSubstr(expected_config_default_part),
::testing::HasSubstr(config_file_expected1),
::testing::HasSubstr(config_file_expected2)));
}
INSTANTIATE_TEST_SUITE_P(
BootstrapOkTest, RouterBootstrapOkTest,
::testing::Values(
BootstrapTestParam{ClusterType::GR_V2, "gr", "bootstrap_gr.js", "", ""},
BootstrapTestParam{ClusterType::RS_V2, "ar", "bootstrap_ar.js", "", ""},
BootstrapTestParam{ClusterType::GR_V1, "gr_v1", "bootstrap_gr_v1.js",
"", ""}),
get_test_description);
struct BootstrapOkBasePortTestParam {
const char *test_name;
std::vector<std::string> bs_params;
uint16_t expected_port_classic_rw;
uint16_t expected_port_classic_ro;
uint16_t expected_port_x_rw;
uint16_t expected_port_x_ro;
};
class RouterBootstrapOkBasePortTest
: public RouterComponentBootstrapTest,
public ::testing::WithParamInterface<BootstrapOkBasePortTestParam> {};
namespace {
void check_bind_port(const std::string &conf_file_content,
const std::string &route_name,
const std::string &protocol_name,
const std::string &server_role,
uint16_t expected_bind_port) {
const std::string routing_strategy = server_role == "PRIMARY"
? "first-available"
: "round-robin-with-fallback";
// clang-format off
const std::string routing_section =
"[routing:"s + route_name + "]\n"
"bind_address=0.0.0.0\n" +
"bind_port=" + std::to_string(expected_bind_port) + "\n" +
"destinations=metadata-cache://mycluster/?role=" + server_role + "\n" +
"routing_strategy=" + routing_strategy + "\n" +
"protocol=" + protocol_name + "\n";
// clang-format on
EXPECT_TRUE(conf_file_content.find(routing_section) != std::string::npos)
<< conf_file_content << "EXPECTED: \n"
<< routing_section;
}
bool config_file_contains(const std::string &conf_file_content,
const std::string &line,
const size_t occurences = 1) {
return occurences == count_str_occurences(conf_file_content, line);
}
} // namespace
/**
* @test
* verify that the --conf-base-port bootstrap parameter is handled
* properly
*/
TEST_P(RouterBootstrapOkBasePortTest, RouterBootstrapOkBasePort) {
const auto param = GetParam();
const std::string tracefile = "bootstrap_gr.js";
std::vector<Config> mock_servers{
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(), get_data_dir().join(tracefile).str()},
};
std::vector<std::string> cmdline = {
"--bootstrap=" + mock_servers.at(0).ip + ":" +
std::to_string(mock_servers.at(0).port),
"-d", bootstrap_dir.name()};
cmdline.insert(cmdline.begin(), param.bs_params.begin(),
param.bs_params.end());
ASSERT_NO_FATAL_FAILURE(
bootstrap_failover(mock_servers, ClusterType::GR_V2, cmdline));
// 'config_file' is set as side-effect of bootstrap_failover()
ASSERT_THAT(config_file, ::testing::Not(::testing::IsEmpty()));
// let's check if the actual config file contains what we expect:
const std::string config_file_str = get_file_output(config_file);
// classic RW
check_bind_port(config_file_str, "bootstrap_rw", "classic", "PRIMARY",
param.expected_port_classic_rw);
// classic RO
check_bind_port(config_file_str, "bootstrap_ro", "classic", "SECONDARY",
param.expected_port_classic_ro);
// x RW
check_bind_port(config_file_str, "bootstrap_x_rw", "x", "PRIMARY",
param.expected_port_x_rw);
// x RO
check_bind_port(config_file_str, "bootstrap_x_ro", "x", "SECONDARY",
param.expected_port_x_ro);
}
const BootstrapOkBasePortTestParam bootstrap_ok_base_port_test_param[] = {
{"default_ports",
/* bs_params */ {},
/* expected_port_classic_rw */ 6446,
/* expected_port_classic_ro */ 6447,
/* expected_port_x_rw */ 6448,
/* expected_port_x_ro */ 6449},
{"legacy_default_ports",
/* bs_params */ {"--conf-base-port=0"},
/* expected_port_classic_rw */ 6446,
/* expected_port_classic_ro */ 6447,
/* expected_port_x_rw */ 64460,
/* expected_port_x_ro */ 64470},
{"consecutive_ports",
/* bs_params */ {"--conf-base-port=1234"},
/* expected_port_classic_rw */ 1234,
/* expected_port_classic_ro */ 1235,
/* expected_port_x_rw */ 1236,
/* expected_port_x_ro */ 1237}};
INSTANTIATE_TEST_SUITE_P(RouterBootstrapOkBasePort,
RouterBootstrapOkBasePortTest,
::testing::ValuesIn(bootstrap_ok_base_port_test_param),
[](const auto &info) { return info.param.test_name; });
struct BootstrapErrorBasePortTestParam {
const char *test_name;
std::vector<std::string> bs_params;
std::string expected_error;
};
class RouterBootstrapErrorBasePortTest
: public RouterComponentBootstrapTest,
public ::testing::WithParamInterface<BootstrapErrorBasePortTestParam> {};
/**
* @test
* verify that the --conf-base-port bootstrap parameter errors are handled
* properly
*/
TEST_P(RouterBootstrapErrorBasePortTest, RouterBootstrapErrorBasePort) {
const auto param = GetParam();
const std::string tracefile = "bootstrap_gr.js";
std::vector<Config> mock_servers{
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(), get_data_dir().join(tracefile).str()},
};
const uint16_t server_port = port_pool_.get_next_available();
const std::string json_stmts = get_data_dir().join(tracefile).str();
launch_mysql_server_mock(json_stmts, server_port, EXIT_SUCCESS, false);
// launch the router in bootstrap mode
std::vector<std::string> cmdline = {"--bootstrap=root:"s + kRootPassword +
"@localhost:"s +
std::to_string(server_port),
"-d", bootstrap_dir.name()};
cmdline.insert(cmdline.begin(), param.bs_params.begin(),
param.bs_params.end());
auto &router = launch_router_for_bootstrap(cmdline, EXIT_FAILURE);
check_exit_code(router, EXIT_FAILURE);
// let's check if the expected error was reported:
EXPECT_THAT(router.get_full_output(),
::testing::ContainsRegex(param.expected_error));
}
const BootstrapErrorBasePortTestParam bootstrap_error_base_port_test_param[] = {
{"negative",
{"--conf-base-port=-1"},
"--conf-base-port needs value between 0 and 65532 inclusive, was '-1'"},
{"too_big",
{"--conf-base-port=65533"},
"--conf-base-port needs value between 0 and 65532 inclusive, was '65533'"},
{"nan",
{"--conf-base-port=abc"},
"--conf-base-port needs value between 0 and 65532 inclusive, was 'abc'"},
{"empty",
{"--conf-base-port="},
"--conf-base-port needs value between 0 and 65532 inclusive, was ''"},
};
INSTANTIATE_TEST_SUITE_P(
RouterBootstrapErrorBasePort, RouterBootstrapErrorBasePortTest,
::testing::ValuesIn(bootstrap_error_base_port_test_param),
[](const auto &info) { return info.param.test_name; });
struct ReBootstrapOkBasePortTestParam {
const char *test_name;
std::vector<std::string> first_bs_params;
std::vector<std::string> second_bs_params;
uint16_t expected_port_classic_rw;
uint16_t expected_port_classic_ro;
uint16_t expected_port_x_rw;
uint16_t expected_port_x_ro;
};
class RouterReBootstrapOkBasePortTest
: public RouterComponentBootstrapTest,
public ::testing::WithParamInterface<ReBootstrapOkBasePortTestParam> {};
/**
* @test
* verify that the --conf-base-port bootstrap parameter is handled
* properly when we overwrite an existing Router configuration
*/
TEST_P(RouterReBootstrapOkBasePortTest, RouterReBootstrapOkBasePort) {
const auto param = GetParam();
const std::string tracefile = "bootstrap_gr.js";
const uint16_t server_port = port_pool_.get_next_available();
const std::string json_stmts = get_data_dir().join(tracefile).str();
launch_mysql_server_mock(json_stmts, server_port, EXIT_SUCCESS, false);
// do the first bootstrap
std::vector<std::string> cmdline_first_bs = {
"--bootstrap=root:"s + kRootPassword + "@localhost:"s +
std::to_string(server_port),
"-d", bootstrap_dir.name()};
cmdline_first_bs.insert(cmdline_first_bs.begin(),
param.first_bs_params.begin(),
param.first_bs_params.end());
auto &router_bs1 = launch_router_for_bootstrap(cmdline_first_bs);
check_exit_code(router_bs1, EXIT_SUCCESS);
const std::string conf_file2 =
mysql_harness::Path(bootstrap_dir.name()).join("mysqlrouter.conf").str();
// do the second bootstrap using the same directory
std::vector<std::string> cmdline_second_bs = {
"--bootstrap=root:"s + kRootPassword + "@localhost:"s +
std::to_string(server_port),
"-d", bootstrap_dir.name()};
cmdline_second_bs.insert(cmdline_second_bs.begin(),
param.second_bs_params.begin(),
param.second_bs_params.end());
auto &router_bs2 = launch_router_for_bootstrap(cmdline_second_bs);
check_exit_code(router_bs2, EXIT_SUCCESS);
const std::string conf_file =
mysql_harness::Path(bootstrap_dir.name()).join("mysqlrouter.conf").str();
// let's check if the actual config file contains what we expect:
const std::string config_file_str = get_file_output(conf_file);
// classic RW
check_bind_port(config_file_str, "bootstrap_rw", "classic", "PRIMARY",
param.expected_port_classic_rw);
// classic RO
check_bind_port(config_file_str, "bootstrap_ro", "classic", "SECONDARY",
param.expected_port_classic_ro);
// x RW
check_bind_port(config_file_str, "bootstrap_x_rw", "x", "PRIMARY",
param.expected_port_x_rw);
// x RO
check_bind_port(config_file_str, "bootstrap_x_ro", "x", "SECONDARY",
param.expected_port_x_ro);
}
const ReBootstrapOkBasePortTestParam rebootstrap_ok_base_port_test_param[] = {
// create a config with legacy defaults [6446, 6447, 64460, 64470]
// bootstrap again on top of that config with no conf-base-port parameter
// since the existing conf uses legacy default we should stick to them
{"overwrite_over_legacy_defaults_keep_them",
/* first_bs_params */ {"--conf-base-port=0"},
/* second_bs_params */ {},
/* expected_port_classic_rw */ 6446,
/* expected_port_classic_ro */ 6447,
/* expected_port_x_rw */ 64460,
/* expected_port_x_ro */ 64470},
// create a config with custom ports [5000, 5001, 5002, 5003]
// bootstrap again on top of that config with no conf-base-port parameter
// we expect new default ports to be used
{"overwrite_custom_ports",
/* first_bs_params */ {"--conf-base-port=5000"},
/* second_bs_params */ {},
/* expected_port_classic_rw */ 6446,
/* expected_port_classic_ro */ 6447,
/* expected_port_x_rw */ 6448,
/* expected_port_x_ro */ 6449},
// create a config with legacy ports [6446, 6447, 64460, 64470]
// bootstrap again on top of that config with --conf-base-port=1 parameter
// we expect 1, 2, 3, 4 ports to overwrite the legacy ports
{"overwrite_legacy_with_custom_ports",
/* first_bs_params */ {"--conf-base-port=0"},
/* second_bs_params */ {"--conf-base-port=1"},
/* expected_port_classic_rw */ 1,
/* expected_port_classic_ro */ 2,
/* expected_port_x_rw */ 3,
/* expected_port_x_ro */ 4},
// create a config with legacy defaults [6446, 6447, 64460, 64470]
// bootstrap again on top of that config with specifying conf-base-port
// parameter even though the existing conf uses legacy default we change
// them because the user used conf-base-port, so we should not be using
// defaults
{"overwrite_over_legacy_defaults_using_param_change_them",
/* first_bs_params */ {"--conf-base-port=0"},
/* second_bs_params */ {"--conf-base-port=6446"},
/* expected_port_classic_rw */ 6446,
/* expected_port_classic_ro */ 6447,
/* expected_port_x_rw */ 6448,
/* expected_port_x_ro */ 6449},
// create a config with custom ports [6666, 6667, 6668, 6669]
// bootstrap again on top of that config with conf-base-port=0 parameter
// since the user requested legacy defaults the ports in the config should
// be [6446, 6447, 64460, 64470]
{"overwrite_custom_ports_with_legacy",
/* first_bs_params */ {"--conf-base-port=6666"},
/* second_bs_params */ {"--conf-base-port=0"},
/* expected_port_classic_rw */ 6446,
/* expected_port_classic_ro */ 6447,
/* expected_port_x_rw */ 64460,
/* expected_port_x_ro */ 64470},
#ifndef _WIN32
// create a config with no tcp endpoints
// bootstrap again on top of that config with no conf-base-port parameter
// the new defaults should be used
{"overwrite_over_no_tcp_config_new_defaults",
/* first_bs_params */ {"--conf-skip-tcp"},
/* second_bs_params */ {},
/* expected_port_classic_rw */ 6446,
/* expected_port_classic_ro */ 6447,
/* expected_port_x_rw */ 6448,
/* expected_port_x_ro */ 6449},
// create a config with no tcp endpoints
// bootstrap again on top of that config with conf-base-port=0 parameter
// since the user requested legacy defaults the ports in the config should
// be [6446, 6447, 64460, 64470]
{"overwrite_over_no_tcp_config_legacy_defaults",
/* first_bs_params */ {"--conf-skip-tcp"},
/* second_bs_params */ {"--conf-base-port=0"},
/* expected_port_classic_rw */ 6446,
/* expected_port_classic_ro */ 6447,
/* expected_port_x_rw */ 64460,
/* expected_port_x_ro */ 64470}
#endif
};
INSTANTIATE_TEST_SUITE_P(
RouterReBootstrapOkBasePort, RouterReBootstrapOkBasePortTest,
::testing::ValuesIn(rebootstrap_ok_base_port_test_param),
[](const auto &info) { return info.param.test_name; });
#ifndef _WIN32
/**
* verify that the router's \c --user is ignored if it matches the current
* username.
*
* skipped on win32 as \c --user isn't supported on windows
*
* @test
* test if Bug#27698052 is fixed
* @test
* Group Replication roles:
* - PRIMARY
*/
class RouterBootstrapUserIsCurrentUser
: public RouterComponentBootstrapTest,
public ::testing::WithParamInterface<BootstrapTestParam> {};
TEST_P(RouterBootstrapUserIsCurrentUser, BootstrapUserIsCurrentUser) {
const auto param = GetParam();
auto current_userid = geteuid();
auto current_userpw = getpwuid(current_userid);
if (current_userpw != nullptr) {
const char *current_username = current_userpw->pw_name;
std::vector<Config> mock_servers{
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join(param.trace_file).str()},
};
std::vector<std::string> router_options = {
"--bootstrap=" + mock_servers.at(0).ip + ":" +
std::to_string(mock_servers.at(0).port),
"-d", bootstrap_dir.name(), "--user", current_username};
ASSERT_NO_FATAL_FAILURE(bootstrap_failover(
mock_servers, GetParam().cluster_type, router_options));
}
}
INSTANTIATE_TEST_SUITE_P(
BootstrapUserIsCurrentUser, RouterBootstrapUserIsCurrentUser,
::testing::Values(
BootstrapTestParam{ClusterType::GR_V2, "gr", "bootstrap_gr.js", "", ""},
BootstrapTestParam{ClusterType::RS_V2, "ar", "bootstrap_ar.js", "", ""},
BootstrapTestParam{ClusterType::GR_V1, "gr_v1", "bootstrap_gr_v1.js",
"", ""}),
get_test_description);
#endif
class RouterBootstrapailoverClusterIdDiffers
: public RouterComponentBootstrapTest,
public ::testing::WithParamInterface<BootstrapTestParam> {};
/**
* @test
* verify that the router's \c --bootstrap fails when it fails over to the
* node with a different cluster-id/replication-group-id
*/
TEST_P(RouterBootstrapailoverClusterIdDiffers,
BootstrapFailoverClusterIdDiffers) {
std::vector<Config> mock_servers{
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join(GetParam().trace_file).str(), false, "cluster-id-1"},
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join(GetParam().trace_file2).str(), false,
"cluster-id-2"},
};
// check that it failed as expected
ASSERT_NO_FATAL_FAILURE(bootstrap_failover(
mock_servers, ClusterType::RS_V2, {}, EXIT_FAILURE,
{"Node on '.*' that the bootstrap failed over to, seems to belong to "
"different cluster\\(cluster-id-1 != cluster-id-2\\), skipping"}));
}
INSTANTIATE_TEST_SUITE_P(
BootstrapFailoverClusterIdDiffers, RouterBootstrapailoverClusterIdDiffers,
::testing::Values(
BootstrapTestParam{ClusterType::GR_V2, "gr",
"bootstrap_failover_super_read_only_1_gr.js",
"bootstrap_failover_super_read_only_1_gr.js", ""},
BootstrapTestParam{ClusterType::RS_V2, "ar",
"bootstrap_failover_super_read_only_1_ar.js",
"bootstrap_failover_super_read_only_1_ar.js", ""},
BootstrapTestParam{ClusterType::GR_V1, "gr_v1",
"bootstrap_failover_super_read_only_1_gr_v1.js",
"bootstrap_failover_super_read_only_1_gr_v1.js",
""}),
get_test_description);
/**
* @test
* verify that the router's \c --bootstrap can bootstrap
* from metadata-server's PRIMARY over TCP/IP and generate
* a configuration with unix-sockets only
* @test
* Group Replication roles:
* - PRIMARY
*/
class RouterBootstrapOnlySockets
: public RouterComponentBootstrapTest,
public ::testing::WithParamInterface<BootstrapTestParam> {};
TEST_P(RouterBootstrapOnlySockets, BootstrapOnlySockets) {
const auto param = GetParam();
std::vector<Config> mock_servers{
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join(param.trace_file).str()},
};
std::vector<std::string> router_options = {
"--bootstrap=" + mock_servers.at(0).ip + ":" +
std::to_string(mock_servers.at(0).port),
"-d", bootstrap_dir.name(), "--conf-skip-tcp", "--conf-use-sockets"};
#ifndef _WIN32
const std::vector<std::string> expected_output{
"- Read/Write Connections: .*/mysqlx.sock",
"- Read/Only Connections: .*/mysqlxro.sock"};
const auto expected_result = EXIT_SUCCESS;
#else
const std::vector<std::string> expected_output{
"Error: unknown option '--conf-skip-tcp'"};
const auto expected_result = EXIT_FAILURE;
#endif
ASSERT_NO_FATAL_FAILURE(
bootstrap_failover(mock_servers, GetParam().cluster_type, router_options,
expected_result, expected_output));
}
INSTANTIATE_TEST_SUITE_P(
BootstrapOnlySockets, RouterBootstrapOnlySockets,
::testing::Values(
BootstrapTestParam{ClusterType::GR_V2, "gr", "bootstrap_gr.js", "", ""},
BootstrapTestParam{ClusterType::RS_V2, "ar", "bootstrap_ar.js", "", ""},
BootstrapTestParam{ClusterType::GR_V1, "gr_v1", "bootstrap_gr_v1.js",
"", ""}),
get_test_description);
class BootstrapUnsupportedSchemaVersionTest
: public RouterComponentBootstrapTest,
public ::testing::WithParamInterface<mysqlrouter::MetadataSchemaVersion> {
};
/**
* @test
* verify that the router's \c --bootstrap detects an unsupported
* metadata schema version
*/
TEST_P(BootstrapUnsupportedSchemaVersionTest,
BootstrapUnsupportedSchemaVersion) {
std::vector<Config> mock_servers{
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join("bootstrap_unsupported_schema_version.js").str()},
};
const auto version = GetParam();
// check that it failed as expected
ASSERT_NO_FATAL_FAILURE(bootstrap_failover(
mock_servers, ClusterType::GR_V2, {}, EXIT_FAILURE,
{"^Error: This version of MySQL Router is not compatible "
"with the provided MySQL InnoDB cluster metadata. "
"Expected metadata version 1.0.0, 2.0.0, got " +
to_string(version)},
10s, GetParam()));
}
INSTANTIATE_TEST_SUITE_P(
BootstrapUnsupportedSchemaVersion, BootstrapUnsupportedSchemaVersionTest,
::testing::Values(mysqlrouter::MetadataSchemaVersion{3, 0, 0},
mysqlrouter::MetadataSchemaVersion{0, 0, 1},
mysqlrouter::MetadataSchemaVersion{3, 1, 0}));
/**
* @test
* verify that the router errors out cleanly when received some unexpected
* error from the metadata server
*/
TEST_F(RouterComponentBootstrapTest, BootstrapErrorOnFirstQuery) {
std::vector<Config> mock_servers{
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join("bootstrap_error_on_first_query.js").str()},
};
// check that it failed as expected
ASSERT_NO_FATAL_FAILURE(bootstrap_failover(
mock_servers, ClusterType::RS_V2, {}, EXIT_FAILURE,
{"Error executing MySQL query", "Some unexpected error occured"}, 10s));
}
/**
* @test
* verify that the router's \c --bootstrap detects an upgrade
* metadata schema version and gives a proper message
*/
TEST_F(RouterComponentBootstrapTest, BootstrapWhileMetadataUpgradeInProgress) {
std::vector<Config> mock_servers{
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join("bootstrap_unsupported_schema_version.js").str()},
};
ASSERT_NO_FATAL_FAILURE(bootstrap_failover(
mock_servers, ClusterType::GR_V2, {}, EXIT_FAILURE,
{"^Error: Currently the cluster metadata update is in progress. Please "
"rerun the bootstrap when it is finished."},
10s, {0, 0, 0}));
}
/**
* @test
* verify that the router's \c --bootstrap handles --pid-file option on
* command line correctly
* TS_FR12_01
*/
TEST_F(RouterComponentBootstrapTest, BootstrapPidfileOpt) {
std::string pidfile =
mysql_harness::Path(get_test_temp_dir_name()).join("test.pid").str();
std::vector<Config> config{
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join("bootstrap_gr.js").str()},
};
std::vector<std::string> router_options = {
"--pid-file", pidfile,
"--bootstrap=" + config.at(0).ip + ":" +
std::to_string(config.at(0).port),
"-d", bootstrap_dir.name()};
ASSERT_NO_FATAL_FAILURE(bootstrap_failover(
config, ClusterType::GR_V2, router_options, EXIT_FAILURE,
{"^Error: Option --pid-file cannot be used together "
"with -B/--bootstrap"},
10s));
}
/**
* @test
* verify that the router's \c --bootstrap handles pid_file option in
* config file correctly
* TS_FR13_01
*/
TEST_F(RouterComponentBootstrapTest, BootstrapPidfileCfg) {
std::string pidfile = mysql_harness::Path(get_test_temp_dir_name())
.real_path()
.join("test.pid")
.str();
auto params = get_DEFAULT_defaults();
params["pid_file"] = pidfile;
std::string conf_file =
create_config_file(get_test_temp_dir_name(), "", ¶ms);
{
std::vector<Config> config{
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join("bootstrap_gr.js").str()},
};
std::vector<std::string> router_options = {
"-c", conf_file,
"--bootstrap=" + config.at(0).ip + ":" +
std::to_string(config.at(0).port),
"-d", bootstrap_dir.name()};
ASSERT_NO_FATAL_FAILURE(
bootstrap_failover(config, ClusterType::GR_V2, router_options));
ASSERT_FALSE(mysql_harness::Path(pidfile.c_str()).exists());
}
// Post check that pid_file is not included in config
const std::string config_file_str = get_file_output(config_file);
EXPECT_TRUE(config_file_str.find("pid_file") == std::string::npos)
<< "config file includes pid_file setting :" << std::endl
<< config_file_str << std::endl;
}
/**
* @test
* verify that the router's \c --bootstrap does not create a pidfile when
* ROUTER_PID is specified
* TS_FR13_02
*/
TEST_F(RouterComponentBootstrapTest, BootstrapPidfileEnv) {
// Set ROUTER_PID
std::string pidfile = mysql_harness::Path(get_test_temp_dir_name())
.real_path()
.join("test.pid")
.str();
#ifdef _WIN32
int err_code = _putenv_s("ROUTER_PID", pidfile.c_str());
#else
int err_code = ::setenv("ROUTER_PID", pidfile.c_str(), 1);
#endif
if (err_code) throw std::runtime_error("Failed to add ROUTER_PID");
{
std::vector<Config> config{
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join("bootstrap_gr.js").str()},
};
std::vector<std::string> router_options = {
"--bootstrap=" + config.at(0).ip + ":" +
std::to_string(config.at(0).port),
"-d", bootstrap_dir.name()};
ASSERT_NO_FATAL_FAILURE(
bootstrap_failover(config, ClusterType::GR_V2, router_options));
ASSERT_FALSE(mysql_harness::Path(pidfile.c_str()).exists());
}
// reset ROUTER_PID
#ifdef _WIN32
err_code = _putenv_s("ROUTER_PID", "");
#else
err_code = ::unsetenv("ROUTER_PID");
#endif
if (err_code) throw std::runtime_error("Failed to remove ROUTER_PID");
// Post check that pid_file is not included in config
const std::string config_file_str = get_file_output(config_file);
EXPECT_TRUE(config_file_str.find("pid_file") == std::string::npos)
<< "config file includes pid_file setting :" << std::endl
<< config_file_str << std::endl;
}
class RouterBootstrapFailoverSuperReadonly
: public RouterComponentBootstrapTest,
public ::testing::WithParamInterface<BootstrapTestParam> {};
/**
* @test
* verify that bootstrap will fail-over to another node if the initial
* node is not writable
* @test
* Group Replication roles:
* - SECONDARY
* - PRIMARY
* - SECONDARY (not used)
*/
TEST_P(RouterBootstrapFailoverSuperReadonly, BootstrapFailoverSuperReadonly) {
const auto param = GetParam();
std::vector<Config> config{
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join(param.trace_file).str()},
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join(param.trace_file2).str()},
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(), ""},
};
ASSERT_NO_FATAL_FAILURE(bootstrap_failover(config, param.cluster_type));
}
INSTANTIATE_TEST_SUITE_P(
BootstrapFailoverSuperReadonly, RouterBootstrapFailoverSuperReadonly,
::testing::Values(
BootstrapTestParam{ClusterType::GR_V2, "gr",
"bootstrap_failover_super_read_only_1_gr.js",
"bootstrap_gr.js", ""},
BootstrapTestParam{ClusterType::RS_V2, "ar",
"bootstrap_failover_super_read_only_1_ar.js",
"bootstrap_ar.js", ""},
BootstrapTestParam{ClusterType::GR_V1, "gr_v1",
"bootstrap_failover_super_read_only_1_gr_v1.js",
"bootstrap_gr_v1.js", ""}),
get_test_description);
class RouterBootstrapFailoverSuperReadonly2ndNodeDead
: public RouterComponentBootstrapTest,
public ::testing::WithParamInterface<BootstrapTestParam> {};
/**
* @test
* verify that bootstrap will fail-over to another node if the initial
* node is not writable and 2nd candidate has connection problems
* @test
* Group Replication roles:
* - SECONDARY
* - <connect-failure>
* - PRIMARY
* @test
* connection problems could be anything from 'auth-failure' to
* 'network-errors'. This test uses a \c port==0 to create a failure which is
* reserved and unassigned.
*
* @note The implementation uses \c port=65536 to circumvents libmysqlclients
* \code{.py} if port == 0: port = 3306 \endcode default port assignment. As the
* port will later be narrowed to an 16bit unsigned integer \code port & 0xffff
* \endcode the code will connect to port 0 in the end.
*
* @todo As soon as the mysql-server-mock supports authentication failures
* the code can take that into account too.
*/
TEST_P(RouterBootstrapFailoverSuperReadonly2ndNodeDead,
BootstrapFailoverSuperReadonly2ndNodeDead) {
const auto param = GetParam();
const auto dead_port = port_pool_.get_next_available();
std::vector<Config> config{
// member-1, PRIMARY, fails at first write
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join(param.trace_file).str()},
// member-2, unreachable
{"127.0.0.1", dead_port, port_pool_.get_next_available(), "",
/*unaccessible=*/true},
// member-3, succeeds
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join(param.trace_file2).str()},
};
ASSERT_NO_FATAL_FAILURE(bootstrap_failover(
config, param.cluster_type, {}, EXIT_SUCCESS,
{
"^Fetching Cluster Members",
"^Failed connecting to 127\\.0\\.0\\.1:"s +
std::to_string(dead_port) + ": .*, trying next$",
}));
}
INSTANTIATE_TEST_SUITE_P(
BootstrapFailoverSuperReadonly2ndNodeDead,
RouterBootstrapFailoverSuperReadonly2ndNodeDead,
::testing::Values(
BootstrapTestParam{ClusterType::GR_V2, "gr",
"bootstrap_failover_super_read_only_1_gr.js",
"bootstrap_gr.js", ""},
BootstrapTestParam{ClusterType::RS_V2, "ar",
"bootstrap_failover_super_read_only_1_ar.js",
"bootstrap_ar.js", ""},
BootstrapTestParam{ClusterType::GR_V1, "gr_v1",
"bootstrap_failover_super_read_only_1_gr_v1.js",
"bootstrap_gr_v1.js", ""}),
get_test_description);
class RouterBootstrapFailoverPrimaryUnreachable
: public RouterComponentBootstrapTest,
public ::testing::WithParamInterface<BootstrapTestParam> {};
/**
* @test
* verify that bootstrap will fail-over to another node if the initial
* nodes are not writable and the 3rd one is unreachable
* @test
* Group Replication roles:
* - SECONDARY
* - SECONDARY
* - PRIMARY (unreachable)
*/
TEST_P(RouterBootstrapFailoverPrimaryUnreachable,
BootstrapFailoverPrimaryUnreachable) {
const auto param = GetParam();
const auto dead_port = port_pool_.get_next_available();
std::vector<Config> config{
// member-1, fails at first write (SEONDARY)
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join(param.trace_file).str()},
// member-2, fails at first write (SEONDARY)
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join(param.trace_file).str()},
// member-3, unreachable (potential PRIMARY)
{"127.0.0.1", dead_port, port_pool_.get_next_available(), "",
/*unaccessible=*/true},
};
ASSERT_NO_FATAL_FAILURE(bootstrap_failover(
config, param.cluster_type, {}, EXIT_FAILURE,
{"^Fetching Cluster Members",
"^Failed connecting to 127\\.0\\.0\\.1:"s + std::to_string(dead_port) +
": .*, trying next$",
"Error: no more nodes to fail-over too, giving up."}));
}
INSTANTIATE_TEST_SUITE_P(
BootstrapFailoverPrimaryUnreachable,
RouterBootstrapFailoverPrimaryUnreachable,
::testing::Values(
BootstrapTestParam{ClusterType::GR_V2, "gr",
"bootstrap_failover_super_read_only_1_gr.js", "",
""},
BootstrapTestParam{ClusterType::RS_V2, "ar",
"bootstrap_failover_super_read_only_1_ar.js", "",
""},
BootstrapTestParam{ClusterType::GR_V1, "gr_v1",
"bootstrap_failover_super_read_only_1_gr_v1.js", "",
""}),
get_test_description);
class RouterBootstrapFailoverSuperReadonlyCreateAccountFails
: public RouterComponentBootstrapTest,
public ::testing::WithParamInterface<BootstrapTestParam> {};
/**
* @test
* verify that bootstrap fails over and continues if create-account
fails
* due to 1st node not being writable
* @test
* Group Replication roles:
* - SECONDARY
* - PRIMARY
* - SECONDARY (not used)
*/
TEST_P(RouterBootstrapFailoverSuperReadonlyCreateAccountFails,
BootstrapFailoverSuperReadonlyCreateAccountFails) {
const auto param = GetParam();
std::vector<Config> config{
// member-1: SECONDARY, fails at DROP USER due to RW request on RO node
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join(param.trace_file).str()},
// member-2: PRIMARY, succeeds
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join(param.trace_file2).str()},
// member-3: defined, but unused
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(), ""},
};
ASSERT_NO_FATAL_FAILURE(bootstrap_failover(config, param.cluster_type));
}
INSTANTIATE_TEST_SUITE_P(
BootstrapFailoverSuperReadonlyCreateAccountFails,
RouterBootstrapFailoverSuperReadonlyCreateAccountFails,
::testing::Values(
BootstrapTestParam{
ClusterType::GR_V2, "gr",
"bootstrap_failover_super_read_only_dead_2nd_1_gr.js",
"bootstrap_failover_reconfigure_ok.js", ""},
BootstrapTestParam{
ClusterType::RS_V2, "ar",
"bootstrap_failover_super_read_only_dead_2nd_1_ar.js",
"bootstrap_failover_reconfigure_ok.js", ""},
BootstrapTestParam{
ClusterType::GR_V1, "gr_v1",
"bootstrap_failover_super_read_only_dead_2nd_1_gr_v1.js",
"bootstrap_failover_reconfigure_ok_v1.js", ""}),
get_test_description);
class RouterBootstrapFailoverSuperReadonlyCreateAccountGrantFails
: public RouterComponentBootstrapTest,
public ::testing::WithParamInterface<BootstrapTestParam> {};
/**
* @test
* verify that bootstrap DOES NOT fail over if create-account GRANT fails
* @test
* Group Replication roles:
* - SECONDARY
* - (not used)
* - (not used)
*
*/
TEST_P(RouterBootstrapFailoverSuperReadonlyCreateAccountGrantFails,
BootstrapFailoverSuperReadonlyCreateAccountGrantFails) {
std::vector<Config> config{
// member-1: SECONDARY fails and exits after GRANT
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join(GetParam().trace_file).str()},
// member-2: defined, but unused
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(), ""},
// member-3: defined, but unused
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(), ""},
};
ASSERT_NO_FATAL_FAILURE(bootstrap_failover(
config, GetParam().cluster_type, {}, EXIT_FAILURE,
{"Error: Error creating MySQL account for router \\(GRANTs stage\\): "
"Error executing MySQL query \"GRANT SELECT, EXECUTE ON "
"mysql_innodb_cluster_metadata.*\": The MySQL server is running with "
"the --super-read-only option so it cannot execute this statement"}));
}
INSTANTIATE_TEST_SUITE_P(
BootstrapFailoverSuperReadonlyCreateAccountGrantFails,
RouterBootstrapFailoverSuperReadonlyCreateAccountGrantFails,
::testing::Values(
BootstrapTestParam{ClusterType::GR_V2, "gr",
"bootstrap_failover_at_grant_gr.js", "", ""},
BootstrapTestParam{ClusterType::RS_V2, "ar",
"bootstrap_failover_at_grant_ar.js", "", ""},
BootstrapTestParam{ClusterType::GR_V1, "gr_v1",
"bootstrap_failover_at_grant_gr_v1.js", "", ""}),
get_test_description);
/**
* @test
* verify that bootstrapping via a unix-socket fails over to the
* IP-addresses of the members
* @test
* Group Replication roles:
* - SECONDARY
* - PRIMARY
* - SECONDARY (not used)
* @test
* Initial connect via unix-socket to the 1st node, all further connects
* via TCP/IP
*
* @todo needs unix-socket support in the mock-server
*/
TEST_F(RouterBootstrapTest, DISABLED_BootstrapFailoverSuperReadonlyFromSocket) {
std::vector<Config> mock_servers{
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join("bootstrap_failover_super_read_only_1.js").str()},
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join("bootstrap_gr.js").str()},
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(), ""},
};
std::vector<std::string> router_options = {
"--bootstrap=localhost", "--bootstrap-socket=" + mock_servers.at(0).ip,
"-d", bootstrap_dir.name()};
ASSERT_NO_FATAL_FAILURE(bootstrap_failover(
mock_servers, ClusterType::GR_V2, router_options, EXIT_FAILURE,
{"Error: Error executing MySQL query: Lost connection to "
"MySQL server during query \\(2013\\)"}));
}
class RouterBootstrapFailoverSuperReadonlyNewPrimaryCrash
: public RouterComponentBootstrapTest,
public ::testing::WithParamInterface<BootstrapTestParam> {};
/**
* @test
* verify that bootstrap fails over if PRIMARY crashes while bootstrapping
*
* @test
* Group Replication roles:
* - SECONDARY
* - PRIMARY (crashing)
* - PRIMARY
*/
TEST_P(RouterBootstrapFailoverSuperReadonlyNewPrimaryCrash,
BootstrapFailoverSuperReadonlyNewPrimaryCrash) {
std::vector<Config> mock_servers{
// member-1: PRIMARY, fails at DROP USER
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join(GetParam().trace_file).str()},
// member-2: PRIMARY, but crashing
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join(GetParam().trace_file2).str()},
// member-3: newly elected PRIMARY, succeeds
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join(GetParam().trace_file3).str()},
};
ASSERT_NO_FATAL_FAILURE(
bootstrap_failover(mock_servers, GetParam().cluster_type));
}
INSTANTIATE_TEST_SUITE_P(
BootstrapFailoverSuperReadonlyNewPrimaryCrash,
RouterBootstrapFailoverSuperReadonlyNewPrimaryCrash,
::testing::Values(
BootstrapTestParam{
ClusterType::GR_V2, "gr",
"bootstrap_failover_super_read_only_dead_2nd_1_gr.js",
"bootstrap_failover_at_crash.js",
"bootstrap_failover_reconfigure_ok.js"},
BootstrapTestParam{
ClusterType::RS_V2, "ar",
"bootstrap_failover_super_read_only_dead_2nd_1_ar.js",
"bootstrap_failover_at_crash.js",
"bootstrap_failover_reconfigure_ok.js"},
BootstrapTestParam{
ClusterType::GR_V1, "gr_v1",
"bootstrap_failover_super_read_only_dead_2nd_1_gr_v1.js",
"bootstrap_failover_at_crash_v1.js",
"bootstrap_failover_reconfigure_ok_v1.js"}),
get_test_description);
/**
* @test
* This test proves that bootstrap will not print out the success message
* ("MySQL Router configured for the InnoDB cluster 'mycluster'" and many lines
* that follow it) until entire bootstrap succeeds.
*
* At the time of writing, the last operation that bootstrap performs is
* writing a config file and backing up the old one. Therefore we use that
* as the basis of assessing the above expectation is met.
*/
TEST_F(RouterBootstrapTest,
bootstrap_report_not_shown_until_bootstrap_succeeds) {
TempDirectory bootstrap_directory;
// create config files
const Path bs_dir(bootstrap_directory.name());
const std::string config_file = bs_dir.join("mysqlrouter.conf").str();
const std::string config_bak_file = bs_dir.join("mysqlrouter.conf.bak").str();
{
std::ofstream f1(config_file);
std::ofstream f2(config_bak_file);
// contents must be different, otherwise a backup will not be attempted
f1 << "[DEFAULT]\nkey1=val1\n";
f2 << "[DEFAULT]\nkey2=val2\n";
}
// make config backup file RO to trigger the error
#ifdef _WIN32
EXPECT_EQ(_chmod(config_bak_file.c_str(), S_IREAD), 0);
#else
EXPECT_EQ(chmod(config_bak_file.c_str(), S_IRUSR), 0);
#endif
// launch mock server that is our metadata server for the bootstrap
const uint16_t server_port = port_pool_.get_next_available();
const std::string json_stmts =
get_data_dir()
.join("bootstrap_report_host.js")
.str(); // we piggy back on existing .js to avoid creating a new one
auto &server_mock =
launch_mysql_server_mock(json_stmts, server_port, EXIT_SUCCESS, false);
// launch the router in bootstrap mode
const std::vector<std::string> cmdline = {
"--bootstrap=127.0.0.1:" + std::to_string(server_port), "-d",
bootstrap_directory.name()};
auto &router = launch_router_for_bootstrap(cmdline, EXIT_FAILURE);
check_exit_code(router, EXIT_FAILURE);
// expect config write error
EXPECT_THAT(router.get_full_output(),
::testing::ContainsRegex("Error: Could not create file "
"'.*/mysqlrouter.conf.bak'"));
// expect that the bootstrap success message (bootstrap report) is not
// displayed
EXPECT_THAT(router.get_full_output(), ::testing::Not(::testing::HasSubstr(
"MySQL Router configured for the "
"InnoDB cluster 'mycluster'")));
server_mock.kill();
}
/**
* @test
* verify connection times at bootstrap can be configured
*/
TEST_F(RouterBootstrapTest,
BootstrapSucceedWhenServerResponseLessThanReadTimeout) {
std::vector<Config> mock_servers{
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join("bootstrap_exec_time_2_seconds.js").str()},
};
std::vector<std::string> router_options = {
"--bootstrap=" + mock_servers.at(0).ip + ":" +
std::to_string(mock_servers.at(0).port),
"-d", bootstrap_dir.name(), "--connect-timeout=3", "--read-timeout=3"};
ASSERT_NO_FATAL_FAILURE(bootstrap_failover(mock_servers, ClusterType::GR_V2,
router_options, EXIT_SUCCESS, {}));
}
TEST_F(RouterBootstrapTest, BootstrapAccessErrorAtGrantStatement) {
std::vector<Config> config{
// member-1: PRIMARY, fails after GRANT
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join("bootstrap_access_error_at_grant.js").str()},
// member-2: defined, but unused
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(), ""},
// member-3: defined, but unused
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(), ""},
};
ASSERT_NO_FATAL_FAILURE(
bootstrap_failover(config, ClusterType::GR_V2, {}, EXIT_FAILURE,
{"Access denied for user 'native'@'%' to database "
"'mysql_innodb_cluster_metadata"}));
}
class RouterBootstrapBootstrapNoGroupReplicationSetup
: public RouterComponentBootstrapTest,
public ::testing::WithParamInterface<BootstrapTestParam> {};
/**
* @test
* ensure a reasonable error message if schema exists, but no
* group-replication is setup.
*/
TEST_P(RouterBootstrapBootstrapNoGroupReplicationSetup,
BootstrapNoGroupReplicationSetup) {
const auto param = GetParam();
std::vector<Config> config{
// member-1: schema exists, but no group replication configured
{
"127.0.0.1",
port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join(param.trace_file).str(),
},
};
ASSERT_NO_FATAL_FAILURE(
bootstrap_failover(config, param.cluster_type, {}, EXIT_FAILURE,
{"to have Group Replication running"}));
}
INSTANTIATE_TEST_SUITE_P(
BootstrapNoGroupReplicationSetup,
RouterBootstrapBootstrapNoGroupReplicationSetup,
::testing::Values(BootstrapTestParam{ClusterType::GR_V2, "gr",
"bootstrap_no_gr.js", "", ""},
BootstrapTestParam{ClusterType::GR_V1, "gr_v1",
"bootstrap_no_gr_v1.js", "", ""}),
get_test_description);
/**
* @test
* ensure a reasonable error message if metadata schema does not exist.
*/
TEST_F(RouterBootstrapTest, BootstrapNoMetadataSchema) {
std::vector<Config> config{
// member-1: no metadata schema
{
"127.0.0.1",
port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join("bootstrap_no_schema.js").str(),
},
};
ASSERT_NO_FATAL_FAILURE(
bootstrap_failover(config, ClusterType::GR_V2, {}, EXIT_FAILURE,
{"to contain the metadata of MySQL InnoDB Cluster"}));
}
/**
* @test
* verify connection times at bootstrap can be configured
*/
TEST_F(RouterBootstrapTest, BootstrapFailWhenServerResponseExceedsReadTimeout) {
std::vector<Config> mock_servers{
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join("bootstrap_exec_time_2_seconds.js").str()},
};
std::vector<std::string> router_options = {
"--bootstrap=" + mock_servers.at(0).ip + ":" +
std::to_string(mock_servers.at(0).port),
"-d", bootstrap_dir.name(), "--connect-timeout=1", "--read-timeout=1"};
ASSERT_NO_FATAL_FAILURE(bootstrap_failover(
mock_servers, ClusterType::GR_V2, router_options, EXIT_FAILURE,
{"Error: Error executing MySQL query \".*\": Lost connection to "
"MySQL server during query \\(2013\\)"}));
}
/**
* @test
* verify that bootstrap succeeds when master key writer is used
*
*/
TEST_F(RouterBootstrapTest,
NoMasterKeyFileWhenBootstrapPassWithMasterKeyReader) {
std::vector<Config> config{
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join("bootstrap_gr.js").str()},
};
ScriptGenerator script_generator(ProcessManager::get_origin(),
get_test_temp_dir_name());
std::vector<std::string> router_options = {
"--bootstrap=" + config.at(0).ip + ":" +
std::to_string(config.at(0).port),
"-d", bootstrap_dir.name(),
"--master-key-reader=" + script_generator.get_reader_script(),
"--master-key-writer=" + script_generator.get_writer_script()};
ASSERT_NO_FATAL_FAILURE(
bootstrap_failover(config, ClusterType::GR_V2, router_options));
Path tmp(bootstrap_dir.name());
Path master_key_file(tmp.join("mysqlrouter.key").str());
ASSERT_FALSE(master_key_file.exists());
Path keyring_file(tmp.join("data").join("keyring").str());
ASSERT_TRUE(keyring_file.exists());
Path dir(get_test_temp_dir_name());
Path data_file(dir.join("master_key").str());
ASSERT_TRUE(data_file.exists());
}
/**
* @test
* verify that master key file is not overridden by subsequent bootstrap.
*/
TEST_F(RouterBootstrapTest, MasterKeyFileNotChangedAfterSecondBootstrap) {
mysql_harness::mkdir(Path(bootstrap_dir.name()).str(), 0777);
mysql_harness::mkdir(Path(bootstrap_dir.name()).join("data").str(), 0777);
const std::string master_key_path =
Path(bootstrap_dir.name()).real_path().join("mysqlrouter.key").str();
const std::string keyring_path =
Path(bootstrap_dir.name()).real_path().join("data").join("keyring").str();
SCOPED_TRACE("// create the keyrings manually.");
auto &proc = launch_command(get_origin().join("mysqlrouter_keyring").str(),
{
"init",
keyring_path,
"--master-key-file",
master_key_path,
});
ASSERT_NO_THROW(proc.wait_for_exit());
// remember the initially generated master-key
const auto master_key = get_file_output(master_key_path);
SCOPED_TRACE("// bootstrap.");
std::vector<Config> mock_servers{
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(),
get_data_dir().join("bootstrap_gr.js").str()},
};
std::vector<std::string> router_options = {
"--bootstrap=" + mock_servers.at(0).ip + ":" +
std::to_string(mock_servers.at(0).port),
"-d", bootstrap_dir.name(), "--force"};
ASSERT_NO_FATAL_FAILURE(bootstrap_failover(mock_servers, ClusterType::GR_V2,
router_options, EXIT_SUCCESS, {}));
SCOPED_TRACE("// check master-key-file doesn't change after bootstrap.");
ASSERT_THAT(master_key, testing::Eq(get_file_output(master_key_path)));
}
struct UseGrNotificationTestParams {
std::vector<std::string> bootstrap_params;
std::vector<std::string> expected_config_lines;
mysqlrouter::MetadataSchemaVersion metadata_schema_version;
};
class ConfUseGrNotificationParamTest
: public RouterBootstrapTest,
public ::testing::WithParamInterface<UseGrNotificationTestParams> {};
/**
* @test
* verify that using --conf-use-gr-notifications creates proper config
* file entry.
*/
TEST_P(ConfUseGrNotificationParamTest, ConfUseGrNotificationParam) {
const auto server_port = port_pool_.get_next_available();
const auto server_x_port = port_pool_.get_next_available();
const auto http_port = port_pool_.get_next_available();
const std::string json_stmts = get_data_dir().join("bootstrap_gr.js").str();
// launch mock server that is our metadata server for the bootstrap
auto &server_mock = launch_mysql_server_mock(json_stmts, server_port,
EXIT_SUCCESS, false, http_port);
set_mock_bootstrap_data(http_port, "test", {{"localhost", server_port}},
GetParam().metadata_schema_version,
"cluster-specific-id");
const auto router_port_rw = port_pool_.get_next_available();
const auto router_port_ro = port_pool_.get_next_available();
const auto router_port_x_rw = port_pool_.get_next_available();
const auto router_port_x_ro = port_pool_.get_next_available();
std::vector<std::string> bootstrap_params{
"--bootstrap=127.0.0.1:" + std::to_string(server_port),
"-d",
bootstrap_dir.name(),
"--conf-set-option=routing:bootstrap_rw.bind_port=" +
std::to_string(router_port_rw),
"--conf-set-option=routing:bootstrap_ro.bind_port=" +
std::to_string(router_port_ro),
"--conf-set-option=routing:bootstrap_x_rw.bind_port=" +
std::to_string(router_port_x_rw),
"--conf-set-option=routing:bootstrap_x_ro.bind_port=" +
std::to_string(router_port_x_ro)};
bootstrap_params.insert(bootstrap_params.end(),
GetParam().bootstrap_params.begin(),
GetParam().bootstrap_params.end());
// launch the router in bootstrap mode
auto &router = launch_router_for_bootstrap(bootstrap_params);
check_exit_code(router, EXIT_SUCCESS);
const std::string conf_file = bootstrap_dir.name() + "/mysqlrouter.conf";
// check if valid config option was added to the file
auto conf_file_content = get_file_output(conf_file);
auto conf_lines = mysql_harness::split_string(conf_file_content, '\n');
EXPECT_THAT(conf_lines,
::testing::IsSupersetOf(GetParam().expected_config_lines));
server_mock.send_clean_shutdown_event();
EXPECT_NO_THROW(server_mock.wait_for_exit());
auto plugin_dir = mysql_harness::get_plugin_dir(get_origin().str());
ASSERT_TRUE(add_line_to_config_file(conf_file, "DEFAULT", "plugin_folder",
plugin_dir));
const std::string runtime_json_stmts =
get_data_dir().join("metadata_dynamic_nodes_v2_gr.js").str();
// launch mock server that is our metadata server
launch_mysql_server_mock(runtime_json_stmts, server_port, EXIT_SUCCESS, false,
http_port);
set_mock_metadata(http_port, "cluster-specific-id",
{GRNode{server_port, "uuid-1"}}, 0,
{ClusterNode{server_port, "uuid-1", server_x_port}});
// check that the Router accepts the config file
auto &router2 = launch_router({"-c", conf_file});
router2.set_logging_path(bootstrap_dir.name() + "/log", "mysqlrouter.log");
}
INSTANTIATE_TEST_SUITE_P(
ConfUseGrNotificationParam, ConfUseGrNotificationParamTest,
::testing::Values(
// 0, 1) --conf-use-gr-notifications with no param
UseGrNotificationTestParams{{"--conf-use-gr-notifications"},
{"use_gr_notifications=1", "ttl=60",
"auth_cache_refresh_interval=60"},
{2, 0, 3}},
UseGrNotificationTestParams{{"--conf-use-gr-notifications"},
{"use_gr_notifications=1", "ttl=60",
"auth_cache_refresh_interval=60"},
{2, 1, 0}},
// 2, 3) --conf-use-gr-notifications=1
// [@FR5.2.2]
UseGrNotificationTestParams{{"--conf-use-gr-notifications=1"},
{"use_gr_notifications=1", "ttl=60",
"auth_cache_refresh_interval=60"},
{2, 0, 3}},
UseGrNotificationTestParams{{"--conf-use-gr-notifications=1"},
{"use_gr_notifications=1", "ttl=60",
"auth_cache_refresh_interval=60"},
{2, 1, 0}},
// 4, 5) no --conf-use-gr-notifications param
UseGrNotificationTestParams{{},
{"use_gr_notifications=0", "ttl=0.5",
"auth_cache_refresh_interval=2"},
{2, 0, 3}},
UseGrNotificationTestParams{{},
{"use_gr_notifications=0", "ttl=0.5",
"auth_cache_refresh_interval=2"},
{2, 1, 0}},
// 6, 7) --conf-use-gr-notification=0
// [@FR5.2.1]
UseGrNotificationTestParams{{"--conf-use-gr-notifications=0"},
{"use_gr_notifications=0", "ttl=0.5",
"auth_cache_refresh_interval=2"},
{2, 0, 3}},
UseGrNotificationTestParams{{"--conf-use-gr-notifications=0"},
{"use_gr_notifications=0", "ttl=0.5",
"auth_cache_refresh_interval=2"},
{2, 1, 0}}));
class ErrorReportTest : public RouterComponentBootstrapTest {};
/**
* @test
* verify that --conf-use-gr-notifications used with no bootstrap
* causes proper error report
*/
TEST_F(ErrorReportTest, ConfUseGrNotificationsNoBootstrap) {
auto &router = launch_router_for_bootstrap({"--conf-use-gr-notifications"},
EXIT_FAILURE);
EXPECT_NO_THROW(router.wait_for_exit());
EXPECT_THAT(
router.get_full_output(),
::testing::HasSubstr("Error: Option --conf-use-gr-notifications can only "
"be used together with -B/--bootstrap"));
check_exit_code(router, EXIT_FAILURE);
}
class ConfUseGrNotificationWrongValueParamTest
: public RouterBootstrapTest,
public ::testing::WithParamInterface<std::string> {};
/**
* @test
* verify that --conf-use-gr-notifications used with value other than 0
* and 1 causes proper error report
* [@FR5.2.4]
*/
TEST_P(ConfUseGrNotificationWrongValueParamTest,
ConfUseGrNotificationWrongValueParam) {
auto &router = launch_router_for_bootstrap(
{"-B", "somehost:12345", "--conf-use-gr-notifications=" + GetParam()},
EXIT_FAILURE);
EXPECT_NO_THROW(router.wait_for_exit());
EXPECT_THAT(router.get_full_output(),
::testing::HasSubstr(
"Error: Value for parameter '--conf-use-gr-notifications' "
"needs to be one of: ['0', '1']"));
check_exit_code(router, EXIT_FAILURE);
}
INSTANTIATE_TEST_SUITE_P(ConfUseGrNotificationWrongValueParam,
ConfUseGrNotificationWrongValueParamTest,
::testing::Values("2", "true", "false", "N/A", "yes",
"no"));
/**
* @test
* verify that running bootstrap with -d with dir that already exists and
* is not empty gives an appropriate error to the user; particularly it
* should mention:
* - directory name
* - error type (it's not empty)
*/
TEST_F(ErrorReportTest, bootstrap_dir_exists_and_is_not_empty) {
const std::string json_stmts = get_data_dir().join("bootstrap_gr.js").str();
const uint16_t server_port = port_pool_.get_next_available();
TempDirectory bootstrap_directory;
// populate bootstrap dir with a file, so it's not empty
EXPECT_NO_THROW({
mysql_harness::Path path =
mysql_harness::Path(bootstrap_directory.name()).join("some_file");
std::ofstream of(path.str());
of << "blablabla";
});
// launch the router in bootstrap mode
auto &router = launch_router_for_bootstrap(
{
"--bootstrap=127.0.0.1:" + std::to_string(server_port),
"--connect-timeout=1",
"-d",
bootstrap_directory.name(),
},
EXIT_FAILURE);
// verify that appropriate message was logged (first line) and error message
// printed (last line)
std::string err_msg = "Directory '" + bootstrap_directory.name() +
"' already contains files\n"
"Error: Directory already exits";
check_exit_code(router, EXIT_FAILURE);
}
TEST_F(ErrorReportTest, bootstrap_conf_base_port_hex) {
const std::string json_stmts = get_data_dir().join("bootstrap_gr.js").str();
const uint16_t server_port = port_pool_.get_next_available();
TempDirectory bootstrap_directory;
// launch the router in bootstrap mode
auto &router = launch_router_for_bootstrap(
{
"--bootstrap", "127.0.0.1:" + std::to_string(server_port), //
"--connect-timeout", "1", //
"--conf-base-port", "0x0", //
"-d", bootstrap_directory.name(), //
},
EXIT_FAILURE);
check_exit_code(router, EXIT_FAILURE);
EXPECT_THAT(router.get_full_output(),
::testing::HasSubstr("--conf-base-port needs value between 0 and "
"65532 inclusive, was '0x0'"));
}
// unfortunately it's not (reasonably) possible to make folders read-only on
// Windows, therefore we can run the following tests only on Unix
//
// https://support.microsoft.com/en-us/help/326549/you-cannot-view-or-change-the-read-only-or-the-system-attributes-of-fo
#ifndef _WIN32
/**
* @test
* verify that running bootstrap with -d with dir that already exists but
* is inaccessible gives an appropriate error to the user; particularly it
* should mention:
* - directory name
* - error type (permission denied)
* - suggests AppArmor config might be at fault
*/
TEST_F(ErrorReportTest, bootstrap_dir_exists_but_is_inaccessible) {
const std::string json_stmts = get_data_dir().join("bootstrap_gr.js").str();
const uint16_t server_port = port_pool_.get_next_available();
TempDirectory bootstrap_directory;
std::shared_ptr<void> exit_guard(nullptr, [&](void *) {
chmod(bootstrap_directory.name().c_str(),
S_IRUSR | S_IWUSR | S_IXUSR); // restore RWX for owner
});
// make bootstrap directory inaccessible to trigger the error
EXPECT_EQ(chmod(bootstrap_directory.name().c_str(), 0), 0);
// launch the router in bootstrap mode: -d set to existing but inaccessible
// dir
auto &router = launch_router_for_bootstrap(
{
"--bootstrap=127.0.0.1:" + std::to_string(server_port),
"--connect-timeout=1",
"-d",
bootstrap_directory.name(),
},
EXIT_FAILURE);
// verify that appropriate message was logged (all but last) and error message
// printed (last line)
std::string err_msg =
"Failed to open directory '.*" + bootstrap_directory.name() +
"': Permission denied\n"
"This may be caused by insufficient rights or AppArmor settings.\n.*"
"Error: Could not check contents of existing deployment directory";
check_exit_code(router, EXIT_FAILURE);
}
/**
* @test
* verify that running bootstrap with -d with dir that doesn't exists and
* cannot be created gives an appropriate error to the user; particularly
* it should mention:
* - directory name
* - error type (permission denied)
* - suggests AppArmor config might be at fault
*/
TEST_F(ErrorReportTest,
bootstrap_dir_does_not_exist_and_is_impossible_to_create) {
const std::string json_stmts = get_data_dir().join("bootstrap_gr.js").str();
const uint16_t server_port = port_pool_.get_next_available();
TempDirectory bootstrap_superdir;
std::shared_ptr<void> exit_guard(nullptr, [&](void *) {
chmod(bootstrap_superdir.name().c_str(),
S_IRUSR | S_IWUSR | S_IXUSR); // restore RWX for owner
});
// make bootstrap directory inaccessible to trigger the error
EXPECT_EQ(chmod(bootstrap_superdir.name().c_str(), 0), 0);
// launch the router in bootstrap mode: -d set to non-existent dir and
// impossible to create
std::string bootstrap_directory =
mysql_harness::Path(bootstrap_superdir.name()).join("subdir").str();
auto &router = launch_router_for_bootstrap(
{
"--bootstrap=127.0.0.1:" + std::to_string(server_port),
"--connect-timeout=1",
"-d",
bootstrap_directory,
},
EXIT_FAILURE);
// verify that appropriate message was logged (all but last) and error message
// printed (last line)
std::string err_msg =
"Cannot create directory '" + bootstrap_directory +
"': Permission denied\n"
"This may be caused by insufficient rights or AppArmor settings.\n.*"
"Error: Could not create deployment directory";
check_exit_code(router, EXIT_FAILURE);
}
#endif
/**
* @test
* verify that using --conf-use-gr-notifications creates proper error when
* the cluster type is ReplicaSet.
*/
TEST_F(ErrorReportTest, ConfUseGrNotificationsAsyncReplicaset) {
TempDirectory bootstrap_directory;
const auto server_port = port_pool_.get_next_available();
const std::string json_stmts = get_data_dir().join("bootstrap_ar.js").str();
// launch mock server that is our metadata server for the bootstrap
launch_mysql_server_mock(json_stmts, server_port, EXIT_SUCCESS, false);
// launch the router in bootstrap mode
auto &router = launch_router_for_bootstrap(
{"--bootstrap=127.0.0.1:" + std::to_string(server_port), "-d",
bootstrap_directory.name(), "--conf-use-gr-notifications"},
EXIT_FAILURE);
EXPECT_NO_THROW(router.wait_for_exit());
EXPECT_THAT(
router.get_full_output(),
::testing::HasSubstr("Error: The parameter 'use-gr-notifications' is "
"valid only for GR cluster type"));
check_exit_code(router, EXIT_FAILURE);
}
/**
* @test
* verify that trying to register that is not unique in the metadata
* gives expected results.
*/
TEST_F(RouterBootstrapTest, BootstrapRouterDuplicateEntry) {
TempDirectory bootstrap_directory;
const auto server_port = port_pool_.get_next_available();
const auto bootstrap_server_port = port_pool_.get_next_available();
// const auto server_http_port = port_pool_.get_next_available();
const auto bootstrap_server_http_port = port_pool_.get_next_available();
const std::string json_stmts =
get_data_dir().join("bootstrap_gr_dup_router.js").str();
// launch mock server that is our metadata server for the bootstrap
// auto &server_mock =
launch_mysql_server_mock(json_stmts, bootstrap_server_port, EXIT_SUCCESS,
false, bootstrap_server_http_port);
set_mock_bootstrap_data(bootstrap_server_http_port, "test",
{{"127.0.0.1", server_port}}, {2, 0, 3},
"cluster-specific-id");
// launch the router in bootstrap mode
auto &router = launch_router_for_bootstrap(
{"--bootstrap=127.0.0.1:" + std::to_string(bootstrap_server_port), "-d",
bootstrap_directory.name()},
EXIT_FAILURE);
check_exit_code(router, EXIT_FAILURE);
// there should be an errors about duplicate router entry
EXPECT_TRUE(router.expect_output(
"Error: It appears that a router instance named '' has been previously "
"configured in this host. If that instance no longer exists, use the "
"--force option to overwrite it.",
false, 0ms));
// there should be no errors about not being able to remove dirs nor files
EXPECT_FALSE(
router.expect_output("Could not delete directory .*", true, 0ms));
EXPECT_FALSE(router.expect_output("Could not delete file .*", true, 0ms));
}
TEST_F(RouterBootstrapTest, CheckAuthBackendWhenOldMetadata) {
TempDirectory bootstrap_directory;
const auto server_port = port_pool_.get_next_available();
const auto http_port = port_pool_.get_next_available();
const std::string json_stmts =
get_data_dir().join("bootstrap_gr_v1.js").str();
// launch mock server that is our metadata server for the bootstrap
launch_mysql_server_mock(json_stmts, server_port, EXIT_SUCCESS, false,
http_port);
set_mock_bootstrap_data(http_port, "test", {{"localhost", server_port}},
{1, 0, 0}, "cluster-specific-id");
const auto base_listening_port = port_pool_.get_next_available();
std::vector<std::string> bootsrtap_params{
"--bootstrap=127.0.0.1:" + std::to_string(server_port), "-d",
bootstrap_directory.name(),
"--conf-base-port=" + std::to_string(base_listening_port)};
// launch the router in bootstrap mode
auto &router = launch_router_for_bootstrap(bootsrtap_params, EXIT_SUCCESS,
/*disable rest*/ false);
check_exit_code(router, EXIT_SUCCESS);
const std::string conf_file =
bootstrap_directory.name() + "/mysqlrouter.conf";
// check if valid authentication backend option was added to the config file
auto conf_file_content = get_file_output(conf_file);
auto conf_lines = mysql_harness::split_string(conf_file_content, '\n');
const auto passwd_file = mysql_harness::Path{
bootstrap_directory.name() + "/data/auth_backend_passwd_file"};
EXPECT_THAT(conf_lines,
::testing::IsSupersetOf(
{::testing::ContainsRegex("backend=file"),
::testing::ContainsRegex(std::string{"filename=.*"} +
passwd_file.str())}));
ASSERT_TRUE(passwd_file.exists());
}
/**
* @test
* verify that trying to register Router that is not unique in the
* metadata with --force parameter gives expected results.
*/
TEST_F(RouterBootstrapTest, BootstrapRouterDuplicateEntryOverwrite) {
TempDirectory bootstrap_directory;
const auto bootstrap_server_port = port_pool_.get_next_available();
// const auto server_http_port = port_pool_.get_next_available();
const auto bootstrap_server_http_port = port_pool_.get_next_available();
const std::string json_stmts =
get_data_dir().join("bootstrap_gr_dup_router.js").str();
// launch mock server that is our metadata server for the bootstrap
launch_mysql_server_mock(json_stmts, bootstrap_server_port, EXIT_SUCCESS,
false, bootstrap_server_http_port);
set_mock_metadata(bootstrap_server_http_port, "cluster-specific-id",
classic_ports_to_gr_nodes({bootstrap_server_port}), 0,
{bootstrap_server_port});
// launch the router in bootstrap mode
auto &router = launch_router_for_bootstrap(
{"--bootstrap=127.0.0.1:" + std::to_string(bootstrap_server_port), "-d",
bootstrap_directory.name(), "--force"},
EXIT_SUCCESS);
check_exit_code(router, EXIT_SUCCESS);
}
/**
* @test
* verify that Router creates an account even if the router_id
* AUTOINCREMENT value is high
*/
TEST_F(RouterBootstrapTest, BootstrapRouterRouterIdMax) {
TempDirectory bootstrap_directory;
const auto server_port = port_pool_.get_next_available();
// const auto server_http_port = port_pool_.get_next_available();
const auto http_port = port_pool_.get_next_available();
const std::string json_stmts = get_data_dir().join("bootstrap_gr.js").str();
// launch mock server that is our metadata server for the bootstrap
launch_mysql_server_mock(json_stmts, server_port, EXIT_SUCCESS, false,
http_port);
set_mock_metadata(http_port, "cluster-specific-id",
classic_ports_to_gr_nodes({server_port}), 0, {server_port});
{
std::string server_globals =
MockServerRestClient(http_port).get_globals_as_json_string();
JsonDocument globals;
if (globals.Parse<0>(server_globals.c_str()).HasParseError()) {
FAIL() << "Failed parsing mock server globals";
}
JsonAllocator allocator;
// mimic the highiest possible router_id (2^32-1)
globals.AddMember("last_insert_id", std::numeric_limits<uint32_t>::max(),
allocator);
server_globals = json_to_string(globals);
MockServerRestClient(http_port).set_globals(server_globals);
}
// launch the router in bootstrap mode
auto &router = launch_router_for_bootstrap(
{"--bootstrap=127.0.0.1:" + std::to_string(server_port), "-d",
bootstrap_directory.name()},
EXIT_SUCCESS);
// the bootstrap should be fine even with the router_id that high
check_exit_code(router, EXIT_SUCCESS);
}
class ConfSetOptionTest : public RouterBootstrapTest {};
/**
* @test
* verify that using --conf-set-option for not bootstrap gives a proper
* error
*/
TEST_F(ConfSetOptionTest, ErrorIfNotBootstrap) {
const std::string tracefile = "bootstrap_gr.js";
std::vector<std::string> cmdline = {
"--conf-set-option=DEFAULT.max_total_connections=1024"};
auto &router = launch_router_for_bootstrap(cmdline, EXIT_FAILURE);
check_exit_code(router, EXIT_FAILURE);
// let's check if the expected error was reported:
EXPECT_THAT(
router.get_full_output(),
::testing::ContainsRegex("Error: Option --conf-set-option can only be "
"used together with -B/--bootstrap"));
}
/**
* @test
* verify that the --conf-set-option bootstrap parameter is handled
* properly when used to set bind port of each route along with other config
* options
*/
TEST_F(ConfSetOptionTest, MultipleConfOptionsSet) {
const std::string tracefile = "bootstrap_gr.js";
std::vector<Config> mock_servers{
{"127.0.0.1", port_pool_.get_next_available(),
port_pool_.get_next_available(), get_data_dir().join(tracefile).str()},
};
// mysqlrouter -B ...
// --conf-set-option=routing:bootstrap_rw.bind_port=A -
// --conf-set-option=routing:bootstrap_ro.bind_port=B
// --conf-set-option=routing:bootstrap_x_rw.bind_port=C
// --conf-set-option=routing:bootstrap_x_ro.bind_port=D
// --conf-set-option=logger.level=DEBUG
// --conf-set-option=DEFAULT.read_timeout=50
// --conf-set-option=DEFAULT.connect_timeout=38
// --conf-set-option=DEFAULT.unknown_config_option=warning
const uint16_t classic_rw_port = 1234;
const uint16_t classic_ro_port = 2345;
const uint16_t x_rw_port = 2222;
const uint16_t x_ro_port = 3333;
const std::string log_level = "DEBUG";
const int read_tout = 50;
const int connect_tout = 38;
std::vector<std::string> cmdline = {
"--bootstrap=" + mock_servers.at(0).ip + ":" +
std::to_string(mock_servers.at(0).port),
"-d",
bootstrap_dir.name(),
"--conf-set-option=routing:bootstrap_rw.bind_port=" +
std::to_string(classic_rw_port),
"--conf-set-option=routing:bootstrap_ro.bind_port=" +
std::to_string(classic_ro_port),
"--conf-set-option=routing:bootstrap_x_rw.bind_port=" +
std::to_string(x_rw_port),
"--conf-set-option=routing:bootstrap_x_ro.bind_port=" +
std::to_string(x_ro_port),
"--conf-set-option=logger.level=" + log_level,
"--conf-set-option=DEFAULT.read_timeout=" + std::to_string(read_tout),
"--conf-set-option=DEFAULT.connect_timeout=" +
std::to_string(connect_tout),
"--conf-set-option=DEFAULT.unknown_config_option=warning"};
ASSERT_NO_FATAL_FAILURE(
bootstrap_failover(mock_servers, ClusterType::GR_V2, cmdline));
// 'config_file' is set as side-effect of bootstrap_failover()
ASSERT_THAT(config_file, ::testing::Not(::testing::IsEmpty()));
// let's check if the actual config file contains what we expect:
const std::string config_file_str = get_file_output(config_file);
// classic RW
check_bind_port(config_file_str, "bootstrap_rw", "classic", "PRIMARY",
classic_rw_port);
// classic RO
check_bind_port(config_file_str, "bootstrap_ro", "classic", "SECONDARY",
classic_ro_port);
// x RW
check_bind_port(config_file_str, "bootstrap_x_rw", "x", "PRIMARY", x_rw_port);
// x RO
check_bind_port(config_file_str, "bootstrap_x_ro", "x", "SECONDARY",
x_ro_port);
EXPECT_TRUE(config_file_contains(config_file_str, "level=" + log_level))
<< config_file_str;
EXPECT_TRUE(config_file_contains(config_file_str,
"read_timeout=" + std::to_string(read_tout)))
<< config_file_str;
EXPECT_TRUE(config_file_contains(
config_file_str, "connect_timeout=" + std::to_string(connect_tout)))
<< config_file_str;
EXPECT_TRUE(
config_file_contains(config_file_str, "unknown_config_option=warning"))
<< config_file_str;
EXPECT_FALSE(
config_file_contains(config_file_str, "unknown_config_option=error"))
<< config_file_str;
}
struct ConfSetOptionErrorTestParam {
std::vector<std::string> con_set_option_params;
std::string expected_error;
};
class ConfSetOptionErrorTest
: public ConfSetOptionTest,
public ::testing::WithParamInterface<ConfSetOptionErrorTestParam> {};
TEST_P(ConfSetOptionErrorTest, ErrorTest) {
const std::string tracefile = get_data_dir().join("bootstrap_gr.js").str();
const auto mock_server_port = port_pool_.get_next_available();
launch_mysql_server_mock(tracefile, mock_server_port, EXIT_SUCCESS, false);
std::vector<std::string> cmdline = {
"--bootstrap=127.0.0.1:" + std::to_string(mock_server_port), "-d",
bootstrap_dir.name()};
for (const auto ¶m : GetParam().con_set_option_params) {
cmdline.push_back(param);
}
auto &router = launch_router_for_bootstrap(cmdline, EXIT_FAILURE);
check_exit_code(router, EXIT_FAILURE);
// let's check if the expected error was reported:
EXPECT_THAT(router.get_full_output(),
::testing::ContainsRegex(GetParam().expected_error));
}
INSTANTIATE_TEST_SUITE_P(
ErrorTest, ConfSetOptionErrorTest,
::testing::Values(
ConfSetOptionErrorTestParam{
{"--conf-set-option=:test_rw.bind_port=6666"},
"Error: conf-set-option: invalid section name ':test_rw'"},
ConfSetOptionErrorTestParam{
{"--conf-set-option=routing:=6666"},
"Error: conf-set-option: invalid option 'routing:=6666', should be "
"section.option_name=value"},
ConfSetOptionErrorTestParam{
{"--conf-set-option=.para=value"},
"Error: conf-set-option: invalid section name ''"},
ConfSetOptionErrorTestParam{
{"--conf-set-option=.:="},
"Error: conf-set-option: invalid section name ''"},
ConfSetOptionErrorTestParam{
{"--conf-set-option=:.="},
"Error: conf-set-option: invalid section name ':'"},
ConfSetOptionErrorTestParam{
{"--conf-set-option=DEFAULT.read_timeout=1",
"--conf-set-option=DEFAULT.read_timeout=1"},
"Error: conf-set-option: duplicate value for option "
"'default.read_timeout'"},
ConfSetOptionErrorTestParam{
{"--conf-set-option=DEFAULT.read_timeout=1",
"--conf-set-option=DEFAULT.read_timeout=2"},
"Error: conf-set-option: duplicate value for option "
"'default.read_timeout'"},
ConfSetOptionErrorTestParam{
{"--conf-set-option=DEFAULT.connect_timeout=1",
"--connect-timeout=20",
"--conf-set-option=DEFAULT.connect_timeout=3"},
"Error: conf-set-option: duplicate value for option "
"'default.connect_timeout'"},
ConfSetOptionErrorTestParam{
{"--conf-set-option=MySection:AB.read_timeout=1",
"--conf-set-option=mysection:ab.read_TimeOut=2"},
"Error: conf-set-option: duplicate value for option "
"'mysection:ab.read_timeout'"},
ConfSetOptionErrorTestParam{
{"--conf-set-option=DEFAULT.read_timeout=1",
"--conf-set-option=DEFAULT.read_timeout=2",
"--conf-set-option=DEFAULT.read_timeout=3"},
"Error: conf-set-option: duplicate value for option "
"'default.read_timeout'"},
ConfSetOptionErrorTestParam{
{"--conf-set-option=DEFAULT.=xx"},
"Error: conf-set-option: invalid option name ''"},
ConfSetOptionErrorTestParam{
{"--conf-set-option=DEFAULT.:=xx"},
"Error: conf-set-option: invalid option name ':'"},
ConfSetOptionErrorTestParam{{"--conf-set-option=DEFAULT:.option=xx"},
"Error: conf-set-option: DEFAULT section "
"is not allowed to have a key: 'DEFAULT:"},
ConfSetOptionErrorTestParam{
{"--conf-set-option=DEFAULT:aa.option=xx"},
"Error: conf-set-option: DEFAULT section is not allowed to have a "
"key: 'DEFAULT:aa'"},
ConfSetOptionErrorTestParam{
{"--conf-set-option=abc"},
"Error: conf-set-option: invalid option 'abc', should be "
"section.option_name=value"}));
struct ConfSetOptionTestParam {
std::vector<std::string> bootstrap_params;
std::vector<std::string> expected_conf_entries;
std::vector<std::string> unexpected_conf_entries;
};
class ConfSetOptionParamTest
: public ConfSetOptionTest,
public ::testing::WithParamInterface<ConfSetOptionTestParam> {};
TEST_P(ConfSetOptionParamTest, Spec) {
const std::string tracefile = get_data_dir().join("bootstrap_gr.js").str();
const auto mock_server_port = port_pool_.get_next_available();
launch_mysql_server_mock(tracefile, mock_server_port, EXIT_SUCCESS, false);
std::vector<std::string> cmdline = {
"--bootstrap=127.0.0.1:" + std::to_string(mock_server_port), "-d",
bootstrap_dir.name()};
// add parameters passed by the testcase
cmdline.insert(cmdline.end(), GetParam().bootstrap_params.begin(),
GetParam().bootstrap_params.end());
auto &router = launch_router_for_bootstrap(cmdline, EXIT_SUCCESS, false);
ASSERT_NO_FATAL_FAILURE(check_exit_code(router, EXIT_SUCCESS));
config_file = bootstrap_dir.name() + "/mysqlrouter.conf";
const std::string config_file_str = get_file_output(config_file);
// check that expected entries are in the config file
for (const auto &entry : GetParam().expected_conf_entries) {
EXPECT_TRUE(config_file_contains(config_file_str, entry))
<< entry << "\n"
<< config_file_str;
}
// check that unexpected entries are NOT in the config file
for (const auto &entry : GetParam().unexpected_conf_entries) {
EXPECT_FALSE(config_file_contains(config_file_str, entry))
<< entry << "\n"
<< config_file_str;
}
}
/**
* @test
* verify that the --conf-set-option bootstrap parameter has precedence
* over other existing bootstrap options setting configuration values
*/
INSTANTIATE_TEST_SUITE_P(
OverwriteTest, ConfSetOptionParamTest,
::testing::Values(
ConfSetOptionTestParam{
{"--connect-timeout=20",
"--conf-set-option=DEFAULT.connect_timeout=1"},
/*expected_conf_entries=*/{"connect_timeout=1"},
/*unexpected_conf_entries=*/{"connect_timeout=20"}},
ConfSetOptionTestParam{
{"--connect-timeout=1",
"--conf-set-option=DEFAULT.connect_timeout=20"},
/*expected_conf_entries=*/{"connect_timeout=20"},
/*unexpected_conf_entries=*/{"connect_timeout=1"}},
ConfSetOptionTestParam{
{"--read-timeout=20", "--conf-set-option=DEFAULT.read_timeout=1"},
/*expected_conf_entry=*/{"read_timeout=1"},
/*unexpected_conf_entry=*/{"read_timeout=20"}},
ConfSetOptionTestParam{
{"--conf-base-port=1000",
"--conf-set-option=routing:bootstrap_rw.bind_port=2000"},
/*expected_conf_entries=*/{"bind_port=2000"},
/*unexpected_conf_entries=*/{"bind_port=1000"}},
// ConfSetOptionTestParam{
// {"--ssl-mode=REQUIRED",
// "--conf-set-option=metadata_cache:bootstrap.ssl_mode=DISABLED"},
// /*expected_conf_entries=*/{"ssl_mode=DISABLED"},
// /*unexpected_conf_entries=*/{"ssl-mode=REQUIRED"}}
ConfSetOptionTestParam{
{"--https-port=101", "--conf-set-option=http_server.port=202"},
/*expected_conf_entries=*/{"port=202"},
/*unexpected_conf_entries=*/{"port=101"}},
ConfSetOptionTestParam{
{"--name=Router01", "--conf-set-option=DEFAULT.name=Router02"},
/*expected_conf_entries=*/{"name=Router02"},
/*unexpected_conf_entries=*/{"name=Router01"}}));
/**
* @test
* verify that the --conf-set-option section name and option name are case
* insensitive
*/
INSTANTIATE_TEST_SUITE_P(
CaseSensitivity, ConfSetOptionParamTest,
::testing::Values(
ConfSetOptionTestParam{
{"--conf-set-option=DEFAULt.read_timeout=1"},
/*expected_conf_entries=*/{"[DEFAULT]", "read_timeout=1"},
/*unexpected_conf_entries=*/{"[DEFAULt]", "[default]"}},
ConfSetOptionTestParam{
{"--conf-set-option=default.connect_timeout=15"},
/*expected_conf_entries=*/{"[DEFAULT]", "connect_timeout=15"},
/*unexpected_conf_entries=*/{"[default]"}},
ConfSetOptionTestParam{
{"--conf-set-option=LOGGER.level=DEBUG"},
/*expected_conf_entries=*/{"[logger]", "level=DEBUG"},
/*unexpected_conf_entries=*/{"[LOGGER]", "level=debug"}},
ConfSetOptionTestParam{
{"--conf-set-option=METADATA_cache:BOOTSTRAP.router_id=1"},
/*expected_conf_entries=*/
{"[metadata_cache:bootstrap]", "router_id=1"},
/*unexpected_conf_entries=*/
{"[METADATA_cache:BOOTSTRAP]", "[metadata_cache:BOOTSTRAP]"}},
ConfSetOptionTestParam{
{"--conf-set-option=test_section.para1=10",
"--conf-set-option=test_Section.para2=20",
"--conf-set-option=TEST_SECTION.para3=30"},
/*expected_conf_entries=*/
{"[test_section]", "para1=10", "para2=20", "para3=30"},
/*unexpected_conf_entries=*/
{"[test_Section]", "[TEST_SECTION]"}},
ConfSetOptionTestParam{
{"--conf-set-option=test_section:SUB.para1=10",
"--conf-set-option=test_Section:Sub.para2=20",
"--conf-set-option=TEST_SECTION:sub.para3=30"},
/*expected_conf_entries=*/
{"[test_section:sub]", "para1=10", "para2=20", "para3=30"},
/*unexpected_conf_entries=*/
{"[test_section:SUB]", "[TEST_SECTION:sub]", "[TEST_SECTION:sub]"}},
ConfSetOptionTestParam{{"--conf-set-option=DEFAULT.READ_TIMEOUT=1"},
/*expected_conf_entries=*/
{"read_timeout=1"},
/*unexpected_conf_entries=*/
{"READ_TIMEOUT=1"}},
ConfSetOptionTestParam{{"--conf-set-option=DEFAULT.READ_Timeout=1"},
/*expected_conf_entries=*/
{"read_timeout=1"},
/*unexpected_conf_entries=*/
{"READ_Timeout=1"}},
ConfSetOptionTestParam{{"--conf-set-option=test_section.para1=10",
"--conf-set-option=test_section.Para2=20",
"--conf-set-option=test_section.PARA3=30"},
/*expected_conf_entries=*/
{"para1=10", "para2=20", "para3=30"},
/*unexpected_conf_entries=*/
{"Para2=10", "PARA3=20"}},
ConfSetOptionTestParam{{"--conf-set-option=DEFAULT.name=\"My Router\""},
/*expected_conf_entries=*/
{"name=\"My Router\""},
/*unexpected_conf_entries=*/
{}},
ConfSetOptionTestParam{
{"--name=\"My Router\"",
"--conf-set-option=DEFAULT.name=\"other router\""},
/*expected_conf_entries=*/
{"name=\"other router\""},
/*unexpected_conf_entries=*/
{"name=\"My Router\""}},
ConfSetOptionTestParam{{"--name=\"My Router\"",
"--conf-set-option=DEFAULT.name=\"MY router\""},
/*expected_conf_entries=*/
{"name=\"MY router\""},
/*unexpected_conf_entries=*/
{"name=\"My Router\""}}
));
/**
* @test
* verify that using ssl options during the bootstrap creates the
* configuration file that is usable by the Router
*/
TEST_F(RouterBootstrapTest, SSLOptions) {
TempDirectory bootstrap_directory;
const auto server_port = port_pool_.get_next_available();
const auto server_port2 = port_pool_.get_next_available();
const auto http_port = port_pool_.get_next_available();
const std::string json_stmts = get_data_dir().join("bootstrap_gr.js").str();
// launch mock server that is our metadata server for the bootstrap
auto &server_mock = launch_mysql_server_mock(json_stmts, server_port,
EXIT_SUCCESS, false, http_port);
set_mock_bootstrap_data(
http_port, "test",
{{"localhost", server_port}, {"localhost", server_port2}}, {2, 1, 0},
"00000000-0000-0000-0000-0000000000g1");
const auto router_port_rw = port_pool_.get_next_available();
const auto router_port_ro = port_pool_.get_next_available();
const auto router_port_x_rw = port_pool_.get_next_available();
const auto router_port_x_ro = port_pool_.get_next_available();
std::vector<std::string> bootsrtap_params{
"--bootstrap=127.0.0.1:" + std::to_string(server_port),
"-d",
bootstrap_directory.name(),
"--conf-set-option=routing:bootstrap_rw.bind_port=" +
std::to_string(router_port_rw),
"--conf-set-option=routing:bootstrap_ro.bind_port=" +
std::to_string(router_port_ro),
"--conf-set-option=routing:bootstrap_x_rw.bind_port=" +
std::to_string(router_port_x_rw),
"--conf-set-option=routing:bootstrap_x_ro.bind_port=" +
std::to_string(router_port_x_ro),
"--ssl-mode=disabled",
"--ssl-cipher=some",
"--tls-version=TLSv1.2",
"--ssl-ca=some",
"--ssl-capath=some",
"--ssl-crl=some",
"--ssl-crlpath=some"};
// launch the router in bootstrap mode
auto &router = launch_router_for_bootstrap(bootsrtap_params);
check_exit_code(router, EXIT_SUCCESS);
const std::string conf_file =
bootstrap_directory.name() + "/mysqlrouter.conf";
std::vector<std::string> expected_config_lines{
"ssl_mode=disabled", "ssl_cipher=some", "tls_version=TLSv1.2",
"ssl_ca=some", "ssl_capath=some", "ssl_crl=some",
"ssl_crlpath=some"};
// check if valid config options were added to the file
auto conf_file_content = get_file_output(conf_file);
auto conf_lines = mysql_harness::split_string(conf_file_content, '\n');
EXPECT_THAT(conf_lines, ::testing::IsSupersetOf(expected_config_lines));
server_mock.send_clean_shutdown_event();
EXPECT_NO_THROW(server_mock.wait_for_exit());
auto plugin_dir = mysql_harness::get_plugin_dir(get_origin().str());
ASSERT_TRUE(add_line_to_config_file(conf_file, "DEFAULT", "plugin_folder",
plugin_dir));
const std::string runtime_json_stmts =
get_data_dir().join("metadata_dynamic_nodes_v2_gr.js").str();
// launch mock server that is our metadata server
launch_mysql_server_mock(runtime_json_stmts, server_port, EXIT_SUCCESS, false,
http_port);
set_mock_metadata(http_port, "00000000-0000-0000-0000-0000000000g1",
{server_port}, 0, {server_port});
// check that the Router is running fine with this configuration file
ASSERT_NO_FATAL_FAILURE(launch_router({"-c", conf_file}));
}
/**
* @test
* verify that Router can be re-bootstrapped using the same directory if
* the cluster name has changed in the meantime
*/
TEST_F(RouterComponentBootstrapTest, RouterReBootstrapClusetNameChange) {
const std::string tracefile = "bootstrap_gr.js";
const std::string kInitialClusterName = "initial_cluster_name";
const std::string kChangedClusterName = "changed_cluster_name";
const auto classic_port = port_pool_.get_next_available();
const auto http_port = port_pool_.get_next_available();
const std::string json_stmts = get_data_dir().join(tracefile).str();
launch_mysql_server_mock(json_stmts, classic_port, EXIT_SUCCESS, false,
http_port);
set_mock_bootstrap_data(http_port, kInitialClusterName,
{{"localhost", classic_port}}, {2, 1, 0}, "gr-uuid");
// do the first bootstrap
std::vector<std::string> cmdline_bs = {"--bootstrap=root:"s + kRootPassword +
"@localhost:"s +
std::to_string(classic_port),
"-d", bootstrap_dir.name()};
auto &router_bs1 = launch_router_for_bootstrap(cmdline_bs);
check_exit_code(router_bs1, EXIT_SUCCESS);
// change the cluster name
set_mock_bootstrap_data(http_port, kChangedClusterName,
{{"localhost", classic_port}}, {2, 1, 0}, "gr-uuid");
// do the second bootstrap using the same directory
auto &router_bs2 = launch_router_for_bootstrap(cmdline_bs);
check_exit_code(router_bs2, EXIT_SUCCESS);
}
/**
* @test
* verify that using --force-password-validation is still supported for
* backward compatibility
*/
TEST_F(RouterComponentBootstrapTest, ForcePasswordValidation) {
const std::string tracefile = "bootstrap_gr.js";
const auto classic_port = port_pool_.get_next_available();
const auto http_port = port_pool_.get_next_available();
const std::string json_stmts = get_data_dir().join(tracefile).str();
launch_mysql_server_mock(json_stmts, classic_port, EXIT_SUCCESS, false,
http_port);
set_mock_bootstrap_data(http_port, "cluster-name",
{{"localhost", classic_port}}, {2, 1, 0}, "gr-uuid");
// do the first bootstrap
std::vector<std::string> cmdline_bs = {
"--bootstrap=root:"s + kRootPassword + "@localhost:"s +
std::to_string(classic_port),
"--force-password-validation", "-d", bootstrap_dir.name()};
auto &router_bs = launch_router_for_bootstrap(cmdline_bs);
check_exit_code(router_bs, EXIT_SUCCESS);
}
TEST_F(RouterComponentBootstrapTest, ShowCipherInvalidResult) {
const std::string tracefile =
get_data_dir()
.join("bootstrap_show_cipher_status_invalid_result.js")
.str();
const auto mock_server_port = port_pool_.get_next_available();
const auto mock_http_port = port_pool_.get_next_available();
launch_mysql_server_mock(tracefile, mock_server_port, EXIT_SUCCESS, false,
mock_http_port);
set_mock_bootstrap_data(mock_http_port, "cluster-name",
{{"localhost", mock_server_port}}, {2, 1, 0},
"gr-uuid");
std::vector<std::string> cmdline = {
"--bootstrap=127.0.0.1:" + std::to_string(mock_server_port), "-d",
bootstrap_dir.name()};
auto &router = launch_router_for_bootstrap(cmdline, EXIT_FAILURE);
check_exit_code(router, EXIT_FAILURE);
// let's check if the expected error was reported:
EXPECT_THAT(router.get_full_output(),
::testing::HasSubstr(
"Failed determining if metadata connection uses SSL: Error "
"reading 'ssl_cipher' status variable"));
}
struct BootstrapErrorTestParam {
std::vector<std::string> bs_params;
std::string expected_error;
};
class BootstrapErrorTest
: public RouterComponentBootstrapTest,
public ::testing::WithParamInterface<BootstrapErrorTestParam> {};
TEST_P(BootstrapErrorTest, Spec) {
std::vector<std::string> cmdline = {"-d", bootstrap_dir.name()};
for (const auto ¶m : GetParam().bs_params) {
cmdline.push_back(param);
}
auto &router = launch_router_for_bootstrap(cmdline, EXIT_FAILURE);
check_exit_code(router, EXIT_FAILURE);
// let's check if the expected error was reported:
EXPECT_THAT(router.get_full_output(),
::testing::HasSubstr(GetParam().expected_error));
}
INSTANTIATE_TEST_SUITE_P(
Spec, BootstrapErrorTest,
::testing::Values(
BootstrapErrorTestParam{
{"-B=["},
"Error: invalid URI: expected to find IPv6 address, but failed at "
"position 9 for: mysql://[\n"},
BootstrapErrorTestParam{
{"-B=abc.nodomain.com#fragment"},
"Error: the bootstrap URI contains a #fragement, but shouldn't"},
BootstrapErrorTestParam{
{"-B=abc.nodomain.com?query=q"},
"Error: the bootstrap URI contains a ?query, but shouldn't"},
BootstrapErrorTestParam{
{"-B=abc.nodomain.com/path"},
"Error: the bootstrap URI contains a /path, but shouldn't"},
BootstrapErrorTestParam{
{"--bootstrap-socket=/mysock", "-B=abc.nodomain.com"},
"Error: --bootstrap-socket given, but --bootstrap option contains "
"a non-'localhost' hostname: abc.nodomain.com"}));
class BootstrapErrorTestWithMock
: public RouterComponentBootstrapTest,
public ::testing::WithParamInterface<BootstrapErrorTestParam> {};
TEST_P(BootstrapErrorTestWithMock, Spec) {
const std::string tracefile = get_data_dir().join("bootstrap_gr.js").str();
const auto mock_server_port = port_pool_.get_next_available();
launch_mysql_server_mock(tracefile, mock_server_port, EXIT_SUCCESS, false);
std::vector<std::string> cmdline = {"--bootstrap=root:"s + kRootPassword +
"@localhost:"s +
std::to_string(mock_server_port),
"-d", bootstrap_dir.name()};
for (const auto ¶m : GetParam().bs_params) {
cmdline.push_back(param);
}
auto &router = launch_router_for_bootstrap(cmdline, EXIT_FAILURE);
check_exit_code(router, EXIT_FAILURE);
// let's check if the expected error was reported:
EXPECT_THAT(router.get_full_output(),
::testing::HasSubstr(GetParam().expected_error));
}
INSTANTIATE_TEST_SUITE_P(
Spec, BootstrapErrorTestWithMock,
::testing::Values(
BootstrapErrorTestParam{
{"--conf-target-cluster=primary"},
"The parameter 'target-cluster' is valid only for Cluster that "
"is part of the ClusterSet."},
BootstrapErrorTestParam{{"--conf-target-cluster-by-name=name"},
"The parameter 'target-cluster-by-name' is "
"valid only for Cluster that "
"is part of the ClusterSet."},
BootstrapErrorTestParam{{"--conf-bind-address=.foo"},
"Invalid --conf-bind-address value '.foo'"},
BootstrapErrorTestParam{
{"--name=name\n"},
"Router name 'name\n' contains invalid characters."},
BootstrapErrorTestParam{{"--name=system"},
"Router name 'system' is reserved"},
BootstrapErrorTestParam{
{"--name=" + std::string(256, 'a')},
"Router name '" +
mysql_harness::truncate_string(std::string(256, 'a')) +
"' too long (max 255)."},
BootstrapErrorTestParam{
{"--password-retries=abc"},
"Configuration error: --password-retries needs value between 1 and "
"10000 inclusive, was 'abc'"},
BootstrapErrorTestParam{
{"--password-retries="},
"Configuration error: --password-retries needs value between 1 and "
"10000 inclusive, was ''"}));
struct AuthPluginTestParam {
// what are the host/plugin pairs in the mysql.user table for the bootstrap
// user
std::vector<std::pair<std::string, std::string>> auth_host_plugins;
// what is the default authentication plugin on the server we bootstrap
// against
std::string default_auth_plugin;
// vector of strings expected on the console after the bootstrap
std::vector<std::string> expected_output_strings;
// vector of strings NOT expected on the console after the bootstrap
std::vector<std::string> unexpected_output_strings;
// describes the test scenario and expectations
std::string test_description;
// should the "select host, plugin.." query fail on the server
bool fail_host_plugin_query{false};
// should the "select @@default_authentication_plugin" query fail on the
// server
bool fail_default_auth_plugin_query{false};
// should the "alter user" query fail on the server
bool fail_alter_user_query{false};
};
class BootstrapChangeAuthPluginTest
: public RouterComponentBootstrapTest,
public ::testing::WithParamInterface<AuthPluginTestParam> {};
/**
* @test
* verify that the functionality that checks if the existing user account
* is not using depracated mysql_native_password and tries to upgrade it works
* correctly.
*/
TEST_P(BootstrapChangeAuthPluginTest, Spec) {
RecordProperty("Description", GetParam().test_description);
TempDirectory bootstrap_directory;
const auto server_port = port_pool_.get_next_available();
const auto http_port = port_pool_.get_next_available();
const std::string json_stmts =
get_data_dir().join("bootstrap_change_auth_plugin.js").str();
// launch mock server that is our metadata server for the bootstrap
launch_mysql_server_mock(json_stmts, server_port, EXIT_SUCCESS, false,
http_port);
set_mock_metadata(http_port, "cluster-specific-id",
classic_ports_to_gr_nodes({server_port}), 0, {server_port});
{
std::string server_globals =
MockServerRestClient(http_port).get_globals_as_json_string();
JsonDocument globals;
if (globals.Parse<0>(server_globals.c_str()).HasParseError()) {
FAIL() << "Failed parsing mock server globals";
}
JsonAllocator allocator;
JsonValue auth_host_plugins_json(rapidjson::kArrayType);
for (const auto &auth_host_plugin : GetParam().auth_host_plugins) {
JsonValue auth_host_plugin_json(rapidjson::kArrayType);
auth_host_plugin_json.PushBack(
JsonValue(auth_host_plugin.first.c_str(),
auth_host_plugin.first.length(), allocator),
allocator);
auth_host_plugin_json.PushBack(
JsonValue(auth_host_plugin.second.c_str(),
auth_host_plugin.second.length(), allocator),
allocator);
auth_host_plugins_json.PushBack(auth_host_plugin_json, allocator);
}
globals.AddMember("auth_host_plugins", auth_host_plugins_json, allocator);
globals.AddMember(
"default_auth_plugin",
JsonValue(GetParam().default_auth_plugin.c_str(),
GetParam().default_auth_plugin.length(), allocator),
allocator);
globals.AddMember("fail_host_plugin_query",
GetParam().fail_host_plugin_query, allocator);
globals.AddMember("fail_default_auth_plugin_query",
GetParam().fail_default_auth_plugin_query, allocator);
globals.AddMember("fail_alter_user_query", GetParam().fail_alter_user_query,
allocator);
server_globals = json_to_string(globals);
MockServerRestClient(http_port).set_globals(server_globals);
}
std::vector<std::string> bootsrtap_params{
"--bootstrap=127.0.0.1:" + std::to_string(server_port), "-d",
bootstrap_directory.name()};
// launch the router in bootstrap mode
auto &router = launch_router_for_bootstrap(bootsrtap_params, EXIT_SUCCESS);
check_exit_code(router, EXIT_SUCCESS);
const std::string router_console_output = router.get_full_output();
for (const auto &expected_output_string :
GetParam().expected_output_strings) {
EXPECT_TRUE(pattern_found(router_console_output, expected_output_string))
<< router_console_output;
}
for (const auto &unexpected_output_string :
GetParam().unexpected_output_strings) {
EXPECT_FALSE(pattern_found(router_console_output, unexpected_output_string))
<< router_console_output;
}
}
INSTANTIATE_TEST_SUITE_P(
Spec, BootstrapChangeAuthPluginTest,
::testing::Values(
AuthPluginTestParam{
/* auth_host_plugins */
{{"localhost", "caching_sha2_password"}},
/* default_auth_plugin */
"caching_sha2_password",
/*expected_output_strings*/
{},
/*unexpected_output_strings*/
{"Successfully changed the authentication plugin for .*"},
/*test_description*/
"There is single existing account for our router user but is uses "
"caching_sha2_password. There is no need for any auth_plugin "
"change."},
AuthPluginTestParam{
/* auth_host_plugins */
{{"localhost", "caching_sha2_password"},
{"10.20.*.*", "caching_sha2_password"}},
/* default_auth_plugin */
"caching_sha2_password",
/*expected_output_strings*/
{},
/*unexpected_output_strings*/
{"Successfully changed the authentication plugin for .*"},
/*test_description*/
"There are 2 existing accounts for our router user but both use "
"caching_sha2_password. There is no need for any auth_plugin "
"change."},
AuthPluginTestParam{
/* auth_host_plugins */
{{"localhost", "mysql_native_password"}},
/* default_auth_plugin */
"caching_sha2_password",
/*expected_output_strings*/
{"Existing account '.*'@localhost is using authentication plugin "
"'mysql_native_password'. Changing the "
"authentication plugin to 'caching_sha2_password'",
"Successfully changed the authentication plugin for "
"'.*'@localhost from mysql_native_password to "
"caching_sha2_password"},
/*unexpected_output_strings*/
{},
/*test_description*/
"There is single existing account for our user that uses "
"mysql_native_password. The default auth_plugin on the server is "
"caching_sha2_password. We expect successful change of the "
"auth_plugin for our account"},
AuthPluginTestParam{
/* auth_host_plugins */
{{"%", "mysql_native_password"},
{"localhost", "mysql_native_password"}},
/* default_auth_plugin */
"caching_sha2_password",
/*expected_output_strings*/
{"Account '.*'@% is using depracated 'mysql_native_password' "
"authentication plugin. Change the authentication plugin using "
"'alter user' SQL statement.",
"Account '.*'@localhost is using depracated "
"'mysql_native_password' authentication plugin. Change the "
"authentication plugin using 'alter user' SQL statement."},
/*unexpected_output_strings*/
{"Successfully changed the authentication plugin for .*"},
/*test_description*/
"There is more than one host account for our user. Both use "
"mysql_native_password. Since there is more than one we do not "
"attempt to change the auth_plugin, only give a warning advising "
"the user to do so manually. We expect that warning twice, once "
"per each user@host combination."},
AuthPluginTestParam{
/* auth_host_plugins */
{{"%", "mysql_native_password"},
{"localhost", "caching_sha2_password"}},
/* default_auth_plugin */
"caching_sha2_password",
/*expected_output_strings*/
{"Account '.*'@% is using depracated 'mysql_native_password' "
"authentication plugin. Change the authentication plugin using "
"'alter user' SQL statement."},
/*unexpected_output_strings*/
{"Successfully changed the authentication plugin for .*",
"Account '.*'@localhost is using depracated "
"'mysql_native_password' authentication plugin. Change the "
"authentication plugin using 'alter user' SQL statement."},
/*test_description*/
"There are 2 host accounts for our user. Only one uses "
"mysql_native_password. Since there is more than one we do not "
"attempt to change the auth_plugin, only give a warning advising "
"the user to do so manually. We expect that warning only once, for "
"the account that uses mysql_native_password."},
AuthPluginTestParam{
/* auth_host_plugins */
{{"localhost", "mysql_native_password"}},
/* default_auth_plugin */
"mysql_native_password",
/*expected_output_strings*/
{"Failed changing the authentication plugin for account "
"'.*'@'localhost': mysql_native_password which is deprecated is "
"the default authentication plugin on this server."},
/*unexpected_output_strings*/
{"Successfully changed the authentication plugin for .*"},
/*test_description*/
"There is single existing account for our user that uses "
"mysql_native_password. The default auth_plugin on the server is "
"mysql_native_password so we can't change the auth_plugin. We are "
"only expected to give a warning."},
AuthPluginTestParam{
/* auth_host_plugins */
{{"localhost", "mysql_native_password"}},
/* default_auth_plugin */
"",
/*expected_output_strings*/
{},
/*unexpected_output_strings*/
{"Successfully changed the authentication plugin for .*",
"Failed checking the Router account .*"},
/*test_description*/
"Querying for the host, plugin accounts for our user fails. We "
"expect no warning, the Router should just leave and quit trying "
"to upgrade an account.",
/*fail_host_plugin_query*/ true},
AuthPluginTestParam{
/* auth_host_plugins */
{{"localhost", "mysql_native_password"}},
/* default_auth_plugin */
"",
/*expected_output_strings*/
{"Failed getting default authentication plugin while changing the "
"authentication plugin for account "
"'.*'@'localhost': Error executing MySQL query \"select "
"@@default_authentication_plugin\": Unexpected "
"error .*"},
/*unexpected_output_strings*/
{"Successfully changed the authentication plugin for .*"},
/*test_description*/
"Querying for the the default auth plugin fails. We expect a "
"proper warning.",
/*fail_host_plugin_query*/ false,
/*fail_default_auth_plugin_query*/ true},
AuthPluginTestParam{
/* auth_host_plugins */
{{"localhost", "mysql_native_password"}},
/* default_auth_plugin */
"caching_sha2_password",
/*expected_output_strings*/
{"Existing account '.*'@localhost is using authentication plugin "
"'mysql_native_password'. Changing the authentication plugin to "
"'caching_sha2_password'",
"Failed changing the authentication plugin for account "
"'.*'@'localhost': Error executing MySQL query \"alter user "
"'.*'@'localhost' identified with `caching_sha2_password` by "
"\\*\\*\\*\": Unexpected error .*"},
/*unexpected_output_strings*/
{"Successfully changed the authentication plugin for .*"},
/*test_description*/
"'alter user' statement fails. We expect a proper warning.",
/*fail_host_plugin_query*/ false,
/*fail_default_auth_plugin_query*/ false,
/*fail_alter_user_query*/ true}));
static constexpr const unsigned long max_supported_version =
MYSQL_ROUTER_VERSION_MAJOR * 10000 + MYSQL_ROUTER_VERSION_MINOR * 100 + 99;
struct ServerCompatTestParam {
std::string description;
std::string tracefile;
std::string server_version;
bool expect_failure;
std::string expected_error_msg;
};
class CheckServerCompatibilityTest
: public RouterComponentBootstrapTest,
public ::testing::WithParamInterface<ServerCompatTestParam> {};
/**
* @test
* Verifies that the server version is checked for compatibility when the
* Router is bootstrapped against the GR Cluster and Replica Set
*/
TEST_P(CheckServerCompatibilityTest, Spec) {
RecordProperty("Description", GetParam().description);
const auto classic_port = port_pool_.get_next_available();
const auto http_port = port_pool_.get_next_available();
const std::string tracefile = get_data_dir().join(GetParam().tracefile).str();
launch_mysql_server_mock(tracefile, classic_port, EXIT_SUCCESS, false,
http_port);
set_mock_metadata(http_port, "gr-uuid",
classic_ports_to_gr_nodes({classic_port}), 0,
{classic_port});
set_mock_server_version(http_port, GetParam().server_version);
std::vector<std::string> cmdline_bs = {"--bootstrap=root:"s + kRootPassword +
"@localhost:"s +
std::to_string(classic_port),
"-d", bootstrap_dir.name()};
const auto expected_exit_code =
GetParam().expect_failure ? EXIT_FAILURE : EXIT_SUCCESS;
auto &router = launch_router_for_bootstrap(cmdline_bs, expected_exit_code);
check_exit_code(router, expected_exit_code);
if (GetParam().expect_failure) {
const std::string router_console_output = router.get_full_output();
EXPECT_TRUE(
pattern_found(router_console_output, GetParam().expected_error_msg))
<< router_console_output;
}
}
INSTANTIATE_TEST_SUITE_P(
Spec, CheckServerCompatibilityTest,
::testing::Values(
ServerCompatTestParam{
"GR Cluster; Server is the same version as Router - bootstrap OK",
"bootstrap_gr.js",
std::to_string(MYSQL_ROUTER_VERSION_MAJOR) + "." +
std::to_string(MYSQL_ROUTER_VERSION_MINOR) + "." +
std::to_string(MYSQL_ROUTER_VERSION_PATCH),
false, ""},
ServerCompatTestParam{
"Replica Set; Server is the same version as Router - bootstrap OK",
"bootstrap_ar.js",
std::to_string(MYSQL_ROUTER_VERSION_MAJOR) + "." +
std::to_string(MYSQL_ROUTER_VERSION_MINOR) + "." +
std::to_string(MYSQL_ROUTER_VERSION_PATCH),
false, ""},
ServerCompatTestParam{
"GR Cluster; Server major version is highier than Router - "
"bootstrap should fail",
"bootstrap_gr.js",
std::to_string(MYSQL_ROUTER_VERSION_MAJOR + 1) + "." +
std::to_string(MYSQL_ROUTER_VERSION_MINOR) + "." +
std::to_string(MYSQL_ROUTER_VERSION_PATCH),
true,
"Error: Unsupported MySQL Server version '.*'. Maximal supported "
"version is '" +
std::to_string(max_supported_version) + "'."},
ServerCompatTestParam{
"GR Cluster; Server minor version is highier than Router - "
"bootstrap should fail",
"bootstrap_gr.js",
std::to_string(MYSQL_ROUTER_VERSION_MAJOR) + "." +
std::to_string(MYSQL_ROUTER_VERSION_MINOR + 1) + "." +
std::to_string(MYSQL_ROUTER_VERSION_PATCH),
true,
"Error: Unsupported MySQL Server version '.*'. Maximal supported "
"version is '" +
std::to_string(max_supported_version) + "'."},
ServerCompatTestParam{
"GR Cluster; Server patch version is highier than Router - "
"bootstrap OK",
"bootstrap_gr.js",
std::to_string(MYSQL_ROUTER_VERSION_MAJOR) + "." +
std::to_string(MYSQL_ROUTER_VERSION_MINOR) + "." +
std::to_string(MYSQL_ROUTER_VERSION_PATCH + 1),
false, ""},
ServerCompatTestParam{
"Replica Set; Server major version is highier than Router - "
"bootstrap should fail",
"bootstrap_ar.js",
std::to_string(MYSQL_ROUTER_VERSION_MAJOR) + "." +
std::to_string(MYSQL_ROUTER_VERSION_MINOR + 1) + "." +
std::to_string(MYSQL_ROUTER_VERSION_PATCH),
true,
"Error: Unsupported MySQL Server version '.*'. Maximal supported "
"version is '" +
std::to_string(max_supported_version) + "'."}));
int main(int argc, char *argv[]) {
init_windows_sockets();
ProcessManager::set_origin(Path(argv[0]).dirname());
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
|