|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import json |
15 | 16 | import os |
16 | 17 | import time |
17 | 18 | from types import SimpleNamespace |
@@ -613,45 +614,49 @@ def test_pod_log_empty_lines(self): |
613 | 614 | self.api.delete_namespaced_pod(name=pod_name, namespace=self.namespace) |
614 | 615 | self.api.delete_namespaced_pod.assert_called_once_with(name=pod_name, namespace=self.namespace) |
615 | 616 |
|
616 | | -# Comment out the test below, it does not work currently. |
617 | | -# def test_watch_with_deserialize_param(self): |
618 | | -# """test watch.stream() deserialize param""" |
619 | | -# # prepare test data |
620 | | -# test_json = '{"type": "ADDED", "object": {"metadata": {"name": "test1", "resourceVersion": "1"}, "spec": {}, "status": {}}}' |
621 | | -# fake_resp = Mock() |
622 | | -# fake_resp.close = Mock() |
623 | | -# fake_resp.release_conn = Mock() |
624 | | -# fake_resp.stream = Mock(return_value=[test_json + '\n']) |
625 | | -# |
626 | | -# fake_api = Mock() |
627 | | -# fake_api.get_namespaces = Mock(return_value=fake_resp) |
628 | | -# fake_api.get_namespaces.__doc__ = ':rtype: V1NamespaceList' |
629 | | -# |
630 | | -# # test case with deserialize=True |
631 | | -# w = Watch() |
632 | | -# for e in w.stream(fake_api.get_namespaces, deserialize=True): |
633 | | -# self.assertEqual("ADDED", e['type']) |
634 | | -# # Verify that the object is deserialized correctly |
635 | | -# self.assertTrue(hasattr(e['object'], 'metadata')) |
636 | | -# self.assertEqual("test1", e['object'].metadata.name) |
637 | | -# self.assertEqual("1", e['object'].metadata.resource_version) |
638 | | -# # Verify that the original object is saved |
639 | | -# self.assertEqual(json.loads(test_json)['object'], e['raw_object']) |
640 | | -# |
641 | | -# # test case with deserialize=False |
642 | | -# w = Watch() |
643 | | -# for e in w.stream(fake_api.get_namespaces, deserialize=False): |
644 | | -# self.assertEqual("ADDED", e['type']) |
645 | | -# # The validation object remains in the original dictionary format |
646 | | -# self.assertIsInstance(e['object'], dict) |
647 | | -# self.assertEqual("test1", e['object']['metadata']['name']) |
648 | | -# self.assertEqual("1", e['object']['metadata']['resourceVersion']) |
649 | | -# |
650 | | -# # verify the api is called twice |
651 | | -# fake_api.get_namespaces.assert_has_calls([ |
652 | | -# call(_preload_content=False, watch=True), |
653 | | -# call(_preload_content=False, watch=True) |
654 | | -# ]) |
| 617 | + def test_watch_with_deserialize_param(self): |
| 618 | + """test watch.stream() deserialize param""" |
| 619 | + # prepare test data |
| 620 | + test_json = '{"type": "ADDED", "object": {"metadata": {"name": "test1", "resourceVersion": "1"}, "spec": {}, "status": {}}}' |
| 621 | + fake_resp = Mock() |
| 622 | + fake_resp.close = Mock() |
| 623 | + fake_resp.release_conn = Mock() |
| 624 | + fake_resp.stream = Mock(return_value=[test_json + '\n']) |
| 625 | + |
| 626 | + fake_api = Mock() |
| 627 | + fake_api.get_namespaces = Mock(return_value=fake_resp) |
| 628 | + fake_api.get_namespaces.__doc__ = ':rtype: V1NamespaceList' |
| 629 | + |
| 630 | + # test case with deserialize=True |
| 631 | + w = Watch() |
| 632 | + count = 0 |
| 633 | + for e in w.stream(fake_api.get_namespaces, deserialize=True): |
| 634 | + self.assertEqual("ADDED", e['type']) |
| 635 | + # Verify that the object is deserialized correctly |
| 636 | + self.assertTrue(hasattr(e['object'], 'metadata')) |
| 637 | + self.assertEqual("test1", e['object'].metadata.name) |
| 638 | + self.assertEqual("1", e['object'].metadata.resource_version) |
| 639 | + # Verify that the original object is saved |
| 640 | + self.assertEqual(json.loads(test_json)['object'], e['raw_object']) |
| 641 | + count += 1 |
| 642 | + if count == 1: |
| 643 | + w.stop() |
| 644 | + self.assertEqual(1, count) |
| 645 | + |
| 646 | + # test case with deserialize=False |
| 647 | + w = Watch() |
| 648 | + for e in w.stream(fake_api.get_namespaces, deserialize=False): |
| 649 | + self.assertEqual("ADDED", e['type']) |
| 650 | + # The validation object remains in the original dictionary format |
| 651 | + self.assertIsInstance(e['object'], dict) |
| 652 | + self.assertEqual("test1", e['object']['metadata']['name']) |
| 653 | + self.assertEqual("1", e['object']['metadata']['resourceVersion']) |
| 654 | + |
| 655 | + # verify the api is called twice |
| 656 | + fake_api.get_namespaces.assert_has_calls([ |
| 657 | + call(_preload_content=False, watch=True), |
| 658 | + call(_preload_content=False, watch=True) |
| 659 | + ]) |
655 | 660 |
|
656 | 661 |
|
657 | 662 | if __name__ == '__main__': |
|
0 commit comments