|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# 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, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | +import uuid |
| 17 | + |
| 18 | +from kubernetes import watch |
| 19 | +from kubernetes.client import api_client |
| 20 | +from kubernetes.client.api import core_v1_api |
| 21 | +from kubernetes.e2e_test import base |
| 22 | + |
| 23 | + |
| 24 | +def short_uuid(): |
| 25 | + id = str(uuid.uuid4()) |
| 26 | + return id[-12:] |
| 27 | + |
| 28 | + |
| 29 | +def config_map_with_value(name, value): |
| 30 | + return { |
| 31 | + "apiVersion": "v1", |
| 32 | + "kind": "ConfigMap", |
| 33 | + "metadata": { |
| 34 | + "name": name, |
| 35 | + }, |
| 36 | + "data": { |
| 37 | + "key": value, |
| 38 | + "config": "dummy", |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | +class TestClient(unittest.TestCase): |
| 44 | + |
| 45 | + @classmethod |
| 46 | + def setUpClass(cls): |
| 47 | + cls.config = base.get_e2e_configuration() |
| 48 | + |
| 49 | + def test_watch_configmaps(self): |
| 50 | + client = api_client.ApiClient(configuration=self.config) |
| 51 | + api = core_v1_api.CoreV1Api(client) |
| 52 | + |
| 53 | + # create a configmap |
| 54 | + name_a = 'configmap-a-' + short_uuid() |
| 55 | + configmap_a = config_map_with_value(name_a, "a") |
| 56 | + api.create_namespaced_config_map( |
| 57 | + body=configmap_a, namespace='default') |
| 58 | + |
| 59 | + # list all configmaps and extract the resource version |
| 60 | + resp = api.list_namespaced_config_map('default') |
| 61 | + rv = resp.metadata.resource_version |
| 62 | + |
| 63 | + # create another configmap |
| 64 | + name_b = 'configmap-b-' + short_uuid() |
| 65 | + configmap_b = config_map_with_value(name_b, "b") |
| 66 | + api.create_namespaced_config_map( |
| 67 | + body=configmap_b, namespace='default') |
| 68 | + |
| 69 | + # patch configmap b |
| 70 | + configmap_b['data']['config'] = "{}" |
| 71 | + api.patch_namespaced_config_map( |
| 72 | + name=name_b, namespace='default', body=configmap_b) |
| 73 | + |
| 74 | + # delete all configmaps |
| 75 | + api.delete_collection_namespaced_config_map( |
| 76 | + namespace='default') |
| 77 | + |
| 78 | + w = watch.Watch() |
| 79 | + # expect to observe all events happened after the initial LIST |
| 80 | + expect = ['ADDED', 'MODIFIED', 'DELETED', 'DELETED'] |
| 81 | + i = 0 |
| 82 | + # start watching with the resource version we got from the LIST |
| 83 | + for event in w.stream(api.list_namespaced_config_map, |
| 84 | + namespace='default', |
| 85 | + resource_version=rv, |
| 86 | + timeout_seconds=5): |
| 87 | + self.assertEqual(event['type'], expect[i]) |
| 88 | + # Kubernetes doesn't guarantee the order of the two objects |
| 89 | + # being deleted |
| 90 | + if i < 2: |
| 91 | + self.assertEqual(event['object'].metadata.name, name_b) |
| 92 | + i = i + 1 |
| 93 | + |
| 94 | + self.assertEqual(i, 4) |
0 commit comments