|
| 1 | +import sys |
| 2 | +import pytest |
| 3 | + |
| 4 | +from ansible_runner.interface import run |
| 5 | + |
| 6 | + |
| 7 | +TEST_BRANCHES = ( |
| 8 | + 'devel', |
| 9 | + 'stable-2.16', |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.test_all_runtimes |
| 14 | +@pytest.mark.parametrize('branch', TEST_BRANCHES) |
| 15 | +@pytest.mark.skipif(sys.platform == 'darwin', reason='does not work on macOS') |
| 16 | +def test_adhoc(tmp_path, runtime, branch, container_image_devel): |
| 17 | + # pvt_data_dir is mounted on the container, so it must contain the expected directories |
| 18 | + project_dir = tmp_path / 'project' |
| 19 | + project_dir.mkdir() |
| 20 | + r = run(private_data_dir=str(tmp_path), |
| 21 | + host_pattern='localhost', |
| 22 | + module='shell', |
| 23 | + module_args='pwd', |
| 24 | + process_isolation_executable=runtime, |
| 25 | + process_isolation=True, |
| 26 | + container_image=container_image_devel, |
| 27 | + ) |
| 28 | + |
| 29 | + assert r.status == 'successful' |
| 30 | + assert r.rc == 0 |
| 31 | + assert 'ok' in r.stats |
| 32 | + assert 'localhost' in r.stats['ok'] |
| 33 | + events = [x['event'] for x in r.events if x['event'] != 'verbose'] |
| 34 | + assert len(events) == 4 |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.test_all_runtimes |
| 38 | +@pytest.mark.parametrize('branch', TEST_BRANCHES) |
| 39 | +@pytest.mark.skipif(sys.platform == 'darwin', reason='does not work on macOS') |
| 40 | +def test_playbook(tmp_path, runtime, branch, container_image_devel): |
| 41 | + PLAYBOOK = """ |
| 42 | +- hosts: localhost |
| 43 | + gather_facts: False |
| 44 | + tasks: |
| 45 | + - set_fact: |
| 46 | + foo: bar |
| 47 | +""" |
| 48 | + |
| 49 | + # pvt_data_dir is mounted on the container, so it must contain the expected directories |
| 50 | + project_dir = tmp_path / 'project' |
| 51 | + project_dir.mkdir() |
| 52 | + inventory_dir = tmp_path / 'inventory' |
| 53 | + inventory_dir.mkdir() |
| 54 | + |
| 55 | + hosts_file = inventory_dir / 'hosts' |
| 56 | + hosts_file.write_text('localhost\n') |
| 57 | + |
| 58 | + playbook = project_dir / 'test.yml' |
| 59 | + playbook.write_text(PLAYBOOK) |
| 60 | + |
| 61 | + r = run(private_data_dir=str(tmp_path), |
| 62 | + playbook='test.yml', |
| 63 | + process_isolation_executable=runtime, |
| 64 | + process_isolation=True, |
| 65 | + container_image=container_image_devel, |
| 66 | + ) |
| 67 | + |
| 68 | + expected_events = [ |
| 69 | + 'playbook_on_start', |
| 70 | + 'playbook_on_play_start', |
| 71 | + 'playbook_on_task_start', |
| 72 | + 'runner_on_start', |
| 73 | + 'runner_on_ok', |
| 74 | + 'playbook_on_stats', |
| 75 | + ] |
| 76 | + |
| 77 | + assert r.status == 'successful' |
| 78 | + assert r.rc == 0 |
| 79 | + assert 'ok' in r.stats |
| 80 | + assert 'localhost' in r.stats['ok'] |
| 81 | + events = [x['event'] for x in r.events if x['event'] != 'verbose'] |
| 82 | + assert events == expected_events |
0 commit comments