File: test_provision_manager.py

package info (click to toggle)
incant 0.5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 30,640 kB
  • sloc: python: 1,730; sh: 35; makefile: 10; ruby: 1
file content (71 lines) | stat: -rw-r--r-- 2,258 bytes parent folder | download
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
from unittest.mock import Mock, call

import pytest

from incant.provisioning_manager import ProvisionManager
from incant.reporter import Reporter


@pytest.fixture
def mock_incus_cli():
    return Mock()


@pytest.fixture
def mock_reporter():
    return Mock(spec=Reporter)


def test_provision_llmnr(mock_incus_cli, mock_reporter):
    """Test that LLMNR provisioning calls the expected incus commands."""
    pm = ProvisionManager(mock_incus_cli, mock_reporter)

    # Mock exec to simulate apt-get presence
    # The first call checks for apt-get, we want it to succeed (return None or empty)
    # The second call installs it.
    # Subsequent calls are for configuration.
    mock_incus_cli.exec.return_value = None

    instance_name = "test-instance"
    provision_config = [{"llmnr": True}]

    pm.provision(instance_name, provision_config)

    # Verify calls
    # 1. Check apt-get
    # 2. Install package
    # 3. Configure file
    # 4. Restart service

    # We check that at least the installation was attempted
    assert (
        call(instance_name, ["sh", "-c", "command -v apt-get"], capture_output=True)
        in mock_incus_cli.exec.call_args_list
    )
    assert (
        call(
            instance_name,
            ["sh", "-c", "apt-get update && apt-get -y install systemd-resolved"],
            capture_output=False,
        )
        in mock_incus_cli.exec.call_args_list
    )

    # Verify reporter success message
    mock_reporter.success.assert_any_call(f"LLMNR enabled on {instance_name}.")


def test_provision_unknown_provisioner_runtime_check(mock_incus_cli, mock_reporter):
    """
    Test that ProvisionManager handles provisioners safely.
    Note: Ideally ConfigManager catches unknown keys before this,
    but ProvisionManager should also be robust or at least fail clearly if passed invalid data.
    """
    pm = ProvisionManager(mock_incus_cli, mock_reporter)

    # We need to bypass ConfigManager validation to test this runtime behavior
    # or we can rely on the fact that ConfigManager would have raised an error.
    # Here we simulate a "script" step just to prove the manager works.

    pm.provision("test", ["echo hello"])
    mock_incus_cli.run_script.assert_called_with("test", "echo hello")