Skip to content

Add unit tests for info_* helper functions with mocked subprocess and file I/O#115

Merged
gsanchietti merged 2 commits intoissue1512from
copilot/sub-pr-113-again
Feb 11, 2026
Merged

Add unit tests for info_* helper functions with mocked subprocess and file I/O#115
gsanchietti merged 2 commits intoissue1512from
copilot/sub-pr-113-again

Conversation

Copy link
Copy Markdown

Copilot AI commented Feb 11, 2026

Addresses review feedback requesting test coverage for the info_* helper functions introduced in #113. These functions gather system information via subprocess calls and /proc reads but lacked unit tests.

Changes

  • File I/O tests: Mock /proc/version and /proc/uptime reads for info_kernel_version() and info_uptime_seconds() with exception handling

  • IPv4/IPv6 detection tests: Mock subprocess.run for all three fallback methods in info_default_ipv4() (dig → curl → ip command) and info_default_ipv6() with early-exit validation

  • Update checking tests: Mock subprocess calls to /usr/libexec/rpcd/ns.update for info_package_updates_available() and info_image_updates_available()

  • Version parsing tests: Validate parse_version() handles prefix removal, comparison operators, and invalid input

All tests use unittest.mock.patch and mock_open to isolate system dependencies. 17 new tests covering success paths, fallback chains, and failure modes.

def test_info_default_ipv4_curl_fallback(tmp_path):
    """Test info_default_ipv4 falls back to curl when dig fails"""
    u = EUci(confdir=tmp_path.as_posix())
    
    def mock_subprocess_run(cmd, **kwargs):
        result = MagicMock()
        if 'dig' in cmd:
            result.returncode = 1
        elif 'curl' in cmd:
            result.returncode = 0
            result.stdout = '203.0.113.1\n'
        return result
    
    with patch('subprocess.run', side_effect=mock_subprocess_run):
        result = inventory.info_default_ipv4(u)
        assert result.startswith('anon-')

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: gsanchietti <804596+gsanchietti@users.noreply.github.com>
Copilot AI changed the title [WIP] WIP address feedback from review on extra info in inventory Add unit tests for info_* helper functions with mocked subprocess and file I/O Feb 11, 2026
Copilot AI requested a review from gsanchietti February 11, 2026 10:29
@gsanchietti gsanchietti marked this pull request as ready for review February 11, 2026 10:31
@gsanchietti gsanchietti merged commit 6aa6998 into issue1512 Feb 11, 2026
2 checks passed
@gsanchietti gsanchietti deleted the copilot/sub-pr-113-again branch February 11, 2026 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants