-
Notifications
You must be signed in to change notification settings - Fork 75
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
Stop the iterator for empty responses and do not process ERROR responses #22
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6f8acee
Remove unused code lines.
olitheolix fb2f64e
`unmarshal_event` now return K8s ERROR responses verbatim.
olitheolix 61852ea
An empty K8s response now terminates the iterator.
olitheolix 341cde4
Explicitly test the resource_version argument.
olitheolix bdd1053
Removed unused import.
olitheolix 871e42a
Make code Python 3.5 compatible.
olitheolix ce3f4f1
PR feedback: use `assertEqual` instead of `assert`
olitheolix 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 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 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 |
---|---|---|
|
@@ -12,7 +12,9 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from asynctest import CoroutineMock, Mock, TestCase, patch | ||
import json | ||
|
||
from asynctest import CoroutineMock, Mock, TestCase | ||
|
||
import kubernetes_asyncio | ||
from kubernetes_asyncio.watch import Watch | ||
|
@@ -23,30 +25,68 @@ class WatchTest(TestCase): | |
async def test_watch_with_decode(self): | ||
fake_resp = CoroutineMock() | ||
fake_resp.content.readline = CoroutineMock() | ||
fake_resp.content.readline.side_effect = [ | ||
'{"type": "ADDED", "object": {"metadata": {"name": "test1"},"spec": {}, "status": {}}}', | ||
'{"type": "ADDED", "object": {"metadata": {"name": "test2"},"spec": {}, "status": {}}}', | ||
'{"type": "ADDED", "object": {"metadata": {"name": "test3"},"spec": {}, "status": {}}}', | ||
'should_not_happened'] | ||
side_effects = [ | ||
{ | ||
"type": "ADDED", | ||
"object": { | ||
"metadata": {"name": "test{}".format(uid)}, | ||
"spec": {}, "status": {} | ||
} | ||
} | ||
for uid in range(3) | ||
] | ||
side_effects = [json.dumps(_).encode('utf8') for _ in side_effects] | ||
side_effects.extend([AssertionError('Should not have been called')]) | ||
fake_resp.content.readline.side_effect = side_effects | ||
|
||
fake_api = Mock() | ||
fake_api.get_namespaces = CoroutineMock(return_value=fake_resp) | ||
fake_api.get_namespaces.__doc__ = ':return: V1NamespaceList' | ||
|
||
watch = kubernetes_asyncio.watch.Watch() | ||
count = 1 | ||
async for e in watch.stream(fake_api.get_namespaces): | ||
count = 0 | ||
async for e in watch.stream(fake_api.get_namespaces, resource_version='123'): | ||
self.assertEqual("ADDED", e['type']) | ||
# make sure decoder worked and we got a model with the right name | ||
self.assertEqual("test%d" % count, e['object'].metadata.name) | ||
|
||
# Stop the watch. This must not return the next event which would | ||
# be an AssertionError exception. | ||
count += 1 | ||
# make sure we can stop the watch and the last event with won't be | ||
# returned | ||
if count == 4: | ||
if count == len(side_effects) - 1: | ||
watch.stop() | ||
|
||
fake_api.get_namespaces.assert_called_once_with( | ||
_preload_content=False, watch=True) | ||
_preload_content=False, watch=True, resource_version='123') | ||
|
||
async def test_watch_k8s_empty_response(self): | ||
"""Stop the iterator when the response is empty. | ||
|
||
This typically happens when the user supplied timeout expires. | ||
|
||
""" | ||
# Mock the readline return value to first return a valid response | ||
# followed by an empty response. | ||
fake_resp = CoroutineMock() | ||
fake_resp.content.readline = CoroutineMock() | ||
side_effects = [ | ||
{"type": "ADDED", "object": {"metadata": {"name": "test0"}, "spec": {}, "status": {}}}, | ||
{"type": "ADDED", "object": {"metadata": {"name": "test1"}, "spec": {}, "status": {}}}, | ||
] | ||
side_effects = [json.dumps(_).encode('utf8') for _ in side_effects] | ||
fake_resp.content.readline.side_effect = side_effects + [b''] | ||
|
||
# Fake the K8s resource object to watch. | ||
fake_api = Mock() | ||
fake_api.get_namespaces = CoroutineMock(return_value=fake_resp) | ||
fake_api.get_namespaces.__doc__ = ':return: V1NamespaceList' | ||
|
||
# Iteration must cease after all valid responses were received. | ||
watch = kubernetes_asyncio.watch.Watch() | ||
cnt = 0 | ||
async for _ in watch.stream(fake_api.get_namespaces): | ||
cnt += 1 | ||
assert cnt == len(side_effects) | ||
|
||
def test_unmarshal_with_float_object(self): | ||
w = Watch() | ||
|
@@ -64,6 +104,28 @@ def test_unmarshal_with_no_return_type(self): | |
self.assertEqual(["test1"], event['object']) | ||
self.assertEqual(["test1"], event['raw_object']) | ||
|
||
async def test_unmarshall_k8s_error_response(self): | ||
"""Never parse messages of type ERROR. | ||
|
||
This test uses an actually recorded error, in this case for an outdated | ||
resource version. | ||
|
||
""" | ||
# An actual error response sent by K8s during testing. | ||
k8s_err = { | ||
'type': 'ERROR', | ||
'object': { | ||
'kind': 'Status', 'apiVersion': 'v1', 'metadata': {}, | ||
'status': 'Failure', | ||
'message': 'too old resource version: 1 (8146471)', | ||
'reason': 'Gone', 'code': 410 | ||
} | ||
} | ||
|
||
ret = Watch().unmarshal_event(json.dumps(k8s_err), None) | ||
assert ret['type'] == k8s_err['type'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using |
||
assert ret['object'] == ret['raw_object'] == k8s_err['object'] | ||
|
||
async def test_watch_with_exception(self): | ||
fake_resp = CoroutineMock() | ||
fake_resp.content.readline = CoroutineMock() | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.assertEqual
will be better here, it produces more meaningful errors.