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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Ci::Runner, type: :model, feature_category: :runner do
include StubGitlabCalls
let_it_be(:group) { create(:group) }
let_it_be(:project) { create(:project, group: group) }
it_behaves_like 'having unique enum values'
it_behaves_like 'it has loose foreign keys' do
let(:factory_name) { :ci_runner }
end
context 'loose foreign key on ci_runners.creator_id' do
it_behaves_like 'cleanup by a loose foreign key' do
let!(:parent) { create(:user) }
let!(:model) { create(:ci_runner, creator: parent) }
end
end
describe 'groups association' do
# Due to other associations such as projects this whole spec is allowed to
# generate cross-database queries. So we have this temporary spec to
# validate that at least groups association does not generate cross-DB
# queries.
it 'does not create a cross-database query' do
runner = create(:ci_runner, :group, groups: [group])
with_cross_joins_prevented do
expect(runner.groups.count).to eq(1)
end
end
end
describe '#owner_runner_namespace' do
it 'considers the first group' do
runner = create(:ci_runner, :group, groups: [group])
with_cross_joins_prevented do
expect(runner.owner_runner_namespace.namespace_id).to eq(runner.groups.first.id)
end
end
end
describe 'projects association' do
let(:runner) { create(:ci_runner, :project, projects: [project]) }
it 'does not create a cross-database query' do
with_cross_joins_prevented do
expect(runner.projects.count).to eq(1)
end
end
end
describe 'acts_as_taggable' do
let(:tag_name) { 'tag123' }
context 'on save' do
let_it_be_with_reload(:runner) { create(:ci_runner) }
before do
runner.tag_list = [tag_name]
end
context 'tag does not exist' do
it 'creates a tag' do
expect { runner.save! }.to change(Ci::Tag, :count).by(1)
end
it 'creates an association to the tag' do
runner.save!
expect(described_class.tagged_with(tag_name)).to include(runner)
end
end
context 'tag already exists' do
before do
Ci::Tag.create!(name: tag_name)
end
it 'does not create a tag' do
expect { runner.save! }.not_to change(Ci::Tag, :count)
end
it 'creates an association to the tag' do
runner.save!
expect(described_class.tagged_with(tag_name)).to include(runner)
end
end
end
end
describe 'validation' do
it { is_expected.to validate_length_of(:name).is_at_most(256) }
it { is_expected.to validate_length_of(:description).is_at_most(1024) }
it { is_expected.to validate_presence_of(:access_level) }
it { is_expected.to validate_presence_of(:runner_type) }
it { is_expected.to validate_presence_of(:registration_type) }
it { is_expected.to validate_presence_of(:sharding_key_id) }
context 'when runner is instance type' do
let(:runner) { build(:ci_runner, :instance_type) }
it { expect(runner).to be_valid }
context 'when sharding_key_id is present' do
let(:runner) { build(:ci_runner, :instance_type, sharding_key_id: non_existing_record_id) }
it 'is invalid' do
expect(runner).to be_invalid
expect(runner.errors.full_messages).to contain_exactly('Runner cannot have sharding_key_id assigned')
end
end
end
context 'when runner is not allowed to pick untagged jobs' do
context 'when runner does not have tags' do
let(:runner) { build(:ci_runner, tag_list: [], run_untagged: false) }
it { expect(runner).to be_invalid }
end
context 'when runner has too many tags' do
let(:runner) { build(:ci_runner, tag_list: (1..::Ci::Runner::TAG_LIST_MAX_LENGTH + 1).map { |i| "tag#{i}" }, run_untagged: false) }
it { expect(runner).to be_invalid }
end
context 'when runner has tags' do
let(:runner) { build(:ci_runner, tag_list: ['tag'], run_untagged: false) }
it { expect(runner).to be_valid }
end
end
describe '#exactly_one_group' do
let(:runner) { create(:ci_runner, :group, groups: [group]) }
it 'disallows assigning group if already assigned to a group' do
runner.runner_namespaces << create(:ci_runner_namespace, runner: runner)
expect(runner).not_to be_valid
expect(runner.errors.full_messages).to include('Runner needs to be assigned to exactly one group')
end
end
context 'runner_type validations' do
it 'disallows assigning group to project_type runner' do
project_runner = build(:ci_runner, :project, :without_projects, groups: [group])
expect(project_runner).not_to be_valid
expect(project_runner.errors.full_messages).to include('Runner cannot have groups assigned')
end
it 'disallows assigning group to instance_type runner' do
instance_runner = build(:ci_runner, :instance, groups: [group])
expect(instance_runner).not_to be_valid
expect(instance_runner.errors.full_messages).to include('Runner cannot have groups assigned')
end
it 'disallows assigning project to group_type runner' do
group_runner = build(:ci_runner, :instance, projects: [project])
expect(group_runner).not_to be_valid
expect(group_runner.errors.full_messages).to include('Runner cannot have projects assigned')
end
it 'disallows assigning project to instance_type runner' do
instance_runner = build(:ci_runner, :instance, projects: [project])
expect(instance_runner).not_to be_valid
expect(instance_runner.errors.full_messages).to include('Runner cannot have projects assigned')
end
it 'fails to save a group assigned to a project runner even if the runner is already saved' do
project_runner = create(:ci_runner, :project, projects: [project])
expect { create(:group, runners: [project_runner]) }
.to raise_error(ActiveRecord::RecordInvalid)
end
end
context 'cost factors validations' do
it 'disallows :private_projects_minutes_cost_factor being nil' do
runner = build(:ci_runner, private_projects_minutes_cost_factor: nil)
expect(runner).to be_invalid
expect(runner.errors.full_messages).to include('Private projects minutes cost factor needs to be non-negative')
end
it 'disallows :public_projects_minutes_cost_factor being nil' do
runner = build(:ci_runner, public_projects_minutes_cost_factor: nil)
expect(runner).to be_invalid
expect(runner.errors.full_messages).to include('Public projects minutes cost factor needs to be non-negative')
end
it 'disallows :private_projects_minutes_cost_factor being negative' do
runner = build(:ci_runner, private_projects_minutes_cost_factor: -1.1)
expect(runner).to be_invalid
expect(runner.errors.full_messages).to include('Private projects minutes cost factor needs to be non-negative')
end
it 'disallows :public_projects_minutes_cost_factor being negative' do
runner = build(:ci_runner, public_projects_minutes_cost_factor: -2.2)
expect(runner).to be_invalid
expect(runner.errors.full_messages).to include('Public projects minutes cost factor needs to be non-negative')
end
end
describe '#no_allowed_plan_ids' do
let_it_be(:default_plan) { create(:default_plan) }
context 'when runner is instance type' do
let(:runner) { create(:ci_runner, :instance) }
it 'allows assign allowed_plans' do
runner.allowed_plan_ids = [default_plan.id]
expect(runner).to be_valid
puts runner.errors.full_messages
end
end
context 'when runner is not an instance type' do
let(:runner) { create(:ci_runner, :group, groups: [group]) }
subject { runner.allowed_plan_ids = [default_plan.id] }
it 'allows assign allowed_plans' do
runner.allowed_plan_ids = [default_plan.id]
expect(runner).not_to be_valid
expect(runner.errors.full_messages).to include('Runner cannot have allowed plans assigned')
puts runner.errors.full_messages
end
end
end
end
describe 'constraints' do
it '.UPDATE_CONTACT_COLUMN_EVERY' do
expect(described_class::UPDATE_CONTACT_COLUMN_EVERY.max)
.to be <= described_class::ONLINE_CONTACT_TIMEOUT
end
end
describe '#access_level' do
context 'when creating new runner and access_level is nil' do
let(:runner) { build(:ci_runner, access_level: nil) }
it "object is invalid" do
expect(runner).not_to be_valid
end
end
context 'when creating new runner and access_level is defined in enum' do
let(:runner) { build(:ci_runner, access_level: :not_protected) }
it "object is valid" do
expect(runner).to be_valid
end
end
context 'when creating new runner and access_level is not defined in enum' do
it "raises an error" do
expect { build(:ci_runner, access_level: :this_is_not_defined) }.to raise_error(ArgumentError)
end
end
end
describe '#owner' do
subject(:owner) { runner.owner }
context 'when runner does not have creator_id' do
let_it_be(:runner) { create(:ci_runner, :instance) }
it { is_expected.to be_nil }
end
context 'when runner has creator' do
let_it_be(:creator) { create(:user) }
let_it_be(:runner) { create(:ci_runner, :instance, creator: creator) }
it { is_expected.to eq creator }
end
end
describe '.instance_type' do
let!(:group_runner) { create(:ci_runner, :group, groups: [group]) }
let!(:project_runner) { create(:ci_runner, :project, projects: [project]) }
let!(:shared_runner) { create(:ci_runner, :instance) }
it 'returns only shared runners' do
expect(described_class.instance_type).to contain_exactly(shared_runner)
end
end
describe '.belonging_to_project' do
it 'returns the project runner' do
# own
own_project = create(:project)
own_runner = create(:ci_runner, :project, projects: [own_project])
# other
other_project = create(:project)
create(:ci_runner, :project, projects: [other_project])
expect(described_class.belonging_to_project(own_project.id)).to eq [own_runner]
end
end
shared_examples '.belonging_to_parent_groups_of_project' do
let_it_be(:group1) { create(:group) }
let_it_be(:project1) { create(:project, group: group1) }
let_it_be(:runner1) { create(:ci_runner, :group, groups: [group1]) }
let_it_be(:group2) { create(:group) }
let_it_be(:project2) { create(:project, group: group2) }
let_it_be(:runner2) { create(:ci_runner, :group, groups: [group2]) }
let(:project_id) { project1.id }
subject(:result) { described_class.belonging_to_parent_groups_of_project(project_id) }
it 'returns the group runner' do
expect(result).to contain_exactly(runner1)
end
context 'with a parent group with a runner', :sidekiq_inline do
before do
group1.update!(parent: group2)
end
it 'returns the group runner from the group and the parent group' do
expect(result).to contain_exactly(runner1, runner2)
end
end
context 'with multiple project ids' do
let(:project_id) { [project1.id, project2.id] }
it 'raises ArgumentError' do
expect { result }.to raise_error(ArgumentError)
end
end
end
it_behaves_like '.belonging_to_parent_groups_of_project'
context 'with instance runners sharing enabled' do
# group specific
let_it_be(:group) { create(:group, shared_runners_enabled: true) }
let_it_be(:project) { create(:project, group: group, shared_runners_enabled: true) }
let_it_be(:group_runner) { create(:ci_runner, :group, groups: [group]) }
# project specific
let_it_be(:project_runner) { create(:ci_runner, :project, projects: [project]) }
# globally shared
let_it_be(:shared_runner) { create(:ci_runner, :instance) }
describe '.owned_or_instance_wide' do
subject { described_class.owned_or_instance_wide(project.id) }
it 'returns a shared, project and group runner' do
is_expected.to contain_exactly(group_runner, project_runner, shared_runner)
end
end
describe '.group_or_instance_wide' do
subject { described_class.group_or_instance_wide(group) }
before do
# Ensure the project runner is instantiated
project_runner
end
it 'returns a globally shared and a group runner' do
is_expected.to contain_exactly(group_runner, shared_runner)
end
end
end
context 'with instance runners sharing disabled' do
# group specific
let_it_be(:group) { create(:group, shared_runners_enabled: false) }
let_it_be(:group_runner) { create(:ci_runner, :group, groups: [group]) }
let(:group_runners_enabled) { true }
let(:project) { create(:project, group: group, shared_runners_enabled: false) }
# project specific
let(:project_runner) { create(:ci_runner, :project, projects: [project]) }
# globally shared
let_it_be(:shared_runner) { create(:ci_runner, :instance) }
before do
project.update!(group_runners_enabled: group_runners_enabled)
end
describe '.owned_or_instance_wide' do
subject { described_class.owned_or_instance_wide(project.id) }
context 'with group runners disabled' do
let(:group_runners_enabled) { false }
it 'returns only the project runner' do
is_expected.to contain_exactly(project_runner)
end
end
context 'with group runners enabled' do
let(:group_runners_enabled) { true }
it 'returns a project runner and a group runner' do
is_expected.to contain_exactly(group_runner, project_runner)
end
end
end
describe '.group_or_instance_wide' do
subject { described_class.group_or_instance_wide(group) }
before do
# Ensure the project runner is instantiated
project_runner
end
it 'returns a group runner' do
is_expected.to contain_exactly(group_runner)
end
end
end
describe '#display_name' do
it 'returns the description if it has a value' do
runner = build(:ci_runner, description: 'Linux/Ruby-1.9.3-p448')
expect(runner.display_name).to eq 'Linux/Ruby-1.9.3-p448'
end
it 'returns the token if it does not have a description' do
runner = create(:ci_runner)
expect(runner.display_name).to eq runner.description
end
it 'returns the token if the description is an empty string' do
runner = build(:ci_runner, description: '', token: 'token')
expect(runner.display_name).to eq runner.token
end
end
describe '#only_for' do
let_it_be_with_reload(:runner) { create(:ci_runner, :project, projects: [project]) }
subject { runner.only_for?(project) }
context 'with matching project' do
it { is_expected.to be_truthy }
end
context 'without matching project' do
let_it_be(:project) { create(:project) }
it { is_expected.to be_falsey }
end
context 'with runner having multiple projects' do
let_it_be(:other_project) { create(:project) }
let_it_be(:runner_project) { create(:ci_runner_project, project: other_project, runner: runner) }
it { is_expected.to be_falsey }
end
end
describe '#assign_to' do
let_it_be(:project) { create(:project) }
subject(:assign_to) { runner.assign_to(project) }
context 'with instance runner' do
let(:runner) { create(:ci_runner, :instance) }
it 'raises an error' do
expect { assign_to }
.to raise_error(ArgumentError, 'Transitioning an instance runner to a project runner is not supported')
end
end
context 'with group runner' do
let(:group) { create(:group) }
let(:runner) { create(:ci_runner, :group, groups: [group]) }
it 'raises an error' do
expect { assign_to }
.to raise_error(ArgumentError, 'Transitioning a group runner to a project runner is not supported')
end
end
context 'with project runner' do
let_it_be(:other_project) { create(:project) }
let(:runner) { create(:ci_runner, :project, projects: [other_project]) }
it 'assigns runner to project' do
expect(assign_to).to be_truthy
expect(runner).to be_project_type
expect(runner.runner_projects.pluck(:project_id)).to contain_exactly(project.id, other_project.id)
end
it 'does not change sharding_key_id' do
expect { assign_to }.not_to change { runner.sharding_key_id }.from(other_project.id)
end
end
end
describe '.recent', :freeze_time do
subject { described_class.recent }
let!(:runner1) { create(:ci_runner, :unregistered, :created_within_stale_deadline) }
let!(:runner2) { create(:ci_runner, :unregistered, :stale) }
let!(:runner3) { create(:ci_runner, :created_within_stale_deadline, :contacted_within_stale_deadline) }
let!(:runner4) { create(:ci_runner, :stale, :contacted_within_stale_deadline) }
let!(:runner5) { create(:ci_runner, :stale) }
it { is_expected.to contain_exactly(runner1, runner3, runner4) }
end
describe '.active' do
subject { described_class.active(active_value) }
let_it_be(:runner1) { create(:ci_runner, :instance, :paused) }
let_it_be(:runner2) { create(:ci_runner, :instance) }
context 'with active_value set to false' do
let(:active_value) { false }
it 'returns paused runners' do
is_expected.to contain_exactly(runner1)
end
end
context 'with active_value set to true' do
let(:active_value) { true }
it 'returns active runners' do
is_expected.to contain_exactly(runner2)
end
end
end
describe '.paused' do
subject(:paused) { described_class.paused }
let!(:runner1) { create(:ci_runner, :instance, :paused) }
let!(:runner2) { create(:ci_runner, :instance) }
it 'returns paused runners' do
expect(described_class).to receive(:active).with(false).and_call_original
expect(paused).to contain_exactly(runner1)
end
end
describe '.with_creator_id' do
let_it_be(:admin) { create(:admin, username: 'root') }
let_it_be(:user2) { create(:user, username: 'user2') }
let_it_be(:user_runner1) { create(:ci_runner, creator: user2) }
let_it_be(:admin_runner1) { create(:ci_runner, creator: admin) }
let_it_be(:admin_runner2) { create(:ci_runner, creator: admin) }
let_it_be(:runner_without_creator) { create(:ci_runner, creator: nil) }
subject { described_class.with_creator_id(admin.id.to_s) }
it { is_expected.to contain_exactly(admin_runner1, admin_runner2) }
end
describe '.with_version_prefix' do
subject { described_class.with_version_prefix('15.11.') }
let_it_be(:runner1) { create(:ci_runner) }
let_it_be(:runner2) { create(:ci_runner) }
let_it_be(:runner3) { create(:ci_runner) }
before_all do
create(:ci_runner_machine, runner: runner1, version: '15.11.0')
create(:ci_runner_machine, runner: runner2, version: '15.9.0')
create(:ci_runner_machine, runner: runner3, version: '15.9.0')
# Add another runner_machine to runner3 to ensure edge case is handled (searching multiple machines in a single runner)
create(:ci_runner_machine, runner: runner3, version: '15.11.5')
end
it 'returns runners containing runner managers with versions starting with 15.11.' do
is_expected.to contain_exactly(runner1, runner3)
end
end
describe '#stale?', :clean_gitlab_redis_cache, :freeze_time do
let(:runner) { build(:ci_runner, :instance) }
subject { runner.stale? }
before do
allow(Ci::Runners::ProcessRunnerVersionUpdateWorker).to receive(:perform_async).once
end
context 'table tests' do
using RSpec::Parameterized::TableSyntax
let(:stale_deadline) { described_class.stale_deadline }
let(:almost_stale_deadline) { 1.second.after(stale_deadline) }
where(:created_at, :contacted_at, :expected_stale?) do
nil | nil | false
ref(:stale_deadline) | ref(:stale_deadline) | true
ref(:stale_deadline) | ref(:almost_stale_deadline) | false
ref(:stale_deadline) | nil | true
ref(:almost_stale_deadline) | nil | false
end
with_them do
before do
runner.created_at = created_at
runner.contacted_at = contacted_at
end
it { is_expected.to eq(expected_stale?) }
context 'with cache value' do
before do
stub_redis_runner_contacted_at(contacted_at.to_s)
end
it { is_expected.to eq(expected_stale?) }
end
def stub_redis_runner_contacted_at(value)
return unless created_at
Gitlab::Redis::Cache.with do |redis|
cache_key = runner.send(:cache_attribute_key)
expect(redis).to receive(:get).with(cache_key)
.and_return({ contacted_at: value }.to_json).at_least(:once)
end
end
end
end
end
describe '#online?', :clean_gitlab_redis_cache, :freeze_time do
subject { runner.online? }
context 'never contacted' do
let(:runner) { build(:ci_runner, :unregistered) }
it { is_expected.to be_falsey }
end
context 'contacted long time ago' do
let(:runner) { build(:ci_runner, :stale) }
it { is_expected.to be_falsey }
end
context 'almost offline' do
let(:runner) { build(:ci_runner, :almost_offline) }
it { is_expected.to be_truthy }
end
context 'with cache value' do
let(:runner) { create(:ci_runner, :stale) }
before do
stub_redis_runner_contacted_at(cached_contacted_at.to_s)
end
context 'contacted long time ago' do
let(:cached_contacted_at) { runner.uncached_contacted_at }
it { is_expected.to be_falsey }
end
context 'contacted 1s ago' do
let(:cached_contacted_at) { 1.second.ago }
it { is_expected.to be_truthy }
end
def stub_redis_runner_contacted_at(value)
Gitlab::Redis::Cache.with do |redis|
cache_key = runner.send(:cache_attribute_key)
expect(redis).to receive(:get).with(cache_key)
.and_return({ contacted_at: value }.to_json).at_least(:once)
end
end
end
end
describe '.with_executing_builds' do
subject(:scope) { described_class.with_executing_builds }
let_it_be(:runners_by_status) do
Ci::HasStatus::AVAILABLE_STATUSES.index_with { |_status| create(:ci_runner) }
end
let_it_be(:busy_runners) do
Ci::HasStatus::EXECUTING_STATUSES.map { |status| runners_by_status[status] }
end
context 'with no builds running' do
it { is_expected.to be_empty }
end
context 'with builds' do
before_all do
pipeline = create(:ci_pipeline, :running)
Ci::HasStatus::AVAILABLE_STATUSES.each do |status|
create(:ci_build, status, runner: runners_by_status[status], pipeline: pipeline)
end
end
it { is_expected.to match_array(busy_runners) }
end
end
describe '#matches_build?' do
using RSpec::Parameterized::TableSyntax
let_it_be(:pipeline) { create(:ci_pipeline) }
let(:build) { create(:ci_build, pipeline: pipeline) }
let(:runner_project) { build.project }
let(:runner) { create(:ci_runner, :project, projects: [runner_project], tag_list: tag_list, run_untagged: run_untagged) }
let(:tag_list) { [] }
let(:run_untagged) { true }
subject { runner.matches_build?(build) }
context 'when runner does not have tags' do
it { is_expected.to be_truthy }
it 'cannot handle build with tags' do
build.tag_list = ['aa']
is_expected.to be_falsey
end
end
context 'when runner has tags' do
let(:tag_list) { %w[bb cc] }
shared_examples 'tagged build picker' do
it 'can handle build with matching tags' do
build.tag_list = ['bb']
is_expected.to be_truthy
end
it 'cannot handle build without matching tags' do
build.tag_list = ['aa']
is_expected.to be_falsey
end
end
context 'when runner can pick untagged jobs' do
it { is_expected.to be_truthy }
it_behaves_like 'tagged build picker'
end
context 'when runner cannot pick untagged jobs' do
let(:run_untagged) { false }
it { is_expected.to be_falsey }
it_behaves_like 'tagged build picker'
end
end
context 'when runner is shared' do
let(:runner) { create(:ci_runner, :instance) }
it { is_expected.to be_truthy }
context 'when runner is locked' do
let(:runner) { create(:ci_runner, :instance, locked: true) }
it { is_expected.to be_truthy }
end
it 'does not query for owned or instance runners' do
expect(described_class).not_to receive(:owned_or_instance_wide)
subject
end
end
context 'when runner is not shared' do
context 'when runner is assigned to a project' do
it { is_expected.to be_truthy }
end
context 'when runner is assigned to a group' do
let(:group) { create(:group, projects: [build.project]) }
let(:runner) { create(:ci_runner, :group, tag_list: tag_list, run_untagged: run_untagged, groups: [group]) }
it { is_expected.to be_truthy }
it 'knows namespace id it is assigned to' do
expect(runner.namespace_ids).to eq [group.id]
end
end
end
context 'when access_level of runner is not_protected' do
before do
runner.not_protected!
end
context 'when build is protected' do
before do
build.protected = true
end
it { is_expected.to be_truthy }
end
context 'when build is unprotected' do
before do
build.protected = false
end
it { is_expected.to be_truthy }
end
end
context 'when access_level of runner is ref_protected' do
before do
runner.ref_protected!
end
context 'when build is protected' do
before do
build.protected = true
end
it { is_expected.to be_truthy }
end
context 'when build is unprotected' do
before do
build.protected = false
end
it { is_expected.to be_falsey }
end
end
context 'matches tags' do
where(:run_untagged, :runner_tags, :build_tags, :result) do
true | [] | [] | true
true | [] | ['a'] | false
true | %w[a b] | ['a'] | true
true | ['a'] | %w[a b] | false
true | ['a'] | ['a'] | true
false | ['a'] | ['a'] | true
false | ['b'] | ['a'] | false
false | %w[a b] | ['a'] | true
end
with_them do
let(:tag_list) { runner_tags }
before do
build.tag_list = build_tags
end
it { is_expected.to eq(result) }
end
end
end
describe '#status', :freeze_time do
let(:runner) { build(:ci_runner, *Array.wrap(traits)) }
subject { runner.status }
context 'stale, never contacted' do
let(:traits) { %i[unregistered stale] }
it { is_expected.to eq(:stale) }
context 'created recently, never contacted' do
let(:traits) { %i[unregistered online] }
it { is_expected.to eq(:never_contacted) }
end
end
context 'online, paused' do
let(:traits) { %i[paused online] }
it { is_expected.to eq(:online) }
end
context 'online' do
let(:traits) { :almost_offline }
it { is_expected.to eq(:online) }
end
context 'offline' do
let(:traits) { :offline }
it { is_expected.to eq(:offline) }
end
context 'stale' do
let(:traits) { :stale }
it { is_expected.to eq(:stale) }
end
end
describe '#deprecated_rest_status', :freeze_time do
let(:runner) { build(:ci_runner, *Array.wrap(traits)) }
subject { runner.deprecated_rest_status }
context 'never connected' do
let(:traits) { :unregistered }
it { is_expected.to eq(:never_contacted) }
end
context 'contacted recently' do
let(:traits) { :almost_offline }
it { is_expected.to eq(:online) }
end
context 'contacted long time ago' do
let(:traits) { :stale }
it { is_expected.to eq(:stale) }
end
context 'paused' do
let(:traits) { %i[paused online] }
it { is_expected.to eq(:paused) }
end
end
describe '#tick_runner_queue' do
let(:runner) { build(:ci_runner) }
it 'returns a new last_update value' do
expect(runner.tick_runner_queue).not_to be_empty
end
it 'sticks the runner to the primary and calls the original method' do
runner = create(:ci_runner)
expect(described_class.sticking).to receive(:stick).with(:runner, runner.id)
expect(Gitlab::Workhorse).to receive(:set_key_and_notify)
runner.tick_runner_queue
end
end
describe '#ensure_runner_queue_value' do
let(:runner) { create(:ci_runner) }
it 'sets a new last_update value when it is called the first time' do
last_update = runner.ensure_runner_queue_value
expect(value_in_queues).to eq(last_update)
end
it 'does not change if it is not expired and called again' do
last_update = runner.ensure_runner_queue_value
expect(runner.ensure_runner_queue_value).to eq(last_update)
expect(value_in_queues).to eq(last_update)
end
context 'updates runner queue after changing editable value' do
let!(:last_update) { runner.ensure_runner_queue_value }
before do
Ci::Runners::UpdateRunnerService.new(nil, runner).execute(description: 'new runner')
end
it 'sets a new last_update value' do
expect(value_in_queues).not_to eq(last_update)
end
end
context 'does not update runner value after save' do
let!(:last_update) { runner.ensure_runner_queue_value }
before do
runner.touch
end
it 'has an old last_update value' do
expect(value_in_queues).to eq(last_update)
end
end
def value_in_queues
Gitlab::Redis::Workhorse.with do |redis|
runner_queue_key = runner.send(:runner_queue_key)
redis.get(runner_queue_key)
end
end
end
describe '#heartbeat', :freeze_time do
subject(:heartbeat) do
runner.heartbeat
end
context 'when database was updated recently' do
let(:runner) { create(:ci_runner, :almost_offline) }
it 'updates cache' do
expect_redis_update
heartbeat
end
end
context 'when database was not updated recently' do
context 'with invalid runner' do
let(:runner) { create(:ci_runner, :offline, :project, :without_projects) }
it 'still updates contacted at in redis cache and database' do
expect(runner).to be_invalid
expect_redis_update(contacted_at: Time.current, creation_state: :finished)
expect { heartbeat }.to change { runner.reload.read_attribute(:contacted_at) }
end
it 'only updates contacted at in redis cache and database' do
expect_redis_update(contacted_at: Time.current, creation_state: :finished)
expect { heartbeat }.to change { runner.reload.read_attribute(:contacted_at) }
end
end
end
def expect_redis_update(values = anything)
values_json = values == anything ? anything : Gitlab::Json.dump(values)
Gitlab::Redis::Cache.with do |redis|
redis_key = runner.send(:cache_attribute_key)
expect(redis).to receive(:set).with(redis_key, values_json, any_args).and_call_original
end
end
def does_db_update
expect { heartbeat }.to change { runner.reload.read_attribute(:contacted_at) }
end
end
describe '#clear_heartbeat', :freeze_time do
let!(:runner) { create(:ci_runner) }
it 'clears contacted at' do
expect do
runner.heartbeat
end.to change { runner.reload.contacted_at }.from(nil).to(Time.current)
.and change { runner.reload.uncached_contacted_at }.from(nil).to(Time.current)
expect do
runner.clear_heartbeat
end.to change { runner.reload.contacted_at }.from(Time.current).to(nil)
.and change { runner.reload.uncached_contacted_at }.from(Time.current).to(nil)
end
end
describe '#destroy' do
let(:runner) { create(:ci_runner) }
context 'when there is a tick in the queue' do
let!(:queue_key) { runner.send(:runner_queue_key) }
before do
runner.tick_runner_queue
end
it 'cleans up the queue' do
expect(Gitlab::Workhorse).to receive(:cleanup_key).with(queue_key)
runner.destroy!
end
end
end
describe '.assignable_for' do
let_it_be(:project) { create(:project) }
let_it_be(:group) { create(:group) }
let_it_be(:another_project) { create(:project) }
let_it_be(:unlocked_project_runner) { create(:ci_runner, :project, projects: [project]) }
let_it_be(:locked_project_runner) { create(:ci_runner, :project, locked: true, projects: [project]) }
let_it_be(:group_runner) { create(:ci_runner, :group, groups: [group]) }
let_it_be(:instance_runner) { create(:ci_runner, :instance) }
context 'with already assigned project' do
subject { described_class.assignable_for(project) }
it { is_expected.to be_empty }
end
context 'with a different project' do
subject { described_class.assignable_for(another_project) }
it { is_expected.to include(unlocked_project_runner) }
it { is_expected.not_to include(group_runner) }
it { is_expected.not_to include(locked_project_runner) }
it { is_expected.not_to include(instance_runner) }
end
end
describe 'Project-related queries' do
let_it_be(:project1) { create(:project) }
let_it_be(:project2) { create(:project) }
describe '#owner' do
subject(:owner) { project_runner.owner }
context 'with project1 as first project associated with runner' do
let_it_be(:project_runner) { create(:ci_runner, :project, projects: [project1, project2]) }
it { is_expected.to eq project1 }
end
context 'with project2 as first project associated with runner' do
let_it_be(:project_runner) { create(:ci_runner, :project, projects: [project2, project1]) }
it { is_expected.to eq project2 }
end
end
describe '#belongs_to_one_project?' do
it "returns false if there are two projects runner is assigned to" do
runner = create(:ci_runner, :project, projects: [project1, project2])
expect(runner.belongs_to_one_project?).to be_falsey
end
it 'returns true if there is only one project runner is assigned to' do
runner = create(:ci_runner, :project, projects: [project1])
expect(runner.belongs_to_one_project?).to be_truthy
end
end
describe '#belongs_to_more_than_one_project?' do
context 'project runner' do
context 'two projects assigned to runner' do
let(:runner) { create(:ci_runner, :project, projects: [project1, project2]) }
it 'returns true' do
expect(runner.belongs_to_more_than_one_project?).to be_truthy
end
end
context 'one project assigned to runner' do
let(:runner) { create(:ci_runner, :project, projects: [project1]) }
it 'returns false' do
expect(runner.belongs_to_more_than_one_project?).to be_falsey
end
end
end
context 'group runner' do
let(:group) { create(:group) }
let(:runner) { create(:ci_runner, :group, groups: [group]) }
it 'returns false' do
expect(runner.belongs_to_more_than_one_project?).to be_falsey
end
end
context 'shared runner' do
let(:runner) { create(:ci_runner, :instance) }
it 'returns false' do
expect(runner.belongs_to_more_than_one_project?).to be_falsey
end
end
end
end
describe '#save_tags' do
let(:runner) { build(:ci_runner, tag_list: ['tag']) }
it 'saves tags' do
runner.save!
expect(runner.tags.count).to eq(1)
expect(runner.tags.first.name).to eq('tag')
end
it 'strips tags' do
runner.tag_list = [' taga', 'tagb ', ' tagc ']
runner.save!
expect(runner.tags.map(&:name)).to match_array(%w[taga tagb tagc])
end
context 'with BulkInsertableTags.with_bulk_insert_tags' do
it 'does not save_tags' do
Ci::BulkInsertableTags.with_bulk_insert_tags do
runner.save!
end
expect(runner.tags).to be_empty
end
context 'over TAG_LIST_MAX_LENGTH' do
let(:tag_list) { (1..described_class::TAG_LIST_MAX_LENGTH + 1).map { |i| "tag#{i}" } }
let(:runner) { build(:ci_runner, tag_list: tag_list) }
it 'fails validation if over tag limit' do
Ci::BulkInsertableTags.with_bulk_insert_tags do
expect { runner.save! }.to raise_error(ActiveRecord::RecordInvalid)
end
expect(runner.tags).to be_empty
end
end
end
end
describe '#has_tags?' do
context 'when runner has tags' do
subject { build(:ci_runner, tag_list: ['tag']) }
it { is_expected.to have_tags }
end
context 'when runner does not have tags' do
subject { build(:ci_runner, tag_list: []) }
it { is_expected.not_to have_tags }
end
end
describe '.search' do
let_it_be(:runner) { create(:ci_runner, token: '123abc', description: 'test runner') }
it 'returns runners with a matching token' do
expect(described_class.search(runner.token)).to eq([runner])
end
it 'does not return runners with a partially matching token' do
expect(described_class.search(runner.token[0..2])).to be_empty
end
it 'does not return runners with a matching token with different casing' do
expect(described_class.search(runner.token.upcase)).to be_empty
end
it 'returns runners with a matching description' do
expect(described_class.search(runner.description)).to eq([runner])
end
it 'returns runners with a partially matching description' do
expect(described_class.search(runner.description[0..2])).to eq([runner])
end
it 'returns runners with a matching description regardless of the casing' do
expect(described_class.search(runner.description.upcase)).to eq([runner])
end
end
describe '#pick_build!' do
let_it_be(:runner) { create(:ci_runner) }
let(:build) { create(:ci_build) }
context 'runner can pick the build' do
it 'calls #tick_runner_queue' do
expect(runner).to receive(:tick_runner_queue)
runner.pick_build!(build)
end
end
context 'runner cannot pick the build' do
before do
build.tag_list = [:docker]
end
it 'does not call #tick_runner_queue' do
expect(runner).not_to receive(:tick_runner_queue)
runner.pick_build!(build)
end
end
end
describe 'project runner without projects is destroyable' do
let!(:runner) { create(:ci_runner, :project, :without_projects) }
subject(:destroy!) { runner.destroy! }
it 'does not have projects' do
expect(runner.runner_projects).to be_empty
end
it 'can be destroyed' do
expect { destroy! }.to change { described_class.count }.by(-1)
end
end
describe '.order_by' do
let_it_be(:runner1) { create(:ci_runner, created_at: 1.year.ago, contacted_at: 1.year.ago) }
let_it_be(:runner2) { create(:ci_runner, created_at: 1.month.ago, contacted_at: 1.month.ago) }
before do
runner1.update!(token_expires_at: 1.year.from_now)
end
it 'supports ordering by the contact date' do
runners = described_class.order_by('contacted_asc')
expect(runners).to eq([runner1, runner2])
end
it 'supports ordering by the creation date' do
runners = described_class.order_by('created_asc')
expect(runners).to eq([runner2, runner1])
end
it 'supports ordering by the token expiration' do
runner3 = create(:ci_runner)
runner3.update!(token_expires_at: 1.month.from_now)
runners = described_class.order_by('token_expires_at_asc')
expect(runners).to eq([runner3, runner1, runner2])
runners = described_class.order_by('token_expires_at_desc')
expect(runners).to eq([runner2, runner1, runner3])
end
end
describe '.runner_matchers' do
subject(:matchers) { described_class.all.runner_matchers }
context 'deduplicates on runner_type' do
before do
create_list(:ci_runner, 2, :instance)
create_list(:ci_runner, 2, :project, projects: [project])
end
it 'creates two matchers' do
expect(matchers.size).to eq(2)
expect(matchers.map(&:runner_type)).to match_array(%w[instance_type project_type])
end
end
context 'deduplicates on public_projects_minutes_cost_factor' do
before do
create_list(:ci_runner, 2, public_projects_minutes_cost_factor: 5)
create_list(:ci_runner, 2, public_projects_minutes_cost_factor: 10)
end
it 'creates two matchers' do
expect(matchers.size).to eq(2)
expect(matchers.map(&:public_projects_minutes_cost_factor)).to match_array([5, 10])
end
end
context 'deduplicates on private_projects_minutes_cost_factor' do
before do
create_list(:ci_runner, 2, private_projects_minutes_cost_factor: 5)
create_list(:ci_runner, 2, private_projects_minutes_cost_factor: 10)
end
it 'creates two matchers' do
expect(matchers.size).to eq(2)
expect(matchers.map(&:private_projects_minutes_cost_factor)).to match_array([5, 10])
end
end
context 'deduplicates on run_untagged' do
before do
create_list(:ci_runner, 2, run_untagged: true, tag_list: ['a'])
create_list(:ci_runner, 2, run_untagged: false, tag_list: ['a'])
end
it 'creates two matchers' do
expect(matchers.size).to eq(2)
expect(matchers.map(&:run_untagged)).to match_array([true, false])
end
end
context 'deduplicates on access_level' do
before do
create_list(:ci_runner, 2, access_level: :ref_protected)
create_list(:ci_runner, 2, access_level: :not_protected)
end
it 'creates two matchers' do
expect(matchers.size).to eq(2)
expect(matchers.map(&:access_level)).to match_array(%w[ref_protected not_protected])
end
end
context 'deduplicates on tag_list' do
before do
create_list(:ci_runner, 2, tag_list: %w[tag1 tag2])
create_list(:ci_runner, 2, tag_list: %w[tag3 tag4])
end
it 'creates two matchers' do
expect(matchers.size).to eq(2)
expect(matchers.map(&:tag_list)).to match_array([%w[tag1 tag2], %w[tag3 tag4]])
end
end
context 'deduplicates on allowed_plan_ids' do
before do
create_list(:ci_runner, 2, allowed_plan_ids: [1, 2])
create_list(:ci_runner, 2, allowed_plan_ids: [3, 4])
end
it 'creates two matchers' do
expect(matchers.size).to eq(2)
expect(matchers.map(&:allowed_plan_ids)).to match_array([[1, 2], [3, 4]])
end
end
context 'with runner_ids' do
before do
create_list(:ci_runner, 2)
end
it 'includes runner_ids' do
expect(matchers.size).to eq(1)
expect(matchers.first.runner_ids).to match_array(described_class.all.pluck(:id))
end
end
end
describe '#runner_matcher' do
let(:runner) do
build_stubbed(:ci_runner, :instance_type, tag_list: %w[tag1 tag2], allowed_plan_ids: [1, 2])
end
subject(:matcher) { runner.runner_matcher }
it { expect(matcher.runner_ids).to eq([runner.id]) }
it { expect(matcher.runner_type).to eq(runner.runner_type) }
it { expect(matcher.public_projects_minutes_cost_factor).to eq(runner.public_projects_minutes_cost_factor) }
it { expect(matcher.private_projects_minutes_cost_factor).to eq(runner.private_projects_minutes_cost_factor) }
it { expect(matcher.run_untagged).to eq(runner.run_untagged) }
it { expect(matcher.access_level).to eq(runner.access_level) }
it { expect(matcher.tag_list).to match_array(runner.tag_list) }
it { expect(matcher.allowed_plan_ids).to match_array(runner.allowed_plan_ids) }
end
describe '#uncached_contacted_at' do
let(:contacted_at_stored) { 1.hour.ago.change(usec: 0) }
let(:runner) { create(:ci_runner, contacted_at: contacted_at_stored) }
subject { runner.uncached_contacted_at }
it { is_expected.to eq(contacted_at_stored) }
end
describe 'Group-related queries' do
# Groups
let_it_be(:top_level_group) { create(:group) }
let_it_be(:child_group) { create(:group, parent: top_level_group) }
let_it_be(:child_group2) { create(:group, parent: top_level_group) }
let_it_be(:other_top_level_group) { create(:group) }
# Projects
let_it_be(:top_level_group_project) { create(:project, group: top_level_group) }
let_it_be(:child_group_project) { create(:project, group: child_group) }
let_it_be(:other_top_level_group_project) { create(:project, group: other_top_level_group) }
# Runners
let_it_be(:instance_runner) { create(:ci_runner, :instance) }
let_it_be(:top_level_group_runner) { create(:ci_runner, :group, groups: [top_level_group]) }
let_it_be(:child_group_runner) { create(:ci_runner, :group, groups: [child_group]) }
let_it_be(:child_group2_runner) { create(:ci_runner, :group, groups: [child_group2]) }
let_it_be(:other_top_level_group_runner) do
create(:ci_runner, :group, groups: [other_top_level_group])
end
let_it_be(:top_level_group_project_runner) do
create(:ci_runner, :project, projects: [top_level_group_project])
end
let_it_be(:child_group_project_runner) do
create(:ci_runner, :project, projects: [child_group_project])
end
let_it_be(:other_top_level_group_project_runner) do
create(:ci_runner, :project, projects: [other_top_level_group_project])
end
let_it_be(:shared_top_level_group_project_runner) do
create(:ci_runner, :project, projects: [top_level_group_project, child_group_project])
end
describe '.belonging_to_group' do
subject(:relation) { described_class.belonging_to_group(scope.id) }
context 'with scope set to top_level_group' do
let(:scope) { top_level_group }
it 'returns the group runners from the top_level_group' do
is_expected.to contain_exactly(top_level_group_runner)
end
end
context 'with scope set to child_group' do
let(:scope) { child_group }
it 'returns the group runners from the child_group' do
is_expected.to contain_exactly(child_group_runner)
end
end
end
describe '.belonging_to_group_and_ancestors' do
subject(:relation) { described_class.belonging_to_group_and_ancestors(child_group.id) }
it 'returns the group runners from the group and parent group' do
is_expected.to contain_exactly(child_group_runner, top_level_group_runner)
end
end
describe '.belonging_to_group_or_project_descendants' do
subject(:relation) { described_class.belonging_to_group_or_project_descendants(scope.id) }
context 'with scope set to top_level_group' do
let(:scope) { top_level_group }
it 'returns the expected group and project runners without duplicates', :aggregate_failures do
expect(relation).to contain_exactly(
top_level_group_runner,
top_level_group_project_runner,
child_group_runner,
child_group_project_runner,
child_group2_runner,
shared_top_level_group_project_runner
)
# Ensure no duplicates are returned
expect(relation.distinct).to match_array(relation)
end
end
context 'with scope set to child_group' do
let(:scope) { child_group }
it 'returns the expected group and project runners without duplicates', :aggregate_failures do
expect(relation).to contain_exactly(
child_group_runner,
child_group_project_runner,
shared_top_level_group_project_runner
)
# Ensure no duplicates are returned
expect(relation.distinct).to match_array(relation)
end
end
end
describe '.usable_from_scope' do
subject(:relation) { described_class.usable_from_scope(scope) }
context 'with scope set to top_level_group' do
let(:scope) { top_level_group }
it 'returns all runners usable from top_level_group without duplicates' do
expect(relation).to contain_exactly(
instance_runner,
top_level_group_runner,
top_level_group_project_runner,
child_group_runner,
child_group_project_runner,
child_group2_runner,
shared_top_level_group_project_runner
)
# Ensure no duplicates are returned
expect(relation.distinct).to match_array(relation)
end
end
context 'with scope set to child_group' do
let(:scope) { child_group }
it 'returns all runners usable from child_group' do
expect(relation).to contain_exactly(
instance_runner,
top_level_group_runner,
child_group_runner,
child_group_project_runner,
shared_top_level_group_project_runner
)
end
end
context 'with scope set to other_top_level_group' do
let(:scope) { other_top_level_group }
it 'returns all runners usable from other_top_level_group' do
expect(relation).to contain_exactly(
instance_runner,
other_top_level_group_runner,
other_top_level_group_project_runner
)
end
end
end
describe '#owner' do
subject(:owner) { runner.owner }
context 'with runner assigned to child_group' do
let(:runner) { child_group_runner }
it { is_expected.to eq child_group }
end
context 'with runner assigned to top_level_group_runner' do
let(:runner) { top_level_group_runner }
it { is_expected.to eq top_level_group }
end
end
end
describe '#short_sha' do
subject(:short_sha) { runner.short_sha }
context 'when registered via command-line' do
let(:runner) { create(:ci_runner) }
specify { expect(runner.token).not_to start_with(described_class::CREATED_RUNNER_TOKEN_PREFIX) }
it { is_expected.to match(/[0-9a-zA-Z_-]{8}/) }
it { is_expected.not_to start_with('t1_') }
it { is_expected.not_to start_with(described_class::CREATED_RUNNER_TOKEN_PREFIX) }
end
context 'when creating new runner via UI' do
let(:runner) { create(:ci_runner, registration_type: :authenticated_user) }
specify { expect(runner.token).to start_with(described_class::CREATED_RUNNER_TOKEN_PREFIX) }
it { is_expected.to match(/[0-9a-zA-Z_-]{8}/) }
it { is_expected.not_to start_with('t1_') }
it { is_expected.not_to start_with(described_class::CREATED_RUNNER_TOKEN_PREFIX) }
end
end
describe '#token' do
subject(:token) { runner.token }
let(:runner_type) { :instance_type }
let(:attrs) { {} }
let(:runner) { create(:ci_runner, runner_type, registration_type: registration_type, **attrs) }
context 'when runner is registered' do
let(:registration_type) { :registration_token }
it { is_expected.not_to start_with('glrt-') }
it { is_expected.to start_with('t1_') }
context 'when runner is group type' do
let(:runner_type) { :group_type }
let(:attrs) { { groups: [group] } }
it { is_expected.to start_with('t2_') }
end
context 'when runner is project type' do
let(:runner_type) { :project_type }
let(:attrs) { { projects: [project] } }
it { is_expected.to start_with('t3_') }
end
end
context 'when runner is created via UI' do
let(:registration_type) { :authenticated_user }
it { is_expected.to start_with('glrt-t1_') }
context 'when runner is group type' do
let(:runner_type) { :group_type }
let(:attrs) { { groups: [group] } }
it { is_expected.to start_with('glrt-t2_') }
end
context 'when runner is project type' do
let(:runner_type) { :project_type }
let(:attrs) { { projects: [project] } }
it { is_expected.to start_with('glrt-t3_') }
end
end
end
describe '#token_expires_at', :freeze_time do
shared_examples 'expiring token' do |interval:|
it 'expires' do
expect(runner.token_expires_at).to eq(interval.from_now)
end
end
shared_examples 'non-expiring token' do
it 'does not expire' do
expect(runner.token_expires_at).to be_nil
end
end
context 'no expiration' do
let(:runner) { create(:ci_runner) }
it_behaves_like 'non-expiring token'
end
context 'system-wide shared expiration' do
before do
stub_application_setting(runner_token_expiration_interval: 5.days.to_i)
end
let(:runner) { create(:ci_runner) }
it_behaves_like 'expiring token', interval: 5.days
end
context 'system-wide group expiration' do
before do
stub_application_setting(group_runner_token_expiration_interval: 5.days.to_i)
end
let(:runner) { create(:ci_runner) }
it_behaves_like 'non-expiring token'
end
context 'system-wide project expiration' do
before do
stub_application_setting(project_runner_token_expiration_interval: 5.days.to_i)
end
let(:runner) { create(:ci_runner) }
it_behaves_like 'non-expiring token'
end
context 'group expiration' do
let(:group_settings) { create(:namespace_settings, runner_token_expiration_interval: 6.days.to_i) }
let(:group) { create(:group, namespace_settings: group_settings) }
let(:runner) { create(:ci_runner, :group, groups: [group]) }
it_behaves_like 'expiring token', interval: 6.days
end
context 'human-readable group expiration' do
let(:group_settings) { create(:namespace_settings, runner_token_expiration_interval_human_readable: '7 days') }
let(:group) { create(:group, namespace_settings: group_settings) }
let(:runner) { create(:ci_runner, :group, groups: [group]) }
it_behaves_like 'expiring token', interval: 7.days
end
context 'project expiration' do
let(:project) { create(:project, runner_token_expiration_interval: 4.days.to_i).tap(&:save!) }
let(:runner) { create(:ci_runner, :project, projects: [project]) }
it_behaves_like 'expiring token', interval: 4.days
end
context 'human-readable project expiration' do
let(:project) { create(:project, runner_token_expiration_interval_human_readable: '5 days').tap(&:save!) }
let(:runner) { create(:ci_runner, :project, projects: [project]) }
it_behaves_like 'expiring token', interval: 5.days
end
context 'multiple projects' do
let(:project1) { create(:project, runner_token_expiration_interval: 8.days.to_i).tap(&:save!) }
let(:project2) { create(:project, runner_token_expiration_interval: 7.days.to_i).tap(&:save!) }
let(:project3) { create(:project, runner_token_expiration_interval: 9.days.to_i).tap(&:save!) }
let(:runner) { create(:ci_runner, :project, projects: [project1, project2, project3]) }
it_behaves_like 'expiring token', interval: 7.days
end
context 'with project runner token expiring' do
let_it_be(:project) { create(:project, runner_token_expiration_interval: 4.days.to_i).tap(&:save!) }
context 'project overrides system' do
before do
stub_application_setting(project_runner_token_expiration_interval: 5.days.to_i)
end
let(:runner) { create(:ci_runner, :project, projects: [project]) }
it_behaves_like 'expiring token', interval: 4.days
end
context 'system overrides project' do
before do
stub_application_setting(project_runner_token_expiration_interval: 3.days.to_i)
end
let(:runner) { create(:ci_runner, :project, projects: [project]) }
it_behaves_like 'expiring token', interval: 3.days
end
end
context 'with group runner token expiring' do
let_it_be(:group_settings) { create(:namespace_settings, runner_token_expiration_interval: 4.days.to_i) }
let_it_be(:group) { create(:group, namespace_settings: group_settings) }
context 'group overrides system' do
before do
stub_application_setting(group_runner_token_expiration_interval: 5.days.to_i)
end
let(:runner) { create(:ci_runner, :group, groups: [group]) }
it_behaves_like 'expiring token', interval: 4.days
end
context 'system overrides group' do
before do
stub_application_setting(group_runner_token_expiration_interval: 3.days.to_i)
end
let(:runner) { create(:ci_runner, :group, groups: [group]) }
it_behaves_like 'expiring token', interval: 3.days
end
end
context "with group's project runner token expiring" do
let_it_be(:parent_group_settings) { create(:namespace_settings, subgroup_runner_token_expiration_interval: 2.days.to_i) }
let_it_be(:parent_group) { create(:group, namespace_settings: parent_group_settings) }
context 'parent group overrides subgroup' do
let(:group_settings) { create(:namespace_settings, runner_token_expiration_interval: 3.days.to_i) }
let(:group) { create(:group, parent: parent_group, namespace_settings: group_settings) }
let(:runner) { create(:ci_runner, :group, groups: [group]) }
it_behaves_like 'expiring token', interval: 2.days
end
context 'subgroup overrides parent group' do
let(:group_settings) { create(:namespace_settings, runner_token_expiration_interval: 1.day.to_i) }
let(:group) { create(:group, parent: parent_group, namespace_settings: group_settings) }
let(:runner) { create(:ci_runner, :group, groups: [group]) }
it_behaves_like 'expiring token', interval: 1.day
end
end
context "with group's project runner token expiring" do
let_it_be(:group_settings) { create(:namespace_settings, project_runner_token_expiration_interval: 2.days.to_i) }
let_it_be(:group) { create(:group, namespace_settings: group_settings) }
context 'group overrides project' do
let(:project) { create(:project, group: group, runner_token_expiration_interval: 3.days.to_i).tap(&:save!) }
let(:runner) { create(:ci_runner, :project, projects: [project]) }
it_behaves_like 'expiring token', interval: 2.days
end
context 'project overrides group' do
let(:project) { create(:project, group: group, runner_token_expiration_interval: 1.day.to_i).tap(&:save!) }
let(:runner) { create(:ci_runner, :project, projects: [project]) }
it_behaves_like 'expiring token', interval: 1.day
end
end
end
describe '.with_upgrade_status' do
subject(:scope) { described_class.with_upgrade_status(upgrade_status) }
let_it_be(:runner_14_0_0) { create(:ci_runner) }
let_it_be(:runner_14_1_0_and_14_0_0) { create(:ci_runner) }
let_it_be(:runner_14_1_0) { create(:ci_runner) }
let_it_be(:runner_14_1_1) { create(:ci_runner) }
before_all do
create(:ci_runner_machine, runner: runner_14_1_0_and_14_0_0, version: '14.0.0')
create(:ci_runner_machine, runner: runner_14_1_0_and_14_0_0, version: '14.1.0')
create(:ci_runner_machine, runner: runner_14_0_0, version: '14.0.0')
create(:ci_runner_machine, runner: runner_14_1_0, version: '14.1.0')
create(:ci_runner_machine, runner: runner_14_1_1, version: '14.1.1')
create(:ci_runner_version, version: '14.0.0', status: :available)
create(:ci_runner_version, version: '14.1.0', status: :recommended)
create(:ci_runner_version, version: '14.1.1', status: :unavailable)
end
context ':unavailable' do
let(:upgrade_status) { :unavailable }
it 'returns runners with runner managers whose version is assigned :unavailable' do
is_expected.to contain_exactly(runner_14_1_1)
end
end
context ':available' do
let(:upgrade_status) { :available }
it 'returns runners with runner managers whose version is assigned :available' do
is_expected.to contain_exactly(runner_14_0_0, runner_14_1_0_and_14_0_0)
end
end
context ':recommended' do
let(:upgrade_status) { :recommended }
it 'returns runners with runner managers whose version is assigned :recommended' do
is_expected.to contain_exactly(runner_14_1_0_and_14_0_0, runner_14_1_0)
end
end
describe 'composed with other scopes' do
subject { described_class.active(false).with_upgrade_status(:available) }
before do
create(:ci_runner_machine, runner: paused_runner_14_0_0, version: '14.0.0')
end
let(:paused_runner_14_0_0) { create(:ci_runner, :paused) }
it 'returns runner matching the composed scope' do
is_expected.to contain_exactly(paused_runner_14_0_0)
end
end
end
describe '.with_creator' do
subject { described_class.with_creator }
let!(:user) { create(:admin) }
let!(:runner) { create(:ci_runner, creator: user) }
it { is_expected.to contain_exactly(runner) }
end
describe '#ensure_token' do
let(:runner) { build(:ci_runner, registration_type: registration_type) }
let(:token) { 'an_existing_secret_token' }
let(:static_prefix) { described_class::CREATED_RUNNER_TOKEN_PREFIX }
context 'when runner is initialized without a token' do
context 'with registration_token' do
let(:registration_type) { :registration_token }
it 'generates a token' do
expect { runner.ensure_token }.to change { runner.token }.from(nil)
end
end
context 'with authenticated_user' do
let(:registration_type) { :authenticated_user }
it 'generates a token with prefix' do
expect { runner.ensure_token }.to change { runner.token }.from(nil).to(a_string_starting_with(static_prefix))
end
end
end
context 'when runner is initialized with a token' do
before do
runner.set_token(token)
end
context 'with registration_token' do
let(:registration_type) { :registration_token }
it 'does not change the existing token' do
expect { runner.ensure_token }.not_to change { runner.token }.from(token)
end
end
context 'with authenticated_user' do
let(:registration_type) { :authenticated_user }
it 'does not change the existing token' do
expect { runner.ensure_token }.not_to change { runner.token }.from(token)
end
end
end
end
describe '#gitlab_hosted?' do
using RSpec::Parameterized::TableSyntax
subject(:runner) { build_stubbed(:ci_runner) }
where(:saas, :runner_type, :expected_value) do
true | :instance_type | true
true | :group_type | false
true | :project_type | false
false | :instance_type | false
false | :group_type | false
false | :project_type | false
end
with_them do
before do
allow(Gitlab).to receive(:com?).and_return(saas)
runner.runner_type = runner_type
end
it 'returns the correct value based on saas and runner type' do
expect(runner.gitlab_hosted?).to eq(expected_value)
end
end
end
describe 'status scopes', :freeze_time do
before_all do
freeze_time # Freeze time before `let_it_be` runs, so that runner statuses are frozen during execution
end
after :all do
unfreeze_time
end
let_it_be(:online_runner) { create(:ci_runner, :instance, :almost_offline) }
let_it_be(:offline_runner) { create(:ci_runner, :instance, :offline) }
let_it_be(:never_contacted_runner) { create(:ci_runner, :instance, :unregistered) }
describe '.online' do
subject(:runners) { described_class.online }
it 'returns online runners' do
expect(runners).to contain_exactly(online_runner)
end
end
describe '.offline' do
subject(:runners) { described_class.offline }
it 'returns offline runners' do
expect(runners).to contain_exactly(offline_runner)
end
end
describe '.never_contacted' do
subject(:runners) { described_class.never_contacted }
it 'returns never contacted runners' do
expect(runners).to contain_exactly(never_contacted_runner)
end
end
describe '.stale' do
subject { described_class.stale }
let!(:stale_runner1) { create(:ci_runner, :unregistered, :stale) }
let!(:stale_runner2) { create(:ci_runner, :stale) }
it 'returns stale runners' do
is_expected.to contain_exactly(stale_runner1, stale_runner2)
end
end
include_examples 'runner with status scope'
end
describe '.available_statuses' do
subject { described_class.available_statuses }
it { is_expected.to eq(%w[active paused online offline never_contacted stale]) }
end
describe '.online_contact_time_deadline', :freeze_time do
subject { described_class.online_contact_time_deadline }
it { is_expected.to eq(2.hours.ago) }
end
describe '.stale_deadline', :freeze_time do
subject { described_class.stale_deadline }
it { is_expected.to eq(7.days.ago) }
end
describe '.with_runner_type' do
subject { described_class.with_runner_type(runner_type) }
let_it_be(:instance_runner) { create(:ci_runner, :instance) }
let_it_be(:group_runner) { create(:ci_runner, :group, groups: [group]) }
let_it_be(:project_runner) { create(:ci_runner, :project, :without_projects) }
context 'with instance_type' do
let(:runner_type) { 'instance_type' }
it { is_expected.to contain_exactly(instance_runner) }
end
context 'with group_type' do
let(:runner_type) { 'group_type' }
it { is_expected.to contain_exactly(group_runner) }
end
context 'with project_type' do
let(:runner_type) { 'project_type' }
it { is_expected.to contain_exactly(project_runner) }
end
context 'with invalid runner type' do
let(:runner_type) { 'invalid runner type' }
it { is_expected.to contain_exactly(instance_runner, group_runner, project_runner) }
end
end
end
|