Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ jobs:
name: Linting
- name: Bandit
uses: PyCQA/bandit-action@v1
with:
exclude: "tests,charts"
skips: B101
- name: Pytest
run: |
pip install poetry
Expand Down
4 changes: 2 additions & 2 deletions controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def configure(memo: kopf.Memo, **_):
async def cleanup_fn(logger, **kwargs):
logger.info("Cleaning up resources...")

@kopf.on.event('v1', 'services',
@kopf.on.event('v1', 'services',
annotations={os.environ.get('SWAGGER_OPERATOR_PATH_KEY', 'swagger-operator-path'):kopf.PRESENT},
)
def service_event(event, memo: kopf.Memo, logger, **kwargs):
Expand All @@ -34,7 +34,7 @@ def service_event(event, memo: kopf.Memo, logger, **kwargs):
if not application_url.netloc:
if not application_url.path.startswith('/'):
application_url = urlparse(f"/{application_url.path}")
application_url = urlparse(f"http://{service_name}.{namespace}.svc.cluster.local:{application_port}{application_path}")
application_url = urlparse(f"http://{service_name}.{namespace}.svc.cluster.local:{application_port}{application_url.path}")



Expand Down
99 changes: 99 additions & 0 deletions tests/test_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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