-
-
Notifications
You must be signed in to change notification settings - Fork 0
add tests for controller (kopf) #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6025299
add tests for controller (kopf)
Ziul 2d21175
add tests for controller (kopf)
Ziul 9b18f56
add tests for controller (kopf)
Ziul c4927c3
add tests for controller (kopf)
Ziul 3913313
add tests for controller (kopf)
Ziul 9182ef1
add tests for controller (kopf)
Ziul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.