|
| 1 | +# Copyright 2016 The Kubernetes Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | + |
| 17 | +from mock import Mock |
| 18 | + |
| 19 | +from .watch import Watch |
| 20 | + |
| 21 | + |
| 22 | +class WatchTests(unittest.TestCase): |
| 23 | + |
| 24 | + def test_watch_with_decode(self): |
| 25 | + fake_resp = Mock() |
| 26 | + fake_resp.close = Mock() |
| 27 | + fake_resp.release_conn = Mock() |
| 28 | + fake_resp.read_chunked = Mock( |
| 29 | + return_value=[ |
| 30 | + '{"type": "ADDED", "object": {"metadata": {"name": "test1"}' |
| 31 | + ',"spec": {}, "status": {}}}\n', |
| 32 | + '{"type": "ADDED", "object": {"metadata": {"name": "test2"}' |
| 33 | + ',"spec": {}, "sta', |
| 34 | + 'tus": {}}}\n' |
| 35 | + '{"type": "ADDED", "object": {"metadata": {"name": "test3"},' |
| 36 | + '"spec": {}, "status": {}}}\n', |
| 37 | + 'should_not_happened\n']) |
| 38 | + |
| 39 | + fake_api = Mock() |
| 40 | + fake_api.get_namespaces = Mock(return_value=fake_resp) |
| 41 | + fake_api.get_namespaces.__doc__ = ':return: V1NamespaceList' |
| 42 | + |
| 43 | + w = Watch() |
| 44 | + count = 1 |
| 45 | + for e in w.stream(fake_api.get_namespaces): |
| 46 | + self.assertEqual("ADDED", e['type']) |
| 47 | + # make sure decoder worked and we got a model with the right name |
| 48 | + self.assertEqual("test%d" % count, e['object'].metadata.name) |
| 49 | + count += 1 |
| 50 | + # make sure we can stop the watch and the last event with won't be |
| 51 | + # returned |
| 52 | + if count == 4: |
| 53 | + w.stop() |
| 54 | + |
| 55 | + fake_api.get_namespaces.assert_called_once_with( |
| 56 | + _preload_content=False, watch=True) |
| 57 | + fake_resp.read_chunked.assert_called_once_with(decode_content=False) |
| 58 | + fake_resp.close.assert_called_once() |
| 59 | + fake_resp.release_conn.assert_called_once() |
| 60 | + |
| 61 | + def test_unmarshal_with_float_object(self): |
| 62 | + w = Watch() |
| 63 | + event = w.unmarshal_event('{"type": "ADDED", "object": 1}', 'float') |
| 64 | + self.assertEqual("ADDED", event['type']) |
| 65 | + self.assertEqual(1.0, event['object']) |
| 66 | + self.assertTrue(isinstance(event['object'], float)) |
| 67 | + self.assertEqual(1, event['raw_object']) |
| 68 | + |
| 69 | + def test_unmarshal_with_no_return_type(self): |
| 70 | + w = Watch() |
| 71 | + event = w.unmarshal_event('{"type": "ADDED", "object": ["test1"]}', |
| 72 | + None) |
| 73 | + self.assertEqual("ADDED", event['type']) |
| 74 | + self.assertEqual(["test1"], event['object']) |
| 75 | + self.assertEqual(["test1"], event['raw_object']) |
| 76 | + |
| 77 | + def test_watch_with_exception(self): |
| 78 | + fake_resp = Mock() |
| 79 | + fake_resp.close = Mock() |
| 80 | + fake_resp.release_conn = Mock() |
| 81 | + fake_resp.read_chunked = Mock(side_effect=KeyError('expected')) |
| 82 | + |
| 83 | + fake_api = Mock() |
| 84 | + fake_api.get_thing = Mock(return_value=fake_resp) |
| 85 | + |
| 86 | + w = Watch() |
| 87 | + try: |
| 88 | + for _ in w.stream(fake_api.get_thing): |
| 89 | + self.fail(self, "Should fail on exception.") |
| 90 | + except KeyError: |
| 91 | + pass |
| 92 | + # expected |
| 93 | + |
| 94 | + fake_api.get_thing.assert_called_once_with( |
| 95 | + _preload_content=False, watch=True) |
| 96 | + fake_resp.read_chunked.assert_called_once_with(decode_content=False) |
| 97 | + fake_resp.close.assert_called_once() |
| 98 | + fake_resp.release_conn.assert_called_once() |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == '__main__': |
| 102 | + unittest.main() |
0 commit comments