|
| 1 | +"""Tests for testing_farm_tools module.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import shlex |
| 5 | +import pytest |
| 6 | +from unittest.mock import AsyncMock, patch |
| 7 | +from mcp_server.testing_farm_tools import run_testing_farm_test, get_compose_from_branch |
| 8 | + |
| 9 | + |
| 10 | +def test_tmt_prepare_quoting(): |
| 11 | + """Test that package URLs with special characters are properly quoted.""" |
| 12 | + # Test with a package URL that has special characters |
| 13 | + package = 'http://example.com/package-1.0-1.el9.x86_64.rpm' |
| 14 | + tmt_prepare = f'--insert --how install --package {shlex.quote(package)}' |
| 15 | + |
| 16 | + # Should be properly quoted |
| 17 | + assert '--insert' in tmt_prepare |
| 18 | + assert '--how' in tmt_prepare |
| 19 | + assert 'install' in tmt_prepare |
| 20 | + assert '--package' in tmt_prepare |
| 21 | + assert package in tmt_prepare |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.asyncio |
| 25 | +async def test_get_compose_from_branch(): |
| 26 | + """Test get_compose_from_branch function with various branch formats.""" |
| 27 | + test_cases = [ |
| 28 | + ("rhel-9.7.0", "RHEL-9.7.0-Nightly"), |
| 29 | + ("rhel-10.2", "RHEL-10.2-Nightly"), |
| 30 | + ("rhel-8.10", "RHEL-8.10.0-Nightly"), |
| 31 | + ("c8s", "CentOS-Stream-8"), |
| 32 | + ] |
| 33 | + |
| 34 | + for branch, expected_compose in test_cases: |
| 35 | + result = await get_compose_from_branch(branch) |
| 36 | + assert result == expected_compose, ( |
| 37 | + f"Branch '{branch}' should return '{expected_compose}', " |
| 38 | + f"but got '{result}'" |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.asyncio |
| 43 | +async def test_run_testing_farm_test_success(): |
| 44 | + """Test run_testing_farm_test with successful test execution.""" |
| 45 | + git_url = "https://gitlab.com/redhat/rhel/tests/expat.git" |
| 46 | + git_ref = "master" |
| 47 | + path_to_test = "Security/RHEL-114639-CVE-2025-59375-expat-libexpat-in-Expat-allows" |
| 48 | + package = "http://coprbe.devel.redhat.com/results/mmassari/RHEL-114644/rhel-9-x86_64/00127295-expat/expat-2.5.0-5.el9.x86_64.rpm" |
| 49 | + dist_git_branch = "rhel-9.7.0" |
| 50 | + |
| 51 | + # Mock the subprocess to return success |
| 52 | + mock_process = AsyncMock() |
| 53 | + mock_process.returncode = 0 |
| 54 | + mock_process.communicate = AsyncMock(return_value=(b"Test passed", b"")) |
| 55 | + |
| 56 | + with patch('mcp_server.testing_farm_tools.asyncio.create_subprocess_exec', return_value=mock_process): |
| 57 | + success, result = await run_testing_farm_test( |
| 58 | + git_url=git_url, |
| 59 | + git_ref=git_ref, |
| 60 | + path_to_test=path_to_test, |
| 61 | + package=package, |
| 62 | + dist_git_branch=dist_git_branch, |
| 63 | + ) |
| 64 | + |
| 65 | + assert success is True |
| 66 | + assert "Test passed" in result |
| 67 | + assert "Testing Farm test passed" in result |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.asyncio |
| 71 | +async def test_run_testing_farm_test_failure(): |
| 72 | + """Test run_testing_farm_test with failed test execution.""" |
| 73 | + git_url = "https://gitlab.com/redhat/rhel/tests/expat.git" |
| 74 | + git_ref = "master" |
| 75 | + path_to_test = "Security/RHEL-114639-CVE-2025-59375-expat-libexpat-in-Expat-allows" |
| 76 | + package = "http://coprbe.devel.redhat.com/results/mmassari/RHEL-114644/rhel-9-x86_64/00127295-expat/expat-2.5.0-5.el9.x86_64.rpm" |
| 77 | + dist_git_branch = "rhel-9.7.0" |
| 78 | + |
| 79 | + # Mock the subprocess to return failure |
| 80 | + mock_process = AsyncMock() |
| 81 | + mock_process.returncode = 1 |
| 82 | + mock_process.communicate = AsyncMock(return_value=(b"", b"Test failed with error")) |
| 83 | + |
| 84 | + with patch('mcp_server.testing_farm_tools.asyncio.create_subprocess_exec', return_value=mock_process): |
| 85 | + success, result = await run_testing_farm_test( |
| 86 | + git_url=git_url, |
| 87 | + git_ref=git_ref, |
| 88 | + path_to_test=path_to_test, |
| 89 | + package=package, |
| 90 | + dist_git_branch=dist_git_branch, |
| 91 | + ) |
| 92 | + |
| 93 | + assert success is False |
| 94 | + assert "Test failed" in result or "exit code 1" in result |
| 95 | + assert "Testing Farm test failed" in result |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == '__main__': |
| 99 | + # Run tests if executed directly |
| 100 | + test_tmt_prepare_quoting() |
| 101 | + # Run async tests |
| 102 | + asyncio.run(test_get_compose_from_branch()) |
| 103 | + asyncio.run(test_run_testing_farm_test_success()) |
| 104 | + asyncio.run(test_run_testing_farm_test_failure()) |
| 105 | + print("All tests passed!") |
0 commit comments