|  | 
| 19 | 19 | 
 | 
| 20 | 20 | from kubernetes.client import api_client | 
| 21 | 21 | from kubernetes.client.apis import core_v1_api | 
|  | 22 | +from kubernetes.client.apis import apiextensions_v1beta1_api | 
|  | 23 | +from kubernetes.client.apis import custom_objects_api | 
| 22 | 24 | from kubernetes.e2e_test import base | 
| 23 | 25 | from kubernetes.stream import stream | 
| 24 | 26 | from kubernetes.stream.ws_client import ERROR_CHANNEL | 
| @@ -234,3 +236,63 @@ def test_node_apis(self): | 
| 234 | 236 |             node = api.read_node(name=item.metadata.name) | 
| 235 | 237 |             self.assertTrue(len(node.metadata.labels) > 0) | 
| 236 | 238 |             self.assertTrue(isinstance(node.metadata.labels, dict)) | 
|  | 239 | + | 
|  | 240 | +    def test_custom_objects_apis(self): | 
|  | 241 | +        """ | 
|  | 242 | +        It should be able to create and patch a namespaced custom object. | 
|  | 243 | +        """ | 
|  | 244 | +        client = api_client.ApiClient(configuration=self.config) | 
|  | 245 | +        crd_api = apiextensions_v1beta1_api.ApiextensionsV1beta1Api(client) | 
|  | 246 | +        cr_api = custom_objects_api.CustomObjectsApi(client) | 
|  | 247 | + | 
|  | 248 | +        # create a CRD that defines a namespace-scoped custom resource Foo | 
|  | 249 | +        # TODO: The test objects in this test file are unstructured. Verify | 
|  | 250 | +        # if it makes more sense to switch to typed objects defined under | 
|  | 251 | +        # kubernetes.client.models | 
|  | 252 | +        group_prefix = 'test-crd-' + short_uuid() | 
|  | 253 | +        group = group_prefix + '.test.e2e.com' | 
|  | 254 | +        api_version = group + '/v1' | 
|  | 255 | +        name = 'foos.' + group | 
|  | 256 | +        test_crd = { | 
|  | 257 | +            'kind': 'CustomResourceDefinition', | 
|  | 258 | +            'apiVersion': 'apiextensions.k8s.io/v1beta1', | 
|  | 259 | +            'metadata': { | 
|  | 260 | +                'name': name, | 
|  | 261 | +            }, | 
|  | 262 | +            'spec': { | 
|  | 263 | +                'group': group, | 
|  | 264 | +                'versions': [{ | 
|  | 265 | +                    'name': 'v1', | 
|  | 266 | +                    'served': True, | 
|  | 267 | +                    'storage': True | 
|  | 268 | +                }], | 
|  | 269 | +                'names': { | 
|  | 270 | +                    'kind': 'Foo', | 
|  | 271 | +                    'plural': 'foos' | 
|  | 272 | +                }, | 
|  | 273 | +                'scope': 'Namespaced' | 
|  | 274 | +            } | 
|  | 275 | +        } | 
|  | 276 | +        resp = crd_api.create_custom_resource_definition(body=test_crd) | 
|  | 277 | +        self.assertEqual(name, resp.metadata.name) | 
|  | 278 | +        print('E2E test CRD created') | 
|  | 279 | + | 
|  | 280 | +        # wait for the CRD to be ready | 
|  | 281 | +        time.sleep(5) | 
|  | 282 | + | 
|  | 283 | +        # create a custom object | 
|  | 284 | +        name = 'test-foo-' + short_uuid() | 
|  | 285 | +        resp = cr_api.create_namespaced_custom_object(group=group, | 
|  | 286 | +            version='v1', | 
|  | 287 | +            plural='foos', | 
|  | 288 | +            namespace='default', | 
|  | 289 | +            body={'kind':'Foo', 'apiVersion':api_version, 'metadata':{'name':name}}) | 
|  | 290 | +        print('E2E test CR created') | 
|  | 291 | + | 
|  | 292 | +        # perform an emtpy JSON merge patch on the custom object | 
|  | 293 | +        resp = cr_api.patch_namespaced_custom_object(group=group, | 
|  | 294 | +            version='v1', | 
|  | 295 | +            plural='foos', | 
|  | 296 | +            namespace='default', | 
|  | 297 | +            name=name, | 
|  | 298 | +            body={}) | 
0 commit comments