12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ import asyncio
15
16
import json
16
17
17
- from asynctest import CoroutineMock , Mock , TestCase
18
+ from asynctest import CoroutineMock , Mock , TestCase , call
18
19
19
20
import kubernetes_asyncio
20
21
from kubernetes_asyncio .watch import Watch
@@ -29,7 +30,8 @@ async def test_watch_with_decode(self):
29
30
{
30
31
"type" : "ADDED" ,
31
32
"object" : {
32
- "metadata" : {"name" : "test{}" .format (uid )},
33
+ "metadata" : {"name" : "test{}" .format (uid ),
34
+ "resourceVersion" : str (uid )},
33
35
"spec" : {}, "status" : {}
34
36
}
35
37
}
@@ -49,6 +51,9 @@ async def test_watch_with_decode(self):
49
51
self .assertEqual ("ADDED" , e ['type' ])
50
52
# make sure decoder worked and we got a model with the right name
51
53
self .assertEqual ("test%d" % count , e ['object' ].metadata .name )
54
+ # make sure decoder worked and updated Watch.resource_version
55
+ self .assertEqual (e ['object' ].metadata .resource_version , str (count ))
56
+ self .assertEqual (watch .resource_version , str (count ))
52
57
53
58
# Stop the watch. This must not return the next event which would
54
59
# be an AssertionError exception.
@@ -127,6 +132,19 @@ async def test_unmarshall_k8s_error_response(self):
127
132
self .assertEqual (ret ['object' ], k8s_err ['object' ])
128
133
self .assertEqual (ret ['object' ], k8s_err ['object' ])
129
134
135
+ def test_unmarshal_with_custom_object (self ):
136
+ w = Watch ()
137
+ event = w .unmarshal_event ('{"type": "ADDED", "object": {"apiVersion":'
138
+ '"test.com/v1beta1","kind":"foo","metadata":'
139
+ '{"name": "bar", "resourceVersion": "1"}}}' ,
140
+ 'object' )
141
+ self .assertEqual ("ADDED" , event ['type' ])
142
+ # make sure decoder deserialized json into dictionary and updated
143
+ # Watch.resource_version
144
+ self .assertTrue (isinstance (event ['object' ], dict ))
145
+ self .assertEqual ("1" , event ['object' ]['metadata' ]['resourceVersion' ])
146
+ self .assertEqual ("1" , w .resource_version )
147
+
130
148
async def test_watch_with_exception (self ):
131
149
fake_resp = CoroutineMock ()
132
150
fake_resp .content .readline = CoroutineMock ()
@@ -140,6 +158,32 @@ async def test_watch_with_exception(self):
140
158
async for e in watch .stream (fake_api .get_namespaces , timeout_seconds = 10 ): # noqa
141
159
pass
142
160
161
+ async def test_watch_timeout (self ):
162
+ fake_resp = CoroutineMock ()
163
+ fake_resp .content .readline = CoroutineMock ()
164
+
165
+ mock_event = {"type" : "ADDED" ,
166
+ "object" : {"metadata" : {"name" : "test1555" ,
167
+ "resourceVersion" : "1555" },
168
+ "spec" : {},
169
+ "status" : {}}}
170
+
171
+ fake_resp .content .readline .side_effect = [json .dumps (mock_event ).encode ('utf8' ),
172
+ asyncio .TimeoutError (),
173
+ b"" ]
174
+
175
+ fake_api = Mock ()
176
+ fake_api .get_namespaces = CoroutineMock (return_value = fake_resp )
177
+ fake_api .get_namespaces .__doc__ = ':return: V1NamespaceList'
178
+
179
+ watch = kubernetes_asyncio .watch .Watch ()
180
+ async for e in watch .stream (fake_api .get_namespaces ): # noqa
181
+ pass
182
+
183
+ fake_api .get_namespaces .assert_has_calls (
184
+ [call (_preload_content = False , watch = True ),
185
+ call (_preload_content = False , watch = True , resource_version = '1555' )])
186
+
143
187
144
188
if __name__ == '__main__' :
145
189
import asynctest
0 commit comments