Skip to content

Commit 6a2fb5a

Browse files
committed
Add runner tests from ansible core test suite
1 parent 309baad commit 6a2fb5a

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed

test/integration/conftest.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,62 @@ def container_image(request, cli, tmp_path): # pylint: disable=W0621
125125
[runtime, 'rmi', '-f', image_name],
126126
bare=True,
127127
)
128+
129+
130+
@pytest.fixture
131+
def container_image_devel(request, cli, tmp_path): # pylint: disable=W0621
132+
branch = request.getfixturevalue('branch')
133+
134+
DOCKERFILE = f"""
135+
FROM quay.io/centos/centos:stream9
136+
ARG WHEEL
137+
COPY $WHEEL /$WHEEL
138+
139+
# Need python 3.11 minimum for devel
140+
RUN dnf install -y python3.11 python3.11-pip git
141+
RUN alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 0
142+
RUN python3 -m pip install /$WHEEL git+https://github.com/ansible/ansible@{branch}
143+
144+
RUN mkdir -p /runner/{{env,inventory,project,artifacts}} /home/runner/.ansible/tmp
145+
RUN chmod -R 777 /runner /home/runner
146+
WORKDIR /runner
147+
ENV HOME=/home/runner
148+
CMD ["ansible-runner", "run", "/runner"]
149+
"""
150+
151+
try:
152+
containerized = request.getfixturevalue('containerized')
153+
if not containerized:
154+
yield None
155+
return
156+
except Exception:
157+
# Test func doesn't use containerized
158+
pass
159+
160+
if (env_image_name := os.getenv('RUNNER_TEST_IMAGE_NAME')):
161+
yield env_image_name
162+
return
163+
164+
cli(
165+
['pyproject-build', '-w', '-o', str(tmp_path)],
166+
cwd=here.parent.parent,
167+
bare=True,
168+
)
169+
170+
wheel = next(tmp_path.glob('*.whl')) # pylint: disable=R1708
171+
172+
runtime = request.getfixturevalue('runtime')
173+
dockerfile_path = tmp_path / 'Dockerfile'
174+
dockerfile_path.write_text(DOCKERFILE)
175+
random_string = ''.join(random.choice(ascii_lowercase) for i in range(10))
176+
image_name = f'ansible-runner-{random_string}-event-test'
177+
178+
cli(
179+
[runtime, 'build', '--build-arg', f'WHEEL={wheel.name}', '--rm=true', '-t', image_name, '-f', str(dockerfile_path), str(tmp_path)],
180+
bare=True,
181+
)
182+
yield image_name
183+
cli(
184+
[runtime, 'rmi', '-f', image_name],
185+
bare=True,
186+
)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)