Make duck-typing in serialize_body method more restrictive

This commit is contained in:
piglei 2021-08-22 11:20:59 +08:00
parent dd15ac6263
commit 66a45cd081
2 changed files with 36 additions and 1 deletions

View File

@ -98,7 +98,12 @@ class DynamicClient(object):
return namespace
def serialize_body(self, body):
if hasattr(body, 'to_dict'):
"""Serialize body to raw dict so apiserver can handle it
:param body: kubernetes resource body, current support: Union[Dict, ResourceInstance]
"""
# This should match any `ResourceInstance` instances
if callable(getattr(body, 'to_dict', None)):
return body.to_dict()
return body or {}

View File

@ -20,6 +20,7 @@ from kubernetes.e2e_test import base
from kubernetes.client import api_client
from . import DynamicClient
from .resource import ResourceInstance, ResourceField
from .exceptions import ResourceNotFoundError
@ -392,3 +393,32 @@ class TestDynamicClient(unittest.TestCase):
resp = api.get(**params)
self.assertEqual('PartialObjectMetadataList', resp.kind)
self.assertEqual('meta.k8s.io/v1', resp.apiVersion)
class TestDynamicClientSerialization(unittest.TestCase):
@classmethod
def setUpClass(cls):
config = base.get_e2e_configuration()
cls.client = DynamicClient(api_client.ApiClient(configuration=config))
cls.pod_manifest = {
'apiVersion': 'v1',
'kind': 'Pod',
'metadata': {'name': 'foo-pod'},
'spec': {'containers': [{'name': "main", 'image': "busybox"}]},
}
def test_dict_type(self):
self.assertEqual(self.client.serialize_body(self.pod_manifest), self.pod_manifest)
def test_resource_instance_type(self):
inst = ResourceInstance(self.client, self.pod_manifest)
self.assertEqual(self.client.serialize_body(inst), self.pod_manifest)
def test_resource_field(self):
"""`ResourceField` is a special type which overwrites `__getattr__` method to return `None`
when a non-existent attribute was accessed. which means it can pass any `hasattr(...)` tests.
"""
res = ResourceField(foo='bar')
# method will return original object when it doesn't know how to proceed
self.assertEqual(self.client.serialize_body(res), res)