-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_controller.py
More file actions
99 lines (91 loc) · 3.08 KB
/
test_controller.py
File metadata and controls
99 lines (91 loc) · 3.08 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
from unittest.mock import MagicMock, patch
import pytest
from controller import service_event
@pytest.fixture
def fake_memo():
class Memo:
apps = {}
return Memo()
def make_event(path):
return {
'type': 'ADDED',
'object': {
'metadata': {
'name': 'my-service',
'namespace': 'default',
'annotations': {
'swagger-operator-path': path,
'swagger-operator-name': 'my-app',
'swagger-operator-port': '8080',
'swagger-operator-header': 'X-API-KEY'
}
}
}
}
def test_path_without_slash(fake_memo):
# Path without leading slash
event = make_event("openapi.json")
logger = MagicMock()
with patch("builtins.open"), patch("json.dump"):
service_event(event, fake_memo, logger)
key = "default/my-app"
assert key in fake_memo.apps
# The final path should contain the original path
assert fake_memo.apps[key]['url'].endswith("/openapi.json")
def test_path_with_slash(fake_memo):
# Path with leading slash
event = make_event("/openapi.json")
logger = MagicMock()
with patch("builtins.open"), patch("json.dump"):
service_event(event, fake_memo, logger)
key = "default/my-app"
assert key in fake_memo.apps
assert fake_memo.apps[key]['url'].endswith("/openapi.json")
def test_path_with_host(fake_memo):
# Path with host
event = make_event("http://myhost/openapi.json")
logger = MagicMock()
with patch("builtins.open"), patch("json.dump"):
service_event(event, fake_memo, logger)
key = "default/my-app"
assert key in fake_memo.apps
# The host should be preserved
assert fake_memo.apps[key]['url'].startswith("http://myhost")
def test_missing_annotation(fake_memo):
# Missing annotation
event = make_event("/openapi.json")
del event['object']['metadata']['annotations']['swagger-operator-path']
logger = MagicMock()
with patch("builtins.open"), patch("json.dump"):
try:
service_event(event, fake_memo, logger)
assert False, "Should raise KeyError"
except KeyError:
pass
def test_service_event_deleted(fake_memo):
# Pre-add an app to memo
fake_memo.apps["default/my-app"] = {
"url": "http://dummy",
"name": "my-app",
"header": "X-API-KEY"
}
event = {
'type': 'DELETED',
'object': {
'metadata': {
'name': 'my-service',
'namespace': 'default',
'annotations': {
'swagger-operator-path': '/openapi.json',
'swagger-operator-name': 'my-app',
'swagger-operator-port': '8080',
'swagger-operator-header': 'X-API-KEY'
}
}
}
}
logger = MagicMock()
with patch("builtins.open"), patch("json.dump"):
service_event(event, fake_memo, logger)
# Should remove the app from memo
assert "default/my-app" not in fake_memo.apps