-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_update.py
More file actions
360 lines (273 loc) · 14.5 KB
/
test_update.py
File metadata and controls
360 lines (273 loc) · 14.5 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import json
import os
from copy import deepcopy
import jsonschema
import pytest
from marble_node_registry import update
GOOD_SERVICES = {
"services": [
{
"name": "geoserver",
"keywords": ["data", "service-wps", "service-wms", "service-wfs"],
"description": "GeoServer is a server that allows users to view and edit geospatial data.",
"links": [
{"rel": "service", "type": "application/json", "href": "https://daccs-uoft.example.com/geoserver/"},
{"rel": "service-doc", "type": "text/html", "href": "https://docs.geoserver.org/"},
],
},
{
"name": "weaver",
"keywords": ["service-ogcapi_processes"],
"description": "An OGC-API flavored Execution Management Service",
"links": [
{"rel": "service", "type": "application/json", "href": "https://daccs-uoft.example.com/weaver/"},
{"rel": "service-doc", "type": "text/html", "href": "https://pavics-weaver.readthedocs.io/"},
{
"rel": "http://www.opengis.net/def/rel/ogc/1.0/conformance",
"type": "application/json",
"href": "https://example.com/weaver/conformance/",
},
],
},
]
}
@pytest.fixture(autouse=True)
def updated_registry(mocker):
"""Mock the _write_registry function so that nothing is actually written to disk during the tests run"""
yield mocker.patch.object(update, "_write_registry")
@pytest.fixture(autouse=True)
def links_json_schema(requests_mock, request):
"""
Mock the `requests.get` call to the json-schema links hyper-schema so that these tests can be run offline if needed.
"""
this_dir = os.path.dirname(request.fspath)
with open(os.path.join(this_dir, "fixtures", "links-schema-cache.json")) as f:
content = f.read()
requests_mock.get("https://json-schema.org/draft/2020-12/links", text=content)
@pytest.fixture
def patched_registry(mocker):
"""Mock the _load_registry function so that the original registry content can be manipulated for the test"""
yield mocker.patch.object(update, "_load_registry")
@pytest.fixture
def example_registry_content(request):
"""Return the content contained in ../doc/node_registry.example.json"""
root_dir = os.path.dirname(os.path.dirname(request.fspath))
example_registry = os.path.join(root_dir, "doc", "node_registry.example.json")
with open(example_registry) as f:
content = json.load(f)
return content
@pytest.fixture
def example_initial_registry_content(example_registry_content):
"""
Return the content contained in ../doc/node_registry.example.json without the content that would not be updated
until the first update has run
"""
for key in example_registry_content:
data = example_registry_content[key]
data.pop("services")
data.pop("last_updated")
data.pop("version")
data.pop("status")
return example_registry_content
@pytest.fixture
def example_initial_registry(patched_registry, example_initial_registry_content):
"""
Return a patched version of the _load_registry function that returns the content of
example_initial_registry_content
"""
patched_registry.return_value = deepcopy(example_initial_registry_content)
return patched_registry
@pytest.fixture
def example_registry(patched_registry, example_registry_content):
"""Return a patched version of the _load_registry function that returns the content of example_registry_content"""
patched_registry.return_value = deepcopy(example_registry_content)
return patched_registry
@pytest.fixture
def example_node_name(example_registry_content):
"""Return the name of the node in example_registry_content"""
return list(example_registry_content)[0]
@pytest.fixture
def node_registry_schema():
return update._load_schema()
class TestEmptyRegistry:
"""Test when the registry is initially empty"""
@pytest.fixture(autouse=True)
def setup(self, patched_registry):
patched_registry.return_value = {}
update.update_registry()
def test_nothing_updated(self, updated_registry):
"""Test that the registry did not change"""
assert updated_registry.call_args.args == ({},)
class TestOfflineNode:
"""Test when the node is offline (unavailable over the network)"""
@pytest.fixture(autouse=True)
def setup(self, mocker, example_registry):
mocker.patch.object(update.requests, "get").side_effect = update.requests.exceptions.ConnectionError("message")
update.update_registry()
def test_status_offline(self, example_node_name, example_registry_content, updated_registry):
"""Test that the status is set to 'offline'"""
assert updated_registry.call_args.args[0][example_node_name]["status"] == "offline"
def test_services_no_change(self, example_node_name, example_registry_content, updated_registry):
"""Test that the services values did not change"""
assert (
updated_registry.call_args.args[0][example_node_name]["services"]
== example_registry_content[example_node_name]["services"]
)
def test_version_no_change(self, example_node_name, example_registry_content, updated_registry):
"""Test that the version value did not change"""
assert (
updated_registry.call_args.args[0][example_node_name]["version"]
== example_registry_content[example_node_name]["version"]
)
def test_last_updated_no_change(self, example_node_name, example_registry_content, updated_registry):
"""Test that the last_updated did not change"""
assert (
updated_registry.call_args.args[0][example_node_name]["last_updated"]
== example_registry_content[example_node_name]["last_updated"]
)
class TestNodeReturnsInvalidJson:
"""Test when the /services route returns a string that is not parseable as valid json"""
@pytest.fixture(autouse=True)
def setup(self, example_node_name, example_registry, example_registry_content, requests_mock):
services_url = next(
link["href"] for link in example_registry_content[example_node_name]["links"] if link["rel"] == "collection"
)
version_url = next(
link["href"] for link in example_registry_content[example_node_name]["links"] if link["rel"] == "version"
)
requests_mock.get(services_url, text='{"a":')
requests_mock.get(version_url, text="1.2.3")
update.update_registry()
def test_status_unresponsive(self, example_node_name, example_registry_content, updated_registry):
"""Test that the status is updated to 'unresponsive'"""
assert updated_registry.call_args.args[0][example_node_name]["status"] == "unresponsive"
def test_services_no_change(self, example_node_name, example_registry_content, updated_registry):
"""Test that the services values did not change"""
assert (
updated_registry.call_args.args[0][example_node_name]["services"]
== example_registry_content[example_node_name]["services"]
)
def test_version_no_change(self, example_node_name, example_registry_content, updated_registry):
"""Test that the version value did not change"""
assert (
updated_registry.call_args.args[0][example_node_name]["version"]
== example_registry_content[example_node_name]["version"]
)
def test_last_updated_no_change(self, example_node_name, example_registry_content, updated_registry):
"""Test that the last_updated value did not change"""
assert (
updated_registry.call_args.args[0][example_node_name]["last_updated"]
== example_registry_content[example_node_name]["last_updated"]
)
class InitialTests:
"""Abstract test class used to test when no updates have previously been run"""
services: dict
version: dict = {"version": "1.2.3"}
@pytest.fixture(autouse=True)
def setup(self, example_node_name, example_initial_registry, example_registry_content, requests_mock):
services_url = next(
link["href"] for link in example_registry_content[example_node_name]["links"] if link["rel"] == "collection"
)
version_url = next(
link["href"] for link in example_registry_content[example_node_name]["links"] if link["rel"] == "version"
)
services = deepcopy(self.services)
for s in services.values():
if isinstance(s, dict) and "date_added" in s:
s.pop("date_added")
requests_mock.get(services_url, json=self.services)
requests_mock.get(version_url, json=self.version)
update.update_registry()
class NonInitialTests:
"""Abstract test class used to test when updates have previously been run"""
services: dict
version: dict = {"version": "1.2.3"}
@pytest.fixture(autouse=True)
def setup(self, example_node_name, example_registry, example_registry_content, requests_mock):
services_url = next(
link["href"] for link in example_registry_content[example_node_name]["links"] if link["rel"] == "collection"
)
version_url = next(
link["href"] for link in example_registry_content[example_node_name]["links"] if link["rel"] == "version"
)
requests_mock.get(services_url, json=self.services)
requests_mock.get(version_url, json=self.version)
update.update_registry()
class ValidResponseTests:
"""Abstract test class used to test when the updates are expected to complete successfully for all nodes"""
services: dict
version: dict
def test_status_online(self, example_node_name, updated_registry):
"""Test that the status is updated to 'online'"""
assert updated_registry.call_args.args[0][example_node_name]["status"] == "online"
def test_services_updated(self, example_node_name, updated_registry):
"""Test that the services values are updated"""
assert updated_registry.call_args.args[0][example_node_name]["services"] == self.services["services"]
def test_version_updated(self, example_node_name, updated_registry):
"""Test that the version value is updated"""
assert updated_registry.call_args.args[0][example_node_name]["version"] == self.version["version"]
def test_last_updated_updated(self, example_node_name, example_registry_content, updated_registry):
"""Test that the last_updated value is updated"""
assert updated_registry.call_args.args[0][example_node_name]["last_updated"] != example_registry_content[
example_node_name
].get("last_updated")
def test_final_registry_valid(self, updated_registry, node_registry_schema):
jsonschema.validate(instance=updated_registry.call_args.args[0], schema=node_registry_schema)
class InvalidResponseTests:
"""
Abstract test class used to test when the updates are not expected to complete successfully for all nodes because
resulting registry content is invalid according to the json schema
"""
services: dict
version: dict
def test_status_invalid_configuration(self, example_node_name, updated_registry):
"""Test that the status is updated to 'invalid_configuration'"""
assert updated_registry.call_args.args[0][example_node_name]["status"] == "invalid_configuration"
def test_services_no_change(self, example_node_name, example_registry_content, updated_registry):
"""Test that the services values are not updated"""
assert updated_registry.call_args.args[0][example_node_name].get("services") == example_registry_content[
example_node_name
].get("services")
def test_version_no_change(self, example_node_name, example_registry_content, updated_registry):
"""Test that the version value is not updated"""
assert updated_registry.call_args.args[0][example_node_name].get("version", {}) == example_registry_content[
example_node_name
].get("version", {})
def test_last_updated_no_change(self, example_node_name, example_registry_content, updated_registry):
"""Test that the last_updated value is not updated"""
assert updated_registry.call_args.args[0][example_node_name].get("last_updated") == example_registry_content[
example_node_name
].get("last_updated")
def test_final_registry_valid(self, updated_registry, node_registry_schema):
jsonschema.validate(instance=updated_registry.call_args.args[0], schema=node_registry_schema)
class TestInitialUpdateNoServices(ValidResponseTests, InitialTests):
"""Test when no updates have previously been run and there are no reported services"""
services = {"services": []}
class TestInitialUpdateInvalidServices(InvalidResponseTests, InitialTests):
"""
Test when no updates have previously been run and the reported services are invalid according to the json schema
"""
services = {"services": [{"bad_key": "some_value"}]}
class TestOnlineNodeInitialUpdateWithValidServicesAndVersion(ValidResponseTests, InitialTests):
"""Test when no updates have previously been run and the reported services and version are valid"""
services = GOOD_SERVICES
class TestOnlineNodeInitialUpdateWithInvalidVersion(InvalidResponseTests, InitialTests):
"""Test when no updates have previously been run and the reported version is not valid"""
services = GOOD_SERVICES
version = {"version": "abc123"}
class TestOnlineNodeUpdateWithValidServicesAndVersion(ValidResponseTests, NonInitialTests):
"""Test when updates have previously been run and the reported services and version are valid"""
services = GOOD_SERVICES
class TestOnlineNodeUpdateWithInvalidVersion(InvalidResponseTests, NonInitialTests):
"""Test when updates have previously been run and the reported version is not valid"""
services = GOOD_SERVICES
version = {"version": "abc123"}
class TestOnlineNodeUpdateWithInvalidServices(InvalidResponseTests, NonInitialTests):
"""Test when updates have previously been run and the reported services are not valid"""
services = {"services": [{"bad_key": "some_value"}]}
class TestOnlineNodeUpdateWithInvalidServiceKeywords(InvalidResponseTests, NonInitialTests):
"""Test when updates have previously been run and the reported services keywords are not valid"""
services = deepcopy(GOOD_SERVICES)
@pytest.fixture(scope="class", autouse=True)
def bad_keywords(self):
self.services["services"][0]["keywords"] = ["something-bad"]