Loading...
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 | # SPDX-License-Identifier: GPL-2.0+ # Copyright 2026 Simon Glass <sjg@chromium.org> """Tests for the machine module""" # pylint: disable=W0212 import json import os import unittest from unittest import mock from u_boot_pylib import command from u_boot_pylib import terminal from buildman import bsettings from buildman import machine # Base machine-info dict used by probe tests. Individual tests override # fields as needed (e.g. load_1m, mem_avail_mb) via {**MACHINE_INFO, ...}. MACHINE_INFO = { 'arch': 'x86_64', 'cpus': 4, 'threads': 8, 'bogomips': 5000.0, 'load_1m': 0.5, 'mem_avail_mb': 16000, 'disk_avail_mb': 50000, } class TestParseToolchainList(unittest.TestCase): """Test _parse_toolchain_list()""" def test_parse_normal(self): """Test parsing normal toolchain list output""" output = '''List of available toolchains (3): arm : /usr/bin/arm-linux-gnueabi-gcc aarch64 : /usr/bin/aarch64-linux-gnu-gcc sandbox : /usr/bin/gcc ''' result = machine._parse_toolchain_list(output) self.assertEqual(result, { 'arm': '/usr/bin/arm-linux-gnueabi-gcc', 'aarch64': '/usr/bin/aarch64-linux-gnu-gcc', 'sandbox': '/usr/bin/gcc', }) def test_parse_empty(self): """Test parsing empty output""" self.assertEqual(machine._parse_toolchain_list(''), {}) def test_parse_none_toolchains(self): """Test parsing when no toolchains are available""" output = '''List of available toolchains (0): None ''' result = machine._parse_toolchain_list(output) self.assertEqual(result, {}) def test_parse_with_colour(self): """Test parsing output that has extra text before the list""" output = """Some preamble text List of available toolchains (2): arm : /opt/toolchains/arm-gcc x86 : /usr/bin/x86_64-linux-gcc Some trailing text """ result = machine._parse_toolchain_list(output) self.assertEqual(result, { 'arm': '/opt/toolchains/arm-gcc', 'x86': '/usr/bin/x86_64-linux-gcc', }) class TestMachine(unittest.TestCase): """Test Machine class""" def test_init(self): """Test initial state of a Machine""" m = machine.Machine('myhost') self.assertEqual(m.hostname, 'myhost') self.assertEqual(m.info.arch, '') self.assertFalse(m.avail) self.assertEqual(m.reason, 'not probed') self.assertEqual(m.weight, 0) self.assertEqual(m.toolchains, {}) @mock.patch('buildman.machine._run_ssh') def test_probe_success(self, mock_ssh): """Test successful probe""" mock_ssh.return_value = json.dumps({ **MACHINE_INFO, 'cpus': 8, 'threads': 16, 'load_1m': 1.5}) m = machine.Machine('server1') result = m.probe() self.assertTrue(result) self.assertTrue(m.avail) self.assertEqual(m.info.cpus, 8) self.assertEqual(m.info.threads, 16) self.assertAlmostEqual(m.info.load, 1.5) self.assertEqual(m.info.mem_avail_mb, 16000) self.assertEqual(m.info.disk_avail_mb, 50000) self.assertGreater(m.weight, 0) @mock.patch('buildman.machine._run_ssh') def test_probe_busy(self, mock_ssh): """Test probe of a busy machine""" mock_ssh.return_value = json.dumps({ **MACHINE_INFO, 'load_1m': 5.0}) m = machine.Machine('busy-host') result = m.probe() self.assertTrue(result) self.assertFalse(m.avail) self.assertIn('busy', m.reason) @mock.patch('buildman.machine._run_ssh') def test_probe_low_disk(self, mock_ssh): """Test probe of a machine with low disk space""" mock_ssh.return_value = json.dumps({ **MACHINE_INFO, 'disk_avail_mb': 500}) m = machine.Machine('low-disk') result = m.probe() self.assertTrue(result) self.assertFalse(m.avail) self.assertIn('disk', m.reason) @mock.patch('buildman.machine._run_ssh') def test_probe_low_mem(self, mock_ssh): """Test probe of a machine with low memory""" mock_ssh.return_value = json.dumps({ **MACHINE_INFO, 'mem_avail_mb': 200}) m = machine.Machine('low-mem') result = m.probe() self.assertTrue(result) self.assertFalse(m.avail) self.assertIn('memory', m.reason) @mock.patch('buildman.machine._run_ssh') def test_probe_ssh_failure(self, mock_ssh): """Test probe when SSH fails""" mock_ssh.side_effect = machine.MachineError('connection refused') m = machine.Machine('bad-host') result = m.probe() self.assertFalse(result) self.assertFalse(m.avail) self.assertIn('connection refused', m.reason) @mock.patch('buildman.machine._run_ssh') def test_probe_bad_json(self, mock_ssh): """Test probe when remote returns invalid JSON""" mock_ssh.return_value = 'not json at all' m = machine.Machine('bad-json') result = m.probe() self.assertFalse(result) self.assertFalse(m.avail) self.assertIn('invalid probe response', m.reason) @mock.patch('buildman.machine._run_ssh') def test_probe_toolchains(self, mock_ssh): """Test probing toolchains""" mock_ssh.return_value = '''List of available toolchains (2): arm : /usr/bin/arm-linux-gnueabi-gcc sandbox : /usr/bin/gcc ''' m = machine.Machine('server1') archs = m.probe_toolchains('buildman') self.assertEqual(archs, { 'arm': '/usr/bin/arm-linux-gnueabi-gcc', 'sandbox': '/usr/bin/gcc', }) @mock.patch('buildman.machine._run_ssh') def test_fetch_toolchain_success(self, mock_ssh): """Test successful toolchain fetch""" mock_ssh.return_value = 'Downloading...\nDone' m = machine.Machine('server1') self.assertTrue(m.fetch_toolchain('buildman', 'arm')) @mock.patch('buildman.machine._run_ssh') def test_fetch_toolchain_failure(self, mock_ssh): """Test failed toolchain fetch""" mock_ssh.side_effect = machine.MachineError('fetch failed') m = machine.Machine('server1') self.assertFalse(m.fetch_toolchain('buildman', 'arm')) @mock.patch('buildman.machine._run_ssh') def test_weight_calculation(self, mock_ssh): """Test weight calculation based on load""" mock_ssh.return_value = json.dumps({ **MACHINE_INFO, 'cpus': 8, 'threads': 16, 'load_1m': 4.0}) m = machine.Machine('server1') m.probe() # weight = threads - int(load) = 16 - 4 = 12 self.assertEqual(m.weight, 12) @mock.patch('buildman.machine._run_ssh') def test_weight_minimum(self, mock_ssh): """Test weight is at least 1 when available""" mock_ssh.return_value = json.dumps({ **MACHINE_INFO, 'arch': 'aarch64', 'threads': 4, 'bogomips': 48.0, 'load_1m': 3.1}) m = machine.Machine('server1') m.probe() # weight = max(1, 4 - 3) = 1 self.assertEqual(m.weight, 1) def test_repr(self): """Test string representation""" m = machine.Machine('server1') self.assertIn('server1', repr(m)) self.assertIn('unavail', repr(m)) class TestMachinePool(unittest.TestCase): """Test MachinePool class""" def setUp(self): """Set up bsettings for each test""" bsettings.setup(None) def test_empty_pool(self): """Test pool with no machines configured""" pool = machine.MachinePool() self.assertEqual(pool.machines, []) self.assertEqual(pool.get_available(), []) self.assertEqual(pool.get_total_weight(), 0) def test_load_from_config(self): """Test loading machines from config with bare hostnames""" bsettings.add_file( '[machines]\n' 'ohau\n' 'moa\n' ) pool = machine.MachinePool() self.assertEqual(len(pool.machines), 2) self.assertEqual(pool.machines[0].hostname, 'ohau') self.assertEqual(pool.machines[1].hostname, 'moa') def test_load_from_config_key_value(self): """Test loading machines from config with key=value pairs""" bsettings.add_file( '[machines]\n' 'server1 = build1.example.com\n' 'server2 = user@build2.example.com\n' ) pool = machine.MachinePool() self.assertEqual(len(pool.machines), 2) self.assertEqual(pool.machines[0].hostname, 'build1.example.com') self.assertEqual(pool.machines[1].hostname, 'user@build2.example.com') @mock.patch('buildman.machine._run_ssh') def test_probe_all(self, mock_ssh): """Test probing all machines""" mock_ssh.return_value = json.dumps({ **MACHINE_INFO, 'load_1m': 1.0, 'mem_avail_mb': 8000, 'disk_avail_mb': 20000}) bsettings.add_file( '[machines]\n' 'host1\n' 'host2\n' ) pool = machine.MachinePool() with terminal.capture(): available = pool.probe_all() self.assertEqual(len(available), 2) self.assertEqual(pool.get_total_weight(), 14) @mock.patch('buildman.machine._run_ssh') def test_probe_mixed(self, mock_ssh): """Test probing with some machines available and some not""" def ssh_side_effect(hostname, _cmd, **_kwargs): if hostname == 'host1': return json.dumps({ **MACHINE_INFO, 'load_1m': 1.0, 'mem_avail_mb': 8000, 'disk_avail_mb': 20000}) raise machine.MachineError('connection refused') mock_ssh.side_effect = ssh_side_effect bsettings.add_file( '[machines]\n' 'host1\n' 'host2\n' ) pool = machine.MachinePool() with terminal.capture(): available = pool.probe_all() self.assertEqual(len(available), 1) self.assertEqual(available[0].hostname, 'host1') @mock.patch('buildman.machine._run_ssh') def test_check_toolchains(self, mock_ssh): """Test checking toolchains on machines""" def ssh_side_effect(_hostname, cmd, **_kwargs): if 'python3' in cmd: return json.dumps({ **MACHINE_INFO, 'load_1m': 1.0, 'mem_avail_mb': 8000, 'disk_avail_mb': 20000}) if '--list-tool-chains' in cmd: return '''List of available toolchains (2): arm : /usr/bin/arm-gcc sandbox : /usr/bin/gcc ''' return '' mock_ssh.side_effect = ssh_side_effect bsettings.add_file( '[machines]\n' 'host1\n' ) pool = machine.MachinePool() with terminal.capture(): pool.probe_all() with terminal.capture(): missing = pool.check_toolchains({'arm', 'sandbox'}) self.assertEqual(missing, {}) @mock.patch('buildman.machine._run_ssh') def test_check_toolchains_missing(self, mock_ssh): """Test checking toolchains with some missing""" def ssh_side_effect(_hostname, cmd, **_kwargs): if 'python3' in cmd: return json.dumps({ **MACHINE_INFO, 'load_1m': 1.0, 'mem_avail_mb': 8000, 'disk_avail_mb': 20000}) if '--list-tool-chains' in cmd: return '''List of available toolchains (1): sandbox : /usr/bin/gcc ''' return '' mock_ssh.side_effect = ssh_side_effect bsettings.add_file( '[machines]\n' 'host1\n' ) pool = machine.MachinePool() with terminal.capture(): pool.probe_all() with terminal.capture(): missing = pool.check_toolchains({'arm', 'sandbox'}) self.assertEqual(len(missing), 1) m = list(missing.keys())[0] self.assertIn('arm', missing[m]) class TestRunSsh(unittest.TestCase): """Test _run_ssh()""" @mock.patch('buildman.machine.command.run_pipe') def test_success(self, mock_pipe): """Test successful SSH command""" mock_pipe.return_value = mock.Mock( return_code=0, stdout='hello\n', stderr='') result = machine._run_ssh('host1', ['echo', 'hello']) self.assertEqual(result, 'hello\n') # Verify SSH options pipe_list = mock_pipe.call_args[0][0] cmd = pipe_list[0] self.assertIn('ssh', cmd) self.assertIn('BatchMode=yes', cmd) self.assertIn('host1', cmd) self.assertIn('echo', cmd) @mock.patch('buildman.machine.command.run_pipe') def test_failure(self, mock_pipe): """Test SSH command failure""" mock_pipe.return_value = mock.Mock( return_code=255, stdout='', stderr='Connection refused') with self.assertRaises(machine.MachineError) as ctx: machine._run_ssh('host1', ['echo', 'hello']) self.assertIn('Connection refused', str(ctx.exception)) @mock.patch('buildman.machine.command.run_pipe') def test_failure_multiline_stderr(self, mock_pipe): """Test SSH failure with multi-line stderr picks last line""" mock_pipe.return_value = mock.Mock( return_code=255, stdout='', stderr='Warning: Added host key\n' 'Permission denied (publickey).') with self.assertRaises(machine.MachineError) as ctx: machine._run_ssh('host1', ['echo', 'hello']) self.assertIn('Permission denied', str(ctx.exception)) self.assertNotIn('Warning', str(ctx.exception)) @mock.patch('buildman.machine.command.run_pipe') def test_command_exc(self, mock_pipe): """Test SSH command exception""" mock_pipe.side_effect = command.CommandExc( 'ssh failed', command.CommandResult()) with self.assertRaises(machine.MachineError) as ctx: machine._run_ssh('host1', ['echo', 'hello']) self.assertIn('ssh failed', str(ctx.exception)) class TestGetMachinesConfig(unittest.TestCase): """Test get_machines_config()""" def setUp(self): bsettings.setup(None) def test_empty(self): """Test with no machines configured""" self.assertEqual(machine.get_machines_config(), []) def test_with_machines(self): """Test with machines configured""" bsettings.add_file( '[machines]\n' 'host1\n' 'host2\n' ) result = machine.get_machines_config() self.assertEqual(result, ['host1', 'host2']) class TestGccVersion(unittest.TestCase): """Test gcc_version()""" def test_normal_path(self): """Test extracting version from a standard toolchain path""" path = ('~/.buildman-toolchains/gcc-13.1.0-nolibc/' 'aarch64-linux/bin/aarch64-linux-gcc') self.assertEqual(machine.gcc_version(path), 'gcc-13.1.0-nolibc') def test_no_match(self): """Test path that does not contain a gcc-*-nolibc component""" self.assertIsNone(machine.gcc_version('/usr/bin/gcc')) def test_empty(self): """Test empty path""" self.assertIsNone(machine.gcc_version('')) class TestBuildVersionMap(unittest.TestCase): """Test build_version_map()""" def test_normal(self): """Test building version map from gcc dict""" gcc = { 'arm': '~/.buildman-toolchains/gcc-13.1.0-nolibc/arm/bin/gcc', 'sandbox': '/usr/bin/gcc', } result = machine.build_version_map(gcc) self.assertEqual(result, {'arm': 'gcc-13.1.0-nolibc'}) def test_none(self): """Test with None input""" self.assertEqual(machine.build_version_map(None), {}) class TestResolveToolchainAliases(unittest.TestCase): """Test resolve_toolchain_aliases()""" def setUp(self): bsettings.setup(None) def test_alias(self): """Test resolving aliases from config""" bsettings.add_file( '[toolchain-alias]\n' 'x86 = i386 i686\n' ) gcc = {'i386': '/usr/bin/i386-gcc'} machine.resolve_toolchain_aliases(gcc) self.assertEqual(gcc['x86'], '/usr/bin/i386-gcc') def test_no_alias_needed(self): """Test when arch already exists""" bsettings.add_file( '[toolchain-alias]\n' 'x86 = i386\n' ) gcc = {'x86': '/usr/bin/x86-gcc', 'i386': '/usr/bin/i386-gcc'} machine.resolve_toolchain_aliases(gcc) # Should not overwrite existing self.assertEqual(gcc['x86'], '/usr/bin/x86-gcc') class TestToolchainStatus(unittest.TestCase): """Test _toolchain_status()""" def test_no_toolchains_no_error(self): """Test machine with no toolchains and no error""" m = machine.Machine('host1') m.avail = True m.info.arch = 'x86_64' text, colour = machine._toolchain_status(m, set()) self.assertEqual(text, 'none') def test_no_toolchains_with_error(self): """Test machine with toolchain error""" m = machine.Machine('host1') m.tc_error = 'SSH failed' text, colour = machine._toolchain_status(m, set()) self.assertEqual(text, 'fail') def test_all_present(self): """Test all local toolchains present on machine""" m = machine.Machine('host1') m.toolchains = {'arm': '/usr/bin/arm-gcc', 'sandbox': '/usr/bin/gcc'} local = {'arm', 'sandbox'} text, colour = machine._toolchain_status(m, local) self.assertEqual(text, 'OK') def test_some_missing(self): """Test some toolchains missing""" m = machine.Machine('host1') m.toolchains = {'sandbox': '/usr/bin/gcc'} local = {'arm', 'sandbox'} text, colour = machine._toolchain_status(m, local) self.assertIn('missing', text) def test_version_mismatch(self): """Test version mismatch detection""" m = machine.Machine('host1') m.toolchains = { 'arm': '~/.buildman-toolchains/gcc-12.0.0-nolibc/arm/bin/gcc'} local_gcc = { 'arm': '~/.buildman-toolchains/gcc-13.1.0-nolibc/arm/bin/gcc'} text, colour = machine._toolchain_status( m, {'arm'}, local_gcc=local_gcc) self.assertIn('wrong ver', text) def test_no_local_archs(self): """Test with empty local arch set""" m = machine.Machine('host1') m.toolchains = {'arm': '/usr/bin/gcc', 'x86': '/usr/bin/gcc'} text, colour = machine._toolchain_status(m, set()) self.assertEqual(text, '2') def test_unreachable_no_toolchains(self): """Test unreachable machine with no arch info""" m = machine.Machine('host1') text, colour = machine._toolchain_status(m, {'arm'}) self.assertEqual(text, '-') class TestMachineExtended(unittest.TestCase): """Extended Machine tests for coverage""" @mock.patch('buildman.machine._run_ssh') def test_probe_toolchains_ssh_failure(self, mock_ssh): """Test toolchain probe when SSH fails""" mock_ssh.side_effect = machine.MachineError('timeout') m = machine.Machine('host1') result = m.probe_toolchains('buildman') self.assertEqual(result, {}) self.assertIn('timeout', m.tc_error) @mock.patch('buildman.machine._run_ssh') def test_probe_toolchains_from_boss(self, mock_ssh): """Test probing toolchains by checking boss's paths on remote""" home = os.path.expanduser('~') local_gcc = { 'arm': f'{home}/.buildman-toolchains/gcc-13/arm/bin/gcc', 'x86': f'{home}/.buildman-toolchains/gcc-13/x86/bin/gcc', } mock_ssh.return_value = 'arm:yes\nx86:no\n' m = machine.Machine('host1') result = m.probe_toolchains('buildman', local_gcc=local_gcc) self.assertIn('arm', result) self.assertNotIn('x86', result) @mock.patch('buildman.machine._run_ssh') def test_probe_toolchains_from_boss_ssh_fail(self, mock_ssh): """Test probing boss toolchains when SSH fails""" home = os.path.expanduser('~') local_gcc = { 'arm': f'{home}/.buildman-toolchains/gcc-13/arm/bin/gcc', } mock_ssh.side_effect = machine.MachineError('conn refused') m = machine.Machine('host1') result = m.probe_toolchains('buildman', local_gcc=local_gcc) self.assertEqual(result, {}) self.assertIn('conn refused', m.tc_error) @mock.patch('buildman.machine._run_ssh') def test_probe_toolchains_from_boss_no_home(self, mock_ssh): """Test probing boss toolchains with non-home paths""" local_gcc = {'sandbox': '/usr/bin/gcc'} m = machine.Machine('host1') result = m.probe_toolchains('buildman', local_gcc=local_gcc) self.assertEqual(result, {}) mock_ssh.assert_not_called() @mock.patch('buildman.machine._run_ssh') def test_fetch_toolchain_success(self, mock_ssh): """Test successful toolchain fetch""" mock_ssh.return_value = 'Fetched arm toolchain' m = machine.Machine('host1') self.assertTrue(m.fetch_toolchain('buildman', 'arm')) @mock.patch('buildman.machine._run_ssh') def test_fetch_toolchain_failure(self, mock_ssh): """Test failed toolchain fetch""" mock_ssh.side_effect = machine.MachineError('fetch failed') m = machine.Machine('host1') self.assertFalse(m.fetch_toolchain('buildman', 'arm')) @mock.patch('buildman.machine._run_ssh') def test_weight_unavailable(self, mock_ssh): """Test weight is 0 when unavailable""" m = machine.Machine('host1') m.avail = False m._calc_weight() self.assertEqual(m.weight, 0) class TestRunSshExtended(unittest.TestCase): """Extended _run_ssh tests""" @mock.patch('buildman.machine.command.run_pipe') def test_failure_no_stderr(self, mock_pipe): """Test SSH failure with no stderr""" mock_pipe.return_value = mock.Mock( return_code=1, stdout='', stderr='') with self.assertRaises(machine.MachineError) as ctx: machine._run_ssh('host1', ['cmd']) self.assertIn('failed with code 1', str(ctx.exception)) @mock.patch('buildman.machine.command.run_pipe') def test_stdin_data(self, mock_pipe): """Test SSH with stdin_data""" mock_pipe.return_value = mock.Mock( return_code=0, stdout='result\n', stderr='') result = machine._run_ssh('host1', ['python3'], stdin_data='print("result")') self.assertEqual(result, 'result\n') # Verify stdin_data was passed through _, kwargs = mock_pipe.call_args self.assertEqual(kwargs['stdin_data'], 'print("result")') class TestMachinePoolExtended(unittest.TestCase): """Extended MachinePool tests for coverage""" def setUp(self): bsettings.setup(None) def test_load_with_max_boards(self): """Test loading machines with max_boards config""" bsettings.add_file( '[machines]\n' 'server1\n' '[machine:server1]\n' 'max_boards = 50\n' ) pool = machine.MachinePool() self.assertEqual(len(pool.machines), 1) self.assertEqual(pool.machines[0].max_boards, 50) def test_load_filtered_names(self): """Test loading only specified machine names""" bsettings.add_file( '[machines]\n' 'host1\n' 'host2\n' 'host3\n' ) pool = machine.MachinePool(names=['host1', 'host3']) self.assertEqual(len(pool.machines), 2) names = [m.hostname for m in pool.machines] self.assertEqual(names, ['host1', 'host3']) def test_get_reachable(self): """Test get_reachable includes busy machines""" bsettings.add_file('[machines]\nhost1\nhost2\n') pool = machine.MachinePool() # Simulate host1 reachable but busy, host2 unreachable pool.machines[0].avail = False pool.machines[0].info.arch = 'x86_64' self.assertEqual(len(pool.get_reachable()), 1) self.assertEqual(pool.get_reachable()[0].hostname, 'host1') @mock.patch('buildman.machine._run_ssh') def test_print_summary(self, mock_ssh): """Test print_summary runs without error""" mock_ssh.return_value = json.dumps({ **MACHINE_INFO, 'load_1m': 1.0, 'mem_avail_mb': 8000, 'disk_avail_mb': 20000}) bsettings.add_file('[machines]\nhost1\n') pool = machine.MachinePool() with terminal.capture(): pool.probe_all() # Just verify it doesn't crash with terminal.capture(): pool.print_summary() @mock.patch('buildman.machine._run_ssh') def test_print_summary_with_toolchains(self, mock_ssh): """Test print_summary with toolchain info""" def ssh_side_effect(_hostname, cmd, **_kwargs): if 'python3' in cmd: return json.dumps({ **MACHINE_INFO, 'load_1m': 1.0, 'mem_avail_mb': 8000, 'disk_avail_mb': 20000}) if '--list-tool-chains' in cmd: return 'List of available toolchains (1):\narm : /bin/gcc\n\n' return '' mock_ssh.side_effect = ssh_side_effect bsettings.add_file('[machines]\nhost1\n') pool = machine.MachinePool() with terminal.capture(): pool.probe_all() with terminal.capture(): pool.check_toolchains({'arm', 'sandbox'}) with terminal.capture(): pool.print_summary(local_archs={'arm', 'sandbox'}) @mock.patch('buildman.machine._run_ssh') def test_check_toolchains_version_mismatch(self, mock_ssh): """Test version mismatch detection in check_toolchains""" home = os.path.expanduser('~') # The remote has gcc-12, but the boss has gcc-13 remote_gcc = (f'.buildman-toolchains/gcc-12.0.0-nolibc/' 'arm/bin/arm-linux-gcc') def ssh_side_effect(_hostname, cmd, **_kwargs): if 'python3' in cmd: return json.dumps({ **MACHINE_INFO, 'load_1m': 1.0, 'mem_avail_mb': 8000, 'disk_avail_mb': 20000}) # Boss probe: report arm as present (wrong version) return 'arm:yes\n' mock_ssh.side_effect = ssh_side_effect bsettings.add_file('[machines]\nhost1\n') pool = machine.MachinePool() with terminal.capture(): pool.probe_all() local_gcc = { 'arm': f'{home}/.buildman-toolchains/gcc-13.1.0-nolibc/' 'arm/bin/arm-linux-gcc', } # check_toolchains will re-probe, which sets the remote path # via _probe_toolchains_from_boss. The SSH mock returns # arm:yes, so the remote path uses the boss's relative path # but we need it to have the old version. Patch the machine's # toolchains after the probe runs. orig_probe = machine.Machine._probe_toolchains_from_boss def fake_probe(self_mach, lg): orig_probe(self_mach, lg) # Override with the wrong version if 'arm' in self_mach.toolchains: self_mach.toolchains['arm'] = f'~/{remote_gcc}' return self_mach.toolchains with mock.patch.object(machine.Machine, '_probe_toolchains_from_boss', fake_probe): with terminal.capture(): missing = pool.check_toolchains({'arm'}, local_gcc=local_gcc) # arm should be flagged as missing due to version mismatch self.assertEqual(len(missing), 1) class TestFetchMissing(unittest.TestCase): """Test _fetch_all_missing()""" def setUp(self): bsettings.setup(None) @mock.patch('buildman.machine._run_ssh') def test_fetch_success(self, mock_ssh): """Test successful toolchain fetch""" mock_ssh.return_value = '' bsettings.add_file('[machines]\nhost1\n') pool = machine.MachinePool() m = pool.machines[0] m.avail = True m.info.arch = 'x86_64' missing_map = {m: {'arm'}} with terminal.capture(): pool._fetch_all_missing(missing_map, {}, None, 'buildman') # After successful fetch + re-probe, arch is removed # (re-probe returns empty since SSH mock returns '') @mock.patch('buildman.machine._run_ssh') def test_fetch_failure(self, mock_ssh): """Test failed toolchain fetch""" mock_ssh.side_effect = machine.MachineError('failed') bsettings.add_file('[machines]\nhost1\n') pool = machine.MachinePool() m = pool.machines[0] m.avail = True m.info.arch = 'x86_64' missing_map = {m: {'arm'}} with terminal.capture(): pool._fetch_all_missing(missing_map, {}, None, 'buildman') # Should still have the missing arch self.assertIn('arm', missing_map[m]) @mock.patch('buildman.machine._run_ssh') def test_fetch_with_version_removal(self, mock_ssh): """Test fetching removes old version first""" calls = [] def ssh_side_effect(hostname, cmd, **_kwargs): calls.append(cmd) if '--fetch-arch' in cmd: return '' if 'rm' in cmd: return '' return '' mock_ssh.side_effect = ssh_side_effect bsettings.add_file('[machines]\nhost1\n') pool = machine.MachinePool() m = pool.machines[0] m.avail = True m.info.arch = 'x86_64' m.toolchains = { 'arm': '~/.buildman-toolchains/gcc-12.0.0-nolibc/arm/bin/gcc'} missing_map = {m: {'arm'}} local_versions = {'arm': 'gcc-13.1.0-nolibc'} with terminal.capture(): pool._fetch_all_missing(missing_map, local_versions, None, 'buildman') # Should have called rm -rf for the old version rm_calls = [c for c in calls if 'rm' in c] self.assertTrue(len(rm_calls) > 0) class TestPrintSummaryEdgeCases(unittest.TestCase): """Test print_summary edge cases""" def setUp(self): bsettings.setup(None) @mock.patch('buildman.machine._run_ssh') def test_unavailable_machine(self, mock_ssh): """Test summary with unavailable machine""" mock_ssh.side_effect = machine.MachineError('refused') bsettings.add_file('[machines]\nhost1\n') pool = machine.MachinePool() with terminal.capture(): pool.probe_all() # Should not crash with unavailable machine with terminal.capture(): pool.print_summary() @mock.patch('buildman.machine._run_ssh') def test_busy_machine(self, mock_ssh): """Test summary with busy machine""" mock_ssh.return_value = json.dumps({ **MACHINE_INFO, 'load_1m': 10.0}) bsettings.add_file('[machines]\nhost1\n') pool = machine.MachinePool() with terminal.capture(): pool.probe_all() with terminal.capture(): pool.print_summary() @mock.patch('buildman.machine._run_ssh') def test_with_max_boards(self, mock_ssh): """Test summary shows max_boards""" mock_ssh.return_value = json.dumps({ **MACHINE_INFO, 'load_1m': 1.0, 'mem_avail_mb': 8000, 'disk_avail_mb': 20000}) bsettings.add_file( '[machines]\nhost1\n' '[machine:host1]\nmax_boards = 50\n') pool = machine.MachinePool() with terminal.capture(): pool.probe_all() with terminal.capture(): pool.print_summary() @mock.patch('buildman.machine._run_ssh') def test_with_tc_error(self, mock_ssh): """Test summary with toolchain error note""" mock_ssh.return_value = json.dumps({ **MACHINE_INFO, 'load_1m': 1.0, 'mem_avail_mb': 8000, 'disk_avail_mb': 20000}) bsettings.add_file('[machines]\nhost1\n') pool = machine.MachinePool() with terminal.capture(): pool.probe_all() pool.machines[0].tc_error = 'buildman not found' with terminal.capture(): pool.print_summary(local_archs={'arm'}) @mock.patch('buildman.machine._run_ssh') def test_with_missing_toolchains(self, mock_ssh): """Test summary with missing toolchain notes""" mock_ssh.return_value = json.dumps({ **MACHINE_INFO, 'load_1m': 1.0, 'mem_avail_mb': 8000, 'disk_avail_mb': 20000}) bsettings.add_file('[machines]\nhost1\n') pool = machine.MachinePool() with terminal.capture(): pool.probe_all() pool.machines[0].toolchains = {'sandbox': '/usr/bin/gcc'} local_gcc = { 'arm': os.path.expanduser( '~/.buildman-toolchains/gcc-13/arm/bin/gcc'), } with terminal.capture(): pool.print_summary(local_archs={'arm', 'sandbox'}, local_gcc=local_gcc) class TestCheckToolchainsEdge(unittest.TestCase): """Test check_toolchains edge cases""" def setUp(self): bsettings.setup(None) def test_no_reachable(self): """Test check_toolchains with no reachable machines (line 482)""" bsettings.add_file('[machines]\nhost1\n') pool = machine.MachinePool() # Machine is not probed, so not reachable with terminal.capture(): result = pool.check_toolchains({'arm'}) self.assertEqual(result, {}) @mock.patch('buildman.machine._run_ssh') def test_fetch_flag(self, mock_ssh): """Test check_toolchains with fetch=True (line 524)""" home = os.path.expanduser('~') def ssh_side_effect(_hostname, cmd, **_kwargs): if 'python3' in cmd: return json.dumps({ **MACHINE_INFO, 'load_1m': 1.0, 'mem_avail_mb': 8000, 'disk_avail_mb': 20000}) # No toolchains found return '' mock_ssh.side_effect = ssh_side_effect bsettings.add_file('[machines]\nhost1\n') pool = machine.MachinePool() with terminal.capture(): pool.probe_all() local_gcc = { 'arm': f'{home}/.buildman-toolchains/gcc-13/arm/bin/gcc', } # fetch=True should trigger _fetch_all_missing with mock.patch.object(pool, '_fetch_all_missing') as mock_fetch: with terminal.capture(): pool.check_toolchains({'arm'}, fetch=True, local_gcc=local_gcc) mock_fetch.assert_called_once() class TestFetchVersionRemovalFailure(unittest.TestCase): """Test _fetch_all_missing rm -rf failure path (lines 563-564)""" def setUp(self): bsettings.setup(None) @mock.patch('buildman.machine._run_ssh') def test_rm_failure_continues(self, mock_ssh): """Test that rm -rf failure is silently ignored""" call_count = [0] def ssh_side_effect(hostname, cmd, **_kwargs): call_count[0] += 1 if 'rm' in cmd: raise machine.MachineError('rm failed') if '--fetch-arch' in cmd: return '' return '' mock_ssh.side_effect = ssh_side_effect bsettings.add_file('[machines]\nhost1\n') pool = machine.MachinePool() m = pool.machines[0] m.avail = True m.info.arch = 'x86_64' # Old version that differs from local m.toolchains = { 'arm': '~/.buildman-toolchains/gcc-12.0.0-nolibc/arm/bin/gcc'} missing_map = {m: {'arm'}} local_versions = {'arm': 'gcc-13.1.0-nolibc'} with terminal.capture(): pool._fetch_all_missing(missing_map, local_versions, None, 'buildman') # Should have attempted rm and then fetch despite rm failure self.assertGreater(call_count[0], 1) class TestPrintSummaryNotProbed(unittest.TestCase): """Test print_summary 'not probed' branch (lines 669-670)""" def setUp(self): bsettings.setup(None) def test_not_probed_machine(self): """Test summary shows 'not probed' for unprobed machine""" bsettings.add_file('[machines]\nhost1\n') pool = machine.MachinePool() # Don't probe - machine stays in 'not probed' state with terminal.capture(): pool.print_summary() class TestPrintSummaryMissingNoVersion(unittest.TestCase): """Test print_summary missing toolchain without version (line 707)""" def setUp(self): bsettings.setup(None) @mock.patch('buildman.machine._run_ssh') def test_missing_with_and_without_version(self, mock_ssh): """Test missing note for archs with and without version info""" home = os.path.expanduser('~') mock_ssh.return_value = json.dumps({ **MACHINE_INFO, 'load_1m': 1.0, 'mem_avail_mb': 8000, 'disk_avail_mb': 20000}) bsettings.add_file('[machines]\nhost1\n') pool = machine.MachinePool() with terminal.capture(): pool.probe_all() pool.machines[0].toolchains = {} # arm has a version (under ~/.buildman-toolchains), # sandbox does not local_gcc = { 'arm': f'{home}/.buildman-toolchains/gcc-13.1.0-nolibc/' 'arm/bin/gcc', 'sandbox': '/usr/bin/gcc', } with terminal.capture(): pool.print_summary(local_archs={'arm', 'sandbox'}, local_gcc=local_gcc) class TestDoProbe(unittest.TestCase): """Test do_probe_machines()""" def setUp(self): bsettings.setup(None) def test_no_machines(self): """Test with no machines configured""" with terminal.capture(): ret = machine.do_probe_machines() self.assertEqual(ret, 1) @mock.patch('buildman.machine._run_ssh') def test_with_machines(self, mock_ssh): """Test probing configured machines""" def ssh_side_effect(_hostname, cmd, **_kwargs): if 'python3' in cmd: return json.dumps({ **MACHINE_INFO, 'load_1m': 1.0, 'mem_avail_mb': 8000, 'disk_avail_mb': 20000}) if '--list-tool-chains' in cmd: return 'List of available toolchains (0):\n\n' return '' mock_ssh.side_effect = ssh_side_effect bsettings.add_file('[machines]\nhost1\n') with terminal.capture(): ret = machine.do_probe_machines() self.assertEqual(ret, 0) if __name__ == '__main__': unittest.main() |