|
14 | 14 |
|
15 | 15 | import unittest
|
16 | 16 |
|
17 |
| -from mock import Mock |
| 17 | +from mock import Mock, call |
18 | 18 |
|
19 | 19 | from .watch import Watch
|
| 20 | +callcount = 0 |
20 | 21 |
|
21 | 22 |
|
22 | 23 | class WatchTests(unittest.TestCase):
|
@@ -62,6 +63,68 @@ def test_watch_with_decode(self):
|
62 | 63 | fake_resp.close.assert_called_once()
|
63 | 64 | fake_resp.release_conn.assert_called_once()
|
64 | 65 |
|
| 66 | + def test_watch_resource_version_set(self): |
| 67 | + # gh-700 ensure watching from a resource version does reset to resource |
| 68 | + # version 0 after k8s resets the watch connection |
| 69 | + fake_resp = Mock() |
| 70 | + fake_resp.close = Mock() |
| 71 | + fake_resp.release_conn = Mock() |
| 72 | + values = [ |
| 73 | + '{"type": "ADDED", "object": {"metadata": {"name": "test1",' |
| 74 | + '"resourceVersion": "1"}, "spec": {}, "status": {}}}\n', |
| 75 | + '{"type": "ADDED", "object": {"metadata": {"name": "test2",' |
| 76 | + '"resourceVersion": "2"}, "spec": {}, "sta', |
| 77 | + 'tus": {}}}\n' |
| 78 | + '{"type": "ADDED", "object": {"metadata": {"name": "test3",' |
| 79 | + '"resourceVersion": "3"}, "spec": {}, "status": {}}}\n' |
| 80 | + ] |
| 81 | + # return nothing on the first call and values on the second |
| 82 | + # this emulates a watch from a rv that returns nothing in the first k8s |
| 83 | + # watch reset and values later |
| 84 | + def get_values(*args, **kwargs): |
| 85 | + global callcount |
| 86 | + callcount += 1 |
| 87 | + if callcount == 1: |
| 88 | + return [] |
| 89 | + else: |
| 90 | + return values |
| 91 | + |
| 92 | + fake_resp.read_chunked = Mock( |
| 93 | + side_effect=get_values) |
| 94 | + |
| 95 | + fake_api = Mock() |
| 96 | + fake_api.get_namespaces = Mock(return_value=fake_resp) |
| 97 | + fake_api.get_namespaces.__doc__ = ':return: V1NamespaceList' |
| 98 | + |
| 99 | + w = Watch() |
| 100 | + count = 1 |
| 101 | + # ensure we keep our requested resource version or the version latest |
| 102 | + # returned version when the existing versions are older than the |
| 103 | + # requested version |
| 104 | + # needed for the list existing objects, then watch from there use case |
| 105 | + calls = [] |
| 106 | + first = True |
| 107 | + for e in w.stream(fake_api.get_namespaces, resource_version="5"): |
| 108 | + count += 1 |
| 109 | + # first call must use the passed rv |
| 110 | + # following calls the last rv of the returned values |
| 111 | + if count % 3 == 0: |
| 112 | + if first: |
| 113 | + rv = "5" |
| 114 | + first = False |
| 115 | + else: |
| 116 | + # ideally we want 5 here but as rv must be treated as an |
| 117 | + # opaque value we cannot interpret it and order it so rely |
| 118 | + # on k8s returning the events completely and in order |
| 119 | + rv = "3" |
| 120 | + calls.append(call(_preload_content=False, watch=True, |
| 121 | + resource_version=rv)) |
| 122 | + # returned |
| 123 | + if count == len(values) * 3: |
| 124 | + w.stop() |
| 125 | + |
| 126 | + fake_api.get_namespaces.assert_has_calls(calls) |
| 127 | + |
65 | 128 | def test_watch_stream_twice(self):
|
66 | 129 | w = Watch(float)
|
67 | 130 | for step in ['first', 'second']:
|
|
0 commit comments