Skip to content

Commit 0a59aa7

Browse files
committed
Keyword argument protocol changes
1 parent b965f9c commit 0a59aa7

File tree

6 files changed

+86
-8
lines changed

6 files changed

+86
-8
lines changed

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#
1515
import datetime
1616

17-
from ansible_runner.__main__ import VERSION
17+
from ansible_runner.version import VERSION
1818

1919

2020
# -- Project information -----------------------------------------------------

src/ansible_runner/__main__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@
4646
from ansible_runner import cleanup
4747
from ansible_runner.utils import dump_artifact, Bunch, register_for_cleanup
4848
from ansible_runner.utils.capacity import get_cpu_count, get_mem_in_bytes, ensure_uuid
49-
from ansible_runner.utils.importlib_compat import importlib_metadata
5049
from ansible_runner.runner import Runner
50+
from ansible_runner.version import VERSION
5151

52-
VERSION = importlib_metadata.version("ansible_runner")
5352

5453
DEFAULT_ROLES_PATH = os.getenv('ANSIBLE_ROLES_PATH', None)
5554
DEFAULT_RUNNER_BINARY = os.getenv('RUNNER_BINARY', None)

src/ansible_runner/interface.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
sanitize_json_response,
3838
signal_handler,
3939
)
40+
from ansible_runner.version import VERSION
4041

4142
logging.getLogger('ansible-runner').addHandler(logging.NullHandler())
4243

@@ -98,7 +99,7 @@ def init_runner(**kwargs):
9899
streamer = kwargs.pop('streamer', None)
99100
if streamer:
100101
if streamer == 'transmit':
101-
stream_transmitter = Transmitter(**kwargs)
102+
stream_transmitter = Transmitter(runner_version=VERSION, **kwargs)
102103
return stream_transmitter
103104

104105
if streamer == 'worker':

src/ansible_runner/streaming.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from collections.abc import Mapping
1313
from functools import wraps
14+
from packaging.version import Version
1415
from threading import Event, RLock, Thread
1516

1617
import ansible_runner
@@ -37,7 +38,9 @@ def __init__(self, settings):
3738

3839

3940
class Transmitter:
40-
def __init__(self, _output=None, **kwargs):
41+
def __init__(self, runner_version: str, _output=None, **kwargs):
42+
self.runner_version = runner_version
43+
4144
if _output is None:
4245
_output = sys.stdout.buffer
4346
self._output = _output
@@ -53,7 +56,12 @@ def __init__(self, _output=None, **kwargs):
5356

5457
def run(self):
5558
self._output.write(
56-
json.dumps({'kwargs': self.kwargs}, cls=UUIDEncoder).encode('utf-8')
59+
json.dumps(
60+
{
61+
'runner_version': self.runner_version,
62+
'kwargs': self.kwargs
63+
},
64+
cls=UUIDEncoder).encode('utf-8')
5765
)
5866
self._output.write(b'\n')
5967
self._output.flush()
@@ -69,7 +77,9 @@ def run(self):
6977

7078

7179
class Worker:
72-
def __init__(self, _input=None, _output=None, keepalive_seconds: float | None = None, **kwargs):
80+
def __init__(self, runner_version: str, _input=None, _output=None, keepalive_seconds: float | None = None, **kwargs):
81+
self.runner_version = runner_version
82+
7383
if _input is None:
7484
_input = sys.stdin.buffer
7585
if _output is None:
@@ -187,6 +197,9 @@ def run(self):
187197
self.finished_callback(None) # send eof line
188198
return self.status, self.rc
189199

200+
if 'runner_version' in data:
201+
if Version(self.runner_version) < Version(data['runner_version']):
202+
raise Exception("Received newer data to older Worker")
190203
if 'kwargs' in data:
191204
self.job_kwargs = self.update_paths(data['kwargs'])
192205
elif 'zipfile' in data:

src/ansible_runner/version.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .utils.importlib_compat import importlib_metadata
2+
3+
VERSION = importlib_metadata.version("ansible_runner")

test/unit/test_streaming.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import io
12
import os
23

3-
from ansible_runner.streaming import Processor
4+
import pytest
5+
6+
from ansible_runner.streaming import Processor, Transmitter, Worker
47

58

69
class TestProcessor:
@@ -14,3 +17,62 @@ def test_artifact_dir_with_int_ident(self, tmp_path):
1417
assert p.artifact_dir == os.path.join(kwargs['private_data_dir'],
1518
'artifacts',
1619
str(kwargs['ident']))
20+
21+
22+
class TestTransmitter:
23+
24+
def test_job_arguments(self, tmp_path, project_fixtures):
25+
"""
26+
Test format of sending job arguments.
27+
"""
28+
transmit_dir = project_fixtures / 'debug'
29+
outgoing_buffer_file = tmp_path / 'buffer_out'
30+
outgoing_buffer_file.touch()
31+
32+
kwargs = {
33+
'playbook': 'debug.yml',
34+
'only_transmit_kwargs': True
35+
}
36+
37+
with outgoing_buffer_file.open('b+r') as outgoing_buffer:
38+
transmitter = Transmitter(
39+
runner_version="1.0.0",
40+
_output=outgoing_buffer,
41+
private_data_dir=transmit_dir,
42+
**kwargs)
43+
transmitter.run()
44+
outgoing_buffer.seek(0)
45+
sent = outgoing_buffer.read()
46+
47+
expected = b'{"runner_version": "1.0.0", "kwargs": {"playbook": "debug.yml"}}\n{"eof": true}\n'
48+
assert sent == expected
49+
50+
def test_version_mismatch(self, project_fixtures):
51+
transmit_dir = project_fixtures / 'debug'
52+
transmit_buffer = io.BytesIO()
53+
output_buffer = io.BytesIO()
54+
55+
for buffer in (transmit_buffer, output_buffer):
56+
buffer.name = 'foo'
57+
58+
kwargs = {
59+
'playbook': 'debug.yml',
60+
'only_transmit_kwargs': True
61+
}
62+
63+
status, rc = Transmitter(
64+
runner_version="1.0.0",
65+
_output=transmit_buffer,
66+
private_data_dir=transmit_dir,
67+
**kwargs).run()
68+
69+
assert rc in (None, 0)
70+
assert status == 'unstarted'
71+
transmit_buffer.seek(0)
72+
73+
worker = Worker(runner_version="0.1.0",
74+
_input=transmit_buffer,
75+
_output=output_buffer)
76+
77+
with pytest.raises(Exception, match="Received newer data to older Worker"):
78+
worker.run()

0 commit comments

Comments
 (0)