-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest_dispatcher.py
186 lines (169 loc) · 5.34 KB
/
test_dispatcher.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from __future__ import annotations
import json
from unittest import mock
import pytest
from workflows.recipe import Recipe
from workflows.transport.offline_transport import OfflineTransport
import zocalo.configuration
from zocalo.service.dispatcher import Dispatcher
@pytest.fixture
def mock_zocalo_configuration(tmp_path):
mock_zc = mock.MagicMock(zocalo.configuration.Configuration)
mock_zc.storage = {
"zocalo.recipe_directory": tmp_path,
}
return mock_zc
@pytest.fixture
def mock_environment(mock_zocalo_configuration):
return {"config": mock_zocalo_configuration}
@pytest.fixture
def offline_transport(mocker):
transport = OfflineTransport()
mocker.spy(transport, "send")
return transport
@pytest.fixture
def example_recipe(tmp_path):
recipe_path = tmp_path / "example-recipe.json"
recipe_path.write_text(
"""
{
"1": { "service": "dispatcher test",
"queue": "{queue}"
},
"start": [
[1, { "purpose": "test for zocalo dispatcher service loading a processing recipe from an external file" }]
]
}
"""
)
return recipe_path
def test_parsing_a_custom_recipe_and_replacing_parameters(
mock_environment, offline_transport
):
"""Passing in a recipe to the service without external dependencies.
The recipe should be interpreted, the 'food' placeholder replaced using
the parameter field, and the message passed back.
The message should then contain the recipe and a correctly set pointer."""
recipe = {
1: {
"service": "foo",
"queue": "bar",
"food": "{food}",
},
"start": [(1, {"purpose": "trivial test for the recipe parsing service"})],
}
header = {
"message-id": mock.sentinel,
"subscription": mock.sentinel,
}
parameters = {"food": "spam"}
service = Dispatcher(environment=mock_environment)
service.transport = offline_transport
service.start()
service.process(
None, header, message={"parameters": parameters, "custom_recipe": recipe}
)
expected_recipe = Recipe(recipe)
expected_recipe.apply_parameters(parameters)
expected_message = {
"payload": recipe["start"][0][1],
"recipe": expected_recipe.recipe,
"recipe-path": [],
"recipe-pointer": 1,
"environment": mock.ANY,
}
offline_transport.send.assert_called_with(
"bar",
expected_message,
headers={"workflows-recipe": True},
transaction=mock.ANY,
)
def test_loading_a_recipe_from_a_file(
mock_environment, offline_transport, example_recipe
):
"""When a file name is passed to the service the file should be loaded and
parsed correctly, including parameter replacement."""
header = {
"message-id": mock.sentinel,
"subscription": mock.sentinel,
}
parameters = {"queue": "foo"}
service = Dispatcher(environment=mock_environment)
service.transport = offline_transport
service.start()
service.process(
None,
header,
message={"parameters": parameters, "recipes": [example_recipe.stem]},
)
with example_recipe.open() as fh:
recipe = json.load(fh)
expected_recipe = Recipe(recipe)
expected_recipe.apply_parameters(parameters)
expected_message = {
"payload": recipe["start"][0][1],
"recipe": expected_recipe.recipe,
"recipe-path": [],
"recipe-pointer": 1,
"environment": mock.ANY,
}
offline_transport.send.assert_called_with(
"foo",
expected_message,
headers={"workflows-recipe": True},
transaction=mock.ANY,
)
def test_combining_recipes(mock_environment, offline_transport, example_recipe):
"""Combine a recipe from a file and a custom recipe."""
header = {
"message-id": mock.sentinel,
"subscription": mock.sentinel,
}
parameters = {"queue": "foo"}
custom_recipe = {
1: {"service": "dispatcher test", "queue": "bar"},
"start": [(1, {"purpose": "test recipe merging"})],
}
message = {
"parameters": parameters,
"recipes": [example_recipe.stem],
"custom_recipe": custom_recipe,
}
service = Dispatcher(environment=mock_environment)
service.transport = offline_transport
service.start()
service.process(None, header=header, message=message)
with example_recipe.open() as fh:
recipe = json.load(fh)
expected_recipe = Recipe(recipe)
expected_recipe.apply_parameters(parameters)
common_expected_message = {
"recipe": mock.ANY,
"recipe-path": [],
"environment": mock.ANY,
}
offline_transport.send.assert_has_calls(
[
mock.call(
"bar",
{
**common_expected_message,
"recipe-pointer": 1,
"payload": custom_recipe["start"][0][1],
},
headers={"workflows-recipe": True},
transaction=mock.ANY,
),
mock.call(
"foo",
{
**common_expected_message,
"recipe-pointer": 2,
"payload": recipe["start"][0][1],
},
headers={"workflows-recipe": True},
transaction=mock.ANY,
),
],
any_order=True,
)