File: test_core_integration.py

package info (click to toggle)
ansible-runner 2.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,232 kB
  • sloc: python: 9,896; makefile: 19
file content (84 lines) | stat: -rw-r--r-- 2,530 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
72
73
74
75
76
77
78
79
80
81
82
83
84
import sys
import pytest

from ansible_runner.interface import run


TEST_BRANCHES = (
    'devel',
    'milestone',
    'stable-2.18',   # current stable
    'stable-2.17',   # stable - 1
)


@pytest.mark.test_all_runtimes
@pytest.mark.parametrize('branch', TEST_BRANCHES)
@pytest.mark.skipif(sys.platform == 'darwin', reason='does not work on macOS')
def test_adhoc(tmp_path, runtime, branch, container_image_devel):  # pylint: disable=W0613
    # pvt_data_dir is mounted on the container, so it must contain the expected directories
    project_dir = tmp_path / 'project'
    project_dir.mkdir()
    r = run(private_data_dir=str(tmp_path),
            host_pattern='localhost',
            module='shell',
            module_args='pwd',
            process_isolation_executable=runtime,
            process_isolation=True,
            container_image=container_image_devel,
            )

    assert r.status == 'successful'
    assert r.rc == 0
    assert 'ok' in r.stats
    assert 'localhost' in r.stats['ok']
    events = [x['event'] for x in r.events if x['event'] != 'verbose']
    assert len(events) == 4


@pytest.mark.test_all_runtimes
@pytest.mark.parametrize('branch', TEST_BRANCHES)
@pytest.mark.skipif(sys.platform == 'darwin', reason='does not work on macOS')
def test_playbook(tmp_path, runtime, branch, container_image_devel):  # pylint: disable=W0613
    PLAYBOOK = """
- hosts: localhost
  gather_facts: False
  tasks:
    - set_fact:
        foo: bar
"""

    # pvt_data_dir is mounted on the container, so it must contain the expected directories
    project_dir = tmp_path / 'project'
    project_dir.mkdir()
    inventory_dir = tmp_path / 'inventory'
    inventory_dir.mkdir()

    hosts_file = inventory_dir / 'hosts'
    hosts_file.write_text('localhost\n')

    playbook = project_dir / 'test.yml'
    playbook.write_text(PLAYBOOK)

    r = run(private_data_dir=str(tmp_path),
            playbook='test.yml',
            process_isolation_executable=runtime,
            process_isolation=True,
            container_image=container_image_devel,
            )

    expected_events = [
        'playbook_on_start',
        'playbook_on_play_start',
        'playbook_on_task_start',
        'runner_on_start',
        'runner_on_ok',
        'playbook_on_stats',
    ]

    assert r.status == 'successful'
    assert r.rc == 0
    assert 'ok' in r.stats
    assert 'localhost' in r.stats['ok']
    events = [x['event'] for x in r.events if x['event'] != 'verbose']
    assert events == expected_events